lis3lv02d.c 27 KB

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