1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Linux I2C core ACPI support code 4 * 5 * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com> 6 */ 7 8 #include <linux/acpi.h> 9 #include <linux/device.h> 10 #include <linux/err.h> 11 #include <linux/i2c.h> 12 #include <linux/list.h> 13 #include <linux/module.h> 14 #include <linux/slab.h> 15 16 #include "i2c-core.h" 17 18 struct i2c_acpi_handler_data { 19 struct acpi_connection_info info; 20 struct i2c_adapter *adapter; 21 }; 22 23 struct gsb_buffer { 24 u8 status; 25 u8 len; 26 union { 27 u16 wdata; 28 u8 bdata; 29 u8 data[0]; 30 }; 31 } __packed; 32 33 struct i2c_acpi_lookup { 34 struct i2c_board_info *info; 35 acpi_handle adapter_handle; 36 acpi_handle device_handle; 37 acpi_handle search_handle; 38 int n; 39 int index; 40 u32 speed; 41 u32 min_speed; 42 }; 43 44 /** 45 * i2c_acpi_get_i2c_resource - Gets I2cSerialBus resource if type matches 46 * @ares: ACPI resource 47 * @i2c: Pointer to I2cSerialBus resource will be returned here 48 * 49 * Checks if the given ACPI resource is of type I2cSerialBus. 50 * In this case, returns a pointer to it to the caller. 51 * 52 * Returns true if resource type is of I2cSerialBus, otherwise false. 53 */ 54 bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares, 55 struct acpi_resource_i2c_serialbus **i2c) 56 { 57 struct acpi_resource_i2c_serialbus *sb; 58 59 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 60 return false; 61 62 sb = &ares->data.i2c_serial_bus; 63 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) 64 return false; 65 66 *i2c = sb; 67 return true; 68 } 69 EXPORT_SYMBOL_GPL(i2c_acpi_get_i2c_resource); 70 71 static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data) 72 { 73 struct i2c_acpi_lookup *lookup = data; 74 struct i2c_board_info *info = lookup->info; 75 struct acpi_resource_i2c_serialbus *sb; 76 acpi_status status; 77 78 if (info->addr || !i2c_acpi_get_i2c_resource(ares, &sb)) 79 return 1; 80 81 if (lookup->index != -1 && lookup->n++ != lookup->index) 82 return 1; 83 84 status = acpi_get_handle(lookup->device_handle, 85 sb->resource_source.string_ptr, 86 &lookup->adapter_handle); 87 if (ACPI_FAILURE(status)) 88 return 1; 89 90 info->addr = sb->slave_address; 91 lookup->speed = sb->connection_speed; 92 if (sb->access_mode == ACPI_I2C_10BIT_MODE) 93 info->flags |= I2C_CLIENT_TEN; 94 95 return 1; 96 } 97 98 static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = { 99 /* 100 * ACPI video acpi_devices, which are handled by the acpi-video driver 101 * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these. 102 */ 103 { ACPI_VIDEO_HID, 0 }, 104 {} 105 }; 106 107 static int i2c_acpi_do_lookup(struct acpi_device *adev, 108 struct i2c_acpi_lookup *lookup) 109 { 110 struct i2c_board_info *info = lookup->info; 111 struct list_head resource_list; 112 int ret; 113 114 if (acpi_bus_get_status(adev) || !adev->status.present || 115 acpi_device_enumerated(adev)) 116 return -EINVAL; 117 118 if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0) 119 return -ENODEV; 120 121 memset(info, 0, sizeof(*info)); 122 lookup->device_handle = acpi_device_handle(adev); 123 124 /* Look up for I2cSerialBus resource */ 125 INIT_LIST_HEAD(&resource_list); 126 ret = acpi_dev_get_resources(adev, &resource_list, 127 i2c_acpi_fill_info, lookup); 128 acpi_dev_free_resource_list(&resource_list); 129 130 if (ret < 0 || !info->addr) 131 return -EINVAL; 132 133 return 0; 134 } 135 136 static int i2c_acpi_get_info(struct acpi_device *adev, 137 struct i2c_board_info *info, 138 struct i2c_adapter *adapter, 139 acpi_handle *adapter_handle) 140 { 141 struct list_head resource_list; 142 struct resource_entry *entry; 143 struct i2c_acpi_lookup lookup; 144 int ret; 145 146 memset(&lookup, 0, sizeof(lookup)); 147 lookup.info = info; 148 lookup.index = -1; 149 150 ret = i2c_acpi_do_lookup(adev, &lookup); 151 if (ret) 152 return ret; 153 154 if (adapter) { 155 /* The adapter must match the one in I2cSerialBus() connector */ 156 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle) 157 return -ENODEV; 158 } else { 159 struct acpi_device *adapter_adev; 160 161 /* The adapter must be present */ 162 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev)) 163 return -ENODEV; 164 if (acpi_bus_get_status(adapter_adev) || 165 !adapter_adev->status.present) 166 return -ENODEV; 167 } 168 169 info->fwnode = acpi_fwnode_handle(adev); 170 if (adapter_handle) 171 *adapter_handle = lookup.adapter_handle; 172 173 /* Then fill IRQ number if any */ 174 INIT_LIST_HEAD(&resource_list); 175 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL); 176 if (ret < 0) 177 return -EINVAL; 178 179 resource_list_for_each_entry(entry, &resource_list) { 180 if (resource_type(entry->res) == IORESOURCE_IRQ) { 181 info->irq = entry->res->start; 182 break; 183 } 184 } 185 186 acpi_dev_free_resource_list(&resource_list); 187 188 acpi_set_modalias(adev, dev_name(&adev->dev), info->type, 189 sizeof(info->type)); 190 191 return 0; 192 } 193 194 static void i2c_acpi_register_device(struct i2c_adapter *adapter, 195 struct acpi_device *adev, 196 struct i2c_board_info *info) 197 { 198 adev->power.flags.ignore_parent = true; 199 acpi_device_set_enumerated(adev); 200 201 if (!i2c_new_device(adapter, info)) { 202 adev->power.flags.ignore_parent = false; 203 dev_err(&adapter->dev, 204 "failed to add I2C device %s from ACPI\n", 205 dev_name(&adev->dev)); 206 } 207 } 208 209 static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level, 210 void *data, void **return_value) 211 { 212 struct i2c_adapter *adapter = data; 213 struct acpi_device *adev; 214 struct i2c_board_info info; 215 216 if (acpi_bus_get_device(handle, &adev)) 217 return AE_OK; 218 219 if (i2c_acpi_get_info(adev, &info, adapter, NULL)) 220 return AE_OK; 221 222 i2c_acpi_register_device(adapter, adev, &info); 223 224 return AE_OK; 225 } 226 227 #define I2C_ACPI_MAX_SCAN_DEPTH 32 228 229 /** 230 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter 231 * @adap: pointer to adapter 232 * 233 * Enumerate all I2C slave devices behind this adapter by walking the ACPI 234 * namespace. When a device is found it will be added to the Linux device 235 * model and bound to the corresponding ACPI handle. 236 */ 237 void i2c_acpi_register_devices(struct i2c_adapter *adap) 238 { 239 acpi_status status; 240 241 if (!has_acpi_companion(&adap->dev)) 242 return; 243 244 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 245 I2C_ACPI_MAX_SCAN_DEPTH, 246 i2c_acpi_add_device, NULL, 247 adap, NULL); 248 if (ACPI_FAILURE(status)) 249 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n"); 250 } 251 252 const struct acpi_device_id * 253 i2c_acpi_match_device(const struct acpi_device_id *matches, 254 struct i2c_client *client) 255 { 256 if (!(client && matches)) 257 return NULL; 258 259 return acpi_match_device(matches, &client->dev); 260 } 261 262 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level, 263 void *data, void **return_value) 264 { 265 struct i2c_acpi_lookup *lookup = data; 266 struct acpi_device *adev; 267 268 if (acpi_bus_get_device(handle, &adev)) 269 return AE_OK; 270 271 if (i2c_acpi_do_lookup(adev, lookup)) 272 return AE_OK; 273 274 if (lookup->search_handle != lookup->adapter_handle) 275 return AE_OK; 276 277 if (lookup->speed <= lookup->min_speed) 278 lookup->min_speed = lookup->speed; 279 280 return AE_OK; 281 } 282 283 /** 284 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI 285 * @dev: The device owning the bus 286 * 287 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves 288 * devices connected to this bus and use the speed of slowest device. 289 * 290 * Returns the speed in Hz or zero 291 */ 292 u32 i2c_acpi_find_bus_speed(struct device *dev) 293 { 294 struct i2c_acpi_lookup lookup; 295 struct i2c_board_info dummy; 296 acpi_status status; 297 298 if (!has_acpi_companion(dev)) 299 return 0; 300 301 memset(&lookup, 0, sizeof(lookup)); 302 lookup.search_handle = ACPI_HANDLE(dev); 303 lookup.min_speed = UINT_MAX; 304 lookup.info = &dummy; 305 lookup.index = -1; 306 307 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 308 I2C_ACPI_MAX_SCAN_DEPTH, 309 i2c_acpi_lookup_speed, NULL, 310 &lookup, NULL); 311 312 if (ACPI_FAILURE(status)) { 313 dev_warn(dev, "unable to find I2C bus speed from ACPI\n"); 314 return 0; 315 } 316 317 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0; 318 } 319 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed); 320 321 static int i2c_acpi_find_match_adapter(struct device *dev, void *data) 322 { 323 struct i2c_adapter *adapter = i2c_verify_adapter(dev); 324 325 if (!adapter) 326 return 0; 327 328 return ACPI_HANDLE(dev) == (acpi_handle)data; 329 } 330 331 static int i2c_acpi_find_match_device(struct device *dev, void *data) 332 { 333 return ACPI_COMPANION(dev) == data; 334 } 335 336 struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle) 337 { 338 struct device *dev; 339 340 dev = bus_find_device(&i2c_bus_type, NULL, handle, 341 i2c_acpi_find_match_adapter); 342 return dev ? i2c_verify_adapter(dev) : NULL; 343 } 344 EXPORT_SYMBOL_GPL(i2c_acpi_find_adapter_by_handle); 345 346 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev) 347 { 348 struct device *dev; 349 350 dev = bus_find_device(&i2c_bus_type, NULL, adev, 351 i2c_acpi_find_match_device); 352 return dev ? i2c_verify_client(dev) : NULL; 353 } 354 355 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value, 356 void *arg) 357 { 358 struct acpi_device *adev = arg; 359 struct i2c_board_info info; 360 acpi_handle adapter_handle; 361 struct i2c_adapter *adapter; 362 struct i2c_client *client; 363 364 switch (value) { 365 case ACPI_RECONFIG_DEVICE_ADD: 366 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle)) 367 break; 368 369 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle); 370 if (!adapter) 371 break; 372 373 i2c_acpi_register_device(adapter, adev, &info); 374 break; 375 case ACPI_RECONFIG_DEVICE_REMOVE: 376 if (!acpi_device_enumerated(adev)) 377 break; 378 379 client = i2c_acpi_find_client_by_adev(adev); 380 if (!client) 381 break; 382 383 i2c_unregister_device(client); 384 put_device(&client->dev); 385 break; 386 } 387 388 return NOTIFY_OK; 389 } 390 391 struct notifier_block i2c_acpi_notifier = { 392 .notifier_call = i2c_acpi_notify, 393 }; 394 395 /** 396 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource 397 * @dev: Device owning the ACPI resources to get the client from 398 * @index: Index of ACPI resource to get 399 * @info: describes the I2C device; note this is modified (addr gets set) 400 * Context: can sleep 401 * 402 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus 403 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus 404 * resources, in that case this function can be used to create an i2c-client 405 * for other I2cSerialBus resources in the Current Resource Settings table. 406 * 407 * Also see i2c_new_device, which this function calls to create the i2c-client. 408 * 409 * Returns a pointer to the new i2c-client, or error pointer in case of failure. 410 * Specifically, -EPROBE_DEFER is returned if the adapter is not found. 411 */ 412 struct i2c_client *i2c_acpi_new_device(struct device *dev, int index, 413 struct i2c_board_info *info) 414 { 415 struct i2c_acpi_lookup lookup; 416 struct i2c_adapter *adapter; 417 struct i2c_client *client; 418 struct acpi_device *adev; 419 LIST_HEAD(resource_list); 420 int ret; 421 422 adev = ACPI_COMPANION(dev); 423 if (!adev) 424 return ERR_PTR(-EINVAL); 425 426 memset(&lookup, 0, sizeof(lookup)); 427 lookup.info = info; 428 lookup.device_handle = acpi_device_handle(adev); 429 lookup.index = index; 430 431 ret = acpi_dev_get_resources(adev, &resource_list, 432 i2c_acpi_fill_info, &lookup); 433 if (ret < 0) 434 return ERR_PTR(ret); 435 436 acpi_dev_free_resource_list(&resource_list); 437 438 if (!info->addr) 439 return ERR_PTR(-EADDRNOTAVAIL); 440 441 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle); 442 if (!adapter) 443 return ERR_PTR(-EPROBE_DEFER); 444 445 client = i2c_new_device(adapter, info); 446 if (!client) 447 return ERR_PTR(-ENODEV); 448 449 return client; 450 } 451 EXPORT_SYMBOL_GPL(i2c_acpi_new_device); 452 453 #ifdef CONFIG_ACPI_I2C_OPREGION 454 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client, 455 u8 cmd, u8 *data, u8 data_len) 456 { 457 458 struct i2c_msg msgs[2]; 459 int ret; 460 u8 *buffer; 461 462 buffer = kzalloc(data_len, GFP_KERNEL); 463 if (!buffer) 464 return AE_NO_MEMORY; 465 466 msgs[0].addr = client->addr; 467 msgs[0].flags = client->flags; 468 msgs[0].len = 1; 469 msgs[0].buf = &cmd; 470 471 msgs[1].addr = client->addr; 472 msgs[1].flags = client->flags | I2C_M_RD; 473 msgs[1].len = data_len; 474 msgs[1].buf = buffer; 475 476 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 477 if (ret < 0) { 478 /* Getting a NACK is unfortunately normal with some DSTDs */ 479 if (ret == -EREMOTEIO) 480 dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n", 481 data_len, client->addr, cmd, ret); 482 else 483 dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n", 484 data_len, client->addr, cmd, ret); 485 /* 2 transfers must have completed successfully */ 486 } else if (ret == 2) { 487 memcpy(data, buffer, data_len); 488 ret = 0; 489 } else { 490 ret = -EIO; 491 } 492 493 kfree(buffer); 494 return ret; 495 } 496 497 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client, 498 u8 cmd, u8 *data, u8 data_len) 499 { 500 501 struct i2c_msg msgs[1]; 502 u8 *buffer; 503 int ret = AE_OK; 504 505 buffer = kzalloc(data_len + 1, GFP_KERNEL); 506 if (!buffer) 507 return AE_NO_MEMORY; 508 509 buffer[0] = cmd; 510 memcpy(buffer + 1, data, data_len); 511 512 msgs[0].addr = client->addr; 513 msgs[0].flags = client->flags; 514 msgs[0].len = data_len + 1; 515 msgs[0].buf = buffer; 516 517 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 518 519 kfree(buffer); 520 521 if (ret < 0) { 522 dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret); 523 return ret; 524 } 525 526 /* 1 transfer must have completed successfully */ 527 return (ret == 1) ? 0 : -EIO; 528 } 529 530 static acpi_status 531 i2c_acpi_space_handler(u32 function, acpi_physical_address command, 532 u32 bits, u64 *value64, 533 void *handler_context, void *region_context) 534 { 535 struct gsb_buffer *gsb = (struct gsb_buffer *)value64; 536 struct i2c_acpi_handler_data *data = handler_context; 537 struct acpi_connection_info *info = &data->info; 538 struct acpi_resource_i2c_serialbus *sb; 539 struct i2c_adapter *adapter = data->adapter; 540 struct i2c_client *client; 541 struct acpi_resource *ares; 542 u32 accessor_type = function >> 16; 543 u8 action = function & ACPI_IO_MASK; 544 acpi_status ret; 545 int status; 546 547 ret = acpi_buffer_to_resource(info->connection, info->length, &ares); 548 if (ACPI_FAILURE(ret)) 549 return ret; 550 551 client = kzalloc(sizeof(*client), GFP_KERNEL); 552 if (!client) { 553 ret = AE_NO_MEMORY; 554 goto err; 555 } 556 557 if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) { 558 ret = AE_BAD_PARAMETER; 559 goto err; 560 } 561 562 client->adapter = adapter; 563 client->addr = sb->slave_address; 564 565 if (sb->access_mode == ACPI_I2C_10BIT_MODE) 566 client->flags |= I2C_CLIENT_TEN; 567 568 switch (accessor_type) { 569 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV: 570 if (action == ACPI_READ) { 571 status = i2c_smbus_read_byte(client); 572 if (status >= 0) { 573 gsb->bdata = status; 574 status = 0; 575 } 576 } else { 577 status = i2c_smbus_write_byte(client, gsb->bdata); 578 } 579 break; 580 581 case ACPI_GSB_ACCESS_ATTRIB_BYTE: 582 if (action == ACPI_READ) { 583 status = i2c_smbus_read_byte_data(client, command); 584 if (status >= 0) { 585 gsb->bdata = status; 586 status = 0; 587 } 588 } else { 589 status = i2c_smbus_write_byte_data(client, command, 590 gsb->bdata); 591 } 592 break; 593 594 case ACPI_GSB_ACCESS_ATTRIB_WORD: 595 if (action == ACPI_READ) { 596 status = i2c_smbus_read_word_data(client, command); 597 if (status >= 0) { 598 gsb->wdata = status; 599 status = 0; 600 } 601 } else { 602 status = i2c_smbus_write_word_data(client, command, 603 gsb->wdata); 604 } 605 break; 606 607 case ACPI_GSB_ACCESS_ATTRIB_BLOCK: 608 if (action == ACPI_READ) { 609 status = i2c_smbus_read_block_data(client, command, 610 gsb->data); 611 if (status >= 0) { 612 gsb->len = status; 613 status = 0; 614 } 615 } else { 616 status = i2c_smbus_write_block_data(client, command, 617 gsb->len, gsb->data); 618 } 619 break; 620 621 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE: 622 if (action == ACPI_READ) { 623 status = acpi_gsb_i2c_read_bytes(client, command, 624 gsb->data, info->access_length); 625 } else { 626 status = acpi_gsb_i2c_write_bytes(client, command, 627 gsb->data, info->access_length); 628 } 629 break; 630 631 default: 632 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n", 633 accessor_type, client->addr); 634 ret = AE_BAD_PARAMETER; 635 goto err; 636 } 637 638 gsb->status = status; 639 640 err: 641 kfree(client); 642 ACPI_FREE(ares); 643 return ret; 644 } 645 646 647 int i2c_acpi_install_space_handler(struct i2c_adapter *adapter) 648 { 649 acpi_handle handle; 650 struct i2c_acpi_handler_data *data; 651 acpi_status status; 652 653 if (!adapter->dev.parent) 654 return -ENODEV; 655 656 handle = ACPI_HANDLE(adapter->dev.parent); 657 658 if (!handle) 659 return -ENODEV; 660 661 data = kzalloc(sizeof(struct i2c_acpi_handler_data), 662 GFP_KERNEL); 663 if (!data) 664 return -ENOMEM; 665 666 data->adapter = adapter; 667 status = acpi_bus_attach_private_data(handle, (void *)data); 668 if (ACPI_FAILURE(status)) { 669 kfree(data); 670 return -ENOMEM; 671 } 672 673 status = acpi_install_address_space_handler(handle, 674 ACPI_ADR_SPACE_GSBUS, 675 &i2c_acpi_space_handler, 676 NULL, 677 data); 678 if (ACPI_FAILURE(status)) { 679 dev_err(&adapter->dev, "Error installing i2c space handler\n"); 680 acpi_bus_detach_private_data(handle); 681 kfree(data); 682 return -ENOMEM; 683 } 684 685 acpi_walk_dep_device_list(handle); 686 return 0; 687 } 688 689 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter) 690 { 691 acpi_handle handle; 692 struct i2c_acpi_handler_data *data; 693 acpi_status status; 694 695 if (!adapter->dev.parent) 696 return; 697 698 handle = ACPI_HANDLE(adapter->dev.parent); 699 700 if (!handle) 701 return; 702 703 acpi_remove_address_space_handler(handle, 704 ACPI_ADR_SPACE_GSBUS, 705 &i2c_acpi_space_handler); 706 707 status = acpi_bus_get_private_data(handle, (void **)&data); 708 if (ACPI_SUCCESS(status)) 709 kfree(data); 710 711 acpi_bus_detach_private_data(handle); 712 } 713 #endif /* CONFIG_ACPI_I2C_OPREGION */ 714