lis3lv02d.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #define LIS3_PWRON_DELAY_WAI_12B (5000)
  50. #define LIS3_PWRON_DELAY_WAI_8B (3000)
  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_12(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. /* conversion btw sampling rate and the register values */
  104. static int lis3_12_rates[4] = {40, 160, 640, 2560};
  105. static int lis3_8_rates[2] = {100, 400};
  106. static int lis3lv02d_get_odr(void)
  107. {
  108. u8 ctrl;
  109. int val;
  110. lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
  111. if (lis3_dev.whoami == WAI_12B)
  112. val = lis3_12_rates[(ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4];
  113. else
  114. val = lis3_8_rates[(ctrl & CTRL1_DR) >> 7];
  115. return val;
  116. }
  117. void lis3lv02d_poweroff(struct lis3lv02d *lis3)
  118. {
  119. /* disable X,Y,Z axis and power down */
  120. lis3->write(lis3, CTRL_REG1, 0x00);
  121. }
  122. EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
  123. void lis3lv02d_poweron(struct lis3lv02d *lis3)
  124. {
  125. u8 reg;
  126. lis3->init(lis3);
  127. /* LIS3 power on delay is quite long */
  128. msleep(lis3->pwron_delay / lis3lv02d_get_odr());
  129. /*
  130. * Common configuration
  131. * BDU: (12 bits sensors only) LSB and MSB values are not updated until
  132. * both have been read. So the value read will always be correct.
  133. */
  134. if (lis3->whoami == WAI_12B) {
  135. lis3->read(lis3, CTRL_REG2, &reg);
  136. reg |= CTRL2_BDU;
  137. lis3->write(lis3, CTRL_REG2, reg);
  138. }
  139. }
  140. EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
  141. static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
  142. {
  143. /*
  144. * Be careful: on some HP laptops the bios force DD when on battery and
  145. * the lid is closed. This leads to interrupts as soon as a little move
  146. * is done.
  147. */
  148. atomic_inc(&lis3_dev.count);
  149. wake_up_interruptible(&lis3_dev.misc_wait);
  150. kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
  151. return IRQ_HANDLED;
  152. }
  153. static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
  154. {
  155. int ret;
  156. if (test_and_set_bit(0, &lis3_dev.misc_opened))
  157. return -EBUSY; /* already open */
  158. atomic_set(&lis3_dev.count, 0);
  159. /*
  160. * The sensor can generate interrupts for free-fall and direction
  161. * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
  162. * the things simple and _fast_ we activate it only for free-fall, so
  163. * no need to read register (very slow with ACPI). For the same reason,
  164. * we forbid shared interrupts.
  165. *
  166. * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
  167. * io-apic is not configurable (and generates a warning) but I keep it
  168. * in case of support for other hardware.
  169. */
  170. ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
  171. DRIVER_NAME, &lis3_dev);
  172. if (ret) {
  173. clear_bit(0, &lis3_dev.misc_opened);
  174. printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", lis3_dev.irq);
  175. return -EBUSY;
  176. }
  177. return 0;
  178. }
  179. static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
  180. {
  181. fasync_helper(-1, file, 0, &lis3_dev.async_queue);
  182. free_irq(lis3_dev.irq, &lis3_dev);
  183. clear_bit(0, &lis3_dev.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(&lis3_dev.misc_wait, &wait);
  196. while (true) {
  197. set_current_state(TASK_INTERRUPTIBLE);
  198. data = atomic_xchg(&lis3_dev.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(&lis3_dev.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, &lis3_dev.misc_wait, wait);
  228. if (atomic_read(&lis3_dev.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, &lis3_dev.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. static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
  251. {
  252. int x, y, z;
  253. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  254. input_report_abs(pidev->input, ABS_X, x - lis3_dev.xcalib);
  255. input_report_abs(pidev->input, ABS_Y, y - lis3_dev.ycalib);
  256. input_report_abs(pidev->input, ABS_Z, z - lis3_dev.zcalib);
  257. input_sync(pidev->input);
  258. }
  259. static inline void lis3lv02d_calibrate_joystick(void)
  260. {
  261. lis3lv02d_get_xyz(&lis3_dev,
  262. &lis3_dev.xcalib, &lis3_dev.ycalib, &lis3_dev.zcalib);
  263. }
  264. int lis3lv02d_joystick_enable(void)
  265. {
  266. struct input_dev *input_dev;
  267. int err;
  268. if (lis3_dev.idev)
  269. return -EINVAL;
  270. lis3_dev.idev = input_allocate_polled_device();
  271. if (!lis3_dev.idev)
  272. return -ENOMEM;
  273. lis3_dev.idev->poll = lis3lv02d_joystick_poll;
  274. lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
  275. input_dev = lis3_dev.idev->input;
  276. lis3lv02d_calibrate_joystick();
  277. input_dev->name = "ST LIS3LV02DL Accelerometer";
  278. input_dev->phys = DRIVER_NAME "/input0";
  279. input_dev->id.bustype = BUS_HOST;
  280. input_dev->id.vendor = 0;
  281. input_dev->dev.parent = &lis3_dev.pdev->dev;
  282. set_bit(EV_ABS, input_dev->evbit);
  283. input_set_abs_params(input_dev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  284. input_set_abs_params(input_dev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  285. input_set_abs_params(input_dev, ABS_Z, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
  286. err = input_register_polled_device(lis3_dev.idev);
  287. if (err) {
  288. input_free_polled_device(lis3_dev.idev);
  289. lis3_dev.idev = NULL;
  290. }
  291. return err;
  292. }
  293. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  294. void lis3lv02d_joystick_disable(void)
  295. {
  296. if (!lis3_dev.idev)
  297. return;
  298. if (lis3_dev.irq)
  299. misc_deregister(&lis3lv02d_misc_device);
  300. input_unregister_polled_device(lis3_dev.idev);
  301. input_free_polled_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. static ssize_t lis3lv02d_rate_show(struct device *dev,
  326. struct device_attribute *attr, char *buf)
  327. {
  328. return sprintf(buf, "%d\n", lis3lv02d_get_odr());
  329. }
  330. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  331. static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
  332. lis3lv02d_calibrate_store);
  333. static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
  334. static struct attribute *lis3lv02d_attributes[] = {
  335. &dev_attr_position.attr,
  336. &dev_attr_calibrate.attr,
  337. &dev_attr_rate.attr,
  338. NULL
  339. };
  340. static struct attribute_group lis3lv02d_attribute_group = {
  341. .attrs = lis3lv02d_attributes
  342. };
  343. static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
  344. {
  345. lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  346. if (IS_ERR(lis3->pdev))
  347. return PTR_ERR(lis3->pdev);
  348. return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  349. }
  350. int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
  351. {
  352. sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  353. platform_device_unregister(lis3->pdev);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  357. /*
  358. * Initialise the accelerometer and the various subsystems.
  359. * Should be rather independent of the bus system.
  360. */
  361. int lis3lv02d_init_device(struct lis3lv02d *dev)
  362. {
  363. dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
  364. switch (dev->whoami) {
  365. case WAI_12B:
  366. printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
  367. dev->read_data = lis3lv02d_read_12;
  368. dev->mdps_max_val = 2048;
  369. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
  370. break;
  371. case WAI_8B:
  372. printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
  373. dev->read_data = lis3lv02d_read_8;
  374. dev->mdps_max_val = 128;
  375. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  376. break;
  377. default:
  378. printk(KERN_ERR DRIVER_NAME
  379. ": unknown sensor type 0x%X\n", dev->whoami);
  380. return -EINVAL;
  381. }
  382. lis3lv02d_add_fs(dev);
  383. lis3lv02d_poweron(dev);
  384. if (lis3lv02d_joystick_enable())
  385. printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
  386. /* passing in platform specific data is purely optional and only
  387. * used by the SPI transport layer at the moment */
  388. if (dev->pdata) {
  389. struct lis3lv02d_platform_data *p = dev->pdata;
  390. if (p->click_flags && (dev->whoami == WAI_8B)) {
  391. dev->write(dev, CLICK_CFG, p->click_flags);
  392. dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
  393. dev->write(dev, CLICK_LATENCY, p->click_latency);
  394. dev->write(dev, CLICK_WINDOW, p->click_window);
  395. dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
  396. dev->write(dev, CLICK_THSY_X,
  397. (p->click_thresh_x & 0xf) |
  398. (p->click_thresh_y << 4));
  399. }
  400. if (p->wakeup_flags && (dev->whoami == WAI_8B)) {
  401. dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
  402. dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
  403. /* default to 2.5ms for now */
  404. dev->write(dev, FF_WU_DURATION_1, 1);
  405. /* enable high pass filter for both free-fall units */
  406. dev->write(dev, CTRL_REG2, HP_FF_WU1 | HP_FF_WU2);
  407. }
  408. if (p->irq_cfg)
  409. dev->write(dev, CTRL_REG3, p->irq_cfg);
  410. }
  411. /* bail if we did not get an IRQ from the bus layer */
  412. if (!dev->irq) {
  413. printk(KERN_ERR DRIVER_NAME
  414. ": No IRQ. Disabling /dev/freefall\n");
  415. goto out;
  416. }
  417. if (misc_register(&lis3lv02d_misc_device))
  418. printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
  419. out:
  420. return 0;
  421. }
  422. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  423. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  424. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  425. MODULE_LICENSE("GPL");