lis3lv02d.c 26 KB

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