1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * lis3lv02d.c - ST LIS3LV02DL accelerometer driver 4 * 5 * Copyright (C) 2007-2008 Yan Burman 6 * Copyright (C) 2008 Eric Piel 7 * Copyright (C) 2008-2009 Pavel Machek 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/kernel.h> 13 #include <linux/sched/signal.h> 14 #include <linux/dmi.h> 15 #include <linux/module.h> 16 #include <linux/types.h> 17 #include <linux/interrupt.h> 18 #include <linux/input.h> 19 #include <linux/delay.h> 20 #include <linux/wait.h> 21 #include <linux/poll.h> 22 #include <linux/slab.h> 23 #include <linux/freezer.h> 24 #include <linux/uaccess.h> 25 #include <linux/miscdevice.h> 26 #include <linux/pm_runtime.h> 27 #include <linux/atomic.h> 28 #include <linux/of.h> 29 #include "lis3lv02d.h" 30 31 #define DRIVER_NAME "lis3lv02d" 32 33 /* joystick device poll interval in milliseconds */ 34 #define MDPS_POLL_INTERVAL 50 35 #define MDPS_POLL_MIN 0 36 #define MDPS_POLL_MAX 2000 37 38 #define LIS3_SYSFS_POWERDOWN_DELAY 5000 /* In milliseconds */ 39 40 #define SELFTEST_OK 0 41 #define SELFTEST_FAIL -1 42 #define SELFTEST_IRQ -2 43 44 #define IRQ_LINE0 0 45 #define IRQ_LINE1 1 46 47 /* 48 * The sensor can also generate interrupts (DRDY) but it's pretty pointless 49 * because they are generated even if the data do not change. So it's better 50 * to keep the interrupt for the free-fall event. The values are updated at 51 * 40Hz (at the lowest frequency), but as it can be pretty time consuming on 52 * some low processor, we poll the sensor only at 20Hz... enough for the 53 * joystick. 54 */ 55 56 #define LIS3_PWRON_DELAY_WAI_12B (5000) 57 #define LIS3_PWRON_DELAY_WAI_8B (3000) 58 59 /* 60 * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG 61 * LIS302D spec says: 18 mG / digit 62 * LIS3_ACCURACY is used to increase accuracy of the intermediate 63 * calculation results. 64 */ 65 #define LIS3_ACCURACY 1024 66 /* Sensitivity values for -2G +2G scale */ 67 #define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024) 68 #define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY) 69 70 /* 71 * LIS331DLH spec says 1LSBs corresponds 4G/4096 -> 1LSB is 1000/1024 mG. 72 * Below macros defines sensitivity values for +/-2G. Dataout bits for 73 * +/-2G range is 12 bits so 4 bits adjustment must be done to get 12bit 74 * data from 16bit value. Currently this driver supports only 2G range. 75 */ 76 #define LIS3DLH_SENSITIVITY_2G ((LIS3_ACCURACY * 1000) / 1024) 77 #define SHIFT_ADJ_2G 4 78 79 #define LIS3_DEFAULT_FUZZ_12B 3 80 #define LIS3_DEFAULT_FLAT_12B 3 81 #define LIS3_DEFAULT_FUZZ_8B 1 82 #define LIS3_DEFAULT_FLAT_8B 1 83 84 struct lis3lv02d lis3_dev = { 85 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait), 86 }; 87 EXPORT_SYMBOL_GPL(lis3_dev); 88 89 /* just like param_set_int() but does sanity-check so that it won't point 90 * over the axis array size 91 */ 92 static int param_set_axis(const char *val, const struct kernel_param *kp) 93 { 94 int ret = param_set_int(val, kp); 95 if (!ret) { 96 int val = *(int *)kp->arg; 97 if (val < 0) 98 val = -val; 99 if (!val || val > 3) 100 return -EINVAL; 101 } 102 return ret; 103 } 104 105 static const struct kernel_param_ops param_ops_axis = { 106 .set = param_set_axis, 107 .get = param_get_int, 108 }; 109 110 #define param_check_axis(name, p) param_check_int(name, p) 111 112 module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644); 113 MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions"); 114 115 static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg) 116 { 117 s8 lo; 118 if (lis3->read(lis3, reg, &lo) < 0) 119 return 0; 120 121 return lo; 122 } 123 124 static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg) 125 { 126 u8 lo, hi; 127 128 lis3->read(lis3, reg - 1, &lo); 129 lis3->read(lis3, reg, &hi); 130 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */ 131 return (s16)((hi << 8) | lo); 132 } 133 134 /* 12bits for 2G range, 13 bits for 4G range and 14 bits for 8G range */ 135 static s16 lis331dlh_read_data(struct lis3lv02d *lis3, int reg) 136 { 137 u8 lo, hi; 138 int v; 139 140 lis3->read(lis3, reg - 1, &lo); 141 lis3->read(lis3, reg, &hi); 142 v = (int) ((hi << 8) | lo); 143 144 return (s16) v >> lis3->shift_adj; 145 } 146 147 /** 148 * lis3lv02d_get_axis - For the given axis, give the value converted 149 * @axis: 1,2,3 - can also be negative 150 * @hw_values: raw values returned by the hardware 151 * 152 * Returns the converted value. 153 */ 154 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3]) 155 { 156 if (axis > 0) 157 return hw_values[axis - 1]; 158 else 159 return -hw_values[-axis - 1]; 160 } 161 162 /** 163 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer 164 * @lis3: pointer to the device struct 165 * @x: where to store the X axis value 166 * @y: where to store the Y axis value 167 * @z: where to store the Z axis value 168 * 169 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ 170 */ 171 static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z) 172 { 173 int position[3]; 174 int i; 175 176 if (lis3->blkread) { 177 if (lis3->whoami == WAI_12B) { 178 u16 data[3]; 179 lis3->blkread(lis3, OUTX_L, 6, (u8 *)data); 180 for (i = 0; i < 3; i++) 181 position[i] = (s16)le16_to_cpu(data[i]); 182 } else { 183 u8 data[5]; 184 /* Data: x, dummy, y, dummy, z */ 185 lis3->blkread(lis3, OUTX, 5, data); 186 for (i = 0; i < 3; i++) 187 position[i] = (s8)data[i * 2]; 188 } 189 } else { 190 position[0] = lis3->read_data(lis3, OUTX); 191 position[1] = lis3->read_data(lis3, OUTY); 192 position[2] = lis3->read_data(lis3, OUTZ); 193 } 194 195 for (i = 0; i < 3; i++) 196 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY; 197 198 *x = lis3lv02d_get_axis(lis3->ac.x, position); 199 *y = lis3lv02d_get_axis(lis3->ac.y, position); 200 *z = lis3lv02d_get_axis(lis3->ac.z, position); 201 } 202 203 /* conversion btw sampling rate and the register values */ 204 static int lis3_12_rates[4] = {40, 160, 640, 2560}; 205 static int lis3_8_rates[2] = {100, 400}; 206 static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000}; 207 static int lis3_3dlh_rates[4] = {50, 100, 400, 1000}; 208 209 /* ODR is Output Data Rate */ 210 static int lis3lv02d_get_odr_index(struct lis3lv02d *lis3) 211 { 212 u8 ctrl; 213 int shift; 214 215 lis3->read(lis3, CTRL_REG1, &ctrl); 216 ctrl &= lis3->odr_mask; 217 shift = ffs(lis3->odr_mask) - 1; 218 return (ctrl >> shift); 219 } 220 221 static int lis3lv02d_get_pwron_wait(struct lis3lv02d *lis3) 222 { 223 int odr_idx = lis3lv02d_get_odr_index(lis3); 224 int div = lis3->odrs[odr_idx]; 225 226 if (div == 0) { 227 if (odr_idx == 0) { 228 /* Power-down mode, not sampling no need to sleep */ 229 return 0; 230 } 231 232 dev_err(&lis3->fdev->dev, "Error unknown odrs-index: %d\n", odr_idx); 233 return -ENXIO; 234 } 235 236 /* LIS3 power on delay is quite long */ 237 msleep(lis3->pwron_delay / div); 238 return 0; 239 } 240 241 static int lis3lv02d_set_odr(struct lis3lv02d *lis3, int rate) 242 { 243 u8 ctrl; 244 int i, len, shift; 245 246 if (!rate) 247 return -EINVAL; 248 249 lis3->read(lis3, CTRL_REG1, &ctrl); 250 ctrl &= ~lis3->odr_mask; 251 len = 1 << hweight_long(lis3->odr_mask); /* # of possible values */ 252 shift = ffs(lis3->odr_mask) - 1; 253 254 for (i = 0; i < len; i++) 255 if (lis3->odrs[i] == rate) { 256 lis3->write(lis3, CTRL_REG1, 257 ctrl | (i << shift)); 258 return 0; 259 } 260 return -EINVAL; 261 } 262 263 static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3]) 264 { 265 u8 ctlreg, reg; 266 s16 x, y, z; 267 u8 selftest; 268 int ret; 269 u8 ctrl_reg_data; 270 unsigned char irq_cfg; 271 272 mutex_lock(&lis3->mutex); 273 274 irq_cfg = lis3->irq_cfg; 275 if (lis3->whoami == WAI_8B) { 276 lis3->data_ready_count[IRQ_LINE0] = 0; 277 lis3->data_ready_count[IRQ_LINE1] = 0; 278 279 /* Change interrupt cfg to data ready for selftest */ 280 atomic_inc(&lis3->wake_thread); 281 lis3->irq_cfg = LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY; 282 lis3->read(lis3, CTRL_REG3, &ctrl_reg_data); 283 lis3->write(lis3, CTRL_REG3, (ctrl_reg_data & 284 ~(LIS3_IRQ1_MASK | LIS3_IRQ2_MASK)) | 285 (LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY)); 286 } 287 288 if ((lis3->whoami == WAI_3DC) || (lis3->whoami == WAI_3DLH)) { 289 ctlreg = CTRL_REG4; 290 selftest = CTRL4_ST0; 291 } else { 292 ctlreg = CTRL_REG1; 293 if (lis3->whoami == WAI_12B) 294 selftest = CTRL1_ST; 295 else 296 selftest = CTRL1_STP; 297 } 298 299 lis3->read(lis3, ctlreg, ®); 300 lis3->write(lis3, ctlreg, (reg | selftest)); 301 ret = lis3lv02d_get_pwron_wait(lis3); 302 if (ret) 303 goto fail; 304 305 /* Read directly to avoid axis remap */ 306 x = lis3->read_data(lis3, OUTX); 307 y = lis3->read_data(lis3, OUTY); 308 z = lis3->read_data(lis3, OUTZ); 309 310 /* back to normal settings */ 311 lis3->write(lis3, ctlreg, reg); 312 ret = lis3lv02d_get_pwron_wait(lis3); 313 if (ret) 314 goto fail; 315 316 results[0] = x - lis3->read_data(lis3, OUTX); 317 results[1] = y - lis3->read_data(lis3, OUTY); 318 results[2] = z - lis3->read_data(lis3, OUTZ); 319 320 ret = 0; 321 322 if (lis3->whoami == WAI_8B) { 323 /* Restore original interrupt configuration */ 324 atomic_dec(&lis3->wake_thread); 325 lis3->write(lis3, CTRL_REG3, ctrl_reg_data); 326 lis3->irq_cfg = irq_cfg; 327 328 if ((irq_cfg & LIS3_IRQ1_MASK) && 329 lis3->data_ready_count[IRQ_LINE0] < 2) { 330 ret = SELFTEST_IRQ; 331 goto fail; 332 } 333 334 if ((irq_cfg & LIS3_IRQ2_MASK) && 335 lis3->data_ready_count[IRQ_LINE1] < 2) { 336 ret = SELFTEST_IRQ; 337 goto fail; 338 } 339 } 340 341 if (lis3->pdata) { 342 int i; 343 for (i = 0; i < 3; i++) { 344 /* Check against selftest acceptance limits */ 345 if ((results[i] < lis3->pdata->st_min_limits[i]) || 346 (results[i] > lis3->pdata->st_max_limits[i])) { 347 ret = SELFTEST_FAIL; 348 goto fail; 349 } 350 } 351 } 352 353 /* test passed */ 354 fail: 355 mutex_unlock(&lis3->mutex); 356 return ret; 357 } 358 359 /* 360 * Order of registers in the list affects to order of the restore process. 361 * Perhaps it is a good idea to set interrupt enable register as a last one 362 * after all other configurations 363 */ 364 static u8 lis3_wai8_regs[] = { FF_WU_CFG_1, FF_WU_THS_1, FF_WU_DURATION_1, 365 FF_WU_CFG_2, FF_WU_THS_2, FF_WU_DURATION_2, 366 CLICK_CFG, CLICK_SRC, CLICK_THSY_X, CLICK_THSZ, 367 CLICK_TIMELIMIT, CLICK_LATENCY, CLICK_WINDOW, 368 CTRL_REG1, CTRL_REG2, CTRL_REG3}; 369 370 static u8 lis3_wai12_regs[] = {FF_WU_CFG, FF_WU_THS_L, FF_WU_THS_H, 371 FF_WU_DURATION, DD_CFG, DD_THSI_L, DD_THSI_H, 372 DD_THSE_L, DD_THSE_H, 373 CTRL_REG1, CTRL_REG3, CTRL_REG2}; 374 375 static inline void lis3_context_save(struct lis3lv02d *lis3) 376 { 377 int i; 378 for (i = 0; i < lis3->regs_size; i++) 379 lis3->read(lis3, lis3->regs[i], &lis3->reg_cache[i]); 380 lis3->regs_stored = true; 381 } 382 383 static inline void lis3_context_restore(struct lis3lv02d *lis3) 384 { 385 int i; 386 if (lis3->regs_stored) 387 for (i = 0; i < lis3->regs_size; i++) 388 lis3->write(lis3, lis3->regs[i], lis3->reg_cache[i]); 389 } 390 391 void lis3lv02d_poweroff(struct lis3lv02d *lis3) 392 { 393 if (lis3->reg_ctrl) 394 lis3_context_save(lis3); 395 /* disable X,Y,Z axis and power down */ 396 lis3->write(lis3, CTRL_REG1, 0x00); 397 if (lis3->reg_ctrl) 398 lis3->reg_ctrl(lis3, LIS3_REG_OFF); 399 } 400 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff); 401 402 int lis3lv02d_poweron(struct lis3lv02d *lis3) 403 { 404 int err; 405 u8 reg; 406 407 lis3->init(lis3); 408 409 /* 410 * Common configuration 411 * BDU: (12 bits sensors only) LSB and MSB values are not updated until 412 * both have been read. So the value read will always be correct. 413 * Set BOOT bit to refresh factory tuning values. 414 */ 415 if (lis3->pdata) { 416 lis3->read(lis3, CTRL_REG2, ®); 417 if (lis3->whoami == WAI_12B) 418 reg |= CTRL2_BDU | CTRL2_BOOT; 419 else if (lis3->whoami == WAI_3DLH) 420 reg |= CTRL2_BOOT_3DLH; 421 else 422 reg |= CTRL2_BOOT_8B; 423 lis3->write(lis3, CTRL_REG2, reg); 424 425 if (lis3->whoami == WAI_3DLH) { 426 lis3->read(lis3, CTRL_REG4, ®); 427 reg |= CTRL4_BDU; 428 lis3->write(lis3, CTRL_REG4, reg); 429 } 430 } 431 432 err = lis3lv02d_get_pwron_wait(lis3); 433 if (err) 434 return err; 435 436 if (lis3->reg_ctrl) 437 lis3_context_restore(lis3); 438 439 return 0; 440 } 441 EXPORT_SYMBOL_GPL(lis3lv02d_poweron); 442 443 444 static void lis3lv02d_joystick_poll(struct input_dev *input) 445 { 446 struct lis3lv02d *lis3 = input_get_drvdata(input); 447 int x, y, z; 448 449 mutex_lock(&lis3->mutex); 450 lis3lv02d_get_xyz(lis3, &x, &y, &z); 451 input_report_abs(input, ABS_X, x); 452 input_report_abs(input, ABS_Y, y); 453 input_report_abs(input, ABS_Z, z); 454 input_sync(input); 455 mutex_unlock(&lis3->mutex); 456 } 457 458 static int lis3lv02d_joystick_open(struct input_dev *input) 459 { 460 struct lis3lv02d *lis3 = input_get_drvdata(input); 461 462 if (lis3->pm_dev) 463 pm_runtime_get_sync(lis3->pm_dev); 464 465 if (lis3->pdata && lis3->whoami == WAI_8B && lis3->idev) 466 atomic_set(&lis3->wake_thread, 1); 467 /* 468 * Update coordinates for the case where poll interval is 0 and 469 * the chip in running purely under interrupt control 470 */ 471 lis3lv02d_joystick_poll(input); 472 473 return 0; 474 } 475 476 static void lis3lv02d_joystick_close(struct input_dev *input) 477 { 478 struct lis3lv02d *lis3 = input_get_drvdata(input); 479 480 atomic_set(&lis3->wake_thread, 0); 481 if (lis3->pm_dev) 482 pm_runtime_put(lis3->pm_dev); 483 } 484 485 static irqreturn_t lis302dl_interrupt(int irq, void *data) 486 { 487 struct lis3lv02d *lis3 = data; 488 489 if (!test_bit(0, &lis3->misc_opened)) 490 goto out; 491 492 /* 493 * Be careful: on some HP laptops the bios force DD when on battery and 494 * the lid is closed. This leads to interrupts as soon as a little move 495 * is done. 496 */ 497 atomic_inc(&lis3->count); 498 499 wake_up_interruptible(&lis3->misc_wait); 500 kill_fasync(&lis3->async_queue, SIGIO, POLL_IN); 501 out: 502 if (atomic_read(&lis3->wake_thread)) 503 return IRQ_WAKE_THREAD; 504 return IRQ_HANDLED; 505 } 506 507 static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3) 508 { 509 struct input_dev *dev = lis3->idev; 510 u8 click_src; 511 512 mutex_lock(&lis3->mutex); 513 lis3->read(lis3, CLICK_SRC, &click_src); 514 515 if (click_src & CLICK_SINGLE_X) { 516 input_report_key(dev, lis3->mapped_btns[0], 1); 517 input_report_key(dev, lis3->mapped_btns[0], 0); 518 } 519 520 if (click_src & CLICK_SINGLE_Y) { 521 input_report_key(dev, lis3->mapped_btns[1], 1); 522 input_report_key(dev, lis3->mapped_btns[1], 0); 523 } 524 525 if (click_src & CLICK_SINGLE_Z) { 526 input_report_key(dev, lis3->mapped_btns[2], 1); 527 input_report_key(dev, lis3->mapped_btns[2], 0); 528 } 529 input_sync(dev); 530 mutex_unlock(&lis3->mutex); 531 } 532 533 static inline void lis302dl_data_ready(struct lis3lv02d *lis3, int index) 534 { 535 int dummy; 536 537 /* Dummy read to ack interrupt */ 538 lis3lv02d_get_xyz(lis3, &dummy, &dummy, &dummy); 539 lis3->data_ready_count[index]++; 540 } 541 542 static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data) 543 { 544 struct lis3lv02d *lis3 = data; 545 u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ1_MASK; 546 547 if (irq_cfg == LIS3_IRQ1_CLICK) 548 lis302dl_interrupt_handle_click(lis3); 549 else if (unlikely(irq_cfg == LIS3_IRQ1_DATA_READY)) 550 lis302dl_data_ready(lis3, IRQ_LINE0); 551 else 552 lis3lv02d_joystick_poll(lis3->idev); 553 554 return IRQ_HANDLED; 555 } 556 557 static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data) 558 { 559 struct lis3lv02d *lis3 = data; 560 u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ2_MASK; 561 562 if (irq_cfg == LIS3_IRQ2_CLICK) 563 lis302dl_interrupt_handle_click(lis3); 564 else if (unlikely(irq_cfg == LIS3_IRQ2_DATA_READY)) 565 lis302dl_data_ready(lis3, IRQ_LINE1); 566 else 567 lis3lv02d_joystick_poll(lis3->idev); 568 569 return IRQ_HANDLED; 570 } 571 572 static int lis3lv02d_misc_open(struct inode *inode, struct file *file) 573 { 574 struct lis3lv02d *lis3 = container_of(file->private_data, 575 struct lis3lv02d, miscdev); 576 577 if (test_and_set_bit(0, &lis3->misc_opened)) 578 return -EBUSY; /* already open */ 579 580 if (lis3->pm_dev) 581 pm_runtime_get_sync(lis3->pm_dev); 582 583 atomic_set(&lis3->count, 0); 584 return 0; 585 } 586 587 static int lis3lv02d_misc_release(struct inode *inode, struct file *file) 588 { 589 struct lis3lv02d *lis3 = container_of(file->private_data, 590 struct lis3lv02d, miscdev); 591 592 clear_bit(0, &lis3->misc_opened); /* release the device */ 593 if (lis3->pm_dev) 594 pm_runtime_put(lis3->pm_dev); 595 return 0; 596 } 597 598 static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf, 599 size_t count, loff_t *pos) 600 { 601 struct lis3lv02d *lis3 = container_of(file->private_data, 602 struct lis3lv02d, miscdev); 603 604 DECLARE_WAITQUEUE(wait, current); 605 u32 data; 606 unsigned char byte_data; 607 ssize_t retval = 1; 608 609 if (count < 1) 610 return -EINVAL; 611 612 add_wait_queue(&lis3->misc_wait, &wait); 613 while (true) { 614 set_current_state(TASK_INTERRUPTIBLE); 615 data = atomic_xchg(&lis3->count, 0); 616 if (data) 617 break; 618 619 if (file->f_flags & O_NONBLOCK) { 620 retval = -EAGAIN; 621 goto out; 622 } 623 624 if (signal_pending(current)) { 625 retval = -ERESTARTSYS; 626 goto out; 627 } 628 629 schedule(); 630 } 631 632 if (data < 255) 633 byte_data = data; 634 else 635 byte_data = 255; 636 637 /* make sure we are not going into copy_to_user() with 638 * TASK_INTERRUPTIBLE state */ 639 set_current_state(TASK_RUNNING); 640 if (copy_to_user(buf, &byte_data, sizeof(byte_data))) 641 retval = -EFAULT; 642 643 out: 644 __set_current_state(TASK_RUNNING); 645 remove_wait_queue(&lis3->misc_wait, &wait); 646 647 return retval; 648 } 649 650 static __poll_t lis3lv02d_misc_poll(struct file *file, poll_table *wait) 651 { 652 struct lis3lv02d *lis3 = container_of(file->private_data, 653 struct lis3lv02d, miscdev); 654 655 poll_wait(file, &lis3->misc_wait, wait); 656 if (atomic_read(&lis3->count)) 657 return EPOLLIN | EPOLLRDNORM; 658 return 0; 659 } 660 661 static int lis3lv02d_misc_fasync(int fd, struct file *file, int on) 662 { 663 struct lis3lv02d *lis3 = container_of(file->private_data, 664 struct lis3lv02d, miscdev); 665 666 return fasync_helper(fd, file, on, &lis3->async_queue); 667 } 668 669 static const struct file_operations lis3lv02d_misc_fops = { 670 .owner = THIS_MODULE, 671 .read = lis3lv02d_misc_read, 672 .open = lis3lv02d_misc_open, 673 .release = lis3lv02d_misc_release, 674 .poll = lis3lv02d_misc_poll, 675 .fasync = lis3lv02d_misc_fasync, 676 }; 677 678 int lis3lv02d_joystick_enable(struct lis3lv02d *lis3) 679 { 680 struct input_dev *input_dev; 681 int err; 682 int max_val, fuzz, flat; 683 int btns[] = {BTN_X, BTN_Y, BTN_Z}; 684 685 if (lis3->idev) 686 return -EINVAL; 687 688 input_dev = input_allocate_device(); 689 if (!input_dev) 690 return -ENOMEM; 691 692 input_dev->name = "ST LIS3LV02DL Accelerometer"; 693 input_dev->phys = DRIVER_NAME "/input0"; 694 input_dev->id.bustype = BUS_HOST; 695 input_dev->id.vendor = 0; 696 input_dev->dev.parent = &lis3->fdev->dev; 697 698 input_dev->open = lis3lv02d_joystick_open; 699 input_dev->close = lis3lv02d_joystick_close; 700 701 max_val = (lis3->mdps_max_val * lis3->scale) / LIS3_ACCURACY; 702 if (lis3->whoami == WAI_12B) { 703 fuzz = LIS3_DEFAULT_FUZZ_12B; 704 flat = LIS3_DEFAULT_FLAT_12B; 705 } else { 706 fuzz = LIS3_DEFAULT_FUZZ_8B; 707 flat = LIS3_DEFAULT_FLAT_8B; 708 } 709 fuzz = (fuzz * lis3->scale) / LIS3_ACCURACY; 710 flat = (flat * lis3->scale) / LIS3_ACCURACY; 711 712 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat); 713 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat); 714 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat); 715 716 input_set_drvdata(input_dev, lis3); 717 lis3->idev = input_dev; 718 719 err = input_setup_polling(input_dev, lis3lv02d_joystick_poll); 720 if (err) 721 goto err_free_input; 722 723 input_set_poll_interval(input_dev, MDPS_POLL_INTERVAL); 724 input_set_min_poll_interval(input_dev, MDPS_POLL_MIN); 725 input_set_max_poll_interval(input_dev, MDPS_POLL_MAX); 726 727 lis3->mapped_btns[0] = lis3lv02d_get_axis(abs(lis3->ac.x), btns); 728 lis3->mapped_btns[1] = lis3lv02d_get_axis(abs(lis3->ac.y), btns); 729 lis3->mapped_btns[2] = lis3lv02d_get_axis(abs(lis3->ac.z), btns); 730 731 err = input_register_device(lis3->idev); 732 if (err) 733 goto err_free_input; 734 735 return 0; 736 737 err_free_input: 738 input_free_device(input_dev); 739 lis3->idev = NULL; 740 return err; 741 742 } 743 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable); 744 745 void lis3lv02d_joystick_disable(struct lis3lv02d *lis3) 746 { 747 if (lis3->irq) 748 free_irq(lis3->irq, lis3); 749 if (lis3->pdata && lis3->pdata->irq2) 750 free_irq(lis3->pdata->irq2, lis3); 751 752 if (!lis3->idev) 753 return; 754 755 if (lis3->irq) 756 misc_deregister(&lis3->miscdev); 757 input_unregister_device(lis3->idev); 758 lis3->idev = NULL; 759 } 760 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable); 761 762 /* Sysfs stuff */ 763 static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3) 764 { 765 /* 766 * SYSFS functions are fast visitors so put-call 767 * immediately after the get-call. However, keep 768 * chip running for a while and schedule delayed 769 * suspend. This way periodic sysfs calls doesn't 770 * suffer from relatively long power up time. 771 */ 772 773 if (lis3->pm_dev) { 774 pm_runtime_get_sync(lis3->pm_dev); 775 pm_runtime_put_noidle(lis3->pm_dev); 776 pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY); 777 } 778 } 779 780 static ssize_t lis3lv02d_selftest_show(struct device *dev, 781 struct device_attribute *attr, char *buf) 782 { 783 struct lis3lv02d *lis3 = dev_get_drvdata(dev); 784 s16 values[3]; 785 786 static const char ok[] = "OK"; 787 static const char fail[] = "FAIL"; 788 static const char irq[] = "FAIL_IRQ"; 789 const char *res; 790 791 lis3lv02d_sysfs_poweron(lis3); 792 switch (lis3lv02d_selftest(lis3, values)) { 793 case SELFTEST_FAIL: 794 res = fail; 795 break; 796 case SELFTEST_IRQ: 797 res = irq; 798 break; 799 case SELFTEST_OK: 800 default: 801 res = ok; 802 break; 803 } 804 return sprintf(buf, "%s %d %d %d\n", res, 805 values[0], values[1], values[2]); 806 } 807 808 static ssize_t lis3lv02d_position_show(struct device *dev, 809 struct device_attribute *attr, char *buf) 810 { 811 struct lis3lv02d *lis3 = dev_get_drvdata(dev); 812 int x, y, z; 813 814 lis3lv02d_sysfs_poweron(lis3); 815 mutex_lock(&lis3->mutex); 816 lis3lv02d_get_xyz(lis3, &x, &y, &z); 817 mutex_unlock(&lis3->mutex); 818 return sprintf(buf, "(%d,%d,%d)\n", x, y, z); 819 } 820 821 static ssize_t lis3lv02d_rate_show(struct device *dev, 822 struct device_attribute *attr, char *buf) 823 { 824 struct lis3lv02d *lis3 = dev_get_drvdata(dev); 825 int odr_idx; 826 827 lis3lv02d_sysfs_poweron(lis3); 828 829 odr_idx = lis3lv02d_get_odr_index(lis3); 830 return sprintf(buf, "%d\n", lis3->odrs[odr_idx]); 831 } 832 833 static ssize_t lis3lv02d_rate_set(struct device *dev, 834 struct device_attribute *attr, const char *buf, 835 size_t count) 836 { 837 struct lis3lv02d *lis3 = dev_get_drvdata(dev); 838 unsigned long rate; 839 int ret; 840 841 ret = kstrtoul(buf, 0, &rate); 842 if (ret) 843 return ret; 844 845 lis3lv02d_sysfs_poweron(lis3); 846 if (lis3lv02d_set_odr(lis3, rate)) 847 return -EINVAL; 848 849 return count; 850 } 851 852 static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL); 853 static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL); 854 static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show, 855 lis3lv02d_rate_set); 856 857 static struct attribute *lis3lv02d_attrs[] = { 858 &dev_attr_selftest.attr, 859 &dev_attr_position.attr, 860 &dev_attr_rate.attr, 861 NULL 862 }; 863 ATTRIBUTE_GROUPS(lis3lv02d); 864 865 static int lis3lv02d_add_fs(struct lis3lv02d *lis3) 866 { 867 lis3->fdev = faux_device_create_with_groups(DRIVER_NAME, NULL, NULL, lis3lv02d_groups); 868 if (!lis3->fdev) 869 return -ENODEV; 870 871 faux_device_set_drvdata(lis3->fdev, lis3); 872 return 0; 873 } 874 875 void lis3lv02d_remove_fs(struct lis3lv02d *lis3) 876 { 877 faux_device_destroy(lis3->fdev); 878 if (lis3->pm_dev) { 879 /* Barrier after the sysfs remove */ 880 pm_runtime_barrier(lis3->pm_dev); 881 882 /* SYSFS may have left chip running. Turn off if necessary */ 883 if (!pm_runtime_suspended(lis3->pm_dev)) 884 lis3lv02d_poweroff(lis3); 885 886 pm_runtime_disable(lis3->pm_dev); 887 pm_runtime_set_suspended(lis3->pm_dev); 888 } 889 kfree(lis3->reg_cache); 890 } 891 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs); 892 893 static void lis3lv02d_8b_configure(struct lis3lv02d *lis3, 894 struct lis3lv02d_platform_data *p) 895 { 896 int err; 897 int ctrl2 = p->hipass_ctrl; 898 899 if (p->click_flags) { 900 lis3->write(lis3, CLICK_CFG, p->click_flags); 901 lis3->write(lis3, CLICK_TIMELIMIT, p->click_time_limit); 902 lis3->write(lis3, CLICK_LATENCY, p->click_latency); 903 lis3->write(lis3, CLICK_WINDOW, p->click_window); 904 lis3->write(lis3, CLICK_THSZ, p->click_thresh_z & 0xf); 905 lis3->write(lis3, CLICK_THSY_X, 906 (p->click_thresh_x & 0xf) | 907 (p->click_thresh_y << 4)); 908 909 if (lis3->idev) { 910 input_set_capability(lis3->idev, EV_KEY, BTN_X); 911 input_set_capability(lis3->idev, EV_KEY, BTN_Y); 912 input_set_capability(lis3->idev, EV_KEY, BTN_Z); 913 } 914 } 915 916 if (p->wakeup_flags) { 917 lis3->write(lis3, FF_WU_CFG_1, p->wakeup_flags); 918 lis3->write(lis3, FF_WU_THS_1, p->wakeup_thresh & 0x7f); 919 /* pdata value + 1 to keep this backward compatible*/ 920 lis3->write(lis3, FF_WU_DURATION_1, p->duration1 + 1); 921 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/ 922 } 923 924 if (p->wakeup_flags2) { 925 lis3->write(lis3, FF_WU_CFG_2, p->wakeup_flags2); 926 lis3->write(lis3, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f); 927 /* pdata value + 1 to keep this backward compatible*/ 928 lis3->write(lis3, FF_WU_DURATION_2, p->duration2 + 1); 929 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/ 930 } 931 /* Configure hipass filters */ 932 lis3->write(lis3, CTRL_REG2, ctrl2); 933 934 if (p->irq2) { 935 err = request_threaded_irq(p->irq2, 936 NULL, 937 lis302dl_interrupt_thread2_8b, 938 IRQF_TRIGGER_RISING | IRQF_ONESHOT | 939 (p->irq_flags2 & IRQF_TRIGGER_MASK), 940 DRIVER_NAME, lis3); 941 if (err < 0) 942 pr_err("No second IRQ. Limited functionality\n"); 943 } 944 } 945 946 #ifdef CONFIG_OF 947 int lis3lv02d_init_dt(struct lis3lv02d *lis3) 948 { 949 struct lis3lv02d_platform_data *pdata; 950 struct device_node *np = lis3->of_node; 951 u32 val; 952 s32 sval; 953 954 if (!lis3->of_node) 955 return 0; 956 957 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); 958 if (!pdata) 959 return -ENOMEM; 960 961 if (of_property_read_bool(np, "st,click-single-x")) 962 pdata->click_flags |= LIS3_CLICK_SINGLE_X; 963 if (of_property_read_bool(np, "st,click-double-x")) 964 pdata->click_flags |= LIS3_CLICK_DOUBLE_X; 965 966 if (of_property_read_bool(np, "st,click-single-y")) 967 pdata->click_flags |= LIS3_CLICK_SINGLE_Y; 968 if (of_property_read_bool(np, "st,click-double-y")) 969 pdata->click_flags |= LIS3_CLICK_DOUBLE_Y; 970 971 if (of_property_read_bool(np, "st,click-single-z")) 972 pdata->click_flags |= LIS3_CLICK_SINGLE_Z; 973 if (of_property_read_bool(np, "st,click-double-z")) 974 pdata->click_flags |= LIS3_CLICK_DOUBLE_Z; 975 976 if (!of_property_read_u32(np, "st,click-threshold-x", &val)) 977 pdata->click_thresh_x = val; 978 if (!of_property_read_u32(np, "st,click-threshold-y", &val)) 979 pdata->click_thresh_y = val; 980 if (!of_property_read_u32(np, "st,click-threshold-z", &val)) 981 pdata->click_thresh_z = val; 982 983 if (!of_property_read_u32(np, "st,click-time-limit", &val)) 984 pdata->click_time_limit = val; 985 if (!of_property_read_u32(np, "st,click-latency", &val)) 986 pdata->click_latency = val; 987 if (!of_property_read_u32(np, "st,click-window", &val)) 988 pdata->click_window = val; 989 990 if (of_property_read_bool(np, "st,irq1-disable")) 991 pdata->irq_cfg |= LIS3_IRQ1_DISABLE; 992 if (of_property_read_bool(np, "st,irq1-ff-wu-1")) 993 pdata->irq_cfg |= LIS3_IRQ1_FF_WU_1; 994 if (of_property_read_bool(np, "st,irq1-ff-wu-2")) 995 pdata->irq_cfg |= LIS3_IRQ1_FF_WU_2; 996 if (of_property_read_bool(np, "st,irq1-data-ready")) 997 pdata->irq_cfg |= LIS3_IRQ1_DATA_READY; 998 if (of_property_read_bool(np, "st,irq1-click")) 999 pdata->irq_cfg |= LIS3_IRQ1_CLICK; 1000 1001 if (of_property_read_bool(np, "st,irq2-disable")) 1002 pdata->irq_cfg |= LIS3_IRQ2_DISABLE; 1003 if (of_property_read_bool(np, "st,irq2-ff-wu-1")) 1004 pdata->irq_cfg |= LIS3_IRQ2_FF_WU_1; 1005 if (of_property_read_bool(np, "st,irq2-ff-wu-2")) 1006 pdata->irq_cfg |= LIS3_IRQ2_FF_WU_2; 1007 if (of_property_read_bool(np, "st,irq2-data-ready")) 1008 pdata->irq_cfg |= LIS3_IRQ2_DATA_READY; 1009 if (of_property_read_bool(np, "st,irq2-click")) 1010 pdata->irq_cfg |= LIS3_IRQ2_CLICK; 1011 1012 if (of_property_read_bool(np, "st,irq-open-drain")) 1013 pdata->irq_cfg |= LIS3_IRQ_OPEN_DRAIN; 1014 if (of_property_read_bool(np, "st,irq-active-low")) 1015 pdata->irq_cfg |= LIS3_IRQ_ACTIVE_LOW; 1016 1017 if (!of_property_read_u32(np, "st,wu-duration-1", &val)) 1018 pdata->duration1 = val; 1019 if (!of_property_read_u32(np, "st,wu-duration-2", &val)) 1020 pdata->duration2 = val; 1021 1022 if (of_property_read_bool(np, "st,wakeup-x-lo")) 1023 pdata->wakeup_flags |= LIS3_WAKEUP_X_LO; 1024 if (of_property_read_bool(np, "st,wakeup-x-hi")) 1025 pdata->wakeup_flags |= LIS3_WAKEUP_X_HI; 1026 if (of_property_read_bool(np, "st,wakeup-y-lo")) 1027 pdata->wakeup_flags |= LIS3_WAKEUP_Y_LO; 1028 if (of_property_read_bool(np, "st,wakeup-y-hi")) 1029 pdata->wakeup_flags |= LIS3_WAKEUP_Y_HI; 1030 if (of_property_read_bool(np, "st,wakeup-z-lo")) 1031 pdata->wakeup_flags |= LIS3_WAKEUP_Z_LO; 1032 if (of_property_read_bool(np, "st,wakeup-z-hi")) 1033 pdata->wakeup_flags |= LIS3_WAKEUP_Z_HI; 1034 if (!of_property_read_u32(np, "st,wakeup-threshold", &val)) 1035 pdata->wakeup_thresh = val; 1036 1037 if (of_property_read_bool(np, "st,wakeup2-x-lo")) 1038 pdata->wakeup_flags2 |= LIS3_WAKEUP_X_LO; 1039 if (of_property_read_bool(np, "st,wakeup2-x-hi")) 1040 pdata->wakeup_flags2 |= LIS3_WAKEUP_X_HI; 1041 if (of_property_read_bool(np, "st,wakeup2-y-lo")) 1042 pdata->wakeup_flags2 |= LIS3_WAKEUP_Y_LO; 1043 if (of_property_read_bool(np, "st,wakeup2-y-hi")) 1044 pdata->wakeup_flags2 |= LIS3_WAKEUP_Y_HI; 1045 if (of_property_read_bool(np, "st,wakeup2-z-lo")) 1046 pdata->wakeup_flags2 |= LIS3_WAKEUP_Z_LO; 1047 if (of_property_read_bool(np, "st,wakeup2-z-hi")) 1048 pdata->wakeup_flags2 |= LIS3_WAKEUP_Z_HI; 1049 if (!of_property_read_u32(np, "st,wakeup2-threshold", &val)) 1050 pdata->wakeup_thresh2 = val; 1051 1052 if (!of_property_read_u32(np, "st,highpass-cutoff-hz", &val)) { 1053 switch (val) { 1054 case 1: 1055 pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_1HZ; 1056 break; 1057 case 2: 1058 pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_2HZ; 1059 break; 1060 case 4: 1061 pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_4HZ; 1062 break; 1063 case 8: 1064 pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_8HZ; 1065 break; 1066 } 1067 } 1068 1069 if (of_property_read_bool(np, "st,hipass1-disable")) 1070 pdata->hipass_ctrl |= LIS3_HIPASS1_DISABLE; 1071 if (of_property_read_bool(np, "st,hipass2-disable")) 1072 pdata->hipass_ctrl |= LIS3_HIPASS2_DISABLE; 1073 1074 if (of_property_read_s32(np, "st,axis-x", &sval) == 0) 1075 pdata->axis_x = sval; 1076 if (of_property_read_s32(np, "st,axis-y", &sval) == 0) 1077 pdata->axis_y = sval; 1078 if (of_property_read_s32(np, "st,axis-z", &sval) == 0) 1079 pdata->axis_z = sval; 1080 1081 if (of_property_read_u32(np, "st,default-rate", &val) == 0) 1082 pdata->default_rate = val; 1083 1084 if (of_property_read_s32(np, "st,min-limit-x", &sval) == 0) 1085 pdata->st_min_limits[0] = sval; 1086 if (of_property_read_s32(np, "st,min-limit-y", &sval) == 0) 1087 pdata->st_min_limits[1] = sval; 1088 if (of_property_read_s32(np, "st,min-limit-z", &sval) == 0) 1089 pdata->st_min_limits[2] = sval; 1090 1091 if (of_property_read_s32(np, "st,max-limit-x", &sval) == 0) 1092 pdata->st_max_limits[0] = sval; 1093 if (of_property_read_s32(np, "st,max-limit-y", &sval) == 0) 1094 pdata->st_max_limits[1] = sval; 1095 if (of_property_read_s32(np, "st,max-limit-z", &sval) == 0) 1096 pdata->st_max_limits[2] = sval; 1097 1098 1099 lis3->pdata = pdata; 1100 1101 return 0; 1102 } 1103 1104 #else 1105 int lis3lv02d_init_dt(struct lis3lv02d *lis3) 1106 { 1107 return 0; 1108 } 1109 #endif 1110 EXPORT_SYMBOL_GPL(lis3lv02d_init_dt); 1111 1112 /* 1113 * Initialise the accelerometer and the various subsystems. 1114 * Should be rather independent of the bus system. 1115 */ 1116 int lis3lv02d_init_device(struct lis3lv02d *lis3) 1117 { 1118 int err; 1119 irq_handler_t thread_fn; 1120 int irq_flags = 0; 1121 1122 lis3->whoami = lis3lv02d_read_8(lis3, WHO_AM_I); 1123 1124 switch (lis3->whoami) { 1125 case WAI_12B: 1126 pr_info("12 bits sensor found\n"); 1127 lis3->read_data = lis3lv02d_read_12; 1128 lis3->mdps_max_val = 2048; 1129 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_12B; 1130 lis3->odrs = lis3_12_rates; 1131 lis3->odr_mask = CTRL1_DF0 | CTRL1_DF1; 1132 lis3->scale = LIS3_SENSITIVITY_12B; 1133 lis3->regs = lis3_wai12_regs; 1134 lis3->regs_size = ARRAY_SIZE(lis3_wai12_regs); 1135 break; 1136 case WAI_8B: 1137 pr_info("8 bits sensor found\n"); 1138 lis3->read_data = lis3lv02d_read_8; 1139 lis3->mdps_max_val = 128; 1140 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B; 1141 lis3->odrs = lis3_8_rates; 1142 lis3->odr_mask = CTRL1_DR; 1143 lis3->scale = LIS3_SENSITIVITY_8B; 1144 lis3->regs = lis3_wai8_regs; 1145 lis3->regs_size = ARRAY_SIZE(lis3_wai8_regs); 1146 break; 1147 case WAI_3DC: 1148 pr_info("8 bits 3DC sensor found\n"); 1149 lis3->read_data = lis3lv02d_read_8; 1150 lis3->mdps_max_val = 128; 1151 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B; 1152 lis3->odrs = lis3_3dc_rates; 1153 lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3; 1154 lis3->scale = LIS3_SENSITIVITY_8B; 1155 break; 1156 case WAI_3DLH: 1157 pr_info("16 bits lis331dlh sensor found\n"); 1158 lis3->read_data = lis331dlh_read_data; 1159 lis3->mdps_max_val = 2048; /* 12 bits for 2G */ 1160 lis3->shift_adj = SHIFT_ADJ_2G; 1161 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B; 1162 lis3->odrs = lis3_3dlh_rates; 1163 lis3->odr_mask = CTRL1_DR0 | CTRL1_DR1; 1164 lis3->scale = LIS3DLH_SENSITIVITY_2G; 1165 break; 1166 default: 1167 pr_err("unknown sensor type 0x%X\n", lis3->whoami); 1168 return -ENODEV; 1169 } 1170 1171 lis3->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs), 1172 sizeof(lis3_wai12_regs)), GFP_KERNEL); 1173 1174 if (lis3->reg_cache == NULL) 1175 return -ENOMEM; 1176 1177 mutex_init(&lis3->mutex); 1178 atomic_set(&lis3->wake_thread, 0); 1179 1180 lis3lv02d_add_fs(lis3); 1181 err = lis3lv02d_poweron(lis3); 1182 if (err) { 1183 lis3lv02d_remove_fs(lis3); 1184 return err; 1185 } 1186 1187 if (lis3->pm_dev) { 1188 pm_runtime_set_active(lis3->pm_dev); 1189 pm_runtime_enable(lis3->pm_dev); 1190 } 1191 1192 if (lis3lv02d_joystick_enable(lis3)) 1193 pr_err("joystick initialization failed\n"); 1194 1195 /* passing in platform specific data is purely optional and only 1196 * used by the SPI transport layer at the moment */ 1197 if (lis3->pdata) { 1198 struct lis3lv02d_platform_data *p = lis3->pdata; 1199 1200 if (lis3->whoami == WAI_8B) 1201 lis3lv02d_8b_configure(lis3, p); 1202 1203 irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK; 1204 1205 lis3->irq_cfg = p->irq_cfg; 1206 if (p->irq_cfg) 1207 lis3->write(lis3, CTRL_REG3, p->irq_cfg); 1208 1209 if (p->default_rate) 1210 lis3lv02d_set_odr(lis3, p->default_rate); 1211 } 1212 1213 /* bail if we did not get an IRQ from the bus layer */ 1214 if (!lis3->irq) { 1215 pr_debug("No IRQ. Disabling /dev/freefall\n"); 1216 goto out; 1217 } 1218 1219 /* 1220 * The sensor can generate interrupts for free-fall and direction 1221 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep 1222 * the things simple and _fast_ we activate it only for free-fall, so 1223 * no need to read register (very slow with ACPI). For the same reason, 1224 * we forbid shared interrupts. 1225 * 1226 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the 1227 * io-apic is not configurable (and generates a warning) but I keep it 1228 * in case of support for other hardware. 1229 */ 1230 if (lis3->pdata && lis3->whoami == WAI_8B) 1231 thread_fn = lis302dl_interrupt_thread1_8b; 1232 else 1233 thread_fn = NULL; 1234 1235 err = request_threaded_irq(lis3->irq, lis302dl_interrupt, 1236 thread_fn, 1237 IRQF_TRIGGER_RISING | IRQF_ONESHOT | 1238 irq_flags, 1239 DRIVER_NAME, lis3); 1240 1241 if (err < 0) { 1242 pr_err("Cannot get IRQ\n"); 1243 goto out; 1244 } 1245 1246 lis3->miscdev.minor = MISC_DYNAMIC_MINOR; 1247 lis3->miscdev.name = "freefall"; 1248 lis3->miscdev.fops = &lis3lv02d_misc_fops; 1249 1250 if (misc_register(&lis3->miscdev)) 1251 pr_err("misc_register failed\n"); 1252 out: 1253 return 0; 1254 } 1255 EXPORT_SYMBOL_GPL(lis3lv02d_init_device); 1256 1257 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver"); 1258 MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek"); 1259 MODULE_LICENSE("GPL"); 1260