Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / x86 / intel-vbtn.c
blob3b49a1f4061bcdaf689cdcda28980d3b924debd8
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Intel Virtual Button driver for Windows 8.1+
5 * Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com>
6 * Copyright (C) 2016 Alex Hung <alex.hung@canonical.com>
7 */
9 #include <linux/acpi.h>
10 #include <linux/dmi.h>
11 #include <linux/input.h>
12 #include <linux/input/sparse-keymap.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/suspend.h>
18 /* Returned when NOT in tablet mode on some HP Stream x360 11 models */
19 #define VGBS_TABLET_MODE_FLAG_ALT 0x10
20 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
21 #define VGBS_TABLET_MODE_FLAG 0x40
22 #define VGBS_DOCK_MODE_FLAG 0x80
24 #define VGBS_TABLET_MODE_FLAGS (VGBS_TABLET_MODE_FLAG | VGBS_TABLET_MODE_FLAG_ALT)
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("AceLan Kao");
29 static const struct acpi_device_id intel_vbtn_ids[] = {
30 {"INT33D6", 0},
31 {"", 0},
33 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
35 /* In theory, these are HID usages. */
36 static const struct key_entry intel_vbtn_keymap[] = {
37 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
38 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
39 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
40 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
41 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
42 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
43 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
44 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
45 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
46 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
49 static const struct key_entry intel_vbtn_switchmap[] = {
50 { KE_SW, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */
51 { KE_SW, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */
52 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
53 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
56 #define KEYMAP_LEN \
57 (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
59 struct intel_vbtn_priv {
60 struct key_entry keymap[KEYMAP_LEN];
61 struct input_dev *input_dev;
62 bool has_buttons;
63 bool has_switches;
64 bool wakeup_mode;
67 static void detect_tablet_mode(struct platform_device *device)
69 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
70 acpi_handle handle = ACPI_HANDLE(&device->dev);
71 unsigned long long vgbs;
72 acpi_status status;
73 int m;
75 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
76 if (ACPI_FAILURE(status))
77 return;
79 m = !(vgbs & VGBS_TABLET_MODE_FLAGS);
80 input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
81 m = (vgbs & VGBS_DOCK_MODE_FLAG) ? 1 : 0;
82 input_report_switch(priv->input_dev, SW_DOCK, m);
85 static int intel_vbtn_input_setup(struct platform_device *device)
87 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
88 int ret, keymap_len = 0;
90 if (priv->has_buttons) {
91 memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
92 ARRAY_SIZE(intel_vbtn_keymap) *
93 sizeof(struct key_entry));
94 keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
97 if (priv->has_switches) {
98 memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
99 ARRAY_SIZE(intel_vbtn_switchmap) *
100 sizeof(struct key_entry));
101 keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
104 priv->keymap[keymap_len].type = KE_END;
106 priv->input_dev = devm_input_allocate_device(&device->dev);
107 if (!priv->input_dev)
108 return -ENOMEM;
110 ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
111 if (ret)
112 return ret;
114 priv->input_dev->dev.parent = &device->dev;
115 priv->input_dev->name = "Intel Virtual Button driver";
116 priv->input_dev->id.bustype = BUS_HOST;
118 if (priv->has_switches)
119 detect_tablet_mode(device);
121 return input_register_device(priv->input_dev);
124 static void notify_handler(acpi_handle handle, u32 event, void *context)
126 struct platform_device *device = context;
127 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
128 unsigned int val = !(event & 1); /* Even=press, Odd=release */
129 const struct key_entry *ke, *ke_rel;
130 bool autorelease;
132 if (priv->wakeup_mode) {
133 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
134 if (ke) {
135 pm_wakeup_hard_event(&device->dev);
138 * Switch events like tablet mode will wake the device
139 * and report the new switch position to the input
140 * subsystem.
142 if (ke->type == KE_SW)
143 sparse_keymap_report_event(priv->input_dev,
144 event,
145 val,
147 return;
149 goto out_unknown;
153 * Even press events are autorelease if there is no corresponding odd
154 * release event, or if the odd event is KE_IGNORE.
156 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
157 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
159 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
160 return;
162 out_unknown:
163 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
166 static bool intel_vbtn_has_buttons(acpi_handle handle)
168 acpi_status status;
170 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
171 return ACPI_SUCCESS(status);
175 * There are several laptops (non 2-in-1) models out there which support VGBS,
176 * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
177 * turn causes userspace (libinput) to suppress events from the builtin
178 * keyboard and touchpad, making the laptop essentially unusable.
180 * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
181 * with libinput, leads to a non-usable system. Where as OTOH many people will
182 * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
183 * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
185 * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
186 * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
187 * these are matched on a per model basis, since many normal laptops with a
188 * possible broken VGBS ACPI-method also use these chassis-types.
190 static const struct dmi_system_id dmi_switches_allow_list[] = {
192 .matches = {
193 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
197 .matches = {
198 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
202 .matches = {
203 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
204 DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
208 .matches = {
209 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
210 DMI_MATCH(DMI_PRODUCT_NAME, "HP Stream x360 Convertible PC 11"),
214 .matches = {
215 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
216 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion 13 x360 PC"),
220 .matches = {
221 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
222 DMI_MATCH(DMI_PRODUCT_NAME, "Switch SA5-271"),
225 {} /* Array terminator */
228 static bool intel_vbtn_has_switches(acpi_handle handle)
230 unsigned long long vgbs;
231 acpi_status status;
233 if (!dmi_check_system(dmi_switches_allow_list))
234 return false;
236 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
237 return ACPI_SUCCESS(status);
240 static int intel_vbtn_probe(struct platform_device *device)
242 acpi_handle handle = ACPI_HANDLE(&device->dev);
243 bool has_buttons, has_switches;
244 struct intel_vbtn_priv *priv;
245 acpi_status status;
246 int err;
248 has_buttons = intel_vbtn_has_buttons(handle);
249 has_switches = intel_vbtn_has_switches(handle);
251 if (!has_buttons && !has_switches) {
252 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
253 return -ENODEV;
256 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
257 if (!priv)
258 return -ENOMEM;
259 dev_set_drvdata(&device->dev, priv);
261 priv->has_buttons = has_buttons;
262 priv->has_switches = has_switches;
264 err = intel_vbtn_input_setup(device);
265 if (err) {
266 pr_err("Failed to setup Intel Virtual Button\n");
267 return err;
270 status = acpi_install_notify_handler(handle,
271 ACPI_DEVICE_NOTIFY,
272 notify_handler,
273 device);
274 if (ACPI_FAILURE(status))
275 return -EBUSY;
277 device_init_wakeup(&device->dev, true);
279 * In order for system wakeup to work, the EC GPE has to be marked as
280 * a wakeup one, so do that here (this setting will persist, but it has
281 * no effect until the wakeup mask is set for the EC GPE).
283 acpi_ec_mark_gpe_for_wake();
284 return 0;
287 static int intel_vbtn_remove(struct platform_device *device)
289 acpi_handle handle = ACPI_HANDLE(&device->dev);
291 device_init_wakeup(&device->dev, false);
292 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
295 * Even if we failed to shut off the event stream, we can still
296 * safely detach from the device.
298 return 0;
301 static int intel_vbtn_pm_prepare(struct device *dev)
303 if (device_may_wakeup(dev)) {
304 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
306 priv->wakeup_mode = true;
308 return 0;
311 static void intel_vbtn_pm_complete(struct device *dev)
313 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
315 priv->wakeup_mode = false;
318 static int intel_vbtn_pm_resume(struct device *dev)
320 intel_vbtn_pm_complete(dev);
321 return 0;
324 static const struct dev_pm_ops intel_vbtn_pm_ops = {
325 .prepare = intel_vbtn_pm_prepare,
326 .complete = intel_vbtn_pm_complete,
327 .resume = intel_vbtn_pm_resume,
328 .restore = intel_vbtn_pm_resume,
329 .thaw = intel_vbtn_pm_resume,
332 static struct platform_driver intel_vbtn_pl_driver = {
333 .driver = {
334 .name = "intel-vbtn",
335 .acpi_match_table = intel_vbtn_ids,
336 .pm = &intel_vbtn_pm_ops,
338 .probe = intel_vbtn_probe,
339 .remove = intel_vbtn_remove,
342 static acpi_status __init
343 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
345 const struct acpi_device_id *ids = context;
346 struct acpi_device *dev;
348 if (acpi_bus_get_device(handle, &dev) != 0)
349 return AE_OK;
351 if (acpi_match_device_ids(dev, ids) == 0)
352 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
353 dev_info(&dev->dev,
354 "intel-vbtn: created platform device\n");
356 return AE_OK;
359 static int __init intel_vbtn_init(void)
361 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
362 ACPI_UINT32_MAX, check_acpi_dev, NULL,
363 (void *)intel_vbtn_ids, NULL);
365 return platform_driver_register(&intel_vbtn_pl_driver);
367 module_init(intel_vbtn_init);
369 static void __exit intel_vbtn_exit(void)
371 platform_driver_unregister(&intel_vbtn_pl_driver);
373 module_exit(intel_vbtn_exit);