1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * DMI based code to deal with broken DSDTs on X86 tablets which ship with 4 * Android as (part of) the factory image. The factory kernels shipped on these 5 * devices typically have a bunch of things hardcoded, rather than specified 6 * in their DSDT. 7 * 8 * Copyright (C) 2021-2023 Hans de Goede <hansg@kernel.org> 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/acpi.h> 14 #include <linux/bug.h> 15 #include <linux/device.h> 16 #include <linux/dmi.h> 17 #include <linux/fwnode.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/gpio/machine.h> 20 #include <linux/irq.h> 21 #include <linux/module.h> 22 #include <linux/pci.h> 23 #include <linux/platform_device.h> 24 #include <linux/serdev.h> 25 #include <linux/string.h> 26 27 #include "x86-android-tablets.h" 28 #include "../serdev_helpers.h" 29 30 static struct platform_device *x86_android_tablet_device; 31 32 /* 33 * This helper allows getting a GPIO descriptor *before* the actual device 34 * consuming it has been instantiated. This function MUST only be used to 35 * handle this special case such as, e.g.: 36 * 37 * 1. Getting an IRQ from a GPIO for i2c_board_info.irq which is passed to 38 * i2c_client_new() to instantiate i2c_client-s; or 39 * 2. Calling desc_to_gpio() to get an old style GPIO number for gpio-keys 40 * platform_data which still uses old style GPIO numbers. 41 * 42 * Since the consuming device has not been instantiated yet a dynamic lookup 43 * is generated using the special x86_android_tablet device for dev_id. 44 * 45 * For normal GPIO lookups a standard static struct gpiod_lookup_table MUST be used. 46 */ 47 int x86_android_tablet_get_gpiod(const char *chip, int pin, const char *con_id, 48 bool active_low, enum gpiod_flags dflags, 49 struct gpio_desc **desc) 50 { 51 struct gpiod_lookup_table *lookup; 52 struct gpio_desc *gpiod; 53 54 lookup = kzalloc_flex(*lookup, table, 2); 55 if (!lookup) 56 return -ENOMEM; 57 58 lookup->dev_id = KBUILD_MODNAME; 59 lookup->table[0] = 60 GPIO_LOOKUP(chip, pin, con_id, active_low ? GPIO_ACTIVE_LOW : GPIO_ACTIVE_HIGH); 61 62 gpiod_add_lookup_table(lookup); 63 gpiod = devm_gpiod_get(&x86_android_tablet_device->dev, con_id, dflags); 64 gpiod_remove_lookup_table(lookup); 65 kfree(lookup); 66 67 if (IS_ERR(gpiod)) { 68 pr_err("error %ld getting GPIO %s %d\n", PTR_ERR(gpiod), chip, pin); 69 return PTR_ERR(gpiod); 70 } 71 72 if (desc) 73 *desc = gpiod; 74 75 return 0; 76 } 77 78 int x86_acpi_irq_helper_get(const struct x86_acpi_irq_data *data) 79 { 80 struct irq_fwspec fwspec = { }; 81 struct irq_domain *domain; 82 struct acpi_device *adev; 83 struct gpio_desc *gpiod; 84 unsigned int irq_type; 85 acpi_handle handle; 86 acpi_status status; 87 int irq, ret; 88 89 switch (data->type) { 90 case X86_ACPI_IRQ_TYPE_APIC: 91 /* 92 * The DSDT may already reference the GSI in a device skipped by 93 * acpi_quirk_skip_i2c_client_enumeration(). Unregister the GSI 94 * to avoid -EBUSY errors in this case. 95 */ 96 acpi_unregister_gsi(data->index); 97 irq = acpi_register_gsi(NULL, data->index, data->trigger, data->polarity); 98 if (irq < 0) 99 pr_err("error %d getting APIC IRQ %d\n", irq, data->index); 100 101 return irq; 102 case X86_ACPI_IRQ_TYPE_GPIOINT: 103 /* Like acpi_dev_gpio_irq_get(), but without parsing ACPI resources */ 104 ret = x86_android_tablet_get_gpiod(data->chip, data->index, data->con_id, 105 false, GPIOD_ASIS, &gpiod); 106 if (ret) 107 return ret; 108 109 irq = gpiod_to_irq(gpiod); 110 if (irq < 0) { 111 pr_err("error %d getting IRQ %s %d\n", irq, data->chip, data->index); 112 return irq; 113 } 114 115 irq_type = acpi_dev_get_irq_type(data->trigger, data->polarity); 116 if (irq_type != IRQ_TYPE_NONE && irq_type != irq_get_trigger_type(irq)) 117 irq_set_irq_type(irq, irq_type); 118 119 if (data->free_gpio) 120 devm_gpiod_put(&x86_android_tablet_device->dev, gpiod); 121 122 return irq; 123 case X86_ACPI_IRQ_TYPE_PMIC: 124 status = acpi_get_handle(NULL, data->chip, &handle); 125 if (ACPI_FAILURE(status)) { 126 pr_err("error could not get %s handle\n", data->chip); 127 return -ENODEV; 128 } 129 130 adev = acpi_fetch_acpi_dev(handle); 131 if (!adev) { 132 pr_err("error could not get %s adev\n", data->chip); 133 return -ENODEV; 134 } 135 136 fwspec.fwnode = acpi_fwnode_handle(adev); 137 domain = irq_find_matching_fwspec(&fwspec, data->domain); 138 if (!domain) { 139 pr_err("error could not find IRQ domain for %s\n", data->chip); 140 return -ENODEV; 141 } 142 143 return irq_create_mapping(domain, data->index); 144 default: 145 return 0; 146 } 147 } 148 149 static int i2c_client_count; 150 static int spi_dev_count; 151 static int pdev_count; 152 static int serdev_count; 153 static struct i2c_client **i2c_clients; 154 static struct spi_device **spi_devs; 155 static struct platform_device **pdevs; 156 static struct serdev_device **serdevs; 157 static const struct software_node **gpio_button_swnodes; 158 static const struct software_node **swnode_group; 159 static void (*exit_handler)(void); 160 161 static __init struct i2c_adapter * 162 get_i2c_adap_by_handle(const struct x86_i2c_client_info *client_info) 163 { 164 acpi_handle handle; 165 acpi_status status; 166 167 status = acpi_get_handle(NULL, client_info->adapter_path, &handle); 168 if (ACPI_FAILURE(status)) { 169 pr_err("Error could not get %s handle\n", client_info->adapter_path); 170 return NULL; 171 } 172 173 return i2c_acpi_find_adapter_by_handle(handle); 174 } 175 176 static __init int match_parent(struct device *dev, const void *data) 177 { 178 return dev->parent == data; 179 } 180 181 static __init struct i2c_adapter * 182 get_i2c_adap_by_pci_parent(const struct x86_i2c_client_info *client_info) 183 { 184 struct i2c_adapter *adap = NULL; 185 struct device *pdev, *adap_dev; 186 187 pdev = bus_find_device_by_name(&pci_bus_type, NULL, client_info->adapter_path); 188 if (!pdev) { 189 pr_err("Error could not find %s PCI device\n", client_info->adapter_path); 190 return NULL; 191 } 192 193 adap_dev = bus_find_device(&i2c_bus_type, NULL, pdev, match_parent); 194 if (adap_dev) { 195 adap = i2c_verify_adapter(adap_dev); 196 if (!adap) 197 put_device(adap_dev); 198 } 199 200 put_device(pdev); 201 202 return adap; 203 } 204 205 static __init int x86_instantiate_i2c_client(const struct x86_dev_info *dev_info, 206 int idx) 207 { 208 const struct x86_i2c_client_info *client_info = &dev_info->i2c_client_info[idx]; 209 struct i2c_board_info board_info = client_info->board_info; 210 struct i2c_adapter *adap; 211 212 board_info.irq = x86_acpi_irq_helper_get(&client_info->irq_data); 213 if (board_info.irq < 0) 214 return board_info.irq; 215 216 if (dev_info->use_pci) 217 adap = get_i2c_adap_by_pci_parent(client_info); 218 else 219 adap = get_i2c_adap_by_handle(client_info); 220 221 if (!adap) { 222 pr_err("error could not get %s adapter\n", client_info->adapter_path); 223 return -ENODEV; 224 } 225 226 i2c_clients[idx] = i2c_new_client_device(adap, &board_info); 227 put_device(&adap->dev); 228 if (IS_ERR(i2c_clients[idx])) 229 return dev_err_probe(&adap->dev, PTR_ERR(i2c_clients[idx]), 230 "creating I2C-client %d\n", idx); 231 232 return 0; 233 } 234 235 static __init int x86_instantiate_spi_dev(const struct x86_dev_info *dev_info, int idx) 236 { 237 const struct x86_spi_dev_info *spi_dev_info = &dev_info->spi_dev_info[idx]; 238 struct spi_board_info board_info = spi_dev_info->board_info; 239 struct spi_controller *controller; 240 struct acpi_device *adev; 241 acpi_handle handle; 242 acpi_status status; 243 244 board_info.irq = x86_acpi_irq_helper_get(&spi_dev_info->irq_data); 245 if (board_info.irq < 0) 246 return board_info.irq; 247 248 status = acpi_get_handle(NULL, spi_dev_info->ctrl_path, &handle); 249 if (ACPI_FAILURE(status)) { 250 pr_err("Error could not get %s handle\n", spi_dev_info->ctrl_path); 251 return -ENODEV; 252 } 253 254 adev = acpi_fetch_acpi_dev(handle); 255 if (!adev) { 256 pr_err("Error could not get adev for %s\n", spi_dev_info->ctrl_path); 257 return -ENODEV; 258 } 259 260 controller = acpi_spi_find_controller_by_adev(adev); 261 if (!controller) { 262 pr_err("Error could not get SPI controller for %s\n", spi_dev_info->ctrl_path); 263 return -ENODEV; 264 } 265 266 spi_devs[idx] = spi_new_device(controller, &board_info); 267 put_device(&controller->dev); 268 if (!spi_devs[idx]) 269 return -ENOMEM; 270 271 return 0; 272 } 273 274 static __init struct device * 275 get_serdev_controller_by_pci_parent(const struct x86_serdev_info *info) 276 { 277 struct pci_dev *pdev; 278 279 pdev = pci_get_domain_bus_and_slot(0, 0, info->ctrl.pci.devfn); 280 if (!pdev) { 281 pr_err("error could not get PCI serdev at devfn 0x%02x\n", info->ctrl.pci.devfn); 282 return ERR_PTR(-ENODEV); 283 } 284 285 /* This puts our reference on pdev and returns a ref on the ctrl */ 286 return get_serdev_controller_from_parent(&pdev->dev, 0, info->ctrl_devname); 287 } 288 289 static __init int x86_instantiate_serdev(const struct x86_dev_info *dev_info, int idx) 290 { 291 const struct x86_serdev_info *info = &dev_info->serdev_info[idx]; 292 struct acpi_device *serdev_adev; 293 struct serdev_device *serdev; 294 struct device *ctrl_dev; 295 int ret = -ENODEV; 296 297 if (dev_info->use_pci) 298 ctrl_dev = get_serdev_controller_by_pci_parent(info); 299 else 300 ctrl_dev = get_serdev_controller(info->ctrl.acpi.hid, info->ctrl.acpi.uid, 301 0, info->ctrl_devname); 302 if (IS_ERR(ctrl_dev)) 303 return PTR_ERR(ctrl_dev); 304 305 serdev_adev = acpi_dev_get_first_match_dev(info->serdev_hid, NULL, -1); 306 if (!serdev_adev) { 307 pr_err("error could not get %s serdev adev\n", info->serdev_hid); 308 goto put_ctrl_dev; 309 } 310 311 serdev = serdev_device_alloc(to_serdev_controller(ctrl_dev)); 312 if (!serdev) { 313 ret = -ENOMEM; 314 goto put_serdev_adev; 315 } 316 317 ACPI_COMPANION_SET(&serdev->dev, serdev_adev); 318 acpi_device_set_enumerated(serdev_adev); 319 320 ret = serdev_device_add(serdev); 321 if (ret) { 322 dev_err(&serdev->dev, "error %d adding serdev\n", ret); 323 serdev_device_put(serdev); 324 goto put_serdev_adev; 325 } 326 327 serdevs[idx] = serdev; 328 329 put_serdev_adev: 330 acpi_dev_put(serdev_adev); 331 put_ctrl_dev: 332 put_device(ctrl_dev); 333 return ret; 334 } 335 336 const struct software_node baytrail_gpiochip_nodes[] = { 337 { .name = "INT33FC:00" }, 338 { .name = "INT33FC:01" }, 339 { .name = "INT33FC:02" }, 340 }; 341 342 static const struct software_node *baytrail_gpiochip_node_group[] = { 343 &baytrail_gpiochip_nodes[0], 344 &baytrail_gpiochip_nodes[1], 345 &baytrail_gpiochip_nodes[2], 346 NULL 347 }; 348 349 const struct software_node cherryview_gpiochip_nodes[] = { 350 { .name = "INT33FF:00" }, 351 { .name = "INT33FF:01" }, 352 { .name = "INT33FF:02" }, 353 { .name = "INT33FF:03" }, 354 }; 355 356 static const struct software_node *cherryview_gpiochip_node_group[] = { 357 &cherryview_gpiochip_nodes[0], 358 &cherryview_gpiochip_nodes[1], 359 &cherryview_gpiochip_nodes[2], 360 &cherryview_gpiochip_nodes[3], 361 NULL 362 }; 363 364 const struct software_node crystalcove_gpiochip_node = { 365 .name = "INT33FD:00", 366 }; 367 368 static const struct software_node *crystalcove_gpiochip_node_group[] = { 369 &crystalcove_gpiochip_node, 370 NULL 371 }; 372 373 static void gpio_secondary_unset(void *data) 374 { 375 struct device *dev = data; 376 377 set_secondary_fwnode(dev, NULL); 378 put_device(dev); 379 } 380 381 static void gpio_secondary_unregister_node_group(void *data) 382 { 383 const struct software_node **nodes = data; 384 385 software_node_unregister_node_group(nodes); 386 } 387 388 static int gpio_secondary_fwnode_init(struct device *parent, 389 const struct software_node * const *node_group) 390 { 391 const struct software_node *const *swnode; 392 struct fwnode_handle *fwnode; 393 struct device *phys_dev; 394 int ret; 395 396 if (!node_group) 397 return 0; 398 399 ret = software_node_register_node_group(node_group); 400 if (ret) 401 return ret; 402 403 ret = devm_add_action_or_reset(parent, 404 gpio_secondary_unregister_node_group, 405 (void *)node_group); 406 if (ret) 407 return ret; 408 409 for (swnode = node_group; *swnode; swnode++) { 410 struct device *dev __free(put_device) = 411 acpi_bus_find_device_by_name((*swnode)->name); 412 if (!dev) 413 return dev_err_probe(parent, 414 -ENODEV, "Failed to find the required GPIO controller: %s\n", 415 (*swnode)->name); 416 417 fwnode = software_node_fwnode(*swnode); 418 if (WARN_ON(!fwnode)) 419 return -ENOENT; 420 421 phys_dev = acpi_get_first_physical_node(to_acpi_device(dev)); 422 if (!phys_dev) 423 return dev_err_probe(parent, -ENODEV, 424 "No physical device for ACPI GPIO dev: %pfwP\n", 425 fwnode); 426 427 set_secondary_fwnode(phys_dev, fwnode); 428 429 ret = devm_add_action_or_reset(parent, gpio_secondary_unset, get_device(phys_dev)); 430 if (ret) 431 return ret; 432 } 433 434 return 0; 435 } 436 437 static void x86_android_tablet_remove(struct platform_device *pdev) 438 { 439 int i; 440 441 for (i = serdev_count - 1; i >= 0; i--) { 442 if (serdevs[i]) 443 serdev_device_remove(serdevs[i]); 444 } 445 446 kfree(serdevs); 447 448 for (i = pdev_count - 1; i >= 0; i--) 449 platform_device_unregister(pdevs[i]); 450 451 kfree(pdevs); 452 453 for (i = spi_dev_count - 1; i >= 0; i--) 454 spi_unregister_device(spi_devs[i]); 455 456 kfree(spi_devs); 457 458 for (i = i2c_client_count - 1; i >= 0; i--) 459 i2c_unregister_device(i2c_clients[i]); 460 461 kfree(i2c_clients); 462 463 if (exit_handler) 464 exit_handler(); 465 466 software_node_unregister_node_group(gpio_button_swnodes); 467 software_node_unregister_node_group(swnode_group); 468 } 469 470 static __init int x86_android_tablet_probe(struct platform_device *pdev) 471 { 472 const struct software_node * const *gpiochip_node_group; 473 const struct x86_dev_info *dev_info; 474 const struct dmi_system_id *id; 475 int i, ret = 0; 476 477 id = dmi_first_match(x86_android_tablet_ids); 478 if (!id) 479 return -ENODEV; 480 481 dev_info = id->driver_data; 482 /* Allow x86_android_tablet_device use before probe() exits */ 483 x86_android_tablet_device = pdev; 484 485 /* 486 * Since this runs from module_init() it cannot use -EPROBE_DEFER, 487 * instead pre-load any modules which are listed as requirements. 488 */ 489 for (i = 0; dev_info->modules && dev_info->modules[i]; i++) 490 request_module(dev_info->modules[i]); 491 492 switch (dev_info->gpiochip_type) { 493 case X86_GPIOCHIP_BAYTRAIL: 494 gpiochip_node_group = baytrail_gpiochip_node_group; 495 break; 496 case X86_GPIOCHIP_CHERRYVIEW: 497 gpiochip_node_group = cherryview_gpiochip_node_group; 498 break; 499 case X86_GPIOCHIP_UNSPECIFIED: 500 gpiochip_node_group = NULL; 501 break; 502 } 503 504 ret = gpio_secondary_fwnode_init(&pdev->dev, gpiochip_node_group); 505 if (ret) { 506 x86_android_tablet_remove(pdev); 507 return ret; 508 } 509 510 if (dev_info->has_crystalcove) { 511 ret = gpio_secondary_fwnode_init(&pdev->dev, crystalcove_gpiochip_node_group); 512 if (ret) { 513 x86_android_tablet_remove(pdev); 514 return ret; 515 } 516 } 517 518 ret = software_node_register_node_group(dev_info->swnode_group); 519 if (ret) { 520 x86_android_tablet_remove(pdev); 521 return ret; 522 } 523 swnode_group = dev_info->swnode_group; 524 525 if (dev_info->init) { 526 ret = dev_info->init(&pdev->dev); 527 if (ret < 0) { 528 x86_android_tablet_remove(pdev); 529 return ret; 530 } 531 exit_handler = dev_info->exit; 532 } 533 534 i2c_clients = kzalloc_objs(*i2c_clients, dev_info->i2c_client_count); 535 if (!i2c_clients) { 536 x86_android_tablet_remove(pdev); 537 return -ENOMEM; 538 } 539 540 i2c_client_count = dev_info->i2c_client_count; 541 for (i = 0; i < i2c_client_count; i++) { 542 ret = x86_instantiate_i2c_client(dev_info, i); 543 if (ret < 0) { 544 x86_android_tablet_remove(pdev); 545 return ret; 546 } 547 } 548 549 spi_devs = kzalloc_objs(*spi_devs, dev_info->spi_dev_count); 550 if (!spi_devs) { 551 x86_android_tablet_remove(pdev); 552 return -ENOMEM; 553 } 554 555 spi_dev_count = dev_info->spi_dev_count; 556 for (i = 0; i < spi_dev_count; i++) { 557 ret = x86_instantiate_spi_dev(dev_info, i); 558 if (ret < 0) { 559 x86_android_tablet_remove(pdev); 560 return ret; 561 } 562 } 563 564 /* + 1 to make space for the (optional) gpio_keys_button platform device */ 565 pdevs = kzalloc_objs(*pdevs, dev_info->pdev_count + 1); 566 if (!pdevs) { 567 x86_android_tablet_remove(pdev); 568 return -ENOMEM; 569 } 570 571 pdev_count = dev_info->pdev_count; 572 for (i = 0; i < pdev_count; i++) { 573 pdevs[i] = platform_device_register_full(&dev_info->pdev_info[i]); 574 if (IS_ERR(pdevs[i])) { 575 ret = PTR_ERR(pdevs[i]); 576 x86_android_tablet_remove(pdev); 577 return ret; 578 } 579 } 580 581 serdevs = kzalloc_objs(*serdevs, dev_info->serdev_count); 582 if (!serdevs) { 583 x86_android_tablet_remove(pdev); 584 return -ENOMEM; 585 } 586 587 serdev_count = dev_info->serdev_count; 588 for (i = 0; i < serdev_count; i++) { 589 ret = x86_instantiate_serdev(dev_info, i); 590 if (ret < 0) { 591 x86_android_tablet_remove(pdev); 592 return ret; 593 } 594 } 595 596 if (dev_info->gpio_button_swnodes) { 597 struct platform_device_info button_info = { 598 .name = "gpio-keys", 599 .id = PLATFORM_DEVID_AUTO, 600 }; 601 602 ret = software_node_register_node_group(dev_info->gpio_button_swnodes); 603 if (ret < 0) { 604 x86_android_tablet_remove(pdev); 605 return ret; 606 } 607 608 gpio_button_swnodes = dev_info->gpio_button_swnodes; 609 610 button_info.fwnode = software_node_fwnode(dev_info->gpio_button_swnodes[0]); 611 pdevs[pdev_count] = platform_device_register_full(&button_info); 612 if (IS_ERR(pdevs[pdev_count])) { 613 ret = PTR_ERR(pdevs[pdev_count]); 614 x86_android_tablet_remove(pdev); 615 return ret; 616 } 617 pdev_count++; 618 } 619 620 return 0; 621 } 622 623 static struct platform_driver x86_android_tablet_driver = { 624 .driver = { 625 .name = KBUILD_MODNAME, 626 }, 627 .remove = x86_android_tablet_remove, 628 }; 629 630 static int __init x86_android_tablet_init(void) 631 { 632 x86_android_tablet_device = platform_create_bundle(&x86_android_tablet_driver, 633 x86_android_tablet_probe, 634 NULL, 0, NULL, 0); 635 636 return PTR_ERR_OR_ZERO(x86_android_tablet_device); 637 } 638 module_init(x86_android_tablet_init); 639 640 static void __exit x86_android_tablet_exit(void) 641 { 642 platform_device_unregister(x86_android_tablet_device); 643 platform_driver_unregister(&x86_android_tablet_driver); 644 } 645 module_exit(x86_android_tablet_exit); 646 647 MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>"); 648 MODULE_DESCRIPTION("X86 Android tablets DSDT fixups driver"); 649 MODULE_LICENSE("GPL"); 650