2 * power/home/volume button support for
3 * Microsoft Surface Pro 3/4 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_PRO3_BUTTON_HID "MSHW0028"
23 #define SURFACE_PRO4_BUTTON_HID "MSHW0040"
24 #define SURFACE_BUTTON_OBJ_NAME "VGBI"
25 #define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
27 #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
28 #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
30 #define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
31 #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
33 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
34 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
36 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
37 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
39 ACPI_MODULE_NAME("surface pro 3 button");
41 MODULE_AUTHOR("Chen Yu");
42 MODULE_DESCRIPTION("Surface Pro3 Button Driver");
43 MODULE_LICENSE("GPL v2");
46 * Power button, Home button, Volume buttons support is supposed to
47 * be covered by drivers/input/misc/soc_button_array.c, which is implemented
48 * according to "Windows ACPI Design Guide for SoC Platforms".
49 * However surface pro3 seems not to obey the specs, instead it uses
50 * device VGBI(MSHW0028) for dispatching the events.
51 * We choose acpi_driver rather than platform_driver/i2c_driver because
52 * although VGBI has an i2c resource connected to i2c controller, it
53 * is not embedded in any i2c controller's scope, thus neither platform_device
54 * will be created, nor i2c_client will be enumerated, we have to use
57 static const struct acpi_device_id surface_button_device_ids
[] = {
58 {SURFACE_PRO3_BUTTON_HID
, 0},
59 {SURFACE_PRO4_BUTTON_HID
, 0},
62 MODULE_DEVICE_TABLE(acpi
, surface_button_device_ids
);
64 struct surface_button
{
66 struct input_dev
*input
;
67 char phys
[32]; /* for input device */
72 static void surface_button_notify(struct acpi_device
*device
, u32 event
)
74 struct surface_button
*button
= acpi_driver_data(device
);
75 struct input_dev
*input
;
76 int key_code
= KEY_RESERVED
;
80 /* Power button press,release handle */
81 case SURFACE_BUTTON_NOTIFY_PRESS_POWER
:
84 case SURFACE_BUTTON_NOTIFY_RELEASE_POWER
:
87 /* Home button press,release handle */
88 case SURFACE_BUTTON_NOTIFY_PRESS_HOME
:
91 case SURFACE_BUTTON_NOTIFY_RELEASE_HOME
:
92 key_code
= KEY_LEFTMETA
;
94 /* Volume up button press,release handle */
95 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP
:
98 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP
:
99 key_code
= KEY_VOLUMEUP
;
101 /* Volume down button press,release handle */
102 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN
:
105 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN
:
106 key_code
= KEY_VOLUMEDOWN
;
109 dev_info_ratelimited(&device
->dev
,
110 "Unsupported event [0x%x]\n", event
);
113 input
= button
->input
;
114 if (key_code
== KEY_RESERVED
)
117 pm_wakeup_event(&device
->dev
, 0);
118 if (button
->suspended
)
120 input_report_key(input
, key_code
, pressed
?1:0);
124 #ifdef CONFIG_PM_SLEEP
125 static int surface_button_suspend(struct device
*dev
)
127 struct acpi_device
*device
= to_acpi_device(dev
);
128 struct surface_button
*button
= acpi_driver_data(device
);
130 button
->suspended
= true;
134 static int surface_button_resume(struct device
*dev
)
136 struct acpi_device
*device
= to_acpi_device(dev
);
137 struct surface_button
*button
= acpi_driver_data(device
);
139 button
->suspended
= false;
144 static int surface_button_add(struct acpi_device
*device
)
146 struct surface_button
*button
;
147 struct input_dev
*input
;
148 const char *hid
= acpi_device_hid(device
);
152 if (strncmp(acpi_device_bid(device
), SURFACE_BUTTON_OBJ_NAME
,
153 strlen(SURFACE_BUTTON_OBJ_NAME
)))
156 button
= kzalloc(sizeof(struct surface_button
), GFP_KERNEL
);
160 device
->driver_data
= button
;
161 button
->input
= input
= input_allocate_device();
164 goto err_free_button
;
167 name
= acpi_device_name(device
);
168 strcpy(name
, SURFACE_BUTTON_DEVICE_NAME
);
169 snprintf(button
->phys
, sizeof(button
->phys
), "%s/buttons", hid
);
172 input
->phys
= button
->phys
;
173 input
->id
.bustype
= BUS_HOST
;
174 input
->dev
.parent
= &device
->dev
;
175 input_set_capability(input
, EV_KEY
, KEY_POWER
);
176 input_set_capability(input
, EV_KEY
, KEY_LEFTMETA
);
177 input_set_capability(input
, EV_KEY
, KEY_VOLUMEUP
);
178 input_set_capability(input
, EV_KEY
, KEY_VOLUMEDOWN
);
180 error
= input_register_device(input
);
183 dev_info(&device
->dev
,
184 "%s [%s]\n", name
, acpi_device_bid(device
));
188 input_free_device(input
);
194 static int surface_button_remove(struct acpi_device
*device
)
196 struct surface_button
*button
= acpi_driver_data(device
);
198 input_unregister_device(button
->input
);
203 static SIMPLE_DEV_PM_OPS(surface_button_pm
,
204 surface_button_suspend
, surface_button_resume
);
206 static struct acpi_driver surface_button_driver
= {
207 .name
= "surface_pro3_button",
208 .class = "SurfacePro3",
209 .ids
= surface_button_device_ids
,
211 .add
= surface_button_add
,
212 .remove
= surface_button_remove
,
213 .notify
= surface_button_notify
,
215 .drv
.pm
= &surface_button_pm
,
218 module_acpi_driver(surface_button_driver
);