lis3lv02d.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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->read_data(lis3, OUTX);
  97. position[1] = lis3->read_data(lis3, OUTY);
  98. position[2] = lis3->read_data(lis3, OUTZ);
  99. *x = lis3lv02d_get_axis(lis3->ac.x, position);
  100. *y = lis3lv02d_get_axis(lis3->ac.y, position);
  101. *z = lis3lv02d_get_axis(lis3->ac.z, position);
  102. }
  103. void lis3lv02d_poweroff(struct lis3lv02d *lis3)
  104. {
  105. /* disable X,Y,Z axis and power down */
  106. lis3->write(lis3, CTRL_REG1, 0x00);
  107. }
  108. EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
  109. void lis3lv02d_poweron(struct lis3lv02d *lis3)
  110. {
  111. u8 reg;
  112. lis3->init(lis3);
  113. /*
  114. * Common configuration
  115. * BDU: LSB and MSB values are not updated until both have been read.
  116. * So the value read will always be correct.
  117. */
  118. lis3->read(lis3, CTRL_REG2, &reg);
  119. reg |= CTRL2_BDU;
  120. lis3->write(lis3, CTRL_REG2, reg);
  121. }
  122. EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
  123. static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
  124. {
  125. /*
  126. * Be careful: on some HP laptops the bios force DD when on battery and
  127. * the lid is closed. This leads to interrupts as soon as a little move
  128. * is done.
  129. */
  130. atomic_inc(&lis3_dev.count);
  131. wake_up_interruptible(&lis3_dev.misc_wait);
  132. kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
  133. return IRQ_HANDLED;
  134. }
  135. static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
  136. {
  137. int ret;
  138. if (test_and_set_bit(0, &lis3_dev.misc_opened))
  139. return -EBUSY; /* already open */
  140. atomic_set(&lis3_dev.count, 0);
  141. /*
  142. * The sensor can generate interrupts for free-fall and direction
  143. * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
  144. * the things simple and _fast_ we activate it only for free-fall, so
  145. * no need to read register (very slow with ACPI). For the same reason,
  146. * we forbid shared interrupts.
  147. *
  148. * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
  149. * io-apic is not configurable (and generates a warning) but I keep it
  150. * in case of support for other hardware.
  151. */
  152. ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
  153. DRIVER_NAME, &lis3_dev);
  154. if (ret) {
  155. clear_bit(0, &lis3_dev.misc_opened);
  156. printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", lis3_dev.irq);
  157. return -EBUSY;
  158. }
  159. return 0;
  160. }
  161. static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
  162. {
  163. fasync_helper(-1, file, 0, &lis3_dev.async_queue);
  164. free_irq(lis3_dev.irq, &lis3_dev);
  165. clear_bit(0, &lis3_dev.misc_opened); /* release the device */
  166. return 0;
  167. }
  168. static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
  169. size_t count, loff_t *pos)
  170. {
  171. DECLARE_WAITQUEUE(wait, current);
  172. u32 data;
  173. unsigned char byte_data;
  174. ssize_t retval = 1;
  175. if (count < 1)
  176. return -EINVAL;
  177. add_wait_queue(&lis3_dev.misc_wait, &wait);
  178. while (true) {
  179. set_current_state(TASK_INTERRUPTIBLE);
  180. data = atomic_xchg(&lis3_dev.count, 0);
  181. if (data)
  182. break;
  183. if (file->f_flags & O_NONBLOCK) {
  184. retval = -EAGAIN;
  185. goto out;
  186. }
  187. if (signal_pending(current)) {
  188. retval = -ERESTARTSYS;
  189. goto out;
  190. }
  191. schedule();
  192. }
  193. if (data < 255)
  194. byte_data = data;
  195. else
  196. byte_data = 255;
  197. /* make sure we are not going into copy_to_user() with
  198. * TASK_INTERRUPTIBLE state */
  199. set_current_state(TASK_RUNNING);
  200. if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
  201. retval = -EFAULT;
  202. out:
  203. __set_current_state(TASK_RUNNING);
  204. remove_wait_queue(&lis3_dev.misc_wait, &wait);
  205. return retval;
  206. }
  207. static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
  208. {
  209. poll_wait(file, &lis3_dev.misc_wait, wait);
  210. if (atomic_read(&lis3_dev.count))
  211. return POLLIN | POLLRDNORM;
  212. return 0;
  213. }
  214. static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
  215. {
  216. return fasync_helper(fd, file, on, &lis3_dev.async_queue);
  217. }
  218. static const struct file_operations lis3lv02d_misc_fops = {
  219. .owner = THIS_MODULE,
  220. .llseek = no_llseek,
  221. .read = lis3lv02d_misc_read,
  222. .open = lis3lv02d_misc_open,
  223. .release = lis3lv02d_misc_release,
  224. .poll = lis3lv02d_misc_poll,
  225. .fasync = lis3lv02d_misc_fasync,
  226. };
  227. static struct miscdevice lis3lv02d_misc_device = {
  228. .minor = MISC_DYNAMIC_MINOR,
  229. .name = "freefall",
  230. .fops = &lis3lv02d_misc_fops,
  231. };
  232. /**
  233. * lis3lv02d_joystick_kthread - Kthread polling function
  234. * @data: unused - here to conform to threadfn prototype
  235. */
  236. static int lis3lv02d_joystick_kthread(void *data)
  237. {
  238. int x, y, z;
  239. while (!kthread_should_stop()) {
  240. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  241. input_report_abs(lis3_dev.idev, ABS_X, x - lis3_dev.xcalib);
  242. input_report_abs(lis3_dev.idev, ABS_Y, y - lis3_dev.ycalib);
  243. input_report_abs(lis3_dev.idev, ABS_Z, z - lis3_dev.zcalib);
  244. input_sync(lis3_dev.idev);
  245. try_to_freeze();
  246. msleep_interruptible(MDPS_POLL_INTERVAL);
  247. }
  248. return 0;
  249. }
  250. static int lis3lv02d_joystick_open(struct input_dev *input)
  251. {
  252. lis3_dev.kthread = kthread_run(lis3lv02d_joystick_kthread, NULL, "klis3lv02d");
  253. if (IS_ERR(lis3_dev.kthread)) {
  254. return PTR_ERR(lis3_dev.kthread);
  255. }
  256. return 0;
  257. }
  258. static void lis3lv02d_joystick_close(struct input_dev *input)
  259. {
  260. kthread_stop(lis3_dev.kthread);
  261. }
  262. static inline void lis3lv02d_calibrate_joystick(void)
  263. {
  264. lis3lv02d_get_xyz(&lis3_dev,
  265. &lis3_dev.xcalib, &lis3_dev.ycalib, &lis3_dev.zcalib);
  266. }
  267. int lis3lv02d_joystick_enable(void)
  268. {
  269. int err;
  270. if (lis3_dev.idev)
  271. return -EINVAL;
  272. lis3_dev.idev = input_allocate_device();
  273. if (!lis3_dev.idev)
  274. return -ENOMEM;
  275. lis3lv02d_calibrate_joystick();
  276. lis3_dev.idev->name = "ST LIS3LV02DL Accelerometer";
  277. lis3_dev.idev->phys = DRIVER_NAME "/input0";
  278. lis3_dev.idev->id.bustype = BUS_HOST;
  279. lis3_dev.idev->id.vendor = 0;
  280. lis3_dev.idev->dev.parent = &lis3_dev.pdev->dev;
  281. lis3_dev.idev->open = lis3lv02d_joystick_open;
  282. lis3_dev.idev->close = lis3lv02d_joystick_close;
  283. set_bit(EV_ABS, lis3_dev.idev->evbit);
  284. input_set_abs_params(lis3_dev.idev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  285. input_set_abs_params(lis3_dev.idev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  286. input_set_abs_params(lis3_dev.idev, ABS_Z, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  287. err = input_register_device(lis3_dev.idev);
  288. if (err) {
  289. input_free_device(lis3_dev.idev);
  290. lis3_dev.idev = NULL;
  291. }
  292. return err;
  293. }
  294. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  295. void lis3lv02d_joystick_disable(void)
  296. {
  297. if (!lis3_dev.idev)
  298. return;
  299. if (lis3_dev.irq)
  300. misc_deregister(&lis3lv02d_misc_device);
  301. input_unregister_device(lis3_dev.idev);
  302. lis3_dev.idev = NULL;
  303. }
  304. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
  305. /* Sysfs stuff */
  306. static ssize_t lis3lv02d_position_show(struct device *dev,
  307. struct device_attribute *attr, char *buf)
  308. {
  309. int x, y, z;
  310. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  311. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  312. }
  313. static ssize_t lis3lv02d_calibrate_show(struct device *dev,
  314. struct device_attribute *attr, char *buf)
  315. {
  316. return sprintf(buf, "(%d,%d,%d)\n", lis3_dev.xcalib, lis3_dev.ycalib, lis3_dev.zcalib);
  317. }
  318. static ssize_t lis3lv02d_calibrate_store(struct device *dev,
  319. struct device_attribute *attr,
  320. const char *buf, size_t count)
  321. {
  322. lis3lv02d_calibrate_joystick();
  323. return count;
  324. }
  325. /* conversion btw sampling rate and the register values */
  326. static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
  327. static ssize_t lis3lv02d_rate_show(struct device *dev,
  328. struct device_attribute *attr, char *buf)
  329. {
  330. u8 ctrl;
  331. int val;
  332. lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
  333. val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
  334. return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
  335. }
  336. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  337. static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
  338. lis3lv02d_calibrate_store);
  339. static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
  340. static struct attribute *lis3lv02d_attributes[] = {
  341. &dev_attr_position.attr,
  342. &dev_attr_calibrate.attr,
  343. &dev_attr_rate.attr,
  344. NULL
  345. };
  346. static struct attribute_group lis3lv02d_attribute_group = {
  347. .attrs = lis3lv02d_attributes
  348. };
  349. static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
  350. {
  351. lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  352. if (IS_ERR(lis3->pdev))
  353. return PTR_ERR(lis3->pdev);
  354. return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  355. }
  356. int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
  357. {
  358. sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  359. platform_device_unregister(lis3->pdev);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  363. /*
  364. * Initialise the accelerometer and the various subsystems.
  365. * Should be rather independant of the bus system.
  366. */
  367. int lis3lv02d_init_device(struct lis3lv02d *dev)
  368. {
  369. dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
  370. switch (dev->whoami) {
  371. case LIS_DOUBLE_ID:
  372. printk(KERN_INFO DRIVER_NAME ": 2-byte sensor found\n");
  373. dev->read_data = lis3lv02d_read_16;
  374. dev->mdps_max_val = 2048;
  375. break;
  376. case LIS_SINGLE_ID:
  377. printk(KERN_INFO DRIVER_NAME ": 1-byte sensor found\n");
  378. dev->read_data = lis3lv02d_read_8;
  379. dev->mdps_max_val = 128;
  380. break;
  381. default:
  382. printk(KERN_ERR DRIVER_NAME
  383. ": unknown sensor type 0x%X\n", dev->whoami);
  384. return -EINVAL;
  385. }
  386. lis3lv02d_add_fs(dev);
  387. lis3lv02d_poweron(dev);
  388. if (lis3lv02d_joystick_enable())
  389. printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
  390. /* bail if we did not get an IRQ from the bus layer */
  391. if (!dev->irq) {
  392. printk(KERN_ERR DRIVER_NAME
  393. ": No IRQ. Disabling /dev/freefall\n");
  394. goto out;
  395. }
  396. if (misc_register(&lis3lv02d_misc_device))
  397. printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
  398. out:
  399. return 0;
  400. }
  401. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  402. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  403. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  404. MODULE_LICENSE("GPL");