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

source code of linux/drivers/input/misc/soc_button_array.c