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