Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / platform / x86 / intel-vbtn.c
blobc13780b8dabbe089c24a2b5c323f1510f589153e
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 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
19 #define TABLET_MODE_FLAG 0x40
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("AceLan Kao");
24 static const struct acpi_device_id intel_vbtn_ids[] = {
25 {"INT33D6", 0},
26 {"", 0},
29 /* In theory, these are HID usages. */
30 static const struct key_entry intel_vbtn_keymap[] = {
31 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
32 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
33 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
34 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
35 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
36 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
37 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
38 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
39 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
40 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
41 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
42 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
43 { KE_END },
46 struct intel_vbtn_priv {
47 struct input_dev *input_dev;
48 bool wakeup_mode;
51 static int intel_vbtn_input_setup(struct platform_device *device)
53 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
54 int ret;
56 priv->input_dev = devm_input_allocate_device(&device->dev);
57 if (!priv->input_dev)
58 return -ENOMEM;
60 ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
61 if (ret)
62 return ret;
64 priv->input_dev->dev.parent = &device->dev;
65 priv->input_dev->name = "Intel Virtual Button driver";
66 priv->input_dev->id.bustype = BUS_HOST;
68 return input_register_device(priv->input_dev);
71 static void notify_handler(acpi_handle handle, u32 event, void *context)
73 struct platform_device *device = context;
74 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
75 unsigned int val = !(event & 1); /* Even=press, Odd=release */
76 const struct key_entry *ke_rel;
77 bool autorelease;
79 if (priv->wakeup_mode) {
80 if (sparse_keymap_entry_from_scancode(priv->input_dev, event)) {
81 pm_wakeup_hard_event(&device->dev);
82 return;
84 goto out_unknown;
88 * Even press events are autorelease if there is no corresponding odd
89 * release event, or if the odd event is KE_IGNORE.
91 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
92 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
94 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
95 return;
97 out_unknown:
98 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
101 static void detect_tablet_mode(struct platform_device *device)
103 const char *chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
104 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
105 acpi_handle handle = ACPI_HANDLE(&device->dev);
106 struct acpi_buffer vgbs_output = { ACPI_ALLOCATE_BUFFER, NULL };
107 union acpi_object *obj;
108 acpi_status status;
109 int m;
111 if (!(chassis_type && strcmp(chassis_type, "31") == 0))
112 goto out;
114 status = acpi_evaluate_object(handle, "VGBS", NULL, &vgbs_output);
115 if (ACPI_FAILURE(status))
116 goto out;
118 obj = vgbs_output.pointer;
119 if (!(obj && obj->type == ACPI_TYPE_INTEGER))
120 goto out;
122 m = !(obj->integer.value & TABLET_MODE_FLAG);
123 input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
124 out:
125 kfree(vgbs_output.pointer);
128 static int intel_vbtn_probe(struct platform_device *device)
130 acpi_handle handle = ACPI_HANDLE(&device->dev);
131 struct intel_vbtn_priv *priv;
132 acpi_status status;
133 int err;
135 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
136 if (ACPI_FAILURE(status)) {
137 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
138 return -ENODEV;
141 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
142 if (!priv)
143 return -ENOMEM;
144 dev_set_drvdata(&device->dev, priv);
146 err = intel_vbtn_input_setup(device);
147 if (err) {
148 pr_err("Failed to setup Intel Virtual Button\n");
149 return err;
152 detect_tablet_mode(device);
154 status = acpi_install_notify_handler(handle,
155 ACPI_DEVICE_NOTIFY,
156 notify_handler,
157 device);
158 if (ACPI_FAILURE(status))
159 return -EBUSY;
161 device_init_wakeup(&device->dev, true);
162 return 0;
165 static int intel_vbtn_remove(struct platform_device *device)
167 acpi_handle handle = ACPI_HANDLE(&device->dev);
169 device_init_wakeup(&device->dev, false);
170 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
173 * Even if we failed to shut off the event stream, we can still
174 * safely detach from the device.
176 return 0;
179 static int intel_vbtn_pm_prepare(struct device *dev)
181 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
183 priv->wakeup_mode = true;
184 return 0;
187 static int intel_vbtn_pm_resume(struct device *dev)
189 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
191 priv->wakeup_mode = false;
192 return 0;
195 static const struct dev_pm_ops intel_vbtn_pm_ops = {
196 .prepare = intel_vbtn_pm_prepare,
197 .resume = intel_vbtn_pm_resume,
198 .restore = intel_vbtn_pm_resume,
199 .thaw = intel_vbtn_pm_resume,
202 static struct platform_driver intel_vbtn_pl_driver = {
203 .driver = {
204 .name = "intel-vbtn",
205 .acpi_match_table = intel_vbtn_ids,
206 .pm = &intel_vbtn_pm_ops,
208 .probe = intel_vbtn_probe,
209 .remove = intel_vbtn_remove,
211 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
213 static acpi_status __init
214 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
216 const struct acpi_device_id *ids = context;
217 struct acpi_device *dev;
219 if (acpi_bus_get_device(handle, &dev) != 0)
220 return AE_OK;
222 if (acpi_match_device_ids(dev, ids) == 0)
223 if (acpi_create_platform_device(dev, NULL))
224 dev_info(&dev->dev,
225 "intel-vbtn: created platform device\n");
227 return AE_OK;
230 static int __init intel_vbtn_init(void)
232 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
233 ACPI_UINT32_MAX, check_acpi_dev, NULL,
234 (void *)intel_vbtn_ids, NULL);
236 return platform_driver_register(&intel_vbtn_pl_driver);
238 module_init(intel_vbtn_init);
240 static void __exit intel_vbtn_exit(void)
242 platform_driver_unregister(&intel_vbtn_pl_driver);
244 module_exit(intel_vbtn_exit);