1 /*
2  * power/home/volume button support for
3  * Microsoft Surface Pro 3 tablet.
4  *
5  * Copyright (c) 2015 Intel Corporation.
6  * All rights reserved.
7  *
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
11  * of the License.
12  */
13 
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>
21 
22 #define SURFACE_BUTTON_HID		"MSHW0028"
23 #define SURFACE_BUTTON_OBJ_NAME		"VGBI"
24 #define SURFACE_BUTTON_DEVICE_NAME	"Surface Pro 3 Buttons"
25 
26 #define SURFACE_BUTTON_NOTIFY_PRESS_POWER	0xc6
27 #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER	0xc7
28 
29 #define SURFACE_BUTTON_NOTIFY_PRESS_HOME	0xc4
30 #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME	0xc5
31 
32 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP	0xc0
33 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP	0xc1
34 
35 #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN	0xc2
36 #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN	0xc3
37 
38 ACPI_MODULE_NAME("surface pro 3 button");
39 
40 MODULE_AUTHOR("Chen Yu");
41 MODULE_DESCRIPTION("Surface Pro3 Button Driver");
42 MODULE_LICENSE("GPL v2");
43 
44 /*
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
54  * acpi_driver.
55  */
56 static const struct acpi_device_id surface_button_device_ids[] = {
57 	{SURFACE_BUTTON_HID,    0},
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 
surface_button_notify(struct acpi_device * device,u32 event)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;
106 	default:
107 		dev_info_ratelimited(&device->dev,
108 				  "Unsupported event [0x%x]\n", event);
109 		break;
110 	}
111 	input = button->input;
112 	if (KEY_RESERVED == key_code)
113 		return;
114 	if (pressed)
115 		pm_wakeup_event(&device->dev, 0);
116 	if (button->suspended)
117 		return;
118 	input_report_key(input, key_code, pressed?1:0);
119 	input_sync(input);
120 }
121 
122 #ifdef CONFIG_PM_SLEEP
surface_button_suspend(struct device * dev)123 static int surface_button_suspend(struct device *dev)
124 {
125 	struct acpi_device *device = to_acpi_device(dev);
126 	struct surface_button *button = acpi_driver_data(device);
127 
128 	button->suspended = true;
129 	return 0;
130 }
131 
surface_button_resume(struct device * dev)132 static int surface_button_resume(struct device *dev)
133 {
134 	struct acpi_device *device = to_acpi_device(dev);
135 	struct surface_button *button = acpi_driver_data(device);
136 
137 	button->suspended = false;
138 	return 0;
139 }
140 #endif
141 
surface_button_add(struct acpi_device * device)142 static int surface_button_add(struct acpi_device *device)
143 {
144 	struct surface_button *button;
145 	struct input_dev *input;
146 	const char *hid = acpi_device_hid(device);
147 	char *name;
148 	int error;
149 
150 	if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
151 	    strlen(SURFACE_BUTTON_OBJ_NAME)))
152 		return -ENODEV;
153 
154 	button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
155 	if (!button)
156 		return -ENOMEM;
157 
158 	device->driver_data = button;
159 	button->input = input = input_allocate_device();
160 	if (!input) {
161 		error = -ENOMEM;
162 		goto err_free_button;
163 	}
164 
165 	name = acpi_device_name(device);
166 	strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
167 	snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
168 
169 	input->name = name;
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);
177 
178 	error = input_register_device(input);
179 	if (error)
180 		goto err_free_input;
181 	dev_info(&device->dev,
182 			"%s [%s]\n", name, acpi_device_bid(device));
183 	return 0;
184 
185  err_free_input:
186 	input_free_device(input);
187  err_free_button:
188 	kfree(button);
189 	return error;
190 }
191 
surface_button_remove(struct acpi_device * device)192 static int surface_button_remove(struct acpi_device *device)
193 {
194 	struct surface_button *button = acpi_driver_data(device);
195 
196 	input_unregister_device(button->input);
197 	kfree(button);
198 	return 0;
199 }
200 
201 static SIMPLE_DEV_PM_OPS(surface_button_pm,
202 		surface_button_suspend, surface_button_resume);
203 
204 static struct acpi_driver surface_button_driver = {
205 	.name = "surface_pro3_button",
206 	.class = "SurfacePro3",
207 	.ids = surface_button_device_ids,
208 	.ops = {
209 		.add = surface_button_add,
210 		.remove = surface_button_remove,
211 		.notify = surface_button_notify,
212 	},
213 	.drv.pm = &surface_button_pm,
214 };
215 
216 module_acpi_driver(surface_button_driver);
217