lis3lv02d.c 33 KB

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