lis3lv02d.c 13 KB

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