1 // SPDX-License-Identifier: GPL-2.0-only
3 * Supports for the button array on SoC tablets originally running
6 * (C) Copyright 2014 Intel Corporation
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>
21 struct soc_button_info {
24 unsigned int event_type;
25 unsigned int event_code;
31 struct soc_device_data {
32 const struct soc_button_info *button_info;
33 int (*check)(struct device *dev);
37 * Some of the buttons like volume up/down are auto repeat, while others
38 * are not. To support both, we register two platform devices, and put
39 * buttons into them based on whether the key should be auto repeat.
41 #define BUTTON_TYPES 2
43 struct soc_button_data {
44 struct platform_device *children[BUTTON_TYPES];
48 * Some 2-in-1s which use the soc_button_array driver have this ugly issue in
49 * their DSDT where the _LID method modifies the irq-type settings of the GPIOs
50 * used for the power and home buttons. The intend of this AML code is to
51 * disable these buttons when the lid is closed.
52 * The AML does this by directly poking the GPIO controllers registers. This is
53 * problematic because when re-enabling the irq, which happens whenever _LID
54 * gets called with the lid open (e.g. on boot and on resume), it sets the
55 * irq-type to IRQ_TYPE_LEVEL_LOW. Where as the gpio-keys driver programs the
56 * type to, and expects it to be, IRQ_TYPE_EDGE_BOTH.
57 * To work around this we don't set gpio_keys_button.gpio on these 2-in-1s,
58 * instead we get the irq for the GPIO ourselves, configure it as
59 * IRQ_TYPE_LEVEL_LOW (to match how the _LID AML code configures it) and pass
60 * the irq in gpio_keys_button.irq. Below is a list of affected devices.
62 static const struct dmi_system_id dmi_use_low_level_irq[] = {
65 * Acer Switch 10 SW5-012. _LID method messes with home- and
66 * power-button GPIO IRQ settings. When (re-)enabling the irq
67 * it ors in its own flags without clearing the previous set
68 * ones, leading to an irq-type of IRQ_TYPE_LEVEL_LOW |
69 * IRQ_TYPE_LEVEL_HIGH causing a continuous interrupt storm.
72 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
73 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
78 * Acer One S1003. _LID method messes with power-button GPIO
79 * IRQ settings, leading to a non working power-button.
82 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
83 DMI_MATCH(DMI_PRODUCT_NAME, "One S1003"),
88 * Lenovo Yoga Tab2 1051F/1051L, something messes with the home-button
89 * IRQ settings, leading to a non working home-button.
92 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
93 DMI_MATCH(DMI_PRODUCT_NAME, "60073"),
94 DMI_MATCH(DMI_PRODUCT_VERSION, "1051"),
97 {} /* Terminating entry */
101 * Get the Nth GPIO number from the ACPI object.
103 static int soc_button_lookup_gpio(struct device *dev, int acpi_index,
104 int *gpio_ret, int *irq_ret)
106 struct gpio_desc *desc;
108 desc = gpiod_get_index(dev, NULL, acpi_index, GPIOD_ASIS);
110 return PTR_ERR(desc);
112 *gpio_ret = desc_to_gpio(desc);
113 *irq_ret = gpiod_to_irq(desc);
120 static struct platform_device *
121 soc_button_device_create(struct platform_device *pdev,
122 const struct soc_button_info *button_info,
125 const struct soc_button_info *info;
126 struct platform_device *pd;
127 struct gpio_keys_button *gpio_keys;
128 struct gpio_keys_platform_data *gpio_keys_pdata;
129 int error, gpio, irq;
132 for (info = button_info; info->name; info++)
133 if (info->autorepeat == autorepeat)
136 gpio_keys_pdata = devm_kzalloc(&pdev->dev,
137 sizeof(*gpio_keys_pdata) +
138 sizeof(*gpio_keys) * n_buttons,
140 if (!gpio_keys_pdata)
141 return ERR_PTR(-ENOMEM);
143 gpio_keys = (void *)(gpio_keys_pdata + 1);
146 for (info = button_info; info->name; info++) {
147 if (info->autorepeat != autorepeat)
150 error = soc_button_lookup_gpio(&pdev->dev, info->acpi_index, &gpio, &irq);
151 if (error || irq < 0) {
153 * Skip GPIO if not present. Note we deliberately
154 * ignore -EPROBE_DEFER errors here. On some devices
155 * Intel is using so called virtual GPIOs which are not
156 * GPIOs at all but some way for AML code to check some
157 * random status bits without need a custom opregion.
158 * In some cases the resources table we parse points to
159 * such a virtual GPIO, since these are not real GPIOs
160 * we do not have a driver for these so they will never
161 * show up, therefore we ignore -EPROBE_DEFER.
166 /* See dmi_use_low_level_irq[] comment */
167 if (!autorepeat && dmi_check_system(dmi_use_low_level_irq)) {
168 irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
169 gpio_keys[n_buttons].irq = irq;
170 gpio_keys[n_buttons].gpio = -ENOENT;
172 gpio_keys[n_buttons].gpio = gpio;
175 gpio_keys[n_buttons].type = info->event_type;
176 gpio_keys[n_buttons].code = info->event_code;
177 gpio_keys[n_buttons].active_low = info->active_low;
178 gpio_keys[n_buttons].desc = info->name;
179 gpio_keys[n_buttons].wakeup = info->wakeup;
180 /* These devices often use cheap buttons, use 50 ms debounce */
181 gpio_keys[n_buttons].debounce_interval = 50;
185 if (n_buttons == 0) {
190 gpio_keys_pdata->buttons = gpio_keys;
191 gpio_keys_pdata->nbuttons = n_buttons;
192 gpio_keys_pdata->rep = autorepeat;
194 pd = platform_device_register_resndata(&pdev->dev, "gpio-keys",
195 PLATFORM_DEVID_AUTO, NULL, 0,
197 sizeof(*gpio_keys_pdata));
198 error = PTR_ERR_OR_ZERO(pd);
201 "failed registering gpio-keys: %d\n", error);
208 devm_kfree(&pdev->dev, gpio_keys_pdata);
209 return ERR_PTR(error);
212 static int soc_button_get_acpi_object_int(const union acpi_object *obj)
214 if (obj->type != ACPI_TYPE_INTEGER)
217 return obj->integer.value;
220 /* Parse a single ACPI0011 _DSD button descriptor */
221 static int soc_button_parse_btn_desc(struct device *dev,
222 const union acpi_object *desc,
224 struct soc_button_info *info)
228 if (desc->type != ACPI_TYPE_PACKAGE ||
229 desc->package.count != 5 ||
230 /* First byte should be 1 (control) */
231 soc_button_get_acpi_object_int(&desc->package.elements[0]) != 1 ||
232 /* Third byte should be collection uid */
233 soc_button_get_acpi_object_int(&desc->package.elements[2]) !=
235 dev_err(dev, "Invalid ACPI Button Descriptor\n");
239 info->event_type = EV_KEY;
240 info->active_low = true;
242 soc_button_get_acpi_object_int(&desc->package.elements[1]);
243 upage = soc_button_get_acpi_object_int(&desc->package.elements[3]);
244 usage = soc_button_get_acpi_object_int(&desc->package.elements[4]);
247 * The UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e descriptors use HID
248 * usage page and usage codes, but otherwise the device is not HID
249 * compliant: it uses one irq per button instead of generating HID
250 * input reports and some buttons should generate wakeups where as
251 * others should not, so we cannot use the HID subsystem.
253 * Luckily all devices only use a few usage page + usage combinations,
254 * so we can simply check for the known combinations here.
256 if (upage == 0x01 && usage == 0x81) {
257 info->name = "power";
258 info->event_code = KEY_POWER;
260 } else if (upage == 0x01 && usage == 0xca) {
261 info->name = "rotation lock switch";
262 info->event_type = EV_SW;
263 info->event_code = SW_ROTATE_LOCK;
264 } else if (upage == 0x07 && usage == 0xe3) {
266 info->event_code = KEY_LEFTMETA;
268 } else if (upage == 0x0c && usage == 0xe9) {
269 info->name = "volume_up";
270 info->event_code = KEY_VOLUMEUP;
271 info->autorepeat = true;
272 } else if (upage == 0x0c && usage == 0xea) {
273 info->name = "volume_down";
274 info->event_code = KEY_VOLUMEDOWN;
275 info->autorepeat = true;
277 dev_warn(dev, "Unknown button index %d upage %02x usage %02x, ignoring\n",
278 info->acpi_index, upage, usage);
279 info->name = "unknown";
280 info->event_code = KEY_RESERVED;
286 /* ACPI0011 _DSD btns descriptors UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e */
287 static const u8 btns_desc_uuid[16] = {
288 0x25, 0xd6, 0x6b, 0xfa, 0xe8, 0x9c, 0x0d, 0x47,
289 0xa2, 0xc7, 0xb3, 0xca, 0x36, 0xc4, 0x28, 0x2e
292 /* Parse ACPI0011 _DSD button descriptors */
293 static struct soc_button_info *soc_button_get_button_info(struct device *dev)
295 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
296 const union acpi_object *desc, *el0, *uuid, *btns_desc = NULL;
297 struct soc_button_info *button_info;
299 int i, btn, collection_uid = -1;
301 status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL,
302 &buf, ACPI_TYPE_PACKAGE);
303 if (ACPI_FAILURE(status)) {
304 dev_err(dev, "ACPI _DSD object not found\n");
305 return ERR_PTR(-ENODEV);
308 /* Look for the Button Descriptors UUID */
310 for (i = 0; (i + 1) < desc->package.count; i += 2) {
311 uuid = &desc->package.elements[i];
313 if (uuid->type != ACPI_TYPE_BUFFER ||
314 uuid->buffer.length != 16 ||
315 desc->package.elements[i + 1].type != ACPI_TYPE_PACKAGE) {
319 if (memcmp(uuid->buffer.pointer, btns_desc_uuid, 16) == 0) {
320 btns_desc = &desc->package.elements[i + 1];
326 dev_err(dev, "ACPI Button Descriptors not found\n");
327 button_info = ERR_PTR(-ENODEV);
331 /* The first package describes the collection */
332 el0 = &btns_desc->package.elements[0];
333 if (el0->type == ACPI_TYPE_PACKAGE &&
334 el0->package.count == 5 &&
335 /* First byte should be 0 (collection) */
336 soc_button_get_acpi_object_int(&el0->package.elements[0]) == 0 &&
337 /* Third byte should be 0 (top level collection) */
338 soc_button_get_acpi_object_int(&el0->package.elements[2]) == 0) {
339 collection_uid = soc_button_get_acpi_object_int(
340 &el0->package.elements[1]);
342 if (collection_uid == -1) {
343 dev_err(dev, "Invalid Button Collection Descriptor\n");
344 button_info = ERR_PTR(-ENODEV);
348 /* There are package.count - 1 buttons + 1 terminating empty entry */
349 button_info = devm_kcalloc(dev, btns_desc->package.count,
350 sizeof(*button_info), GFP_KERNEL);
352 button_info = ERR_PTR(-ENOMEM);
356 /* Parse the button descriptors */
357 for (i = 1, btn = 0; i < btns_desc->package.count; i++, btn++) {
358 if (soc_button_parse_btn_desc(dev,
359 &btns_desc->package.elements[i],
361 &button_info[btn])) {
362 button_info = ERR_PTR(-ENODEV);
372 static int soc_button_remove(struct platform_device *pdev)
374 struct soc_button_data *priv = platform_get_drvdata(pdev);
378 for (i = 0; i < BUTTON_TYPES; i++)
379 if (priv->children[i])
380 platform_device_unregister(priv->children[i]);
385 static int soc_button_probe(struct platform_device *pdev)
387 struct device *dev = &pdev->dev;
388 const struct soc_device_data *device_data;
389 const struct soc_button_info *button_info;
390 struct soc_button_data *priv;
391 struct platform_device *pd;
395 device_data = acpi_device_get_match_data(dev);
396 if (device_data && device_data->check) {
397 error = device_data->check(dev);
402 if (device_data && device_data->button_info) {
403 button_info = device_data->button_info;
405 button_info = soc_button_get_button_info(dev);
406 if (IS_ERR(button_info))
407 return PTR_ERR(button_info);
410 error = gpiod_count(dev, NULL);
412 dev_dbg(dev, "no GPIO attached, ignoring...\n");
416 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
420 platform_set_drvdata(pdev, priv);
422 for (i = 0; i < BUTTON_TYPES; i++) {
423 pd = soc_button_device_create(pdev, button_info, i == 0);
426 if (error != -ENODEV) {
427 soc_button_remove(pdev);
433 priv->children[i] = pd;
436 if (!priv->children[0] && !priv->children[1])
439 if (!device_data || !device_data->button_info)
440 devm_kfree(dev, button_info);
446 * Definition of buttons on the tablet. The ACPI index of each button
447 * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
450 static const struct soc_button_info soc_button_PNP0C40[] = {
451 { "power", 0, EV_KEY, KEY_POWER, false, true, true },
452 { "home", 1, EV_KEY, KEY_LEFTMETA, false, true, true },
453 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
454 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
455 { "rotation_lock", 4, EV_KEY, KEY_ROTATE_LOCK_TOGGLE, false, false, true },
459 static const struct soc_device_data soc_device_PNP0C40 = {
460 .button_info = soc_button_PNP0C40,
463 static const struct soc_button_info soc_button_INT33D3[] = {
464 { "tablet_mode", 0, EV_SW, SW_TABLET_MODE, false, false, false },
468 static const struct soc_device_data soc_device_INT33D3 = {
469 .button_info = soc_button_INT33D3,
473 * Button info for Microsoft Surface 3 (non pro), this is indentical to
474 * the PNP0C40 info except that the home button is active-high.
476 * The Surface 3 Pro also has a MSHW0028 ACPI device, but that uses a custom
477 * version of the drivers/platform/x86/intel/hid.c 5 button array ACPI API
478 * instead. A check() callback is not necessary though as the Surface 3 Pro
479 * MSHW0028 ACPI device's resource table does not contain any GPIOs.
481 static const struct soc_button_info soc_button_MSHW0028[] = {
482 { "power", 0, EV_KEY, KEY_POWER, false, true, true },
483 { "home", 1, EV_KEY, KEY_LEFTMETA, false, true, false },
484 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
485 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
489 static const struct soc_device_data soc_device_MSHW0028 = {
490 .button_info = soc_button_MSHW0028,
494 * Special device check for Surface Book 2 and Surface Pro (2017).
495 * Both, the Surface Pro 4 (surfacepro3_button.c) and the above mentioned
496 * devices use MSHW0040 for power and volume buttons, however the way they
497 * have to be addressed differs. Make sure that we only load this drivers
498 * for the correct devices by checking the OEM Platform Revision provided by
501 #define MSHW0040_DSM_REVISION 0x01
502 #define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
503 static const guid_t MSHW0040_DSM_UUID =
504 GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
507 static int soc_device_check_MSHW0040(struct device *dev)
509 acpi_handle handle = ACPI_HANDLE(dev);
510 union acpi_object *result;
511 u64 oem_platform_rev = 0; // valid revisions are nonzero
513 // get OEM platform revision
514 result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
515 MSHW0040_DSM_REVISION,
516 MSHW0040_DSM_GET_OMPR, NULL,
520 oem_platform_rev = result->integer.value;
525 * If the revision is zero here, the _DSM evaluation has failed. This
526 * indicates that we have a Pro 4 or Book 1 and this driver should not
529 if (oem_platform_rev == 0)
532 dev_dbg(dev, "OEM Platform Revision %llu\n", oem_platform_rev);
538 * Button infos for Microsoft Surface Book 2 and Surface Pro (2017).
539 * Obtained from DSDT/testing.
541 static const struct soc_button_info soc_button_MSHW0040[] = {
542 { "power", 0, EV_KEY, KEY_POWER, false, true, true },
543 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
544 { "volume_down", 4, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
548 static const struct soc_device_data soc_device_MSHW0040 = {
549 .button_info = soc_button_MSHW0040,
550 .check = soc_device_check_MSHW0040,
553 static const struct acpi_device_id soc_button_acpi_match[] = {
554 { "PNP0C40", (unsigned long)&soc_device_PNP0C40 },
555 { "INT33D3", (unsigned long)&soc_device_INT33D3 },
556 { "ID9001", (unsigned long)&soc_device_INT33D3 },
559 /* Microsoft Surface Devices (3th, 5th and 6th generation) */
560 { "MSHW0028", (unsigned long)&soc_device_MSHW0028 },
561 { "MSHW0040", (unsigned long)&soc_device_MSHW0040 },
566 MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
568 static struct platform_driver soc_button_driver = {
569 .probe = soc_button_probe,
570 .remove = soc_button_remove,
572 .name = KBUILD_MODNAME,
573 .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
576 module_platform_driver(soc_button_driver);
578 MODULE_LICENSE("GPL");