lis3lv02d.c 33 KB

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