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 |
Samu Onkalo | 4a70a41 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 44 | #define MDPS_POLL_MIN 0 |
| 45 | #define MDPS_POLL_MAX 2000 |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 46 | /* |
| 47 | * The sensor can also generate interrupts (DRDY) but it's pretty pointless |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 48 | * 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] | 49 | * to keep the interrupt for the free-fall event. The values are updated at |
| 50 | * 40Hz (at the lowest frequency), but as it can be pretty time consuming on |
| 51 | * some low processor, we poll the sensor only at 20Hz... enough for the |
| 52 | * joystick. |
| 53 | */ |
| 54 | |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 55 | #define LIS3_PWRON_DELAY_WAI_12B (5000) |
| 56 | #define LIS3_PWRON_DELAY_WAI_8B (3000) |
| 57 | |
Samu Onkalo | 32496c7 | 2009-12-14 18:01:46 -0800 | [diff] [blame] | 58 | /* |
| 59 | * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG |
| 60 | * LIS302D spec says: 18 mG / digit |
| 61 | * LIS3_ACCURACY is used to increase accuracy of the intermediate |
| 62 | * calculation results. |
| 63 | */ |
| 64 | #define LIS3_ACCURACY 1024 |
| 65 | /* Sensitivity values for -2G +2G scale */ |
| 66 | #define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024) |
| 67 | #define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY) |
| 68 | |
| 69 | #define LIS3_DEFAULT_FUZZ 3 |
| 70 | #define LIS3_DEFAULT_FLAT 3 |
| 71 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 72 | struct lis3lv02d lis3_dev = { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 73 | .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait), |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(lis3_dev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 77 | |
Takashi Iwai | 2ee3214 | 2010-10-01 17:14:25 -0400 | [diff] [blame] | 78 | /* just like param_set_int() but does sanity-check so that it won't point |
| 79 | * over the axis array size |
| 80 | */ |
| 81 | static int param_set_axis(const char *val, const struct kernel_param *kp) |
| 82 | { |
| 83 | int ret = param_set_int(val, kp); |
| 84 | if (!ret) { |
| 85 | int val = *(int *)kp->arg; |
| 86 | if (val < 0) |
| 87 | val = -val; |
| 88 | if (!val || val > 3) |
| 89 | return -EINVAL; |
| 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static struct kernel_param_ops param_ops_axis = { |
| 95 | .set = param_set_axis, |
| 96 | .get = param_get_int, |
| 97 | }; |
| 98 | |
| 99 | module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644); |
| 100 | MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions"); |
| 101 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 102 | static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg) |
| 103 | { |
| 104 | s8 lo; |
| 105 | if (lis3->read(lis3, reg, &lo) < 0) |
| 106 | return 0; |
| 107 | |
| 108 | return lo; |
| 109 | } |
| 110 | |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 111 | static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg) |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 112 | { |
| 113 | u8 lo, hi; |
| 114 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 115 | lis3->read(lis3, reg - 1, &lo); |
| 116 | lis3->read(lis3, reg, &hi); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 117 | /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */ |
| 118 | return (s16)((hi << 8) | lo); |
| 119 | } |
| 120 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 121 | /** |
| 122 | * lis3lv02d_get_axis - For the given axis, give the value converted |
| 123 | * @axis: 1,2,3 - can also be negative |
| 124 | * @hw_values: raw values returned by the hardware |
| 125 | * |
| 126 | * Returns the converted value. |
| 127 | */ |
| 128 | static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3]) |
| 129 | { |
| 130 | if (axis > 0) |
| 131 | return hw_values[axis - 1]; |
| 132 | else |
| 133 | return -hw_values[-axis - 1]; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * 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] | 138 | * @lis3: pointer to the device struct |
| 139 | * @x: where to store the X axis value |
| 140 | * @y: where to store the Y axis value |
| 141 | * @z: where to store the Z axis value |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 142 | * |
| 143 | * Note that 40Hz input device can eat up about 10% CPU at 800MHZ |
| 144 | */ |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 145 | 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] | 146 | { |
| 147 | int position[3]; |
Samu Onkalo | 32496c7 | 2009-12-14 18:01:46 -0800 | [diff] [blame] | 148 | int i; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 149 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 150 | position[0] = lis3->read_data(lis3, OUTX); |
| 151 | position[1] = lis3->read_data(lis3, OUTY); |
| 152 | position[2] = lis3->read_data(lis3, OUTZ); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 153 | |
Samu Onkalo | 32496c7 | 2009-12-14 18:01:46 -0800 | [diff] [blame] | 154 | for (i = 0; i < 3; i++) |
| 155 | position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY; |
| 156 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 157 | *x = lis3lv02d_get_axis(lis3->ac.x, position); |
| 158 | *y = lis3lv02d_get_axis(lis3->ac.y, position); |
| 159 | *z = lis3lv02d_get_axis(lis3->ac.z, position); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 162 | /* conversion btw sampling rate and the register values */ |
| 163 | static int lis3_12_rates[4] = {40, 160, 640, 2560}; |
| 164 | static int lis3_8_rates[2] = {100, 400}; |
Takashi Iwai | 78537c3 | 2010-09-23 10:01:39 -0700 | [diff] [blame] | 165 | static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000}; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 166 | |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 167 | /* ODR is Output Data Rate */ |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 168 | static int lis3lv02d_get_odr(void) |
| 169 | { |
| 170 | u8 ctrl; |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 171 | int shift; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 172 | |
| 173 | lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl); |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 174 | ctrl &= lis3_dev.odr_mask; |
| 175 | shift = ffs(lis3_dev.odr_mask) - 1; |
| 176 | return lis3_dev.odrs[(ctrl >> shift)]; |
| 177 | } |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 178 | |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 179 | static int lis3lv02d_set_odr(int rate) |
| 180 | { |
| 181 | u8 ctrl; |
| 182 | int i, len, shift; |
| 183 | |
Takashi Iwai | 78537c3 | 2010-09-23 10:01:39 -0700 | [diff] [blame] | 184 | if (!rate) |
| 185 | return -EINVAL; |
| 186 | |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 187 | lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl); |
| 188 | ctrl &= ~lis3_dev.odr_mask; |
| 189 | len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */ |
| 190 | shift = ffs(lis3_dev.odr_mask) - 1; |
| 191 | |
| 192 | for (i = 0; i < len; i++) |
| 193 | if (lis3_dev.odrs[i] == rate) { |
| 194 | lis3_dev.write(&lis3_dev, CTRL_REG1, |
| 195 | ctrl | (i << shift)); |
| 196 | return 0; |
| 197 | } |
| 198 | return -EINVAL; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 201 | static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3]) |
| 202 | { |
Takashi Iwai | 78537c3 | 2010-09-23 10:01:39 -0700 | [diff] [blame] | 203 | u8 ctlreg, reg; |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 204 | s16 x, y, z; |
| 205 | u8 selftest; |
| 206 | int ret; |
| 207 | |
| 208 | mutex_lock(&lis3->mutex); |
Takashi Iwai | 78537c3 | 2010-09-23 10:01:39 -0700 | [diff] [blame] | 209 | if (lis3_dev.whoami == WAI_3DC) { |
| 210 | ctlreg = CTRL_REG4; |
| 211 | selftest = CTRL4_ST0; |
| 212 | } else { |
| 213 | ctlreg = CTRL_REG1; |
| 214 | if (lis3_dev.whoami == WAI_12B) |
| 215 | selftest = CTRL1_ST; |
| 216 | else |
| 217 | selftest = CTRL1_STP; |
| 218 | } |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 219 | |
Takashi Iwai | 78537c3 | 2010-09-23 10:01:39 -0700 | [diff] [blame] | 220 | lis3->read(lis3, ctlreg, ®); |
| 221 | lis3->write(lis3, ctlreg, (reg | selftest)); |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 222 | msleep(lis3->pwron_delay / lis3lv02d_get_odr()); |
| 223 | |
| 224 | /* Read directly to avoid axis remap */ |
| 225 | x = lis3->read_data(lis3, OUTX); |
| 226 | y = lis3->read_data(lis3, OUTY); |
| 227 | z = lis3->read_data(lis3, OUTZ); |
| 228 | |
| 229 | /* back to normal settings */ |
Takashi Iwai | 78537c3 | 2010-09-23 10:01:39 -0700 | [diff] [blame] | 230 | lis3->write(lis3, ctlreg, reg); |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 231 | msleep(lis3->pwron_delay / lis3lv02d_get_odr()); |
| 232 | |
| 233 | results[0] = x - lis3->read_data(lis3, OUTX); |
| 234 | results[1] = y - lis3->read_data(lis3, OUTY); |
| 235 | results[2] = z - lis3->read_data(lis3, OUTZ); |
| 236 | |
| 237 | ret = 0; |
| 238 | if (lis3->pdata) { |
| 239 | int i; |
| 240 | for (i = 0; i < 3; i++) { |
| 241 | /* Check against selftest acceptance limits */ |
| 242 | if ((results[i] < lis3->pdata->st_min_limits[i]) || |
| 243 | (results[i] > lis3->pdata->st_max_limits[i])) { |
| 244 | ret = -EIO; |
| 245 | goto fail; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* test passed */ |
| 251 | fail: |
| 252 | mutex_unlock(&lis3->mutex); |
| 253 | return ret; |
| 254 | } |
| 255 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 256 | void lis3lv02d_poweroff(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 257 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 258 | /* disable X,Y,Z axis and power down */ |
| 259 | lis3->write(lis3, CTRL_REG1, 0x00); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 260 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 261 | EXPORT_SYMBOL_GPL(lis3lv02d_poweroff); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 262 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 263 | void lis3lv02d_poweron(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 264 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 265 | u8 reg; |
| 266 | |
| 267 | lis3->init(lis3); |
| 268 | |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 269 | /* LIS3 power on delay is quite long */ |
| 270 | msleep(lis3->pwron_delay / lis3lv02d_get_odr()); |
| 271 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 272 | /* |
| 273 | * Common configuration |
Éric Piel | 4b5d95b | 2009-12-14 18:01:40 -0800 | [diff] [blame] | 274 | * BDU: (12 bits sensors only) LSB and MSB values are not updated until |
| 275 | * both have been read. So the value read will always be correct. |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 276 | */ |
Éric Piel | 4b5d95b | 2009-12-14 18:01:40 -0800 | [diff] [blame] | 277 | if (lis3->whoami == WAI_12B) { |
| 278 | lis3->read(lis3, CTRL_REG2, ®); |
| 279 | reg |= CTRL2_BDU; |
| 280 | lis3->write(lis3, CTRL_REG2, reg); |
| 281 | } |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 282 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 283 | EXPORT_SYMBOL_GPL(lis3lv02d_poweron); |
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 | |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 286 | static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev) |
| 287 | { |
| 288 | int x, y, z; |
| 289 | |
| 290 | mutex_lock(&lis3_dev.mutex); |
| 291 | lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z); |
| 292 | input_report_abs(pidev->input, ABS_X, x); |
| 293 | input_report_abs(pidev->input, ABS_Y, y); |
| 294 | input_report_abs(pidev->input, ABS_Z, z); |
| 295 | input_sync(pidev->input); |
| 296 | mutex_unlock(&lis3_dev.mutex); |
| 297 | } |
| 298 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 299 | static irqreturn_t lis302dl_interrupt(int irq, void *dummy) |
| 300 | { |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 301 | if (!test_bit(0, &lis3_dev.misc_opened)) |
| 302 | goto out; |
| 303 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 304 | /* |
| 305 | * Be careful: on some HP laptops the bios force DD when on battery and |
| 306 | * the lid is closed. This leads to interrupts as soon as a little move |
| 307 | * is done. |
| 308 | */ |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 309 | atomic_inc(&lis3_dev.count); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 310 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 311 | wake_up_interruptible(&lis3_dev.misc_wait); |
| 312 | kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN); |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 313 | out: |
Takashi Iwai | f7c77a3 | 2010-09-23 10:01:11 -0700 | [diff] [blame] | 314 | if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev && |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 315 | lis3_dev.idev->input->users) |
| 316 | return IRQ_WAKE_THREAD; |
| 317 | return IRQ_HANDLED; |
| 318 | } |
| 319 | |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 320 | static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3) |
| 321 | { |
| 322 | struct input_dev *dev = lis3->idev->input; |
| 323 | u8 click_src; |
| 324 | |
| 325 | mutex_lock(&lis3->mutex); |
| 326 | lis3->read(lis3, CLICK_SRC, &click_src); |
| 327 | |
| 328 | if (click_src & CLICK_SINGLE_X) { |
| 329 | input_report_key(dev, lis3->mapped_btns[0], 1); |
| 330 | input_report_key(dev, lis3->mapped_btns[0], 0); |
| 331 | } |
| 332 | |
| 333 | if (click_src & CLICK_SINGLE_Y) { |
| 334 | input_report_key(dev, lis3->mapped_btns[1], 1); |
| 335 | input_report_key(dev, lis3->mapped_btns[1], 0); |
| 336 | } |
| 337 | |
| 338 | if (click_src & CLICK_SINGLE_Z) { |
| 339 | input_report_key(dev, lis3->mapped_btns[2], 1); |
| 340 | input_report_key(dev, lis3->mapped_btns[2], 0); |
| 341 | } |
| 342 | input_sync(dev); |
| 343 | mutex_unlock(&lis3->mutex); |
| 344 | } |
| 345 | |
| 346 | static void lis302dl_interrupt_handle_ff_wu(struct lis3lv02d *lis3) |
| 347 | { |
| 348 | u8 wu1_src; |
| 349 | u8 wu2_src; |
| 350 | |
| 351 | lis3->read(lis3, FF_WU_SRC_1, &wu1_src); |
| 352 | lis3->read(lis3, FF_WU_SRC_2, &wu2_src); |
| 353 | |
| 354 | wu1_src = wu1_src & FF_WU_SRC_IA ? wu1_src : 0; |
| 355 | wu2_src = wu2_src & FF_WU_SRC_IA ? wu2_src : 0; |
| 356 | |
| 357 | /* joystick poll is internally protected by the lis3->mutex. */ |
| 358 | if (wu1_src || wu2_src) |
| 359 | lis3lv02d_joystick_poll(lis3_dev.idev); |
| 360 | } |
| 361 | |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 362 | static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data) |
| 363 | { |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 364 | |
| 365 | struct lis3lv02d *lis3 = data; |
| 366 | |
| 367 | if ((lis3->pdata->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK) |
| 368 | lis302dl_interrupt_handle_click(lis3); |
| 369 | else |
| 370 | lis302dl_interrupt_handle_ff_wu(lis3); |
| 371 | |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 372 | return IRQ_HANDLED; |
| 373 | } |
| 374 | |
| 375 | static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data) |
| 376 | { |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 377 | |
| 378 | struct lis3lv02d *lis3 = data; |
| 379 | |
| 380 | if ((lis3->pdata->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK) |
| 381 | lis302dl_interrupt_handle_click(lis3); |
| 382 | else |
| 383 | lis302dl_interrupt_handle_ff_wu(lis3); |
| 384 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 385 | return IRQ_HANDLED; |
| 386 | } |
| 387 | |
| 388 | static int lis3lv02d_misc_open(struct inode *inode, struct file *file) |
| 389 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 390 | if (test_and_set_bit(0, &lis3_dev.misc_opened)) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 391 | return -EBUSY; /* already open */ |
| 392 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 393 | atomic_set(&lis3_dev.count, 0); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int lis3lv02d_misc_release(struct inode *inode, struct file *file) |
| 398 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 399 | fasync_helper(-1, file, 0, &lis3_dev.async_queue); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 400 | clear_bit(0, &lis3_dev.misc_opened); /* release the device */ |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf, |
| 405 | size_t count, loff_t *pos) |
| 406 | { |
| 407 | DECLARE_WAITQUEUE(wait, current); |
| 408 | u32 data; |
| 409 | unsigned char byte_data; |
| 410 | ssize_t retval = 1; |
| 411 | |
| 412 | if (count < 1) |
| 413 | return -EINVAL; |
| 414 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 415 | add_wait_queue(&lis3_dev.misc_wait, &wait); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 416 | while (true) { |
| 417 | set_current_state(TASK_INTERRUPTIBLE); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 418 | data = atomic_xchg(&lis3_dev.count, 0); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 419 | if (data) |
| 420 | break; |
| 421 | |
| 422 | if (file->f_flags & O_NONBLOCK) { |
| 423 | retval = -EAGAIN; |
| 424 | goto out; |
| 425 | } |
| 426 | |
| 427 | if (signal_pending(current)) { |
| 428 | retval = -ERESTARTSYS; |
| 429 | goto out; |
| 430 | } |
| 431 | |
| 432 | schedule(); |
| 433 | } |
| 434 | |
| 435 | if (data < 255) |
| 436 | byte_data = data; |
| 437 | else |
| 438 | byte_data = 255; |
| 439 | |
| 440 | /* make sure we are not going into copy_to_user() with |
| 441 | * TASK_INTERRUPTIBLE state */ |
| 442 | set_current_state(TASK_RUNNING); |
| 443 | if (copy_to_user(buf, &byte_data, sizeof(byte_data))) |
| 444 | retval = -EFAULT; |
| 445 | |
| 446 | out: |
| 447 | __set_current_state(TASK_RUNNING); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 448 | remove_wait_queue(&lis3_dev.misc_wait, &wait); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 449 | |
| 450 | return retval; |
| 451 | } |
| 452 | |
| 453 | static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait) |
| 454 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 455 | poll_wait(file, &lis3_dev.misc_wait, wait); |
| 456 | if (atomic_read(&lis3_dev.count)) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 457 | return POLLIN | POLLRDNORM; |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int lis3lv02d_misc_fasync(int fd, struct file *file, int on) |
| 462 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 463 | return fasync_helper(fd, file, on, &lis3_dev.async_queue); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static const struct file_operations lis3lv02d_misc_fops = { |
| 467 | .owner = THIS_MODULE, |
| 468 | .llseek = no_llseek, |
| 469 | .read = lis3lv02d_misc_read, |
| 470 | .open = lis3lv02d_misc_open, |
| 471 | .release = lis3lv02d_misc_release, |
| 472 | .poll = lis3lv02d_misc_poll, |
| 473 | .fasync = lis3lv02d_misc_fasync, |
| 474 | }; |
| 475 | |
| 476 | static struct miscdevice lis3lv02d_misc_device = { |
| 477 | .minor = MISC_DYNAMIC_MINOR, |
| 478 | .name = "freefall", |
| 479 | .fops = &lis3lv02d_misc_fops, |
| 480 | }; |
| 481 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 482 | int lis3lv02d_joystick_enable(void) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 483 | { |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 484 | struct input_dev *input_dev; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 485 | int err; |
Samu Onkalo | 32496c7 | 2009-12-14 18:01:46 -0800 | [diff] [blame] | 486 | int max_val, fuzz, flat; |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 487 | int btns[] = {BTN_X, BTN_Y, BTN_Z}; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 488 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 489 | if (lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 490 | return -EINVAL; |
| 491 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 492 | lis3_dev.idev = input_allocate_polled_device(); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 493 | if (!lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 494 | return -ENOMEM; |
| 495 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 496 | lis3_dev.idev->poll = lis3lv02d_joystick_poll; |
| 497 | lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL; |
Samu Onkalo | 4a70a41 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 498 | lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN; |
| 499 | lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX; |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 500 | input_dev = lis3_dev.idev->input; |
| 501 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 502 | input_dev->name = "ST LIS3LV02DL Accelerometer"; |
| 503 | input_dev->phys = DRIVER_NAME "/input0"; |
| 504 | input_dev->id.bustype = BUS_HOST; |
| 505 | input_dev->id.vendor = 0; |
| 506 | input_dev->dev.parent = &lis3_dev.pdev->dev; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 507 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 508 | set_bit(EV_ABS, input_dev->evbit); |
Samu Onkalo | 32496c7 | 2009-12-14 18:01:46 -0800 | [diff] [blame] | 509 | max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY; |
| 510 | fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY; |
| 511 | flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY; |
| 512 | input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat); |
| 513 | input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat); |
| 514 | input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 515 | |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 516 | lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns); |
| 517 | lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns); |
| 518 | lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns); |
| 519 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 520 | err = input_register_polled_device(lis3_dev.idev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 521 | if (err) { |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 522 | input_free_polled_device(lis3_dev.idev); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 523 | lis3_dev.idev = NULL; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | return err; |
| 527 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 528 | EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 529 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 530 | void lis3lv02d_joystick_disable(void) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 531 | { |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 532 | if (lis3_dev.irq) |
| 533 | free_irq(lis3_dev.irq, &lis3_dev); |
| 534 | if (lis3_dev.pdata && lis3_dev.pdata->irq2) |
| 535 | free_irq(lis3_dev.pdata->irq2, &lis3_dev); |
| 536 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 537 | if (!lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 538 | return; |
| 539 | |
Eric Piel | c288424 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 540 | if (lis3_dev.irq) |
| 541 | misc_deregister(&lis3lv02d_misc_device); |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 542 | input_unregister_polled_device(lis3_dev.idev); |
Samu Onkalo | 66c8569 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 543 | input_free_polled_device(lis3_dev.idev); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 544 | lis3_dev.idev = NULL; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 545 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 546 | EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 547 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 548 | /* Sysfs stuff */ |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 549 | static ssize_t lis3lv02d_selftest_show(struct device *dev, |
| 550 | struct device_attribute *attr, char *buf) |
| 551 | { |
| 552 | int result; |
| 553 | s16 values[3]; |
| 554 | |
| 555 | result = lis3lv02d_selftest(&lis3_dev, values); |
| 556 | return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL", |
| 557 | values[0], values[1], values[2]); |
| 558 | } |
| 559 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 560 | static ssize_t lis3lv02d_position_show(struct device *dev, |
| 561 | struct device_attribute *attr, char *buf) |
| 562 | { |
| 563 | int x, y, z; |
| 564 | |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 565 | mutex_lock(&lis3_dev.mutex); |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 566 | lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z); |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 567 | mutex_unlock(&lis3_dev.mutex); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 568 | return sprintf(buf, "(%d,%d,%d)\n", x, y, z); |
| 569 | } |
| 570 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 571 | static ssize_t lis3lv02d_rate_show(struct device *dev, |
| 572 | struct device_attribute *attr, char *buf) |
| 573 | { |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 574 | return sprintf(buf, "%d\n", lis3lv02d_get_odr()); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 577 | static ssize_t lis3lv02d_rate_set(struct device *dev, |
| 578 | struct device_attribute *attr, const char *buf, |
| 579 | size_t count) |
| 580 | { |
| 581 | unsigned long rate; |
| 582 | |
| 583 | if (strict_strtoul(buf, 0, &rate)) |
| 584 | return -EINVAL; |
| 585 | |
| 586 | if (lis3lv02d_set_odr(rate)) |
| 587 | return -EINVAL; |
| 588 | |
| 589 | return count; |
| 590 | } |
| 591 | |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 592 | static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 593 | static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL); |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 594 | static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show, |
| 595 | lis3lv02d_rate_set); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 596 | |
| 597 | static struct attribute *lis3lv02d_attributes[] = { |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 598 | &dev_attr_selftest.attr, |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 599 | &dev_attr_position.attr, |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 600 | &dev_attr_rate.attr, |
| 601 | NULL |
| 602 | }; |
| 603 | |
| 604 | static struct attribute_group lis3lv02d_attribute_group = { |
| 605 | .attrs = lis3lv02d_attributes |
| 606 | }; |
| 607 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 608 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 609 | static int lis3lv02d_add_fs(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 610 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 611 | lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); |
| 612 | if (IS_ERR(lis3->pdev)) |
| 613 | return PTR_ERR(lis3->pdev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 614 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 615 | return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 618 | int lis3lv02d_remove_fs(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 619 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 620 | sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group); |
| 621 | platform_device_unregister(lis3->pdev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 622 | return 0; |
| 623 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 624 | EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 625 | |
Samu Onkalo | ecc437a | 2010-05-24 14:33:34 -0700 | [diff] [blame] | 626 | static void lis3lv02d_8b_configure(struct lis3lv02d *dev, |
| 627 | struct lis3lv02d_platform_data *p) |
| 628 | { |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 629 | int err; |
Samu Onkalo | 342c5f1 | 2010-05-24 14:33:35 -0700 | [diff] [blame] | 630 | int ctrl2 = p->hipass_ctrl; |
| 631 | |
Samu Onkalo | ecc437a | 2010-05-24 14:33:34 -0700 | [diff] [blame] | 632 | if (p->click_flags) { |
| 633 | dev->write(dev, CLICK_CFG, p->click_flags); |
| 634 | dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit); |
| 635 | dev->write(dev, CLICK_LATENCY, p->click_latency); |
| 636 | dev->write(dev, CLICK_WINDOW, p->click_window); |
| 637 | dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf); |
| 638 | dev->write(dev, CLICK_THSY_X, |
| 639 | (p->click_thresh_x & 0xf) | |
| 640 | (p->click_thresh_y << 4)); |
Samu Onkalo | 6d94d40 | 2010-05-24 14:33:37 -0700 | [diff] [blame] | 641 | |
| 642 | if (dev->idev) { |
| 643 | struct input_dev *input_dev = lis3_dev.idev->input; |
| 644 | input_set_capability(input_dev, EV_KEY, BTN_X); |
| 645 | input_set_capability(input_dev, EV_KEY, BTN_Y); |
| 646 | input_set_capability(input_dev, EV_KEY, BTN_Z); |
| 647 | } |
Samu Onkalo | ecc437a | 2010-05-24 14:33:34 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | if (p->wakeup_flags) { |
| 651 | dev->write(dev, FF_WU_CFG_1, p->wakeup_flags); |
| 652 | dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f); |
| 653 | /* default to 2.5ms for now */ |
| 654 | dev->write(dev, FF_WU_DURATION_1, 1); |
Samu Onkalo | 342c5f1 | 2010-05-24 14:33:35 -0700 | [diff] [blame] | 655 | ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/ |
Samu Onkalo | ecc437a | 2010-05-24 14:33:34 -0700 | [diff] [blame] | 656 | } |
Samu Onkalo | 342c5f1 | 2010-05-24 14:33:35 -0700 | [diff] [blame] | 657 | |
| 658 | if (p->wakeup_flags2) { |
| 659 | dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2); |
| 660 | dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f); |
| 661 | /* default to 2.5ms for now */ |
| 662 | dev->write(dev, FF_WU_DURATION_2, 1); |
| 663 | ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/ |
| 664 | } |
| 665 | /* Configure hipass filters */ |
| 666 | dev->write(dev, CTRL_REG2, ctrl2); |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 667 | |
| 668 | if (p->irq2) { |
| 669 | err = request_threaded_irq(p->irq2, |
| 670 | NULL, |
| 671 | lis302dl_interrupt_thread2_8b, |
| 672 | IRQF_TRIGGER_RISING | |
| 673 | IRQF_ONESHOT, |
| 674 | DRIVER_NAME, &lis3_dev); |
| 675 | if (err < 0) |
| 676 | printk(KERN_ERR DRIVER_NAME |
| 677 | "No second IRQ. Limited functionality\n"); |
| 678 | } |
Samu Onkalo | ecc437a | 2010-05-24 14:33:34 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 681 | /* |
| 682 | * Initialise the accelerometer and the various subsystems. |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 683 | * Should be rather independent of the bus system. |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 684 | */ |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 685 | int lis3lv02d_init_device(struct lis3lv02d *dev) |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 686 | { |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 687 | int err; |
| 688 | irq_handler_t thread_fn; |
| 689 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 690 | dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I); |
| 691 | |
| 692 | switch (dev->whoami) { |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 693 | case WAI_12B: |
| 694 | printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n"); |
| 695 | dev->read_data = lis3lv02d_read_12; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 696 | dev->mdps_max_val = 2048; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 697 | dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B; |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 698 | dev->odrs = lis3_12_rates; |
| 699 | dev->odr_mask = CTRL1_DF0 | CTRL1_DF1; |
Samu Onkalo | 32496c7 | 2009-12-14 18:01:46 -0800 | [diff] [blame] | 700 | dev->scale = LIS3_SENSITIVITY_12B; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 701 | break; |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 702 | case WAI_8B: |
| 703 | printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n"); |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 704 | dev->read_data = lis3lv02d_read_8; |
| 705 | dev->mdps_max_val = 128; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 706 | dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B; |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame] | 707 | dev->odrs = lis3_8_rates; |
| 708 | dev->odr_mask = CTRL1_DR; |
Samu Onkalo | 32496c7 | 2009-12-14 18:01:46 -0800 | [diff] [blame] | 709 | dev->scale = LIS3_SENSITIVITY_8B; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 710 | break; |
Takashi Iwai | 78537c3 | 2010-09-23 10:01:39 -0700 | [diff] [blame] | 711 | case WAI_3DC: |
| 712 | printk(KERN_INFO DRIVER_NAME ": 8 bits 3DC sensor found\n"); |
| 713 | dev->read_data = lis3lv02d_read_8; |
| 714 | dev->mdps_max_val = 128; |
| 715 | dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B; |
| 716 | dev->odrs = lis3_3dc_rates; |
| 717 | dev->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3; |
| 718 | dev->scale = LIS3_SENSITIVITY_8B; |
| 719 | break; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 720 | default: |
| 721 | printk(KERN_ERR DRIVER_NAME |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 722 | ": unknown sensor type 0x%X\n", dev->whoami); |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 723 | return -EINVAL; |
| 724 | } |
| 725 | |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 726 | mutex_init(&dev->mutex); |
| 727 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 728 | lis3lv02d_add_fs(dev); |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 729 | lis3lv02d_poweron(dev); |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 730 | |
| 731 | if (lis3lv02d_joystick_enable()) |
| 732 | printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n"); |
| 733 | |
Daniel Mack | 8f3128e | 2009-06-16 15:34:17 -0700 | [diff] [blame] | 734 | /* passing in platform specific data is purely optional and only |
| 735 | * used by the SPI transport layer at the moment */ |
| 736 | if (dev->pdata) { |
| 737 | struct lis3lv02d_platform_data *p = dev->pdata; |
| 738 | |
Samu Onkalo | ecc437a | 2010-05-24 14:33:34 -0700 | [diff] [blame] | 739 | if (dev->whoami == WAI_8B) |
| 740 | lis3lv02d_8b_configure(dev, p); |
Daniel Mack | 8873c33 | 2009-09-21 17:04:43 -0700 | [diff] [blame] | 741 | |
Daniel Mack | 8f3128e | 2009-06-16 15:34:17 -0700 | [diff] [blame] | 742 | if (p->irq_cfg) |
| 743 | dev->write(dev, CTRL_REG3, p->irq_cfg); |
| 744 | } |
| 745 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 746 | /* bail if we did not get an IRQ from the bus layer */ |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 747 | if (!dev->irq) { |
| 748 | printk(KERN_ERR DRIVER_NAME |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 749 | ": No IRQ. Disabling /dev/freefall\n"); |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 750 | goto out; |
| 751 | } |
| 752 | |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 753 | /* |
| 754 | * The sensor can generate interrupts for free-fall and direction |
| 755 | * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep |
| 756 | * the things simple and _fast_ we activate it only for free-fall, so |
| 757 | * no need to read register (very slow with ACPI). For the same reason, |
| 758 | * we forbid shared interrupts. |
| 759 | * |
| 760 | * IRQF_TRIGGER_RISING seems pointless on HP laptops because the |
| 761 | * io-apic is not configurable (and generates a warning) but I keep it |
| 762 | * in case of support for other hardware. |
| 763 | */ |
Takashi Iwai | f7c77a3 | 2010-09-23 10:01:11 -0700 | [diff] [blame] | 764 | if (dev->pdata && dev->whoami == WAI_8B) |
Samu Onkalo | 92ba4fe | 2010-05-24 14:33:36 -0700 | [diff] [blame] | 765 | thread_fn = lis302dl_interrupt_thread1_8b; |
| 766 | else |
| 767 | thread_fn = NULL; |
| 768 | |
| 769 | err = request_threaded_irq(dev->irq, lis302dl_interrupt, |
| 770 | thread_fn, |
| 771 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 772 | DRIVER_NAME, &lis3_dev); |
| 773 | |
| 774 | if (err < 0) { |
| 775 | printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n"); |
| 776 | goto out; |
| 777 | } |
| 778 | |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 779 | if (misc_register(&lis3lv02d_misc_device)) |
| 780 | printk(KERN_ERR DRIVER_NAME ": misc_register failed\n"); |
| 781 | out: |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 782 | return 0; |
| 783 | } |
| 784 | EXPORT_SYMBOL_GPL(lis3lv02d_init_device); |
| 785 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 786 | MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver"); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 787 | MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek"); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 788 | MODULE_LICENSE("GPL"); |