hp_accel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * hp_accel.c - Interface between LIS3LV02DL driver and HP ACPI BIOS
  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.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/version.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/leds.h>
  39. #include <acpi/acpi_drivers.h>
  40. #include <asm/atomic.h>
  41. #include "lis3lv02d.h"
  42. #define DRIVER_NAME "lis3lv02d"
  43. #define ACPI_MDPS_CLASS "accelerometer"
  44. /* Delayed LEDs infrastructure ------------------------------------ */
  45. /* Special LED class that can defer work */
  46. struct delayed_led_classdev {
  47. struct led_classdev led_classdev;
  48. struct work_struct work;
  49. enum led_brightness new_brightness;
  50. unsigned int led; /* For driver */
  51. void (*set_brightness)(struct delayed_led_classdev *data, enum led_brightness value);
  52. };
  53. static inline void delayed_set_status_worker(struct work_struct *work)
  54. {
  55. struct delayed_led_classdev *data =
  56. container_of(work, struct delayed_led_classdev, work);
  57. data->set_brightness(data, data->new_brightness);
  58. }
  59. static inline void delayed_sysfs_set(struct led_classdev *led_cdev,
  60. enum led_brightness brightness)
  61. {
  62. struct delayed_led_classdev *data = container_of(led_cdev,
  63. struct delayed_led_classdev, led_classdev);
  64. data->new_brightness = brightness;
  65. schedule_work(&data->work);
  66. }
  67. /* HP-specific accelerometer driver ------------------------------------ */
  68. /* For automatic insertion of the module */
  69. static struct acpi_device_id lis3lv02d_device_ids[] = {
  70. {"HPQ0004", 0}, /* HP Mobile Data Protection System PNP */
  71. {"", 0},
  72. };
  73. MODULE_DEVICE_TABLE(acpi, lis3lv02d_device_ids);
  74. /**
  75. * lis3lv02d_acpi_init - ACPI _INI method: initialize the device.
  76. * @handle: the handle of the device
  77. *
  78. * Returns AE_OK on success.
  79. */
  80. acpi_status lis3lv02d_acpi_init(acpi_handle handle)
  81. {
  82. return acpi_evaluate_object(handle, METHOD_NAME__INI, NULL, NULL);
  83. }
  84. /**
  85. * lis3lv02d_acpi_read - ACPI ALRD method: read a register
  86. * @handle: the handle of the device
  87. * @reg: the register to read
  88. * @ret: result of the operation
  89. *
  90. * Returns AE_OK on success.
  91. */
  92. acpi_status lis3lv02d_acpi_read(acpi_handle handle, int reg, u8 *ret)
  93. {
  94. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  95. struct acpi_object_list args = { 1, &arg0 };
  96. unsigned long long lret;
  97. acpi_status status;
  98. arg0.integer.value = reg;
  99. status = acpi_evaluate_integer(handle, "ALRD", &args, &lret);
  100. *ret = lret;
  101. return status;
  102. }
  103. /**
  104. * lis3lv02d_acpi_write - ACPI ALWR method: write to a register
  105. * @handle: the handle of the device
  106. * @reg: the register to write to
  107. * @val: the value to write
  108. *
  109. * Returns AE_OK on success.
  110. */
  111. acpi_status lis3lv02d_acpi_write(acpi_handle handle, int reg, u8 val)
  112. {
  113. unsigned long long ret; /* Not used when writting */
  114. union acpi_object in_obj[2];
  115. struct acpi_object_list args = { 2, in_obj };
  116. in_obj[0].type = ACPI_TYPE_INTEGER;
  117. in_obj[0].integer.value = reg;
  118. in_obj[1].type = ACPI_TYPE_INTEGER;
  119. in_obj[1].integer.value = val;
  120. return acpi_evaluate_integer(handle, "ALWR", &args, &ret);
  121. }
  122. static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
  123. {
  124. adev.ac = *((struct axis_conversion *)dmi->driver_data);
  125. printk(KERN_INFO DRIVER_NAME ": hardware type %s found.\n", dmi->ident);
  126. return 1;
  127. }
  128. /* Represents, for each axis seen by userspace, the corresponding hw axis (+1).
  129. * If the value is negative, the opposite of the hw value is used. */
  130. static struct axis_conversion lis3lv02d_axis_normal = {1, 2, 3};
  131. static struct axis_conversion lis3lv02d_axis_y_inverted = {1, -2, 3};
  132. static struct axis_conversion lis3lv02d_axis_x_inverted = {-1, 2, 3};
  133. static struct axis_conversion lis3lv02d_axis_z_inverted = {1, 2, -3};
  134. static struct axis_conversion lis3lv02d_axis_xy_rotated_left = {-2, 1, 3};
  135. static struct axis_conversion lis3lv02d_axis_xy_rotated_left_usd = {-2, 1, -3};
  136. static struct axis_conversion lis3lv02d_axis_xy_swap_inverted = {-2, -1, 3};
  137. static struct axis_conversion lis3lv02d_axis_xy_rotated_right = {2, -1, 3};
  138. static struct axis_conversion lis3lv02d_axis_xy_swap_yz_inverted = {2, -1, -3};
  139. #define AXIS_DMI_MATCH(_ident, _name, _axis) { \
  140. .ident = _ident, \
  141. .callback = lis3lv02d_dmi_matched, \
  142. .matches = { \
  143. DMI_MATCH(DMI_PRODUCT_NAME, _name) \
  144. }, \
  145. .driver_data = &lis3lv02d_axis_##_axis \
  146. }
  147. #define AXIS_DMI_MATCH2(_ident, _class1, _name1, \
  148. _class2, _name2, \
  149. _axis) { \
  150. .ident = _ident, \
  151. .callback = lis3lv02d_dmi_matched, \
  152. .matches = { \
  153. DMI_MATCH(DMI_##_class1, _name1), \
  154. DMI_MATCH(DMI_##_class2, _name2), \
  155. }, \
  156. .driver_data = &lis3lv02d_axis_##_axis \
  157. }
  158. static struct dmi_system_id lis3lv02d_dmi_ids[] = {
  159. /* product names are truncated to match all kinds of a same model */
  160. AXIS_DMI_MATCH("NC64x0", "HP Compaq nc64", x_inverted),
  161. AXIS_DMI_MATCH("NC84x0", "HP Compaq nc84", z_inverted),
  162. AXIS_DMI_MATCH("NX9420", "HP Compaq nx9420", x_inverted),
  163. AXIS_DMI_MATCH("NW9440", "HP Compaq nw9440", x_inverted),
  164. AXIS_DMI_MATCH("NC2510", "HP Compaq 2510", y_inverted),
  165. AXIS_DMI_MATCH("NC8510", "HP Compaq 8510", xy_swap_inverted),
  166. AXIS_DMI_MATCH("HP2133", "HP 2133", xy_rotated_left),
  167. AXIS_DMI_MATCH("NC653x", "HP Compaq 653", xy_rotated_left_usd),
  168. AXIS_DMI_MATCH("NC673x", "HP Compaq 673", xy_rotated_left_usd),
  169. AXIS_DMI_MATCH("NC651xx", "HP Compaq 651", xy_rotated_right),
  170. AXIS_DMI_MATCH("NC671xx", "HP Compaq 671", xy_swap_yz_inverted),
  171. /* Intel-based HP Pavilion dv5 */
  172. AXIS_DMI_MATCH2("HPDV5_I",
  173. PRODUCT_NAME, "HP Pavilion dv5",
  174. BOARD_NAME, "3603",
  175. x_inverted),
  176. /* AMD-based HP Pavilion dv5 */
  177. AXIS_DMI_MATCH2("HPDV5_A",
  178. PRODUCT_NAME, "HP Pavilion dv5",
  179. BOARD_NAME, "3600",
  180. y_inverted),
  181. { NULL, }
  182. /* Laptop models without axis info (yet):
  183. * "NC6910" "HP Compaq 6910"
  184. * HP Compaq 8710x Notebook PC / Mobile Workstation
  185. * "NC2400" "HP Compaq nc2400"
  186. * "NX74x0" "HP Compaq nx74"
  187. * "NX6325" "HP Compaq nx6325"
  188. * "NC4400" "HP Compaq nc4400"
  189. */
  190. };
  191. static void hpled_set(struct delayed_led_classdev *led_cdev, enum led_brightness value)
  192. {
  193. acpi_handle handle = adev.device->handle;
  194. unsigned long long ret; /* Not used when writing */
  195. union acpi_object in_obj[1];
  196. struct acpi_object_list args = { 1, in_obj };
  197. in_obj[0].type = ACPI_TYPE_INTEGER;
  198. in_obj[0].integer.value = !!value;
  199. acpi_evaluate_integer(handle, "ALED", &args, &ret);
  200. }
  201. static struct delayed_led_classdev hpled_led = {
  202. .led_classdev = {
  203. .name = "hp::hddprotect",
  204. .default_trigger = "none",
  205. .brightness_set = delayed_sysfs_set,
  206. .flags = LED_CORE_SUSPENDRESUME,
  207. },
  208. .set_brightness = hpled_set,
  209. };
  210. static acpi_status
  211. lis3lv02d_get_resource(struct acpi_resource *resource, void *context)
  212. {
  213. if (resource->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  214. struct acpi_resource_extended_irq *irq;
  215. u32 *device_irq = context;
  216. irq = &resource->data.extended_irq;
  217. *device_irq = irq->interrupts[0];
  218. }
  219. return AE_OK;
  220. }
  221. static void lis3lv02d_enum_resources(struct acpi_device *device)
  222. {
  223. acpi_status status;
  224. status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  225. lis3lv02d_get_resource, &adev.irq);
  226. if (ACPI_FAILURE(status))
  227. printk(KERN_DEBUG DRIVER_NAME ": Error getting resources\n");
  228. }
  229. static s16 lis3lv02d_read_16(acpi_handle handle, int reg)
  230. {
  231. u8 lo, hi;
  232. adev.read(handle, reg - 1, &lo);
  233. adev.read(handle, reg, &hi);
  234. /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
  235. return (s16)((hi << 8) | lo);
  236. }
  237. static s16 lis3lv02d_read_8(acpi_handle handle, int reg)
  238. {
  239. s8 lo;
  240. adev.read(handle, reg, &lo);
  241. return lo;
  242. }
  243. static int lis3lv02d_add(struct acpi_device *device)
  244. {
  245. int ret;
  246. if (!device)
  247. return -EINVAL;
  248. adev.device = device;
  249. adev.init = lis3lv02d_acpi_init;
  250. adev.read = lis3lv02d_acpi_read;
  251. adev.write = lis3lv02d_acpi_write;
  252. strcpy(acpi_device_name(device), DRIVER_NAME);
  253. strcpy(acpi_device_class(device), ACPI_MDPS_CLASS);
  254. device->driver_data = &adev;
  255. lis3lv02d_acpi_read(device->handle, WHO_AM_I, &adev.whoami);
  256. switch (adev.whoami) {
  257. case LIS_DOUBLE_ID:
  258. printk(KERN_INFO DRIVER_NAME ": 2-byte sensor found\n");
  259. adev.read_data = lis3lv02d_read_16;
  260. adev.mdps_max_val = 2048;
  261. break;
  262. case LIS_SINGLE_ID:
  263. printk(KERN_INFO DRIVER_NAME ": 1-byte sensor found\n");
  264. adev.read_data = lis3lv02d_read_8;
  265. adev.mdps_max_val = 128;
  266. break;
  267. default:
  268. printk(KERN_ERR DRIVER_NAME
  269. ": unknown sensor type 0x%X\n", adev.whoami);
  270. return -EINVAL;
  271. }
  272. /* If possible use a "standard" axes order */
  273. if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
  274. printk(KERN_INFO DRIVER_NAME ": laptop model unknown, "
  275. "using default axes configuration\n");
  276. adev.ac = lis3lv02d_axis_normal;
  277. }
  278. INIT_WORK(&hpled_led.work, delayed_set_status_worker);
  279. ret = led_classdev_register(NULL, &hpled_led.led_classdev);
  280. if (ret)
  281. return ret;
  282. /* obtain IRQ number of our device from ACPI */
  283. lis3lv02d_enum_resources(adev.device);
  284. ret = lis3lv02d_init_device(&adev);
  285. if (ret) {
  286. flush_work(&hpled_led.work);
  287. led_classdev_unregister(&hpled_led.led_classdev);
  288. return ret;
  289. }
  290. return ret;
  291. }
  292. static int lis3lv02d_remove(struct acpi_device *device, int type)
  293. {
  294. if (!device)
  295. return -EINVAL;
  296. lis3lv02d_joystick_disable();
  297. lis3lv02d_poweroff(device->handle);
  298. flush_work(&hpled_led.work);
  299. led_classdev_unregister(&hpled_led.led_classdev);
  300. return lis3lv02d_remove_fs();
  301. }
  302. #ifdef CONFIG_PM
  303. static int lis3lv02d_suspend(struct acpi_device *device, pm_message_t state)
  304. {
  305. /* make sure the device is off when we suspend */
  306. lis3lv02d_poweroff(device->handle);
  307. return 0;
  308. }
  309. static int lis3lv02d_resume(struct acpi_device *device)
  310. {
  311. /* put back the device in the right state (ACPI might turn it on) */
  312. mutex_lock(&adev.lock);
  313. if (adev.usage > 0)
  314. lis3lv02d_poweron(device->handle);
  315. else
  316. lis3lv02d_poweroff(device->handle);
  317. mutex_unlock(&adev.lock);
  318. return 0;
  319. }
  320. #else
  321. #define lis3lv02d_suspend NULL
  322. #define lis3lv02d_resume NULL
  323. #endif
  324. /* For the HP MDPS aka 3D Driveguard */
  325. static struct acpi_driver lis3lv02d_driver = {
  326. .name = DRIVER_NAME,
  327. .class = ACPI_MDPS_CLASS,
  328. .ids = lis3lv02d_device_ids,
  329. .ops = {
  330. .add = lis3lv02d_add,
  331. .remove = lis3lv02d_remove,
  332. .suspend = lis3lv02d_suspend,
  333. .resume = lis3lv02d_resume,
  334. }
  335. };
  336. static int __init lis3lv02d_init_module(void)
  337. {
  338. int ret;
  339. if (acpi_disabled)
  340. return -ENODEV;
  341. ret = acpi_bus_register_driver(&lis3lv02d_driver);
  342. if (ret < 0)
  343. return ret;
  344. printk(KERN_INFO DRIVER_NAME " driver loaded.\n");
  345. return 0;
  346. }
  347. static void __exit lis3lv02d_exit_module(void)
  348. {
  349. acpi_bus_unregister_driver(&lis3lv02d_driver);
  350. }
  351. MODULE_DESCRIPTION("Glue between LIS3LV02Dx and HP ACPI BIOS and support for disk protection LED.");
  352. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  353. MODULE_LICENSE("GPL");
  354. module_init(lis3lv02d_init_module);
  355. module_exit(lis3lv02d_exit_module);