lis3lv02d.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 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 <acpi/acpi_drivers.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. /* Maximum value our axis may get for the input device (signed 12 bits) */
  52. #define MDPS_MAX_VAL 2048
  53. struct acpi_lis3lv02d adev;
  54. EXPORT_SYMBOL_GPL(adev);
  55. static int lis3lv02d_add_fs(struct acpi_device *device);
  56. static s16 lis3lv02d_read_16(acpi_handle handle, int reg)
  57. {
  58. u8 lo, hi;
  59. adev.read(handle, reg, &lo);
  60. adev.read(handle, reg + 1, &hi);
  61. /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
  62. return (s16)((hi << 8) | lo);
  63. }
  64. /**
  65. * lis3lv02d_get_axis - For the given axis, give the value converted
  66. * @axis: 1,2,3 - can also be negative
  67. * @hw_values: raw values returned by the hardware
  68. *
  69. * Returns the converted value.
  70. */
  71. static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
  72. {
  73. if (axis > 0)
  74. return hw_values[axis - 1];
  75. else
  76. return -hw_values[-axis - 1];
  77. }
  78. /**
  79. * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
  80. * @handle: the handle to the device
  81. * @x: where to store the X axis value
  82. * @y: where to store the Y axis value
  83. * @z: where to store the Z axis value
  84. *
  85. * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
  86. */
  87. static void lis3lv02d_get_xyz(acpi_handle handle, int *x, int *y, int *z)
  88. {
  89. int position[3];
  90. position[0] = lis3lv02d_read_16(handle, OUTX_L);
  91. position[1] = lis3lv02d_read_16(handle, OUTY_L);
  92. position[2] = lis3lv02d_read_16(handle, OUTZ_L);
  93. *x = lis3lv02d_get_axis(adev.ac.x, position);
  94. *y = lis3lv02d_get_axis(adev.ac.y, position);
  95. *z = lis3lv02d_get_axis(adev.ac.z, position);
  96. }
  97. void lis3lv02d_poweroff(acpi_handle handle)
  98. {
  99. adev.is_on = 0;
  100. /* disable X,Y,Z axis and power down */
  101. adev.write(handle, CTRL_REG1, 0x00);
  102. }
  103. EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
  104. void lis3lv02d_poweron(acpi_handle handle)
  105. {
  106. u8 val;
  107. adev.is_on = 1;
  108. adev.init(handle);
  109. adev.write(handle, FF_WU_CFG, 0);
  110. /*
  111. * BDU: LSB and MSB values are not updated until both have been read.
  112. * So the value read will always be correct.
  113. * IEN: Interrupt for free-fall and DD, not for data-ready.
  114. */
  115. adev.read(handle, CTRL_REG2, &val);
  116. val |= CTRL2_BDU | CTRL2_IEN;
  117. adev.write(handle, CTRL_REG2, val);
  118. }
  119. EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
  120. /*
  121. * To be called before starting to use the device. It makes sure that the
  122. * device will always be on until a call to lis3lv02d_decrease_use(). Not to be
  123. * used from interrupt context.
  124. */
  125. static void lis3lv02d_increase_use(struct acpi_lis3lv02d *dev)
  126. {
  127. mutex_lock(&dev->lock);
  128. dev->usage++;
  129. if (dev->usage == 1) {
  130. if (!dev->is_on)
  131. lis3lv02d_poweron(dev->device->handle);
  132. }
  133. mutex_unlock(&dev->lock);
  134. }
  135. /*
  136. * To be called whenever a usage of the device is stopped.
  137. * It will make sure to turn off the device when there is not usage.
  138. */
  139. static void lis3lv02d_decrease_use(struct acpi_lis3lv02d *dev)
  140. {
  141. mutex_lock(&dev->lock);
  142. dev->usage--;
  143. if (dev->usage == 0)
  144. lis3lv02d_poweroff(dev->device->handle);
  145. mutex_unlock(&dev->lock);
  146. }
  147. /**
  148. * lis3lv02d_joystick_kthread - Kthread polling function
  149. * @data: unused - here to conform to threadfn prototype
  150. */
  151. static int lis3lv02d_joystick_kthread(void *data)
  152. {
  153. int x, y, z;
  154. while (!kthread_should_stop()) {
  155. lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
  156. input_report_abs(adev.idev, ABS_X, x - adev.xcalib);
  157. input_report_abs(adev.idev, ABS_Y, y - adev.ycalib);
  158. input_report_abs(adev.idev, ABS_Z, z - adev.zcalib);
  159. input_sync(adev.idev);
  160. try_to_freeze();
  161. msleep_interruptible(MDPS_POLL_INTERVAL);
  162. }
  163. return 0;
  164. }
  165. static int lis3lv02d_joystick_open(struct input_dev *input)
  166. {
  167. lis3lv02d_increase_use(&adev);
  168. adev.kthread = kthread_run(lis3lv02d_joystick_kthread, NULL, "klis3lv02d");
  169. if (IS_ERR(adev.kthread)) {
  170. lis3lv02d_decrease_use(&adev);
  171. return PTR_ERR(adev.kthread);
  172. }
  173. return 0;
  174. }
  175. static void lis3lv02d_joystick_close(struct input_dev *input)
  176. {
  177. kthread_stop(adev.kthread);
  178. lis3lv02d_decrease_use(&adev);
  179. }
  180. static inline void lis3lv02d_calibrate_joystick(void)
  181. {
  182. lis3lv02d_get_xyz(adev.device->handle, &adev.xcalib, &adev.ycalib, &adev.zcalib);
  183. }
  184. int lis3lv02d_joystick_enable(void)
  185. {
  186. int err;
  187. if (adev.idev)
  188. return -EINVAL;
  189. adev.idev = input_allocate_device();
  190. if (!adev.idev)
  191. return -ENOMEM;
  192. lis3lv02d_calibrate_joystick();
  193. adev.idev->name = "ST LIS3LV02DL Accelerometer";
  194. adev.idev->phys = DRIVER_NAME "/input0";
  195. adev.idev->id.bustype = BUS_HOST;
  196. adev.idev->id.vendor = 0;
  197. adev.idev->dev.parent = &adev.pdev->dev;
  198. adev.idev->open = lis3lv02d_joystick_open;
  199. adev.idev->close = lis3lv02d_joystick_close;
  200. set_bit(EV_ABS, adev.idev->evbit);
  201. input_set_abs_params(adev.idev, ABS_X, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
  202. input_set_abs_params(adev.idev, ABS_Y, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
  203. input_set_abs_params(adev.idev, ABS_Z, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
  204. err = input_register_device(adev.idev);
  205. if (err) {
  206. input_free_device(adev.idev);
  207. adev.idev = NULL;
  208. }
  209. return err;
  210. }
  211. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  212. void lis3lv02d_joystick_disable(void)
  213. {
  214. if (!adev.idev)
  215. return;
  216. input_unregister_device(adev.idev);
  217. adev.idev = NULL;
  218. }
  219. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
  220. /*
  221. * Initialise the accelerometer and the various subsystems.
  222. * Should be rather independant of the bus system.
  223. */
  224. int lis3lv02d_init_device(struct acpi_lis3lv02d *dev)
  225. {
  226. mutex_init(&dev->lock);
  227. lis3lv02d_add_fs(dev->device);
  228. lis3lv02d_increase_use(dev);
  229. if (lis3lv02d_joystick_enable())
  230. printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
  231. lis3lv02d_decrease_use(dev);
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  235. /* Sysfs stuff */
  236. static ssize_t lis3lv02d_position_show(struct device *dev,
  237. struct device_attribute *attr, char *buf)
  238. {
  239. int x, y, z;
  240. lis3lv02d_increase_use(&adev);
  241. lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
  242. lis3lv02d_decrease_use(&adev);
  243. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  244. }
  245. static ssize_t lis3lv02d_calibrate_show(struct device *dev,
  246. struct device_attribute *attr, char *buf)
  247. {
  248. return sprintf(buf, "(%d,%d,%d)\n", adev.xcalib, adev.ycalib, adev.zcalib);
  249. }
  250. static ssize_t lis3lv02d_calibrate_store(struct device *dev,
  251. struct device_attribute *attr,
  252. const char *buf, size_t count)
  253. {
  254. lis3lv02d_increase_use(&adev);
  255. lis3lv02d_calibrate_joystick();
  256. lis3lv02d_decrease_use(&adev);
  257. return count;
  258. }
  259. /* conversion btw sampling rate and the register values */
  260. static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
  261. static ssize_t lis3lv02d_rate_show(struct device *dev,
  262. struct device_attribute *attr, char *buf)
  263. {
  264. u8 ctrl;
  265. int val;
  266. lis3lv02d_increase_use(&adev);
  267. adev.read(adev.device->handle, CTRL_REG1, &ctrl);
  268. lis3lv02d_decrease_use(&adev);
  269. val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
  270. return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
  271. }
  272. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  273. static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
  274. lis3lv02d_calibrate_store);
  275. static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
  276. static struct attribute *lis3lv02d_attributes[] = {
  277. &dev_attr_position.attr,
  278. &dev_attr_calibrate.attr,
  279. &dev_attr_rate.attr,
  280. NULL
  281. };
  282. static struct attribute_group lis3lv02d_attribute_group = {
  283. .attrs = lis3lv02d_attributes
  284. };
  285. static int lis3lv02d_add_fs(struct acpi_device *device)
  286. {
  287. adev.pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  288. if (IS_ERR(adev.pdev))
  289. return PTR_ERR(adev.pdev);
  290. return sysfs_create_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
  291. }
  292. int lis3lv02d_remove_fs(void)
  293. {
  294. sysfs_remove_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
  295. platform_device_unregister(adev.pdev);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  299. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  300. MODULE_AUTHOR("Yan Burman and Eric Piel");
  301. MODULE_LICENSE("GPL");