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