lis3lv02d.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. 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_debug("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");