Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 2 | /* |
| 3 | * power/home/volume button support for |
Weng Xuetian | 6d5ac6e | 2016-01-17 15:10:38 -0800 | [diff] [blame] | 4 | * Microsoft Surface Pro 3/4 tablet. |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 Intel Corporation. |
| 7 | * All rights reserved. |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 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> |
| 17 | |
Weng Xuetian | 6d5ac6e | 2016-01-17 15:10:38 -0800 | [diff] [blame] | 18 | #define SURFACE_PRO3_BUTTON_HID "MSHW0028" |
| 19 | #define SURFACE_PRO4_BUTTON_HID "MSHW0040" |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 20 | #define SURFACE_BUTTON_OBJ_NAME "VGBI" |
Weng Xuetian | 6d5ac6e | 2016-01-17 15:10:38 -0800 | [diff] [blame] | 21 | #define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons" |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 22 | |
Andy Shevchenko | e27ffe7 | 2016-05-10 14:49:55 +0300 | [diff] [blame] | 23 | #define SURFACE_BUTTON_NOTIFY_TABLET_MODE 0xc8 |
| 24 | |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 25 | #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6 |
| 26 | #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7 |
| 27 | |
| 28 | #define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4 |
| 29 | #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5 |
| 30 | |
| 31 | #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0 |
| 32 | #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1 |
| 33 | |
Andy Shevchenko | e27ffe7 | 2016-05-10 14:49:55 +0300 | [diff] [blame] | 34 | #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2 |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 35 | #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3 |
| 36 | |
| 37 | ACPI_MODULE_NAME("surface pro 3 button"); |
| 38 | |
| 39 | MODULE_AUTHOR("Chen Yu"); |
| 40 | MODULE_DESCRIPTION("Surface Pro3 Button Driver"); |
| 41 | MODULE_LICENSE("GPL v2"); |
| 42 | |
| 43 | /* |
| 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 |
| 53 | * acpi_driver. |
| 54 | */ |
| 55 | static const struct acpi_device_id surface_button_device_ids[] = { |
Weng Xuetian | 6d5ac6e | 2016-01-17 15:10:38 -0800 | [diff] [blame] | 56 | {SURFACE_PRO3_BUTTON_HID, 0}, |
| 57 | {SURFACE_PRO4_BUTTON_HID, 0}, |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 58 | {"", 0}, |
| 59 | }; |
| 60 | MODULE_DEVICE_TABLE(acpi, surface_button_device_ids); |
| 61 | |
| 62 | struct surface_button { |
| 63 | unsigned int type; |
| 64 | struct input_dev *input; |
| 65 | char phys[32]; /* for input device */ |
| 66 | unsigned long pushed; |
| 67 | bool suspended; |
| 68 | }; |
| 69 | |
| 70 | static void surface_button_notify(struct acpi_device *device, u32 event) |
| 71 | { |
| 72 | struct surface_button *button = acpi_driver_data(device); |
| 73 | struct input_dev *input; |
| 74 | int key_code = KEY_RESERVED; |
| 75 | bool pressed = false; |
| 76 | |
| 77 | switch (event) { |
| 78 | /* Power button press,release handle */ |
| 79 | case SURFACE_BUTTON_NOTIFY_PRESS_POWER: |
| 80 | pressed = true; |
| 81 | /*fall through*/ |
| 82 | case SURFACE_BUTTON_NOTIFY_RELEASE_POWER: |
| 83 | key_code = KEY_POWER; |
| 84 | break; |
| 85 | /* Home button press,release handle */ |
| 86 | case SURFACE_BUTTON_NOTIFY_PRESS_HOME: |
| 87 | pressed = true; |
| 88 | /*fall through*/ |
| 89 | case SURFACE_BUTTON_NOTIFY_RELEASE_HOME: |
| 90 | key_code = KEY_LEFTMETA; |
| 91 | break; |
| 92 | /* Volume up button press,release handle */ |
| 93 | case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP: |
| 94 | pressed = true; |
| 95 | /*fall through*/ |
| 96 | case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP: |
| 97 | key_code = KEY_VOLUMEUP; |
| 98 | break; |
| 99 | /* Volume down button press,release handle */ |
| 100 | case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN: |
| 101 | pressed = true; |
| 102 | /*fall through*/ |
| 103 | case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN: |
| 104 | key_code = KEY_VOLUMEDOWN; |
| 105 | break; |
Andy Shevchenko | e27ffe7 | 2016-05-10 14:49:55 +0300 | [diff] [blame] | 106 | case SURFACE_BUTTON_NOTIFY_TABLET_MODE: |
| 107 | dev_warn_once(&device->dev, "Tablet mode is not supported\n"); |
| 108 | break; |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 109 | default: |
| 110 | dev_info_ratelimited(&device->dev, |
Andy Shevchenko | e27ffe7 | 2016-05-10 14:49:55 +0300 | [diff] [blame] | 111 | "Unsupported event [0x%x]\n", event); |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | input = button->input; |
Julia Lawall | 4bef0a2 | 2015-12-27 22:10:58 +0100 | [diff] [blame] | 115 | if (key_code == KEY_RESERVED) |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 116 | return; |
| 117 | if (pressed) |
Rafael J. Wysocki | 19351f3 | 2018-01-10 13:26:35 +0100 | [diff] [blame] | 118 | pm_wakeup_dev_event(&device->dev, 0, button->suspended); |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 119 | if (button->suspended) |
| 120 | return; |
| 121 | input_report_key(input, key_code, pressed?1:0); |
| 122 | input_sync(input); |
| 123 | } |
| 124 | |
| 125 | #ifdef CONFIG_PM_SLEEP |
| 126 | static int surface_button_suspend(struct device *dev) |
| 127 | { |
| 128 | struct acpi_device *device = to_acpi_device(dev); |
| 129 | struct surface_button *button = acpi_driver_data(device); |
| 130 | |
| 131 | button->suspended = true; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int surface_button_resume(struct device *dev) |
| 136 | { |
| 137 | struct acpi_device *device = to_acpi_device(dev); |
| 138 | struct surface_button *button = acpi_driver_data(device); |
| 139 | |
| 140 | button->suspended = false; |
| 141 | return 0; |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | static int surface_button_add(struct acpi_device *device) |
| 146 | { |
| 147 | struct surface_button *button; |
| 148 | struct input_dev *input; |
| 149 | const char *hid = acpi_device_hid(device); |
| 150 | char *name; |
| 151 | int error; |
| 152 | |
| 153 | if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME, |
| 154 | strlen(SURFACE_BUTTON_OBJ_NAME))) |
| 155 | return -ENODEV; |
| 156 | |
| 157 | button = kzalloc(sizeof(struct surface_button), GFP_KERNEL); |
| 158 | if (!button) |
| 159 | return -ENOMEM; |
| 160 | |
| 161 | device->driver_data = button; |
| 162 | button->input = input = input_allocate_device(); |
| 163 | if (!input) { |
| 164 | error = -ENOMEM; |
| 165 | goto err_free_button; |
| 166 | } |
| 167 | |
| 168 | name = acpi_device_name(device); |
| 169 | strcpy(name, SURFACE_BUTTON_DEVICE_NAME); |
| 170 | snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid); |
| 171 | |
| 172 | input->name = name; |
| 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); |
| 180 | |
| 181 | error = input_register_device(input); |
| 182 | if (error) |
| 183 | goto err_free_input; |
Rafael J. Wysocki | 19351f3 | 2018-01-10 13:26:35 +0100 | [diff] [blame] | 184 | |
| 185 | device_init_wakeup(&device->dev, true); |
Chen Yu | 2508a45 | 2015-08-18 23:30:25 +0800 | [diff] [blame] | 186 | dev_info(&device->dev, |
| 187 | "%s [%s]\n", name, acpi_device_bid(device)); |
| 188 | return 0; |
| 189 | |
| 190 | err_free_input: |
| 191 | input_free_device(input); |
| 192 | err_free_button: |
| 193 | kfree(button); |
| 194 | return error; |
| 195 | } |
| 196 | |
| 197 | static int surface_button_remove(struct acpi_device *device) |
| 198 | { |
| 199 | struct surface_button *button = acpi_driver_data(device); |
| 200 | |
| 201 | input_unregister_device(button->input); |
| 202 | kfree(button); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static SIMPLE_DEV_PM_OPS(surface_button_pm, |
| 207 | surface_button_suspend, surface_button_resume); |
| 208 | |
| 209 | static struct acpi_driver surface_button_driver = { |
| 210 | .name = "surface_pro3_button", |
| 211 | .class = "SurfacePro3", |
| 212 | .ids = surface_button_device_ids, |
| 213 | .ops = { |
| 214 | .add = surface_button_add, |
| 215 | .remove = surface_button_remove, |
| 216 | .notify = surface_button_notify, |
| 217 | }, |
| 218 | .drv.pm = &surface_button_pm, |
| 219 | }; |
| 220 | |
| 221 | module_acpi_driver(surface_button_driver); |