lis3lv02d.c 16 KB

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