mtd: rawnand: mtk: Fix WAITRDY break condition and timeout
[platform/kernel/linux-rpi.git] / drivers / platform / x86 / intel-vbtn.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
7  */
8
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>
17
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
23
24 #define VGBS_TABLET_MODE_FLAGS (VGBS_TABLET_MODE_FLAG | VGBS_TABLET_MODE_FLAG_ALT)
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("AceLan Kao");
28
29 static const struct acpi_device_id intel_vbtn_ids[] = {
30         {"INT33D6", 0},
31         {"", 0},
32 };
33 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
34
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 */
47         { KE_END }
48 };
49
50 static const struct key_entry intel_vbtn_switchmap[] = {
51         { KE_SW,     0xCA, { .sw = { SW_DOCK, 1 } } },          /* Docked */
52         { KE_SW,     0xCB, { .sw = { SW_DOCK, 0 } } },          /* Undocked */
53         { KE_SW,     0xCC, { .sw = { SW_TABLET_MODE, 1 } } },   /* Tablet */
54         { KE_SW,     0xCD, { .sw = { SW_TABLET_MODE, 0 } } },   /* Laptop */
55         { KE_END }
56 };
57
58 #define KEYMAP_LEN \
59         (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
60
61 struct intel_vbtn_priv {
62         struct input_dev *buttons_dev;
63         struct input_dev *switches_dev;
64         bool has_buttons;
65         bool has_switches;
66         bool wakeup_mode;
67 };
68
69 static void detect_tablet_mode(struct platform_device *device)
70 {
71         struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
72         acpi_handle handle = ACPI_HANDLE(&device->dev);
73         unsigned long long vgbs;
74         acpi_status status;
75         int m;
76
77         status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
78         if (ACPI_FAILURE(status))
79                 return;
80
81         m = !(vgbs & VGBS_TABLET_MODE_FLAGS);
82         input_report_switch(priv->switches_dev, SW_TABLET_MODE, m);
83         m = (vgbs & VGBS_DOCK_MODE_FLAG) ? 1 : 0;
84         input_report_switch(priv->switches_dev, SW_DOCK, m);
85 }
86
87 /*
88  * Note this unconditionally creates the 2 input_dev-s and sets up
89  * the sparse-keymaps. Only the registration is conditional on
90  * have_buttons / have_switches. This is done so that the notify
91  * handler can always call sparse_keymap_entry_from_scancode()
92  * on the input_dev-s do determine the event type.
93  */
94 static int intel_vbtn_input_setup(struct platform_device *device)
95 {
96         struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
97         int ret;
98
99         priv->buttons_dev = devm_input_allocate_device(&device->dev);
100         if (!priv->buttons_dev)
101                 return -ENOMEM;
102
103         ret = sparse_keymap_setup(priv->buttons_dev, intel_vbtn_keymap, NULL);
104         if (ret)
105                 return ret;
106
107         priv->buttons_dev->dev.parent = &device->dev;
108         priv->buttons_dev->name = "Intel Virtual Buttons";
109         priv->buttons_dev->id.bustype = BUS_HOST;
110
111         if (priv->has_buttons) {
112                 ret = input_register_device(priv->buttons_dev);
113                 if (ret)
114                         return ret;
115         }
116
117         priv->switches_dev = devm_input_allocate_device(&device->dev);
118         if (!priv->switches_dev)
119                 return -ENOMEM;
120
121         ret = sparse_keymap_setup(priv->switches_dev, intel_vbtn_switchmap, NULL);
122         if (ret)
123                 return ret;
124
125         priv->switches_dev->dev.parent = &device->dev;
126         priv->switches_dev->name = "Intel Virtual Switches";
127         priv->switches_dev->id.bustype = BUS_HOST;
128
129         if (priv->has_switches) {
130                 detect_tablet_mode(device);
131
132                 ret = input_register_device(priv->switches_dev);
133                 if (ret)
134                         return ret;
135         }
136
137         return 0;
138 }
139
140 static void notify_handler(acpi_handle handle, u32 event, void *context)
141 {
142         struct platform_device *device = context;
143         struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
144         unsigned int val = !(event & 1); /* Even=press, Odd=release */
145         const struct key_entry *ke, *ke_rel;
146         struct input_dev *input_dev;
147         bool autorelease;
148         int ret;
149
150         if ((ke = sparse_keymap_entry_from_scancode(priv->buttons_dev, event))) {
151                 if (!priv->has_buttons) {
152                         dev_warn(&device->dev, "Warning: received a button event on a device without buttons, please report this.\n");
153                         return;
154                 }
155                 input_dev = priv->buttons_dev;
156         } else if ((ke = sparse_keymap_entry_from_scancode(priv->switches_dev, event))) {
157                 if (!priv->has_switches) {
158                         dev_info(&device->dev, "Registering Intel Virtual Switches input-dev after receiving a switch event\n");
159                         ret = input_register_device(priv->switches_dev);
160                         if (ret)
161                                 return;
162
163                         priv->has_switches = true;
164                 }
165                 input_dev = priv->switches_dev;
166         } else {
167                 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
168                 return;
169         }
170
171         if (priv->wakeup_mode) {
172                 pm_wakeup_hard_event(&device->dev);
173
174                 /*
175                  * Skip reporting an evdev event for button wake events,
176                  * mirroring how the drivers/acpi/button.c code skips this too.
177                  */
178                 if (ke->type == KE_KEY)
179                         return;
180         }
181
182         /*
183          * Even press events are autorelease if there is no corresponding odd
184          * release event, or if the odd event is KE_IGNORE.
185          */
186         ke_rel = sparse_keymap_entry_from_scancode(input_dev, event | 1);
187         autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
188
189         sparse_keymap_report_event(input_dev, event, val, autorelease);
190 }
191
192 /*
193  * There are several laptops (non 2-in-1) models out there which support VGBS,
194  * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
195  * turn causes userspace (libinput) to suppress events from the builtin
196  * keyboard and touchpad, making the laptop essentially unusable.
197  *
198  * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
199  * with libinput, leads to a non-usable system. Where as OTOH many people will
200  * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
201  * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
202  *
203  * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
204  * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
205  * these are matched on a per model basis, since many normal laptops with a
206  * possible broken VGBS ACPI-method also use these chassis-types.
207  */
208 static const struct dmi_system_id dmi_switches_allow_list[] = {
209         {
210                 .matches = {
211                         DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
212                 },
213         },
214         {
215                 .matches = {
216                         DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
217                 },
218         },
219         {
220                 .matches = {
221                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
222                         DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
223                 },
224         },
225         {
226                 .matches = {
227                         DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
228                         DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion 13 x360 PC"),
229                 },
230         },
231         {
232                 .matches = {
233                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
234                         DMI_MATCH(DMI_PRODUCT_NAME, "Switch SA5-271"),
235                 },
236         },
237         {
238                 .matches = {
239                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
240                         DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7352"),
241                 },
242         },
243         {} /* Array terminator */
244 };
245
246 static bool intel_vbtn_has_switches(acpi_handle handle)
247 {
248         unsigned long long vgbs;
249         acpi_status status;
250
251         if (!dmi_check_system(dmi_switches_allow_list))
252                 return false;
253
254         status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
255         return ACPI_SUCCESS(status);
256 }
257
258 static int intel_vbtn_probe(struct platform_device *device)
259 {
260         acpi_handle handle = ACPI_HANDLE(&device->dev);
261         bool has_buttons, has_switches;
262         struct intel_vbtn_priv *priv;
263         acpi_status status;
264         int err;
265
266         has_buttons = acpi_has_method(handle, "VBDL");
267         has_switches = intel_vbtn_has_switches(handle);
268
269         if (!has_buttons && !has_switches) {
270                 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
271                 return -ENODEV;
272         }
273
274         priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
275         if (!priv)
276                 return -ENOMEM;
277         dev_set_drvdata(&device->dev, priv);
278
279         priv->has_buttons = has_buttons;
280         priv->has_switches = has_switches;
281
282         err = intel_vbtn_input_setup(device);
283         if (err) {
284                 pr_err("Failed to setup Intel Virtual Button\n");
285                 return err;
286         }
287
288         status = acpi_install_notify_handler(handle,
289                                              ACPI_DEVICE_NOTIFY,
290                                              notify_handler,
291                                              device);
292         if (ACPI_FAILURE(status))
293                 return -EBUSY;
294
295         if (has_buttons) {
296                 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
297                 if (ACPI_FAILURE(status))
298                         dev_err(&device->dev, "Error VBDL failed with ACPI status %d\n", status);
299         }
300
301         device_init_wakeup(&device->dev, true);
302         /*
303          * In order for system wakeup to work, the EC GPE has to be marked as
304          * a wakeup one, so do that here (this setting will persist, but it has
305          * no effect until the wakeup mask is set for the EC GPE).
306          */
307         acpi_ec_mark_gpe_for_wake();
308         return 0;
309 }
310
311 static int intel_vbtn_remove(struct platform_device *device)
312 {
313         acpi_handle handle = ACPI_HANDLE(&device->dev);
314
315         device_init_wakeup(&device->dev, false);
316         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
317
318         /*
319          * Even if we failed to shut off the event stream, we can still
320          * safely detach from the device.
321          */
322         return 0;
323 }
324
325 static int intel_vbtn_pm_prepare(struct device *dev)
326 {
327         if (device_may_wakeup(dev)) {
328                 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
329
330                 priv->wakeup_mode = true;
331         }
332         return 0;
333 }
334
335 static void intel_vbtn_pm_complete(struct device *dev)
336 {
337         struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
338
339         priv->wakeup_mode = false;
340 }
341
342 static int intel_vbtn_pm_resume(struct device *dev)
343 {
344         intel_vbtn_pm_complete(dev);
345         return 0;
346 }
347
348 static const struct dev_pm_ops intel_vbtn_pm_ops = {
349         .prepare = intel_vbtn_pm_prepare,
350         .complete = intel_vbtn_pm_complete,
351         .resume = intel_vbtn_pm_resume,
352         .restore = intel_vbtn_pm_resume,
353         .thaw = intel_vbtn_pm_resume,
354 };
355
356 static struct platform_driver intel_vbtn_pl_driver = {
357         .driver = {
358                 .name = "intel-vbtn",
359                 .acpi_match_table = intel_vbtn_ids,
360                 .pm = &intel_vbtn_pm_ops,
361         },
362         .probe = intel_vbtn_probe,
363         .remove = intel_vbtn_remove,
364 };
365
366 static acpi_status __init
367 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
368 {
369         const struct acpi_device_id *ids = context;
370         struct acpi_device *dev;
371
372         if (acpi_bus_get_device(handle, &dev) != 0)
373                 return AE_OK;
374
375         if (acpi_match_device_ids(dev, ids) == 0)
376                 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
377                         dev_info(&dev->dev,
378                                  "intel-vbtn: created platform device\n");
379
380         return AE_OK;
381 }
382
383 static int __init intel_vbtn_init(void)
384 {
385         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
386                             ACPI_UINT32_MAX, check_acpi_dev, NULL,
387                             (void *)intel_vbtn_ids, NULL);
388
389         return platform_driver_register(&intel_vbtn_pl_driver);
390 }
391 module_init(intel_vbtn_init);
392
393 static void __exit intel_vbtn_exit(void)
394 {
395         platform_driver_unregister(&intel_vbtn_pl_driver);
396 }
397 module_exit(intel_vbtn_exit);