surfacepro3_button.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * power/home/volume button support for
  3. * Microsoft Surface Pro 3/4 tablet.
  4. *
  5. * Copyright (c) 2015 Intel Corporation.
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; version 2
  11. * of the License.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/types.h>
  17. #include <linux/input.h>
  18. #include <linux/acpi.h>
  19. #include <acpi/button.h>
  20. #define SURFACE_PRO3_BUTTON_HID "MSHW0028"
  21. #define SURFACE_PRO4_BUTTON_HID "MSHW0040"
  22. #define SURFACE_BUTTON_OBJ_NAME "VGBI"
  23. #define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
  24. #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
  25. #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
  26. #define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
  27. #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
  28. #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
  29. #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
  30. #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
  31. #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
  32. ACPI_MODULE_NAME("surface pro 3 button");
  33. MODULE_AUTHOR("Chen Yu");
  34. MODULE_DESCRIPTION("Surface Pro3 Button Driver");
  35. MODULE_LICENSE("GPL v2");
  36. /*
  37. * Power button, Home button, Volume buttons support is supposed to
  38. * be covered by drivers/input/misc/soc_button_array.c, which is implemented
  39. * according to "Windows ACPI Design Guide for SoC Platforms".
  40. * However surface pro3 seems not to obey the specs, instead it uses
  41. * device VGBI(MSHW0028) for dispatching the events.
  42. * We choose acpi_driver rather than platform_driver/i2c_driver because
  43. * although VGBI has an i2c resource connected to i2c controller, it
  44. * is not embedded in any i2c controller's scope, thus neither platform_device
  45. * will be created, nor i2c_client will be enumerated, we have to use
  46. * acpi_driver.
  47. */
  48. static const struct acpi_device_id surface_button_device_ids[] = {
  49. {SURFACE_PRO3_BUTTON_HID, 0},
  50. {SURFACE_PRO4_BUTTON_HID, 0},
  51. {"", 0},
  52. };
  53. MODULE_DEVICE_TABLE(acpi, surface_button_device_ids);
  54. struct surface_button {
  55. unsigned int type;
  56. struct input_dev *input;
  57. char phys[32]; /* for input device */
  58. unsigned long pushed;
  59. bool suspended;
  60. };
  61. static void surface_button_notify(struct acpi_device *device, u32 event)
  62. {
  63. struct surface_button *button = acpi_driver_data(device);
  64. struct input_dev *input;
  65. int key_code = KEY_RESERVED;
  66. bool pressed = false;
  67. switch (event) {
  68. /* Power button press,release handle */
  69. case SURFACE_BUTTON_NOTIFY_PRESS_POWER:
  70. pressed = true;
  71. /*fall through*/
  72. case SURFACE_BUTTON_NOTIFY_RELEASE_POWER:
  73. key_code = KEY_POWER;
  74. break;
  75. /* Home button press,release handle */
  76. case SURFACE_BUTTON_NOTIFY_PRESS_HOME:
  77. pressed = true;
  78. /*fall through*/
  79. case SURFACE_BUTTON_NOTIFY_RELEASE_HOME:
  80. key_code = KEY_LEFTMETA;
  81. break;
  82. /* Volume up button press,release handle */
  83. case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP:
  84. pressed = true;
  85. /*fall through*/
  86. case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP:
  87. key_code = KEY_VOLUMEUP;
  88. break;
  89. /* Volume down button press,release handle */
  90. case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN:
  91. pressed = true;
  92. /*fall through*/
  93. case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN:
  94. key_code = KEY_VOLUMEDOWN;
  95. break;
  96. default:
  97. dev_info_ratelimited(&device->dev,
  98. "Unsupported event [0x%x]\n", event);
  99. break;
  100. }
  101. input = button->input;
  102. if (key_code == KEY_RESERVED)
  103. return;
  104. if (pressed)
  105. pm_wakeup_event(&device->dev, 0);
  106. if (button->suspended)
  107. return;
  108. input_report_key(input, key_code, pressed?1:0);
  109. input_sync(input);
  110. }
  111. #ifdef CONFIG_PM_SLEEP
  112. static int surface_button_suspend(struct device *dev)
  113. {
  114. struct acpi_device *device = to_acpi_device(dev);
  115. struct surface_button *button = acpi_driver_data(device);
  116. button->suspended = true;
  117. return 0;
  118. }
  119. static int surface_button_resume(struct device *dev)
  120. {
  121. struct acpi_device *device = to_acpi_device(dev);
  122. struct surface_button *button = acpi_driver_data(device);
  123. button->suspended = false;
  124. return 0;
  125. }
  126. #endif
  127. static int surface_button_add(struct acpi_device *device)
  128. {
  129. struct surface_button *button;
  130. struct input_dev *input;
  131. const char *hid = acpi_device_hid(device);
  132. char *name;
  133. int error;
  134. if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
  135. strlen(SURFACE_BUTTON_OBJ_NAME)))
  136. return -ENODEV;
  137. button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
  138. if (!button)
  139. return -ENOMEM;
  140. device->driver_data = button;
  141. button->input = input = input_allocate_device();
  142. if (!input) {
  143. error = -ENOMEM;
  144. goto err_free_button;
  145. }
  146. name = acpi_device_name(device);
  147. strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
  148. snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
  149. input->name = name;
  150. input->phys = button->phys;
  151. input->id.bustype = BUS_HOST;
  152. input->dev.parent = &device->dev;
  153. input_set_capability(input, EV_KEY, KEY_POWER);
  154. input_set_capability(input, EV_KEY, KEY_LEFTMETA);
  155. input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
  156. input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
  157. error = input_register_device(input);
  158. if (error)
  159. goto err_free_input;
  160. dev_info(&device->dev,
  161. "%s [%s]\n", name, acpi_device_bid(device));
  162. return 0;
  163. err_free_input:
  164. input_free_device(input);
  165. err_free_button:
  166. kfree(button);
  167. return error;
  168. }
  169. static int surface_button_remove(struct acpi_device *device)
  170. {
  171. struct surface_button *button = acpi_driver_data(device);
  172. input_unregister_device(button->input);
  173. kfree(button);
  174. return 0;
  175. }
  176. static SIMPLE_DEV_PM_OPS(surface_button_pm,
  177. surface_button_suspend, surface_button_resume);
  178. static struct acpi_driver surface_button_driver = {
  179. .name = "surface_pro3_button",
  180. .class = "SurfacePro3",
  181. .ids = surface_button_device_ids,
  182. .ops = {
  183. .add = surface_button_add,
  184. .remove = surface_button_remove,
  185. .notify = surface_button_notify,
  186. },
  187. .drv.pm = &surface_button_pm,
  188. };
  189. module_acpi_driver(surface_button_driver);