2 * power/home/volume button support for
3 * Microsoft Surface Pro 3 tablet.
5 * Copyright (c) 2015 Intel Corporation.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/types.h>
18 #include <linux/input.h>
19 #include <linux/acpi.h>
20 #include <acpi/button.h>
22 #define SURFACE_BUTTON_HID "MSHW0028"
23 #define SURFACE_BUTTON_OBJ_NAME "VGBI"
24 #define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3 Buttons"
26 #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
27 #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
29 #define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
30 #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
32 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
33 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
35 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
36 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
38 ACPI_MODULE_NAME("surface pro 3 button");
40 MODULE_AUTHOR("Chen Yu");
41 MODULE_DESCRIPTION("Surface Pro3 Button Driver");
42 MODULE_LICENSE("GPL v2");
45 * Power button, Home button, Volume buttons support is supposed to
46 * be covered by drivers/input/misc/soc_button_array.c, which is implemented
47 * according to "Windows ACPI Design Guide for SoC Platforms".
48 * However surface pro3 seems not to obey the specs, instead it uses
49 * device VGBI(MSHW0028) for dispatching the events.
50 * We choose acpi_driver rather than platform_driver/i2c_driver because
51 * although VGBI has an i2c resource connected to i2c controller, it
52 * is not embedded in any i2c controller's scope, thus neither platform_device
53 * will be created, nor i2c_client will be enumerated, we have to use
56 static const struct acpi_device_id surface_button_device_ids[] = {
57 {SURFACE_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;
107 dev_info_ratelimited(&device->dev,
108 "Unsupported event [0x%x]\n", event);
111 input = button->input;
112 if (KEY_RESERVED == key_code)
115 pm_wakeup_event(&device->dev, 0);
116 if (button->suspended)
118 input_report_key(input, key_code, pressed?1:0);
122 #ifdef CONFIG_PM_SLEEP
123 static int surface_button_suspend(struct device *dev)
125 struct acpi_device *device = to_acpi_device(dev);
126 struct surface_button *button = acpi_driver_data(device);
128 button->suspended = true;
132 static int surface_button_resume(struct device *dev)
134 struct acpi_device *device = to_acpi_device(dev);
135 struct surface_button *button = acpi_driver_data(device);
137 button->suspended = false;
142 static int surface_button_add(struct acpi_device *device)
144 struct surface_button *button;
145 struct input_dev *input;
146 const char *hid = acpi_device_hid(device);
150 if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
151 strlen(SURFACE_BUTTON_OBJ_NAME)))
154 button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
158 device->driver_data = button;
159 button->input = input = input_allocate_device();
162 goto err_free_button;
165 name = acpi_device_name(device);
166 strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
167 snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
170 input->phys = button->phys;
171 input->id.bustype = BUS_HOST;
172 input->dev.parent = &device->dev;
173 input_set_capability(input, EV_KEY, KEY_POWER);
174 input_set_capability(input, EV_KEY, KEY_LEFTMETA);
175 input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
176 input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
178 error = input_register_device(input);
181 dev_info(&device->dev,
182 "%s [%s]\n", name, acpi_device_bid(device));
186 input_free_device(input);
192 static int surface_button_remove(struct acpi_device *device)
194 struct surface_button *button = acpi_driver_data(device);
196 input_unregister_device(button->input);
201 static SIMPLE_DEV_PM_OPS(surface_button_pm,
202 surface_button_suspend, surface_button_resume);
204 static struct acpi_driver surface_button_driver = {
205 .name = "surface_pro3_button",
206 .class = "SurfacePro3",
207 .ids = surface_button_device_ids,
209 .add = surface_button_add,
210 .remove = surface_button_remove,
211 .notify = surface_button_notify,
213 .drv.pm = &surface_button_pm,
216 module_acpi_driver(surface_button_driver);