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