lis3lv02d.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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 <linux/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. if (lis3->pdata) {
  325. lis3->read(lis3, CTRL_REG2, &reg);
  326. if (lis3->whoami == WAI_12B)
  327. reg |= CTRL2_BDU | CTRL2_BOOT;
  328. else
  329. reg |= CTRL2_BOOT_8B;
  330. lis3->write(lis3, CTRL_REG2, reg);
  331. }
  332. /* LIS3 power on delay is quite long */
  333. msleep(lis3->pwron_delay / lis3lv02d_get_odr());
  334. if (lis3->reg_ctrl)
  335. lis3_context_restore(lis3);
  336. }
  337. EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
  338. static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
  339. {
  340. int x, y, z;
  341. mutex_lock(&lis3_dev.mutex);
  342. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  343. input_report_abs(pidev->input, ABS_X, x);
  344. input_report_abs(pidev->input, ABS_Y, y);
  345. input_report_abs(pidev->input, ABS_Z, z);
  346. input_sync(pidev->input);
  347. mutex_unlock(&lis3_dev.mutex);
  348. }
  349. static void lis3lv02d_joystick_open(struct input_polled_dev *pidev)
  350. {
  351. if (lis3_dev.pm_dev)
  352. pm_runtime_get_sync(lis3_dev.pm_dev);
  353. if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev)
  354. atomic_set(&lis3_dev.wake_thread, 1);
  355. /*
  356. * Update coordinates for the case where poll interval is 0 and
  357. * the chip in running purely under interrupt control
  358. */
  359. lis3lv02d_joystick_poll(pidev);
  360. }
  361. static void lis3lv02d_joystick_close(struct input_polled_dev *pidev)
  362. {
  363. atomic_set(&lis3_dev.wake_thread, 0);
  364. if (lis3_dev.pm_dev)
  365. pm_runtime_put(lis3_dev.pm_dev);
  366. }
  367. static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
  368. {
  369. if (!test_bit(0, &lis3_dev.misc_opened))
  370. goto out;
  371. /*
  372. * Be careful: on some HP laptops the bios force DD when on battery and
  373. * the lid is closed. This leads to interrupts as soon as a little move
  374. * is done.
  375. */
  376. atomic_inc(&lis3_dev.count);
  377. wake_up_interruptible(&lis3_dev.misc_wait);
  378. kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
  379. out:
  380. if (atomic_read(&lis3_dev.wake_thread))
  381. return IRQ_WAKE_THREAD;
  382. return IRQ_HANDLED;
  383. }
  384. static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
  385. {
  386. struct input_dev *dev = lis3->idev->input;
  387. u8 click_src;
  388. mutex_lock(&lis3->mutex);
  389. lis3->read(lis3, CLICK_SRC, &click_src);
  390. if (click_src & CLICK_SINGLE_X) {
  391. input_report_key(dev, lis3->mapped_btns[0], 1);
  392. input_report_key(dev, lis3->mapped_btns[0], 0);
  393. }
  394. if (click_src & CLICK_SINGLE_Y) {
  395. input_report_key(dev, lis3->mapped_btns[1], 1);
  396. input_report_key(dev, lis3->mapped_btns[1], 0);
  397. }
  398. if (click_src & CLICK_SINGLE_Z) {
  399. input_report_key(dev, lis3->mapped_btns[2], 1);
  400. input_report_key(dev, lis3->mapped_btns[2], 0);
  401. }
  402. input_sync(dev);
  403. mutex_unlock(&lis3->mutex);
  404. }
  405. static inline void lis302dl_data_ready(struct lis3lv02d *lis3, int index)
  406. {
  407. int dummy;
  408. /* Dummy read to ack interrupt */
  409. lis3lv02d_get_xyz(lis3, &dummy, &dummy, &dummy);
  410. lis3->data_ready_count[index]++;
  411. }
  412. static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
  413. {
  414. struct lis3lv02d *lis3 = data;
  415. u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ1_MASK;
  416. if (irq_cfg == LIS3_IRQ1_CLICK)
  417. lis302dl_interrupt_handle_click(lis3);
  418. else if (unlikely(irq_cfg == LIS3_IRQ1_DATA_READY))
  419. lis302dl_data_ready(lis3, IRQ_LINE0);
  420. else
  421. lis3lv02d_joystick_poll(lis3->idev);
  422. return IRQ_HANDLED;
  423. }
  424. static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
  425. {
  426. struct lis3lv02d *lis3 = data;
  427. u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ2_MASK;
  428. if (irq_cfg == LIS3_IRQ2_CLICK)
  429. lis302dl_interrupt_handle_click(lis3);
  430. else if (unlikely(irq_cfg == LIS3_IRQ2_DATA_READY))
  431. lis302dl_data_ready(lis3, IRQ_LINE1);
  432. else
  433. lis3lv02d_joystick_poll(lis3->idev);
  434. return IRQ_HANDLED;
  435. }
  436. static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
  437. {
  438. if (test_and_set_bit(0, &lis3_dev.misc_opened))
  439. return -EBUSY; /* already open */
  440. if (lis3_dev.pm_dev)
  441. pm_runtime_get_sync(lis3_dev.pm_dev);
  442. atomic_set(&lis3_dev.count, 0);
  443. return 0;
  444. }
  445. static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
  446. {
  447. fasync_helper(-1, file, 0, &lis3_dev.async_queue);
  448. clear_bit(0, &lis3_dev.misc_opened); /* release the device */
  449. if (lis3_dev.pm_dev)
  450. pm_runtime_put(lis3_dev.pm_dev);
  451. return 0;
  452. }
  453. static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
  454. size_t count, loff_t *pos)
  455. {
  456. DECLARE_WAITQUEUE(wait, current);
  457. u32 data;
  458. unsigned char byte_data;
  459. ssize_t retval = 1;
  460. if (count < 1)
  461. return -EINVAL;
  462. add_wait_queue(&lis3_dev.misc_wait, &wait);
  463. while (true) {
  464. set_current_state(TASK_INTERRUPTIBLE);
  465. data = atomic_xchg(&lis3_dev.count, 0);
  466. if (data)
  467. break;
  468. if (file->f_flags & O_NONBLOCK) {
  469. retval = -EAGAIN;
  470. goto out;
  471. }
  472. if (signal_pending(current)) {
  473. retval = -ERESTARTSYS;
  474. goto out;
  475. }
  476. schedule();
  477. }
  478. if (data < 255)
  479. byte_data = data;
  480. else
  481. byte_data = 255;
  482. /* make sure we are not going into copy_to_user() with
  483. * TASK_INTERRUPTIBLE state */
  484. set_current_state(TASK_RUNNING);
  485. if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
  486. retval = -EFAULT;
  487. out:
  488. __set_current_state(TASK_RUNNING);
  489. remove_wait_queue(&lis3_dev.misc_wait, &wait);
  490. return retval;
  491. }
  492. static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
  493. {
  494. poll_wait(file, &lis3_dev.misc_wait, wait);
  495. if (atomic_read(&lis3_dev.count))
  496. return POLLIN | POLLRDNORM;
  497. return 0;
  498. }
  499. static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
  500. {
  501. return fasync_helper(fd, file, on, &lis3_dev.async_queue);
  502. }
  503. static const struct file_operations lis3lv02d_misc_fops = {
  504. .owner = THIS_MODULE,
  505. .llseek = no_llseek,
  506. .read = lis3lv02d_misc_read,
  507. .open = lis3lv02d_misc_open,
  508. .release = lis3lv02d_misc_release,
  509. .poll = lis3lv02d_misc_poll,
  510. .fasync = lis3lv02d_misc_fasync,
  511. };
  512. static struct miscdevice lis3lv02d_misc_device = {
  513. .minor = MISC_DYNAMIC_MINOR,
  514. .name = "freefall",
  515. .fops = &lis3lv02d_misc_fops,
  516. };
  517. int lis3lv02d_joystick_enable(void)
  518. {
  519. struct input_dev *input_dev;
  520. int err;
  521. int max_val, fuzz, flat;
  522. int btns[] = {BTN_X, BTN_Y, BTN_Z};
  523. if (lis3_dev.idev)
  524. return -EINVAL;
  525. lis3_dev.idev = input_allocate_polled_device();
  526. if (!lis3_dev.idev)
  527. return -ENOMEM;
  528. lis3_dev.idev->poll = lis3lv02d_joystick_poll;
  529. lis3_dev.idev->open = lis3lv02d_joystick_open;
  530. lis3_dev.idev->close = lis3lv02d_joystick_close;
  531. lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
  532. lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN;
  533. lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX;
  534. input_dev = lis3_dev.idev->input;
  535. input_dev->name = "ST LIS3LV02DL Accelerometer";
  536. input_dev->phys = DRIVER_NAME "/input0";
  537. input_dev->id.bustype = BUS_HOST;
  538. input_dev->id.vendor = 0;
  539. input_dev->dev.parent = &lis3_dev.pdev->dev;
  540. set_bit(EV_ABS, input_dev->evbit);
  541. max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
  542. if (lis3_dev.whoami == WAI_12B) {
  543. fuzz = LIS3_DEFAULT_FUZZ_12B;
  544. flat = LIS3_DEFAULT_FLAT_12B;
  545. } else {
  546. fuzz = LIS3_DEFAULT_FUZZ_8B;
  547. flat = LIS3_DEFAULT_FLAT_8B;
  548. }
  549. fuzz = (fuzz * lis3_dev.scale) / LIS3_ACCURACY;
  550. flat = (flat * lis3_dev.scale) / LIS3_ACCURACY;
  551. input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
  552. input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
  553. input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
  554. lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
  555. lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
  556. lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
  557. err = input_register_polled_device(lis3_dev.idev);
  558. if (err) {
  559. input_free_polled_device(lis3_dev.idev);
  560. lis3_dev.idev = NULL;
  561. }
  562. return err;
  563. }
  564. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  565. void lis3lv02d_joystick_disable(void)
  566. {
  567. if (lis3_dev.irq)
  568. free_irq(lis3_dev.irq, &lis3_dev);
  569. if (lis3_dev.pdata && lis3_dev.pdata->irq2)
  570. free_irq(lis3_dev.pdata->irq2, &lis3_dev);
  571. if (!lis3_dev.idev)
  572. return;
  573. if (lis3_dev.irq)
  574. misc_deregister(&lis3lv02d_misc_device);
  575. input_unregister_polled_device(lis3_dev.idev);
  576. input_free_polled_device(lis3_dev.idev);
  577. lis3_dev.idev = NULL;
  578. }
  579. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
  580. /* Sysfs stuff */
  581. static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
  582. {
  583. /*
  584. * SYSFS functions are fast visitors so put-call
  585. * immediately after the get-call. However, keep
  586. * chip running for a while and schedule delayed
  587. * suspend. This way periodic sysfs calls doesn't
  588. * suffer from relatively long power up time.
  589. */
  590. if (lis3->pm_dev) {
  591. pm_runtime_get_sync(lis3->pm_dev);
  592. pm_runtime_put_noidle(lis3->pm_dev);
  593. pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
  594. }
  595. }
  596. static ssize_t lis3lv02d_selftest_show(struct device *dev,
  597. struct device_attribute *attr, char *buf)
  598. {
  599. s16 values[3];
  600. static const char ok[] = "OK";
  601. static const char fail[] = "FAIL";
  602. static const char irq[] = "FAIL_IRQ";
  603. const char *res;
  604. lis3lv02d_sysfs_poweron(&lis3_dev);
  605. switch (lis3lv02d_selftest(&lis3_dev, values)) {
  606. case SELFTEST_FAIL:
  607. res = fail;
  608. break;
  609. case SELFTEST_IRQ:
  610. res = irq;
  611. break;
  612. case SELFTEST_OK:
  613. default:
  614. res = ok;
  615. break;
  616. }
  617. return sprintf(buf, "%s %d %d %d\n", res,
  618. values[0], values[1], values[2]);
  619. }
  620. static ssize_t lis3lv02d_position_show(struct device *dev,
  621. struct device_attribute *attr, char *buf)
  622. {
  623. int x, y, z;
  624. lis3lv02d_sysfs_poweron(&lis3_dev);
  625. mutex_lock(&lis3_dev.mutex);
  626. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  627. mutex_unlock(&lis3_dev.mutex);
  628. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  629. }
  630. static ssize_t lis3lv02d_rate_show(struct device *dev,
  631. struct device_attribute *attr, char *buf)
  632. {
  633. lis3lv02d_sysfs_poweron(&lis3_dev);
  634. return sprintf(buf, "%d\n", lis3lv02d_get_odr());
  635. }
  636. static ssize_t lis3lv02d_rate_set(struct device *dev,
  637. struct device_attribute *attr, const char *buf,
  638. size_t count)
  639. {
  640. unsigned long rate;
  641. if (strict_strtoul(buf, 0, &rate))
  642. return -EINVAL;
  643. lis3lv02d_sysfs_poweron(&lis3_dev);
  644. if (lis3lv02d_set_odr(rate))
  645. return -EINVAL;
  646. return count;
  647. }
  648. static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
  649. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  650. static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
  651. lis3lv02d_rate_set);
  652. static struct attribute *lis3lv02d_attributes[] = {
  653. &dev_attr_selftest.attr,
  654. &dev_attr_position.attr,
  655. &dev_attr_rate.attr,
  656. NULL
  657. };
  658. static struct attribute_group lis3lv02d_attribute_group = {
  659. .attrs = lis3lv02d_attributes
  660. };
  661. static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
  662. {
  663. lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  664. if (IS_ERR(lis3->pdev))
  665. return PTR_ERR(lis3->pdev);
  666. return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  667. }
  668. int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
  669. {
  670. sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  671. platform_device_unregister(lis3->pdev);
  672. if (lis3->pm_dev) {
  673. /* Barrier after the sysfs remove */
  674. pm_runtime_barrier(lis3->pm_dev);
  675. /* SYSFS may have left chip running. Turn off if necessary */
  676. if (!pm_runtime_suspended(lis3->pm_dev))
  677. lis3lv02d_poweroff(&lis3_dev);
  678. pm_runtime_disable(lis3->pm_dev);
  679. pm_runtime_set_suspended(lis3->pm_dev);
  680. }
  681. kfree(lis3->reg_cache);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  685. static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
  686. struct lis3lv02d_platform_data *p)
  687. {
  688. int err;
  689. int ctrl2 = p->hipass_ctrl;
  690. if (p->click_flags) {
  691. dev->write(dev, CLICK_CFG, p->click_flags);
  692. dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
  693. dev->write(dev, CLICK_LATENCY, p->click_latency);
  694. dev->write(dev, CLICK_WINDOW, p->click_window);
  695. dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
  696. dev->write(dev, CLICK_THSY_X,
  697. (p->click_thresh_x & 0xf) |
  698. (p->click_thresh_y << 4));
  699. if (dev->idev) {
  700. struct input_dev *input_dev = lis3_dev.idev->input;
  701. input_set_capability(input_dev, EV_KEY, BTN_X);
  702. input_set_capability(input_dev, EV_KEY, BTN_Y);
  703. input_set_capability(input_dev, EV_KEY, BTN_Z);
  704. }
  705. }
  706. if (p->wakeup_flags) {
  707. dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
  708. dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
  709. /* pdata value + 1 to keep this backward compatible*/
  710. dev->write(dev, FF_WU_DURATION_1, p->duration1 + 1);
  711. ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
  712. }
  713. if (p->wakeup_flags2) {
  714. dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
  715. dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
  716. /* pdata value + 1 to keep this backward compatible*/
  717. dev->write(dev, FF_WU_DURATION_2, p->duration2 + 1);
  718. ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
  719. }
  720. /* Configure hipass filters */
  721. dev->write(dev, CTRL_REG2, ctrl2);
  722. if (p->irq2) {
  723. err = request_threaded_irq(p->irq2,
  724. NULL,
  725. lis302dl_interrupt_thread2_8b,
  726. IRQF_TRIGGER_RISING | IRQF_ONESHOT |
  727. (p->irq_flags2 & IRQF_TRIGGER_MASK),
  728. DRIVER_NAME, &lis3_dev);
  729. if (err < 0)
  730. pr_err("No second IRQ. Limited functionality\n");
  731. }
  732. }
  733. /*
  734. * Initialise the accelerometer and the various subsystems.
  735. * Should be rather independent of the bus system.
  736. */
  737. int lis3lv02d_init_device(struct lis3lv02d *dev)
  738. {
  739. int err;
  740. irq_handler_t thread_fn;
  741. int irq_flags = 0;
  742. dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
  743. switch (dev->whoami) {
  744. case WAI_12B:
  745. pr_info("12 bits sensor found\n");
  746. dev->read_data = lis3lv02d_read_12;
  747. dev->mdps_max_val = 2048;
  748. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
  749. dev->odrs = lis3_12_rates;
  750. dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
  751. dev->scale = LIS3_SENSITIVITY_12B;
  752. dev->regs = lis3_wai12_regs;
  753. dev->regs_size = ARRAY_SIZE(lis3_wai12_regs);
  754. break;
  755. case WAI_8B:
  756. pr_info("8 bits sensor found\n");
  757. dev->read_data = lis3lv02d_read_8;
  758. dev->mdps_max_val = 128;
  759. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  760. dev->odrs = lis3_8_rates;
  761. dev->odr_mask = CTRL1_DR;
  762. dev->scale = LIS3_SENSITIVITY_8B;
  763. dev->regs = lis3_wai8_regs;
  764. dev->regs_size = ARRAY_SIZE(lis3_wai8_regs);
  765. break;
  766. case WAI_3DC:
  767. pr_info("8 bits 3DC sensor found\n");
  768. dev->read_data = lis3lv02d_read_8;
  769. dev->mdps_max_val = 128;
  770. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  771. dev->odrs = lis3_3dc_rates;
  772. dev->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
  773. dev->scale = LIS3_SENSITIVITY_8B;
  774. break;
  775. default:
  776. pr_err("unknown sensor type 0x%X\n", dev->whoami);
  777. return -EINVAL;
  778. }
  779. dev->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
  780. sizeof(lis3_wai12_regs)), GFP_KERNEL);
  781. if (dev->reg_cache == NULL) {
  782. printk(KERN_ERR DRIVER_NAME "out of memory\n");
  783. return -ENOMEM;
  784. }
  785. mutex_init(&dev->mutex);
  786. atomic_set(&dev->wake_thread, 0);
  787. lis3lv02d_add_fs(dev);
  788. lis3lv02d_poweron(dev);
  789. if (dev->pm_dev) {
  790. pm_runtime_set_active(dev->pm_dev);
  791. pm_runtime_enable(dev->pm_dev);
  792. }
  793. if (lis3lv02d_joystick_enable())
  794. pr_err("joystick initialization failed\n");
  795. /* passing in platform specific data is purely optional and only
  796. * used by the SPI transport layer at the moment */
  797. if (dev->pdata) {
  798. struct lis3lv02d_platform_data *p = dev->pdata;
  799. if (dev->whoami == WAI_8B)
  800. lis3lv02d_8b_configure(dev, p);
  801. irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK;
  802. dev->irq_cfg = p->irq_cfg;
  803. if (p->irq_cfg)
  804. dev->write(dev, CTRL_REG3, p->irq_cfg);
  805. if (p->default_rate)
  806. lis3lv02d_set_odr(p->default_rate);
  807. }
  808. /* bail if we did not get an IRQ from the bus layer */
  809. if (!dev->irq) {
  810. pr_debug("No IRQ. Disabling /dev/freefall\n");
  811. goto out;
  812. }
  813. /*
  814. * The sensor can generate interrupts for free-fall and direction
  815. * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
  816. * the things simple and _fast_ we activate it only for free-fall, so
  817. * no need to read register (very slow with ACPI). For the same reason,
  818. * we forbid shared interrupts.
  819. *
  820. * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
  821. * io-apic is not configurable (and generates a warning) but I keep it
  822. * in case of support for other hardware.
  823. */
  824. if (dev->pdata && dev->whoami == WAI_8B)
  825. thread_fn = lis302dl_interrupt_thread1_8b;
  826. else
  827. thread_fn = NULL;
  828. err = request_threaded_irq(dev->irq, lis302dl_interrupt,
  829. thread_fn,
  830. IRQF_TRIGGER_RISING | IRQF_ONESHOT |
  831. irq_flags,
  832. DRIVER_NAME, &lis3_dev);
  833. if (err < 0) {
  834. pr_err("Cannot get IRQ\n");
  835. goto out;
  836. }
  837. if (misc_register(&lis3lv02d_misc_device))
  838. pr_err("misc_register failed\n");
  839. out:
  840. return 0;
  841. }
  842. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  843. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  844. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  845. MODULE_LICENSE("GPL");