blob: 634096cef21ae838d147b307285359d6c05b00f4 [file] [log] [blame]
Andy Shevchenko00f49ee2018-01-31 19:54:07 +02001// SPDX-License-Identifier: GPL-2.0+
AceLan Kao332e0812016-07-01 09:51:49 +08002/*
3 * Intel Virtual Button driver for Windows 8.1+
4 *
5 * Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com>
6 * Copyright (C) 2016 Alex Hung <alex.hung@canonical.com>
AceLan Kao332e0812016-07-01 09:51:49 +08007 */
8
Andy Shevchenkobd1b27e2018-01-31 19:54:06 +02009#include <linux/acpi.h>
Mario Limonciellode9647e2018-02-22 13:58:42 -060010#include <linux/dmi.h>
Andy Shevchenkobd1b27e2018-01-31 19:54:06 +020011#include <linux/input.h>
12#include <linux/input/sparse-keymap.h>
AceLan Kao332e0812016-07-01 09:51:49 +080013#include <linux/kernel.h>
14#include <linux/module.h>
AceLan Kao332e0812016-07-01 09:51:49 +080015#include <linux/platform_device.h>
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +020016#include <linux/suspend.h>
AceLan Kao332e0812016-07-01 09:51:49 +080017
Marco Martin30323fb2018-01-29 21:27:43 +010018/* When NOT in tablet mode, VGBS returns with the flag 0x40 */
19#define TABLET_MODE_FLAG 0x40
Matt Delco4f5e81a2018-06-24 17:29:40 -070020#define DOCK_MODE_FLAG 0x80
Marco Martin30323fb2018-01-29 21:27:43 +010021
AceLan Kao332e0812016-07-01 09:51:49 +080022MODULE_LICENSE("GPL");
23MODULE_AUTHOR("AceLan Kao");
24
25static const struct acpi_device_id intel_vbtn_ids[] = {
26 {"INT33D6", 0},
27 {"", 0},
28};
Andy Shevchenko807e92d2020-01-28 12:26:21 +020029MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
AceLan Kao332e0812016-07-01 09:51:49 +080030
31/* In theory, these are HID usages. */
32static const struct key_entry intel_vbtn_keymap[] = {
Mario Limoncielloc8016032017-08-04 12:00:06 -050033 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
34 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
Stefan Brüns9678d0e2017-11-09 23:44:36 +010035 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
36 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
Maarten Maathuis8d9e2992017-04-24 23:35:21 +020037 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
38 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
39 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
40 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
Stefan Brüns3d5d95d2017-11-09 23:44:35 +010041 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
42 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
Hans de Goedef6ba5242020-05-02 20:29:48 +020043};
44
45static const struct key_entry intel_vbtn_switchmap[] = {
Matt Delco4f5e81a2018-06-24 17:29:40 -070046 { KE_SW, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */
47 { KE_SW, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */
Stefan Brüns1c828492017-11-09 23:44:32 +010048 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
49 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
AceLan Kao332e0812016-07-01 09:51:49 +080050};
51
Hans de Goedef6ba5242020-05-02 20:29:48 +020052#define KEYMAP_LEN \
53 (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
54
AceLan Kao332e0812016-07-01 09:51:49 +080055struct intel_vbtn_priv {
Hans de Goedef6ba5242020-05-02 20:29:48 +020056 struct key_entry keymap[KEYMAP_LEN];
AceLan Kao332e0812016-07-01 09:51:49 +080057 struct input_dev *input_dev;
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +020058 bool wakeup_mode;
AceLan Kao332e0812016-07-01 09:51:49 +080059};
60
61static int intel_vbtn_input_setup(struct platform_device *device)
62{
63 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
Hans de Goedef6ba5242020-05-02 20:29:48 +020064 int ret, keymap_len = 0;
65
66 if (true) {
67 memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
68 ARRAY_SIZE(intel_vbtn_keymap) *
69 sizeof(struct key_entry));
70 keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
71 }
72
73 if (true) {
74 memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
75 ARRAY_SIZE(intel_vbtn_switchmap) *
76 sizeof(struct key_entry));
77 keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
78 }
79
80 priv->keymap[keymap_len].type = KE_END;
AceLan Kao332e0812016-07-01 09:51:49 +080081
Axel Linbb9ad482016-09-20 17:01:24 +080082 priv->input_dev = devm_input_allocate_device(&device->dev);
AceLan Kao332e0812016-07-01 09:51:49 +080083 if (!priv->input_dev)
84 return -ENOMEM;
85
Hans de Goedef6ba5242020-05-02 20:29:48 +020086 ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
AceLan Kao332e0812016-07-01 09:51:49 +080087 if (ret)
Axel Linbb9ad482016-09-20 17:01:24 +080088 return ret;
AceLan Kao332e0812016-07-01 09:51:49 +080089
90 priv->input_dev->dev.parent = &device->dev;
91 priv->input_dev->name = "Intel Virtual Button driver";
92 priv->input_dev->id.bustype = BUS_HOST;
93
Axel Linbb9ad482016-09-20 17:01:24 +080094 return input_register_device(priv->input_dev);
AceLan Kao332e0812016-07-01 09:51:49 +080095}
96
97static void notify_handler(acpi_handle handle, u32 event, void *context)
98{
99 struct platform_device *device = context;
100 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
Darren Hart (VMware)1c3fdf12017-12-08 14:57:54 -0800101 unsigned int val = !(event & 1); /* Even=press, Odd=release */
Mathew Kingcb1921b2019-05-20 16:41:24 -0600102 const struct key_entry *ke, *ke_rel;
Stefan Brüns95f38fd2017-11-09 23:44:33 +0100103 bool autorelease;
AceLan Kao332e0812016-07-01 09:51:49 +0800104
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200105 if (priv->wakeup_mode) {
Mathew Kingcb1921b2019-05-20 16:41:24 -0600106 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
107 if (ke) {
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200108 pm_wakeup_hard_event(&device->dev);
Mathew Kingcb1921b2019-05-20 16:41:24 -0600109
110 /*
111 * Switch events like tablet mode will wake the device
112 * and report the new switch position to the input
113 * subsystem.
114 */
115 if (ke->type == KE_SW)
116 sparse_keymap_report_event(priv->input_dev,
117 event,
118 val,
119 0);
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200120 return;
121 }
Darren Hart (VMware)1c3fdf12017-12-08 14:57:54 -0800122 goto out_unknown;
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200123 }
Darren Hart (VMware)1c3fdf12017-12-08 14:57:54 -0800124
125 /*
126 * Even press events are autorelease if there is no corresponding odd
127 * release event, or if the odd event is KE_IGNORE.
128 */
129 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
130 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
131
132 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
133 return;
134
135out_unknown:
Alex Hunga9c37b72017-07-20 20:56:48 -0700136 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
AceLan Kao332e0812016-07-01 09:51:49 +0800137}
138
Mario Limonciellode9647e2018-02-22 13:58:42 -0600139static void detect_tablet_mode(struct platform_device *device)
140{
141 const char *chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
142 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
143 acpi_handle handle = ACPI_HANDLE(&device->dev);
Hans de Goede18937872020-05-02 20:29:47 +0200144 unsigned long long vgbs;
Mario Limonciellode9647e2018-02-22 13:58:42 -0600145 acpi_status status;
146 int m;
147
148 if (!(chassis_type && strcmp(chassis_type, "31") == 0))
Hans de Goede18937872020-05-02 20:29:47 +0200149 return;
Mario Limonciellode9647e2018-02-22 13:58:42 -0600150
Hans de Goede18937872020-05-02 20:29:47 +0200151 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
Mario Limonciellode9647e2018-02-22 13:58:42 -0600152 if (ACPI_FAILURE(status))
Hans de Goede18937872020-05-02 20:29:47 +0200153 return;
Mario Limonciellode9647e2018-02-22 13:58:42 -0600154
Hans de Goede18937872020-05-02 20:29:47 +0200155 m = !(vgbs & TABLET_MODE_FLAG);
Mario Limonciellode9647e2018-02-22 13:58:42 -0600156 input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
Hans de Goede18937872020-05-02 20:29:47 +0200157 m = (vgbs & DOCK_MODE_FLAG) ? 1 : 0;
Matt Delco4f5e81a2018-06-24 17:29:40 -0700158 input_report_switch(priv->input_dev, SW_DOCK, m);
Mario Limonciellode9647e2018-02-22 13:58:42 -0600159}
160
AceLan Kao332e0812016-07-01 09:51:49 +0800161static int intel_vbtn_probe(struct platform_device *device)
162{
163 acpi_handle handle = ACPI_HANDLE(&device->dev);
164 struct intel_vbtn_priv *priv;
165 acpi_status status;
166 int err;
167
168 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
Axel Lin3526eca2016-09-19 09:33:51 +0800169 if (ACPI_FAILURE(status)) {
AceLan Kao332e0812016-07-01 09:51:49 +0800170 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
171 return -ENODEV;
172 }
173
174 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
175 if (!priv)
176 return -ENOMEM;
177 dev_set_drvdata(&device->dev, priv);
178
179 err = intel_vbtn_input_setup(device);
180 if (err) {
181 pr_err("Failed to setup Intel Virtual Button\n");
182 return err;
183 }
184
Mario Limonciellode9647e2018-02-22 13:58:42 -0600185 detect_tablet_mode(device);
Marco Martin30323fb2018-01-29 21:27:43 +0100186
AceLan Kao332e0812016-07-01 09:51:49 +0800187 status = acpi_install_notify_handler(handle,
188 ACPI_DEVICE_NOTIFY,
189 notify_handler,
190 device);
Axel Linbb9ad482016-09-20 17:01:24 +0800191 if (ACPI_FAILURE(status))
192 return -EBUSY;
AceLan Kao332e0812016-07-01 09:51:49 +0800193
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200194 device_init_wakeup(&device->dev, true);
Rafael J. Wysocki10a08fd2019-07-30 11:55:59 +0200195 /*
196 * In order for system wakeup to work, the EC GPE has to be marked as
197 * a wakeup one, so do that here (this setting will persist, but it has
198 * no effect until the wakeup mask is set for the EC GPE).
199 */
200 acpi_ec_mark_gpe_for_wake();
AceLan Kao332e0812016-07-01 09:51:49 +0800201 return 0;
AceLan Kao332e0812016-07-01 09:51:49 +0800202}
203
204static int intel_vbtn_remove(struct platform_device *device)
205{
206 acpi_handle handle = ACPI_HANDLE(&device->dev);
207
Rafael J. Wysockib758dbd2018-02-28 12:09:56 +0100208 device_init_wakeup(&device->dev, false);
AceLan Kao332e0812016-07-01 09:51:49 +0800209 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
210
211 /*
212 * Even if we failed to shut off the event stream, we can still
213 * safely detach from the device.
214 */
215 return 0;
216}
217
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200218static int intel_vbtn_pm_prepare(struct device *dev)
219{
Rafael J. Wysocki10a08fd2019-07-30 11:55:59 +0200220 if (device_may_wakeup(dev)) {
221 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200222
Rafael J. Wysocki10a08fd2019-07-30 11:55:59 +0200223 priv->wakeup_mode = true;
Rafael J. Wysocki10a08fd2019-07-30 11:55:59 +0200224 }
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200225 return 0;
226}
227
Rafael J. Wysocki31eb8452019-08-08 11:39:17 +0200228static void intel_vbtn_pm_complete(struct device *dev)
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200229{
Rafael J. Wysocki31eb8452019-08-08 11:39:17 +0200230 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200231
Rafael J. Wysockib90ff352019-08-21 11:40:19 +0200232 priv->wakeup_mode = false;
Rafael J. Wysocki31eb8452019-08-08 11:39:17 +0200233}
234
235static int intel_vbtn_pm_resume(struct device *dev)
236{
237 intel_vbtn_pm_complete(dev);
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200238 return 0;
239}
240
241static const struct dev_pm_ops intel_vbtn_pm_ops = {
242 .prepare = intel_vbtn_pm_prepare,
Rafael J. Wysocki31eb8452019-08-08 11:39:17 +0200243 .complete = intel_vbtn_pm_complete,
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200244 .resume = intel_vbtn_pm_resume,
245 .restore = intel_vbtn_pm_resume,
246 .thaw = intel_vbtn_pm_resume,
247};
248
AceLan Kao332e0812016-07-01 09:51:49 +0800249static struct platform_driver intel_vbtn_pl_driver = {
250 .driver = {
251 .name = "intel-vbtn",
252 .acpi_match_table = intel_vbtn_ids,
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200253 .pm = &intel_vbtn_pm_ops,
AceLan Kao332e0812016-07-01 09:51:49 +0800254 },
255 .probe = intel_vbtn_probe,
256 .remove = intel_vbtn_remove,
257};
AceLan Kao332e0812016-07-01 09:51:49 +0800258
259static acpi_status __init
260check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
261{
262 const struct acpi_device_id *ids = context;
263 struct acpi_device *dev;
264
265 if (acpi_bus_get_device(handle, &dev) != 0)
266 return AE_OK;
267
268 if (acpi_match_device_ids(dev, ids) == 0)
Heikki Krogerus15718752016-11-03 16:21:26 +0200269 if (acpi_create_platform_device(dev, NULL))
AceLan Kao332e0812016-07-01 09:51:49 +0800270 dev_info(&dev->dev,
271 "intel-vbtn: created platform device\n");
272
273 return AE_OK;
274}
275
276static int __init intel_vbtn_init(void)
277{
278 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
279 ACPI_UINT32_MAX, check_acpi_dev, NULL,
280 (void *)intel_vbtn_ids, NULL);
281
282 return platform_driver_register(&intel_vbtn_pl_driver);
283}
284module_init(intel_vbtn_init);
285
286static void __exit intel_vbtn_exit(void)
287{
288 platform_driver_unregister(&intel_vbtn_pl_driver);
289}
290module_exit(intel_vbtn_exit);