platform/x86: intel-vbtn: Fix probe failure on devices with only switches
[linux-2.6-microblaze.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 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
19 #define TABLET_MODE_FLAG 0x40
20 #define DOCK_MODE_FLAG   0x80
21
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("AceLan Kao");
24
25 static const struct acpi_device_id intel_vbtn_ids[] = {
26         {"INT33D6", 0},
27         {"", 0},
28 };
29 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
30
31 /* In theory, these are HID usages. */
32 static const struct key_entry intel_vbtn_keymap[] = {
33         { KE_KEY, 0xC0, { KEY_POWER } },        /* power key press */
34         { KE_IGNORE, 0xC1, { KEY_POWER } },     /* power key release */
35         { KE_KEY, 0xC2, { KEY_LEFTMETA } },             /* 'Windows' key press */
36         { KE_KEY, 0xC3, { KEY_LEFTMETA } },             /* 'Windows' key release */
37         { 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 */
41         { KE_KEY,    0xC8, { KEY_ROTATE_LOCK_TOGGLE } },        /* rotate-lock key press */
42         { KE_KEY,    0xC9, { KEY_ROTATE_LOCK_TOGGLE } },        /* rotate-lock key release */
43 };
44
45 static const struct key_entry intel_vbtn_switchmap[] = {
46         { KE_SW,     0xCA, { .sw = { SW_DOCK, 1 } } },          /* Docked */
47         { KE_SW,     0xCB, { .sw = { SW_DOCK, 0 } } },          /* Undocked */
48         { KE_SW,     0xCC, { .sw = { SW_TABLET_MODE, 1 } } },   /* Tablet */
49         { KE_SW,     0xCD, { .sw = { SW_TABLET_MODE, 0 } } },   /* Laptop */
50 };
51
52 #define KEYMAP_LEN \
53         (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
54
55 struct intel_vbtn_priv {
56         struct key_entry keymap[KEYMAP_LEN];
57         struct input_dev *input_dev;
58         bool has_buttons;
59         bool has_switches;
60         bool wakeup_mode;
61 };
62
63 static int intel_vbtn_input_setup(struct platform_device *device)
64 {
65         struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
66         int ret, keymap_len = 0;
67
68         if (priv->has_buttons) {
69                 memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
70                        ARRAY_SIZE(intel_vbtn_keymap) *
71                        sizeof(struct key_entry));
72                 keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
73         }
74
75         if (priv->has_switches) {
76                 memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
77                        ARRAY_SIZE(intel_vbtn_switchmap) *
78                        sizeof(struct key_entry));
79                 keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
80         }
81
82         priv->keymap[keymap_len].type = KE_END;
83
84         priv->input_dev = devm_input_allocate_device(&device->dev);
85         if (!priv->input_dev)
86                 return -ENOMEM;
87
88         ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
89         if (ret)
90                 return ret;
91
92         priv->input_dev->dev.parent = &device->dev;
93         priv->input_dev->name = "Intel Virtual Button driver";
94         priv->input_dev->id.bustype = BUS_HOST;
95
96         return input_register_device(priv->input_dev);
97 }
98
99 static void notify_handler(acpi_handle handle, u32 event, void *context)
100 {
101         struct platform_device *device = context;
102         struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
103         unsigned int val = !(event & 1); /* Even=press, Odd=release */
104         const struct key_entry *ke, *ke_rel;
105         bool autorelease;
106
107         if (priv->wakeup_mode) {
108                 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
109                 if (ke) {
110                         pm_wakeup_hard_event(&device->dev);
111
112                         /*
113                          * Switch events like tablet mode will wake the device
114                          * and report the new switch position to the input
115                          * subsystem.
116                          */
117                         if (ke->type == KE_SW)
118                                 sparse_keymap_report_event(priv->input_dev,
119                                                            event,
120                                                            val,
121                                                            0);
122                         return;
123                 }
124                 goto out_unknown;
125         }
126
127         /*
128          * Even press events are autorelease if there is no corresponding odd
129          * release event, or if the odd event is KE_IGNORE.
130          */
131         ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
132         autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
133
134         if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
135                 return;
136
137 out_unknown:
138         dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
139 }
140
141 static void detect_tablet_mode(struct platform_device *device)
142 {
143         struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
144         acpi_handle handle = ACPI_HANDLE(&device->dev);
145         unsigned long long vgbs;
146         acpi_status status;
147         int m;
148
149         status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
150         if (ACPI_FAILURE(status))
151                 return;
152
153         m = !(vgbs & TABLET_MODE_FLAG);
154         input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
155         m = (vgbs & DOCK_MODE_FLAG) ? 1 : 0;
156         input_report_switch(priv->input_dev, SW_DOCK, m);
157 }
158
159 static bool intel_vbtn_has_buttons(acpi_handle handle)
160 {
161         acpi_status status;
162
163         status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
164         return ACPI_SUCCESS(status);
165 }
166
167 static bool intel_vbtn_has_switches(acpi_handle handle)
168 {
169         const char *chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
170         unsigned long chassis_type_int;
171         unsigned long long vgbs;
172         acpi_status status;
173
174         if (kstrtoul(chassis_type, 10, &chassis_type_int))
175                 return false;
176
177         switch (chassis_type_int) {
178         case  8: /* Portable */
179         case 31: /* Convertible */
180         case 32: /* Detachable */
181                 break;
182         default:
183                 return false;
184         }
185
186         status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
187         return ACPI_SUCCESS(status);
188 }
189
190 static int intel_vbtn_probe(struct platform_device *device)
191 {
192         acpi_handle handle = ACPI_HANDLE(&device->dev);
193         bool has_buttons, has_switches;
194         struct intel_vbtn_priv *priv;
195         acpi_status status;
196         int err;
197
198         has_buttons = intel_vbtn_has_buttons(handle);
199         has_switches = intel_vbtn_has_switches(handle);
200
201         if (!has_buttons && !has_switches) {
202                 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
203                 return -ENODEV;
204         }
205
206         priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
207         if (!priv)
208                 return -ENOMEM;
209         dev_set_drvdata(&device->dev, priv);
210
211         priv->has_buttons = has_buttons;
212         priv->has_switches = has_switches;
213
214         err = intel_vbtn_input_setup(device);
215         if (err) {
216                 pr_err("Failed to setup Intel Virtual Button\n");
217                 return err;
218         }
219
220         if (priv->has_switches)
221                 detect_tablet_mode(device);
222
223         status = acpi_install_notify_handler(handle,
224                                              ACPI_DEVICE_NOTIFY,
225                                              notify_handler,
226                                              device);
227         if (ACPI_FAILURE(status))
228                 return -EBUSY;
229
230         device_init_wakeup(&device->dev, true);
231         /*
232          * In order for system wakeup to work, the EC GPE has to be marked as
233          * a wakeup one, so do that here (this setting will persist, but it has
234          * no effect until the wakeup mask is set for the EC GPE).
235          */
236         acpi_ec_mark_gpe_for_wake();
237         return 0;
238 }
239
240 static int intel_vbtn_remove(struct platform_device *device)
241 {
242         acpi_handle handle = ACPI_HANDLE(&device->dev);
243
244         device_init_wakeup(&device->dev, false);
245         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
246
247         /*
248          * Even if we failed to shut off the event stream, we can still
249          * safely detach from the device.
250          */
251         return 0;
252 }
253
254 static int intel_vbtn_pm_prepare(struct device *dev)
255 {
256         if (device_may_wakeup(dev)) {
257                 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
258
259                 priv->wakeup_mode = true;
260         }
261         return 0;
262 }
263
264 static void intel_vbtn_pm_complete(struct device *dev)
265 {
266         struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
267
268         priv->wakeup_mode = false;
269 }
270
271 static int intel_vbtn_pm_resume(struct device *dev)
272 {
273         intel_vbtn_pm_complete(dev);
274         return 0;
275 }
276
277 static const struct dev_pm_ops intel_vbtn_pm_ops = {
278         .prepare = intel_vbtn_pm_prepare,
279         .complete = intel_vbtn_pm_complete,
280         .resume = intel_vbtn_pm_resume,
281         .restore = intel_vbtn_pm_resume,
282         .thaw = intel_vbtn_pm_resume,
283 };
284
285 static struct platform_driver intel_vbtn_pl_driver = {
286         .driver = {
287                 .name = "intel-vbtn",
288                 .acpi_match_table = intel_vbtn_ids,
289                 .pm = &intel_vbtn_pm_ops,
290         },
291         .probe = intel_vbtn_probe,
292         .remove = intel_vbtn_remove,
293 };
294
295 static acpi_status __init
296 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
297 {
298         const struct acpi_device_id *ids = context;
299         struct acpi_device *dev;
300
301         if (acpi_bus_get_device(handle, &dev) != 0)
302                 return AE_OK;
303
304         if (acpi_match_device_ids(dev, ids) == 0)
305                 if (acpi_create_platform_device(dev, NULL))
306                         dev_info(&dev->dev,
307                                  "intel-vbtn: created platform device\n");
308
309         return AE_OK;
310 }
311
312 static int __init intel_vbtn_init(void)
313 {
314         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
315                             ACPI_UINT32_MAX, check_acpi_dev, NULL,
316                             (void *)intel_vbtn_ids, NULL);
317
318         return platform_driver_register(&intel_vbtn_pl_driver);
319 }
320 module_init(intel_vbtn_init);
321
322 static void __exit intel_vbtn_exit(void)
323 {
324         platform_driver_unregister(&intel_vbtn_pl_driver);
325 }
326 module_exit(intel_vbtn_exit);