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