lis3lv02d.c 13 KB

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