hp_accel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * hp_accel.c - Interface between LIS3LV02DL driver and HP ACPI BIOS
  4. *
  5. * Copyright (C) 2007-2008 Yan Burman
  6. * Copyright (C) 2008 Eric Piel
  7. * Copyright (C) 2008-2009 Pavel Machek
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/dmi.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <linux/wait.h>
  19. #include <linux/poll.h>
  20. #include <linux/freezer.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/leds.h>
  23. #include <linux/atomic.h>
  24. #include <linux/acpi.h>
  25. #include <linux/i8042.h>
  26. #include <linux/serio.h>
  27. #include "../../../misc/lis3lv02d/lis3lv02d.h"
  28. /* Delayed LEDs infrastructure ------------------------------------ */
  29. /* Special LED class that can defer work */
  30. struct delayed_led_classdev {
  31. struct led_classdev led_classdev;
  32. struct work_struct work;
  33. enum led_brightness new_brightness;
  34. unsigned int led; /* For driver */
  35. void (*set_brightness)(struct delayed_led_classdev *data, enum led_brightness value);
  36. };
  37. static inline void delayed_set_status_worker(struct work_struct *work)
  38. {
  39. struct delayed_led_classdev *data =
  40. container_of(work, struct delayed_led_classdev, work);
  41. data->set_brightness(data, data->new_brightness);
  42. }
  43. static inline void delayed_sysfs_set(struct led_classdev *led_cdev,
  44. enum led_brightness brightness)
  45. {
  46. struct delayed_led_classdev *data = container_of(led_cdev,
  47. struct delayed_led_classdev, led_classdev);
  48. data->new_brightness = brightness;
  49. schedule_work(&data->work);
  50. }
  51. /* HP-specific accelerometer driver ------------------------------------ */
  52. /* e0 25, e0 26, e0 27, e0 28 are scan codes that the accelerometer with acpi id
  53. * HPQ6000 sends through the keyboard bus */
  54. #define ACCEL_1 0x25
  55. #define ACCEL_2 0x26
  56. #define ACCEL_3 0x27
  57. #define ACCEL_4 0x28
  58. /* For automatic insertion of the module */
  59. static const struct acpi_device_id lis3lv02d_device_ids[] = {
  60. {"HPQ0004", 0}, /* HP Mobile Data Protection System PNP */
  61. {"HPQ6000", 0}, /* HP Mobile Data Protection System PNP */
  62. {"HPQ6007", 0}, /* HP Mobile Data Protection System PNP */
  63. {"", 0},
  64. };
  65. MODULE_DEVICE_TABLE(acpi, lis3lv02d_device_ids);
  66. /**
  67. * lis3lv02d_acpi_init - initialize the device for ACPI
  68. * @lis3: pointer to the device struct
  69. *
  70. * Returns 0 on success.
  71. */
  72. static int lis3lv02d_acpi_init(struct lis3lv02d *lis3)
  73. {
  74. return 0;
  75. }
  76. /**
  77. * lis3lv02d_acpi_read - ACPI ALRD method: read a register
  78. * @lis3: pointer to the device struct
  79. * @reg: the register to read
  80. * @ret: result of the operation
  81. *
  82. * Returns 0 on success.
  83. */
  84. static int lis3lv02d_acpi_read(struct lis3lv02d *lis3, int reg, u8 *ret)
  85. {
  86. struct acpi_device *dev = lis3->bus_priv;
  87. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  88. struct acpi_object_list args = { 1, &arg0 };
  89. unsigned long long lret;
  90. acpi_status status;
  91. arg0.integer.value = reg;
  92. status = acpi_evaluate_integer(dev->handle, "ALRD", &args, &lret);
  93. if (ACPI_FAILURE(status))
  94. return -EINVAL;
  95. *ret = lret;
  96. return 0;
  97. }
  98. /**
  99. * lis3lv02d_acpi_write - ACPI ALWR method: write to a register
  100. * @lis3: pointer to the device struct
  101. * @reg: the register to write to
  102. * @val: the value to write
  103. *
  104. * Returns 0 on success.
  105. */
  106. static int lis3lv02d_acpi_write(struct lis3lv02d *lis3, int reg, u8 val)
  107. {
  108. struct acpi_device *dev = lis3->bus_priv;
  109. unsigned long long ret; /* Not used when writting */
  110. union acpi_object in_obj[2];
  111. struct acpi_object_list args = { 2, in_obj };
  112. in_obj[0].type = ACPI_TYPE_INTEGER;
  113. in_obj[0].integer.value = reg;
  114. in_obj[1].type = ACPI_TYPE_INTEGER;
  115. in_obj[1].integer.value = val;
  116. if (acpi_evaluate_integer(dev->handle, "ALWR", &args, &ret) != AE_OK)
  117. return -EINVAL;
  118. return 0;
  119. }
  120. static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
  121. {
  122. lis3_dev.ac = *((union axis_conversion *)dmi->driver_data);
  123. pr_info("hardware type %s found\n", dmi->ident);
  124. return 1;
  125. }
  126. /* Represents, for each axis seen by userspace, the corresponding hw axis (+1).
  127. * If the value is negative, the opposite of the hw value is used. */
  128. #define DEFINE_CONV(name, x, y, z) \
  129. static union axis_conversion lis3lv02d_axis_##name = \
  130. { .as_array = { x, y, z } }
  131. DEFINE_CONV(normal, 1, 2, 3);
  132. DEFINE_CONV(y_inverted, 1, -2, 3);
  133. DEFINE_CONV(x_inverted, -1, 2, 3);
  134. DEFINE_CONV(x_inverted_usd, -1, 2, -3);
  135. DEFINE_CONV(z_inverted, 1, 2, -3);
  136. DEFINE_CONV(xy_swap, 2, 1, 3);
  137. DEFINE_CONV(xy_rotated_left, -2, 1, 3);
  138. DEFINE_CONV(xy_rotated_left_usd, -2, 1, -3);
  139. DEFINE_CONV(xy_swap_inverted, -2, -1, 3);
  140. DEFINE_CONV(xy_rotated_right, 2, -1, 3);
  141. DEFINE_CONV(xy_swap_yz_inverted, 2, -1, -3);
  142. #define AXIS_DMI_MATCH(_ident, _name, _axis) { \
  143. .ident = _ident, \
  144. .callback = lis3lv02d_dmi_matched, \
  145. .matches = { \
  146. DMI_MATCH(DMI_PRODUCT_NAME, _name) \
  147. }, \
  148. .driver_data = &lis3lv02d_axis_##_axis \
  149. }
  150. #define AXIS_DMI_MATCH2(_ident, _class1, _name1, \
  151. _class2, _name2, \
  152. _axis) { \
  153. .ident = _ident, \
  154. .callback = lis3lv02d_dmi_matched, \
  155. .matches = { \
  156. DMI_MATCH(DMI_##_class1, _name1), \
  157. DMI_MATCH(DMI_##_class2, _name2), \
  158. }, \
  159. .driver_data = &lis3lv02d_axis_##_axis \
  160. }
  161. static const struct dmi_system_id lis3lv02d_dmi_ids[] = {
  162. /* product names are truncated to match all kinds of a same model */
  163. AXIS_DMI_MATCH("NC64x0", "HP Compaq nc64", x_inverted),
  164. AXIS_DMI_MATCH("NC84x0", "HP Compaq nc84", z_inverted),
  165. AXIS_DMI_MATCH("NX9420", "HP Compaq nx9420", x_inverted),
  166. AXIS_DMI_MATCH("NW9440", "HP Compaq nw9440", x_inverted),
  167. AXIS_DMI_MATCH("NC2510", "HP Compaq 2510", y_inverted),
  168. AXIS_DMI_MATCH("NC2710", "HP Compaq 2710", xy_swap),
  169. AXIS_DMI_MATCH("NC8510", "HP Compaq 8510", xy_swap_inverted),
  170. AXIS_DMI_MATCH("HP2133", "HP 2133", xy_rotated_left),
  171. AXIS_DMI_MATCH("HP2140", "HP 2140", xy_swap_inverted),
  172. AXIS_DMI_MATCH("NC653x", "HP Compaq 653", xy_rotated_left_usd),
  173. AXIS_DMI_MATCH("NC6730b", "HP Compaq 6730b", xy_rotated_left_usd),
  174. AXIS_DMI_MATCH("NC6730s", "HP Compaq 6730s", xy_swap),
  175. AXIS_DMI_MATCH("NC651xx", "HP Compaq 651", xy_rotated_right),
  176. AXIS_DMI_MATCH("NC6710x", "HP Compaq 6710", xy_swap_yz_inverted),
  177. AXIS_DMI_MATCH("NC6715x", "HP Compaq 6715", y_inverted),
  178. AXIS_DMI_MATCH("NC693xx", "HP EliteBook 693", xy_rotated_right),
  179. AXIS_DMI_MATCH("NC693xx", "HP EliteBook 853", xy_swap),
  180. AXIS_DMI_MATCH("NC854xx", "HP EliteBook 854", y_inverted),
  181. AXIS_DMI_MATCH("NC273xx", "HP EliteBook 273", y_inverted),
  182. /* Intel-based HP Pavilion dv5 */
  183. AXIS_DMI_MATCH2("HPDV5_I",
  184. PRODUCT_NAME, "HP Pavilion dv5",
  185. BOARD_NAME, "3603",
  186. x_inverted),
  187. /* AMD-based HP Pavilion dv5 */
  188. AXIS_DMI_MATCH2("HPDV5_A",
  189. PRODUCT_NAME, "HP Pavilion dv5",
  190. BOARD_NAME, "3600",
  191. y_inverted),
  192. AXIS_DMI_MATCH("DV7", "HP Pavilion dv7", x_inverted),
  193. AXIS_DMI_MATCH("HP8710", "HP Compaq 8710", y_inverted),
  194. AXIS_DMI_MATCH("HDX18", "HP HDX 18", x_inverted),
  195. AXIS_DMI_MATCH("HPB432x", "HP ProBook 432", xy_rotated_left),
  196. AXIS_DMI_MATCH("HPB440G3", "HP ProBook 440 G3", x_inverted_usd),
  197. AXIS_DMI_MATCH("HPB440G4", "HP ProBook 440 G4", x_inverted),
  198. AXIS_DMI_MATCH("HPB442x", "HP ProBook 442", xy_rotated_left),
  199. AXIS_DMI_MATCH("HPB450G0", "HP ProBook 450 G0", x_inverted),
  200. AXIS_DMI_MATCH("HPB452x", "HP ProBook 452", y_inverted),
  201. AXIS_DMI_MATCH("HPB522x", "HP ProBook 522", xy_swap),
  202. AXIS_DMI_MATCH("HPB532x", "HP ProBook 532", y_inverted),
  203. AXIS_DMI_MATCH("HPB655x", "HP ProBook 655", xy_swap_inverted),
  204. AXIS_DMI_MATCH("Mini510x", "HP Mini 510", xy_rotated_left_usd),
  205. AXIS_DMI_MATCH("HPB63xx", "HP ProBook 63", xy_swap),
  206. AXIS_DMI_MATCH("HPB64xx", "HP ProBook 64", xy_swap),
  207. AXIS_DMI_MATCH("HPB64xx", "HP EliteBook 84", xy_swap),
  208. AXIS_DMI_MATCH("HPB65xx", "HP ProBook 65", x_inverted),
  209. AXIS_DMI_MATCH("HPZBook15", "HP ZBook 15", x_inverted),
  210. AXIS_DMI_MATCH("HPZBook17G5", "HP ZBook 17 G5", x_inverted),
  211. AXIS_DMI_MATCH("HPZBook17", "HP ZBook 17", xy_swap_yz_inverted),
  212. { NULL, }
  213. /* Laptop models without axis info (yet):
  214. * "NC6910" "HP Compaq 6910"
  215. * "NC2400" "HP Compaq nc2400"
  216. * "NX74x0" "HP Compaq nx74"
  217. * "NX6325" "HP Compaq nx6325"
  218. * "NC4400" "HP Compaq nc4400"
  219. */
  220. };
  221. static void hpled_set(struct delayed_led_classdev *led_cdev, enum led_brightness value)
  222. {
  223. struct acpi_device *dev = lis3_dev.bus_priv;
  224. unsigned long long ret; /* Not used when writing */
  225. union acpi_object in_obj[1];
  226. struct acpi_object_list args = { 1, in_obj };
  227. in_obj[0].type = ACPI_TYPE_INTEGER;
  228. in_obj[0].integer.value = !!value;
  229. acpi_evaluate_integer(dev->handle, "ALED", &args, &ret);
  230. }
  231. static struct delayed_led_classdev hpled_led = {
  232. .led_classdev = {
  233. .name = "hp::hddprotect",
  234. .default_trigger = "none",
  235. .brightness_set = delayed_sysfs_set,
  236. .flags = LED_CORE_SUSPENDRESUME,
  237. },
  238. .set_brightness = hpled_set,
  239. };
  240. static bool hp_accel_i8042_filter(unsigned char data, unsigned char str,
  241. struct serio *port)
  242. {
  243. static bool extended;
  244. if (str & I8042_STR_AUXDATA)
  245. return false;
  246. if (data == 0xe0) {
  247. extended = true;
  248. return true;
  249. } else if (unlikely(extended)) {
  250. extended = false;
  251. switch (data) {
  252. case ACCEL_1:
  253. case ACCEL_2:
  254. case ACCEL_3:
  255. case ACCEL_4:
  256. return true;
  257. default:
  258. serio_interrupt(port, 0xe0, 0);
  259. return false;
  260. }
  261. }
  262. return false;
  263. }
  264. static int lis3lv02d_probe(struct platform_device *device)
  265. {
  266. int ret;
  267. lis3_dev.bus_priv = ACPI_COMPANION(&device->dev);
  268. lis3_dev.init = lis3lv02d_acpi_init;
  269. lis3_dev.read = lis3lv02d_acpi_read;
  270. lis3_dev.write = lis3lv02d_acpi_write;
  271. /* obtain IRQ number of our device from ACPI */
  272. ret = platform_get_irq_optional(device, 0);
  273. if (ret > 0)
  274. lis3_dev.irq = ret;
  275. /* If possible use a "standard" axes order */
  276. if (lis3_dev.ac.x && lis3_dev.ac.y && lis3_dev.ac.z) {
  277. pr_info("Using custom axes %d,%d,%d\n",
  278. lis3_dev.ac.x, lis3_dev.ac.y, lis3_dev.ac.z);
  279. } else if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
  280. pr_info("laptop model unknown, using default axes configuration\n");
  281. lis3_dev.ac = lis3lv02d_axis_normal;
  282. }
  283. /* call the core layer do its init */
  284. ret = lis3lv02d_init_device(&lis3_dev);
  285. if (ret)
  286. return ret;
  287. /* filter to remove HPQ6000 accelerometer data
  288. * from keyboard bus stream */
  289. if (strstr(dev_name(&device->dev), "HPQ6000"))
  290. i8042_install_filter(hp_accel_i8042_filter);
  291. INIT_WORK(&hpled_led.work, delayed_set_status_worker);
  292. ret = led_classdev_register(NULL, &hpled_led.led_classdev);
  293. if (ret) {
  294. i8042_remove_filter(hp_accel_i8042_filter);
  295. lis3lv02d_joystick_disable(&lis3_dev);
  296. lis3lv02d_poweroff(&lis3_dev);
  297. flush_work(&hpled_led.work);
  298. lis3lv02d_remove_fs(&lis3_dev);
  299. return ret;
  300. }
  301. return ret;
  302. }
  303. static void lis3lv02d_remove(struct platform_device *device)
  304. {
  305. i8042_remove_filter(hp_accel_i8042_filter);
  306. lis3lv02d_joystick_disable(&lis3_dev);
  307. lis3lv02d_poweroff(&lis3_dev);
  308. led_classdev_unregister(&hpled_led.led_classdev);
  309. flush_work(&hpled_led.work);
  310. lis3lv02d_remove_fs(&lis3_dev);
  311. }
  312. static int __maybe_unused lis3lv02d_suspend(struct device *dev)
  313. {
  314. /* make sure the device is off when we suspend */
  315. lis3lv02d_poweroff(&lis3_dev);
  316. return 0;
  317. }
  318. static int __maybe_unused lis3lv02d_resume(struct device *dev)
  319. {
  320. lis3lv02d_poweron(&lis3_dev);
  321. return 0;
  322. }
  323. static SIMPLE_DEV_PM_OPS(hp_accel_pm, lis3lv02d_suspend, lis3lv02d_resume);
  324. /* For the HP MDPS aka 3D Driveguard */
  325. static struct platform_driver lis3lv02d_driver = {
  326. .probe = lis3lv02d_probe,
  327. .remove_new = lis3lv02d_remove,
  328. .driver = {
  329. .name = "hp_accel",
  330. .pm = &hp_accel_pm,
  331. .acpi_match_table = lis3lv02d_device_ids,
  332. },
  333. };
  334. module_platform_driver(lis3lv02d_driver);
  335. MODULE_DESCRIPTION("Glue between LIS3LV02Dx and HP ACPI BIOS and support for disk protection LED.");
  336. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  337. MODULE_LICENSE("GPL");