lis3lv02d.c 19 KB

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