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"),
86 {} /* Terminating entry */
90 * Get the Nth GPIO number from the ACPI object.
92 static int soc_button_lookup_gpio(struct device *dev, int acpi_index,
93 int *gpio_ret, int *irq_ret)
95 struct gpio_desc *desc;
97 desc = gpiod_get_index(dev, NULL, acpi_index, GPIOD_ASIS);
101 *gpio_ret = desc_to_gpio(desc);
102 *irq_ret = gpiod_to_irq(desc);
109 static struct platform_device *
110 soc_button_device_create(struct platform_device *pdev,
111 const struct soc_button_info *button_info,
114 const struct soc_button_info *info;
115 struct platform_device *pd;
116 struct gpio_keys_button *gpio_keys;
117 struct gpio_keys_platform_data *gpio_keys_pdata;
118 int error, gpio, irq;
121 for (info = button_info; info->name; info++)
122 if (info->autorepeat == autorepeat)
125 gpio_keys_pdata = devm_kzalloc(&pdev->dev,
126 sizeof(*gpio_keys_pdata) +
127 sizeof(*gpio_keys) * n_buttons,
129 if (!gpio_keys_pdata)
130 return ERR_PTR(-ENOMEM);
132 gpio_keys = (void *)(gpio_keys_pdata + 1);
135 for (info = button_info; info->name; info++) {
136 if (info->autorepeat != autorepeat)
139 error = soc_button_lookup_gpio(&pdev->dev, info->acpi_index, &gpio, &irq);
140 if (error || irq < 0) {
142 * Skip GPIO if not present. Note we deliberately
143 * ignore -EPROBE_DEFER errors here. On some devices
144 * Intel is using so called virtual GPIOs which are not
145 * GPIOs at all but some way for AML code to check some
146 * random status bits without need a custom opregion.
147 * In some cases the resources table we parse points to
148 * such a virtual GPIO, since these are not real GPIOs
149 * we do not have a driver for these so they will never
150 * show up, therefore we ignore -EPROBE_DEFER.
155 /* See dmi_use_low_level_irq[] comment */
156 if (!autorepeat && dmi_check_system(dmi_use_low_level_irq)) {
157 irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
158 gpio_keys[n_buttons].irq = irq;
159 gpio_keys[n_buttons].gpio = -ENOENT;
161 gpio_keys[n_buttons].gpio = gpio;
164 gpio_keys[n_buttons].type = info->event_type;
165 gpio_keys[n_buttons].code = info->event_code;
166 gpio_keys[n_buttons].active_low = info->active_low;
167 gpio_keys[n_buttons].desc = info->name;
168 gpio_keys[n_buttons].wakeup = info->wakeup;
169 /* These devices often use cheap buttons, use 50 ms debounce */
170 gpio_keys[n_buttons].debounce_interval = 50;
174 if (n_buttons == 0) {
179 gpio_keys_pdata->buttons = gpio_keys;
180 gpio_keys_pdata->nbuttons = n_buttons;
181 gpio_keys_pdata->rep = autorepeat;
183 pd = platform_device_register_resndata(&pdev->dev, "gpio-keys",
184 PLATFORM_DEVID_AUTO, NULL, 0,
186 sizeof(*gpio_keys_pdata));
187 error = PTR_ERR_OR_ZERO(pd);
190 "failed registering gpio-keys: %d\n", error);
197 devm_kfree(&pdev->dev, gpio_keys_pdata);
198 return ERR_PTR(error);
201 static int soc_button_get_acpi_object_int(const union acpi_object *obj)
203 if (obj->type != ACPI_TYPE_INTEGER)
206 return obj->integer.value;
209 /* Parse a single ACPI0011 _DSD button descriptor */
210 static int soc_button_parse_btn_desc(struct device *dev,
211 const union acpi_object *desc,
213 struct soc_button_info *info)
217 if (desc->type != ACPI_TYPE_PACKAGE ||
218 desc->package.count != 5 ||
219 /* First byte should be 1 (control) */
220 soc_button_get_acpi_object_int(&desc->package.elements[0]) != 1 ||
221 /* Third byte should be collection uid */
222 soc_button_get_acpi_object_int(&desc->package.elements[2]) !=
224 dev_err(dev, "Invalid ACPI Button Descriptor\n");
228 info->event_type = EV_KEY;
229 info->active_low = true;
231 soc_button_get_acpi_object_int(&desc->package.elements[1]);
232 upage = soc_button_get_acpi_object_int(&desc->package.elements[3]);
233 usage = soc_button_get_acpi_object_int(&desc->package.elements[4]);
236 * The UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e descriptors use HID
237 * usage page and usage codes, but otherwise the device is not HID
238 * compliant: it uses one irq per button instead of generating HID
239 * input reports and some buttons should generate wakeups where as
240 * others should not, so we cannot use the HID subsystem.
242 * Luckily all devices only use a few usage page + usage combinations,
243 * so we can simply check for the known combinations here.
245 if (upage == 0x01 && usage == 0x81) {
246 info->name = "power";
247 info->event_code = KEY_POWER;
249 } else if (upage == 0x01 && usage == 0xca) {
250 info->name = "rotation lock switch";
251 info->event_type = EV_SW;
252 info->event_code = SW_ROTATE_LOCK;
253 } else if (upage == 0x07 && usage == 0xe3) {
255 info->event_code = KEY_LEFTMETA;
257 } else if (upage == 0x0c && usage == 0xe9) {
258 info->name = "volume_up";
259 info->event_code = KEY_VOLUMEUP;
260 info->autorepeat = true;
261 } else if (upage == 0x0c && usage == 0xea) {
262 info->name = "volume_down";
263 info->event_code = KEY_VOLUMEDOWN;
264 info->autorepeat = true;
266 dev_warn(dev, "Unknown button index %d upage %02x usage %02x, ignoring\n",
267 info->acpi_index, upage, usage);
268 info->name = "unknown";
269 info->event_code = KEY_RESERVED;
275 /* ACPI0011 _DSD btns descriptors UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e */
276 static const u8 btns_desc_uuid[16] = {
277 0x25, 0xd6, 0x6b, 0xfa, 0xe8, 0x9c, 0x0d, 0x47,
278 0xa2, 0xc7, 0xb3, 0xca, 0x36, 0xc4, 0x28, 0x2e
281 /* Parse ACPI0011 _DSD button descriptors */
282 static struct soc_button_info *soc_button_get_button_info(struct device *dev)
284 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
285 const union acpi_object *desc, *el0, *uuid, *btns_desc = NULL;
286 struct soc_button_info *button_info;
288 int i, btn, collection_uid = -1;
290 status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL,
291 &buf, ACPI_TYPE_PACKAGE);
292 if (ACPI_FAILURE(status)) {
293 dev_err(dev, "ACPI _DSD object not found\n");
294 return ERR_PTR(-ENODEV);
297 /* Look for the Button Descriptors UUID */
299 for (i = 0; (i + 1) < desc->package.count; i += 2) {
300 uuid = &desc->package.elements[i];
302 if (uuid->type != ACPI_TYPE_BUFFER ||
303 uuid->buffer.length != 16 ||
304 desc->package.elements[i + 1].type != ACPI_TYPE_PACKAGE) {
308 if (memcmp(uuid->buffer.pointer, btns_desc_uuid, 16) == 0) {
309 btns_desc = &desc->package.elements[i + 1];
315 dev_err(dev, "ACPI Button Descriptors not found\n");
316 button_info = ERR_PTR(-ENODEV);
320 /* The first package describes the collection */
321 el0 = &btns_desc->package.elements[0];
322 if (el0->type == ACPI_TYPE_PACKAGE &&
323 el0->package.count == 5 &&
324 /* First byte should be 0 (collection) */
325 soc_button_get_acpi_object_int(&el0->package.elements[0]) == 0 &&
326 /* Third byte should be 0 (top level collection) */
327 soc_button_get_acpi_object_int(&el0->package.elements[2]) == 0) {
328 collection_uid = soc_button_get_acpi_object_int(
329 &el0->package.elements[1]);
331 if (collection_uid == -1) {
332 dev_err(dev, "Invalid Button Collection Descriptor\n");
333 button_info = ERR_PTR(-ENODEV);
337 /* There are package.count - 1 buttons + 1 terminating empty entry */
338 button_info = devm_kcalloc(dev, btns_desc->package.count,
339 sizeof(*button_info), GFP_KERNEL);
341 button_info = ERR_PTR(-ENOMEM);
345 /* Parse the button descriptors */
346 for (i = 1, btn = 0; i < btns_desc->package.count; i++, btn++) {
347 if (soc_button_parse_btn_desc(dev,
348 &btns_desc->package.elements[i],
350 &button_info[btn])) {
351 button_info = ERR_PTR(-ENODEV);
361 static int soc_button_remove(struct platform_device *pdev)
363 struct soc_button_data *priv = platform_get_drvdata(pdev);
367 for (i = 0; i < BUTTON_TYPES; i++)
368 if (priv->children[i])
369 platform_device_unregister(priv->children[i]);
374 static int soc_button_probe(struct platform_device *pdev)
376 struct device *dev = &pdev->dev;
377 const struct soc_device_data *device_data;
378 const struct soc_button_info *button_info;
379 struct soc_button_data *priv;
380 struct platform_device *pd;
384 device_data = acpi_device_get_match_data(dev);
385 if (device_data && device_data->check) {
386 error = device_data->check(dev);
391 if (device_data && device_data->button_info) {
392 button_info = device_data->button_info;
394 button_info = soc_button_get_button_info(dev);
395 if (IS_ERR(button_info))
396 return PTR_ERR(button_info);
399 error = gpiod_count(dev, NULL);
401 dev_dbg(dev, "no GPIO attached, ignoring...\n");
405 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
409 platform_set_drvdata(pdev, priv);
411 for (i = 0; i < BUTTON_TYPES; i++) {
412 pd = soc_button_device_create(pdev, button_info, i == 0);
415 if (error != -ENODEV) {
416 soc_button_remove(pdev);
422 priv->children[i] = pd;
425 if (!priv->children[0] && !priv->children[1])
428 if (!device_data || !device_data->button_info)
429 devm_kfree(dev, button_info);
435 * Definition of buttons on the tablet. The ACPI index of each button
436 * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
439 static const struct soc_button_info soc_button_PNP0C40[] = {
440 { "power", 0, EV_KEY, KEY_POWER, false, true, true },
441 { "home", 1, EV_KEY, KEY_LEFTMETA, false, true, true },
442 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
443 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
444 { "rotation_lock", 4, EV_KEY, KEY_ROTATE_LOCK_TOGGLE, false, false, true },
448 static const struct soc_device_data soc_device_PNP0C40 = {
449 .button_info = soc_button_PNP0C40,
452 static const struct soc_button_info soc_button_INT33D3[] = {
453 { "tablet_mode", 0, EV_SW, SW_TABLET_MODE, false, false, false },
457 static const struct soc_device_data soc_device_INT33D3 = {
458 .button_info = soc_button_INT33D3,
462 * Special device check for Surface Book 2 and Surface Pro (2017).
463 * Both, the Surface Pro 4 (surfacepro3_button.c) and the above mentioned
464 * devices use MSHW0040 for power and volume buttons, however the way they
465 * have to be addressed differs. Make sure that we only load this drivers
466 * for the correct devices by checking the OEM Platform Revision provided by
469 #define MSHW0040_DSM_REVISION 0x01
470 #define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
471 static const guid_t MSHW0040_DSM_UUID =
472 GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
475 static int soc_device_check_MSHW0040(struct device *dev)
477 acpi_handle handle = ACPI_HANDLE(dev);
478 union acpi_object *result;
479 u64 oem_platform_rev = 0; // valid revisions are nonzero
481 // get OEM platform revision
482 result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
483 MSHW0040_DSM_REVISION,
484 MSHW0040_DSM_GET_OMPR, NULL,
488 oem_platform_rev = result->integer.value;
493 * If the revision is zero here, the _DSM evaluation has failed. This
494 * indicates that we have a Pro 4 or Book 1 and this driver should not
497 if (oem_platform_rev == 0)
500 dev_dbg(dev, "OEM Platform Revision %llu\n", oem_platform_rev);
506 * Button infos for Microsoft Surface Book 2 and Surface Pro (2017).
507 * Obtained from DSDT/testing.
509 static const struct soc_button_info soc_button_MSHW0040[] = {
510 { "power", 0, EV_KEY, KEY_POWER, false, true, true },
511 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
512 { "volume_down", 4, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
516 static const struct soc_device_data soc_device_MSHW0040 = {
517 .button_info = soc_button_MSHW0040,
518 .check = soc_device_check_MSHW0040,
521 static const struct acpi_device_id soc_button_acpi_match[] = {
522 { "PNP0C40", (unsigned long)&soc_device_PNP0C40 },
523 { "INT33D3", (unsigned long)&soc_device_INT33D3 },
524 { "ID9001", (unsigned long)&soc_device_INT33D3 },
527 /* Microsoft Surface Devices (5th and 6th generation) */
528 { "MSHW0040", (unsigned long)&soc_device_MSHW0040 },
533 MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
535 static struct platform_driver soc_button_driver = {
536 .probe = soc_button_probe,
537 .remove = soc_button_remove,
539 .name = KBUILD_MODNAME,
540 .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
543 module_platform_driver(soc_button_driver);
545 MODULE_LICENSE("GPL");