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