lis3lv02d.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
  3. *
  4. * Copyright (C) 2007-2008 Yan Burman
  5. * Copyright (C) 2008 Eric Piel
  6. * Copyright (C) 2008-2009 Pavel Machek
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/dmi.h>
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/input-polldev.h>
  31. #include <linux/delay.h>
  32. #include <linux/wait.h>
  33. #include <linux/poll.h>
  34. #include <linux/slab.h>
  35. #include <linux/freezer.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/miscdevice.h>
  38. #include <linux/pm_runtime.h>
  39. #include <linux/atomic.h>
  40. #include <linux/of_device.h>
  41. #include "lis3lv02d.h"
  42. #define DRIVER_NAME "lis3lv02d"
  43. /* joystick device poll interval in milliseconds */
  44. #define MDPS_POLL_INTERVAL 50
  45. #define MDPS_POLL_MIN 0
  46. #define MDPS_POLL_MAX 2000
  47. #define LIS3_SYSFS_POWERDOWN_DELAY 5000 /* In milliseconds */
  48. #define SELFTEST_OK 0
  49. #define SELFTEST_FAIL -1
  50. #define SELFTEST_IRQ -2
  51. #define IRQ_LINE0 0
  52. #define IRQ_LINE1 1
  53. /*
  54. * The sensor can also generate interrupts (DRDY) but it's pretty pointless
  55. * because they are generated even if the data do not change. So it's better
  56. * to keep the interrupt for the free-fall event. The values are updated at
  57. * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
  58. * some low processor, we poll the sensor only at 20Hz... enough for the
  59. * joystick.
  60. */
  61. #define LIS3_PWRON_DELAY_WAI_12B (5000)
  62. #define LIS3_PWRON_DELAY_WAI_8B (3000)
  63. /*
  64. * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
  65. * LIS302D spec says: 18 mG / digit
  66. * LIS3_ACCURACY is used to increase accuracy of the intermediate
  67. * calculation results.
  68. */
  69. #define LIS3_ACCURACY 1024
  70. /* Sensitivity values for -2G +2G scale */
  71. #define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024)
  72. #define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY)
  73. /*
  74. * LIS331DLH spec says 1LSBs corresponds 4G/4096 -> 1LSB is 1000/1024 mG.
  75. * Below macros defines sensitivity values for +/-2G. Dataout bits for
  76. * +/-2G range is 12 bits so 4 bits adjustment must be done to get 12bit
  77. * data from 16bit value. Currently this driver supports only 2G range.
  78. */
  79. #define LIS3DLH_SENSITIVITY_2G ((LIS3_ACCURACY * 1000) / 1024)
  80. #define SHIFT_ADJ_2G 4
  81. #define LIS3_DEFAULT_FUZZ_12B 3
  82. #define LIS3_DEFAULT_FLAT_12B 3
  83. #define LIS3_DEFAULT_FUZZ_8B 1
  84. #define LIS3_DEFAULT_FLAT_8B 1
  85. struct lis3lv02d lis3_dev = {
  86. .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
  87. };
  88. EXPORT_SYMBOL_GPL(lis3_dev);
  89. /* just like param_set_int() but does sanity-check so that it won't point
  90. * over the axis array size
  91. */
  92. static int param_set_axis(const char *val, const struct kernel_param *kp)
  93. {
  94. int ret = param_set_int(val, kp);
  95. if (!ret) {
  96. int val = *(int *)kp->arg;
  97. if (val < 0)
  98. val = -val;
  99. if (!val || val > 3)
  100. return -EINVAL;
  101. }
  102. return ret;
  103. }
  104. static struct kernel_param_ops param_ops_axis = {
  105. .set = param_set_axis,
  106. .get = param_get_int,
  107. };
  108. #define param_check_axis(name, p) param_check_int(name, p)
  109. module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
  110. MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
  111. static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
  112. {
  113. s8 lo;
  114. if (lis3->read(lis3, reg, &lo) < 0)
  115. return 0;
  116. return lo;
  117. }
  118. static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
  119. {
  120. u8 lo, hi;
  121. lis3->read(lis3, reg - 1, &lo);
  122. lis3->read(lis3, reg, &hi);
  123. /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
  124. return (s16)((hi << 8) | lo);
  125. }
  126. /* 12bits for 2G range, 13 bits for 4G range and 14 bits for 8G range */
  127. static s16 lis331dlh_read_data(struct lis3lv02d *lis3, int reg)
  128. {
  129. u8 lo, hi;
  130. int v;
  131. lis3->read(lis3, reg - 1, &lo);
  132. lis3->read(lis3, reg, &hi);
  133. v = (int) ((hi << 8) | lo);
  134. return (s16) v >> lis3->shift_adj;
  135. }
  136. /**
  137. * lis3lv02d_get_axis - For the given axis, give the value converted
  138. * @axis: 1,2,3 - can also be negative
  139. * @hw_values: raw values returned by the hardware
  140. *
  141. * Returns the converted value.
  142. */
  143. static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
  144. {
  145. if (axis > 0)
  146. return hw_values[axis - 1];
  147. else
  148. return -hw_values[-axis - 1];
  149. }
  150. /**
  151. * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
  152. * @lis3: pointer to the device struct
  153. * @x: where to store the X axis value
  154. * @y: where to store the Y axis value
  155. * @z: where to store the Z axis value
  156. *
  157. * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
  158. */
  159. static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
  160. {
  161. int position[3];
  162. int i;
  163. if (lis3->blkread) {
  164. if (lis3->whoami == WAI_12B) {
  165. u16 data[3];
  166. lis3->blkread(lis3, OUTX_L, 6, (u8 *)data);
  167. for (i = 0; i < 3; i++)
  168. position[i] = (s16)le16_to_cpu(data[i]);
  169. } else {
  170. u8 data[5];
  171. /* Data: x, dummy, y, dummy, z */
  172. lis3->blkread(lis3, OUTX, 5, data);
  173. for (i = 0; i < 3; i++)
  174. position[i] = (s8)data[i * 2];
  175. }
  176. } else {
  177. position[0] = lis3->read_data(lis3, OUTX);
  178. position[1] = lis3->read_data(lis3, OUTY);
  179. position[2] = lis3->read_data(lis3, OUTZ);
  180. }
  181. for (i = 0; i < 3; i++)
  182. position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
  183. *x = lis3lv02d_get_axis(lis3->ac.x, position);
  184. *y = lis3lv02d_get_axis(lis3->ac.y, position);
  185. *z = lis3lv02d_get_axis(lis3->ac.z, position);
  186. }
  187. /* conversion btw sampling rate and the register values */
  188. static int lis3_12_rates[4] = {40, 160, 640, 2560};
  189. static int lis3_8_rates[2] = {100, 400};
  190. static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
  191. static int lis3_3dlh_rates[4] = {50, 100, 400, 1000};
  192. /* ODR is Output Data Rate */
  193. static int lis3lv02d_get_odr(struct lis3lv02d *lis3)
  194. {
  195. u8 ctrl;
  196. int shift;
  197. lis3->read(lis3, CTRL_REG1, &ctrl);
  198. ctrl &= lis3->odr_mask;
  199. shift = ffs(lis3->odr_mask) - 1;
  200. return lis3->odrs[(ctrl >> shift)];
  201. }
  202. static int lis3lv02d_get_pwron_wait(struct lis3lv02d *lis3)
  203. {
  204. int div = lis3lv02d_get_odr(lis3);
  205. if (WARN_ONCE(div == 0, "device returned spurious data"))
  206. return -ENXIO;
  207. /* LIS3 power on delay is quite long */
  208. msleep(lis3->pwron_delay / div);
  209. return 0;
  210. }
  211. static int lis3lv02d_set_odr(struct lis3lv02d *lis3, int rate)
  212. {
  213. u8 ctrl;
  214. int i, len, shift;
  215. if (!rate)
  216. return -EINVAL;
  217. lis3->read(lis3, CTRL_REG1, &ctrl);
  218. ctrl &= ~lis3->odr_mask;
  219. len = 1 << hweight_long(lis3->odr_mask); /* # of possible values */
  220. shift = ffs(lis3->odr_mask) - 1;
  221. for (i = 0; i < len; i++)
  222. if (lis3->odrs[i] == rate) {
  223. lis3->write(lis3, CTRL_REG1,
  224. ctrl | (i << shift));
  225. return 0;
  226. }
  227. return -EINVAL;
  228. }
  229. static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
  230. {
  231. u8 ctlreg, reg;
  232. s16 x, y, z;
  233. u8 selftest;
  234. int ret;
  235. u8 ctrl_reg_data;
  236. unsigned char irq_cfg;
  237. mutex_lock(&lis3->mutex);
  238. irq_cfg = lis3->irq_cfg;
  239. if (lis3->whoami == WAI_8B) {
  240. lis3->data_ready_count[IRQ_LINE0] = 0;
  241. lis3->data_ready_count[IRQ_LINE1] = 0;
  242. /* Change interrupt cfg to data ready for selftest */
  243. atomic_inc(&lis3->wake_thread);
  244. lis3->irq_cfg = LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY;
  245. lis3->read(lis3, CTRL_REG3, &ctrl_reg_data);
  246. lis3->write(lis3, CTRL_REG3, (ctrl_reg_data &
  247. ~(LIS3_IRQ1_MASK | LIS3_IRQ2_MASK)) |
  248. (LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY));
  249. }
  250. if ((lis3->whoami == WAI_3DC) || (lis3->whoami == WAI_3DLH)) {
  251. ctlreg = CTRL_REG4;
  252. selftest = CTRL4_ST0;
  253. } else {
  254. ctlreg = CTRL_REG1;
  255. if (lis3->whoami == WAI_12B)
  256. selftest = CTRL1_ST;
  257. else
  258. selftest = CTRL1_STP;
  259. }
  260. lis3->read(lis3, ctlreg, &reg);
  261. lis3->write(lis3, ctlreg, (reg | selftest));
  262. ret = lis3lv02d_get_pwron_wait(lis3);
  263. if (ret)
  264. goto fail;
  265. /* Read directly to avoid axis remap */
  266. x = lis3->read_data(lis3, OUTX);
  267. y = lis3->read_data(lis3, OUTY);
  268. z = lis3->read_data(lis3, OUTZ);
  269. /* back to normal settings */
  270. lis3->write(lis3, ctlreg, reg);
  271. ret = lis3lv02d_get_pwron_wait(lis3);
  272. if (ret)
  273. goto fail;
  274. results[0] = x - lis3->read_data(lis3, OUTX);
  275. results[1] = y - lis3->read_data(lis3, OUTY);
  276. results[2] = z - lis3->read_data(lis3, OUTZ);
  277. ret = 0;
  278. if (lis3->whoami == WAI_8B) {
  279. /* Restore original interrupt configuration */
  280. atomic_dec(&lis3->wake_thread);
  281. lis3->write(lis3, CTRL_REG3, ctrl_reg_data);
  282. lis3->irq_cfg = irq_cfg;
  283. if ((irq_cfg & LIS3_IRQ1_MASK) &&
  284. lis3->data_ready_count[IRQ_LINE0] < 2) {
  285. ret = SELFTEST_IRQ;
  286. goto fail;
  287. }
  288. if ((irq_cfg & LIS3_IRQ2_MASK) &&
  289. lis3->data_ready_count[IRQ_LINE1] < 2) {
  290. ret = SELFTEST_IRQ;
  291. goto fail;
  292. }
  293. }
  294. if (lis3->pdata) {
  295. int i;
  296. for (i = 0; i < 3; i++) {
  297. /* Check against selftest acceptance limits */
  298. if ((results[i] < lis3->pdata->st_min_limits[i]) ||
  299. (results[i] > lis3->pdata->st_max_limits[i])) {
  300. ret = SELFTEST_FAIL;
  301. goto fail;
  302. }
  303. }
  304. }
  305. /* test passed */
  306. fail:
  307. mutex_unlock(&lis3->mutex);
  308. return ret;
  309. }
  310. /*
  311. * Order of registers in the list affects to order of the restore process.
  312. * Perhaps it is a good idea to set interrupt enable register as a last one
  313. * after all other configurations
  314. */
  315. static u8 lis3_wai8_regs[] = { FF_WU_CFG_1, FF_WU_THS_1, FF_WU_DURATION_1,
  316. FF_WU_CFG_2, FF_WU_THS_2, FF_WU_DURATION_2,
  317. CLICK_CFG, CLICK_SRC, CLICK_THSY_X, CLICK_THSZ,
  318. CLICK_TIMELIMIT, CLICK_LATENCY, CLICK_WINDOW,
  319. CTRL_REG1, CTRL_REG2, CTRL_REG3};
  320. static u8 lis3_wai12_regs[] = {FF_WU_CFG, FF_WU_THS_L, FF_WU_THS_H,
  321. FF_WU_DURATION, DD_CFG, DD_THSI_L, DD_THSI_H,
  322. DD_THSE_L, DD_THSE_H,
  323. CTRL_REG1, CTRL_REG3, CTRL_REG2};
  324. static inline void lis3_context_save(struct lis3lv02d *lis3)
  325. {
  326. int i;
  327. for (i = 0; i < lis3->regs_size; i++)
  328. lis3->read(lis3, lis3->regs[i], &lis3->reg_cache[i]);
  329. lis3->regs_stored = true;
  330. }
  331. static inline void lis3_context_restore(struct lis3lv02d *lis3)
  332. {
  333. int i;
  334. if (lis3->regs_stored)
  335. for (i = 0; i < lis3->regs_size; i++)
  336. lis3->write(lis3, lis3->regs[i], lis3->reg_cache[i]);
  337. }
  338. void lis3lv02d_poweroff(struct lis3lv02d *lis3)
  339. {
  340. if (lis3->reg_ctrl)
  341. lis3_context_save(lis3);
  342. /* disable X,Y,Z axis and power down */
  343. lis3->write(lis3, CTRL_REG1, 0x00);
  344. if (lis3->reg_ctrl)
  345. lis3->reg_ctrl(lis3, LIS3_REG_OFF);
  346. }
  347. EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
  348. int lis3lv02d_poweron(struct lis3lv02d *lis3)
  349. {
  350. int err;
  351. u8 reg;
  352. lis3->init(lis3);
  353. /*
  354. * Common configuration
  355. * BDU: (12 bits sensors only) LSB and MSB values are not updated until
  356. * both have been read. So the value read will always be correct.
  357. * Set BOOT bit to refresh factory tuning values.
  358. */
  359. if (lis3->pdata) {
  360. lis3->read(lis3, CTRL_REG2, &reg);
  361. if (lis3->whoami == WAI_12B)
  362. reg |= CTRL2_BDU | CTRL2_BOOT;
  363. else if (lis3->whoami == WAI_3DLH)
  364. reg |= CTRL2_BOOT_3DLH;
  365. else
  366. reg |= CTRL2_BOOT_8B;
  367. lis3->write(lis3, CTRL_REG2, reg);
  368. if (lis3->whoami == WAI_3DLH) {
  369. lis3->read(lis3, CTRL_REG4, &reg);
  370. reg |= CTRL4_BDU;
  371. lis3->write(lis3, CTRL_REG4, reg);
  372. }
  373. }
  374. err = lis3lv02d_get_pwron_wait(lis3);
  375. if (err)
  376. return err;
  377. if (lis3->reg_ctrl)
  378. lis3_context_restore(lis3);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
  382. static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
  383. {
  384. struct lis3lv02d *lis3 = pidev->private;
  385. int x, y, z;
  386. mutex_lock(&lis3->mutex);
  387. lis3lv02d_get_xyz(lis3, &x, &y, &z);
  388. input_report_abs(pidev->input, ABS_X, x);
  389. input_report_abs(pidev->input, ABS_Y, y);
  390. input_report_abs(pidev->input, ABS_Z, z);
  391. input_sync(pidev->input);
  392. mutex_unlock(&lis3->mutex);
  393. }
  394. static void lis3lv02d_joystick_open(struct input_polled_dev *pidev)
  395. {
  396. struct lis3lv02d *lis3 = pidev->private;
  397. if (lis3->pm_dev)
  398. pm_runtime_get_sync(lis3->pm_dev);
  399. if (lis3->pdata && lis3->whoami == WAI_8B && lis3->idev)
  400. atomic_set(&lis3->wake_thread, 1);
  401. /*
  402. * Update coordinates for the case where poll interval is 0 and
  403. * the chip in running purely under interrupt control
  404. */
  405. lis3lv02d_joystick_poll(pidev);
  406. }
  407. static void lis3lv02d_joystick_close(struct input_polled_dev *pidev)
  408. {
  409. struct lis3lv02d *lis3 = pidev->private;
  410. atomic_set(&lis3->wake_thread, 0);
  411. if (lis3->pm_dev)
  412. pm_runtime_put(lis3->pm_dev);
  413. }
  414. static irqreturn_t lis302dl_interrupt(int irq, void *data)
  415. {
  416. struct lis3lv02d *lis3 = data;
  417. if (!test_bit(0, &lis3->misc_opened))
  418. goto out;
  419. /*
  420. * Be careful: on some HP laptops the bios force DD when on battery and
  421. * the lid is closed. This leads to interrupts as soon as a little move
  422. * is done.
  423. */
  424. atomic_inc(&lis3->count);
  425. wake_up_interruptible(&lis3->misc_wait);
  426. kill_fasync(&lis3->async_queue, SIGIO, POLL_IN);
  427. out:
  428. if (atomic_read(&lis3->wake_thread))
  429. return IRQ_WAKE_THREAD;
  430. return IRQ_HANDLED;
  431. }
  432. static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
  433. {
  434. struct input_dev *dev = lis3->idev->input;
  435. u8 click_src;
  436. mutex_lock(&lis3->mutex);
  437. lis3->read(lis3, CLICK_SRC, &click_src);
  438. if (click_src & CLICK_SINGLE_X) {
  439. input_report_key(dev, lis3->mapped_btns[0], 1);
  440. input_report_key(dev, lis3->mapped_btns[0], 0);
  441. }
  442. if (click_src & CLICK_SINGLE_Y) {
  443. input_report_key(dev, lis3->mapped_btns[1], 1);
  444. input_report_key(dev, lis3->mapped_btns[1], 0);
  445. }
  446. if (click_src & CLICK_SINGLE_Z) {
  447. input_report_key(dev, lis3->mapped_btns[2], 1);
  448. input_report_key(dev, lis3->mapped_btns[2], 0);
  449. }
  450. input_sync(dev);
  451. mutex_unlock(&lis3->mutex);
  452. }
  453. static inline void lis302dl_data_ready(struct lis3lv02d *lis3, int index)
  454. {
  455. int dummy;
  456. /* Dummy read to ack interrupt */
  457. lis3lv02d_get_xyz(lis3, &dummy, &dummy, &dummy);
  458. lis3->data_ready_count[index]++;
  459. }
  460. static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
  461. {
  462. struct lis3lv02d *lis3 = data;
  463. u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ1_MASK;
  464. if (irq_cfg == LIS3_IRQ1_CLICK)
  465. lis302dl_interrupt_handle_click(lis3);
  466. else if (unlikely(irq_cfg == LIS3_IRQ1_DATA_READY))
  467. lis302dl_data_ready(lis3, IRQ_LINE0);
  468. else
  469. lis3lv02d_joystick_poll(lis3->idev);
  470. return IRQ_HANDLED;
  471. }
  472. static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
  473. {
  474. struct lis3lv02d *lis3 = data;
  475. u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ2_MASK;
  476. if (irq_cfg == LIS3_IRQ2_CLICK)
  477. lis302dl_interrupt_handle_click(lis3);
  478. else if (unlikely(irq_cfg == LIS3_IRQ2_DATA_READY))
  479. lis302dl_data_ready(lis3, IRQ_LINE1);
  480. else
  481. lis3lv02d_joystick_poll(lis3->idev);
  482. return IRQ_HANDLED;
  483. }
  484. static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
  485. {
  486. struct lis3lv02d *lis3 = container_of(file->private_data,
  487. struct lis3lv02d, miscdev);
  488. if (test_and_set_bit(0, &lis3->misc_opened))
  489. return -EBUSY; /* already open */
  490. if (lis3->pm_dev)
  491. pm_runtime_get_sync(lis3->pm_dev);
  492. atomic_set(&lis3->count, 0);
  493. return 0;
  494. }
  495. static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
  496. {
  497. struct lis3lv02d *lis3 = container_of(file->private_data,
  498. struct lis3lv02d, miscdev);
  499. 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 unsigned int 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 POLLIN | POLLRDNORM;
  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 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. if (!lis3->of_node)
  800. return 0;
  801. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  802. if (!pdata)
  803. return -ENOMEM;
  804. if (of_get_property(np, "st,click-single-x", NULL))
  805. pdata->click_flags |= LIS3_CLICK_SINGLE_X;
  806. if (of_get_property(np, "st,click-double-x", NULL))
  807. pdata->click_flags |= LIS3_CLICK_DOUBLE_X;
  808. if (of_get_property(np, "st,click-single-y", NULL))
  809. pdata->click_flags |= LIS3_CLICK_SINGLE_Y;
  810. if (of_get_property(np, "st,click-double-y", NULL))
  811. pdata->click_flags |= LIS3_CLICK_DOUBLE_Y;
  812. if (of_get_property(np, "st,click-single-z", NULL))
  813. pdata->click_flags |= LIS3_CLICK_SINGLE_Z;
  814. if (of_get_property(np, "st,click-double-z", NULL))
  815. pdata->click_flags |= LIS3_CLICK_DOUBLE_Z;
  816. if (!of_property_read_u32(np, "st,click-threshold-x", &val))
  817. pdata->click_thresh_x = val;
  818. if (!of_property_read_u32(np, "st,click-threshold-y", &val))
  819. pdata->click_thresh_y = val;
  820. if (!of_property_read_u32(np, "st,click-threshold-z", &val))
  821. pdata->click_thresh_z = val;
  822. if (!of_property_read_u32(np, "st,click-time-limit", &val))
  823. pdata->click_time_limit = val;
  824. if (!of_property_read_u32(np, "st,click-latency", &val))
  825. pdata->click_latency = val;
  826. if (!of_property_read_u32(np, "st,click-window", &val))
  827. pdata->click_window = val;
  828. if (of_get_property(np, "st,irq1-disable", NULL))
  829. pdata->irq_cfg |= LIS3_IRQ1_DISABLE;
  830. if (of_get_property(np, "st,irq1-ff-wu-1", NULL))
  831. pdata->irq_cfg |= LIS3_IRQ1_FF_WU_1;
  832. if (of_get_property(np, "st,irq1-ff-wu-2", NULL))
  833. pdata->irq_cfg |= LIS3_IRQ1_FF_WU_2;
  834. if (of_get_property(np, "st,irq1-data-ready", NULL))
  835. pdata->irq_cfg |= LIS3_IRQ1_DATA_READY;
  836. if (of_get_property(np, "st,irq1-click", NULL))
  837. pdata->irq_cfg |= LIS3_IRQ1_CLICK;
  838. if (of_get_property(np, "st,irq2-disable", NULL))
  839. pdata->irq_cfg |= LIS3_IRQ2_DISABLE;
  840. if (of_get_property(np, "st,irq2-ff-wu-1", NULL))
  841. pdata->irq_cfg |= LIS3_IRQ2_FF_WU_1;
  842. if (of_get_property(np, "st,irq2-ff-wu-2", NULL))
  843. pdata->irq_cfg |= LIS3_IRQ2_FF_WU_2;
  844. if (of_get_property(np, "st,irq2-data-ready", NULL))
  845. pdata->irq_cfg |= LIS3_IRQ2_DATA_READY;
  846. if (of_get_property(np, "st,irq2-click", NULL))
  847. pdata->irq_cfg |= LIS3_IRQ2_CLICK;
  848. if (of_get_property(np, "st,irq-open-drain", NULL))
  849. pdata->irq_cfg |= LIS3_IRQ_OPEN_DRAIN;
  850. if (of_get_property(np, "st,irq-active-low", NULL))
  851. pdata->irq_cfg |= LIS3_IRQ_ACTIVE_LOW;
  852. if (!of_property_read_u32(np, "st,wu-duration-1", &val))
  853. pdata->duration1 = val;
  854. if (!of_property_read_u32(np, "st,wu-duration-2", &val))
  855. pdata->duration2 = val;
  856. if (of_get_property(np, "st,wakeup-x-lo", NULL))
  857. pdata->wakeup_flags |= LIS3_WAKEUP_X_LO;
  858. if (of_get_property(np, "st,wakeup-x-hi", NULL))
  859. pdata->wakeup_flags |= LIS3_WAKEUP_X_HI;
  860. if (of_get_property(np, "st,wakeup-y-lo", NULL))
  861. pdata->wakeup_flags |= LIS3_WAKEUP_Y_LO;
  862. if (of_get_property(np, "st,wakeup-y-hi", NULL))
  863. pdata->wakeup_flags |= LIS3_WAKEUP_Y_HI;
  864. if (of_get_property(np, "st,wakeup-z-lo", NULL))
  865. pdata->wakeup_flags |= LIS3_WAKEUP_Z_LO;
  866. if (of_get_property(np, "st,wakeup-z-hi", NULL))
  867. pdata->wakeup_flags |= LIS3_WAKEUP_Z_HI;
  868. if (!of_property_read_u32(np, "st,highpass-cutoff-hz", &val)) {
  869. switch (val) {
  870. case 1:
  871. pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_1HZ;
  872. break;
  873. case 2:
  874. pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_2HZ;
  875. break;
  876. case 4:
  877. pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_4HZ;
  878. break;
  879. case 8:
  880. pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_8HZ;
  881. break;
  882. }
  883. }
  884. if (of_get_property(np, "st,hipass1-disable", NULL))
  885. pdata->hipass_ctrl |= LIS3_HIPASS1_DISABLE;
  886. if (of_get_property(np, "st,hipass2-disable", NULL))
  887. pdata->hipass_ctrl |= LIS3_HIPASS2_DISABLE;
  888. if (of_get_property(np, "st,axis-x", &val))
  889. pdata->axis_x = val;
  890. if (of_get_property(np, "st,axis-y", &val))
  891. pdata->axis_y = val;
  892. if (of_get_property(np, "st,axis-z", &val))
  893. pdata->axis_z = val;
  894. if (of_get_property(np, "st,default-rate", NULL))
  895. pdata->default_rate = val;
  896. if (of_get_property(np, "st,min-limit-x", &val))
  897. pdata->st_min_limits[0] = val;
  898. if (of_get_property(np, "st,min-limit-y", &val))
  899. pdata->st_min_limits[1] = val;
  900. if (of_get_property(np, "st,min-limit-z", &val))
  901. pdata->st_min_limits[2] = val;
  902. if (of_get_property(np, "st,max-limit-x", &val))
  903. pdata->st_max_limits[0] = val;
  904. if (of_get_property(np, "st,max-limit-y", &val))
  905. pdata->st_max_limits[1] = val;
  906. if (of_get_property(np, "st,max-limit-z", &val))
  907. pdata->st_max_limits[2] = val;
  908. lis3->pdata = pdata;
  909. return 0;
  910. }
  911. #else
  912. int lis3lv02d_init_dt(struct lis3lv02d *lis3)
  913. {
  914. return 0;
  915. }
  916. #endif
  917. EXPORT_SYMBOL_GPL(lis3lv02d_init_dt);
  918. /*
  919. * Initialise the accelerometer and the various subsystems.
  920. * Should be rather independent of the bus system.
  921. */
  922. int lis3lv02d_init_device(struct lis3lv02d *lis3)
  923. {
  924. int err;
  925. irq_handler_t thread_fn;
  926. int irq_flags = 0;
  927. lis3->whoami = lis3lv02d_read_8(lis3, WHO_AM_I);
  928. switch (lis3->whoami) {
  929. case WAI_12B:
  930. pr_info("12 bits sensor found\n");
  931. lis3->read_data = lis3lv02d_read_12;
  932. lis3->mdps_max_val = 2048;
  933. lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
  934. lis3->odrs = lis3_12_rates;
  935. lis3->odr_mask = CTRL1_DF0 | CTRL1_DF1;
  936. lis3->scale = LIS3_SENSITIVITY_12B;
  937. lis3->regs = lis3_wai12_regs;
  938. lis3->regs_size = ARRAY_SIZE(lis3_wai12_regs);
  939. break;
  940. case WAI_8B:
  941. pr_info("8 bits sensor found\n");
  942. lis3->read_data = lis3lv02d_read_8;
  943. lis3->mdps_max_val = 128;
  944. lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  945. lis3->odrs = lis3_8_rates;
  946. lis3->odr_mask = CTRL1_DR;
  947. lis3->scale = LIS3_SENSITIVITY_8B;
  948. lis3->regs = lis3_wai8_regs;
  949. lis3->regs_size = ARRAY_SIZE(lis3_wai8_regs);
  950. break;
  951. case WAI_3DC:
  952. pr_info("8 bits 3DC sensor found\n");
  953. lis3->read_data = lis3lv02d_read_8;
  954. lis3->mdps_max_val = 128;
  955. lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  956. lis3->odrs = lis3_3dc_rates;
  957. lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
  958. lis3->scale = LIS3_SENSITIVITY_8B;
  959. break;
  960. case WAI_3DLH:
  961. pr_info("16 bits lis331dlh sensor found\n");
  962. lis3->read_data = lis331dlh_read_data;
  963. lis3->mdps_max_val = 2048; /* 12 bits for 2G */
  964. lis3->shift_adj = SHIFT_ADJ_2G;
  965. lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  966. lis3->odrs = lis3_3dlh_rates;
  967. lis3->odr_mask = CTRL1_DR0 | CTRL1_DR1;
  968. lis3->scale = LIS3DLH_SENSITIVITY_2G;
  969. break;
  970. default:
  971. pr_err("unknown sensor type 0x%X\n", lis3->whoami);
  972. return -EINVAL;
  973. }
  974. lis3->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
  975. sizeof(lis3_wai12_regs)), GFP_KERNEL);
  976. if (lis3->reg_cache == NULL) {
  977. printk(KERN_ERR DRIVER_NAME "out of memory\n");
  978. return -ENOMEM;
  979. }
  980. mutex_init(&lis3->mutex);
  981. atomic_set(&lis3->wake_thread, 0);
  982. lis3lv02d_add_fs(lis3);
  983. err = lis3lv02d_poweron(lis3);
  984. if (err) {
  985. lis3lv02d_remove_fs(lis3);
  986. return err;
  987. }
  988. if (lis3->pm_dev) {
  989. pm_runtime_set_active(lis3->pm_dev);
  990. pm_runtime_enable(lis3->pm_dev);
  991. }
  992. if (lis3lv02d_joystick_enable(lis3))
  993. pr_err("joystick initialization failed\n");
  994. /* passing in platform specific data is purely optional and only
  995. * used by the SPI transport layer at the moment */
  996. if (lis3->pdata) {
  997. struct lis3lv02d_platform_data *p = lis3->pdata;
  998. if (lis3->whoami == WAI_8B)
  999. lis3lv02d_8b_configure(lis3, p);
  1000. irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK;
  1001. lis3->irq_cfg = p->irq_cfg;
  1002. if (p->irq_cfg)
  1003. lis3->write(lis3, CTRL_REG3, p->irq_cfg);
  1004. if (p->default_rate)
  1005. lis3lv02d_set_odr(lis3, p->default_rate);
  1006. }
  1007. /* bail if we did not get an IRQ from the bus layer */
  1008. if (!lis3->irq) {
  1009. pr_debug("No IRQ. Disabling /dev/freefall\n");
  1010. goto out;
  1011. }
  1012. /*
  1013. * The sensor can generate interrupts for free-fall and direction
  1014. * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
  1015. * the things simple and _fast_ we activate it only for free-fall, so
  1016. * no need to read register (very slow with ACPI). For the same reason,
  1017. * we forbid shared interrupts.
  1018. *
  1019. * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
  1020. * io-apic is not configurable (and generates a warning) but I keep it
  1021. * in case of support for other hardware.
  1022. */
  1023. if (lis3->pdata && lis3->whoami == WAI_8B)
  1024. thread_fn = lis302dl_interrupt_thread1_8b;
  1025. else
  1026. thread_fn = NULL;
  1027. err = request_threaded_irq(lis3->irq, lis302dl_interrupt,
  1028. thread_fn,
  1029. IRQF_TRIGGER_RISING | IRQF_ONESHOT |
  1030. irq_flags,
  1031. DRIVER_NAME, lis3);
  1032. if (err < 0) {
  1033. pr_err("Cannot get IRQ\n");
  1034. goto out;
  1035. }
  1036. lis3->miscdev.minor = MISC_DYNAMIC_MINOR;
  1037. lis3->miscdev.name = "freefall";
  1038. lis3->miscdev.fops = &lis3lv02d_misc_fops;
  1039. if (misc_register(&lis3->miscdev))
  1040. pr_err("misc_register failed\n");
  1041. out:
  1042. return 0;
  1043. }
  1044. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  1045. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  1046. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  1047. MODULE_LICENSE("GPL");