lis3lv02d.c 13 KB

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