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/platform_data/x86/soc.h> 19 #include <linux/platform_device.h> 20 21 static bool use_low_level_irq; 22 module_param(use_low_level_irq, bool, 0444); 23 MODULE_PARM_DESC(use_low_level_irq, "Use low-level triggered IRQ instead of edge triggered"); 24 25 struct 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 35 struct 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 47 struct 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 */ 66 static 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 */ 115 static 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 */ 135 static 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, acpi_index, GPIOD_ASIS); 141 if (IS_ERR(desc)) 142 return PTR_ERR(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 152 static struct platform_device * 153 soc_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 = 0; 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(&pdev->dev, 171 sizeof(*gpio_keys_pdata) + 172 sizeof(*gpio_keys) * n_buttons, 173 GFP_KERNEL); 174 if (!gpio_keys_pdata) 175 return ERR_PTR(-ENOMEM); 176 177 gpio_keys = (void *)(gpio_keys_pdata + 1); 178 n_buttons = 0; 179 180 dmi_id = dmi_first_match(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(&pdev->dev, info->acpi_index, &gpio, &irq); 192 if (error || irq < 0) { 193 /* 194 * Propagate -EPROBE_DEFER, skip button on other errors. 195 * 196 * -EPROBE_DEFER is ignored on Bay & Cherry Trail. Here 197 * Intel is using so called virtual GPIOs which are not 198 * GPIOs at all but some way for AML code to check some 199 * random status bits without need a custom opregion. 200 * In some cases the resources table we parse points to 201 * such a virtual GPIO, since these are not real GPIOs 202 * we do not have a driver for these so they will never 203 * show up, therefore we ignore -EPROBE_DEFER. 204 */ 205 if ((error == -EPROBE_DEFER || irq == -EPROBE_DEFER) && 206 !(soc_intel_is_byt() || soc_intel_is_cht())) { 207 error = -EPROBE_DEFER; 208 goto err_free_mem; 209 } 210 211 continue; 212 } 213 214 /* See dmi_use_low_level_irq[] comment */ 215 if (!autorepeat && (use_low_level_irq || 216 dmi_check_system(dmi_use_low_level_irq))) { 217 irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW); 218 gpio_keys[n_buttons].irq = irq; 219 gpio_keys[n_buttons].gpio = -ENOENT; 220 } else { 221 gpio_keys[n_buttons].gpio = gpio; 222 } 223 224 gpio_keys[n_buttons].type = info->event_type; 225 gpio_keys[n_buttons].code = info->event_code; 226 gpio_keys[n_buttons].active_low = info->active_low; 227 gpio_keys[n_buttons].desc = info->name; 228 gpio_keys[n_buttons].wakeup = info->wakeup; 229 /* These devices often use cheap buttons, use 50 ms debounce */ 230 gpio_keys[n_buttons].debounce_interval = 50; 231 n_buttons++; 232 } 233 234 if (n_buttons == 0) { 235 error = -ENODEV; 236 goto err_free_mem; 237 } 238 239 gpio_keys_pdata->buttons = gpio_keys; 240 gpio_keys_pdata->nbuttons = n_buttons; 241 gpio_keys_pdata->rep = autorepeat; 242 243 pd = platform_device_register_resndata(&pdev->dev, "gpio-keys", 244 PLATFORM_DEVID_AUTO, NULL, 0, 245 gpio_keys_pdata, 246 sizeof(*gpio_keys_pdata)); 247 error = PTR_ERR_OR_ZERO(pd); 248 if (error) { 249 dev_err(&pdev->dev, 250 "failed registering gpio-keys: %d\n", error); 251 goto err_free_mem; 252 } 253 254 return pd; 255 256 err_free_mem: 257 devm_kfree(&pdev->dev, gpio_keys_pdata); 258 return ERR_PTR(error); 259 } 260 261 static int soc_button_get_acpi_object_int(const union acpi_object *obj) 262 { 263 if (obj->type != ACPI_TYPE_INTEGER) 264 return -1; 265 266 return obj->integer.value; 267 } 268 269 /* Parse a single ACPI0011 _DSD button descriptor */ 270 static int soc_button_parse_btn_desc(struct device *dev, 271 const union acpi_object *desc, 272 int collection_uid, 273 struct soc_button_info *info) 274 { 275 int upage, usage; 276 277 if (desc->type != ACPI_TYPE_PACKAGE || 278 desc->package.count != 5 || 279 /* First byte should be 1 (control) */ 280 soc_button_get_acpi_object_int(&desc->package.elements[0]) != 1 || 281 /* Third byte should be collection uid */ 282 soc_button_get_acpi_object_int(&desc->package.elements[2]) != 283 collection_uid) { 284 dev_err(dev, "Invalid ACPI Button Descriptor\n"); 285 return -ENODEV; 286 } 287 288 info->event_type = EV_KEY; 289 info->active_low = true; 290 info->acpi_index = 291 soc_button_get_acpi_object_int(&desc->package.elements[1]); 292 upage = soc_button_get_acpi_object_int(&desc->package.elements[3]); 293 usage = soc_button_get_acpi_object_int(&desc->package.elements[4]); 294 295 /* 296 * The UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e descriptors use HID 297 * usage page and usage codes, but otherwise the device is not HID 298 * compliant: it uses one irq per button instead of generating HID 299 * input reports and some buttons should generate wakeups where as 300 * others should not, so we cannot use the HID subsystem. 301 * 302 * Luckily all devices only use a few usage page + usage combinations, 303 * so we can simply check for the known combinations here. 304 */ 305 if (upage == 0x01 && usage == 0x81) { 306 info->name = "power"; 307 info->event_code = KEY_POWER; 308 info->wakeup = true; 309 } else if (upage == 0x01 && usage == 0xc6) { 310 info->name = "airplane mode switch"; 311 info->event_type = EV_SW; 312 info->event_code = SW_RFKILL_ALL; 313 info->active_low = false; 314 } else if (upage == 0x01 && usage == 0xca) { 315 info->name = "rotation lock switch"; 316 info->event_type = EV_SW; 317 info->event_code = SW_ROTATE_LOCK; 318 } else if (upage == 0x07 && usage == 0xe3) { 319 info->name = "home"; 320 info->event_code = KEY_LEFTMETA; 321 info->wakeup = true; 322 } else if (upage == 0x0c && usage == 0xe9) { 323 info->name = "volume_up"; 324 info->event_code = KEY_VOLUMEUP; 325 info->autorepeat = true; 326 } else if (upage == 0x0c && usage == 0xea) { 327 info->name = "volume_down"; 328 info->event_code = KEY_VOLUMEDOWN; 329 info->autorepeat = true; 330 } else { 331 dev_warn(dev, "Unknown button index %d upage %02x usage %02x, ignoring\n", 332 info->acpi_index, upage, usage); 333 info->name = "unknown"; 334 info->event_code = KEY_RESERVED; 335 } 336 337 return 0; 338 } 339 340 /* ACPI0011 _DSD btns descriptors UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e */ 341 static const u8 btns_desc_uuid[16] = { 342 0x25, 0xd6, 0x6b, 0xfa, 0xe8, 0x9c, 0x0d, 0x47, 343 0xa2, 0xc7, 0xb3, 0xca, 0x36, 0xc4, 0x28, 0x2e 344 }; 345 346 /* Parse ACPI0011 _DSD button descriptors */ 347 static struct soc_button_info *soc_button_get_button_info(struct device *dev) 348 { 349 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 350 const union acpi_object *desc, *el0, *uuid, *btns_desc = NULL; 351 struct soc_button_info *button_info; 352 acpi_status status; 353 int i, btn, collection_uid = -1; 354 355 status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL, 356 &buf, ACPI_TYPE_PACKAGE); 357 if (ACPI_FAILURE(status)) { 358 dev_err(dev, "ACPI _DSD object not found\n"); 359 return ERR_PTR(-ENODEV); 360 } 361 362 /* Look for the Button Descriptors UUID */ 363 desc = buf.pointer; 364 for (i = 0; (i + 1) < desc->package.count; i += 2) { 365 uuid = &desc->package.elements[i]; 366 367 if (uuid->type != ACPI_TYPE_BUFFER || 368 uuid->buffer.length != 16 || 369 desc->package.elements[i + 1].type != ACPI_TYPE_PACKAGE) { 370 break; 371 } 372 373 if (memcmp(uuid->buffer.pointer, btns_desc_uuid, 16) == 0) { 374 btns_desc = &desc->package.elements[i + 1]; 375 break; 376 } 377 } 378 379 if (!btns_desc || !btns_desc->package.count) { 380 dev_err(dev, "ACPI Button Descriptors not found\n"); 381 button_info = ERR_PTR(-ENODEV); 382 goto out; 383 } 384 385 /* The first package describes the collection */ 386 el0 = &btns_desc->package.elements[0]; 387 if (el0->type == ACPI_TYPE_PACKAGE && 388 el0->package.count == 5 && 389 /* First byte should be 0 (collection) */ 390 soc_button_get_acpi_object_int(&el0->package.elements[0]) == 0 && 391 /* Third byte should be 0 (top level collection) */ 392 soc_button_get_acpi_object_int(&el0->package.elements[2]) == 0) { 393 collection_uid = soc_button_get_acpi_object_int( 394 &el0->package.elements[1]); 395 } 396 if (collection_uid == -1) { 397 dev_err(dev, "Invalid Button Collection Descriptor\n"); 398 button_info = ERR_PTR(-ENODEV); 399 goto out; 400 } 401 402 /* There are package.count - 1 buttons + 1 terminating empty entry */ 403 button_info = devm_kcalloc(dev, btns_desc->package.count, 404 sizeof(*button_info), GFP_KERNEL); 405 if (!button_info) { 406 button_info = ERR_PTR(-ENOMEM); 407 goto out; 408 } 409 410 /* Parse the button descriptors */ 411 for (i = 1, btn = 0; i < btns_desc->package.count; i++, btn++) { 412 if (soc_button_parse_btn_desc(dev, 413 &btns_desc->package.elements[i], 414 collection_uid, 415 &button_info[btn])) { 416 button_info = ERR_PTR(-ENODEV); 417 goto out; 418 } 419 } 420 421 out: 422 kfree(buf.pointer); 423 return button_info; 424 } 425 426 static void soc_button_remove(struct platform_device *pdev) 427 { 428 struct soc_button_data *priv = platform_get_drvdata(pdev); 429 430 int i; 431 432 for (i = 0; i < BUTTON_TYPES; i++) 433 if (priv->children[i]) 434 platform_device_unregister(priv->children[i]); 435 } 436 437 static int soc_button_probe(struct platform_device *pdev) 438 { 439 struct device *dev = &pdev->dev; 440 const struct soc_device_data *device_data; 441 const struct soc_button_info *button_info; 442 struct soc_button_data *priv; 443 struct platform_device *pd; 444 int i; 445 int error; 446 447 device_data = acpi_device_get_match_data(dev); 448 if (device_data && device_data->check) { 449 error = device_data->check(dev); 450 if (error) 451 return error; 452 } 453 454 if (device_data && device_data->button_info) { 455 button_info = device_data->button_info; 456 } else { 457 button_info = soc_button_get_button_info(dev); 458 if (IS_ERR(button_info)) 459 return PTR_ERR(button_info); 460 } 461 462 error = gpiod_count(dev, NULL); 463 if (error < 0) { 464 dev_dbg(dev, "no GPIO attached, ignoring...\n"); 465 return -ENODEV; 466 } 467 468 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 469 if (!priv) 470 return -ENOMEM; 471 472 platform_set_drvdata(pdev, priv); 473 474 for (i = 0; i < BUTTON_TYPES; i++) { 475 pd = soc_button_device_create(pdev, button_info, i == 0); 476 if (IS_ERR(pd)) { 477 error = PTR_ERR(pd); 478 if (error != -ENODEV) { 479 soc_button_remove(pdev); 480 return error; 481 } 482 continue; 483 } 484 485 priv->children[i] = pd; 486 } 487 488 if (!priv->children[0] && !priv->children[1]) 489 return -ENODEV; 490 491 if (!device_data || !device_data->button_info) 492 devm_kfree(dev, button_info); 493 494 return 0; 495 } 496 497 /* 498 * Definition of buttons on the tablet. The ACPI index of each button 499 * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC 500 * Platforms" 501 */ 502 static const struct soc_button_info soc_button_PNP0C40[] = { 503 { "power", 0, EV_KEY, KEY_POWER, false, true, true }, 504 { "home", 1, EV_KEY, KEY_LEFTMETA, false, true, true }, 505 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true }, 506 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false, true }, 507 { "rotation_lock", 4, EV_KEY, KEY_ROTATE_LOCK_TOGGLE, false, false, true }, 508 { } 509 }; 510 511 static const struct soc_device_data soc_device_PNP0C40 = { 512 .button_info = soc_button_PNP0C40, 513 }; 514 515 static const struct soc_button_info soc_button_INT33D3[] = { 516 { "tablet_mode", 0, EV_SW, SW_TABLET_MODE, false, false, false }, 517 { } 518 }; 519 520 static const struct soc_device_data soc_device_INT33D3 = { 521 .button_info = soc_button_INT33D3, 522 }; 523 524 /* 525 * Button info for Microsoft Surface 3 (non pro), this is identical to 526 * the PNP0C40 info except that the home button is active-high. 527 * 528 * The Surface 3 Pro also has a MSHW0028 ACPI device, but that uses a custom 529 * version of the drivers/platform/x86/intel/hid.c 5 button array ACPI API 530 * instead. A check() callback is not necessary though as the Surface 3 Pro 531 * MSHW0028 ACPI device's resource table does not contain any GPIOs. 532 */ 533 static const struct soc_button_info soc_button_MSHW0028[] = { 534 { "power", 0, EV_KEY, KEY_POWER, false, true, true }, 535 { "home", 1, EV_KEY, KEY_LEFTMETA, false, true, false }, 536 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true }, 537 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false, true }, 538 { } 539 }; 540 541 static const struct soc_device_data soc_device_MSHW0028 = { 542 .button_info = soc_button_MSHW0028, 543 }; 544 545 /* 546 * Special device check for Surface Book 2 and Surface Pro (2017). 547 * Both, the Surface Pro 4 (surfacepro3_button.c) and the above mentioned 548 * devices use MSHW0040 for power and volume buttons, however the way they 549 * have to be addressed differs. Make sure that we only load this drivers 550 * for the correct devices by checking the OEM Platform Revision provided by 551 * the _DSM method. 552 */ 553 #define MSHW0040_DSM_REVISION 0x01 554 #define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision 555 static const guid_t MSHW0040_DSM_UUID = 556 GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65, 557 0x49, 0x80, 0x35); 558 559 static int soc_device_check_MSHW0040(struct device *dev) 560 { 561 acpi_handle handle = ACPI_HANDLE(dev); 562 union acpi_object *result; 563 u64 oem_platform_rev = 0; // valid revisions are nonzero 564 565 // get OEM platform revision 566 result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID, 567 MSHW0040_DSM_REVISION, 568 MSHW0040_DSM_GET_OMPR, NULL, 569 ACPI_TYPE_INTEGER); 570 571 if (result) { 572 oem_platform_rev = result->integer.value; 573 ACPI_FREE(result); 574 } 575 576 /* 577 * If the revision is zero here, the _DSM evaluation has failed. This 578 * indicates that we have a Pro 4 or Book 1 and this driver should not 579 * be used. 580 */ 581 if (oem_platform_rev == 0) 582 return -ENODEV; 583 584 dev_dbg(dev, "OEM Platform Revision %llu\n", oem_platform_rev); 585 586 return 0; 587 } 588 589 /* 590 * Button infos for Microsoft Surface Book 2 and Surface Pro (2017). 591 * Obtained from DSDT/testing. 592 */ 593 static const struct soc_button_info soc_button_MSHW0040[] = { 594 { "power", 0, EV_KEY, KEY_POWER, false, true, true }, 595 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true }, 596 { "volume_down", 4, EV_KEY, KEY_VOLUMEDOWN, true, false, true }, 597 { } 598 }; 599 600 static const struct soc_device_data soc_device_MSHW0040 = { 601 .button_info = soc_button_MSHW0040, 602 .check = soc_device_check_MSHW0040, 603 }; 604 605 static const struct acpi_device_id soc_button_acpi_match[] = { 606 { "PNP0C40", (unsigned long)&soc_device_PNP0C40 }, 607 { "INT33D3", (unsigned long)&soc_device_INT33D3 }, 608 { "ID9001", (unsigned long)&soc_device_INT33D3 }, 609 { "ACPI0011", 0 }, 610 611 /* Microsoft Surface Devices (3th, 5th and 6th generation) */ 612 { "MSHW0028", (unsigned long)&soc_device_MSHW0028 }, 613 { "MSHW0040", (unsigned long)&soc_device_MSHW0040 }, 614 615 { } 616 }; 617 618 MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match); 619 620 static struct platform_driver soc_button_driver = { 621 .probe = soc_button_probe, 622 .remove = soc_button_remove, 623 .driver = { 624 .name = KBUILD_MODNAME, 625 .acpi_match_table = ACPI_PTR(soc_button_acpi_match), 626 }, 627 }; 628 module_platform_driver(soc_button_driver); 629 630 MODULE_DESCRIPTION("Windows-compatible SoC Button Array driver"); 631 MODULE_LICENSE("GPL"); 632