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