1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * power/home/volume button support for
4*4882a593Smuzhiyun * Microsoft Surface Pro 3/4 tablet.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (c) 2015 Intel Corporation.
7*4882a593Smuzhiyun * All rights reserved.
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <linux/input.h>
15*4882a593Smuzhiyun #include <linux/acpi.h>
16*4882a593Smuzhiyun #include <acpi/button.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define SURFACE_PRO3_BUTTON_HID "MSHW0028"
19*4882a593Smuzhiyun #define SURFACE_PRO4_BUTTON_HID "MSHW0040"
20*4882a593Smuzhiyun #define SURFACE_BUTTON_OBJ_NAME "VGBI"
21*4882a593Smuzhiyun #define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define MSHW0040_DSM_REVISION 0x01
24*4882a593Smuzhiyun #define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
25*4882a593Smuzhiyun static const guid_t MSHW0040_DSM_UUID =
26*4882a593Smuzhiyun GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
27*4882a593Smuzhiyun 0x49, 0x80, 0x35);
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_TABLET_MODE 0xc8
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
32*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
35*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
38*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
41*4882a593Smuzhiyun #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun ACPI_MODULE_NAME("surface pro 3 button");
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun MODULE_AUTHOR("Chen Yu");
46*4882a593Smuzhiyun MODULE_DESCRIPTION("Surface Pro3 Button Driver");
47*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun * Power button, Home button, Volume buttons support is supposed to
51*4882a593Smuzhiyun * be covered by drivers/input/misc/soc_button_array.c, which is implemented
52*4882a593Smuzhiyun * according to "Windows ACPI Design Guide for SoC Platforms".
53*4882a593Smuzhiyun * However surface pro3 seems not to obey the specs, instead it uses
54*4882a593Smuzhiyun * device VGBI(MSHW0028) for dispatching the events.
55*4882a593Smuzhiyun * We choose acpi_driver rather than platform_driver/i2c_driver because
56*4882a593Smuzhiyun * although VGBI has an i2c resource connected to i2c controller, it
57*4882a593Smuzhiyun * is not embedded in any i2c controller's scope, thus neither platform_device
58*4882a593Smuzhiyun * will be created, nor i2c_client will be enumerated, we have to use
59*4882a593Smuzhiyun * acpi_driver.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun static const struct acpi_device_id surface_button_device_ids[] = {
62*4882a593Smuzhiyun {SURFACE_PRO3_BUTTON_HID, 0},
63*4882a593Smuzhiyun {SURFACE_PRO4_BUTTON_HID, 0},
64*4882a593Smuzhiyun {"", 0},
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, surface_button_device_ids);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun struct surface_button {
69*4882a593Smuzhiyun unsigned int type;
70*4882a593Smuzhiyun struct input_dev *input;
71*4882a593Smuzhiyun char phys[32]; /* for input device */
72*4882a593Smuzhiyun unsigned long pushed;
73*4882a593Smuzhiyun bool suspended;
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun
surface_button_notify(struct acpi_device * device,u32 event)76*4882a593Smuzhiyun static void surface_button_notify(struct acpi_device *device, u32 event)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun struct surface_button *button = acpi_driver_data(device);
79*4882a593Smuzhiyun struct input_dev *input;
80*4882a593Smuzhiyun int key_code = KEY_RESERVED;
81*4882a593Smuzhiyun bool pressed = false;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun switch (event) {
84*4882a593Smuzhiyun /* Power button press,release handle */
85*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_PRESS_POWER:
86*4882a593Smuzhiyun pressed = true;
87*4882a593Smuzhiyun fallthrough;
88*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_RELEASE_POWER:
89*4882a593Smuzhiyun key_code = KEY_POWER;
90*4882a593Smuzhiyun break;
91*4882a593Smuzhiyun /* Home button press,release handle */
92*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_PRESS_HOME:
93*4882a593Smuzhiyun pressed = true;
94*4882a593Smuzhiyun fallthrough;
95*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_RELEASE_HOME:
96*4882a593Smuzhiyun key_code = KEY_LEFTMETA;
97*4882a593Smuzhiyun break;
98*4882a593Smuzhiyun /* Volume up button press,release handle */
99*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP:
100*4882a593Smuzhiyun pressed = true;
101*4882a593Smuzhiyun fallthrough;
102*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP:
103*4882a593Smuzhiyun key_code = KEY_VOLUMEUP;
104*4882a593Smuzhiyun break;
105*4882a593Smuzhiyun /* Volume down button press,release handle */
106*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN:
107*4882a593Smuzhiyun pressed = true;
108*4882a593Smuzhiyun fallthrough;
109*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN:
110*4882a593Smuzhiyun key_code = KEY_VOLUMEDOWN;
111*4882a593Smuzhiyun break;
112*4882a593Smuzhiyun case SURFACE_BUTTON_NOTIFY_TABLET_MODE:
113*4882a593Smuzhiyun dev_warn_once(&device->dev, "Tablet mode is not supported\n");
114*4882a593Smuzhiyun break;
115*4882a593Smuzhiyun default:
116*4882a593Smuzhiyun dev_info_ratelimited(&device->dev,
117*4882a593Smuzhiyun "Unsupported event [0x%x]\n", event);
118*4882a593Smuzhiyun break;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun input = button->input;
121*4882a593Smuzhiyun if (key_code == KEY_RESERVED)
122*4882a593Smuzhiyun return;
123*4882a593Smuzhiyun if (pressed)
124*4882a593Smuzhiyun pm_wakeup_dev_event(&device->dev, 0, button->suspended);
125*4882a593Smuzhiyun if (button->suspended)
126*4882a593Smuzhiyun return;
127*4882a593Smuzhiyun input_report_key(input, key_code, pressed?1:0);
128*4882a593Smuzhiyun input_sync(input);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
surface_button_suspend(struct device * dev)132*4882a593Smuzhiyun static int surface_button_suspend(struct device *dev)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun struct acpi_device *device = to_acpi_device(dev);
135*4882a593Smuzhiyun struct surface_button *button = acpi_driver_data(device);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun button->suspended = true;
138*4882a593Smuzhiyun return 0;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
surface_button_resume(struct device * dev)141*4882a593Smuzhiyun static int surface_button_resume(struct device *dev)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct acpi_device *device = to_acpi_device(dev);
144*4882a593Smuzhiyun struct surface_button *button = acpi_driver_data(device);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun button->suspended = false;
147*4882a593Smuzhiyun return 0;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun #endif
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun * Surface Pro 4 and Surface Book 2 / Surface Pro 2017 use the same device
153*4882a593Smuzhiyun * ID (MSHW0040) for the power/volume buttons. Make sure this is the right
154*4882a593Smuzhiyun * device by checking for the _DSM method and OEM Platform Revision.
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * Returns true if the driver should bind to this device, i.e. the device is
157*4882a593Smuzhiyun * either MSWH0028 (Pro 3) or MSHW0040 on a Pro 4 or Book 1.
158*4882a593Smuzhiyun */
surface_button_check_MSHW0040(struct acpi_device * dev)159*4882a593Smuzhiyun static bool surface_button_check_MSHW0040(struct acpi_device *dev)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun acpi_handle handle = dev->handle;
162*4882a593Smuzhiyun union acpi_object *result;
163*4882a593Smuzhiyun u64 oem_platform_rev = 0; // valid revisions are nonzero
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun // get OEM platform revision
166*4882a593Smuzhiyun result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
167*4882a593Smuzhiyun MSHW0040_DSM_REVISION,
168*4882a593Smuzhiyun MSHW0040_DSM_GET_OMPR,
169*4882a593Smuzhiyun NULL, ACPI_TYPE_INTEGER);
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun * If evaluating the _DSM fails, the method is not present. This means
173*4882a593Smuzhiyun * that we have either MSHW0028 or MSHW0040 on Pro 4 or Book 1, so we
174*4882a593Smuzhiyun * should use this driver. We use revision 0 indicating it is
175*4882a593Smuzhiyun * unavailable.
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun if (result) {
179*4882a593Smuzhiyun oem_platform_rev = result->integer.value;
180*4882a593Smuzhiyun ACPI_FREE(result);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun dev_dbg(&dev->dev, "OEM Platform Revision %llu\n", oem_platform_rev);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun return oem_platform_rev == 0;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun
surface_button_add(struct acpi_device * device)189*4882a593Smuzhiyun static int surface_button_add(struct acpi_device *device)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun struct surface_button *button;
192*4882a593Smuzhiyun struct input_dev *input;
193*4882a593Smuzhiyun const char *hid = acpi_device_hid(device);
194*4882a593Smuzhiyun char *name;
195*4882a593Smuzhiyun int error;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
198*4882a593Smuzhiyun strlen(SURFACE_BUTTON_OBJ_NAME)))
199*4882a593Smuzhiyun return -ENODEV;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun if (!surface_button_check_MSHW0040(device))
202*4882a593Smuzhiyun return -ENODEV;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
205*4882a593Smuzhiyun if (!button)
206*4882a593Smuzhiyun return -ENOMEM;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun device->driver_data = button;
209*4882a593Smuzhiyun button->input = input = input_allocate_device();
210*4882a593Smuzhiyun if (!input) {
211*4882a593Smuzhiyun error = -ENOMEM;
212*4882a593Smuzhiyun goto err_free_button;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun name = acpi_device_name(device);
216*4882a593Smuzhiyun strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
217*4882a593Smuzhiyun snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun input->name = name;
220*4882a593Smuzhiyun input->phys = button->phys;
221*4882a593Smuzhiyun input->id.bustype = BUS_HOST;
222*4882a593Smuzhiyun input->dev.parent = &device->dev;
223*4882a593Smuzhiyun input_set_capability(input, EV_KEY, KEY_POWER);
224*4882a593Smuzhiyun input_set_capability(input, EV_KEY, KEY_LEFTMETA);
225*4882a593Smuzhiyun input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
226*4882a593Smuzhiyun input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun error = input_register_device(input);
229*4882a593Smuzhiyun if (error)
230*4882a593Smuzhiyun goto err_free_input;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun device_init_wakeup(&device->dev, true);
233*4882a593Smuzhiyun dev_info(&device->dev,
234*4882a593Smuzhiyun "%s [%s]\n", name, acpi_device_bid(device));
235*4882a593Smuzhiyun return 0;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun err_free_input:
238*4882a593Smuzhiyun input_free_device(input);
239*4882a593Smuzhiyun err_free_button:
240*4882a593Smuzhiyun kfree(button);
241*4882a593Smuzhiyun return error;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
surface_button_remove(struct acpi_device * device)244*4882a593Smuzhiyun static int surface_button_remove(struct acpi_device *device)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun struct surface_button *button = acpi_driver_data(device);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun input_unregister_device(button->input);
249*4882a593Smuzhiyun kfree(button);
250*4882a593Smuzhiyun return 0;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(surface_button_pm,
254*4882a593Smuzhiyun surface_button_suspend, surface_button_resume);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun static struct acpi_driver surface_button_driver = {
257*4882a593Smuzhiyun .name = "surface_pro3_button",
258*4882a593Smuzhiyun .class = "SurfacePro3",
259*4882a593Smuzhiyun .ids = surface_button_device_ids,
260*4882a593Smuzhiyun .ops = {
261*4882a593Smuzhiyun .add = surface_button_add,
262*4882a593Smuzhiyun .remove = surface_button_remove,
263*4882a593Smuzhiyun .notify = surface_button_notify,
264*4882a593Smuzhiyun },
265*4882a593Smuzhiyun .drv.pm = &surface_button_pm,
266*4882a593Smuzhiyun };
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun module_acpi_driver(surface_button_driver);
269