hp_accel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #define DRIVER_NAME "hp_accel"
  29. #define ACPI_MDPS_CLASS "accelerometer"
  30. /* Delayed LEDs infrastructure ------------------------------------ */
  31. /* Special LED class that can defer work */
  32. struct delayed_led_classdev {
  33. struct led_classdev led_classdev;
  34. struct work_struct work;
  35. enum led_brightness new_brightness;
  36. unsigned int led; /* For driver */
  37. void (*set_brightness)(struct delayed_led_classdev *data, enum led_brightness value);
  38. };
  39. static inline void delayed_set_status_worker(struct work_struct *work)
  40. {
  41. struct delayed_led_classdev *data =
  42. container_of(work, struct delayed_led_classdev, work);
  43. data->set_brightness(data, data->new_brightness);
  44. }
  45. static inline void delayed_sysfs_set(struct led_classdev *led_cdev,
  46. enum led_brightness brightness)
  47. {
  48. struct delayed_led_classdev *data = container_of(led_cdev,
  49. struct delayed_led_classdev, led_classdev);
  50. data->new_brightness = brightness;
  51. schedule_work(&data->work);
  52. }
  53. /* HP-specific accelerometer driver ------------------------------------ */
  54. /* e0 25, e0 26, e0 27, e0 28 are scan codes that the accelerometer with acpi id
  55. * HPQ6000 sends through the keyboard bus */
  56. #define ACCEL_1 0x25
  57. #define ACCEL_2 0x26
  58. #define ACCEL_3 0x27
  59. #define ACCEL_4 0x28
  60. /* For automatic insertion of the module */
  61. static const struct acpi_device_id lis3lv02d_device_ids[] = {
  62. {"HPQ0004", 0}, /* HP Mobile Data Protection System PNP */
  63. {"HPQ6000", 0}, /* HP Mobile Data Protection System PNP */
  64. {"HPQ6007", 0}, /* HP Mobile Data Protection System PNP */
  65. {"", 0},
  66. };
  67. MODULE_DEVICE_TABLE(acpi, lis3lv02d_device_ids);
  68. /**
  69. * lis3lv02d_acpi_init - ACPI _INI method: initialize the device.
  70. * @lis3: pointer to the device struct
  71. *
  72. * Returns 0 on success.
  73. */
  74. static int lis3lv02d_acpi_init(struct lis3lv02d *lis3)
  75. {
  76. struct acpi_device *dev = lis3->bus_priv;
  77. if (!lis3->init_required)
  78. return 0;
  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. static 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. if (ACPI_FAILURE(status))
  102. return -EINVAL;
  103. *ret = lret;
  104. return 0;
  105. }
  106. /**
  107. * lis3lv02d_acpi_write - ACPI ALWR method: write to a register
  108. * @lis3: pointer to the device struct
  109. * @reg: the register to write to
  110. * @val: the value to write
  111. *
  112. * Returns 0 on success.
  113. */
  114. static int lis3lv02d_acpi_write(struct lis3lv02d *lis3, int reg, u8 val)
  115. {
  116. struct acpi_device *dev = lis3->bus_priv;
  117. unsigned long long ret; /* Not used when writting */
  118. union acpi_object in_obj[2];
  119. struct acpi_object_list args = { 2, in_obj };
  120. in_obj[0].type = ACPI_TYPE_INTEGER;
  121. in_obj[0].integer.value = reg;
  122. in_obj[1].type = ACPI_TYPE_INTEGER;
  123. in_obj[1].integer.value = val;
  124. if (acpi_evaluate_integer(dev->handle, "ALWR", &args, &ret) != AE_OK)
  125. return -EINVAL;
  126. return 0;
  127. }
  128. static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
  129. {
  130. lis3_dev.ac = *((union axis_conversion *)dmi->driver_data);
  131. pr_info("hardware type %s found\n", dmi->ident);
  132. return 1;
  133. }
  134. /* Represents, for each axis seen by userspace, the corresponding hw axis (+1).
  135. * If the value is negative, the opposite of the hw value is used. */
  136. #define DEFINE_CONV(name, x, y, z) \
  137. static union axis_conversion lis3lv02d_axis_##name = \
  138. { .as_array = { x, y, z } }
  139. DEFINE_CONV(normal, 1, 2, 3);
  140. DEFINE_CONV(y_inverted, 1, -2, 3);
  141. DEFINE_CONV(x_inverted, -1, 2, 3);
  142. DEFINE_CONV(x_inverted_usd, -1, 2, -3);
  143. DEFINE_CONV(z_inverted, 1, 2, -3);
  144. DEFINE_CONV(xy_swap, 2, 1, 3);
  145. DEFINE_CONV(xy_rotated_left, -2, 1, 3);
  146. DEFINE_CONV(xy_rotated_left_usd, -2, 1, -3);
  147. DEFINE_CONV(xy_swap_inverted, -2, -1, 3);
  148. DEFINE_CONV(xy_rotated_right, 2, -1, 3);
  149. DEFINE_CONV(xy_swap_yz_inverted, 2, -1, -3);
  150. #define AXIS_DMI_MATCH(_ident, _name, _axis) { \
  151. .ident = _ident, \
  152. .callback = lis3lv02d_dmi_matched, \
  153. .matches = { \
  154. DMI_MATCH(DMI_PRODUCT_NAME, _name) \
  155. }, \
  156. .driver_data = &lis3lv02d_axis_##_axis \
  157. }
  158. #define AXIS_DMI_MATCH2(_ident, _class1, _name1, \
  159. _class2, _name2, \
  160. _axis) { \
  161. .ident = _ident, \
  162. .callback = lis3lv02d_dmi_matched, \
  163. .matches = { \
  164. DMI_MATCH(DMI_##_class1, _name1), \
  165. DMI_MATCH(DMI_##_class2, _name2), \
  166. }, \
  167. .driver_data = &lis3lv02d_axis_##_axis \
  168. }
  169. static const struct dmi_system_id lis3lv02d_dmi_ids[] = {
  170. /* product names are truncated to match all kinds of a same model */
  171. AXIS_DMI_MATCH("NC64x0", "HP Compaq nc64", x_inverted),
  172. AXIS_DMI_MATCH("NC84x0", "HP Compaq nc84", z_inverted),
  173. AXIS_DMI_MATCH("NX9420", "HP Compaq nx9420", x_inverted),
  174. AXIS_DMI_MATCH("NW9440", "HP Compaq nw9440", x_inverted),
  175. AXIS_DMI_MATCH("NC2510", "HP Compaq 2510", y_inverted),
  176. AXIS_DMI_MATCH("NC2710", "HP Compaq 2710", xy_swap),
  177. AXIS_DMI_MATCH("NC8510", "HP Compaq 8510", xy_swap_inverted),
  178. AXIS_DMI_MATCH("HP2133", "HP 2133", xy_rotated_left),
  179. AXIS_DMI_MATCH("HP2140", "HP 2140", xy_swap_inverted),
  180. AXIS_DMI_MATCH("NC653x", "HP Compaq 653", xy_rotated_left_usd),
  181. AXIS_DMI_MATCH("NC6730b", "HP Compaq 6730b", xy_rotated_left_usd),
  182. AXIS_DMI_MATCH("NC6730s", "HP Compaq 6730s", xy_swap),
  183. AXIS_DMI_MATCH("NC651xx", "HP Compaq 651", xy_rotated_right),
  184. AXIS_DMI_MATCH("NC6710x", "HP Compaq 6710", xy_swap_yz_inverted),
  185. AXIS_DMI_MATCH("NC6715x", "HP Compaq 6715", y_inverted),
  186. AXIS_DMI_MATCH("NC693xx", "HP EliteBook 693", xy_rotated_right),
  187. AXIS_DMI_MATCH("NC693xx", "HP EliteBook 853", xy_swap),
  188. AXIS_DMI_MATCH("NC854xx", "HP EliteBook 854", y_inverted),
  189. AXIS_DMI_MATCH("NC273xx", "HP EliteBook 273", y_inverted),
  190. /* Intel-based HP Pavilion dv5 */
  191. AXIS_DMI_MATCH2("HPDV5_I",
  192. PRODUCT_NAME, "HP Pavilion dv5",
  193. BOARD_NAME, "3603",
  194. x_inverted),
  195. /* AMD-based HP Pavilion dv5 */
  196. AXIS_DMI_MATCH2("HPDV5_A",
  197. PRODUCT_NAME, "HP Pavilion dv5",
  198. BOARD_NAME, "3600",
  199. y_inverted),
  200. AXIS_DMI_MATCH("DV7", "HP Pavilion dv7", x_inverted),
  201. AXIS_DMI_MATCH("HP8710", "HP Compaq 8710", y_inverted),
  202. AXIS_DMI_MATCH("HDX18", "HP HDX 18", x_inverted),
  203. AXIS_DMI_MATCH("HPB432x", "HP ProBook 432", xy_rotated_left),
  204. AXIS_DMI_MATCH("HPB440G3", "HP ProBook 440 G3", x_inverted_usd),
  205. AXIS_DMI_MATCH("HPB440G4", "HP ProBook 440 G4", x_inverted),
  206. AXIS_DMI_MATCH("HPB442x", "HP ProBook 442", xy_rotated_left),
  207. AXIS_DMI_MATCH("HPB450G0", "HP ProBook 450 G0", x_inverted),
  208. AXIS_DMI_MATCH("HPB452x", "HP ProBook 452", y_inverted),
  209. AXIS_DMI_MATCH("HPB522x", "HP ProBook 522", xy_swap),
  210. AXIS_DMI_MATCH("HPB532x", "HP ProBook 532", y_inverted),
  211. AXIS_DMI_MATCH("HPB655x", "HP ProBook 655", xy_swap_inverted),
  212. AXIS_DMI_MATCH("Mini510x", "HP Mini 510", xy_rotated_left_usd),
  213. AXIS_DMI_MATCH("HPB63xx", "HP ProBook 63", xy_swap),
  214. AXIS_DMI_MATCH("HPB64xx", "HP ProBook 64", xy_swap),
  215. AXIS_DMI_MATCH("HPB64xx", "HP EliteBook 84", xy_swap),
  216. AXIS_DMI_MATCH("HPB65xx", "HP ProBook 65", x_inverted),
  217. AXIS_DMI_MATCH("HPZBook15", "HP ZBook 15", x_inverted),
  218. AXIS_DMI_MATCH("HPZBook17G5", "HP ZBook 17 G5", x_inverted),
  219. AXIS_DMI_MATCH("HPZBook17", "HP ZBook 17", xy_swap_yz_inverted),
  220. { NULL, }
  221. /* Laptop models without axis info (yet):
  222. * "NC6910" "HP Compaq 6910"
  223. * "NC2400" "HP Compaq nc2400"
  224. * "NX74x0" "HP Compaq nx74"
  225. * "NX6325" "HP Compaq nx6325"
  226. * "NC4400" "HP Compaq nc4400"
  227. */
  228. };
  229. static void hpled_set(struct delayed_led_classdev *led_cdev, enum led_brightness value)
  230. {
  231. struct acpi_device *dev = lis3_dev.bus_priv;
  232. unsigned long long ret; /* Not used when writing */
  233. union acpi_object in_obj[1];
  234. struct acpi_object_list args = { 1, in_obj };
  235. in_obj[0].type = ACPI_TYPE_INTEGER;
  236. in_obj[0].integer.value = !!value;
  237. acpi_evaluate_integer(dev->handle, "ALED", &args, &ret);
  238. }
  239. static struct delayed_led_classdev hpled_led = {
  240. .led_classdev = {
  241. .name = "hp::hddprotect",
  242. .default_trigger = "none",
  243. .brightness_set = delayed_sysfs_set,
  244. .flags = LED_CORE_SUSPENDRESUME,
  245. },
  246. .set_brightness = hpled_set,
  247. };
  248. static acpi_status
  249. lis3lv02d_get_resource(struct acpi_resource *resource, void *context)
  250. {
  251. if (resource->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  252. struct acpi_resource_extended_irq *irq;
  253. u32 *device_irq = context;
  254. irq = &resource->data.extended_irq;
  255. *device_irq = irq->interrupts[0];
  256. }
  257. return AE_OK;
  258. }
  259. static void lis3lv02d_enum_resources(struct acpi_device *device)
  260. {
  261. acpi_status status;
  262. status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  263. lis3lv02d_get_resource, &lis3_dev.irq);
  264. if (ACPI_FAILURE(status))
  265. printk(KERN_DEBUG DRIVER_NAME ": Error getting resources\n");
  266. }
  267. static bool hp_accel_i8042_filter(unsigned char data, unsigned char str,
  268. struct serio *port)
  269. {
  270. static bool extended;
  271. if (str & I8042_STR_AUXDATA)
  272. return false;
  273. if (data == 0xe0) {
  274. extended = true;
  275. return true;
  276. } else if (unlikely(extended)) {
  277. extended = false;
  278. switch (data) {
  279. case ACCEL_1:
  280. case ACCEL_2:
  281. case ACCEL_3:
  282. case ACCEL_4:
  283. return true;
  284. default:
  285. serio_interrupt(port, 0xe0, 0);
  286. return false;
  287. }
  288. }
  289. return false;
  290. }
  291. static int lis3lv02d_add(struct acpi_device *device)
  292. {
  293. int ret;
  294. if (!device)
  295. return -EINVAL;
  296. lis3_dev.bus_priv = device;
  297. lis3_dev.init = lis3lv02d_acpi_init;
  298. lis3_dev.read = lis3lv02d_acpi_read;
  299. lis3_dev.write = lis3lv02d_acpi_write;
  300. strcpy(acpi_device_name(device), DRIVER_NAME);
  301. strcpy(acpi_device_class(device), ACPI_MDPS_CLASS);
  302. device->driver_data = &lis3_dev;
  303. /* obtain IRQ number of our device from ACPI */
  304. lis3lv02d_enum_resources(device);
  305. /* If possible use a "standard" axes order */
  306. if (lis3_dev.ac.x && lis3_dev.ac.y && lis3_dev.ac.z) {
  307. pr_info("Using custom axes %d,%d,%d\n",
  308. lis3_dev.ac.x, lis3_dev.ac.y, lis3_dev.ac.z);
  309. } else if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
  310. pr_info("laptop model unknown, using default axes configuration\n");
  311. lis3_dev.ac = lis3lv02d_axis_normal;
  312. }
  313. /* call the core layer do its init */
  314. lis3_dev.init_required = true;
  315. ret = lis3lv02d_init_device(&lis3_dev);
  316. if (ret)
  317. return ret;
  318. /* filter to remove HPQ6000 accelerometer data
  319. * from keyboard bus stream */
  320. if (strstr(dev_name(&device->dev), "HPQ6000"))
  321. i8042_install_filter(hp_accel_i8042_filter);
  322. INIT_WORK(&hpled_led.work, delayed_set_status_worker);
  323. ret = led_classdev_register(NULL, &hpled_led.led_classdev);
  324. if (ret) {
  325. i8042_remove_filter(hp_accel_i8042_filter);
  326. lis3lv02d_joystick_disable(&lis3_dev);
  327. lis3lv02d_poweroff(&lis3_dev);
  328. flush_work(&hpled_led.work);
  329. lis3lv02d_remove_fs(&lis3_dev);
  330. return ret;
  331. }
  332. return ret;
  333. }
  334. static int lis3lv02d_remove(struct acpi_device *device)
  335. {
  336. if (!device)
  337. return -EINVAL;
  338. i8042_remove_filter(hp_accel_i8042_filter);
  339. lis3lv02d_joystick_disable(&lis3_dev);
  340. lis3lv02d_poweroff(&lis3_dev);
  341. led_classdev_unregister(&hpled_led.led_classdev);
  342. flush_work(&hpled_led.work);
  343. return lis3lv02d_remove_fs(&lis3_dev);
  344. }
  345. #ifdef CONFIG_PM_SLEEP
  346. static int lis3lv02d_suspend(struct device *dev)
  347. {
  348. /* make sure the device is off when we suspend */
  349. lis3lv02d_poweroff(&lis3_dev);
  350. return 0;
  351. }
  352. static int lis3lv02d_resume(struct device *dev)
  353. {
  354. lis3_dev.init_required = false;
  355. lis3lv02d_poweron(&lis3_dev);
  356. return 0;
  357. }
  358. static int lis3lv02d_restore(struct device *dev)
  359. {
  360. lis3_dev.init_required = true;
  361. lis3lv02d_poweron(&lis3_dev);
  362. return 0;
  363. }
  364. static const struct dev_pm_ops hp_accel_pm = {
  365. .suspend = lis3lv02d_suspend,
  366. .resume = lis3lv02d_resume,
  367. .freeze = lis3lv02d_suspend,
  368. .thaw = lis3lv02d_resume,
  369. .poweroff = lis3lv02d_suspend,
  370. .restore = lis3lv02d_restore,
  371. };
  372. #define HP_ACCEL_PM (&hp_accel_pm)
  373. #else
  374. #define HP_ACCEL_PM NULL
  375. #endif
  376. /* For the HP MDPS aka 3D Driveguard */
  377. static struct acpi_driver lis3lv02d_driver = {
  378. .name = DRIVER_NAME,
  379. .class = ACPI_MDPS_CLASS,
  380. .ids = lis3lv02d_device_ids,
  381. .ops = {
  382. .add = lis3lv02d_add,
  383. .remove = lis3lv02d_remove,
  384. },
  385. .drv.pm = HP_ACCEL_PM,
  386. };
  387. module_acpi_driver(lis3lv02d_driver);
  388. MODULE_DESCRIPTION("Glue between LIS3LV02Dx and HP ACPI BIOS and support for disk protection LED.");
  389. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  390. MODULE_LICENSE("GPL");