lis3lv02d.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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.h>
  30. #include <linux/kthread.h>
  31. #include <linux/semaphore.h>
  32. #include <linux/delay.h>
  33. #include <linux/wait.h>
  34. #include <linux/poll.h>
  35. #include <linux/freezer.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/miscdevice.h>
  38. #include <asm/atomic.h>
  39. #include "lis3lv02d.h"
  40. #define DRIVER_NAME "lis3lv02d"
  41. /* joystick device poll interval in milliseconds */
  42. #define MDPS_POLL_INTERVAL 50
  43. /*
  44. * The sensor can also generate interrupts (DRDY) but it's pretty pointless
  45. * because their 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. struct lis3lv02d lis3_dev = {
  52. .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
  53. };
  54. EXPORT_SYMBOL_GPL(lis3_dev);
  55. static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
  56. {
  57. s8 lo;
  58. if (lis3->read(lis3, reg, &lo) < 0)
  59. return 0;
  60. return lo;
  61. }
  62. static s16 lis3lv02d_read_16(struct lis3lv02d *lis3, int reg)
  63. {
  64. u8 lo, hi;
  65. lis3->read(lis3, reg - 1, &lo);
  66. lis3->read(lis3, reg, &hi);
  67. /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
  68. return (s16)((hi << 8) | lo);
  69. }
  70. /**
  71. * lis3lv02d_get_axis - For the given axis, give the value converted
  72. * @axis: 1,2,3 - can also be negative
  73. * @hw_values: raw values returned by the hardware
  74. *
  75. * Returns the converted value.
  76. */
  77. static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
  78. {
  79. if (axis > 0)
  80. return hw_values[axis - 1];
  81. else
  82. return -hw_values[-axis - 1];
  83. }
  84. /**
  85. * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
  86. * @lis3: pointer to the device struct
  87. * @x: where to store the X axis value
  88. * @y: where to store the Y axis value
  89. * @z: where to store the Z axis value
  90. *
  91. * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
  92. */
  93. static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
  94. {
  95. int position[3];
  96. position[0] = lis3_dev.read_data(lis3, OUTX);
  97. position[1] = lis3_dev.read_data(lis3, OUTY);
  98. position[2] = lis3_dev.read_data(lis3, OUTZ);
  99. *x = lis3lv02d_get_axis(lis3_dev.ac.x, position);
  100. *y = lis3lv02d_get_axis(lis3_dev.ac.y, position);
  101. *z = lis3lv02d_get_axis(lis3_dev.ac.z, position);
  102. }
  103. void lis3lv02d_poweroff(struct lis3lv02d *lis3)
  104. {
  105. lis3_dev.is_on = 0;
  106. }
  107. EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
  108. void lis3lv02d_poweron(struct lis3lv02d *lis3)
  109. {
  110. lis3_dev.is_on = 1;
  111. lis3_dev.init(lis3);
  112. }
  113. EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
  114. /*
  115. * To be called before starting to use the device. It makes sure that the
  116. * device will always be on until a call to lis3lv02d_decrease_use(). Not to be
  117. * used from interrupt context.
  118. */
  119. static void lis3lv02d_increase_use(struct lis3lv02d *dev)
  120. {
  121. mutex_lock(&dev->lock);
  122. dev->usage++;
  123. if (dev->usage == 1) {
  124. if (!dev->is_on)
  125. lis3lv02d_poweron(dev);
  126. }
  127. mutex_unlock(&dev->lock);
  128. }
  129. /*
  130. * To be called whenever a usage of the device is stopped.
  131. * It will make sure to turn off the device when there is not usage.
  132. */
  133. static void lis3lv02d_decrease_use(struct lis3lv02d *dev)
  134. {
  135. mutex_lock(&dev->lock);
  136. dev->usage--;
  137. if (dev->usage == 0)
  138. lis3lv02d_poweroff(dev);
  139. mutex_unlock(&dev->lock);
  140. }
  141. static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
  142. {
  143. /*
  144. * Be careful: on some HP laptops the bios force DD when on battery and
  145. * the lid is closed. This leads to interrupts as soon as a little move
  146. * is done.
  147. */
  148. atomic_inc(&lis3_dev.count);
  149. wake_up_interruptible(&lis3_dev.misc_wait);
  150. kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
  151. return IRQ_HANDLED;
  152. }
  153. static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
  154. {
  155. int ret;
  156. if (test_and_set_bit(0, &lis3_dev.misc_opened))
  157. return -EBUSY; /* already open */
  158. atomic_set(&lis3_dev.count, 0);
  159. /*
  160. * The sensor can generate interrupts for free-fall and direction
  161. * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
  162. * the things simple and _fast_ we activate it only for free-fall, so
  163. * no need to read register (very slow with ACPI). For the same reason,
  164. * we forbid shared interrupts.
  165. *
  166. * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
  167. * io-apic is not configurable (and generates a warning) but I keep it
  168. * in case of support for other hardware.
  169. */
  170. ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
  171. DRIVER_NAME, &lis3_dev);
  172. if (ret) {
  173. clear_bit(0, &lis3_dev.misc_opened);
  174. printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", lis3_dev.irq);
  175. return -EBUSY;
  176. }
  177. lis3lv02d_increase_use(&lis3_dev);
  178. printk("lis3: registered interrupt %d\n", lis3_dev.irq);
  179. return 0;
  180. }
  181. static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
  182. {
  183. fasync_helper(-1, file, 0, &lis3_dev.async_queue);
  184. lis3lv02d_decrease_use(&lis3_dev);
  185. free_irq(lis3_dev.irq, &lis3_dev);
  186. clear_bit(0, &lis3_dev.misc_opened); /* release the device */
  187. return 0;
  188. }
  189. static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
  190. size_t count, loff_t *pos)
  191. {
  192. DECLARE_WAITQUEUE(wait, current);
  193. u32 data;
  194. unsigned char byte_data;
  195. ssize_t retval = 1;
  196. if (count < 1)
  197. return -EINVAL;
  198. add_wait_queue(&lis3_dev.misc_wait, &wait);
  199. while (true) {
  200. set_current_state(TASK_INTERRUPTIBLE);
  201. data = atomic_xchg(&lis3_dev.count, 0);
  202. if (data)
  203. break;
  204. if (file->f_flags & O_NONBLOCK) {
  205. retval = -EAGAIN;
  206. goto out;
  207. }
  208. if (signal_pending(current)) {
  209. retval = -ERESTARTSYS;
  210. goto out;
  211. }
  212. schedule();
  213. }
  214. if (data < 255)
  215. byte_data = data;
  216. else
  217. byte_data = 255;
  218. /* make sure we are not going into copy_to_user() with
  219. * TASK_INTERRUPTIBLE state */
  220. set_current_state(TASK_RUNNING);
  221. if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
  222. retval = -EFAULT;
  223. out:
  224. __set_current_state(TASK_RUNNING);
  225. remove_wait_queue(&lis3_dev.misc_wait, &wait);
  226. return retval;
  227. }
  228. static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
  229. {
  230. poll_wait(file, &lis3_dev.misc_wait, wait);
  231. if (atomic_read(&lis3_dev.count))
  232. return POLLIN | POLLRDNORM;
  233. return 0;
  234. }
  235. static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
  236. {
  237. return fasync_helper(fd, file, on, &lis3_dev.async_queue);
  238. }
  239. static const struct file_operations lis3lv02d_misc_fops = {
  240. .owner = THIS_MODULE,
  241. .llseek = no_llseek,
  242. .read = lis3lv02d_misc_read,
  243. .open = lis3lv02d_misc_open,
  244. .release = lis3lv02d_misc_release,
  245. .poll = lis3lv02d_misc_poll,
  246. .fasync = lis3lv02d_misc_fasync,
  247. };
  248. static struct miscdevice lis3lv02d_misc_device = {
  249. .minor = MISC_DYNAMIC_MINOR,
  250. .name = "freefall",
  251. .fops = &lis3lv02d_misc_fops,
  252. };
  253. /**
  254. * lis3lv02d_joystick_kthread - Kthread polling function
  255. * @data: unused - here to conform to threadfn prototype
  256. */
  257. static int lis3lv02d_joystick_kthread(void *data)
  258. {
  259. int x, y, z;
  260. while (!kthread_should_stop()) {
  261. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  262. input_report_abs(lis3_dev.idev, ABS_X, x - lis3_dev.xcalib);
  263. input_report_abs(lis3_dev.idev, ABS_Y, y - lis3_dev.ycalib);
  264. input_report_abs(lis3_dev.idev, ABS_Z, z - lis3_dev.zcalib);
  265. input_sync(lis3_dev.idev);
  266. try_to_freeze();
  267. msleep_interruptible(MDPS_POLL_INTERVAL);
  268. }
  269. return 0;
  270. }
  271. static int lis3lv02d_joystick_open(struct input_dev *input)
  272. {
  273. lis3lv02d_increase_use(&lis3_dev);
  274. lis3_dev.kthread = kthread_run(lis3lv02d_joystick_kthread, NULL, "klis3lv02d");
  275. if (IS_ERR(lis3_dev.kthread)) {
  276. lis3lv02d_decrease_use(&lis3_dev);
  277. return PTR_ERR(lis3_dev.kthread);
  278. }
  279. return 0;
  280. }
  281. static void lis3lv02d_joystick_close(struct input_dev *input)
  282. {
  283. kthread_stop(lis3_dev.kthread);
  284. lis3lv02d_decrease_use(&lis3_dev);
  285. }
  286. static inline void lis3lv02d_calibrate_joystick(void)
  287. {
  288. lis3lv02d_get_xyz(&lis3_dev,
  289. &lis3_dev.xcalib, &lis3_dev.ycalib, &lis3_dev.zcalib);
  290. }
  291. int lis3lv02d_joystick_enable(void)
  292. {
  293. int err;
  294. if (lis3_dev.idev)
  295. return -EINVAL;
  296. lis3_dev.idev = input_allocate_device();
  297. if (!lis3_dev.idev)
  298. return -ENOMEM;
  299. lis3lv02d_calibrate_joystick();
  300. lis3_dev.idev->name = "ST LIS3LV02DL Accelerometer";
  301. lis3_dev.idev->phys = DRIVER_NAME "/input0";
  302. lis3_dev.idev->id.bustype = BUS_HOST;
  303. lis3_dev.idev->id.vendor = 0;
  304. lis3_dev.idev->dev.parent = &lis3_dev.pdev->dev;
  305. lis3_dev.idev->open = lis3lv02d_joystick_open;
  306. lis3_dev.idev->close = lis3lv02d_joystick_close;
  307. set_bit(EV_ABS, lis3_dev.idev->evbit);
  308. input_set_abs_params(lis3_dev.idev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  309. input_set_abs_params(lis3_dev.idev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  310. input_set_abs_params(lis3_dev.idev, ABS_Z, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  311. err = input_register_device(lis3_dev.idev);
  312. if (err) {
  313. input_free_device(lis3_dev.idev);
  314. lis3_dev.idev = NULL;
  315. }
  316. return err;
  317. }
  318. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  319. void lis3lv02d_joystick_disable(void)
  320. {
  321. if (!lis3_dev.idev)
  322. return;
  323. misc_deregister(&lis3lv02d_misc_device);
  324. input_unregister_device(lis3_dev.idev);
  325. lis3_dev.idev = NULL;
  326. }
  327. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
  328. /* Sysfs stuff */
  329. static ssize_t lis3lv02d_position_show(struct device *dev,
  330. struct device_attribute *attr, char *buf)
  331. {
  332. int x, y, z;
  333. lis3lv02d_increase_use(&lis3_dev);
  334. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  335. lis3lv02d_decrease_use(&lis3_dev);
  336. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  337. }
  338. static ssize_t lis3lv02d_calibrate_show(struct device *dev,
  339. struct device_attribute *attr, char *buf)
  340. {
  341. return sprintf(buf, "(%d,%d,%d)\n", lis3_dev.xcalib, lis3_dev.ycalib, lis3_dev.zcalib);
  342. }
  343. static ssize_t lis3lv02d_calibrate_store(struct device *dev,
  344. struct device_attribute *attr,
  345. const char *buf, size_t count)
  346. {
  347. lis3lv02d_increase_use(&lis3_dev);
  348. lis3lv02d_calibrate_joystick();
  349. lis3lv02d_decrease_use(&lis3_dev);
  350. return count;
  351. }
  352. /* conversion btw sampling rate and the register values */
  353. static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
  354. static ssize_t lis3lv02d_rate_show(struct device *dev,
  355. struct device_attribute *attr, char *buf)
  356. {
  357. u8 ctrl;
  358. int val;
  359. lis3lv02d_increase_use(&lis3_dev);
  360. lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
  361. lis3lv02d_decrease_use(&lis3_dev);
  362. val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
  363. return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
  364. }
  365. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  366. static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
  367. lis3lv02d_calibrate_store);
  368. static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
  369. static struct attribute *lis3lv02d_attributes[] = {
  370. &dev_attr_position.attr,
  371. &dev_attr_calibrate.attr,
  372. &dev_attr_rate.attr,
  373. NULL
  374. };
  375. static struct attribute_group lis3lv02d_attribute_group = {
  376. .attrs = lis3lv02d_attributes
  377. };
  378. static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
  379. {
  380. lis3_dev.pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  381. if (IS_ERR(lis3_dev.pdev))
  382. return PTR_ERR(lis3_dev.pdev);
  383. return sysfs_create_group(&lis3_dev.pdev->dev.kobj, &lis3lv02d_attribute_group);
  384. }
  385. int lis3lv02d_remove_fs(void)
  386. {
  387. sysfs_remove_group(&lis3_dev.pdev->dev.kobj, &lis3lv02d_attribute_group);
  388. platform_device_unregister(lis3_dev.pdev);
  389. return 0;
  390. }
  391. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  392. /*
  393. * Initialise the accelerometer and the various subsystems.
  394. * Should be rather independant of the bus system.
  395. */
  396. int lis3lv02d_init_device(struct lis3lv02d *dev)
  397. {
  398. dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
  399. switch (dev->whoami) {
  400. case LIS_DOUBLE_ID:
  401. printk(KERN_INFO DRIVER_NAME ": 2-byte sensor found\n");
  402. dev->read_data = lis3lv02d_read_16;
  403. dev->mdps_max_val = 2048;
  404. break;
  405. case LIS_SINGLE_ID:
  406. printk(KERN_INFO DRIVER_NAME ": 1-byte sensor found\n");
  407. dev->read_data = lis3lv02d_read_8;
  408. dev->mdps_max_val = 128;
  409. break;
  410. default:
  411. printk(KERN_ERR DRIVER_NAME
  412. ": unknown sensor type 0x%X\n", lis3_dev.whoami);
  413. return -EINVAL;
  414. }
  415. mutex_init(&dev->lock);
  416. lis3lv02d_add_fs(dev);
  417. lis3lv02d_increase_use(dev);
  418. if (lis3lv02d_joystick_enable())
  419. printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
  420. printk("lis3_init_device: irq %d\n", dev->irq);
  421. /* bail if we did not get an IRQ from the bus layer */
  422. if (!dev->irq) {
  423. printk(KERN_ERR DRIVER_NAME
  424. ": No IRQ. Disabling /dev/freefall\n");
  425. goto out;
  426. }
  427. printk("lis3: registering device\n");
  428. if (misc_register(&lis3lv02d_misc_device))
  429. printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
  430. out:
  431. lis3lv02d_decrease_use(dev);
  432. return 0;
  433. }
  434. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  435. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  436. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  437. MODULE_LICENSE("GPL");