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 SURFACE_BUTTON_NOTIFY_TABLET_MODE 0xc8
25 #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
26 #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
28 #define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
29 #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
31 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
32 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
34 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
35 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
37 ACPI_MODULE_NAME("surface pro 3 button");
39 MODULE_AUTHOR("Chen Yu");
40 MODULE_DESCRIPTION("Surface Pro3 Button Driver");
41 MODULE_LICENSE("GPL v2");
44 * Power button, Home button, Volume buttons support is supposed to
45 * be covered by drivers/input/misc/soc_button_array.c, which is implemented
46 * according to "Windows ACPI Design Guide for SoC Platforms".
47 * However surface pro3 seems not to obey the specs, instead it uses
48 * device VGBI(MSHW0028) for dispatching the events.
49 * We choose acpi_driver rather than platform_driver/i2c_driver because
50 * although VGBI has an i2c resource connected to i2c controller, it
51 * is not embedded in any i2c controller's scope, thus neither platform_device
52 * will be created, nor i2c_client will be enumerated, we have to use
55 static const struct acpi_device_id surface_button_device_ids[] = {
56 {SURFACE_PRO3_BUTTON_HID, 0},
57 {SURFACE_PRO4_BUTTON_HID, 0},
60 MODULE_DEVICE_TABLE(acpi, surface_button_device_ids);
62 struct surface_button {
64 struct input_dev *input;
65 char phys[32]; /* for input device */
70 static void surface_button_notify(struct acpi_device *device, u32 event)
72 struct surface_button *button = acpi_driver_data(device);
73 struct input_dev *input;
74 int key_code = KEY_RESERVED;
78 /* Power button press,release handle */
79 case SURFACE_BUTTON_NOTIFY_PRESS_POWER:
82 case SURFACE_BUTTON_NOTIFY_RELEASE_POWER:
85 /* Home button press,release handle */
86 case SURFACE_BUTTON_NOTIFY_PRESS_HOME:
89 case SURFACE_BUTTON_NOTIFY_RELEASE_HOME:
90 key_code = KEY_LEFTMETA;
92 /* Volume up button press,release handle */
93 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP:
96 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP:
97 key_code = KEY_VOLUMEUP;
99 /* Volume down button press,release handle */
100 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN:
103 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN:
104 key_code = KEY_VOLUMEDOWN;
106 case SURFACE_BUTTON_NOTIFY_TABLET_MODE:
107 dev_warn_once(&device->dev, "Tablet mode is not supported\n");
110 dev_info_ratelimited(&device->dev,
111 "Unsupported event [0x%x]\n", event);
114 input = button->input;
115 if (key_code == KEY_RESERVED)
118 pm_wakeup_dev_event(&device->dev, 0, button->suspended);
119 if (button->suspended)
121 input_report_key(input, key_code, pressed?1:0);
125 #ifdef CONFIG_PM_SLEEP
126 static int surface_button_suspend(struct device *dev)
128 struct acpi_device *device = to_acpi_device(dev);
129 struct surface_button *button = acpi_driver_data(device);
131 button->suspended = true;
135 static int surface_button_resume(struct device *dev)
137 struct acpi_device *device = to_acpi_device(dev);
138 struct surface_button *button = acpi_driver_data(device);
140 button->suspended = false;
145 static int surface_button_add(struct acpi_device *device)
147 struct surface_button *button;
148 struct input_dev *input;
149 const char *hid = acpi_device_hid(device);
153 if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
154 strlen(SURFACE_BUTTON_OBJ_NAME)))
157 button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
161 device->driver_data = button;
162 button->input = input = input_allocate_device();
165 goto err_free_button;
168 name = acpi_device_name(device);
169 strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
170 snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
173 input->phys = button->phys;
174 input->id.bustype = BUS_HOST;
175 input->dev.parent = &device->dev;
176 input_set_capability(input, EV_KEY, KEY_POWER);
177 input_set_capability(input, EV_KEY, KEY_LEFTMETA);
178 input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
179 input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
181 error = input_register_device(input);
185 device_init_wakeup(&device->dev, true);
186 dev_info(&device->dev,
187 "%s [%s]\n", name, acpi_device_bid(device));
191 input_free_device(input);
197 static int surface_button_remove(struct acpi_device *device)
199 struct surface_button *button = acpi_driver_data(device);
201 input_unregister_device(button->input);
206 static SIMPLE_DEV_PM_OPS(surface_button_pm,
207 surface_button_suspend, surface_button_resume);
209 static struct acpi_driver surface_button_driver = {
210 .name = "surface_pro3_button",
211 .class = "SurfacePro3",
212 .ids = surface_button_device_ids,
214 .add = surface_button_add,
215 .remove = surface_button_remove,
216 .notify = surface_button_notify,
218 .drv.pm = &surface_button_pm,
221 module_acpi_driver(surface_button_driver);