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