lis3lv02d.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 they 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_12(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: (12 bits sensors only) LSB and MSB values are not updated until
  114. * both have been read. So the value read will always be correct.
  115. */
  116. if (lis3->whoami == WAI_12B) {
  117. lis3->read(lis3, CTRL_REG2, &reg);
  118. reg |= CTRL2_BDU;
  119. lis3->write(lis3, CTRL_REG2, reg);
  120. }
  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. static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
  233. {
  234. int x, y, z;
  235. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  236. input_report_abs(pidev->input, ABS_X, x - lis3_dev.xcalib);
  237. input_report_abs(pidev->input, ABS_Y, y - lis3_dev.ycalib);
  238. input_report_abs(pidev->input, ABS_Z, z - lis3_dev.zcalib);
  239. input_sync(pidev->input);
  240. }
  241. static inline void lis3lv02d_calibrate_joystick(void)
  242. {
  243. lis3lv02d_get_xyz(&lis3_dev,
  244. &lis3_dev.xcalib, &lis3_dev.ycalib, &lis3_dev.zcalib);
  245. }
  246. int lis3lv02d_joystick_enable(void)
  247. {
  248. struct input_dev *input_dev;
  249. int err;
  250. if (lis3_dev.idev)
  251. return -EINVAL;
  252. lis3_dev.idev = input_allocate_polled_device();
  253. if (!lis3_dev.idev)
  254. return -ENOMEM;
  255. lis3_dev.idev->poll = lis3lv02d_joystick_poll;
  256. lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
  257. input_dev = lis3_dev.idev->input;
  258. lis3lv02d_calibrate_joystick();
  259. input_dev->name = "ST LIS3LV02DL Accelerometer";
  260. input_dev->phys = DRIVER_NAME "/input0";
  261. input_dev->id.bustype = BUS_HOST;
  262. input_dev->id.vendor = 0;
  263. input_dev->dev.parent = &lis3_dev.pdev->dev;
  264. set_bit(EV_ABS, input_dev->evbit);
  265. input_set_abs_params(input_dev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  266. input_set_abs_params(input_dev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  267. input_set_abs_params(input_dev, ABS_Z, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  268. err = input_register_polled_device(lis3_dev.idev);
  269. if (err) {
  270. input_free_polled_device(lis3_dev.idev);
  271. lis3_dev.idev = NULL;
  272. }
  273. return err;
  274. }
  275. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  276. void lis3lv02d_joystick_disable(void)
  277. {
  278. if (!lis3_dev.idev)
  279. return;
  280. if (lis3_dev.irq)
  281. misc_deregister(&lis3lv02d_misc_device);
  282. input_unregister_polled_device(lis3_dev.idev);
  283. input_free_polled_device(lis3_dev.idev);
  284. lis3_dev.idev = NULL;
  285. }
  286. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
  287. /* Sysfs stuff */
  288. static ssize_t lis3lv02d_position_show(struct device *dev,
  289. struct device_attribute *attr, char *buf)
  290. {
  291. int x, y, z;
  292. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  293. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  294. }
  295. static ssize_t lis3lv02d_calibrate_show(struct device *dev,
  296. struct device_attribute *attr, char *buf)
  297. {
  298. return sprintf(buf, "(%d,%d,%d)\n", lis3_dev.xcalib, lis3_dev.ycalib, lis3_dev.zcalib);
  299. }
  300. static ssize_t lis3lv02d_calibrate_store(struct device *dev,
  301. struct device_attribute *attr,
  302. const char *buf, size_t count)
  303. {
  304. lis3lv02d_calibrate_joystick();
  305. return count;
  306. }
  307. /* conversion btw sampling rate and the register values */
  308. static int lis3_12_rates[4] = {40, 160, 640, 2560};
  309. static int lis3_8_rates[2] = {100, 400};
  310. static ssize_t lis3lv02d_rate_show(struct device *dev,
  311. struct device_attribute *attr, char *buf)
  312. {
  313. u8 ctrl;
  314. int val;
  315. lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
  316. if (lis3_dev.whoami == WAI_12B)
  317. val = lis3_12_rates[(ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4];
  318. else
  319. val = lis3_8_rates[(ctrl & CTRL1_DR) >> 7];
  320. return sprintf(buf, "%d\n", val);
  321. }
  322. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  323. static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
  324. lis3lv02d_calibrate_store);
  325. static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
  326. static struct attribute *lis3lv02d_attributes[] = {
  327. &dev_attr_position.attr,
  328. &dev_attr_calibrate.attr,
  329. &dev_attr_rate.attr,
  330. NULL
  331. };
  332. static struct attribute_group lis3lv02d_attribute_group = {
  333. .attrs = lis3lv02d_attributes
  334. };
  335. static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
  336. {
  337. lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  338. if (IS_ERR(lis3->pdev))
  339. return PTR_ERR(lis3->pdev);
  340. return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  341. }
  342. int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
  343. {
  344. sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  345. platform_device_unregister(lis3->pdev);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  349. /*
  350. * Initialise the accelerometer and the various subsystems.
  351. * Should be rather independent of the bus system.
  352. */
  353. int lis3lv02d_init_device(struct lis3lv02d *dev)
  354. {
  355. dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
  356. switch (dev->whoami) {
  357. case WAI_12B:
  358. printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
  359. dev->read_data = lis3lv02d_read_12;
  360. dev->mdps_max_val = 2048;
  361. break;
  362. case WAI_8B:
  363. printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
  364. dev->read_data = lis3lv02d_read_8;
  365. dev->mdps_max_val = 128;
  366. break;
  367. default:
  368. printk(KERN_ERR DRIVER_NAME
  369. ": unknown sensor type 0x%X\n", dev->whoami);
  370. return -EINVAL;
  371. }
  372. lis3lv02d_add_fs(dev);
  373. lis3lv02d_poweron(dev);
  374. if (lis3lv02d_joystick_enable())
  375. printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
  376. /* passing in platform specific data is purely optional and only
  377. * used by the SPI transport layer at the moment */
  378. if (dev->pdata) {
  379. struct lis3lv02d_platform_data *p = dev->pdata;
  380. if (p->click_flags && (dev->whoami == WAI_8B)) {
  381. dev->write(dev, CLICK_CFG, p->click_flags);
  382. dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
  383. dev->write(dev, CLICK_LATENCY, p->click_latency);
  384. dev->write(dev, CLICK_WINDOW, p->click_window);
  385. dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
  386. dev->write(dev, CLICK_THSY_X,
  387. (p->click_thresh_x & 0xf) |
  388. (p->click_thresh_y << 4));
  389. }
  390. if (p->wakeup_flags && (dev->whoami == WAI_8B)) {
  391. dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
  392. dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
  393. /* default to 2.5ms for now */
  394. dev->write(dev, FF_WU_DURATION_1, 1);
  395. /* enable high pass filter for both free-fall units */
  396. dev->write(dev, CTRL_REG2, HP_FF_WU1 | HP_FF_WU2);
  397. }
  398. if (p->irq_cfg)
  399. dev->write(dev, CTRL_REG3, p->irq_cfg);
  400. }
  401. /* bail if we did not get an IRQ from the bus layer */
  402. if (!dev->irq) {
  403. printk(KERN_ERR DRIVER_NAME
  404. ": No IRQ. Disabling /dev/freefall\n");
  405. goto out;
  406. }
  407. if (misc_register(&lis3lv02d_misc_device))
  408. printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
  409. out:
  410. return 0;
  411. }
  412. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  413. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  414. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  415. MODULE_LICENSE("GPL");