lis3lv02d.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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/sched/signal.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 const 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. clear_bit(0, &lis3->misc_opened); /* release the device */
  500. if (lis3->pm_dev)
  501. pm_runtime_put(lis3->pm_dev);
  502. return 0;
  503. }
  504. static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
  505. size_t count, loff_t *pos)
  506. {
  507. struct lis3lv02d *lis3 = container_of(file->private_data,
  508. struct lis3lv02d, miscdev);
  509. DECLARE_WAITQUEUE(wait, current);
  510. u32 data;
  511. unsigned char byte_data;
  512. ssize_t retval = 1;
  513. if (count < 1)
  514. return -EINVAL;
  515. add_wait_queue(&lis3->misc_wait, &wait);
  516. while (true) {
  517. set_current_state(TASK_INTERRUPTIBLE);
  518. data = atomic_xchg(&lis3->count, 0);
  519. if (data)
  520. break;
  521. if (file->f_flags & O_NONBLOCK) {
  522. retval = -EAGAIN;
  523. goto out;
  524. }
  525. if (signal_pending(current)) {
  526. retval = -ERESTARTSYS;
  527. goto out;
  528. }
  529. schedule();
  530. }
  531. if (data < 255)
  532. byte_data = data;
  533. else
  534. byte_data = 255;
  535. /* make sure we are not going into copy_to_user() with
  536. * TASK_INTERRUPTIBLE state */
  537. set_current_state(TASK_RUNNING);
  538. if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
  539. retval = -EFAULT;
  540. out:
  541. __set_current_state(TASK_RUNNING);
  542. remove_wait_queue(&lis3->misc_wait, &wait);
  543. return retval;
  544. }
  545. static __poll_t lis3lv02d_misc_poll(struct file *file, poll_table *wait)
  546. {
  547. struct lis3lv02d *lis3 = container_of(file->private_data,
  548. struct lis3lv02d, miscdev);
  549. poll_wait(file, &lis3->misc_wait, wait);
  550. if (atomic_read(&lis3->count))
  551. return EPOLLIN | EPOLLRDNORM;
  552. return 0;
  553. }
  554. static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
  555. {
  556. struct lis3lv02d *lis3 = container_of(file->private_data,
  557. struct lis3lv02d, miscdev);
  558. return fasync_helper(fd, file, on, &lis3->async_queue);
  559. }
  560. static const struct file_operations lis3lv02d_misc_fops = {
  561. .owner = THIS_MODULE,
  562. .llseek = no_llseek,
  563. .read = lis3lv02d_misc_read,
  564. .open = lis3lv02d_misc_open,
  565. .release = lis3lv02d_misc_release,
  566. .poll = lis3lv02d_misc_poll,
  567. .fasync = lis3lv02d_misc_fasync,
  568. };
  569. int lis3lv02d_joystick_enable(struct lis3lv02d *lis3)
  570. {
  571. struct input_dev *input_dev;
  572. int err;
  573. int max_val, fuzz, flat;
  574. int btns[] = {BTN_X, BTN_Y, BTN_Z};
  575. if (lis3->idev)
  576. return -EINVAL;
  577. lis3->idev = input_allocate_polled_device();
  578. if (!lis3->idev)
  579. return -ENOMEM;
  580. lis3->idev->poll = lis3lv02d_joystick_poll;
  581. lis3->idev->open = lis3lv02d_joystick_open;
  582. lis3->idev->close = lis3lv02d_joystick_close;
  583. lis3->idev->poll_interval = MDPS_POLL_INTERVAL;
  584. lis3->idev->poll_interval_min = MDPS_POLL_MIN;
  585. lis3->idev->poll_interval_max = MDPS_POLL_MAX;
  586. lis3->idev->private = lis3;
  587. input_dev = lis3->idev->input;
  588. input_dev->name = "ST LIS3LV02DL Accelerometer";
  589. input_dev->phys = DRIVER_NAME "/input0";
  590. input_dev->id.bustype = BUS_HOST;
  591. input_dev->id.vendor = 0;
  592. input_dev->dev.parent = &lis3->pdev->dev;
  593. set_bit(EV_ABS, input_dev->evbit);
  594. max_val = (lis3->mdps_max_val * lis3->scale) / LIS3_ACCURACY;
  595. if (lis3->whoami == WAI_12B) {
  596. fuzz = LIS3_DEFAULT_FUZZ_12B;
  597. flat = LIS3_DEFAULT_FLAT_12B;
  598. } else {
  599. fuzz = LIS3_DEFAULT_FUZZ_8B;
  600. flat = LIS3_DEFAULT_FLAT_8B;
  601. }
  602. fuzz = (fuzz * lis3->scale) / LIS3_ACCURACY;
  603. flat = (flat * lis3->scale) / LIS3_ACCURACY;
  604. input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
  605. input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
  606. input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
  607. lis3->mapped_btns[0] = lis3lv02d_get_axis(abs(lis3->ac.x), btns);
  608. lis3->mapped_btns[1] = lis3lv02d_get_axis(abs(lis3->ac.y), btns);
  609. lis3->mapped_btns[2] = lis3lv02d_get_axis(abs(lis3->ac.z), btns);
  610. err = input_register_polled_device(lis3->idev);
  611. if (err) {
  612. input_free_polled_device(lis3->idev);
  613. lis3->idev = NULL;
  614. }
  615. return err;
  616. }
  617. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  618. void lis3lv02d_joystick_disable(struct lis3lv02d *lis3)
  619. {
  620. if (lis3->irq)
  621. free_irq(lis3->irq, lis3);
  622. if (lis3->pdata && lis3->pdata->irq2)
  623. free_irq(lis3->pdata->irq2, lis3);
  624. if (!lis3->idev)
  625. return;
  626. if (lis3->irq)
  627. misc_deregister(&lis3->miscdev);
  628. input_unregister_polled_device(lis3->idev);
  629. input_free_polled_device(lis3->idev);
  630. lis3->idev = NULL;
  631. }
  632. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
  633. /* Sysfs stuff */
  634. static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
  635. {
  636. /*
  637. * SYSFS functions are fast visitors so put-call
  638. * immediately after the get-call. However, keep
  639. * chip running for a while and schedule delayed
  640. * suspend. This way periodic sysfs calls doesn't
  641. * suffer from relatively long power up time.
  642. */
  643. if (lis3->pm_dev) {
  644. pm_runtime_get_sync(lis3->pm_dev);
  645. pm_runtime_put_noidle(lis3->pm_dev);
  646. pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
  647. }
  648. }
  649. static ssize_t lis3lv02d_selftest_show(struct device *dev,
  650. struct device_attribute *attr, char *buf)
  651. {
  652. struct lis3lv02d *lis3 = dev_get_drvdata(dev);
  653. s16 values[3];
  654. static const char ok[] = "OK";
  655. static const char fail[] = "FAIL";
  656. static const char irq[] = "FAIL_IRQ";
  657. const char *res;
  658. lis3lv02d_sysfs_poweron(lis3);
  659. switch (lis3lv02d_selftest(lis3, values)) {
  660. case SELFTEST_FAIL:
  661. res = fail;
  662. break;
  663. case SELFTEST_IRQ:
  664. res = irq;
  665. break;
  666. case SELFTEST_OK:
  667. default:
  668. res = ok;
  669. break;
  670. }
  671. return sprintf(buf, "%s %d %d %d\n", res,
  672. values[0], values[1], values[2]);
  673. }
  674. static ssize_t lis3lv02d_position_show(struct device *dev,
  675. struct device_attribute *attr, char *buf)
  676. {
  677. struct lis3lv02d *lis3 = dev_get_drvdata(dev);
  678. int x, y, z;
  679. lis3lv02d_sysfs_poweron(lis3);
  680. mutex_lock(&lis3->mutex);
  681. lis3lv02d_get_xyz(lis3, &x, &y, &z);
  682. mutex_unlock(&lis3->mutex);
  683. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  684. }
  685. static ssize_t lis3lv02d_rate_show(struct device *dev,
  686. struct device_attribute *attr, char *buf)
  687. {
  688. struct lis3lv02d *lis3 = dev_get_drvdata(dev);
  689. lis3lv02d_sysfs_poweron(lis3);
  690. return sprintf(buf, "%d\n", lis3lv02d_get_odr(lis3));
  691. }
  692. static ssize_t lis3lv02d_rate_set(struct device *dev,
  693. struct device_attribute *attr, const char *buf,
  694. size_t count)
  695. {
  696. struct lis3lv02d *lis3 = dev_get_drvdata(dev);
  697. unsigned long rate;
  698. int ret;
  699. ret = kstrtoul(buf, 0, &rate);
  700. if (ret)
  701. return ret;
  702. lis3lv02d_sysfs_poweron(lis3);
  703. if (lis3lv02d_set_odr(lis3, rate))
  704. return -EINVAL;
  705. return count;
  706. }
  707. static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
  708. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  709. static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
  710. lis3lv02d_rate_set);
  711. static struct attribute *lis3lv02d_attributes[] = {
  712. &dev_attr_selftest.attr,
  713. &dev_attr_position.attr,
  714. &dev_attr_rate.attr,
  715. NULL
  716. };
  717. static const struct attribute_group lis3lv02d_attribute_group = {
  718. .attrs = lis3lv02d_attributes
  719. };
  720. static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
  721. {
  722. lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  723. if (IS_ERR(lis3->pdev))
  724. return PTR_ERR(lis3->pdev);
  725. platform_set_drvdata(lis3->pdev, lis3);
  726. return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  727. }
  728. int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
  729. {
  730. sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  731. platform_device_unregister(lis3->pdev);
  732. if (lis3->pm_dev) {
  733. /* Barrier after the sysfs remove */
  734. pm_runtime_barrier(lis3->pm_dev);
  735. /* SYSFS may have left chip running. Turn off if necessary */
  736. if (!pm_runtime_suspended(lis3->pm_dev))
  737. lis3lv02d_poweroff(lis3);
  738. pm_runtime_disable(lis3->pm_dev);
  739. pm_runtime_set_suspended(lis3->pm_dev);
  740. }
  741. kfree(lis3->reg_cache);
  742. return 0;
  743. }
  744. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  745. static void lis3lv02d_8b_configure(struct lis3lv02d *lis3,
  746. struct lis3lv02d_platform_data *p)
  747. {
  748. int err;
  749. int ctrl2 = p->hipass_ctrl;
  750. if (p->click_flags) {
  751. lis3->write(lis3, CLICK_CFG, p->click_flags);
  752. lis3->write(lis3, CLICK_TIMELIMIT, p->click_time_limit);
  753. lis3->write(lis3, CLICK_LATENCY, p->click_latency);
  754. lis3->write(lis3, CLICK_WINDOW, p->click_window);
  755. lis3->write(lis3, CLICK_THSZ, p->click_thresh_z & 0xf);
  756. lis3->write(lis3, CLICK_THSY_X,
  757. (p->click_thresh_x & 0xf) |
  758. (p->click_thresh_y << 4));
  759. if (lis3->idev) {
  760. struct input_dev *input_dev = lis3->idev->input;
  761. input_set_capability(input_dev, EV_KEY, BTN_X);
  762. input_set_capability(input_dev, EV_KEY, BTN_Y);
  763. input_set_capability(input_dev, EV_KEY, BTN_Z);
  764. }
  765. }
  766. if (p->wakeup_flags) {
  767. lis3->write(lis3, FF_WU_CFG_1, p->wakeup_flags);
  768. lis3->write(lis3, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
  769. /* pdata value + 1 to keep this backward compatible*/
  770. lis3->write(lis3, FF_WU_DURATION_1, p->duration1 + 1);
  771. ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
  772. }
  773. if (p->wakeup_flags2) {
  774. lis3->write(lis3, FF_WU_CFG_2, p->wakeup_flags2);
  775. lis3->write(lis3, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
  776. /* pdata value + 1 to keep this backward compatible*/
  777. lis3->write(lis3, FF_WU_DURATION_2, p->duration2 + 1);
  778. ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
  779. }
  780. /* Configure hipass filters */
  781. lis3->write(lis3, CTRL_REG2, ctrl2);
  782. if (p->irq2) {
  783. err = request_threaded_irq(p->irq2,
  784. NULL,
  785. lis302dl_interrupt_thread2_8b,
  786. IRQF_TRIGGER_RISING | IRQF_ONESHOT |
  787. (p->irq_flags2 & IRQF_TRIGGER_MASK),
  788. DRIVER_NAME, lis3);
  789. if (err < 0)
  790. pr_err("No second IRQ. Limited functionality\n");
  791. }
  792. }
  793. #ifdef CONFIG_OF
  794. int lis3lv02d_init_dt(struct lis3lv02d *lis3)
  795. {
  796. struct lis3lv02d_platform_data *pdata;
  797. struct device_node *np = lis3->of_node;
  798. u32 val;
  799. s32 sval;
  800. if (!lis3->of_node)
  801. return 0;
  802. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  803. if (!pdata)
  804. return -ENOMEM;
  805. if (of_get_property(np, "st,click-single-x", NULL))
  806. pdata->click_flags |= LIS3_CLICK_SINGLE_X;
  807. if (of_get_property(np, "st,click-double-x", NULL))
  808. pdata->click_flags |= LIS3_CLICK_DOUBLE_X;
  809. if (of_get_property(np, "st,click-single-y", NULL))
  810. pdata->click_flags |= LIS3_CLICK_SINGLE_Y;
  811. if (of_get_property(np, "st,click-double-y", NULL))
  812. pdata->click_flags |= LIS3_CLICK_DOUBLE_Y;
  813. if (of_get_property(np, "st,click-single-z", NULL))
  814. pdata->click_flags |= LIS3_CLICK_SINGLE_Z;
  815. if (of_get_property(np, "st,click-double-z", NULL))
  816. pdata->click_flags |= LIS3_CLICK_DOUBLE_Z;
  817. if (!of_property_read_u32(np, "st,click-threshold-x", &val))
  818. pdata->click_thresh_x = val;
  819. if (!of_property_read_u32(np, "st,click-threshold-y", &val))
  820. pdata->click_thresh_y = val;
  821. if (!of_property_read_u32(np, "st,click-threshold-z", &val))
  822. pdata->click_thresh_z = val;
  823. if (!of_property_read_u32(np, "st,click-time-limit", &val))
  824. pdata->click_time_limit = val;
  825. if (!of_property_read_u32(np, "st,click-latency", &val))
  826. pdata->click_latency = val;
  827. if (!of_property_read_u32(np, "st,click-window", &val))
  828. pdata->click_window = val;
  829. if (of_get_property(np, "st,irq1-disable", NULL))
  830. pdata->irq_cfg |= LIS3_IRQ1_DISABLE;
  831. if (of_get_property(np, "st,irq1-ff-wu-1", NULL))
  832. pdata->irq_cfg |= LIS3_IRQ1_FF_WU_1;
  833. if (of_get_property(np, "st,irq1-ff-wu-2", NULL))
  834. pdata->irq_cfg |= LIS3_IRQ1_FF_WU_2;
  835. if (of_get_property(np, "st,irq1-data-ready", NULL))
  836. pdata->irq_cfg |= LIS3_IRQ1_DATA_READY;
  837. if (of_get_property(np, "st,irq1-click", NULL))
  838. pdata->irq_cfg |= LIS3_IRQ1_CLICK;
  839. if (of_get_property(np, "st,irq2-disable", NULL))
  840. pdata->irq_cfg |= LIS3_IRQ2_DISABLE;
  841. if (of_get_property(np, "st,irq2-ff-wu-1", NULL))
  842. pdata->irq_cfg |= LIS3_IRQ2_FF_WU_1;
  843. if (of_get_property(np, "st,irq2-ff-wu-2", NULL))
  844. pdata->irq_cfg |= LIS3_IRQ2_FF_WU_2;
  845. if (of_get_property(np, "st,irq2-data-ready", NULL))
  846. pdata->irq_cfg |= LIS3_IRQ2_DATA_READY;
  847. if (of_get_property(np, "st,irq2-click", NULL))
  848. pdata->irq_cfg |= LIS3_IRQ2_CLICK;
  849. if (of_get_property(np, "st,irq-open-drain", NULL))
  850. pdata->irq_cfg |= LIS3_IRQ_OPEN_DRAIN;
  851. if (of_get_property(np, "st,irq-active-low", NULL))
  852. pdata->irq_cfg |= LIS3_IRQ_ACTIVE_LOW;
  853. if (!of_property_read_u32(np, "st,wu-duration-1", &val))
  854. pdata->duration1 = val;
  855. if (!of_property_read_u32(np, "st,wu-duration-2", &val))
  856. pdata->duration2 = val;
  857. if (of_get_property(np, "st,wakeup-x-lo", NULL))
  858. pdata->wakeup_flags |= LIS3_WAKEUP_X_LO;
  859. if (of_get_property(np, "st,wakeup-x-hi", NULL))
  860. pdata->wakeup_flags |= LIS3_WAKEUP_X_HI;
  861. if (of_get_property(np, "st,wakeup-y-lo", NULL))
  862. pdata->wakeup_flags |= LIS3_WAKEUP_Y_LO;
  863. if (of_get_property(np, "st,wakeup-y-hi", NULL))
  864. pdata->wakeup_flags |= LIS3_WAKEUP_Y_HI;
  865. if (of_get_property(np, "st,wakeup-z-lo", NULL))
  866. pdata->wakeup_flags |= LIS3_WAKEUP_Z_LO;
  867. if (of_get_property(np, "st,wakeup-z-hi", NULL))
  868. pdata->wakeup_flags |= LIS3_WAKEUP_Z_HI;
  869. if (of_get_property(np, "st,wakeup-threshold", &val))
  870. pdata->wakeup_thresh = val;
  871. if (of_get_property(np, "st,wakeup2-x-lo", NULL))
  872. pdata->wakeup_flags2 |= LIS3_WAKEUP_X_LO;
  873. if (of_get_property(np, "st,wakeup2-x-hi", NULL))
  874. pdata->wakeup_flags2 |= LIS3_WAKEUP_X_HI;
  875. if (of_get_property(np, "st,wakeup2-y-lo", NULL))
  876. pdata->wakeup_flags2 |= LIS3_WAKEUP_Y_LO;
  877. if (of_get_property(np, "st,wakeup2-y-hi", NULL))
  878. pdata->wakeup_flags2 |= LIS3_WAKEUP_Y_HI;
  879. if (of_get_property(np, "st,wakeup2-z-lo", NULL))
  880. pdata->wakeup_flags2 |= LIS3_WAKEUP_Z_LO;
  881. if (of_get_property(np, "st,wakeup2-z-hi", NULL))
  882. pdata->wakeup_flags2 |= LIS3_WAKEUP_Z_HI;
  883. if (of_get_property(np, "st,wakeup2-threshold", &val))
  884. pdata->wakeup_thresh2 = val;
  885. if (!of_property_read_u32(np, "st,highpass-cutoff-hz", &val)) {
  886. switch (val) {
  887. case 1:
  888. pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_1HZ;
  889. break;
  890. case 2:
  891. pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_2HZ;
  892. break;
  893. case 4:
  894. pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_4HZ;
  895. break;
  896. case 8:
  897. pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_8HZ;
  898. break;
  899. }
  900. }
  901. if (of_get_property(np, "st,hipass1-disable", NULL))
  902. pdata->hipass_ctrl |= LIS3_HIPASS1_DISABLE;
  903. if (of_get_property(np, "st,hipass2-disable", NULL))
  904. pdata->hipass_ctrl |= LIS3_HIPASS2_DISABLE;
  905. if (of_property_read_s32(np, "st,axis-x", &sval) == 0)
  906. pdata->axis_x = sval;
  907. if (of_property_read_s32(np, "st,axis-y", &sval) == 0)
  908. pdata->axis_y = sval;
  909. if (of_property_read_s32(np, "st,axis-z", &sval) == 0)
  910. pdata->axis_z = sval;
  911. if (of_get_property(np, "st,default-rate", NULL))
  912. pdata->default_rate = val;
  913. if (of_property_read_s32(np, "st,min-limit-x", &sval) == 0)
  914. pdata->st_min_limits[0] = sval;
  915. if (of_property_read_s32(np, "st,min-limit-y", &sval) == 0)
  916. pdata->st_min_limits[1] = sval;
  917. if (of_property_read_s32(np, "st,min-limit-z", &sval) == 0)
  918. pdata->st_min_limits[2] = sval;
  919. if (of_property_read_s32(np, "st,max-limit-x", &sval) == 0)
  920. pdata->st_max_limits[0] = sval;
  921. if (of_property_read_s32(np, "st,max-limit-y", &sval) == 0)
  922. pdata->st_max_limits[1] = sval;
  923. if (of_property_read_s32(np, "st,max-limit-z", &sval) == 0)
  924. pdata->st_max_limits[2] = sval;
  925. lis3->pdata = pdata;
  926. return 0;
  927. }
  928. #else
  929. int lis3lv02d_init_dt(struct lis3lv02d *lis3)
  930. {
  931. return 0;
  932. }
  933. #endif
  934. EXPORT_SYMBOL_GPL(lis3lv02d_init_dt);
  935. /*
  936. * Initialise the accelerometer and the various subsystems.
  937. * Should be rather independent of the bus system.
  938. */
  939. int lis3lv02d_init_device(struct lis3lv02d *lis3)
  940. {
  941. int err;
  942. irq_handler_t thread_fn;
  943. int irq_flags = 0;
  944. lis3->whoami = lis3lv02d_read_8(lis3, WHO_AM_I);
  945. switch (lis3->whoami) {
  946. case WAI_12B:
  947. pr_info("12 bits sensor found\n");
  948. lis3->read_data = lis3lv02d_read_12;
  949. lis3->mdps_max_val = 2048;
  950. lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
  951. lis3->odrs = lis3_12_rates;
  952. lis3->odr_mask = CTRL1_DF0 | CTRL1_DF1;
  953. lis3->scale = LIS3_SENSITIVITY_12B;
  954. lis3->regs = lis3_wai12_regs;
  955. lis3->regs_size = ARRAY_SIZE(lis3_wai12_regs);
  956. break;
  957. case WAI_8B:
  958. pr_info("8 bits sensor found\n");
  959. lis3->read_data = lis3lv02d_read_8;
  960. lis3->mdps_max_val = 128;
  961. lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  962. lis3->odrs = lis3_8_rates;
  963. lis3->odr_mask = CTRL1_DR;
  964. lis3->scale = LIS3_SENSITIVITY_8B;
  965. lis3->regs = lis3_wai8_regs;
  966. lis3->regs_size = ARRAY_SIZE(lis3_wai8_regs);
  967. break;
  968. case WAI_3DC:
  969. pr_info("8 bits 3DC sensor found\n");
  970. lis3->read_data = lis3lv02d_read_8;
  971. lis3->mdps_max_val = 128;
  972. lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  973. lis3->odrs = lis3_3dc_rates;
  974. lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
  975. lis3->scale = LIS3_SENSITIVITY_8B;
  976. break;
  977. case WAI_3DLH:
  978. pr_info("16 bits lis331dlh sensor found\n");
  979. lis3->read_data = lis331dlh_read_data;
  980. lis3->mdps_max_val = 2048; /* 12 bits for 2G */
  981. lis3->shift_adj = SHIFT_ADJ_2G;
  982. lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  983. lis3->odrs = lis3_3dlh_rates;
  984. lis3->odr_mask = CTRL1_DR0 | CTRL1_DR1;
  985. lis3->scale = LIS3DLH_SENSITIVITY_2G;
  986. break;
  987. default:
  988. pr_err("unknown sensor type 0x%X\n", lis3->whoami);
  989. return -EINVAL;
  990. }
  991. lis3->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
  992. sizeof(lis3_wai12_regs)), GFP_KERNEL);
  993. if (lis3->reg_cache == NULL) {
  994. printk(KERN_ERR DRIVER_NAME "out of memory\n");
  995. return -ENOMEM;
  996. }
  997. mutex_init(&lis3->mutex);
  998. atomic_set(&lis3->wake_thread, 0);
  999. lis3lv02d_add_fs(lis3);
  1000. err = lis3lv02d_poweron(lis3);
  1001. if (err) {
  1002. lis3lv02d_remove_fs(lis3);
  1003. return err;
  1004. }
  1005. if (lis3->pm_dev) {
  1006. pm_runtime_set_active(lis3->pm_dev);
  1007. pm_runtime_enable(lis3->pm_dev);
  1008. }
  1009. if (lis3lv02d_joystick_enable(lis3))
  1010. pr_err("joystick initialization failed\n");
  1011. /* passing in platform specific data is purely optional and only
  1012. * used by the SPI transport layer at the moment */
  1013. if (lis3->pdata) {
  1014. struct lis3lv02d_platform_data *p = lis3->pdata;
  1015. if (lis3->whoami == WAI_8B)
  1016. lis3lv02d_8b_configure(lis3, p);
  1017. irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK;
  1018. lis3->irq_cfg = p->irq_cfg;
  1019. if (p->irq_cfg)
  1020. lis3->write(lis3, CTRL_REG3, p->irq_cfg);
  1021. if (p->default_rate)
  1022. lis3lv02d_set_odr(lis3, p->default_rate);
  1023. }
  1024. /* bail if we did not get an IRQ from the bus layer */
  1025. if (!lis3->irq) {
  1026. pr_debug("No IRQ. Disabling /dev/freefall\n");
  1027. goto out;
  1028. }
  1029. /*
  1030. * The sensor can generate interrupts for free-fall and direction
  1031. * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
  1032. * the things simple and _fast_ we activate it only for free-fall, so
  1033. * no need to read register (very slow with ACPI). For the same reason,
  1034. * we forbid shared interrupts.
  1035. *
  1036. * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
  1037. * io-apic is not configurable (and generates a warning) but I keep it
  1038. * in case of support for other hardware.
  1039. */
  1040. if (lis3->pdata && lis3->whoami == WAI_8B)
  1041. thread_fn = lis302dl_interrupt_thread1_8b;
  1042. else
  1043. thread_fn = NULL;
  1044. err = request_threaded_irq(lis3->irq, lis302dl_interrupt,
  1045. thread_fn,
  1046. IRQF_TRIGGER_RISING | IRQF_ONESHOT |
  1047. irq_flags,
  1048. DRIVER_NAME, lis3);
  1049. if (err < 0) {
  1050. pr_err("Cannot get IRQ\n");
  1051. goto out;
  1052. }
  1053. lis3->miscdev.minor = MISC_DYNAMIC_MINOR;
  1054. lis3->miscdev.name = "freefall";
  1055. lis3->miscdev.fops = &lis3lv02d_misc_fops;
  1056. if (misc_register(&lis3->miscdev))
  1057. pr_err("misc_register failed\n");
  1058. out:
  1059. return 0;
  1060. }
  1061. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  1062. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  1063. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  1064. MODULE_LICENSE("GPL");