lis3lv02d.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. #include <linux/kernel.h>
  23. #include <linux/init.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/freezer.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/miscdevice.h>
  36. #include <asm/atomic.h>
  37. #include "lis3lv02d.h"
  38. #define DRIVER_NAME "lis3lv02d"
  39. /* joystick device poll interval in milliseconds */
  40. #define MDPS_POLL_INTERVAL 50
  41. /*
  42. * The sensor can also generate interrupts (DRDY) but it's pretty pointless
  43. * because they are generated even if the data do not change. So it's better
  44. * to keep the interrupt for the free-fall event. The values are updated at
  45. * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
  46. * some low processor, we poll the sensor only at 20Hz... enough for the
  47. * joystick.
  48. */
  49. #define LIS3_PWRON_DELAY_WAI_12B (5000)
  50. #define LIS3_PWRON_DELAY_WAI_8B (3000)
  51. /*
  52. * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
  53. * LIS302D spec says: 18 mG / digit
  54. * LIS3_ACCURACY is used to increase accuracy of the intermediate
  55. * calculation results.
  56. */
  57. #define LIS3_ACCURACY 1024
  58. /* Sensitivity values for -2G +2G scale */
  59. #define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024)
  60. #define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY)
  61. #define LIS3_DEFAULT_FUZZ 3
  62. #define LIS3_DEFAULT_FLAT 3
  63. struct lis3lv02d lis3_dev = {
  64. .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
  65. };
  66. EXPORT_SYMBOL_GPL(lis3_dev);
  67. static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
  68. {
  69. s8 lo;
  70. if (lis3->read(lis3, reg, &lo) < 0)
  71. return 0;
  72. return lo;
  73. }
  74. static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
  75. {
  76. u8 lo, hi;
  77. lis3->read(lis3, reg - 1, &lo);
  78. lis3->read(lis3, reg, &hi);
  79. /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
  80. return (s16)((hi << 8) | lo);
  81. }
  82. /**
  83. * lis3lv02d_get_axis - For the given axis, give the value converted
  84. * @axis: 1,2,3 - can also be negative
  85. * @hw_values: raw values returned by the hardware
  86. *
  87. * Returns the converted value.
  88. */
  89. static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
  90. {
  91. if (axis > 0)
  92. return hw_values[axis - 1];
  93. else
  94. return -hw_values[-axis - 1];
  95. }
  96. /**
  97. * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
  98. * @lis3: pointer to the device struct
  99. * @x: where to store the X axis value
  100. * @y: where to store the Y axis value
  101. * @z: where to store the Z axis value
  102. *
  103. * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
  104. */
  105. static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
  106. {
  107. int position[3];
  108. int i;
  109. position[0] = lis3->read_data(lis3, OUTX);
  110. position[1] = lis3->read_data(lis3, OUTY);
  111. position[2] = lis3->read_data(lis3, OUTZ);
  112. for (i = 0; i < 3; i++)
  113. position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
  114. *x = lis3lv02d_get_axis(lis3->ac.x, position);
  115. *y = lis3lv02d_get_axis(lis3->ac.y, position);
  116. *z = lis3lv02d_get_axis(lis3->ac.z, position);
  117. }
  118. /* conversion btw sampling rate and the register values */
  119. static int lis3_12_rates[4] = {40, 160, 640, 2560};
  120. static int lis3_8_rates[2] = {100, 400};
  121. /* ODR is Output Data Rate */
  122. static int lis3lv02d_get_odr(void)
  123. {
  124. u8 ctrl;
  125. int shift;
  126. lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
  127. ctrl &= lis3_dev.odr_mask;
  128. shift = ffs(lis3_dev.odr_mask) - 1;
  129. return lis3_dev.odrs[(ctrl >> shift)];
  130. }
  131. static int lis3lv02d_set_odr(int rate)
  132. {
  133. u8 ctrl;
  134. int i, len, shift;
  135. lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
  136. ctrl &= ~lis3_dev.odr_mask;
  137. len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
  138. shift = ffs(lis3_dev.odr_mask) - 1;
  139. for (i = 0; i < len; i++)
  140. if (lis3_dev.odrs[i] == rate) {
  141. lis3_dev.write(&lis3_dev, CTRL_REG1,
  142. ctrl | (i << shift));
  143. return 0;
  144. }
  145. return -EINVAL;
  146. }
  147. static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
  148. {
  149. u8 reg;
  150. s16 x, y, z;
  151. u8 selftest;
  152. int ret;
  153. mutex_lock(&lis3->mutex);
  154. if (lis3_dev.whoami == WAI_12B)
  155. selftest = CTRL1_ST;
  156. else
  157. selftest = CTRL1_STP;
  158. lis3->read(lis3, CTRL_REG1, &reg);
  159. lis3->write(lis3, CTRL_REG1, (reg | selftest));
  160. msleep(lis3->pwron_delay / lis3lv02d_get_odr());
  161. /* Read directly to avoid axis remap */
  162. x = lis3->read_data(lis3, OUTX);
  163. y = lis3->read_data(lis3, OUTY);
  164. z = lis3->read_data(lis3, OUTZ);
  165. /* back to normal settings */
  166. lis3->write(lis3, CTRL_REG1, reg);
  167. msleep(lis3->pwron_delay / lis3lv02d_get_odr());
  168. results[0] = x - lis3->read_data(lis3, OUTX);
  169. results[1] = y - lis3->read_data(lis3, OUTY);
  170. results[2] = z - lis3->read_data(lis3, OUTZ);
  171. ret = 0;
  172. if (lis3->pdata) {
  173. int i;
  174. for (i = 0; i < 3; i++) {
  175. /* Check against selftest acceptance limits */
  176. if ((results[i] < lis3->pdata->st_min_limits[i]) ||
  177. (results[i] > lis3->pdata->st_max_limits[i])) {
  178. ret = -EIO;
  179. goto fail;
  180. }
  181. }
  182. }
  183. /* test passed */
  184. fail:
  185. mutex_unlock(&lis3->mutex);
  186. return ret;
  187. }
  188. void lis3lv02d_poweroff(struct lis3lv02d *lis3)
  189. {
  190. /* disable X,Y,Z axis and power down */
  191. lis3->write(lis3, CTRL_REG1, 0x00);
  192. }
  193. EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
  194. void lis3lv02d_poweron(struct lis3lv02d *lis3)
  195. {
  196. u8 reg;
  197. lis3->init(lis3);
  198. /* LIS3 power on delay is quite long */
  199. msleep(lis3->pwron_delay / lis3lv02d_get_odr());
  200. /*
  201. * Common configuration
  202. * BDU: (12 bits sensors only) LSB and MSB values are not updated until
  203. * both have been read. So the value read will always be correct.
  204. */
  205. if (lis3->whoami == WAI_12B) {
  206. lis3->read(lis3, CTRL_REG2, &reg);
  207. reg |= CTRL2_BDU;
  208. lis3->write(lis3, CTRL_REG2, reg);
  209. }
  210. }
  211. EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
  212. static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
  213. {
  214. int x, y, z;
  215. mutex_lock(&lis3_dev.mutex);
  216. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  217. input_report_abs(pidev->input, ABS_X, x);
  218. input_report_abs(pidev->input, ABS_Y, y);
  219. input_report_abs(pidev->input, ABS_Z, z);
  220. input_sync(pidev->input);
  221. mutex_unlock(&lis3_dev.mutex);
  222. }
  223. static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
  224. {
  225. if (!test_bit(0, &lis3_dev.misc_opened))
  226. goto out;
  227. /*
  228. * Be careful: on some HP laptops the bios force DD when on battery and
  229. * the lid is closed. This leads to interrupts as soon as a little move
  230. * is done.
  231. */
  232. atomic_inc(&lis3_dev.count);
  233. wake_up_interruptible(&lis3_dev.misc_wait);
  234. kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
  235. out:
  236. if (lis3_dev.whoami == WAI_8B && lis3_dev.idev &&
  237. lis3_dev.idev->input->users)
  238. return IRQ_WAKE_THREAD;
  239. return IRQ_HANDLED;
  240. }
  241. static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
  242. {
  243. struct input_dev *dev = lis3->idev->input;
  244. u8 click_src;
  245. mutex_lock(&lis3->mutex);
  246. lis3->read(lis3, CLICK_SRC, &click_src);
  247. if (click_src & CLICK_SINGLE_X) {
  248. input_report_key(dev, lis3->mapped_btns[0], 1);
  249. input_report_key(dev, lis3->mapped_btns[0], 0);
  250. }
  251. if (click_src & CLICK_SINGLE_Y) {
  252. input_report_key(dev, lis3->mapped_btns[1], 1);
  253. input_report_key(dev, lis3->mapped_btns[1], 0);
  254. }
  255. if (click_src & CLICK_SINGLE_Z) {
  256. input_report_key(dev, lis3->mapped_btns[2], 1);
  257. input_report_key(dev, lis3->mapped_btns[2], 0);
  258. }
  259. input_sync(dev);
  260. mutex_unlock(&lis3->mutex);
  261. }
  262. static void lis302dl_interrupt_handle_ff_wu(struct lis3lv02d *lis3)
  263. {
  264. u8 wu1_src;
  265. u8 wu2_src;
  266. lis3->read(lis3, FF_WU_SRC_1, &wu1_src);
  267. lis3->read(lis3, FF_WU_SRC_2, &wu2_src);
  268. wu1_src = wu1_src & FF_WU_SRC_IA ? wu1_src : 0;
  269. wu2_src = wu2_src & FF_WU_SRC_IA ? wu2_src : 0;
  270. /* joystick poll is internally protected by the lis3->mutex. */
  271. if (wu1_src || wu2_src)
  272. lis3lv02d_joystick_poll(lis3_dev.idev);
  273. }
  274. static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
  275. {
  276. struct lis3lv02d *lis3 = data;
  277. if ((lis3->pdata->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
  278. lis302dl_interrupt_handle_click(lis3);
  279. else
  280. lis302dl_interrupt_handle_ff_wu(lis3);
  281. return IRQ_HANDLED;
  282. }
  283. static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
  284. {
  285. struct lis3lv02d *lis3 = data;
  286. if ((lis3->pdata->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
  287. lis302dl_interrupt_handle_click(lis3);
  288. else
  289. lis302dl_interrupt_handle_ff_wu(lis3);
  290. return IRQ_HANDLED;
  291. }
  292. static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
  293. {
  294. if (test_and_set_bit(0, &lis3_dev.misc_opened))
  295. return -EBUSY; /* already open */
  296. atomic_set(&lis3_dev.count, 0);
  297. return 0;
  298. }
  299. static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
  300. {
  301. fasync_helper(-1, file, 0, &lis3_dev.async_queue);
  302. clear_bit(0, &lis3_dev.misc_opened); /* release the device */
  303. return 0;
  304. }
  305. static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
  306. size_t count, loff_t *pos)
  307. {
  308. DECLARE_WAITQUEUE(wait, current);
  309. u32 data;
  310. unsigned char byte_data;
  311. ssize_t retval = 1;
  312. if (count < 1)
  313. return -EINVAL;
  314. add_wait_queue(&lis3_dev.misc_wait, &wait);
  315. while (true) {
  316. set_current_state(TASK_INTERRUPTIBLE);
  317. data = atomic_xchg(&lis3_dev.count, 0);
  318. if (data)
  319. break;
  320. if (file->f_flags & O_NONBLOCK) {
  321. retval = -EAGAIN;
  322. goto out;
  323. }
  324. if (signal_pending(current)) {
  325. retval = -ERESTARTSYS;
  326. goto out;
  327. }
  328. schedule();
  329. }
  330. if (data < 255)
  331. byte_data = data;
  332. else
  333. byte_data = 255;
  334. /* make sure we are not going into copy_to_user() with
  335. * TASK_INTERRUPTIBLE state */
  336. set_current_state(TASK_RUNNING);
  337. if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
  338. retval = -EFAULT;
  339. out:
  340. __set_current_state(TASK_RUNNING);
  341. remove_wait_queue(&lis3_dev.misc_wait, &wait);
  342. return retval;
  343. }
  344. static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
  345. {
  346. poll_wait(file, &lis3_dev.misc_wait, wait);
  347. if (atomic_read(&lis3_dev.count))
  348. return POLLIN | POLLRDNORM;
  349. return 0;
  350. }
  351. static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
  352. {
  353. return fasync_helper(fd, file, on, &lis3_dev.async_queue);
  354. }
  355. static const struct file_operations lis3lv02d_misc_fops = {
  356. .owner = THIS_MODULE,
  357. .llseek = no_llseek,
  358. .read = lis3lv02d_misc_read,
  359. .open = lis3lv02d_misc_open,
  360. .release = lis3lv02d_misc_release,
  361. .poll = lis3lv02d_misc_poll,
  362. .fasync = lis3lv02d_misc_fasync,
  363. };
  364. static struct miscdevice lis3lv02d_misc_device = {
  365. .minor = MISC_DYNAMIC_MINOR,
  366. .name = "freefall",
  367. .fops = &lis3lv02d_misc_fops,
  368. };
  369. int lis3lv02d_joystick_enable(void)
  370. {
  371. struct input_dev *input_dev;
  372. int err;
  373. int max_val, fuzz, flat;
  374. int btns[] = {BTN_X, BTN_Y, BTN_Z};
  375. if (lis3_dev.idev)
  376. return -EINVAL;
  377. lis3_dev.idev = input_allocate_polled_device();
  378. if (!lis3_dev.idev)
  379. return -ENOMEM;
  380. lis3_dev.idev->poll = lis3lv02d_joystick_poll;
  381. lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
  382. input_dev = lis3_dev.idev->input;
  383. input_dev->name = "ST LIS3LV02DL Accelerometer";
  384. input_dev->phys = DRIVER_NAME "/input0";
  385. input_dev->id.bustype = BUS_HOST;
  386. input_dev->id.vendor = 0;
  387. input_dev->dev.parent = &lis3_dev.pdev->dev;
  388. set_bit(EV_ABS, input_dev->evbit);
  389. max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
  390. fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
  391. flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
  392. input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
  393. input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
  394. input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
  395. lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
  396. lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
  397. lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
  398. err = input_register_polled_device(lis3_dev.idev);
  399. if (err) {
  400. input_free_polled_device(lis3_dev.idev);
  401. lis3_dev.idev = NULL;
  402. }
  403. return err;
  404. }
  405. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  406. void lis3lv02d_joystick_disable(void)
  407. {
  408. if (lis3_dev.irq)
  409. free_irq(lis3_dev.irq, &lis3_dev);
  410. if (lis3_dev.pdata && lis3_dev.pdata->irq2)
  411. free_irq(lis3_dev.pdata->irq2, &lis3_dev);
  412. if (!lis3_dev.idev)
  413. return;
  414. if (lis3_dev.irq)
  415. misc_deregister(&lis3lv02d_misc_device);
  416. input_unregister_polled_device(lis3_dev.idev);
  417. input_free_polled_device(lis3_dev.idev);
  418. lis3_dev.idev = NULL;
  419. }
  420. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
  421. /* Sysfs stuff */
  422. static ssize_t lis3lv02d_selftest_show(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. int result;
  426. s16 values[3];
  427. result = lis3lv02d_selftest(&lis3_dev, values);
  428. return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
  429. values[0], values[1], values[2]);
  430. }
  431. static ssize_t lis3lv02d_position_show(struct device *dev,
  432. struct device_attribute *attr, char *buf)
  433. {
  434. int x, y, z;
  435. mutex_lock(&lis3_dev.mutex);
  436. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  437. mutex_unlock(&lis3_dev.mutex);
  438. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  439. }
  440. static ssize_t lis3lv02d_rate_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. return sprintf(buf, "%d\n", lis3lv02d_get_odr());
  444. }
  445. static ssize_t lis3lv02d_rate_set(struct device *dev,
  446. struct device_attribute *attr, const char *buf,
  447. size_t count)
  448. {
  449. unsigned long rate;
  450. if (strict_strtoul(buf, 0, &rate))
  451. return -EINVAL;
  452. if (lis3lv02d_set_odr(rate))
  453. return -EINVAL;
  454. return count;
  455. }
  456. static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
  457. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  458. static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
  459. lis3lv02d_rate_set);
  460. static struct attribute *lis3lv02d_attributes[] = {
  461. &dev_attr_selftest.attr,
  462. &dev_attr_position.attr,
  463. &dev_attr_rate.attr,
  464. NULL
  465. };
  466. static struct attribute_group lis3lv02d_attribute_group = {
  467. .attrs = lis3lv02d_attributes
  468. };
  469. static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
  470. {
  471. lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  472. if (IS_ERR(lis3->pdev))
  473. return PTR_ERR(lis3->pdev);
  474. return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  475. }
  476. int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
  477. {
  478. sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  479. platform_device_unregister(lis3->pdev);
  480. return 0;
  481. }
  482. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  483. static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
  484. struct lis3lv02d_platform_data *p)
  485. {
  486. int err;
  487. int ctrl2 = p->hipass_ctrl;
  488. if (p->click_flags) {
  489. dev->write(dev, CLICK_CFG, p->click_flags);
  490. dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
  491. dev->write(dev, CLICK_LATENCY, p->click_latency);
  492. dev->write(dev, CLICK_WINDOW, p->click_window);
  493. dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
  494. dev->write(dev, CLICK_THSY_X,
  495. (p->click_thresh_x & 0xf) |
  496. (p->click_thresh_y << 4));
  497. if (dev->idev) {
  498. struct input_dev *input_dev = lis3_dev.idev->input;
  499. input_set_capability(input_dev, EV_KEY, BTN_X);
  500. input_set_capability(input_dev, EV_KEY, BTN_Y);
  501. input_set_capability(input_dev, EV_KEY, BTN_Z);
  502. }
  503. }
  504. if (p->wakeup_flags) {
  505. dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
  506. dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
  507. /* default to 2.5ms for now */
  508. dev->write(dev, FF_WU_DURATION_1, 1);
  509. ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
  510. }
  511. if (p->wakeup_flags2) {
  512. dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
  513. dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
  514. /* default to 2.5ms for now */
  515. dev->write(dev, FF_WU_DURATION_2, 1);
  516. ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
  517. }
  518. /* Configure hipass filters */
  519. dev->write(dev, CTRL_REG2, ctrl2);
  520. if (p->irq2) {
  521. err = request_threaded_irq(p->irq2,
  522. NULL,
  523. lis302dl_interrupt_thread2_8b,
  524. IRQF_TRIGGER_RISING |
  525. IRQF_ONESHOT,
  526. DRIVER_NAME, &lis3_dev);
  527. if (err < 0)
  528. printk(KERN_ERR DRIVER_NAME
  529. "No second IRQ. Limited functionality\n");
  530. }
  531. }
  532. /*
  533. * Initialise the accelerometer and the various subsystems.
  534. * Should be rather independent of the bus system.
  535. */
  536. int lis3lv02d_init_device(struct lis3lv02d *dev)
  537. {
  538. int err;
  539. irq_handler_t thread_fn;
  540. dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
  541. switch (dev->whoami) {
  542. case WAI_12B:
  543. printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
  544. dev->read_data = lis3lv02d_read_12;
  545. dev->mdps_max_val = 2048;
  546. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
  547. dev->odrs = lis3_12_rates;
  548. dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
  549. dev->scale = LIS3_SENSITIVITY_12B;
  550. break;
  551. case WAI_8B:
  552. printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
  553. dev->read_data = lis3lv02d_read_8;
  554. dev->mdps_max_val = 128;
  555. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  556. dev->odrs = lis3_8_rates;
  557. dev->odr_mask = CTRL1_DR;
  558. dev->scale = LIS3_SENSITIVITY_8B;
  559. break;
  560. default:
  561. printk(KERN_ERR DRIVER_NAME
  562. ": unknown sensor type 0x%X\n", dev->whoami);
  563. return -EINVAL;
  564. }
  565. mutex_init(&dev->mutex);
  566. lis3lv02d_add_fs(dev);
  567. lis3lv02d_poweron(dev);
  568. if (lis3lv02d_joystick_enable())
  569. printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
  570. /* passing in platform specific data is purely optional and only
  571. * used by the SPI transport layer at the moment */
  572. if (dev->pdata) {
  573. struct lis3lv02d_platform_data *p = dev->pdata;
  574. if (dev->whoami == WAI_8B)
  575. lis3lv02d_8b_configure(dev, p);
  576. if (p->irq_cfg)
  577. dev->write(dev, CTRL_REG3, p->irq_cfg);
  578. }
  579. /* bail if we did not get an IRQ from the bus layer */
  580. if (!dev->irq) {
  581. printk(KERN_ERR DRIVER_NAME
  582. ": No IRQ. Disabling /dev/freefall\n");
  583. goto out;
  584. }
  585. /*
  586. * The sensor can generate interrupts for free-fall and direction
  587. * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
  588. * the things simple and _fast_ we activate it only for free-fall, so
  589. * no need to read register (very slow with ACPI). For the same reason,
  590. * we forbid shared interrupts.
  591. *
  592. * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
  593. * io-apic is not configurable (and generates a warning) but I keep it
  594. * in case of support for other hardware.
  595. */
  596. if (dev->whoami == WAI_8B)
  597. thread_fn = lis302dl_interrupt_thread1_8b;
  598. else
  599. thread_fn = NULL;
  600. err = request_threaded_irq(dev->irq, lis302dl_interrupt,
  601. thread_fn,
  602. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  603. DRIVER_NAME, &lis3_dev);
  604. if (err < 0) {
  605. printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
  606. goto out;
  607. }
  608. if (misc_register(&lis3lv02d_misc_device))
  609. printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
  610. out:
  611. return 0;
  612. }
  613. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  614. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  615. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  616. MODULE_LICENSE("GPL");