hp_accel.c 11 KB

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