lis3lv02d.c 27 KB

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