surfacepro3_button.c 5.8 KB

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