lis3lv02d.c 26 KB

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