lis3lv02d.c 34 KB

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