lis3lv02d.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
  3. *
  4. * Copyright (C) 2007-2008 Yan Burman
  5. * Copyright (C) 2008 Eric Piel
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/dmi.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/input.h>
  29. #include <linux/kthread.h>
  30. #include <linux/semaphore.h>
  31. #include <linux/delay.h>
  32. #include <linux/wait.h>
  33. #include <linux/poll.h>
  34. #include <linux/freezer.h>
  35. #include <linux/version.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. #define ACPI_MDPS_CLASS "accelerometer"
  42. /* joystick device poll interval in milliseconds */
  43. #define MDPS_POLL_INTERVAL 50
  44. /*
  45. * The sensor can also generate interrupts (DRDY) but it's pretty pointless
  46. * because their are generated even if the data do not change. So it's better
  47. * to keep the interrupt for the free-fall event. The values are updated at
  48. * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
  49. * some low processor, we poll the sensor only at 20Hz... enough for the
  50. * joystick.
  51. */
  52. /* Maximum value our axis may get for the input device (signed 12 bits) */
  53. #define MDPS_MAX_VAL 2048
  54. struct axis_conversion {
  55. s8 x;
  56. s8 y;
  57. s8 z;
  58. };
  59. struct acpi_lis3lv02d {
  60. struct acpi_device *device; /* The ACPI device */
  61. struct input_dev *idev; /* input device */
  62. struct task_struct *kthread; /* kthread for input */
  63. struct mutex lock;
  64. struct platform_device *pdev; /* platform device */
  65. atomic_t count; /* interrupt count after last read */
  66. int xcalib; /* calibrated null value for x */
  67. int ycalib; /* calibrated null value for y */
  68. int zcalib; /* calibrated null value for z */
  69. unsigned char is_on; /* whether the device is on or off */
  70. unsigned char usage; /* usage counter */
  71. struct axis_conversion ac; /* hw -> logical axis */
  72. };
  73. static struct acpi_lis3lv02d adev;
  74. static int lis3lv02d_remove_fs(void);
  75. static int lis3lv02d_add_fs(struct acpi_device *device);
  76. /* For automatic insertion of the module */
  77. static struct acpi_device_id lis3lv02d_device_ids[] = {
  78. {"HPQ0004", 0}, /* HP Mobile Data Protection System PNP */
  79. {"", 0},
  80. };
  81. MODULE_DEVICE_TABLE(acpi, lis3lv02d_device_ids);
  82. /**
  83. * lis3lv02d_acpi_init - ACPI _INI method: initialize the device.
  84. * @handle: the handle of the device
  85. *
  86. * Returns AE_OK on success.
  87. */
  88. static inline acpi_status lis3lv02d_acpi_init(acpi_handle handle)
  89. {
  90. return acpi_evaluate_object(handle, METHOD_NAME__INI, NULL, NULL);
  91. }
  92. /**
  93. * lis3lv02d_acpi_read - ACPI ALRD method: read a register
  94. * @handle: the handle of the device
  95. * @reg: the register to read
  96. * @ret: result of the operation
  97. *
  98. * Returns AE_OK on success.
  99. */
  100. static acpi_status lis3lv02d_acpi_read(acpi_handle handle, int reg, u8 *ret)
  101. {
  102. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  103. struct acpi_object_list args = { 1, &arg0 };
  104. unsigned long long lret;
  105. acpi_status status;
  106. arg0.integer.value = reg;
  107. status = acpi_evaluate_integer(handle, "ALRD", &args, &lret);
  108. *ret = lret;
  109. return status;
  110. }
  111. /**
  112. * lis3lv02d_acpi_write - ACPI ALWR method: write to a register
  113. * @handle: the handle of the device
  114. * @reg: the register to write to
  115. * @val: the value to write
  116. *
  117. * Returns AE_OK on success.
  118. */
  119. static acpi_status lis3lv02d_acpi_write(acpi_handle handle, int reg, u8 val)
  120. {
  121. unsigned long long ret; /* Not used when writting */
  122. union acpi_object in_obj[2];
  123. struct acpi_object_list args = { 2, in_obj };
  124. in_obj[0].type = ACPI_TYPE_INTEGER;
  125. in_obj[0].integer.value = reg;
  126. in_obj[1].type = ACPI_TYPE_INTEGER;
  127. in_obj[1].integer.value = val;
  128. return acpi_evaluate_integer(handle, "ALWR", &args, &ret);
  129. }
  130. static s16 lis3lv02d_read_16(acpi_handle handle, int reg)
  131. {
  132. u8 lo, hi;
  133. lis3lv02d_acpi_read(handle, reg, &lo);
  134. lis3lv02d_acpi_read(handle, reg + 1, &hi);
  135. /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
  136. return (s16)((hi << 8) | lo);
  137. }
  138. /**
  139. * lis3lv02d_get_axis - For the given axis, give the value converted
  140. * @axis: 1,2,3 - can also be negative
  141. * @hw_values: raw values returned by the hardware
  142. *
  143. * Returns the converted value.
  144. */
  145. static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
  146. {
  147. if (axis > 0)
  148. return hw_values[axis - 1];
  149. else
  150. return -hw_values[-axis - 1];
  151. }
  152. /**
  153. * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
  154. * @handle: the handle to the device
  155. * @x: where to store the X axis value
  156. * @y: where to store the Y axis value
  157. * @z: where to store the Z axis value
  158. *
  159. * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
  160. */
  161. static void lis3lv02d_get_xyz(acpi_handle handle, int *x, int *y, int *z)
  162. {
  163. int position[3];
  164. position[0] = lis3lv02d_read_16(handle, OUTX_L);
  165. position[1] = lis3lv02d_read_16(handle, OUTY_L);
  166. position[2] = lis3lv02d_read_16(handle, OUTZ_L);
  167. *x = lis3lv02d_get_axis(adev.ac.x, position);
  168. *y = lis3lv02d_get_axis(adev.ac.y, position);
  169. *z = lis3lv02d_get_axis(adev.ac.z, position);
  170. }
  171. static inline void lis3lv02d_poweroff(acpi_handle handle)
  172. {
  173. adev.is_on = 0;
  174. /* disable X,Y,Z axis and power down */
  175. lis3lv02d_acpi_write(handle, CTRL_REG1, 0x00);
  176. }
  177. static void lis3lv02d_poweron(acpi_handle handle)
  178. {
  179. u8 val;
  180. adev.is_on = 1;
  181. lis3lv02d_acpi_init(handle);
  182. lis3lv02d_acpi_write(handle, FF_WU_CFG, 0);
  183. /*
  184. * BDU: LSB and MSB values are not updated until both have been read.
  185. * So the value read will always be correct.
  186. * IEN: Interrupt for free-fall and DD, not for data-ready.
  187. */
  188. lis3lv02d_acpi_read(handle, CTRL_REG2, &val);
  189. val |= CTRL2_BDU | CTRL2_IEN;
  190. lis3lv02d_acpi_write(handle, CTRL_REG2, val);
  191. }
  192. #ifdef CONFIG_PM
  193. static int lis3lv02d_suspend(struct acpi_device *device, pm_message_t state)
  194. {
  195. /* make sure the device is off when we suspend */
  196. lis3lv02d_poweroff(device->handle);
  197. return 0;
  198. }
  199. static int lis3lv02d_resume(struct acpi_device *device)
  200. {
  201. /* put back the device in the right state (ACPI might turn it on) */
  202. mutex_lock(&adev.lock);
  203. if (adev.usage > 0)
  204. lis3lv02d_poweron(device->handle);
  205. else
  206. lis3lv02d_poweroff(device->handle);
  207. mutex_unlock(&adev.lock);
  208. return 0;
  209. }
  210. #else
  211. #define lis3lv02d_suspend NULL
  212. #define lis3lv02d_resume NULL
  213. #endif
  214. /*
  215. * To be called before starting to use the device. It makes sure that the
  216. * device will always be on until a call to lis3lv02d_decrease_use(). Not to be
  217. * used from interrupt context.
  218. */
  219. static void lis3lv02d_increase_use(struct acpi_lis3lv02d *dev)
  220. {
  221. mutex_lock(&dev->lock);
  222. dev->usage++;
  223. if (dev->usage == 1) {
  224. if (!dev->is_on)
  225. lis3lv02d_poweron(dev->device->handle);
  226. }
  227. mutex_unlock(&dev->lock);
  228. }
  229. /*
  230. * To be called whenever a usage of the device is stopped.
  231. * It will make sure to turn off the device when there is not usage.
  232. */
  233. static void lis3lv02d_decrease_use(struct acpi_lis3lv02d *dev)
  234. {
  235. mutex_lock(&dev->lock);
  236. dev->usage--;
  237. if (dev->usage == 0)
  238. lis3lv02d_poweroff(dev->device->handle);
  239. mutex_unlock(&dev->lock);
  240. }
  241. /**
  242. * lis3lv02d_joystick_kthread - Kthread polling function
  243. * @data: unused - here to conform to threadfn prototype
  244. */
  245. static int lis3lv02d_joystick_kthread(void *data)
  246. {
  247. int x, y, z;
  248. while (!kthread_should_stop()) {
  249. lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
  250. input_report_abs(adev.idev, ABS_X, x - adev.xcalib);
  251. input_report_abs(adev.idev, ABS_Y, y - adev.ycalib);
  252. input_report_abs(adev.idev, ABS_Z, z - adev.zcalib);
  253. input_sync(adev.idev);
  254. try_to_freeze();
  255. msleep_interruptible(MDPS_POLL_INTERVAL);
  256. }
  257. return 0;
  258. }
  259. static int lis3lv02d_joystick_open(struct input_dev *input)
  260. {
  261. lis3lv02d_increase_use(&adev);
  262. adev.kthread = kthread_run(lis3lv02d_joystick_kthread, NULL, "klis3lv02d");
  263. if (IS_ERR(adev.kthread)) {
  264. lis3lv02d_decrease_use(&adev);
  265. return PTR_ERR(adev.kthread);
  266. }
  267. return 0;
  268. }
  269. static void lis3lv02d_joystick_close(struct input_dev *input)
  270. {
  271. kthread_stop(adev.kthread);
  272. lis3lv02d_decrease_use(&adev);
  273. }
  274. static inline void lis3lv02d_calibrate_joystick(void)
  275. {
  276. lis3lv02d_get_xyz(adev.device->handle, &adev.xcalib, &adev.ycalib, &adev.zcalib);
  277. }
  278. static int lis3lv02d_joystick_enable(void)
  279. {
  280. int err;
  281. if (adev.idev)
  282. return -EINVAL;
  283. adev.idev = input_allocate_device();
  284. if (!adev.idev)
  285. return -ENOMEM;
  286. lis3lv02d_calibrate_joystick();
  287. adev.idev->name = "ST LIS3LV02DL Accelerometer";
  288. adev.idev->phys = DRIVER_NAME "/input0";
  289. adev.idev->id.bustype = BUS_HOST;
  290. adev.idev->id.vendor = 0;
  291. adev.idev->dev.parent = &adev.pdev->dev;
  292. adev.idev->open = lis3lv02d_joystick_open;
  293. adev.idev->close = lis3lv02d_joystick_close;
  294. set_bit(EV_ABS, adev.idev->evbit);
  295. input_set_abs_params(adev.idev, ABS_X, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
  296. input_set_abs_params(adev.idev, ABS_Y, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
  297. input_set_abs_params(adev.idev, ABS_Z, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
  298. err = input_register_device(adev.idev);
  299. if (err) {
  300. input_free_device(adev.idev);
  301. adev.idev = NULL;
  302. }
  303. return err;
  304. }
  305. static void lis3lv02d_joystick_disable(void)
  306. {
  307. if (!adev.idev)
  308. return;
  309. input_unregister_device(adev.idev);
  310. adev.idev = NULL;
  311. }
  312. /*
  313. * Initialise the accelerometer and the various subsystems.
  314. * Should be rather independant of the bus system.
  315. */
  316. static int lis3lv02d_init_device(struct acpi_lis3lv02d *dev)
  317. {
  318. mutex_init(&dev->lock);
  319. lis3lv02d_add_fs(dev->device);
  320. lis3lv02d_increase_use(dev);
  321. if (lis3lv02d_joystick_enable())
  322. printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
  323. lis3lv02d_decrease_use(dev);
  324. return 0;
  325. }
  326. static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
  327. {
  328. adev.ac = *((struct axis_conversion *)dmi->driver_data);
  329. printk(KERN_INFO DRIVER_NAME ": hardware type %s found.\n", dmi->ident);
  330. return 1;
  331. }
  332. /* Represents, for each axis seen by userspace, the corresponding hw axis (+1).
  333. * If the value is negative, the opposite of the hw value is used. */
  334. static struct axis_conversion lis3lv02d_axis_normal = {1, 2, 3};
  335. static struct axis_conversion lis3lv02d_axis_y_inverted = {1, -2, 3};
  336. static struct axis_conversion lis3lv02d_axis_x_inverted = {-1, 2, 3};
  337. static struct axis_conversion lis3lv02d_axis_z_inverted = {1, 2, -3};
  338. static struct axis_conversion lis3lv02d_axis_xy_rotated_left = {-2, 1, 3};
  339. static struct axis_conversion lis3lv02d_axis_xy_swap_inverted = {-2, -1, 3};
  340. #define AXIS_DMI_MATCH(_ident, _name, _axis) { \
  341. .ident = _ident, \
  342. .callback = lis3lv02d_dmi_matched, \
  343. .matches = { \
  344. DMI_MATCH(DMI_PRODUCT_NAME, _name) \
  345. }, \
  346. .driver_data = &lis3lv02d_axis_##_axis \
  347. }
  348. static struct dmi_system_id lis3lv02d_dmi_ids[] = {
  349. /* product names are truncated to match all kinds of a same model */
  350. AXIS_DMI_MATCH("NC64x0", "HP Compaq nc64", x_inverted),
  351. AXIS_DMI_MATCH("NC84x0", "HP Compaq nc84", z_inverted),
  352. AXIS_DMI_MATCH("NX9420", "HP Compaq nx9420", x_inverted),
  353. AXIS_DMI_MATCH("NW9440", "HP Compaq nw9440", x_inverted),
  354. AXIS_DMI_MATCH("NC2510", "HP Compaq 2510", y_inverted),
  355. AXIS_DMI_MATCH("NC8510", "HP Compaq 8510", xy_swap_inverted),
  356. AXIS_DMI_MATCH("HP2133", "HP 2133", xy_rotated_left),
  357. { NULL, }
  358. /* Laptop models without axis info (yet):
  359. * "NC651xx" "HP Compaq 651"
  360. * "NC671xx" "HP Compaq 671"
  361. * "NC6910" "HP Compaq 6910"
  362. * HP Compaq 8710x Notebook PC / Mobile Workstation
  363. * "NC2400" "HP Compaq nc2400"
  364. * "NX74x0" "HP Compaq nx74"
  365. * "NX6325" "HP Compaq nx6325"
  366. * "NC4400" "HP Compaq nc4400"
  367. */
  368. };
  369. static int lis3lv02d_add(struct acpi_device *device)
  370. {
  371. u8 val;
  372. if (!device)
  373. return -EINVAL;
  374. adev.device = device;
  375. strcpy(acpi_device_name(device), DRIVER_NAME);
  376. strcpy(acpi_device_class(device), ACPI_MDPS_CLASS);
  377. device->driver_data = &adev;
  378. lis3lv02d_acpi_read(device->handle, WHO_AM_I, &val);
  379. if ((val != LIS3LV02DL_ID) && (val != LIS302DL_ID)) {
  380. printk(KERN_ERR DRIVER_NAME
  381. ": Accelerometer chip not LIS3LV02D{L,Q}\n");
  382. }
  383. /* If possible use a "standard" axes order */
  384. if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
  385. printk(KERN_INFO DRIVER_NAME ": laptop model unknown, "
  386. "using default axes configuration\n");
  387. adev.ac = lis3lv02d_axis_normal;
  388. }
  389. return lis3lv02d_init_device(&adev);
  390. }
  391. static int lis3lv02d_remove(struct acpi_device *device, int type)
  392. {
  393. if (!device)
  394. return -EINVAL;
  395. lis3lv02d_joystick_disable();
  396. lis3lv02d_poweroff(device->handle);
  397. return lis3lv02d_remove_fs();
  398. }
  399. /* Sysfs stuff */
  400. static ssize_t lis3lv02d_position_show(struct device *dev,
  401. struct device_attribute *attr, char *buf)
  402. {
  403. int x, y, z;
  404. lis3lv02d_increase_use(&adev);
  405. lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
  406. lis3lv02d_decrease_use(&adev);
  407. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  408. }
  409. static ssize_t lis3lv02d_calibrate_show(struct device *dev,
  410. struct device_attribute *attr, char *buf)
  411. {
  412. return sprintf(buf, "(%d,%d,%d)\n", adev.xcalib, adev.ycalib, adev.zcalib);
  413. }
  414. static ssize_t lis3lv02d_calibrate_store(struct device *dev,
  415. struct device_attribute *attr,
  416. const char *buf, size_t count)
  417. {
  418. lis3lv02d_increase_use(&adev);
  419. lis3lv02d_calibrate_joystick();
  420. lis3lv02d_decrease_use(&adev);
  421. return count;
  422. }
  423. /* conversion btw sampling rate and the register values */
  424. static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
  425. static ssize_t lis3lv02d_rate_show(struct device *dev,
  426. struct device_attribute *attr, char *buf)
  427. {
  428. u8 ctrl;
  429. int val;
  430. lis3lv02d_increase_use(&adev);
  431. lis3lv02d_acpi_read(adev.device->handle, CTRL_REG1, &ctrl);
  432. lis3lv02d_decrease_use(&adev);
  433. val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
  434. return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
  435. }
  436. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  437. static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
  438. lis3lv02d_calibrate_store);
  439. static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
  440. static struct attribute *lis3lv02d_attributes[] = {
  441. &dev_attr_position.attr,
  442. &dev_attr_calibrate.attr,
  443. &dev_attr_rate.attr,
  444. NULL
  445. };
  446. static struct attribute_group lis3lv02d_attribute_group = {
  447. .attrs = lis3lv02d_attributes
  448. };
  449. static int lis3lv02d_add_fs(struct acpi_device *device)
  450. {
  451. adev.pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  452. if (IS_ERR(adev.pdev))
  453. return PTR_ERR(adev.pdev);
  454. return sysfs_create_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
  455. }
  456. static int lis3lv02d_remove_fs(void)
  457. {
  458. sysfs_remove_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
  459. platform_device_unregister(adev.pdev);
  460. return 0;
  461. }
  462. /* For the HP MDPS aka 3D Driveguard */
  463. static struct acpi_driver lis3lv02d_driver = {
  464. .name = DRIVER_NAME,
  465. .class = ACPI_MDPS_CLASS,
  466. .ids = lis3lv02d_device_ids,
  467. .ops = {
  468. .add = lis3lv02d_add,
  469. .remove = lis3lv02d_remove,
  470. .suspend = lis3lv02d_suspend,
  471. .resume = lis3lv02d_resume,
  472. }
  473. };
  474. static int __init lis3lv02d_init_module(void)
  475. {
  476. int ret;
  477. if (acpi_disabled)
  478. return -ENODEV;
  479. ret = acpi_bus_register_driver(&lis3lv02d_driver);
  480. if (ret < 0)
  481. return ret;
  482. printk(KERN_INFO DRIVER_NAME " driver loaded.\n");
  483. return 0;
  484. }
  485. static void __exit lis3lv02d_exit_module(void)
  486. {
  487. acpi_bus_unregister_driver(&lis3lv02d_driver);
  488. }
  489. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  490. MODULE_AUTHOR("Yan Burman and Eric Piel");
  491. MODULE_LICENSE("GPL");
  492. module_init(lis3lv02d_init_module);
  493. module_exit(lis3lv02d_exit_module);