Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Supports for the button array on SoC tablets originally running
4 * Windows 8.
5 *
6 * (C) Copyright 2014 Intel Corporation
7 */
8
9#include <linux/module.h>
10#include <linux/input.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/acpi.h>
14#include <linux/dmi.h>
15#include <linux/gpio/consumer.h>
16#include <linux/gpio_keys.h>
17#include <linux/gpio.h>
18#include <linux/platform_device.h>
19
20struct soc_button_info {
21 const char *name;
22 int acpi_index;
23 unsigned int event_type;
24 unsigned int event_code;
25 bool autorepeat;
26 bool wakeup;
27 bool active_low;
28};
29
30struct soc_device_data {
31 const struct soc_button_info *button_info;
32 int (*check)(struct device *dev);
33};
34
35/*
36 * Some of the buttons like volume up/down are auto repeat, while others
37 * are not. To support both, we register two platform devices, and put
38 * buttons into them based on whether the key should be auto repeat.
39 */
40#define BUTTON_TYPES 2
41
42struct soc_button_data {
43 struct platform_device *children[BUTTON_TYPES];
44};
45
46/*
47 * Some 2-in-1s which use the soc_button_array driver have this ugly issue in
48 * their DSDT where the _LID method modifies the irq-type settings of the GPIOs
49 * used for the power and home buttons. The intend of this AML code is to
50 * disable these buttons when the lid is closed.
51 * The AML does this by directly poking the GPIO controllers registers. This is
52 * problematic because when re-enabling the irq, which happens whenever _LID
53 * gets called with the lid open (e.g. on boot and on resume), it sets the
54 * irq-type to IRQ_TYPE_LEVEL_LOW. Where as the gpio-keys driver programs the
55 * type to, and expects it to be, IRQ_TYPE_EDGE_BOTH.
56 * To work around this we don't set gpio_keys_button.gpio on these 2-in-1s,
57 * instead we get the irq for the GPIO ourselves, configure it as
58 * IRQ_TYPE_LEVEL_LOW (to match how the _LID AML code configures it) and pass
59 * the irq in gpio_keys_button.irq. Below is a list of affected devices.
60 */
61static const struct dmi_system_id dmi_use_low_level_irq[] = {
62 {
63 /*
64 * Acer Switch 10 SW5-012. _LID method messes with home- and
65 * power-button GPIO IRQ settings. When (re-)enabling the irq
66 * it ors in its own flags without clearing the previous set
67 * ones, leading to an irq-type of IRQ_TYPE_LEVEL_LOW |
68 * IRQ_TYPE_LEVEL_HIGH causing a continuous interrupt storm.
69 */
70 .matches = {
71 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
72 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
73 },
74 },
75 {
76 /*
77 * Acer One S1003. _LID method messes with power-button GPIO
78 * IRQ settings, leading to a non working power-button.
79 */
80 .matches = {
81 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
82 DMI_MATCH(DMI_PRODUCT_NAME, "One S1003"),
83 },
84 },
85 {} /* Terminating entry */
86};
87
88/*
89 * Get the Nth GPIO number from the ACPI object.
90 */
91static int soc_button_lookup_gpio(struct device *dev, int acpi_index,
92 int *gpio_ret, int *irq_ret)
93{
94 struct gpio_desc *desc;
95
96 desc = gpiod_get_index(dev, NULL, acpi_index, GPIOD_ASIS);
97 if (IS_ERR(desc))
98 return PTR_ERR(desc);
99
100 *gpio_ret = desc_to_gpio(desc);
101 *irq_ret = gpiod_to_irq(desc);
102
103 gpiod_put(desc);
104
105 return 0;
106}
107
108static struct platform_device *
109soc_button_device_create(struct platform_device *pdev,
110 const struct soc_button_info *button_info,
111 bool autorepeat)
112{
113 const struct soc_button_info *info;
114 struct platform_device *pd;
115 struct gpio_keys_button *gpio_keys;
116 struct gpio_keys_platform_data *gpio_keys_pdata;
117 int error, gpio, irq;
118 int n_buttons = 0;
119
120 for (info = button_info; info->name; info++)
121 if (info->autorepeat == autorepeat)
122 n_buttons++;
123
124 gpio_keys_pdata = devm_kzalloc(&pdev->dev,
125 sizeof(*gpio_keys_pdata) +
126 sizeof(*gpio_keys) * n_buttons,
127 GFP_KERNEL);
128 if (!gpio_keys_pdata)
129 return ERR_PTR(-ENOMEM);
130
131 gpio_keys = (void *)(gpio_keys_pdata + 1);
132 n_buttons = 0;
133
134 for (info = button_info; info->name; info++) {
135 if (info->autorepeat != autorepeat)
136 continue;
137
138 error = soc_button_lookup_gpio(&pdev->dev, info->acpi_index, &gpio, &irq);
139 if (error || irq < 0) {
140 /*
141 * Skip GPIO if not present. Note we deliberately
142 * ignore -EPROBE_DEFER errors here. On some devices
143 * Intel is using so called virtual GPIOs which are not
144 * GPIOs at all but some way for AML code to check some
145 * random status bits without need a custom opregion.
146 * In some cases the resources table we parse points to
147 * such a virtual GPIO, since these are not real GPIOs
148 * we do not have a driver for these so they will never
149 * show up, therefore we ignore -EPROBE_DEFER.
150 */
151 continue;
152 }
153
154 /* See dmi_use_low_level_irq[] comment */
155 if (!autorepeat && dmi_check_system(dmi_use_low_level_irq)) {
156 irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
157 gpio_keys[n_buttons].irq = irq;
158 gpio_keys[n_buttons].gpio = -ENOENT;
159 } else {
160 gpio_keys[n_buttons].gpio = gpio;
161 }
162
163 gpio_keys[n_buttons].type = info->event_type;
164 gpio_keys[n_buttons].code = info->event_code;
165 gpio_keys[n_buttons].active_low = info->active_low;
166 gpio_keys[n_buttons].desc = info->name;
167 gpio_keys[n_buttons].wakeup = info->wakeup;
168 /* These devices often use cheap buttons, use 50 ms debounce */
169 gpio_keys[n_buttons].debounce_interval = 50;
170 n_buttons++;
171 }
172
173 if (n_buttons == 0) {
174 error = -ENODEV;
175 goto err_free_mem;
176 }
177
178 gpio_keys_pdata->buttons = gpio_keys;
179 gpio_keys_pdata->nbuttons = n_buttons;
180 gpio_keys_pdata->rep = autorepeat;
181
182 pd = platform_device_register_resndata(&pdev->dev, "gpio-keys",
183 PLATFORM_DEVID_AUTO, NULL, 0,
184 gpio_keys_pdata,
185 sizeof(*gpio_keys_pdata));
186 error = PTR_ERR_OR_ZERO(pd);
187 if (error) {
188 dev_err(&pdev->dev,
189 "failed registering gpio-keys: %d\n", error);
190 goto err_free_mem;
191 }
192
193 return pd;
194
195err_free_mem:
196 devm_kfree(&pdev->dev, gpio_keys_pdata);
197 return ERR_PTR(error);
198}
199
200static int soc_button_get_acpi_object_int(const union acpi_object *obj)
201{
202 if (obj->type != ACPI_TYPE_INTEGER)
203 return -1;
204
205 return obj->integer.value;
206}
207
208/* Parse a single ACPI0011 _DSD button descriptor */
209static int soc_button_parse_btn_desc(struct device *dev,
210 const union acpi_object *desc,
211 int collection_uid,
212 struct soc_button_info *info)
213{
214 int upage, usage;
215
216 if (desc->type != ACPI_TYPE_PACKAGE ||
217 desc->package.count != 5 ||
218 /* First byte should be 1 (control) */
219 soc_button_get_acpi_object_int(&desc->package.elements[0]) != 1 ||
220 /* Third byte should be collection uid */
221 soc_button_get_acpi_object_int(&desc->package.elements[2]) !=
222 collection_uid) {
223 dev_err(dev, "Invalid ACPI Button Descriptor\n");
224 return -ENODEV;
225 }
226
227 info->event_type = EV_KEY;
228 info->active_low = true;
229 info->acpi_index =
230 soc_button_get_acpi_object_int(&desc->package.elements[1]);
231 upage = soc_button_get_acpi_object_int(&desc->package.elements[3]);
232 usage = soc_button_get_acpi_object_int(&desc->package.elements[4]);
233
234 /*
235 * The UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e descriptors use HID
236 * usage page and usage codes, but otherwise the device is not HID
237 * compliant: it uses one irq per button instead of generating HID
238 * input reports and some buttons should generate wakeups where as
239 * others should not, so we cannot use the HID subsystem.
240 *
241 * Luckily all devices only use a few usage page + usage combinations,
242 * so we can simply check for the known combinations here.
243 */
244 if (upage == 0x01 && usage == 0x81) {
245 info->name = "power";
246 info->event_code = KEY_POWER;
247 info->wakeup = true;
248 } else if (upage == 0x01 && usage == 0xca) {
249 info->name = "rotation lock switch";
250 info->event_type = EV_SW;
251 info->event_code = SW_ROTATE_LOCK;
252 } else if (upage == 0x07 && usage == 0xe3) {
253 info->name = "home";
254 info->event_code = KEY_LEFTMETA;
255 info->wakeup = true;
256 } else if (upage == 0x0c && usage == 0xe9) {
257 info->name = "volume_up";
258 info->event_code = KEY_VOLUMEUP;
259 info->autorepeat = true;
260 } else if (upage == 0x0c && usage == 0xea) {
261 info->name = "volume_down";
262 info->event_code = KEY_VOLUMEDOWN;
263 info->autorepeat = true;
264 } else {
265 dev_warn(dev, "Unknown button index %d upage %02x usage %02x, ignoring\n",
266 info->acpi_index, upage, usage);
267 info->name = "unknown";
268 info->event_code = KEY_RESERVED;
269 }
270
271 return 0;
272}
273
274/* ACPI0011 _DSD btns descriptors UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e */
275static const u8 btns_desc_uuid[16] = {
276 0x25, 0xd6, 0x6b, 0xfa, 0xe8, 0x9c, 0x0d, 0x47,
277 0xa2, 0xc7, 0xb3, 0xca, 0x36, 0xc4, 0x28, 0x2e
278};
279
280/* Parse ACPI0011 _DSD button descriptors */
281static struct soc_button_info *soc_button_get_button_info(struct device *dev)
282{
283 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
284 const union acpi_object *desc, *el0, *uuid, *btns_desc = NULL;
285 struct soc_button_info *button_info;
286 acpi_status status;
287 int i, btn, collection_uid = -1;
288
289 status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL,
290 &buf, ACPI_TYPE_PACKAGE);
291 if (ACPI_FAILURE(status)) {
292 dev_err(dev, "ACPI _DSD object not found\n");
293 return ERR_PTR(-ENODEV);
294 }
295
296 /* Look for the Button Descriptors UUID */
297 desc = buf.pointer;
298 for (i = 0; (i + 1) < desc->package.count; i += 2) {
299 uuid = &desc->package.elements[i];
300
301 if (uuid->type != ACPI_TYPE_BUFFER ||
302 uuid->buffer.length != 16 ||
303 desc->package.elements[i + 1].type != ACPI_TYPE_PACKAGE) {
304 break;
305 }
306
307 if (memcmp(uuid->buffer.pointer, btns_desc_uuid, 16) == 0) {
308 btns_desc = &desc->package.elements[i + 1];
309 break;
310 }
311 }
312
313 if (!btns_desc) {
314 dev_err(dev, "ACPI Button Descriptors not found\n");
315 button_info = ERR_PTR(-ENODEV);
316 goto out;
317 }
318
319 /* The first package describes the collection */
320 el0 = &btns_desc->package.elements[0];
321 if (el0->type == ACPI_TYPE_PACKAGE &&
322 el0->package.count == 5 &&
323 /* First byte should be 0 (collection) */
324 soc_button_get_acpi_object_int(&el0->package.elements[0]) == 0 &&
325 /* Third byte should be 0 (top level collection) */
326 soc_button_get_acpi_object_int(&el0->package.elements[2]) == 0) {
327 collection_uid = soc_button_get_acpi_object_int(
328 &el0->package.elements[1]);
329 }
330 if (collection_uid == -1) {
331 dev_err(dev, "Invalid Button Collection Descriptor\n");
332 button_info = ERR_PTR(-ENODEV);
333 goto out;
334 }
335
336 /* There are package.count - 1 buttons + 1 terminating empty entry */
337 button_info = devm_kcalloc(dev, btns_desc->package.count,
338 sizeof(*button_info), GFP_KERNEL);
339 if (!button_info) {
340 button_info = ERR_PTR(-ENOMEM);
341 goto out;
342 }
343
344 /* Parse the button descriptors */
345 for (i = 1, btn = 0; i < btns_desc->package.count; i++, btn++) {
346 if (soc_button_parse_btn_desc(dev,
347 &btns_desc->package.elements[i],
348 collection_uid,
349 &button_info[btn])) {
350 button_info = ERR_PTR(-ENODEV);
351 goto out;
352 }
353 }
354
355out:
356 kfree(buf.pointer);
357 return button_info;
358}
359
360static int soc_button_remove(struct platform_device *pdev)
361{
362 struct soc_button_data *priv = platform_get_drvdata(pdev);
363
364 int i;
365
366 for (i = 0; i < BUTTON_TYPES; i++)
367 if (priv->children[i])
368 platform_device_unregister(priv->children[i]);
369
370 return 0;
371}
372
373static int soc_button_probe(struct platform_device *pdev)
374{
375 struct device *dev = &pdev->dev;
376 const struct soc_device_data *device_data;
377 const struct soc_button_info *button_info;
378 struct soc_button_data *priv;
379 struct platform_device *pd;
380 int i;
381 int error;
382
383 device_data = acpi_device_get_match_data(dev);
384 if (device_data && device_data->check) {
385 error = device_data->check(dev);
386 if (error)
387 return error;
388 }
389
390 if (device_data && device_data->button_info) {
391 button_info = device_data->button_info;
392 } else {
393 button_info = soc_button_get_button_info(dev);
394 if (IS_ERR(button_info))
395 return PTR_ERR(button_info);
396 }
397
398 error = gpiod_count(dev, NULL);
399 if (error < 0) {
400 dev_dbg(dev, "no GPIO attached, ignoring...\n");
401 return -ENODEV;
402 }
403
404 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
405 if (!priv)
406 return -ENOMEM;
407
408 platform_set_drvdata(pdev, priv);
409
410 for (i = 0; i < BUTTON_TYPES; i++) {
411 pd = soc_button_device_create(pdev, button_info, i == 0);
412 if (IS_ERR(pd)) {
413 error = PTR_ERR(pd);
414 if (error != -ENODEV) {
415 soc_button_remove(pdev);
416 return error;
417 }
418 continue;
419 }
420
421 priv->children[i] = pd;
422 }
423
424 if (!priv->children[0] && !priv->children[1])
425 return -ENODEV;
426
427 if (!device_data || !device_data->button_info)
428 devm_kfree(dev, button_info);
429
430 return 0;
431}
432
433/*
434 * Definition of buttons on the tablet. The ACPI index of each button
435 * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
436 * Platforms"
437 */
438static const struct soc_button_info soc_button_PNP0C40[] = {
439 { "power", 0, EV_KEY, KEY_POWER, false, true, true },
440 { "home", 1, EV_KEY, KEY_LEFTMETA, false, true, true },
441 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
442 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
443 { "rotation_lock", 4, EV_KEY, KEY_ROTATE_LOCK_TOGGLE, false, false, true },
444 { }
445};
446
447static const struct soc_device_data soc_device_PNP0C40 = {
448 .button_info = soc_button_PNP0C40,
449};
450
451static const struct soc_button_info soc_button_INT33D3[] = {
452 { "tablet_mode", 0, EV_SW, SW_TABLET_MODE, false, false, false },
453 { }
454};
455
456static const struct soc_device_data soc_device_INT33D3 = {
457 .button_info = soc_button_INT33D3,
458};
459
460/*
461 * Special device check for Surface Book 2 and Surface Pro (2017).
462 * Both, the Surface Pro 4 (surfacepro3_button.c) and the above mentioned
463 * devices use MSHW0040 for power and volume buttons, however the way they
464 * have to be addressed differs. Make sure that we only load this drivers
465 * for the correct devices by checking the OEM Platform Revision provided by
466 * the _DSM method.
467 */
468#define MSHW0040_DSM_REVISION 0x01
469#define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
470static const guid_t MSHW0040_DSM_UUID =
471 GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
472 0x49, 0x80, 0x35);
473
474static int soc_device_check_MSHW0040(struct device *dev)
475{
476 acpi_handle handle = ACPI_HANDLE(dev);
477 union acpi_object *result;
478 u64 oem_platform_rev = 0; // valid revisions are nonzero
479
480 // get OEM platform revision
481 result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
482 MSHW0040_DSM_REVISION,
483 MSHW0040_DSM_GET_OMPR, NULL,
484 ACPI_TYPE_INTEGER);
485
486 if (result) {
487 oem_platform_rev = result->integer.value;
488 ACPI_FREE(result);
489 }
490
491 /*
492 * If the revision is zero here, the _DSM evaluation has failed. This
493 * indicates that we have a Pro 4 or Book 1 and this driver should not
494 * be used.
495 */
496 if (oem_platform_rev == 0)
497 return -ENODEV;
498
499 dev_dbg(dev, "OEM Platform Revision %llu\n", oem_platform_rev);
500
501 return 0;
502}
503
504/*
505 * Button infos for Microsoft Surface Book 2 and Surface Pro (2017).
506 * Obtained from DSDT/testing.
507 */
508static const struct soc_button_info soc_button_MSHW0040[] = {
509 { "power", 0, EV_KEY, KEY_POWER, false, true, true },
510 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
511 { "volume_down", 4, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
512 { }
513};
514
515static const struct soc_device_data soc_device_MSHW0040 = {
516 .button_info = soc_button_MSHW0040,
517 .check = soc_device_check_MSHW0040,
518};
519
520static const struct acpi_device_id soc_button_acpi_match[] = {
521 { "PNP0C40", (unsigned long)&soc_device_PNP0C40 },
522 { "INT33D3", (unsigned long)&soc_device_INT33D3 },
523 { "ID9001", (unsigned long)&soc_device_INT33D3 },
524 { "ACPI0011", 0 },
525
526 /* Microsoft Surface Devices (5th and 6th generation) */
527 { "MSHW0040", (unsigned long)&soc_device_MSHW0040 },
528
529 { }
530};
531
532MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
533
534static struct platform_driver soc_button_driver = {
535 .probe = soc_button_probe,
536 .remove = soc_button_remove,
537 .driver = {
538 .name = KBUILD_MODNAME,
539 .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
540 },
541};
542module_platform_driver(soc_button_driver);
543
544MODULE_LICENSE("GPL");