lis3lv02d.c 33 KB

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