lis3lv02d.c 34 KB

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