lis3lv02d.c 26 KB

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