| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 1 | /******************** (C) COPYRIGHT 2010 STMicroelectronics ******************** |
| 2 | * |
| 3 | * File Name : lis3dh_acc.c |
| 4 | * Authors : MSH - Motion Mems BU - Application Team |
| 5 | * : Matteo Dameno (matteo.dameno@st.com) |
| 6 | * : Carmine Iascone (carmine.iascone@st.com) |
| 7 | * : Samuel Huo (samuel.huo@st.com) |
| 8 | * Version : V.1.1.0 |
| 9 | * Date : 07/10/2012 |
| 10 | * Description : LIS3DH accelerometer sensor driver |
| 11 | * |
| 12 | ******************************************************************************* |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
| 17 | * |
| 18 | * THE PRESENT SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES |
| 19 | * OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, FOR THE SOLE |
| 20 | * PURPOSE TO SUPPORT YOUR APPLICATION DEVELOPMENT. |
| 21 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, |
| 22 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE |
| 23 | * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING |
| 24 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
| 25 | * |
| 26 | * THIS SOFTWARE IS SPECIFICALLY DESIGNED FOR EXCLUSIVE USE WITH ST PARTS. |
| 27 | * |
| 28 | ****************************************************************************** |
| 29 | Revision 1.0.0 05/11/09 |
| 30 | First Release; |
| 31 | Revision 1.0.3 22/01/2010 |
| 32 | Linux K&R Compliant Release; |
| 33 | Revision 1.0.5 16/08/2010 |
| 34 | modified _get_acceleration_data function; |
| 35 | modified _update_odr function; |
| 36 | manages 2 interrupts; |
| 37 | Revision 1.0.6 15/11/2010 |
| 38 | supports sysfs; |
| 39 | no more support for ioctl; |
| 40 | Revision 1.0.7 26/11/2010 |
| 41 | checks for availability of interrupts pins |
| 42 | correction on FUZZ and FLAT values; |
| 43 | Revision 1.0.8 2010/Apr/01 |
| 44 | corrects a bug in interrupt pin management in 1.0.7 |
| 45 | Revision 1.0.9 07/25/2011 |
| 46 | Romove several unused functions,add 5ms delay in init,change sysfs attributes. |
| 47 | Revision 1.1.0 07/10/2012 |
| 48 | To replace some deprecated functions for 3.4 kernel; |
| 49 | To pass the checkpatch's formatting requirement; |
| 50 | To add regulator request; |
| 51 | |
| 52 | ******************************************************************************/ |
| 53 | |
| 54 | #include <linux/err.h> |
| 55 | #include <linux/errno.h> |
| 56 | #include <linux/delay.h> |
| 57 | #include <linux/fs.h> |
| 58 | #include <linux/i2c.h> |
| 59 | #include <linux/input.h> |
| 60 | #include <linux/uaccess.h> |
| 61 | #include <linux/workqueue.h> |
| 62 | #include <linux/irq.h> |
| 63 | #include <linux/gpio.h> |
| 64 | #include <linux/interrupt.h> |
| 65 | #include <linux/slab.h> |
| 66 | #include <linux/pm.h> |
| 67 | #include <linux/input/lis3dh.h> |
| 68 | #include <linux/module.h> |
| 69 | #include <linux/regulator/consumer.h> |
| 70 | |
| 71 | |
| 72 | #define DEBUG 1 |
| 73 | |
| 74 | #define G_MAX 16000 |
| 75 | |
| 76 | |
| 77 | #define SENSITIVITY_2G 1 /** mg/LSB */ |
| 78 | #define SENSITIVITY_4G 2 /** mg/LSB */ |
| 79 | #define SENSITIVITY_8G 4 /** mg/LSB */ |
| 80 | #define SENSITIVITY_16G 12 /** mg/LSB */ |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | /* Accelerometer Sensor Operating Mode */ |
| 86 | #define LIS3DH_ACC_ENABLE 0x01 |
| 87 | #define LIS3DH_ACC_DISABLE 0x00 |
| 88 | |
| 89 | #define HIGH_RESOLUTION 0x08 |
| 90 | |
| 91 | #define AXISDATA_REG 0x28 |
| 92 | #define WHOAMI_LIS3DH_ACC 0x33 /* Expected content for WAI */ |
| 93 | |
| 94 | /* CONTROL REGISTERS */ |
| 95 | #define WHO_AM_I 0x0F /* WhoAmI register */ |
| 96 | #define TEMP_CFG_REG 0x1F /* temper sens control reg */ |
| 97 | /* ctrl 1: ODR3 ODR2 ODR ODR0 LPen Zenable Yenable Zenable */ |
| 98 | #define CTRL_REG1 0x20 /* control reg 1 */ |
| 99 | #define CTRL_REG2 0x21 /* control reg 2 */ |
| 100 | #define CTRL_REG3 0x22 /* control reg 3 */ |
| 101 | #define CTRL_REG4 0x23 /* control reg 4 */ |
| 102 | #define CTRL_REG5 0x24 /* control reg 5 */ |
| 103 | #define CTRL_REG6 0x25 /* control reg 6 */ |
| 104 | |
| 105 | #define FIFO_CTRL_REG 0x2E /* FiFo control reg */ |
| 106 | |
| 107 | #define INT_CFG1 0x30 /* interrupt 1 config */ |
| 108 | #define INT_SRC1 0x31 /* interrupt 1 source */ |
| 109 | #define INT_THS1 0x32 /* interrupt 1 threshold */ |
| 110 | #define INT_DUR1 0x33 /* interrupt 1 duration */ |
| 111 | |
| 112 | |
| 113 | #define TT_CFG 0x38 /* tap config */ |
| 114 | #define TT_SRC 0x39 /* tap source */ |
| 115 | #define TT_THS 0x3A /* tap threshold */ |
| 116 | #define TT_LIM 0x3B /* tap time limit */ |
| 117 | #define TT_TLAT 0x3C /* tap time latency */ |
| 118 | #define TT_TW 0x3D /* tap time window */ |
| 119 | /* end CONTROL REGISTRES */ |
| 120 | |
| 121 | |
| 122 | #define ENABLE_HIGH_RESOLUTION 1 |
| 123 | |
| 124 | #define LIS3DH_ACC_PM_OFF 0x00 |
| 125 | #define LIS3DH_ACC_ENABLE_ALL_AXES 0x07 |
| 126 | |
| 127 | |
| 128 | #define PMODE_MASK 0x08 |
| 129 | #define ODR_MASK 0XF0 |
| 130 | |
| 131 | #define ODR1 0x10 /* 1Hz output data rate */ |
| 132 | #define ODR10 0x20 /* 10Hz output data rate */ |
| 133 | #define ODR25 0x30 /* 25Hz output data rate */ |
| 134 | #define ODR50 0x40 /* 50Hz output data rate */ |
| 135 | #define ODR100 0x50 /* 100Hz output data rate */ |
| 136 | #define ODR200 0x60 /* 200Hz output data rate */ |
| 137 | #define ODR400 0x70 /* 400Hz output data rate */ |
| 138 | #define ODR1250 0x90 /* 1250Hz output data rate */ |
| 139 | |
| 140 | |
| 141 | |
| 142 | #define IA 0x40 |
| 143 | #define ZH 0x20 |
| 144 | #define ZL 0x10 |
| 145 | #define YH 0x08 |
| 146 | #define YL 0x04 |
| 147 | #define XH 0x02 |
| 148 | #define XL 0x01 |
| 149 | /* */ |
| 150 | /* CTRL REG BITS*/ |
| 151 | #define CTRL_REG3_I1_AOI1 0x40 |
| 152 | #define CTRL_REG6_I2_TAPEN 0x80 |
| 153 | #define CTRL_REG6_HLACTIVE 0x02 |
| 154 | /* */ |
| 155 | #define NO_MASK 0xFF |
| 156 | #define INT1_DURATION_MASK 0x7F |
| 157 | #define INT1_THRESHOLD_MASK 0x7F |
| 158 | #define TAP_CFG_MASK 0x3F |
| 159 | #define TAP_THS_MASK 0x7F |
| 160 | #define TAP_TLIM_MASK 0x7F |
| 161 | #define TAP_TLAT_MASK NO_MASK |
| 162 | #define TAP_TW_MASK NO_MASK |
| 163 | |
| 164 | |
| 165 | /* TAP_SOURCE_REG BIT */ |
| 166 | #define DTAP 0x20 |
| 167 | #define STAP 0x10 |
| 168 | #define SIGNTAP 0x08 |
| 169 | #define ZTAP 0x04 |
| 170 | #define YTAP 0x02 |
| 171 | #define XTAZ 0x01 |
| 172 | |
| 173 | |
| 174 | #define FUZZ 0 |
| 175 | #define FLAT 0 |
| 176 | #define I2C_RETRY_DELAY 5 |
| 177 | #define I2C_RETRIES 5 |
| 178 | #define I2C_AUTO_INCREMENT 0x80 |
| 179 | |
| 180 | /* RESUME STATE INDICES */ |
| 181 | #define RES_CTRL_REG1 0 |
| 182 | #define RES_CTRL_REG2 1 |
| 183 | #define RES_CTRL_REG3 2 |
| 184 | #define RES_CTRL_REG4 3 |
| 185 | #define RES_CTRL_REG5 4 |
| 186 | #define RES_CTRL_REG6 5 |
| 187 | |
| 188 | #define RES_INT_CFG1 6 |
| 189 | #define RES_INT_THS1 7 |
| 190 | #define RES_INT_DUR1 8 |
| 191 | |
| 192 | #define RES_TT_CFG 9 |
| 193 | #define RES_TT_THS 10 |
| 194 | #define RES_TT_LIM 11 |
| 195 | #define RES_TT_TLAT 12 |
| 196 | #define RES_TT_TW 13 |
| 197 | |
| 198 | #define RES_TEMP_CFG_REG 14 |
| 199 | #define RES_REFERENCE_REG 15 |
| 200 | #define RES_FIFO_CTRL_REG 16 |
| 201 | |
| 202 | #define RESUME_ENTRIES 17 |
| 203 | /* end RESUME STATE INDICES */ |
| 204 | |
| 205 | |
| 206 | struct { |
| 207 | unsigned int cutoff_ms; |
| 208 | unsigned int mask; |
| 209 | } lis3dh_acc_odr_table[] = { |
| 210 | { 1, ODR1250 }, |
| 211 | { 3, ODR400 }, |
| 212 | { 5, ODR200 }, |
| 213 | { 10, ODR100 }, |
| 214 | { 20, ODR50 }, |
| 215 | { 40, ODR25 }, |
| 216 | { 100, ODR10 }, |
| 217 | { 1000, ODR1 }, |
| 218 | }; |
| 219 | |
| 220 | struct lis3dh_acc_data { |
| 221 | struct i2c_client *client; |
| 222 | struct lis3dh_acc_platform_data *pdata; |
| 223 | |
| 224 | struct mutex lock; |
| 225 | struct delayed_work input_work; |
| 226 | |
| 227 | struct input_dev *input_dev; |
| 228 | |
| 229 | int hw_initialized; |
| 230 | /* hw_working=-1 means not tested yet */ |
| 231 | int hw_working; |
| 232 | atomic_t enabled; |
| 233 | int on_before_suspend; |
| 234 | |
| 235 | u8 sensitivity; |
| 236 | |
| 237 | u8 resume_state[RESUME_ENTRIES]; |
| 238 | |
| 239 | int irq1; |
| 240 | struct work_struct irq1_work; |
| 241 | struct workqueue_struct *irq1_work_queue; |
| 242 | int irq2; |
| 243 | struct work_struct irq2_work; |
| 244 | struct workqueue_struct *irq2_work_queue; |
| 245 | |
| 246 | #ifdef DEBUG |
| 247 | u8 reg_addr; |
| 248 | #endif |
| 249 | }; |
| 250 | |
| 251 | struct sensor_regulator { |
| 252 | struct regulator *vreg; |
| 253 | const char *name; |
| 254 | u32 min_uV; |
| 255 | u32 max_uV; |
| 256 | }; |
| 257 | |
| 258 | struct sensor_regulator lis3dh_acc_vreg[] = { |
| 259 | {NULL, "vdd", 1700000, 3600000}, |
| 260 | {NULL, "vddio", 1700000, 3600000}, |
| 261 | }; |
| 262 | |
| 263 | static int lis3dh_acc_config_regulator(struct lis3dh_acc_data *acc, bool on) |
| 264 | { |
| 265 | int rc = 0, i; |
| 266 | int num_reg = sizeof(lis3dh_acc_vreg) / sizeof(struct sensor_regulator); |
| 267 | |
| 268 | if (on) { |
| 269 | for (i = 0; i < num_reg; i++) { |
| 270 | lis3dh_acc_vreg[i].vreg = |
| 271 | regulator_get(&acc->client->dev, |
| 272 | lis3dh_acc_vreg[i].name); |
| 273 | if (IS_ERR(lis3dh_acc_vreg[i].vreg)) { |
| 274 | rc = PTR_ERR(lis3dh_acc_vreg[i].vreg); |
| 275 | pr_err("%s:regulator get failed rc=%d\n", |
| 276 | __func__, rc); |
| Wentao Xu | 7a69d3b | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 277 | lis3dh_acc_vreg[i].vreg = NULL; |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 278 | goto error_vdd; |
| 279 | } |
| 280 | |
| 281 | if (regulator_count_voltages( |
| 282 | lis3dh_acc_vreg[i].vreg) > 0) { |
| 283 | rc = regulator_set_voltage( |
| 284 | lis3dh_acc_vreg[i].vreg, |
| 285 | lis3dh_acc_vreg[i].min_uV, |
| 286 | lis3dh_acc_vreg[i].max_uV); |
| 287 | if (rc) { |
| 288 | pr_err("%s: set voltage failed rc=%d\n", |
| 289 | __func__, rc); |
| 290 | regulator_put(lis3dh_acc_vreg[i].vreg); |
| Wentao Xu | 7a69d3b | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 291 | lis3dh_acc_vreg[i].vreg = NULL; |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 292 | goto error_vdd; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | rc = regulator_enable(lis3dh_acc_vreg[i].vreg); |
| 297 | if (rc) { |
| 298 | pr_err("%s: regulator_enable failed rc =%d\n", |
| 299 | __func__, rc); |
| 300 | if (regulator_count_voltages( |
| 301 | lis3dh_acc_vreg[i].vreg) > 0) { |
| 302 | regulator_set_voltage( |
| 303 | lis3dh_acc_vreg[i].vreg, 0, |
| 304 | lis3dh_acc_vreg[i].max_uV); |
| 305 | } |
| 306 | regulator_put(lis3dh_acc_vreg[i].vreg); |
| Wentao Xu | 7a69d3b | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 307 | lis3dh_acc_vreg[i].vreg = NULL; |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 308 | goto error_vdd; |
| 309 | } |
| 310 | } |
| 311 | return rc; |
| 312 | } else { |
| 313 | i = num_reg; |
| 314 | } |
| 315 | |
| 316 | error_vdd: |
| 317 | while (--i >= 0) { |
| Wentao Xu | 7a69d3b | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 318 | if (!IS_ERR_OR_NULL(lis3dh_acc_vreg[i].vreg)) { |
| 319 | if (regulator_count_voltages( |
| 320 | lis3dh_acc_vreg[i].vreg) > 0) { |
| 321 | regulator_set_voltage(lis3dh_acc_vreg[i].vreg, |
| 322 | 0, lis3dh_acc_vreg[i].max_uV); |
| 323 | } |
| 324 | regulator_disable(lis3dh_acc_vreg[i].vreg); |
| 325 | regulator_put(lis3dh_acc_vreg[i].vreg); |
| 326 | lis3dh_acc_vreg[i].vreg = NULL; |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 327 | } |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 328 | } |
| 329 | return rc; |
| 330 | } |
| 331 | |
| 332 | static int lis3dh_acc_i2c_read(struct lis3dh_acc_data *acc, |
| 333 | u8 *buf, int len) |
| 334 | { |
| 335 | int err; |
| 336 | int tries = 0; |
| 337 | |
| 338 | struct i2c_msg msgs[] = { |
| 339 | { |
| 340 | .addr = acc->client->addr, |
| 341 | .flags = acc->client->flags & I2C_M_TEN, |
| 342 | .len = 1, |
| 343 | .buf = buf, |
| 344 | }, |
| 345 | { |
| 346 | .addr = acc->client->addr, |
| 347 | .flags = (acc->client->flags & I2C_M_TEN) | I2C_M_RD, |
| 348 | .len = len, |
| 349 | .buf = buf, |
| 350 | }, |
| 351 | }; |
| 352 | |
| 353 | do { |
| 354 | err = i2c_transfer(acc->client->adapter, msgs, 2); |
| 355 | if (err != 2) |
| 356 | msleep_interruptible(I2C_RETRY_DELAY); |
| 357 | } while ((err != 2) && (++tries < I2C_RETRIES)); |
| 358 | |
| 359 | if (err != 2) { |
| 360 | dev_err(&acc->client->dev, "read transfer error\n"); |
| 361 | err = -EIO; |
| 362 | } else { |
| 363 | err = 0; |
| 364 | } |
| 365 | |
| 366 | return err; |
| 367 | } |
| 368 | |
| 369 | static int lis3dh_acc_i2c_write(struct lis3dh_acc_data *acc, u8 *buf, int len) |
| 370 | { |
| 371 | int err; |
| 372 | int tries = 0; |
| 373 | |
| 374 | struct i2c_msg msgs[] = { |
| 375 | { |
| 376 | .addr = acc->client->addr, |
| 377 | .flags = acc->client->flags & I2C_M_TEN, |
| 378 | .len = len + 1, |
| 379 | .buf = buf, |
| 380 | }, |
| 381 | }; |
| 382 | |
| 383 | do { |
| 384 | err = i2c_transfer(acc->client->adapter, msgs, 1); |
| 385 | if (err != 1) |
| 386 | msleep_interruptible(I2C_RETRY_DELAY); |
| 387 | } while ((err != 1) && (++tries < I2C_RETRIES)); |
| 388 | |
| 389 | if (err != 1) { |
| 390 | dev_err(&acc->client->dev, "write transfer error\n"); |
| 391 | err = -EIO; |
| 392 | } else { |
| 393 | err = 0; |
| 394 | } |
| 395 | |
| 396 | return err; |
| 397 | } |
| 398 | |
| 399 | static int lis3dh_acc_hw_init(struct lis3dh_acc_data *acc) |
| 400 | { |
| 401 | int err = -1; |
| 402 | u8 buf[7]; |
| 403 | |
| 404 | printk(KERN_INFO "%s: hw init start\n", LIS3DH_ACC_DEV_NAME); |
| 405 | |
| 406 | buf[0] = WHO_AM_I; |
| 407 | err = lis3dh_acc_i2c_read(acc, buf, 1); |
| 408 | if (err < 0) { |
| 409 | dev_warn(&acc->client->dev, |
| 410 | "Error reading WHO_AM_I: is device available/working?\n"); |
| 411 | goto err_firstread; |
| 412 | } else |
| 413 | acc->hw_working = 1; |
| 414 | if (buf[0] != WHOAMI_LIS3DH_ACC) { |
| 415 | dev_err(&acc->client->dev, |
| 416 | "device unknown. Expected: 0x%x, Replies: 0x%x\n", |
| 417 | WHOAMI_LIS3DH_ACC, buf[0]); |
| 418 | err = -1; /* choose the right coded error */ |
| 419 | goto err_unknown_device; |
| 420 | } |
| 421 | |
| 422 | buf[0] = CTRL_REG1; |
| 423 | buf[1] = acc->resume_state[RES_CTRL_REG1]; |
| 424 | err = lis3dh_acc_i2c_write(acc, buf, 1); |
| 425 | if (err < 0) |
| 426 | goto err_resume_state; |
| 427 | |
| 428 | buf[0] = TEMP_CFG_REG; |
| 429 | buf[1] = acc->resume_state[RES_TEMP_CFG_REG]; |
| 430 | err = lis3dh_acc_i2c_write(acc, buf, 1); |
| 431 | if (err < 0) |
| 432 | goto err_resume_state; |
| 433 | |
| 434 | buf[0] = FIFO_CTRL_REG; |
| 435 | buf[1] = acc->resume_state[RES_FIFO_CTRL_REG]; |
| 436 | err = lis3dh_acc_i2c_write(acc, buf, 1); |
| 437 | if (err < 0) |
| 438 | goto err_resume_state; |
| 439 | |
| 440 | buf[0] = (I2C_AUTO_INCREMENT | TT_THS); |
| 441 | buf[1] = acc->resume_state[RES_TT_THS]; |
| 442 | buf[2] = acc->resume_state[RES_TT_LIM]; |
| 443 | buf[3] = acc->resume_state[RES_TT_TLAT]; |
| 444 | buf[4] = acc->resume_state[RES_TT_TW]; |
| 445 | err = lis3dh_acc_i2c_write(acc, buf, 4); |
| 446 | if (err < 0) |
| 447 | goto err_resume_state; |
| 448 | buf[0] = TT_CFG; |
| 449 | buf[1] = acc->resume_state[RES_TT_CFG]; |
| 450 | err = lis3dh_acc_i2c_write(acc, buf, 1); |
| 451 | if (err < 0) |
| 452 | goto err_resume_state; |
| 453 | |
| 454 | buf[0] = (I2C_AUTO_INCREMENT | INT_THS1); |
| 455 | buf[1] = acc->resume_state[RES_INT_THS1]; |
| 456 | buf[2] = acc->resume_state[RES_INT_DUR1]; |
| 457 | err = lis3dh_acc_i2c_write(acc, buf, 2); |
| 458 | if (err < 0) |
| 459 | goto err_resume_state; |
| 460 | buf[0] = INT_CFG1; |
| 461 | buf[1] = acc->resume_state[RES_INT_CFG1]; |
| 462 | err = lis3dh_acc_i2c_write(acc, buf, 1); |
| 463 | if (err < 0) |
| 464 | goto err_resume_state; |
| 465 | |
| 466 | |
| 467 | buf[0] = (I2C_AUTO_INCREMENT | CTRL_REG2); |
| 468 | buf[1] = acc->resume_state[RES_CTRL_REG2]; |
| 469 | buf[2] = acc->resume_state[RES_CTRL_REG3]; |
| 470 | buf[3] = acc->resume_state[RES_CTRL_REG4]; |
| 471 | buf[4] = acc->resume_state[RES_CTRL_REG5]; |
| 472 | buf[5] = acc->resume_state[RES_CTRL_REG6]; |
| 473 | err = lis3dh_acc_i2c_write(acc, buf, 5); |
| 474 | if (err < 0) |
| 475 | goto err_resume_state; |
| 476 | |
| 477 | acc->hw_initialized = 1; |
| 478 | printk(KERN_INFO "%s: hw init done\n", LIS3DH_ACC_DEV_NAME); |
| 479 | return 0; |
| 480 | |
| 481 | err_firstread: |
| 482 | acc->hw_working = 0; |
| 483 | err_unknown_device: |
| 484 | err_resume_state: |
| 485 | acc->hw_initialized = 0; |
| 486 | dev_err(&acc->client->dev, "hw init error 0x%x,0x%x: %d\n", buf[0], |
| 487 | buf[1], err); |
| 488 | return err; |
| 489 | } |
| 490 | |
| 491 | static void lis3dh_acc_device_power_off(struct lis3dh_acc_data *acc) |
| 492 | { |
| 493 | int err; |
| 494 | u8 buf[2] = { CTRL_REG1, LIS3DH_ACC_PM_OFF }; |
| 495 | |
| 496 | err = lis3dh_acc_i2c_write(acc, buf, 1); |
| 497 | if (err < 0) |
| 498 | dev_err(&acc->client->dev, "soft power off failed: %d\n", err); |
| 499 | |
| 500 | if (acc->pdata->gpio_int1) |
| 501 | disable_irq_nosync(acc->irq1); |
| 502 | if (acc->pdata->gpio_int2) |
| 503 | disable_irq_nosync(acc->irq2); |
| 504 | |
| 505 | lis3dh_acc_config_regulator(acc, false); |
| 506 | |
| 507 | if (acc->hw_initialized) { |
| 508 | if (acc->pdata->gpio_int1) |
| 509 | disable_irq_nosync(acc->irq1); |
| 510 | if (acc->pdata->gpio_int2) |
| 511 | disable_irq_nosync(acc->irq2); |
| 512 | acc->hw_initialized = 0; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | static int lis3dh_acc_device_power_on(struct lis3dh_acc_data *acc) |
| 517 | { |
| 518 | int err = -1; |
| 519 | |
| 520 | err = lis3dh_acc_config_regulator(acc, true); |
| 521 | if (err < 0) { |
| 522 | dev_err(&acc->client->dev, |
| 523 | "power_on failed: %d\n", err); |
| 524 | return err; |
| 525 | } |
| 526 | |
| 527 | if (acc->pdata->gpio_int1 >= 0) |
| 528 | enable_irq(acc->irq1); |
| 529 | if (acc->pdata->gpio_int2 >= 0) |
| 530 | enable_irq(acc->irq2); |
| 531 | |
| 532 | msleep(20); |
| 533 | |
| 534 | if (!acc->hw_initialized) { |
| 535 | err = lis3dh_acc_hw_init(acc); |
| 536 | if (acc->hw_working == 1 && err < 0) { |
| 537 | lis3dh_acc_device_power_off(acc); |
| 538 | return err; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (acc->hw_initialized) { |
| 543 | if (acc->pdata->gpio_int1 >= 0) |
| 544 | enable_irq(acc->irq1); |
| 545 | if (acc->pdata->gpio_int2 >= 0) |
| 546 | enable_irq(acc->irq2); |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static irqreturn_t lis3dh_acc_isr1(int irq, void *dev) |
| 552 | { |
| 553 | struct lis3dh_acc_data *acc = dev; |
| 554 | |
| 555 | disable_irq_nosync(irq); |
| 556 | queue_work(acc->irq1_work_queue, &acc->irq1_work); |
| 557 | #ifdef DEBUG |
| 558 | printk(KERN_INFO "%s: isr1 queued\n", LIS3DH_ACC_DEV_NAME); |
| 559 | #endif |
| 560 | return IRQ_HANDLED; |
| 561 | } |
| 562 | |
| 563 | static irqreturn_t lis3dh_acc_isr2(int irq, void *dev) |
| 564 | { |
| 565 | struct lis3dh_acc_data *acc = dev; |
| 566 | |
| 567 | disable_irq_nosync(irq); |
| 568 | queue_work(acc->irq2_work_queue, &acc->irq2_work); |
| 569 | #ifdef DEBUG |
| 570 | printk(KERN_INFO "%s: isr2 queued\n", LIS3DH_ACC_DEV_NAME); |
| 571 | #endif |
| 572 | return IRQ_HANDLED; |
| 573 | } |
| 574 | |
| 575 | static void lis3dh_acc_irq1_work_func(struct work_struct *work) |
| 576 | { |
| 577 | |
| 578 | struct lis3dh_acc_data *acc = |
| 579 | container_of(work, struct lis3dh_acc_data, irq1_work); |
| 580 | /* TODO add interrupt service procedure. |
| 581 | ie:lis3dh_acc_get_int1_source(acc); */ |
| 582 | ; |
| 583 | /* */ |
| 584 | printk(KERN_INFO "%s: IRQ1 triggered\n", LIS3DH_ACC_DEV_NAME); |
| 585 | goto exit; |
| 586 | exit: |
| 587 | enable_irq(acc->irq1); |
| 588 | } |
| 589 | |
| 590 | static void lis3dh_acc_irq2_work_func(struct work_struct *work) |
| 591 | { |
| 592 | |
| 593 | struct lis3dh_acc_data *acc = |
| 594 | container_of(work, struct lis3dh_acc_data, irq2_work); |
| 595 | /* TODO add interrupt service procedure. |
| 596 | ie:lis3dh_acc_get_tap_source(acc); */ |
| 597 | ; |
| 598 | /* */ |
| 599 | printk(KERN_INFO "%s: IRQ2 triggered\n", LIS3DH_ACC_DEV_NAME); |
| 600 | goto exit; |
| 601 | exit: |
| 602 | enable_irq(acc->irq2); |
| 603 | } |
| 604 | |
| 605 | int lis3dh_acc_update_g_range(struct lis3dh_acc_data *acc, u8 new_g_range) |
| 606 | { |
| 607 | int err = -1; |
| 608 | |
| 609 | u8 sensitivity; |
| 610 | u8 buf[2]; |
| 611 | u8 updated_val; |
| 612 | u8 init_val; |
| 613 | u8 new_val; |
| 614 | u8 mask = LIS3DH_ACC_FS_MASK | HIGH_RESOLUTION; |
| 615 | |
| 616 | switch (new_g_range) { |
| 617 | case LIS3DH_ACC_G_2G: |
| 618 | |
| 619 | sensitivity = SENSITIVITY_2G; |
| 620 | break; |
| 621 | case LIS3DH_ACC_G_4G: |
| 622 | |
| 623 | sensitivity = SENSITIVITY_4G; |
| 624 | break; |
| 625 | case LIS3DH_ACC_G_8G: |
| 626 | |
| 627 | sensitivity = SENSITIVITY_8G; |
| 628 | break; |
| 629 | case LIS3DH_ACC_G_16G: |
| 630 | |
| 631 | sensitivity = SENSITIVITY_16G; |
| 632 | break; |
| 633 | default: |
| 634 | dev_err(&acc->client->dev, "invalid g range requested: %u\n", |
| 635 | new_g_range); |
| 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | if (atomic_read(&acc->enabled)) { |
| 640 | /* Updates configuration register 4, |
| 641 | * which contains g range setting */ |
| 642 | buf[0] = CTRL_REG4; |
| 643 | err = lis3dh_acc_i2c_read(acc, buf, 1); |
| 644 | if (err < 0) |
| 645 | goto error; |
| 646 | init_val = buf[0]; |
| 647 | acc->resume_state[RES_CTRL_REG4] = init_val; |
| 648 | new_val = new_g_range | HIGH_RESOLUTION; |
| 649 | updated_val = ((mask & new_val) | ((~mask) & init_val)); |
| 650 | buf[1] = updated_val; |
| 651 | buf[0] = CTRL_REG4; |
| 652 | err = lis3dh_acc_i2c_write(acc, buf, 1); |
| 653 | if (err < 0) |
| 654 | goto error; |
| 655 | acc->resume_state[RES_CTRL_REG4] = updated_val; |
| 656 | acc->sensitivity = sensitivity; |
| 657 | } |
| 658 | |
| 659 | |
| 660 | return err; |
| 661 | error: |
| 662 | dev_err(&acc->client->dev, "update g range failed 0x%x,0x%x: %d\n", |
| 663 | buf[0], buf[1], err); |
| 664 | |
| 665 | return err; |
| 666 | } |
| 667 | |
| 668 | int lis3dh_acc_update_odr(struct lis3dh_acc_data *acc, int poll_interval_ms) |
| 669 | { |
| 670 | int err = -1; |
| 671 | int i; |
| 672 | u8 config[2]; |
| 673 | |
| 674 | /* Following, looks for the longest possible odr interval scrolling the |
| 675 | * odr_table vector from the end (shortest interval) backward (longest |
| 676 | * interval), to support the poll_interval requested by the system. |
| 677 | * It must be the longest interval lower then the poll interval.*/ |
| 678 | for (i = ARRAY_SIZE(lis3dh_acc_odr_table) - 1; i >= 0; i--) { |
| 679 | if (lis3dh_acc_odr_table[i].cutoff_ms <= poll_interval_ms) |
| 680 | break; |
| 681 | } |
| 682 | config[1] = lis3dh_acc_odr_table[i].mask; |
| 683 | |
| 684 | config[1] |= LIS3DH_ACC_ENABLE_ALL_AXES; |
| 685 | |
| 686 | /* If device is currently enabled, we need to write new |
| 687 | * configuration out to it */ |
| 688 | if (atomic_read(&acc->enabled)) { |
| 689 | config[0] = CTRL_REG1; |
| 690 | err = lis3dh_acc_i2c_write(acc, config, 1); |
| 691 | if (err < 0) |
| 692 | goto error; |
| 693 | acc->resume_state[RES_CTRL_REG1] = config[1]; |
| 694 | } |
| 695 | |
| 696 | return err; |
| 697 | |
| 698 | error: |
| 699 | dev_err(&acc->client->dev, "update odr failed 0x%x,0x%x: %d\n", |
| 700 | config[0], config[1], err); |
| 701 | |
| 702 | return err; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | |
| 707 | static int lis3dh_acc_register_write(struct lis3dh_acc_data *acc, u8 *buf, |
| 708 | u8 reg_address, u8 new_value) |
| 709 | { |
| 710 | int err = -1; |
| 711 | |
| 712 | /* Sets configuration register at reg_address |
| 713 | * NOTE: this is a straight overwrite */ |
| 714 | buf[0] = reg_address; |
| 715 | buf[1] = new_value; |
| 716 | err = lis3dh_acc_i2c_write(acc, buf, 1); |
| 717 | if (err < 0) |
| 718 | return err; |
| 719 | return err; |
| 720 | } |
| 721 | |
| 722 | static int lis3dh_acc_get_acceleration_data(struct lis3dh_acc_data *acc, |
| 723 | int *xyz) |
| 724 | { |
| 725 | int err = -1; |
| 726 | /* Data bytes from hardware xL, xH, yL, yH, zL, zH */ |
| 727 | u8 acc_data[6]; |
| 728 | /* x,y,z hardware data */ |
| 729 | s16 hw_d[3] = { 0 }; |
| 730 | |
| 731 | acc_data[0] = (I2C_AUTO_INCREMENT | AXISDATA_REG); |
| 732 | err = lis3dh_acc_i2c_read(acc, acc_data, 6); |
| 733 | if (err < 0) |
| 734 | return err; |
| 735 | |
| 736 | hw_d[0] = (((s16) ((acc_data[1] << 8) | acc_data[0])) >> 4); |
| 737 | hw_d[1] = (((s16) ((acc_data[3] << 8) | acc_data[2])) >> 4); |
| 738 | hw_d[2] = (((s16) ((acc_data[5] << 8) | acc_data[4])) >> 4); |
| 739 | |
| 740 | hw_d[0] = hw_d[0] * acc->sensitivity; |
| 741 | hw_d[1] = hw_d[1] * acc->sensitivity; |
| 742 | hw_d[2] = hw_d[2] * acc->sensitivity; |
| 743 | |
| 744 | |
| 745 | xyz[0] = ((acc->pdata->negate_x) ? (-hw_d[acc->pdata->axis_map_x]) |
| 746 | : (hw_d[acc->pdata->axis_map_x])); |
| 747 | xyz[1] = ((acc->pdata->negate_y) ? (-hw_d[acc->pdata->axis_map_y]) |
| 748 | : (hw_d[acc->pdata->axis_map_y])); |
| 749 | xyz[2] = ((acc->pdata->negate_z) ? (-hw_d[acc->pdata->axis_map_z]) |
| 750 | : (hw_d[acc->pdata->axis_map_z])); |
| 751 | |
| 752 | #ifdef DEBUG |
| 753 | /* |
| 754 | printk(KERN_INFO "%s read x=%d, y=%d, z=%d\n", |
| 755 | LIS3DH_ACC_DEV_NAME, xyz[0], xyz[1], xyz[2]); |
| 756 | */ |
| 757 | #endif |
| 758 | return err; |
| 759 | } |
| 760 | |
| 761 | static void lis3dh_acc_report_values(struct lis3dh_acc_data *acc, |
| 762 | int *xyz) |
| 763 | { |
| 764 | input_report_abs(acc->input_dev, ABS_X, xyz[0]); |
| 765 | input_report_abs(acc->input_dev, ABS_Y, xyz[1]); |
| 766 | input_report_abs(acc->input_dev, ABS_Z, xyz[2]); |
| 767 | input_sync(acc->input_dev); |
| 768 | } |
| 769 | |
| 770 | static int lis3dh_acc_enable(struct lis3dh_acc_data *acc) |
| 771 | { |
| 772 | int err; |
| 773 | |
| 774 | if (!atomic_cmpxchg(&acc->enabled, 0, 1)) { |
| 775 | err = lis3dh_acc_device_power_on(acc); |
| 776 | if (err < 0) { |
| 777 | atomic_set(&acc->enabled, 0); |
| 778 | return err; |
| 779 | } |
| 780 | schedule_delayed_work(&acc->input_work, |
| 781 | msecs_to_jiffies(acc->pdata->poll_interval)); |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static int lis3dh_acc_disable(struct lis3dh_acc_data *acc) |
| 788 | { |
| 789 | if (atomic_cmpxchg(&acc->enabled, 1, 0)) { |
| 790 | cancel_delayed_work_sync(&acc->input_work); |
| 791 | lis3dh_acc_device_power_off(acc); |
| 792 | } |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | static ssize_t read_single_reg(struct device *dev, char *buf, u8 reg) |
| 799 | { |
| 800 | ssize_t ret; |
| 801 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 802 | int err; |
| 803 | |
| 804 | u8 data = reg; |
| 805 | err = lis3dh_acc_i2c_read(acc, &data, 1); |
| 806 | if (err < 0) |
| 807 | return err; |
| 808 | ret = snprintf(buf, 4, "0x%02x\n", data); |
| 809 | return ret; |
| 810 | |
| 811 | } |
| 812 | |
| 813 | static int write_reg(struct device *dev, const char *buf, u8 reg, |
| 814 | u8 mask, int resumeIndex) |
| 815 | { |
| 816 | int err = -1; |
| 817 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 818 | u8 x[2]; |
| 819 | u8 new_val; |
| 820 | unsigned long val; |
| 821 | |
| 822 | if (kstrtoul(buf, 16, &val)) |
| 823 | return -EINVAL; |
| 824 | |
| 825 | new_val = ((u8) val & mask); |
| 826 | x[0] = reg; |
| 827 | x[1] = new_val; |
| 828 | err = lis3dh_acc_register_write(acc, x, reg, new_val); |
| 829 | if (err < 0) |
| 830 | return err; |
| 831 | acc->resume_state[resumeIndex] = new_val; |
| 832 | return err; |
| 833 | } |
| 834 | |
| 835 | static ssize_t attr_get_polling_rate(struct device *dev, |
| 836 | struct device_attribute *attr, |
| 837 | char *buf) |
| 838 | { |
| 839 | int val; |
| 840 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 841 | mutex_lock(&acc->lock); |
| 842 | val = acc->pdata->poll_interval; |
| 843 | mutex_unlock(&acc->lock); |
| 844 | return snprintf(buf, 8, "%d\n", val); |
| 845 | } |
| 846 | |
| 847 | static ssize_t attr_set_polling_rate(struct device *dev, |
| 848 | struct device_attribute *attr, |
| 849 | const char *buf, size_t size) |
| 850 | { |
| 851 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 852 | unsigned long interval_ms; |
| 853 | |
| 854 | if (kstrtoul(buf, 10, &interval_ms)) |
| 855 | return -EINVAL; |
| 856 | if (!interval_ms) |
| 857 | return -EINVAL; |
| 858 | mutex_lock(&acc->lock); |
| 859 | acc->pdata->poll_interval = interval_ms; |
| 860 | lis3dh_acc_update_odr(acc, interval_ms); |
| 861 | mutex_unlock(&acc->lock); |
| 862 | return size; |
| 863 | } |
| 864 | |
| 865 | static ssize_t attr_get_range(struct device *dev, |
| 866 | struct device_attribute *attr, char *buf) |
| 867 | { |
| 868 | char val; |
| 869 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 870 | char range = 2; |
| 871 | mutex_lock(&acc->lock); |
| 872 | val = acc->pdata->g_range ; |
| 873 | switch (val) { |
| 874 | case LIS3DH_ACC_G_2G: |
| 875 | range = 2; |
| 876 | break; |
| 877 | case LIS3DH_ACC_G_4G: |
| 878 | range = 4; |
| 879 | break; |
| 880 | case LIS3DH_ACC_G_8G: |
| 881 | range = 8; |
| 882 | break; |
| 883 | case LIS3DH_ACC_G_16G: |
| 884 | range = 16; |
| 885 | break; |
| 886 | } |
| 887 | mutex_unlock(&acc->lock); |
| 888 | return snprintf(buf, 4, "%d\n", range); |
| 889 | } |
| 890 | |
| 891 | static ssize_t attr_set_range(struct device *dev, |
| 892 | struct device_attribute *attr, |
| 893 | const char *buf, size_t size) |
| 894 | { |
| 895 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 896 | unsigned long val; |
| 897 | if (kstrtoul(buf, 10, &val)) |
| 898 | return -EINVAL; |
| 899 | mutex_lock(&acc->lock); |
| 900 | acc->pdata->g_range = val; |
| 901 | lis3dh_acc_update_g_range(acc, val); |
| 902 | mutex_unlock(&acc->lock); |
| 903 | return size; |
| 904 | } |
| 905 | |
| 906 | static ssize_t attr_get_enable(struct device *dev, |
| 907 | struct device_attribute *attr, char *buf) |
| 908 | { |
| 909 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 910 | int val = atomic_read(&acc->enabled); |
| 911 | return snprintf(buf, sizeof(val) + 2, "%d\n", val); |
| 912 | } |
| 913 | |
| 914 | static ssize_t attr_set_enable(struct device *dev, |
| 915 | struct device_attribute *attr, |
| 916 | const char *buf, size_t size) |
| 917 | { |
| 918 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 919 | unsigned long val; |
| 920 | |
| 921 | if (kstrtoul(buf, 10, &val)) |
| 922 | return -EINVAL; |
| 923 | |
| 924 | if (val) |
| 925 | lis3dh_acc_enable(acc); |
| 926 | else |
| 927 | lis3dh_acc_disable(acc); |
| 928 | |
| 929 | return size; |
| 930 | } |
| 931 | |
| 932 | static ssize_t attr_set_intconfig1(struct device *dev, |
| 933 | struct device_attribute *attr, const char *buf, size_t size) |
| 934 | { |
| 935 | return write_reg(dev, buf, INT_CFG1, NO_MASK, RES_INT_CFG1); |
| 936 | } |
| 937 | |
| 938 | static ssize_t attr_get_intconfig1(struct device *dev, |
| 939 | struct device_attribute *attr, char *buf) |
| 940 | { |
| 941 | return read_single_reg(dev, buf, INT_CFG1); |
| 942 | } |
| 943 | |
| 944 | static ssize_t attr_set_duration1(struct device *dev, |
| 945 | struct device_attribute *attr, const char *buf, size_t size) |
| 946 | { |
| 947 | return write_reg(dev, buf, INT_DUR1, INT1_DURATION_MASK, RES_INT_DUR1); |
| 948 | } |
| 949 | |
| 950 | static ssize_t attr_get_duration1(struct device *dev, |
| 951 | struct device_attribute *attr, char *buf) |
| 952 | { |
| 953 | return read_single_reg(dev, buf, INT_DUR1); |
| 954 | } |
| 955 | |
| 956 | static ssize_t attr_set_thresh1(struct device *dev, |
| 957 | struct device_attribute *attr, const char *buf, size_t size) |
| 958 | { |
| 959 | return write_reg(dev, buf, INT_THS1, INT1_THRESHOLD_MASK, RES_INT_THS1); |
| 960 | } |
| 961 | |
| 962 | static ssize_t attr_get_thresh1(struct device *dev, |
| 963 | struct device_attribute *attr, char *buf) |
| 964 | { |
| 965 | return read_single_reg(dev, buf, INT_THS1); |
| 966 | } |
| 967 | |
| 968 | static ssize_t attr_get_source1(struct device *dev, |
| 969 | struct device_attribute *attr, char *buf) |
| 970 | { |
| 971 | return read_single_reg(dev, buf, INT_SRC1); |
| 972 | } |
| 973 | |
| 974 | static ssize_t attr_set_click_cfg(struct device *dev, |
| 975 | struct device_attribute *attr, const char *buf, size_t size) |
| 976 | { |
| 977 | return write_reg(dev, buf, TT_CFG, TAP_CFG_MASK, RES_TT_CFG); |
| 978 | } |
| 979 | |
| 980 | static ssize_t attr_get_click_cfg(struct device *dev, |
| 981 | struct device_attribute *attr, char *buf) |
| 982 | { |
| 983 | |
| 984 | return read_single_reg(dev, buf, TT_CFG); |
| 985 | } |
| 986 | |
| 987 | static ssize_t attr_get_click_source(struct device *dev, |
| 988 | struct device_attribute *attr, char *buf) |
| 989 | { |
| 990 | return read_single_reg(dev, buf, TT_SRC); |
| 991 | } |
| 992 | |
| 993 | static ssize_t attr_set_click_ths(struct device *dev, |
| 994 | struct device_attribute *attr, const char *buf, size_t size) |
| 995 | { |
| 996 | return write_reg(dev, buf, TT_THS, TAP_THS_MASK, RES_TT_THS); |
| 997 | } |
| 998 | |
| 999 | static ssize_t attr_get_click_ths(struct device *dev, |
| 1000 | struct device_attribute *attr, char *buf) |
| 1001 | { |
| 1002 | return read_single_reg(dev, buf, TT_THS); |
| 1003 | } |
| 1004 | |
| 1005 | static ssize_t attr_set_click_tlim(struct device *dev, |
| 1006 | struct device_attribute *attr, const char *buf, size_t size) |
| 1007 | { |
| 1008 | return write_reg(dev, buf, TT_LIM, TAP_TLIM_MASK, RES_TT_LIM); |
| 1009 | } |
| 1010 | |
| 1011 | static ssize_t attr_get_click_tlim(struct device *dev, |
| 1012 | struct device_attribute *attr, char *buf) |
| 1013 | { |
| 1014 | return read_single_reg(dev, buf, TT_LIM); |
| 1015 | } |
| 1016 | |
| 1017 | static ssize_t attr_set_click_tlat(struct device *dev, |
| 1018 | struct device_attribute *attr, const char *buf, size_t size) |
| 1019 | { |
| 1020 | return write_reg(dev, buf, TT_TLAT, TAP_TLAT_MASK, RES_TT_TLAT); |
| 1021 | } |
| 1022 | |
| 1023 | static ssize_t attr_get_click_tlat(struct device *dev, |
| 1024 | struct device_attribute *attr, char *buf) |
| 1025 | { |
| 1026 | return read_single_reg(dev, buf, TT_TLAT); |
| 1027 | } |
| 1028 | |
| 1029 | static ssize_t attr_set_click_tw(struct device *dev, |
| 1030 | struct device_attribute *attr, const char *buf, size_t size) |
| 1031 | { |
| 1032 | return write_reg(dev, buf, TT_TLAT, TAP_TW_MASK, RES_TT_TLAT); |
| 1033 | } |
| 1034 | |
| 1035 | static ssize_t attr_get_click_tw(struct device *dev, |
| 1036 | struct device_attribute *attr, char *buf) |
| 1037 | { |
| 1038 | return read_single_reg(dev, buf, TT_TLAT); |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | #ifdef DEBUG |
| 1043 | /* PAY ATTENTION: These DEBUG funtions don't manage resume_state */ |
| 1044 | static ssize_t attr_reg_set(struct device *dev, struct device_attribute *attr, |
| 1045 | const char *buf, size_t size) |
| 1046 | { |
| 1047 | int rc; |
| 1048 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 1049 | u8 x[2]; |
| 1050 | unsigned long val; |
| 1051 | |
| 1052 | if (kstrtoul(buf, 16, &val)) |
| 1053 | return -EINVAL; |
| 1054 | mutex_lock(&acc->lock); |
| 1055 | x[0] = acc->reg_addr; |
| 1056 | mutex_unlock(&acc->lock); |
| 1057 | x[1] = val; |
| 1058 | rc = lis3dh_acc_i2c_write(acc, x, 1); |
| 1059 | /*TODO: error need to be managed */ |
| 1060 | return size; |
| 1061 | } |
| 1062 | |
| 1063 | static ssize_t attr_reg_get(struct device *dev, struct device_attribute *attr, |
| 1064 | char *buf) |
| 1065 | { |
| 1066 | ssize_t ret; |
| 1067 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 1068 | int rc; |
| 1069 | u8 data; |
| 1070 | |
| 1071 | mutex_lock(&acc->lock); |
| 1072 | data = acc->reg_addr; |
| 1073 | mutex_unlock(&acc->lock); |
| 1074 | rc = lis3dh_acc_i2c_read(acc, &data, 1); |
| 1075 | /* TODO: error need to be managed */ |
| 1076 | ret = snprintf(buf, 8, "0x%02x\n", data); |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
| 1080 | static ssize_t attr_addr_set(struct device *dev, struct device_attribute *attr, |
| 1081 | const char *buf, size_t size) |
| 1082 | { |
| 1083 | struct lis3dh_acc_data *acc = dev_get_drvdata(dev); |
| 1084 | unsigned long val; |
| 1085 | if (kstrtoul(buf, 16, &val)) |
| 1086 | return -EINVAL; |
| 1087 | mutex_lock(&acc->lock); |
| 1088 | acc->reg_addr = val; |
| 1089 | mutex_unlock(&acc->lock); |
| 1090 | return size; |
| 1091 | } |
| 1092 | #endif |
| 1093 | |
| 1094 | static struct device_attribute attributes[] = { |
| 1095 | |
| Wentao Xu | 82e6305 | 2012-11-15 16:30:15 -0500 | [diff] [blame] | 1096 | __ATTR(pollrate_ms, 0664, attr_get_polling_rate, |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 1097 | attr_set_polling_rate), |
| Wentao Xu | 82e6305 | 2012-11-15 16:30:15 -0500 | [diff] [blame] | 1098 | __ATTR(range, 0664, attr_get_range, attr_set_range), |
| 1099 | __ATTR(enable, 0664, attr_get_enable, attr_set_enable), |
| 1100 | __ATTR(int1_config, 0664, attr_get_intconfig1, attr_set_intconfig1), |
| 1101 | __ATTR(int1_duration, 0664, attr_get_duration1, attr_set_duration1), |
| 1102 | __ATTR(int1_threshold, 0664, attr_get_thresh1, attr_set_thresh1), |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 1103 | __ATTR(int1_source, 0444, attr_get_source1, NULL), |
| Wentao Xu | 82e6305 | 2012-11-15 16:30:15 -0500 | [diff] [blame] | 1104 | __ATTR(click_config, 0664, attr_get_click_cfg, attr_set_click_cfg), |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 1105 | __ATTR(click_source, 0444, attr_get_click_source, NULL), |
| Wentao Xu | 82e6305 | 2012-11-15 16:30:15 -0500 | [diff] [blame] | 1106 | __ATTR(click_threshold, 0664, attr_get_click_ths, attr_set_click_ths), |
| 1107 | __ATTR(click_timelimit, 0664, attr_get_click_tlim, |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 1108 | attr_set_click_tlim), |
| Wentao Xu | 82e6305 | 2012-11-15 16:30:15 -0500 | [diff] [blame] | 1109 | __ATTR(click_timelatency, 0664, attr_get_click_tlat, |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 1110 | attr_set_click_tlat), |
| Wentao Xu | 82e6305 | 2012-11-15 16:30:15 -0500 | [diff] [blame] | 1111 | __ATTR(click_timewindow, 0664, attr_get_click_tw, attr_set_click_tw), |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 1112 | |
| 1113 | #ifdef DEBUG |
| Wentao Xu | 82e6305 | 2012-11-15 16:30:15 -0500 | [diff] [blame] | 1114 | __ATTR(reg_value, 0664, attr_reg_get, attr_reg_set), |
| 1115 | __ATTR(reg_addr, 0220, NULL, attr_addr_set), |
| Samuel Huo | 84e7b7b | 2012-07-10 12:01:13 -0700 | [diff] [blame] | 1116 | #endif |
| 1117 | }; |
| 1118 | |
| 1119 | static int create_sysfs_interfaces(struct device *dev) |
| 1120 | { |
| 1121 | int i; |
| 1122 | int err; |
| 1123 | for (i = 0; i < ARRAY_SIZE(attributes); i++) { |
| 1124 | err = device_create_file(dev, attributes + i); |
| 1125 | if (err) |
| 1126 | goto error; |
| 1127 | } |
| 1128 | return 0; |
| 1129 | |
| 1130 | error: |
| 1131 | for ( ; i >= 0; i--) |
| 1132 | device_remove_file(dev, attributes + i); |
| 1133 | dev_err(dev, "%s:Unable to create interface\n", __func__); |
| 1134 | return err; |
| 1135 | } |
| 1136 | |
| 1137 | static int remove_sysfs_interfaces(struct device *dev) |
| 1138 | { |
| 1139 | int i; |
| 1140 | for (i = 0; i < ARRAY_SIZE(attributes); i++) |
| 1141 | device_remove_file(dev, attributes + i); |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | static void lis3dh_acc_input_work_func(struct work_struct *work) |
| 1146 | { |
| 1147 | struct lis3dh_acc_data *acc; |
| 1148 | |
| 1149 | int xyz[3] = { 0 }; |
| 1150 | int err; |
| 1151 | |
| 1152 | acc = container_of((struct delayed_work *)work, |
| 1153 | struct lis3dh_acc_data, input_work); |
| 1154 | |
| 1155 | mutex_lock(&acc->lock); |
| 1156 | err = lis3dh_acc_get_acceleration_data(acc, xyz); |
| 1157 | if (err < 0) |
| 1158 | dev_err(&acc->client->dev, "get_acceleration_data failed\n"); |
| 1159 | else |
| 1160 | lis3dh_acc_report_values(acc, xyz); |
| 1161 | |
| 1162 | schedule_delayed_work(&acc->input_work, msecs_to_jiffies( |
| 1163 | acc->pdata->poll_interval)); |
| 1164 | mutex_unlock(&acc->lock); |
| 1165 | } |
| 1166 | |
| 1167 | int lis3dh_acc_input_open(struct input_dev *input) |
| 1168 | { |
| 1169 | struct lis3dh_acc_data *acc = input_get_drvdata(input); |
| 1170 | |
| 1171 | return lis3dh_acc_enable(acc); |
| 1172 | } |
| 1173 | |
| 1174 | void lis3dh_acc_input_close(struct input_dev *dev) |
| 1175 | { |
| 1176 | struct lis3dh_acc_data *acc = input_get_drvdata(dev); |
| 1177 | |
| 1178 | lis3dh_acc_disable(acc); |
| 1179 | } |
| 1180 | |
| 1181 | static int lis3dh_acc_validate_pdata(struct lis3dh_acc_data *acc) |
| 1182 | { |
| 1183 | acc->pdata->poll_interval = max(acc->pdata->poll_interval, |
| 1184 | acc->pdata->min_interval); |
| 1185 | |
| 1186 | if (acc->pdata->axis_map_x > 2 || |
| 1187 | acc->pdata->axis_map_y > 2 || |
| 1188 | acc->pdata->axis_map_z > 2) { |
| 1189 | dev_err(&acc->client->dev, |
| 1190 | "invalid axis_map value x:%u y:%u z%u\n", |
| 1191 | acc->pdata->axis_map_x, |
| 1192 | acc->pdata->axis_map_y, acc->pdata->axis_map_z); |
| 1193 | return -EINVAL; |
| 1194 | } |
| 1195 | |
| 1196 | /* Only allow 0 and 1 for negation boolean flag */ |
| 1197 | if (acc->pdata->negate_x > 1 || acc->pdata->negate_y > 1 |
| 1198 | || acc->pdata->negate_z > 1) { |
| 1199 | dev_err(&acc->client->dev, |
| 1200 | "invalid negate value x:%u y:%u z:%u\n", |
| 1201 | acc->pdata->negate_x, |
| 1202 | acc->pdata->negate_y, acc->pdata->negate_z); |
| 1203 | return -EINVAL; |
| 1204 | } |
| 1205 | |
| 1206 | /* Enforce minimum polling interval */ |
| 1207 | if (acc->pdata->poll_interval < acc->pdata->min_interval) { |
| 1208 | dev_err(&acc->client->dev, "minimum poll interval violated\n"); |
| 1209 | return -EINVAL; |
| 1210 | } |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static int lis3dh_acc_input_init(struct lis3dh_acc_data *acc) |
| 1216 | { |
| 1217 | int err; |
| 1218 | |
| 1219 | INIT_DELAYED_WORK(&acc->input_work, lis3dh_acc_input_work_func); |
| 1220 | acc->input_dev = input_allocate_device(); |
| 1221 | if (!acc->input_dev) { |
| 1222 | err = -ENOMEM; |
| 1223 | dev_err(&acc->client->dev, "input device allocation failed\n"); |
| 1224 | goto err0; |
| 1225 | } |
| 1226 | |
| 1227 | acc->input_dev->open = lis3dh_acc_input_open; |
| 1228 | acc->input_dev->close = lis3dh_acc_input_close; |
| 1229 | acc->input_dev->name = LIS3DH_ACC_DEV_NAME; |
| 1230 | acc->input_dev->id.bustype = BUS_I2C; |
| 1231 | acc->input_dev->dev.parent = &acc->client->dev; |
| 1232 | |
| 1233 | input_set_drvdata(acc->input_dev, acc); |
| 1234 | |
| 1235 | set_bit(EV_ABS, acc->input_dev->evbit); |
| 1236 | /* next is used for interruptA sources data if the case */ |
| 1237 | set_bit(ABS_MISC, acc->input_dev->absbit); |
| 1238 | /* next is used for interruptB sources data if the case */ |
| 1239 | set_bit(ABS_WHEEL, acc->input_dev->absbit); |
| 1240 | |
| 1241 | input_set_abs_params(acc->input_dev, ABS_X, -G_MAX, G_MAX, FUZZ, FLAT); |
| 1242 | input_set_abs_params(acc->input_dev, ABS_Y, -G_MAX, G_MAX, FUZZ, FLAT); |
| 1243 | input_set_abs_params(acc->input_dev, ABS_Z, -G_MAX, G_MAX, FUZZ, FLAT); |
| 1244 | /* next is used for interruptA sources data if the case */ |
| 1245 | input_set_abs_params(acc->input_dev, ABS_MISC, INT_MIN, INT_MAX, 0, 0); |
| 1246 | /* next is used for interruptB sources data if the case */ |
| 1247 | input_set_abs_params(acc->input_dev, ABS_WHEEL, INT_MIN, INT_MAX, 0, 0); |
| 1248 | |
| 1249 | |
| 1250 | err = input_register_device(acc->input_dev); |
| 1251 | if (err) { |
| 1252 | dev_err(&acc->client->dev, |
| 1253 | "unable to register input device %s\n", |
| 1254 | acc->input_dev->name); |
| 1255 | goto err1; |
| 1256 | } |
| 1257 | |
| 1258 | return 0; |
| 1259 | |
| 1260 | err1: |
| 1261 | input_free_device(acc->input_dev); |
| 1262 | err0: |
| 1263 | return err; |
| 1264 | } |
| 1265 | |
| 1266 | static void lis3dh_acc_input_cleanup(struct lis3dh_acc_data *acc) |
| 1267 | { |
| 1268 | input_unregister_device(acc->input_dev); |
| 1269 | input_free_device(acc->input_dev); |
| 1270 | } |
| 1271 | |
| 1272 | static int lis3dh_acc_probe(struct i2c_client *client, |
| 1273 | const struct i2c_device_id *id) |
| 1274 | { |
| 1275 | |
| 1276 | struct lis3dh_acc_data *acc; |
| 1277 | |
| 1278 | int err = -1; |
| 1279 | |
| 1280 | pr_info("%s: probe start.\n", LIS3DH_ACC_DEV_NAME); |
| 1281 | |
| 1282 | if (client->dev.platform_data == NULL) { |
| 1283 | dev_err(&client->dev, "platform data is NULL. exiting.\n"); |
| 1284 | err = -ENODEV; |
| 1285 | goto exit_check_functionality_failed; |
| 1286 | } |
| 1287 | |
| 1288 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 1289 | dev_err(&client->dev, "client not i2c capable\n"); |
| 1290 | err = -ENODEV; |
| 1291 | goto exit_check_functionality_failed; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | if (!i2c_check_functionality(client->adapter, |
| 1296 | I2C_FUNC_SMBUS_BYTE | |
| 1297 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 1298 | I2C_FUNC_SMBUS_WORD_DATA)) { |
| 1299 | dev_err(&client->dev, "client not smb-i2c capable:2\n"); |
| 1300 | err = -EIO; |
| 1301 | goto exit_check_functionality_failed; |
| 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | if (!i2c_check_functionality(client->adapter, |
| 1306 | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 1307 | dev_err(&client->dev, "client not smb-i2c capable:3\n"); |
| 1308 | err = -EIO; |
| 1309 | goto exit_check_functionality_failed; |
| 1310 | } |
| 1311 | */ |
| 1312 | |
| 1313 | acc = kzalloc(sizeof(struct lis3dh_acc_data), GFP_KERNEL); |
| 1314 | if (acc == NULL) { |
| 1315 | err = -ENOMEM; |
| 1316 | dev_err(&client->dev, |
| 1317 | "failed to allocate memory for module data: " |
| 1318 | "%d\n", err); |
| 1319 | goto exit_check_functionality_failed; |
| 1320 | } |
| 1321 | |
| 1322 | |
| 1323 | mutex_init(&acc->lock); |
| 1324 | mutex_lock(&acc->lock); |
| 1325 | |
| 1326 | acc->client = client; |
| 1327 | i2c_set_clientdata(client, acc); |
| 1328 | |
| 1329 | acc->pdata = kmalloc(sizeof(*acc->pdata), GFP_KERNEL); |
| 1330 | if (acc->pdata == NULL) { |
| 1331 | err = -ENOMEM; |
| 1332 | dev_err(&client->dev, |
| 1333 | "failed to allocate memory for pdata: %d\n", |
| 1334 | err); |
| 1335 | goto err_mutexunlock; |
| 1336 | } |
| 1337 | |
| 1338 | memcpy(acc->pdata, client->dev.platform_data, sizeof(*acc->pdata)); |
| 1339 | |
| 1340 | err = lis3dh_acc_validate_pdata(acc); |
| 1341 | if (err < 0) { |
| 1342 | dev_err(&client->dev, "failed to validate platform data\n"); |
| 1343 | goto exit_kfree_pdata; |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | if (acc->pdata->init) { |
| 1348 | err = acc->pdata->init(); |
| 1349 | if (err < 0) { |
| 1350 | dev_err(&client->dev, "init failed: %d\n", err); |
| 1351 | goto err_pdata_init; |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | if (acc->pdata->gpio_int1 >= 0) { |
| 1356 | acc->irq1 = gpio_to_irq(acc->pdata->gpio_int1); |
| 1357 | printk(KERN_INFO "%s: %s has set irq1 to irq: %d\n", |
| 1358 | LIS3DH_ACC_DEV_NAME, __func__, acc->irq1); |
| 1359 | printk(KERN_INFO "%s: %s has mapped irq1 on gpio: %d\n", |
| 1360 | LIS3DH_ACC_DEV_NAME, __func__, |
| 1361 | acc->pdata->gpio_int1); |
| 1362 | } |
| 1363 | |
| 1364 | if (acc->pdata->gpio_int2 >= 0) { |
| 1365 | acc->irq2 = gpio_to_irq(acc->pdata->gpio_int2); |
| 1366 | printk(KERN_INFO "%s: %s has set irq2 to irq: %d\n", |
| 1367 | LIS3DH_ACC_DEV_NAME, __func__, acc->irq2); |
| 1368 | printk(KERN_INFO "%s: %s has mapped irq2 on gpio: %d\n", |
| 1369 | LIS3DH_ACC_DEV_NAME, __func__, |
| 1370 | acc->pdata->gpio_int2); |
| 1371 | } |
| 1372 | |
| 1373 | memset(acc->resume_state, 0, ARRAY_SIZE(acc->resume_state)); |
| 1374 | |
| 1375 | acc->resume_state[RES_CTRL_REG1] = LIS3DH_ACC_ENABLE_ALL_AXES; |
| 1376 | acc->resume_state[RES_CTRL_REG2] = 0x00; |
| 1377 | acc->resume_state[RES_CTRL_REG3] = 0x00; |
| 1378 | acc->resume_state[RES_CTRL_REG4] = 0x00; |
| 1379 | acc->resume_state[RES_CTRL_REG5] = 0x00; |
| 1380 | acc->resume_state[RES_CTRL_REG6] = 0x00; |
| 1381 | |
| 1382 | acc->resume_state[RES_TEMP_CFG_REG] = 0x00; |
| 1383 | acc->resume_state[RES_FIFO_CTRL_REG] = 0x00; |
| 1384 | acc->resume_state[RES_INT_CFG1] = 0x00; |
| 1385 | acc->resume_state[RES_INT_THS1] = 0x00; |
| 1386 | acc->resume_state[RES_INT_DUR1] = 0x00; |
| 1387 | |
| 1388 | acc->resume_state[RES_TT_CFG] = 0x00; |
| 1389 | acc->resume_state[RES_TT_THS] = 0x00; |
| 1390 | acc->resume_state[RES_TT_LIM] = 0x00; |
| 1391 | acc->resume_state[RES_TT_TLAT] = 0x00; |
| 1392 | acc->resume_state[RES_TT_TW] = 0x00; |
| 1393 | |
| 1394 | err = lis3dh_acc_device_power_on(acc); |
| 1395 | if (err < 0) { |
| 1396 | dev_err(&client->dev, "power on failed: %d\n", err); |
| 1397 | goto err_pdata_init; |
| 1398 | } |
| 1399 | |
| 1400 | atomic_set(&acc->enabled, 1); |
| 1401 | |
| 1402 | err = lis3dh_acc_update_g_range(acc, acc->pdata->g_range); |
| 1403 | if (err < 0) { |
| 1404 | dev_err(&client->dev, "update_g_range failed\n"); |
| 1405 | goto err_power_off; |
| 1406 | } |
| 1407 | |
| 1408 | err = lis3dh_acc_update_odr(acc, acc->pdata->poll_interval); |
| 1409 | if (err < 0) { |
| 1410 | dev_err(&client->dev, "update_odr failed\n"); |
| 1411 | goto err_power_off; |
| 1412 | } |
| 1413 | |
| 1414 | err = lis3dh_acc_input_init(acc); |
| 1415 | if (err < 0) { |
| 1416 | dev_err(&client->dev, "input init failed\n"); |
| 1417 | goto err_power_off; |
| 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | err = create_sysfs_interfaces(&client->dev); |
| 1422 | if (err < 0) { |
| 1423 | dev_err(&client->dev, |
| 1424 | "device LIS3DH_ACC_DEV_NAME sysfs register failed\n"); |
| 1425 | goto err_input_cleanup; |
| 1426 | } |
| 1427 | |
| 1428 | lis3dh_acc_device_power_off(acc); |
| 1429 | |
| 1430 | /* As default, do not report information */ |
| 1431 | atomic_set(&acc->enabled, 0); |
| 1432 | |
| 1433 | if (acc->pdata->gpio_int1 >= 0) { |
| 1434 | INIT_WORK(&acc->irq1_work, lis3dh_acc_irq1_work_func); |
| 1435 | acc->irq1_work_queue = |
| 1436 | create_singlethread_workqueue("lis3dh_acc_wq1"); |
| 1437 | if (!acc->irq1_work_queue) { |
| 1438 | err = -ENOMEM; |
| 1439 | dev_err(&client->dev, |
| 1440 | "cannot create work queue1: %d\n", err); |
| 1441 | goto err_remove_sysfs_int; |
| 1442 | } |
| 1443 | err = request_irq(acc->irq1, lis3dh_acc_isr1, |
| 1444 | IRQF_TRIGGER_RISING, "lis3dh_acc_irq1", acc); |
| 1445 | if (err < 0) { |
| 1446 | dev_err(&client->dev, "request irq1 failed: %d\n", err); |
| 1447 | goto err_destoyworkqueue1; |
| 1448 | } |
| 1449 | disable_irq_nosync(acc->irq1); |
| 1450 | } |
| 1451 | |
| 1452 | if (acc->pdata->gpio_int2 >= 0) { |
| 1453 | INIT_WORK(&acc->irq2_work, lis3dh_acc_irq2_work_func); |
| 1454 | acc->irq2_work_queue = |
| 1455 | create_singlethread_workqueue("lis3dh_acc_wq2"); |
| 1456 | if (!acc->irq2_work_queue) { |
| 1457 | err = -ENOMEM; |
| 1458 | dev_err(&client->dev, |
| 1459 | "cannot create work queue2: %d\n", err); |
| 1460 | goto err_free_irq1; |
| 1461 | } |
| 1462 | err = request_irq(acc->irq2, lis3dh_acc_isr2, |
| 1463 | IRQF_TRIGGER_RISING, "lis3dh_acc_irq2", acc); |
| 1464 | if (err < 0) { |
| 1465 | dev_err(&client->dev, "request irq2 failed: %d\n", err); |
| 1466 | goto err_destoyworkqueue2; |
| 1467 | } |
| 1468 | disable_irq_nosync(acc->irq2); |
| 1469 | } |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | mutex_unlock(&acc->lock); |
| 1474 | |
| 1475 | dev_info(&client->dev, "%s: probed\n", LIS3DH_ACC_DEV_NAME); |
| 1476 | |
| 1477 | return 0; |
| 1478 | |
| 1479 | err_destoyworkqueue2: |
| 1480 | if (acc->pdata->gpio_int2 >= 0) |
| 1481 | destroy_workqueue(acc->irq2_work_queue); |
| 1482 | err_free_irq1: |
| 1483 | free_irq(acc->irq1, acc); |
| 1484 | err_destoyworkqueue1: |
| 1485 | if (acc->pdata->gpio_int1 >= 0) |
| 1486 | destroy_workqueue(acc->irq1_work_queue); |
| 1487 | err_remove_sysfs_int: |
| 1488 | remove_sysfs_interfaces(&client->dev); |
| 1489 | err_input_cleanup: |
| 1490 | lis3dh_acc_input_cleanup(acc); |
| 1491 | err_power_off: |
| 1492 | lis3dh_acc_device_power_off(acc); |
| 1493 | err_pdata_init: |
| 1494 | if (acc->pdata->exit) |
| 1495 | acc->pdata->exit(); |
| 1496 | exit_kfree_pdata: |
| 1497 | kfree(acc->pdata); |
| 1498 | err_mutexunlock: |
| 1499 | mutex_unlock(&acc->lock); |
| 1500 | kfree(acc); |
| 1501 | exit_check_functionality_failed: |
| 1502 | printk(KERN_ERR "%s: Driver Init failed\n", LIS3DH_ACC_DEV_NAME); |
| 1503 | return err; |
| 1504 | } |
| 1505 | |
| 1506 | static int __devexit lis3dh_acc_remove(struct i2c_client *client) |
| 1507 | { |
| 1508 | struct lis3dh_acc_data *acc = i2c_get_clientdata(client); |
| 1509 | |
| 1510 | if (acc->pdata->gpio_int1 >= 0) { |
| 1511 | free_irq(acc->irq1, acc); |
| 1512 | gpio_free(acc->pdata->gpio_int1); |
| 1513 | destroy_workqueue(acc->irq1_work_queue); |
| 1514 | } |
| 1515 | |
| 1516 | if (acc->pdata->gpio_int2 >= 0) { |
| 1517 | free_irq(acc->irq2, acc); |
| 1518 | gpio_free(acc->pdata->gpio_int2); |
| 1519 | destroy_workqueue(acc->irq2_work_queue); |
| 1520 | } |
| 1521 | |
| 1522 | lis3dh_acc_input_cleanup(acc); |
| 1523 | lis3dh_acc_device_power_off(acc); |
| 1524 | remove_sysfs_interfaces(&client->dev); |
| 1525 | |
| 1526 | if (acc->pdata->exit) |
| 1527 | acc->pdata->exit(); |
| 1528 | kfree(acc->pdata); |
| 1529 | kfree(acc); |
| 1530 | |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | #ifdef CONFIG_PM |
| 1535 | static int lis3dh_acc_resume(struct i2c_client *client) |
| 1536 | { |
| 1537 | struct lis3dh_acc_data *acc = i2c_get_clientdata(client); |
| 1538 | |
| 1539 | if (acc->on_before_suspend) |
| 1540 | return lis3dh_acc_enable(acc); |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | static int lis3dh_acc_suspend(struct i2c_client *client, pm_message_t mesg) |
| 1545 | { |
| 1546 | struct lis3dh_acc_data *acc = i2c_get_clientdata(client); |
| 1547 | |
| 1548 | acc->on_before_suspend = atomic_read(&acc->enabled); |
| 1549 | return lis3dh_acc_disable(acc); |
| 1550 | } |
| 1551 | #else |
| 1552 | #define lis3dh_acc_suspend NULL |
| 1553 | #define lis3dh_acc_resume NULL |
| 1554 | #endif /* CONFIG_PM */ |
| 1555 | |
| 1556 | static const struct i2c_device_id lis3dh_acc_id[] |
| 1557 | = { { LIS3DH_ACC_DEV_NAME, 0 }, { }, }; |
| 1558 | |
| 1559 | MODULE_DEVICE_TABLE(i2c, lis3dh_acc_id); |
| 1560 | |
| 1561 | static struct i2c_driver lis3dh_acc_driver = { |
| 1562 | .driver = { |
| 1563 | .owner = THIS_MODULE, |
| 1564 | .name = LIS3DH_ACC_DEV_NAME, |
| 1565 | }, |
| 1566 | .probe = lis3dh_acc_probe, |
| 1567 | .remove = __devexit_p(lis3dh_acc_remove), |
| 1568 | .suspend = lis3dh_acc_suspend, |
| 1569 | .resume = lis3dh_acc_resume, |
| 1570 | .id_table = lis3dh_acc_id, |
| 1571 | }; |
| 1572 | |
| 1573 | static int __init lis3dh_acc_init(void) |
| 1574 | { |
| 1575 | printk(KERN_INFO "%s accelerometer driver: init\n", |
| 1576 | LIS3DH_ACC_DEV_NAME); |
| 1577 | return i2c_add_driver(&lis3dh_acc_driver); |
| 1578 | } |
| 1579 | |
| 1580 | static void __exit lis3dh_acc_exit(void) |
| 1581 | { |
| 1582 | #ifdef DEBUG |
| 1583 | printk(KERN_INFO "%s accelerometer driver exit\n", |
| 1584 | LIS3DH_ACC_DEV_NAME); |
| 1585 | #endif /* DEBUG */ |
| 1586 | i2c_del_driver(&lis3dh_acc_driver); |
| 1587 | return; |
| 1588 | } |
| 1589 | |
| 1590 | module_init(lis3dh_acc_init); |
| 1591 | module_exit(lis3dh_acc_exit); |
| 1592 | |
| 1593 | MODULE_DESCRIPTION("lis3dh digital accelerometer sysfs driver"); |
| 1594 | MODULE_AUTHOR("Matteo Dameno, Carmine Iascone, Samuel Huo, STMicroelectronics"); |
| 1595 | MODULE_LICENSE("GPL"); |
| 1596 | |