surfacepro3_button.c 6.0 KB

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