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 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 109 | position[0] = lis3->read_data(lis3, OUTX); |
| 110 | position[1] = lis3->read_data(lis3, OUTY); |
| 111 | position[2] = lis3->read_data(lis3, OUTZ); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 112 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 113 | *x = lis3lv02d_get_axis(lis3->ac.x, position); |
| 114 | *y = lis3lv02d_get_axis(lis3->ac.y, position); |
| 115 | *z = lis3lv02d_get_axis(lis3->ac.z, position); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame^] | 118 | /* conversion btw sampling rate and the register values */ |
| 119 | static int lis3_12_rates[4] = {40, 160, 640, 2560}; |
| 120 | static int lis3_8_rates[2] = {100, 400}; |
| 121 | |
| 122 | static int lis3lv02d_get_odr(void) |
| 123 | { |
| 124 | u8 ctrl; |
| 125 | int val; |
| 126 | |
| 127 | lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl); |
| 128 | |
| 129 | if (lis3_dev.whoami == WAI_12B) |
| 130 | val = lis3_12_rates[(ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4]; |
| 131 | else |
| 132 | val = lis3_8_rates[(ctrl & CTRL1_DR) >> 7]; |
| 133 | return val; |
| 134 | } |
| 135 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 136 | void lis3lv02d_poweroff(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 137 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 138 | /* disable X,Y,Z axis and power down */ |
| 139 | lis3->write(lis3, CTRL_REG1, 0x00); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 140 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 141 | EXPORT_SYMBOL_GPL(lis3lv02d_poweroff); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 142 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 143 | void lis3lv02d_poweron(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 144 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 145 | u8 reg; |
| 146 | |
| 147 | lis3->init(lis3); |
| 148 | |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame^] | 149 | /* LIS3 power on delay is quite long */ |
| 150 | msleep(lis3->pwron_delay / lis3lv02d_get_odr()); |
| 151 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 152 | /* |
| 153 | * Common configuration |
Éric Piel | 4b5d95b | 2009-12-14 18:01:40 -0800 | [diff] [blame] | 154 | * BDU: (12 bits sensors only) LSB and MSB values are not updated until |
| 155 | * both have been read. So the value read will always be correct. |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 156 | */ |
Éric Piel | 4b5d95b | 2009-12-14 18:01:40 -0800 | [diff] [blame] | 157 | if (lis3->whoami == WAI_12B) { |
| 158 | lis3->read(lis3, CTRL_REG2, ®); |
| 159 | reg |= CTRL2_BDU; |
| 160 | lis3->write(lis3, CTRL_REG2, reg); |
| 161 | } |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 162 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 163 | EXPORT_SYMBOL_GPL(lis3lv02d_poweron); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 164 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 165 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 166 | static irqreturn_t lis302dl_interrupt(int irq, void *dummy) |
| 167 | { |
| 168 | /* |
| 169 | * Be careful: on some HP laptops the bios force DD when on battery and |
| 170 | * the lid is closed. This leads to interrupts as soon as a little move |
| 171 | * is done. |
| 172 | */ |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 173 | atomic_inc(&lis3_dev.count); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 174 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 175 | wake_up_interruptible(&lis3_dev.misc_wait); |
| 176 | kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 177 | return IRQ_HANDLED; |
| 178 | } |
| 179 | |
| 180 | static int lis3lv02d_misc_open(struct inode *inode, struct file *file) |
| 181 | { |
| 182 | int ret; |
| 183 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 184 | if (test_and_set_bit(0, &lis3_dev.misc_opened)) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 185 | return -EBUSY; /* already open */ |
| 186 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 187 | atomic_set(&lis3_dev.count, 0); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * The sensor can generate interrupts for free-fall and direction |
| 191 | * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep |
| 192 | * the things simple and _fast_ we activate it only for free-fall, so |
| 193 | * no need to read register (very slow with ACPI). For the same reason, |
| 194 | * we forbid shared interrupts. |
| 195 | * |
| 196 | * IRQF_TRIGGER_RISING seems pointless on HP laptops because the |
| 197 | * io-apic is not configurable (and generates a warning) but I keep it |
| 198 | * in case of support for other hardware. |
| 199 | */ |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 200 | ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING, |
| 201 | DRIVER_NAME, &lis3_dev); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 202 | |
| 203 | if (ret) { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 204 | clear_bit(0, &lis3_dev.misc_opened); |
| 205 | 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] | 206 | return -EBUSY; |
| 207 | } |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int lis3lv02d_misc_release(struct inode *inode, struct file *file) |
| 212 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 213 | fasync_helper(-1, file, 0, &lis3_dev.async_queue); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 214 | free_irq(lis3_dev.irq, &lis3_dev); |
| 215 | clear_bit(0, &lis3_dev.misc_opened); /* release the device */ |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf, |
| 220 | size_t count, loff_t *pos) |
| 221 | { |
| 222 | DECLARE_WAITQUEUE(wait, current); |
| 223 | u32 data; |
| 224 | unsigned char byte_data; |
| 225 | ssize_t retval = 1; |
| 226 | |
| 227 | if (count < 1) |
| 228 | return -EINVAL; |
| 229 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 230 | add_wait_queue(&lis3_dev.misc_wait, &wait); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 231 | while (true) { |
| 232 | set_current_state(TASK_INTERRUPTIBLE); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 233 | data = atomic_xchg(&lis3_dev.count, 0); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 234 | if (data) |
| 235 | break; |
| 236 | |
| 237 | if (file->f_flags & O_NONBLOCK) { |
| 238 | retval = -EAGAIN; |
| 239 | goto out; |
| 240 | } |
| 241 | |
| 242 | if (signal_pending(current)) { |
| 243 | retval = -ERESTARTSYS; |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | schedule(); |
| 248 | } |
| 249 | |
| 250 | if (data < 255) |
| 251 | byte_data = data; |
| 252 | else |
| 253 | byte_data = 255; |
| 254 | |
| 255 | /* make sure we are not going into copy_to_user() with |
| 256 | * TASK_INTERRUPTIBLE state */ |
| 257 | set_current_state(TASK_RUNNING); |
| 258 | if (copy_to_user(buf, &byte_data, sizeof(byte_data))) |
| 259 | retval = -EFAULT; |
| 260 | |
| 261 | out: |
| 262 | __set_current_state(TASK_RUNNING); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 263 | remove_wait_queue(&lis3_dev.misc_wait, &wait); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 264 | |
| 265 | return retval; |
| 266 | } |
| 267 | |
| 268 | static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait) |
| 269 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 270 | poll_wait(file, &lis3_dev.misc_wait, wait); |
| 271 | if (atomic_read(&lis3_dev.count)) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 272 | return POLLIN | POLLRDNORM; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int lis3lv02d_misc_fasync(int fd, struct file *file, int on) |
| 277 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 278 | return fasync_helper(fd, file, on, &lis3_dev.async_queue); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static const struct file_operations lis3lv02d_misc_fops = { |
| 282 | .owner = THIS_MODULE, |
| 283 | .llseek = no_llseek, |
| 284 | .read = lis3lv02d_misc_read, |
| 285 | .open = lis3lv02d_misc_open, |
| 286 | .release = lis3lv02d_misc_release, |
| 287 | .poll = lis3lv02d_misc_poll, |
| 288 | .fasync = lis3lv02d_misc_fasync, |
| 289 | }; |
| 290 | |
| 291 | static struct miscdevice lis3lv02d_misc_device = { |
| 292 | .minor = MISC_DYNAMIC_MINOR, |
| 293 | .name = "freefall", |
| 294 | .fops = &lis3lv02d_misc_fops, |
| 295 | }; |
| 296 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 297 | static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 298 | { |
| 299 | int x, y, z; |
| 300 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 301 | lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z); |
| 302 | input_report_abs(pidev->input, ABS_X, x - lis3_dev.xcalib); |
| 303 | input_report_abs(pidev->input, ABS_Y, y - lis3_dev.ycalib); |
| 304 | input_report_abs(pidev->input, ABS_Z, z - lis3_dev.zcalib); |
Samu Onkalo | d25a8c8 | 2009-12-14 18:01:38 -0800 | [diff] [blame] | 305 | input_sync(pidev->input); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 308 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 309 | static inline void lis3lv02d_calibrate_joystick(void) |
| 310 | { |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 311 | lis3lv02d_get_xyz(&lis3_dev, |
| 312 | &lis3_dev.xcalib, &lis3_dev.ycalib, &lis3_dev.zcalib); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 315 | int lis3lv02d_joystick_enable(void) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 316 | { |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 317 | struct input_dev *input_dev; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 318 | int err; |
| 319 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 320 | if (lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 321 | return -EINVAL; |
| 322 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 323 | lis3_dev.idev = input_allocate_polled_device(); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 324 | if (!lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 325 | return -ENOMEM; |
| 326 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 327 | lis3_dev.idev->poll = lis3lv02d_joystick_poll; |
| 328 | lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL; |
| 329 | input_dev = lis3_dev.idev->input; |
| 330 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 331 | lis3lv02d_calibrate_joystick(); |
| 332 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 333 | input_dev->name = "ST LIS3LV02DL Accelerometer"; |
| 334 | input_dev->phys = DRIVER_NAME "/input0"; |
| 335 | input_dev->id.bustype = BUS_HOST; |
| 336 | input_dev->id.vendor = 0; |
| 337 | input_dev->dev.parent = &lis3_dev.pdev->dev; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 338 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 339 | set_bit(EV_ABS, input_dev->evbit); |
| 340 | input_set_abs_params(input_dev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3); |
| 341 | input_set_abs_params(input_dev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3); |
| 342 | 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] | 343 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 344 | err = input_register_polled_device(lis3_dev.idev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 345 | if (err) { |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 346 | input_free_polled_device(lis3_dev.idev); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 347 | lis3_dev.idev = NULL; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return err; |
| 351 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 352 | EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 353 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 354 | void lis3lv02d_joystick_disable(void) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 355 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 356 | if (!lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 357 | return; |
| 358 | |
Eric Piel | c288424 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 359 | if (lis3_dev.irq) |
| 360 | misc_deregister(&lis3lv02d_misc_device); |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 361 | input_unregister_polled_device(lis3_dev.idev); |
Samu Onkalo | 66c8569 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 362 | input_free_polled_device(lis3_dev.idev); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 363 | lis3_dev.idev = NULL; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 364 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 365 | EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 366 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 367 | /* Sysfs stuff */ |
| 368 | static ssize_t lis3lv02d_position_show(struct device *dev, |
| 369 | struct device_attribute *attr, char *buf) |
| 370 | { |
| 371 | int x, y, z; |
| 372 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 373 | lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 374 | return sprintf(buf, "(%d,%d,%d)\n", x, y, z); |
| 375 | } |
| 376 | |
| 377 | static ssize_t lis3lv02d_calibrate_show(struct device *dev, |
| 378 | struct device_attribute *attr, char *buf) |
| 379 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 380 | 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] | 381 | } |
| 382 | |
| 383 | static ssize_t lis3lv02d_calibrate_store(struct device *dev, |
| 384 | struct device_attribute *attr, |
| 385 | const char *buf, size_t count) |
| 386 | { |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 387 | lis3lv02d_calibrate_joystick(); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 388 | return count; |
| 389 | } |
| 390 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 391 | static ssize_t lis3lv02d_rate_show(struct device *dev, |
| 392 | struct device_attribute *attr, char *buf) |
| 393 | { |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame^] | 394 | return sprintf(buf, "%d\n", lis3lv02d_get_odr()); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL); |
| 398 | static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show, |
| 399 | lis3lv02d_calibrate_store); |
| 400 | static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL); |
| 401 | |
| 402 | static struct attribute *lis3lv02d_attributes[] = { |
| 403 | &dev_attr_position.attr, |
| 404 | &dev_attr_calibrate.attr, |
| 405 | &dev_attr_rate.attr, |
| 406 | NULL |
| 407 | }; |
| 408 | |
| 409 | static struct attribute_group lis3lv02d_attribute_group = { |
| 410 | .attrs = lis3lv02d_attributes |
| 411 | }; |
| 412 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 413 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 414 | static int lis3lv02d_add_fs(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 415 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 416 | lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); |
| 417 | if (IS_ERR(lis3->pdev)) |
| 418 | return PTR_ERR(lis3->pdev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 419 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 420 | return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 423 | int lis3lv02d_remove_fs(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 424 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 425 | sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group); |
| 426 | platform_device_unregister(lis3->pdev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 427 | return 0; |
| 428 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 429 | EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 430 | |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 431 | /* |
| 432 | * Initialise the accelerometer and the various subsystems. |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 433 | * Should be rather independent of the bus system. |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 434 | */ |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 435 | int lis3lv02d_init_device(struct lis3lv02d *dev) |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 436 | { |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 437 | dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I); |
| 438 | |
| 439 | switch (dev->whoami) { |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 440 | case WAI_12B: |
| 441 | printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n"); |
| 442 | dev->read_data = lis3lv02d_read_12; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 443 | dev->mdps_max_val = 2048; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame^] | 444 | dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 445 | break; |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 446 | case WAI_8B: |
| 447 | printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n"); |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 448 | dev->read_data = lis3lv02d_read_8; |
| 449 | dev->mdps_max_val = 128; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame^] | 450 | dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 451 | break; |
| 452 | default: |
| 453 | printk(KERN_ERR DRIVER_NAME |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 454 | ": unknown sensor type 0x%X\n", dev->whoami); |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 455 | return -EINVAL; |
| 456 | } |
| 457 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 458 | lis3lv02d_add_fs(dev); |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 459 | lis3lv02d_poweron(dev); |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 460 | |
| 461 | if (lis3lv02d_joystick_enable()) |
| 462 | printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n"); |
| 463 | |
Daniel Mack | 8f3128e | 2009-06-16 15:34:17 -0700 | [diff] [blame] | 464 | /* passing in platform specific data is purely optional and only |
| 465 | * used by the SPI transport layer at the moment */ |
| 466 | if (dev->pdata) { |
| 467 | struct lis3lv02d_platform_data *p = dev->pdata; |
| 468 | |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 469 | if (p->click_flags && (dev->whoami == WAI_8B)) { |
Daniel Mack | 8f3128e | 2009-06-16 15:34:17 -0700 | [diff] [blame] | 470 | dev->write(dev, CLICK_CFG, p->click_flags); |
| 471 | dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit); |
| 472 | dev->write(dev, CLICK_LATENCY, p->click_latency); |
| 473 | dev->write(dev, CLICK_WINDOW, p->click_window); |
| 474 | dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf); |
| 475 | dev->write(dev, CLICK_THSY_X, |
| 476 | (p->click_thresh_x & 0xf) | |
| 477 | (p->click_thresh_y << 4)); |
| 478 | } |
| 479 | |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 480 | if (p->wakeup_flags && (dev->whoami == WAI_8B)) { |
Daniel Mack | 8873c33 | 2009-09-21 17:04:43 -0700 | [diff] [blame] | 481 | dev->write(dev, FF_WU_CFG_1, p->wakeup_flags); |
| 482 | dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f); |
| 483 | /* default to 2.5ms for now */ |
| 484 | dev->write(dev, FF_WU_DURATION_1, 1); |
| 485 | /* enable high pass filter for both free-fall units */ |
| 486 | dev->write(dev, CTRL_REG2, HP_FF_WU1 | HP_FF_WU2); |
| 487 | } |
| 488 | |
Daniel Mack | 8f3128e | 2009-06-16 15:34:17 -0700 | [diff] [blame] | 489 | if (p->irq_cfg) |
| 490 | dev->write(dev, CTRL_REG3, p->irq_cfg); |
| 491 | } |
| 492 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 493 | /* bail if we did not get an IRQ from the bus layer */ |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 494 | if (!dev->irq) { |
| 495 | printk(KERN_ERR DRIVER_NAME |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 496 | ": No IRQ. Disabling /dev/freefall\n"); |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 497 | goto out; |
| 498 | } |
| 499 | |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 500 | if (misc_register(&lis3lv02d_misc_device)) |
| 501 | printk(KERN_ERR DRIVER_NAME ": misc_register failed\n"); |
| 502 | out: |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | EXPORT_SYMBOL_GPL(lis3lv02d_init_device); |
| 506 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 507 | MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver"); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 508 | MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek"); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 509 | MODULE_LICENSE("GPL"); |
| 510 | |