1 // SPDX-License-Identifier: GPL-2.0-only
3 * power/home/volume button support for
4 * Microsoft Surface Pro 3/4 tablet.
6 * Copyright (c) 2015 Intel Corporation.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/input.h>
15 #include <linux/acpi.h>
16 #include <acpi/button.h>
18 #define SURFACE_PRO3_BUTTON_HID "MSHW0028"
19 #define SURFACE_PRO4_BUTTON_HID "MSHW0040"
20 #define SURFACE_BUTTON_OBJ_NAME "VGBI"
21 #define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
23 #define MSHW0040_DSM_REVISION 0x01
24 #define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
25 static const guid_t MSHW0040_DSM_UUID =
26 GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
29 #define SURFACE_BUTTON_NOTIFY_TABLET_MODE 0xc8
31 #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
32 #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
34 #define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
35 #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
37 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
38 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
40 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
41 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
43 MODULE_AUTHOR("Chen Yu");
44 MODULE_DESCRIPTION("Surface Pro3 Button Driver");
45 MODULE_LICENSE("GPL v2");
48 * Power button, Home button, Volume buttons support is supposed to
49 * be covered by drivers/input/misc/soc_button_array.c, which is implemented
50 * according to "Windows ACPI Design Guide for SoC Platforms".
51 * However surface pro3 seems not to obey the specs, instead it uses
52 * device VGBI(MSHW0028) for dispatching the events.
53 * We choose acpi_driver rather than platform_driver/i2c_driver because
54 * although VGBI has an i2c resource connected to i2c controller, it
55 * is not embedded in any i2c controller's scope, thus neither platform_device
56 * will be created, nor i2c_client will be enumerated, we have to use
59 static const struct acpi_device_id surface_button_device_ids[] = {
60 {SURFACE_PRO3_BUTTON_HID, 0},
61 {SURFACE_PRO4_BUTTON_HID, 0},
64 MODULE_DEVICE_TABLE(acpi, surface_button_device_ids);
66 struct surface_button {
68 struct input_dev *input;
69 char phys[32]; /* for input device */
74 static void surface_button_notify(struct acpi_device *device, u32 event)
76 struct surface_button *button = acpi_driver_data(device);
77 struct input_dev *input;
78 int key_code = KEY_RESERVED;
82 /* Power button press,release handle */
83 case SURFACE_BUTTON_NOTIFY_PRESS_POWER:
86 case SURFACE_BUTTON_NOTIFY_RELEASE_POWER:
89 /* Home button press,release handle */
90 case SURFACE_BUTTON_NOTIFY_PRESS_HOME:
93 case SURFACE_BUTTON_NOTIFY_RELEASE_HOME:
94 key_code = KEY_LEFTMETA;
96 /* Volume up button press,release handle */
97 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP:
100 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP:
101 key_code = KEY_VOLUMEUP;
103 /* Volume down button press,release handle */
104 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN:
107 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN:
108 key_code = KEY_VOLUMEDOWN;
110 case SURFACE_BUTTON_NOTIFY_TABLET_MODE:
111 dev_warn_once(&device->dev, "Tablet mode is not supported\n");
114 dev_info_ratelimited(&device->dev,
115 "Unsupported event [0x%x]\n", event);
118 input = button->input;
119 if (key_code == KEY_RESERVED)
122 pm_wakeup_dev_event(&device->dev, 0, button->suspended);
123 if (button->suspended)
125 input_report_key(input, key_code, pressed?1:0);
129 #ifdef CONFIG_PM_SLEEP
130 static int surface_button_suspend(struct device *dev)
132 struct acpi_device *device = to_acpi_device(dev);
133 struct surface_button *button = acpi_driver_data(device);
135 button->suspended = true;
139 static int surface_button_resume(struct device *dev)
141 struct acpi_device *device = to_acpi_device(dev);
142 struct surface_button *button = acpi_driver_data(device);
144 button->suspended = false;
150 * Surface Pro 4 and Surface Book 2 / Surface Pro 2017 use the same device
151 * ID (MSHW0040) for the power/volume buttons. Make sure this is the right
152 * device by checking for the _DSM method and OEM Platform Revision.
154 * Returns true if the driver should bind to this device, i.e. the device is
155 * either MSWH0028 (Pro 3) or MSHW0040 on a Pro 4 or Book 1.
157 static bool surface_button_check_MSHW0040(struct acpi_device *dev)
159 acpi_handle handle = dev->handle;
160 union acpi_object *result;
161 u64 oem_platform_rev = 0; // valid revisions are nonzero
163 // get OEM platform revision
164 result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
165 MSHW0040_DSM_REVISION,
166 MSHW0040_DSM_GET_OMPR,
167 NULL, ACPI_TYPE_INTEGER);
170 * If evaluating the _DSM fails, the method is not present. This means
171 * that we have either MSHW0028 or MSHW0040 on Pro 4 or Book 1, so we
172 * should use this driver. We use revision 0 indicating it is
177 oem_platform_rev = result->integer.value;
181 dev_dbg(&dev->dev, "OEM Platform Revision %llu\n", oem_platform_rev);
183 return oem_platform_rev == 0;
187 static int surface_button_add(struct acpi_device *device)
189 struct surface_button *button;
190 struct input_dev *input;
191 const char *hid = acpi_device_hid(device);
195 if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
196 strlen(SURFACE_BUTTON_OBJ_NAME)))
199 if (!surface_button_check_MSHW0040(device))
202 button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
206 device->driver_data = button;
207 button->input = input = input_allocate_device();
210 goto err_free_button;
213 name = acpi_device_name(device);
214 strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
215 snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
218 input->phys = button->phys;
219 input->id.bustype = BUS_HOST;
220 input->dev.parent = &device->dev;
221 input_set_capability(input, EV_KEY, KEY_POWER);
222 input_set_capability(input, EV_KEY, KEY_LEFTMETA);
223 input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
224 input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
226 error = input_register_device(input);
230 device_init_wakeup(&device->dev, true);
231 dev_info(&device->dev,
232 "%s [%s]\n", name, acpi_device_bid(device));
236 input_free_device(input);
242 static void surface_button_remove(struct acpi_device *device)
244 struct surface_button *button = acpi_driver_data(device);
246 input_unregister_device(button->input);
250 static SIMPLE_DEV_PM_OPS(surface_button_pm,
251 surface_button_suspend, surface_button_resume);
253 static struct acpi_driver surface_button_driver = {
254 .name = "surface_pro3_button",
255 .class = "SurfacePro3",
256 .ids = surface_button_device_ids,
258 .add = surface_button_add,
259 .remove = surface_button_remove,
260 .notify = surface_button_notify,
262 .drv.pm = &surface_button_pm,
265 module_acpi_driver(surface_button_driver);