/drivers/platform/x86/hp_accel.c

http://github.com/mirrors/linux · C · 431 lines · 316 code · 60 blank · 55 comment · 25 complexity · 3a04ee3c8c204f4c9174cd2db0f2b522 MD5 · raw file

  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 (acpi_evaluate_object(dev->handle, METHOD_NAME__INI,
  78. NULL, NULL) != AE_OK)
  79. return -EINVAL;
  80. return 0;
  81. }
  82. /**
  83. * lis3lv02d_acpi_read - ACPI ALRD method: read a register
  84. * @lis3: pointer to the device struct
  85. * @reg: the register to read
  86. * @ret: result of the operation
  87. *
  88. * Returns 0 on success.
  89. */
  90. static int lis3lv02d_acpi_read(struct lis3lv02d *lis3, int reg, u8 *ret)
  91. {
  92. struct acpi_device *dev = lis3->bus_priv;
  93. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  94. struct acpi_object_list args = { 1, &arg0 };
  95. unsigned long long lret;
  96. acpi_status status;
  97. arg0.integer.value = reg;
  98. status = acpi_evaluate_integer(dev->handle, "ALRD", &args, &lret);
  99. if (ACPI_FAILURE(status))
  100. return -EINVAL;
  101. *ret = lret;
  102. return 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. static 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 = *((union axis_conversion *)dmi->driver_data);
  129. pr_info("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. #define DEFINE_CONV(name, x, y, z) \
  135. static union axis_conversion lis3lv02d_axis_##name = \
  136. { .as_array = { x, y, z } }
  137. DEFINE_CONV(normal, 1, 2, 3);
  138. DEFINE_CONV(y_inverted, 1, -2, 3);
  139. DEFINE_CONV(x_inverted, -1, 2, 3);
  140. DEFINE_CONV(x_inverted_usd, -1, 2, -3);
  141. DEFINE_CONV(z_inverted, 1, 2, -3);
  142. DEFINE_CONV(xy_swap, 2, 1, 3);
  143. DEFINE_CONV(xy_rotated_left, -2, 1, 3);
  144. DEFINE_CONV(xy_rotated_left_usd, -2, 1, -3);
  145. DEFINE_CONV(xy_swap_inverted, -2, -1, 3);
  146. DEFINE_CONV(xy_rotated_right, 2, -1, 3);
  147. DEFINE_CONV(xy_swap_yz_inverted, 2, -1, -3);
  148. #define AXIS_DMI_MATCH(_ident, _name, _axis) { \
  149. .ident = _ident, \
  150. .callback = lis3lv02d_dmi_matched, \
  151. .matches = { \
  152. DMI_MATCH(DMI_PRODUCT_NAME, _name) \
  153. }, \
  154. .driver_data = &lis3lv02d_axis_##_axis \
  155. }
  156. #define AXIS_DMI_MATCH2(_ident, _class1, _name1, \
  157. _class2, _name2, \
  158. _axis) { \
  159. .ident = _ident, \
  160. .callback = lis3lv02d_dmi_matched, \
  161. .matches = { \
  162. DMI_MATCH(DMI_##_class1, _name1), \
  163. DMI_MATCH(DMI_##_class2, _name2), \
  164. }, \
  165. .driver_data = &lis3lv02d_axis_##_axis \
  166. }
  167. static const struct dmi_system_id lis3lv02d_dmi_ids[] = {
  168. /* product names are truncated to match all kinds of a same model */
  169. AXIS_DMI_MATCH("NC64x0", "HP Compaq nc64", x_inverted),
  170. AXIS_DMI_MATCH("NC84x0", "HP Compaq nc84", z_inverted),
  171. AXIS_DMI_MATCH("NX9420", "HP Compaq nx9420", x_inverted),
  172. AXIS_DMI_MATCH("NW9440", "HP Compaq nw9440", x_inverted),
  173. AXIS_DMI_MATCH("NC2510", "HP Compaq 2510", y_inverted),
  174. AXIS_DMI_MATCH("NC2710", "HP Compaq 2710", xy_swap),
  175. AXIS_DMI_MATCH("NC8510", "HP Compaq 8510", xy_swap_inverted),
  176. AXIS_DMI_MATCH("HP2133", "HP 2133", xy_rotated_left),
  177. AXIS_DMI_MATCH("HP2140", "HP 2140", xy_swap_inverted),
  178. AXIS_DMI_MATCH("NC653x", "HP Compaq 653", xy_rotated_left_usd),
  179. AXIS_DMI_MATCH("NC6730b", "HP Compaq 6730b", xy_rotated_left_usd),
  180. AXIS_DMI_MATCH("NC6730s", "HP Compaq 6730s", xy_swap),
  181. AXIS_DMI_MATCH("NC651xx", "HP Compaq 651", xy_rotated_right),
  182. AXIS_DMI_MATCH("NC6710x", "HP Compaq 6710", xy_swap_yz_inverted),
  183. AXIS_DMI_MATCH("NC6715x", "HP Compaq 6715", y_inverted),
  184. AXIS_DMI_MATCH("NC693xx", "HP EliteBook 693", xy_rotated_right),
  185. AXIS_DMI_MATCH("NC693xx", "HP EliteBook 853", xy_swap),
  186. AXIS_DMI_MATCH("NC854xx", "HP EliteBook 854", y_inverted),
  187. AXIS_DMI_MATCH("NC273xx", "HP EliteBook 273", y_inverted),
  188. /* Intel-based HP Pavilion dv5 */
  189. AXIS_DMI_MATCH2("HPDV5_I",
  190. PRODUCT_NAME, "HP Pavilion dv5",
  191. BOARD_NAME, "3603",
  192. x_inverted),
  193. /* AMD-based HP Pavilion dv5 */
  194. AXIS_DMI_MATCH2("HPDV5_A",
  195. PRODUCT_NAME, "HP Pavilion dv5",
  196. BOARD_NAME, "3600",
  197. y_inverted),
  198. AXIS_DMI_MATCH("DV7", "HP Pavilion dv7", x_inverted),
  199. AXIS_DMI_MATCH("HP8710", "HP Compaq 8710", y_inverted),
  200. AXIS_DMI_MATCH("HDX18", "HP HDX 18", x_inverted),
  201. AXIS_DMI_MATCH("HPB432x", "HP ProBook 432", xy_rotated_left),
  202. AXIS_DMI_MATCH("HPB440G3", "HP ProBook 440 G3", x_inverted_usd),
  203. AXIS_DMI_MATCH("HPB440G4", "HP ProBook 440 G4", x_inverted),
  204. AXIS_DMI_MATCH("HPB442x", "HP ProBook 442", xy_rotated_left),
  205. AXIS_DMI_MATCH("HPB450G0", "HP ProBook 450 G0", x_inverted),
  206. AXIS_DMI_MATCH("HPB452x", "HP ProBook 452", y_inverted),
  207. AXIS_DMI_MATCH("HPB522x", "HP ProBook 522", xy_swap),
  208. AXIS_DMI_MATCH("HPB532x", "HP ProBook 532", y_inverted),
  209. AXIS_DMI_MATCH("HPB655x", "HP ProBook 655", xy_swap_inverted),
  210. AXIS_DMI_MATCH("Mini510x", "HP Mini 510", xy_rotated_left_usd),
  211. AXIS_DMI_MATCH("HPB63xx", "HP ProBook 63", xy_swap),
  212. AXIS_DMI_MATCH("HPB64xx", "HP ProBook 64", xy_swap),
  213. AXIS_DMI_MATCH("HPB64xx", "HP EliteBook 84", xy_swap),
  214. AXIS_DMI_MATCH("HPB65xx", "HP ProBook 65", x_inverted),
  215. AXIS_DMI_MATCH("HPZBook15", "HP ZBook 15", x_inverted),
  216. AXIS_DMI_MATCH("HPZBook17G5", "HP ZBook 17 G5", x_inverted),
  217. AXIS_DMI_MATCH("HPZBook17", "HP ZBook 17", xy_swap_yz_inverted),
  218. { NULL, }
  219. /* Laptop models without axis info (yet):
  220. * "NC6910" "HP Compaq 6910"
  221. * "NC2400" "HP Compaq nc2400"
  222. * "NX74x0" "HP Compaq nx74"
  223. * "NX6325" "HP Compaq nx6325"
  224. * "NC4400" "HP Compaq nc4400"
  225. */
  226. };
  227. static void hpled_set(struct delayed_led_classdev *led_cdev, enum led_brightness value)
  228. {
  229. struct acpi_device *dev = lis3_dev.bus_priv;
  230. unsigned long long ret; /* Not used when writing */
  231. union acpi_object in_obj[1];
  232. struct acpi_object_list args = { 1, in_obj };
  233. in_obj[0].type = ACPI_TYPE_INTEGER;
  234. in_obj[0].integer.value = !!value;
  235. acpi_evaluate_integer(dev->handle, "ALED", &args, &ret);
  236. }
  237. static struct delayed_led_classdev hpled_led = {
  238. .led_classdev = {
  239. .name = "hp::hddprotect",
  240. .default_trigger = "none",
  241. .brightness_set = delayed_sysfs_set,
  242. .flags = LED_CORE_SUSPENDRESUME,
  243. },
  244. .set_brightness = hpled_set,
  245. };
  246. static acpi_status
  247. lis3lv02d_get_resource(struct acpi_resource *resource, void *context)
  248. {
  249. if (resource->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  250. struct acpi_resource_extended_irq *irq;
  251. u32 *device_irq = context;
  252. irq = &resource->data.extended_irq;
  253. *device_irq = irq->interrupts[0];
  254. }
  255. return AE_OK;
  256. }
  257. static void lis3lv02d_enum_resources(struct acpi_device *device)
  258. {
  259. acpi_status status;
  260. status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  261. lis3lv02d_get_resource, &lis3_dev.irq);
  262. if (ACPI_FAILURE(status))
  263. printk(KERN_DEBUG DRIVER_NAME ": Error getting resources\n");
  264. }
  265. static bool hp_accel_i8042_filter(unsigned char data, unsigned char str,
  266. struct serio *port)
  267. {
  268. static bool extended;
  269. if (str & I8042_STR_AUXDATA)
  270. return false;
  271. if (data == 0xe0) {
  272. extended = true;
  273. return true;
  274. } else if (unlikely(extended)) {
  275. extended = false;
  276. switch (data) {
  277. case ACCEL_1:
  278. case ACCEL_2:
  279. case ACCEL_3:
  280. case ACCEL_4:
  281. return true;
  282. default:
  283. serio_interrupt(port, 0xe0, 0);
  284. return false;
  285. }
  286. }
  287. return false;
  288. }
  289. static int lis3lv02d_add(struct acpi_device *device)
  290. {
  291. int ret;
  292. if (!device)
  293. return -EINVAL;
  294. lis3_dev.bus_priv = device;
  295. lis3_dev.init = lis3lv02d_acpi_init;
  296. lis3_dev.read = lis3lv02d_acpi_read;
  297. lis3_dev.write = lis3lv02d_acpi_write;
  298. strcpy(acpi_device_name(device), DRIVER_NAME);
  299. strcpy(acpi_device_class(device), ACPI_MDPS_CLASS);
  300. device->driver_data = &lis3_dev;
  301. /* obtain IRQ number of our device from ACPI */
  302. lis3lv02d_enum_resources(device);
  303. /* If possible use a "standard" axes order */
  304. if (lis3_dev.ac.x && lis3_dev.ac.y && lis3_dev.ac.z) {
  305. pr_info("Using custom axes %d,%d,%d\n",
  306. lis3_dev.ac.x, lis3_dev.ac.y, lis3_dev.ac.z);
  307. } else if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
  308. pr_info("laptop model unknown, using default axes configuration\n");
  309. lis3_dev.ac = lis3lv02d_axis_normal;
  310. }
  311. /* call the core layer do its init */
  312. ret = lis3lv02d_init_device(&lis3_dev);
  313. if (ret)
  314. return ret;
  315. /* filter to remove HPQ6000 accelerometer data
  316. * from keyboard bus stream */
  317. if (strstr(dev_name(&device->dev), "HPQ6000"))
  318. i8042_install_filter(hp_accel_i8042_filter);
  319. INIT_WORK(&hpled_led.work, delayed_set_status_worker);
  320. ret = led_classdev_register(NULL, &hpled_led.led_classdev);
  321. if (ret) {
  322. lis3lv02d_joystick_disable(&lis3_dev);
  323. lis3lv02d_poweroff(&lis3_dev);
  324. flush_work(&hpled_led.work);
  325. return ret;
  326. }
  327. return ret;
  328. }
  329. static int lis3lv02d_remove(struct acpi_device *device)
  330. {
  331. if (!device)
  332. return -EINVAL;
  333. i8042_remove_filter(hp_accel_i8042_filter);
  334. lis3lv02d_joystick_disable(&lis3_dev);
  335. lis3lv02d_poweroff(&lis3_dev);
  336. led_classdev_unregister(&hpled_led.led_classdev);
  337. flush_work(&hpled_led.work);
  338. return lis3lv02d_remove_fs(&lis3_dev);
  339. }
  340. #ifdef CONFIG_PM_SLEEP
  341. static int lis3lv02d_suspend(struct device *dev)
  342. {
  343. /* make sure the device is off when we suspend */
  344. lis3lv02d_poweroff(&lis3_dev);
  345. return 0;
  346. }
  347. static int lis3lv02d_resume(struct device *dev)
  348. {
  349. lis3lv02d_poweron(&lis3_dev);
  350. return 0;
  351. }
  352. static SIMPLE_DEV_PM_OPS(hp_accel_pm, lis3lv02d_suspend, lis3lv02d_resume);
  353. #define HP_ACCEL_PM (&hp_accel_pm)
  354. #else
  355. #define HP_ACCEL_PM NULL
  356. #endif
  357. /* For the HP MDPS aka 3D Driveguard */
  358. static struct acpi_driver lis3lv02d_driver = {
  359. .name = DRIVER_NAME,
  360. .class = ACPI_MDPS_CLASS,
  361. .ids = lis3lv02d_device_ids,
  362. .ops = {
  363. .add = lis3lv02d_add,
  364. .remove = lis3lv02d_remove,
  365. },
  366. .drv.pm = HP_ACCEL_PM,
  367. };
  368. module_acpi_driver(lis3lv02d_driver);
  369. MODULE_DESCRIPTION("Glue between LIS3LV02Dx and HP ACPI BIOS and support for disk protection LED.");
  370. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  371. MODULE_LICENSE("GPL");