1 /* 2 * Linux I2C core 3 * 4 * Copyright (C) 1995-99 Simon G. Vogl 5 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> 6 * Mux support by Rodolfo Giometti <giometti@enneenne.com> and 7 * Michael Lawnick <michael.lawnick.ext@nsn.com> 8 * 9 * Copyright (C) 2013-2017 Wolfram Sang <wsa@the-dreams.de> 10 * 11 * This program is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License as published by the Free 13 * Software Foundation; either version 2 of the License, or (at your option) 14 * any later version. 15 * 16 * This program is distributed in the hope that it will be useful, but WITHOUT 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 19 */ 20 21 #define pr_fmt(fmt) "i2c-core: " fmt 22 23 #include <dt-bindings/i2c/i2c.h> 24 #include <linux/acpi.h> 25 #include <linux/clk/clk-conf.h> 26 #include <linux/completion.h> 27 #include <linux/delay.h> 28 #include <linux/err.h> 29 #include <linux/errno.h> 30 #include <linux/gpio/consumer.h> 31 #include <linux/i2c.h> 32 #include <linux/i2c-smbus.h> 33 #include <linux/idr.h> 34 #include <linux/init.h> 35 #include <linux/irqflags.h> 36 #include <linux/jump_label.h> 37 #include <linux/kernel.h> 38 #include <linux/module.h> 39 #include <linux/mutex.h> 40 #include <linux/of_device.h> 41 #include <linux/of.h> 42 #include <linux/of_irq.h> 43 #include <linux/pm_domain.h> 44 #include <linux/pm_runtime.h> 45 #include <linux/pm_wakeirq.h> 46 #include <linux/property.h> 47 #include <linux/rwsem.h> 48 #include <linux/slab.h> 49 50 #include "i2c-core.h" 51 52 #define CREATE_TRACE_POINTS 53 #include <trace/events/i2c.h> 54 55 #define I2C_ADDR_OFFSET_TEN_BIT 0xa000 56 #define I2C_ADDR_OFFSET_SLAVE 0x1000 57 58 #define I2C_ADDR_7BITS_MAX 0x77 59 #define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1) 60 61 #define I2C_ADDR_DEVICE_ID 0x7c 62 63 /* 64 * core_lock protects i2c_adapter_idr, and guarantees that device detection, 65 * deletion of detected devices are serialized 66 */ 67 static DEFINE_MUTEX(core_lock); 68 static DEFINE_IDR(i2c_adapter_idr); 69 70 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); 71 72 static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key); 73 static bool is_registered; 74 75 int i2c_transfer_trace_reg(void) 76 { 77 static_branch_inc(&i2c_trace_msg_key); 78 return 0; 79 } 80 81 void i2c_transfer_trace_unreg(void) 82 { 83 static_branch_dec(&i2c_trace_msg_key); 84 } 85 86 const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 87 const struct i2c_client *client) 88 { 89 if (!(id && client)) 90 return NULL; 91 92 while (id->name[0]) { 93 if (strcmp(client->name, id->name) == 0) 94 return id; 95 id++; 96 } 97 return NULL; 98 } 99 EXPORT_SYMBOL_GPL(i2c_match_id); 100 101 static int i2c_device_match(struct device *dev, struct device_driver *drv) 102 { 103 struct i2c_client *client = i2c_verify_client(dev); 104 struct i2c_driver *driver; 105 106 107 /* Attempt an OF style match */ 108 if (i2c_of_match_device(drv->of_match_table, client)) 109 return 1; 110 111 /* Then ACPI style match */ 112 if (acpi_driver_match_device(dev, drv)) 113 return 1; 114 115 driver = to_i2c_driver(drv); 116 117 /* Finally an I2C match */ 118 if (i2c_match_id(driver->id_table, client)) 119 return 1; 120 121 return 0; 122 } 123 124 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) 125 { 126 struct i2c_client *client = to_i2c_client(dev); 127 int rc; 128 129 rc = of_device_uevent_modalias(dev, env); 130 if (rc != -ENODEV) 131 return rc; 132 133 rc = acpi_device_uevent_modalias(dev, env); 134 if (rc != -ENODEV) 135 return rc; 136 137 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name); 138 } 139 140 /* i2c bus recovery routines */ 141 static int get_scl_gpio_value(struct i2c_adapter *adap) 142 { 143 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod); 144 } 145 146 static void set_scl_gpio_value(struct i2c_adapter *adap, int val) 147 { 148 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val); 149 } 150 151 static int get_sda_gpio_value(struct i2c_adapter *adap) 152 { 153 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod); 154 } 155 156 static void set_sda_gpio_value(struct i2c_adapter *adap, int val) 157 { 158 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val); 159 } 160 161 static int i2c_generic_bus_free(struct i2c_adapter *adap) 162 { 163 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 164 int ret = -EOPNOTSUPP; 165 166 if (bri->get_bus_free) 167 ret = bri->get_bus_free(adap); 168 else if (bri->get_sda) 169 ret = bri->get_sda(adap); 170 171 if (ret < 0) 172 return ret; 173 174 return ret ? 0 : -EBUSY; 175 } 176 177 /* 178 * We are generating clock pulses. ndelay() determines durating of clk pulses. 179 * We will generate clock with rate 100 KHz and so duration of both clock levels 180 * is: delay in ns = (10^6 / 100) / 2 181 */ 182 #define RECOVERY_NDELAY 5000 183 #define RECOVERY_CLK_CNT 9 184 185 int i2c_generic_scl_recovery(struct i2c_adapter *adap) 186 { 187 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 188 int i = 0, scl = 1, ret = 0; 189 190 if (bri->prepare_recovery) 191 bri->prepare_recovery(adap); 192 193 /* 194 * If we can set SDA, we will always create a STOP to ensure additional 195 * pulses will do no harm. This is achieved by letting SDA follow SCL 196 * half a cycle later. Check the 'incomplete_write_byte' fault injector 197 * for details. 198 */ 199 bri->set_scl(adap, scl); 200 ndelay(RECOVERY_NDELAY / 2); 201 if (bri->set_sda) 202 bri->set_sda(adap, scl); 203 ndelay(RECOVERY_NDELAY / 2); 204 205 /* 206 * By this time SCL is high, as we need to give 9 falling-rising edges 207 */ 208 while (i++ < RECOVERY_CLK_CNT * 2) { 209 if (scl) { 210 /* SCL shouldn't be low here */ 211 if (!bri->get_scl(adap)) { 212 dev_err(&adap->dev, 213 "SCL is stuck low, exit recovery\n"); 214 ret = -EBUSY; 215 break; 216 } 217 } 218 219 scl = !scl; 220 bri->set_scl(adap, scl); 221 /* Creating STOP again, see above */ 222 ndelay(RECOVERY_NDELAY / 2); 223 if (bri->set_sda) 224 bri->set_sda(adap, scl); 225 ndelay(RECOVERY_NDELAY / 2); 226 227 if (scl) { 228 ret = i2c_generic_bus_free(adap); 229 if (ret == 0) 230 break; 231 } 232 } 233 234 /* If we can't check bus status, assume recovery worked */ 235 if (ret == -EOPNOTSUPP) 236 ret = 0; 237 238 if (bri->unprepare_recovery) 239 bri->unprepare_recovery(adap); 240 241 return ret; 242 } 243 EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery); 244 245 int i2c_recover_bus(struct i2c_adapter *adap) 246 { 247 if (!adap->bus_recovery_info) 248 return -EOPNOTSUPP; 249 250 dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); 251 return adap->bus_recovery_info->recover_bus(adap); 252 } 253 EXPORT_SYMBOL_GPL(i2c_recover_bus); 254 255 static void i2c_init_recovery(struct i2c_adapter *adap) 256 { 257 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 258 char *err_str; 259 260 if (!bri) 261 return; 262 263 if (!bri->recover_bus) { 264 err_str = "no recover_bus() found"; 265 goto err; 266 } 267 268 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) { 269 bri->get_scl = get_scl_gpio_value; 270 bri->set_scl = set_scl_gpio_value; 271 if (bri->sda_gpiod) { 272 bri->get_sda = get_sda_gpio_value; 273 /* FIXME: add proper flag instead of '0' once available */ 274 if (gpiod_get_direction(bri->sda_gpiod) == 0) 275 bri->set_sda = set_sda_gpio_value; 276 } 277 return; 278 } 279 280 if (bri->recover_bus == i2c_generic_scl_recovery) { 281 /* Generic SCL recovery */ 282 if (!bri->set_scl || !bri->get_scl) { 283 err_str = "no {get|set}_scl() found"; 284 goto err; 285 } 286 if (!bri->set_sda && !bri->get_sda) { 287 err_str = "either get_sda() or set_sda() needed"; 288 goto err; 289 } 290 } 291 292 return; 293 err: 294 dev_err(&adap->dev, "Not using recovery: %s\n", err_str); 295 adap->bus_recovery_info = NULL; 296 } 297 298 static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client) 299 { 300 struct i2c_adapter *adap = client->adapter; 301 unsigned int irq; 302 303 if (!adap->host_notify_domain) 304 return -ENXIO; 305 306 if (client->flags & I2C_CLIENT_TEN) 307 return -EINVAL; 308 309 irq = irq_create_mapping(adap->host_notify_domain, client->addr); 310 311 return irq > 0 ? irq : -ENXIO; 312 } 313 314 static int i2c_device_probe(struct device *dev) 315 { 316 struct i2c_client *client = i2c_verify_client(dev); 317 struct i2c_driver *driver; 318 int status; 319 320 if (!client) 321 return 0; 322 323 driver = to_i2c_driver(dev->driver); 324 325 if (!client->irq && !driver->disable_i2c_core_irq_mapping) { 326 int irq = -ENOENT; 327 328 if (client->flags & I2C_CLIENT_HOST_NOTIFY) { 329 dev_dbg(dev, "Using Host Notify IRQ\n"); 330 /* Keep adapter active when Host Notify is required */ 331 pm_runtime_get_sync(&client->adapter->dev); 332 irq = i2c_smbus_host_notify_to_irq(client); 333 } else if (dev->of_node) { 334 irq = of_irq_get_byname(dev->of_node, "irq"); 335 if (irq == -EINVAL || irq == -ENODATA) 336 irq = of_irq_get(dev->of_node, 0); 337 } else if (ACPI_COMPANION(dev)) { 338 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0); 339 } 340 if (irq == -EPROBE_DEFER) 341 return irq; 342 343 if (irq < 0) 344 irq = 0; 345 346 client->irq = irq; 347 } 348 349 /* 350 * An I2C ID table is not mandatory, if and only if, a suitable OF 351 * or ACPI ID table is supplied for the probing device. 352 */ 353 if (!driver->id_table && 354 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) && 355 !i2c_of_match_device(dev->driver->of_match_table, client)) 356 return -ENODEV; 357 358 if (client->flags & I2C_CLIENT_WAKE) { 359 int wakeirq = -ENOENT; 360 361 if (dev->of_node) { 362 wakeirq = of_irq_get_byname(dev->of_node, "wakeup"); 363 if (wakeirq == -EPROBE_DEFER) 364 return wakeirq; 365 } 366 367 device_init_wakeup(&client->dev, true); 368 369 if (wakeirq > 0 && wakeirq != client->irq) 370 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq); 371 else if (client->irq > 0) 372 status = dev_pm_set_wake_irq(dev, client->irq); 373 else 374 status = 0; 375 376 if (status) 377 dev_warn(&client->dev, "failed to set up wakeup irq\n"); 378 } 379 380 dev_dbg(dev, "probe\n"); 381 382 status = of_clk_set_defaults(dev->of_node, false); 383 if (status < 0) 384 goto err_clear_wakeup_irq; 385 386 status = dev_pm_domain_attach(&client->dev, true); 387 if (status) 388 goto err_clear_wakeup_irq; 389 390 /* 391 * When there are no more users of probe(), 392 * rename probe_new to probe. 393 */ 394 if (driver->probe_new) 395 status = driver->probe_new(client); 396 else if (driver->probe) 397 status = driver->probe(client, 398 i2c_match_id(driver->id_table, client)); 399 else 400 status = -EINVAL; 401 402 if (status) 403 goto err_detach_pm_domain; 404 405 return 0; 406 407 err_detach_pm_domain: 408 dev_pm_domain_detach(&client->dev, true); 409 err_clear_wakeup_irq: 410 dev_pm_clear_wake_irq(&client->dev); 411 device_init_wakeup(&client->dev, false); 412 return status; 413 } 414 415 static int i2c_device_remove(struct device *dev) 416 { 417 struct i2c_client *client = i2c_verify_client(dev); 418 struct i2c_driver *driver; 419 int status = 0; 420 421 if (!client || !dev->driver) 422 return 0; 423 424 driver = to_i2c_driver(dev->driver); 425 if (driver->remove) { 426 dev_dbg(dev, "remove\n"); 427 status = driver->remove(client); 428 } 429 430 dev_pm_domain_detach(&client->dev, true); 431 432 dev_pm_clear_wake_irq(&client->dev); 433 device_init_wakeup(&client->dev, false); 434 435 client->irq = client->init_irq; 436 if (client->flags & I2C_CLIENT_HOST_NOTIFY) 437 pm_runtime_put(&client->adapter->dev); 438 439 return status; 440 } 441 442 static void i2c_device_shutdown(struct device *dev) 443 { 444 struct i2c_client *client = i2c_verify_client(dev); 445 struct i2c_driver *driver; 446 447 if (!client || !dev->driver) 448 return; 449 driver = to_i2c_driver(dev->driver); 450 if (driver->shutdown) 451 driver->shutdown(client); 452 } 453 454 static void i2c_client_dev_release(struct device *dev) 455 { 456 kfree(to_i2c_client(dev)); 457 } 458 459 static ssize_t 460 show_name(struct device *dev, struct device_attribute *attr, char *buf) 461 { 462 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? 463 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); 464 } 465 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 466 467 static ssize_t 468 show_modalias(struct device *dev, struct device_attribute *attr, char *buf) 469 { 470 struct i2c_client *client = to_i2c_client(dev); 471 int len; 472 473 len = of_device_modalias(dev, buf, PAGE_SIZE); 474 if (len != -ENODEV) 475 return len; 476 477 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); 478 if (len != -ENODEV) 479 return len; 480 481 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); 482 } 483 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); 484 485 static struct attribute *i2c_dev_attrs[] = { 486 &dev_attr_name.attr, 487 /* modalias helps coldplug: modprobe $(cat .../modalias) */ 488 &dev_attr_modalias.attr, 489 NULL 490 }; 491 ATTRIBUTE_GROUPS(i2c_dev); 492 493 struct bus_type i2c_bus_type = { 494 .name = "i2c", 495 .match = i2c_device_match, 496 .probe = i2c_device_probe, 497 .remove = i2c_device_remove, 498 .shutdown = i2c_device_shutdown, 499 }; 500 EXPORT_SYMBOL_GPL(i2c_bus_type); 501 502 struct device_type i2c_client_type = { 503 .groups = i2c_dev_groups, 504 .uevent = i2c_device_uevent, 505 .release = i2c_client_dev_release, 506 }; 507 EXPORT_SYMBOL_GPL(i2c_client_type); 508 509 510 /** 511 * i2c_verify_client - return parameter as i2c_client, or NULL 512 * @dev: device, probably from some driver model iterator 513 * 514 * When traversing the driver model tree, perhaps using driver model 515 * iterators like @device_for_each_child(), you can't assume very much 516 * about the nodes you find. Use this function to avoid oopses caused 517 * by wrongly treating some non-I2C device as an i2c_client. 518 */ 519 struct i2c_client *i2c_verify_client(struct device *dev) 520 { 521 return (dev->type == &i2c_client_type) 522 ? to_i2c_client(dev) 523 : NULL; 524 } 525 EXPORT_SYMBOL(i2c_verify_client); 526 527 528 /* Return a unique address which takes the flags of the client into account */ 529 static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client) 530 { 531 unsigned short addr = client->addr; 532 533 /* For some client flags, add an arbitrary offset to avoid collisions */ 534 if (client->flags & I2C_CLIENT_TEN) 535 addr |= I2C_ADDR_OFFSET_TEN_BIT; 536 537 if (client->flags & I2C_CLIENT_SLAVE) 538 addr |= I2C_ADDR_OFFSET_SLAVE; 539 540 return addr; 541 } 542 543 /* This is a permissive address validity check, I2C address map constraints 544 * are purposely not enforced, except for the general call address. */ 545 static int i2c_check_addr_validity(unsigned int addr, unsigned short flags) 546 { 547 if (flags & I2C_CLIENT_TEN) { 548 /* 10-bit address, all values are valid */ 549 if (addr > 0x3ff) 550 return -EINVAL; 551 } else { 552 /* 7-bit address, reject the general call address */ 553 if (addr == 0x00 || addr > 0x7f) 554 return -EINVAL; 555 } 556 return 0; 557 } 558 559 /* And this is a strict address validity check, used when probing. If a 560 * device uses a reserved address, then it shouldn't be probed. 7-bit 561 * addressing is assumed, 10-bit address devices are rare and should be 562 * explicitly enumerated. */ 563 int i2c_check_7bit_addr_validity_strict(unsigned short addr) 564 { 565 /* 566 * Reserved addresses per I2C specification: 567 * 0x00 General call address / START byte 568 * 0x01 CBUS address 569 * 0x02 Reserved for different bus format 570 * 0x03 Reserved for future purposes 571 * 0x04-0x07 Hs-mode master code 572 * 0x78-0x7b 10-bit slave addressing 573 * 0x7c-0x7f Reserved for future purposes 574 */ 575 if (addr < 0x08 || addr > 0x77) 576 return -EINVAL; 577 return 0; 578 } 579 580 static int __i2c_check_addr_busy(struct device *dev, void *addrp) 581 { 582 struct i2c_client *client = i2c_verify_client(dev); 583 int addr = *(int *)addrp; 584 585 if (client && i2c_encode_flags_to_addr(client) == addr) 586 return -EBUSY; 587 return 0; 588 } 589 590 /* walk up mux tree */ 591 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr) 592 { 593 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); 594 int result; 595 596 result = device_for_each_child(&adapter->dev, &addr, 597 __i2c_check_addr_busy); 598 599 if (!result && parent) 600 result = i2c_check_mux_parents(parent, addr); 601 602 return result; 603 } 604 605 /* recurse down mux tree */ 606 static int i2c_check_mux_children(struct device *dev, void *addrp) 607 { 608 int result; 609 610 if (dev->type == &i2c_adapter_type) 611 result = device_for_each_child(dev, addrp, 612 i2c_check_mux_children); 613 else 614 result = __i2c_check_addr_busy(dev, addrp); 615 616 return result; 617 } 618 619 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) 620 { 621 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); 622 int result = 0; 623 624 if (parent) 625 result = i2c_check_mux_parents(parent, addr); 626 627 if (!result) 628 result = device_for_each_child(&adapter->dev, &addr, 629 i2c_check_mux_children); 630 631 return result; 632 } 633 634 /** 635 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment 636 * @adapter: Target I2C bus segment 637 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT 638 * locks only this branch in the adapter tree 639 */ 640 static void i2c_adapter_lock_bus(struct i2c_adapter *adapter, 641 unsigned int flags) 642 { 643 rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter)); 644 } 645 646 /** 647 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment 648 * @adapter: Target I2C bus segment 649 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT 650 * trylocks only this branch in the adapter tree 651 */ 652 static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter, 653 unsigned int flags) 654 { 655 return rt_mutex_trylock(&adapter->bus_lock); 656 } 657 658 /** 659 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment 660 * @adapter: Target I2C bus segment 661 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT 662 * unlocks only this branch in the adapter tree 663 */ 664 static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter, 665 unsigned int flags) 666 { 667 rt_mutex_unlock(&adapter->bus_lock); 668 } 669 670 static void i2c_dev_set_name(struct i2c_adapter *adap, 671 struct i2c_client *client, 672 struct i2c_board_info const *info) 673 { 674 struct acpi_device *adev = ACPI_COMPANION(&client->dev); 675 676 if (info && info->dev_name) { 677 dev_set_name(&client->dev, "i2c-%s", info->dev_name); 678 return; 679 } 680 681 if (adev) { 682 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev)); 683 return; 684 } 685 686 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), 687 i2c_encode_flags_to_addr(client)); 688 } 689 690 static int i2c_dev_irq_from_resources(const struct resource *resources, 691 unsigned int num_resources) 692 { 693 struct irq_data *irqd; 694 int i; 695 696 for (i = 0; i < num_resources; i++) { 697 const struct resource *r = &resources[i]; 698 699 if (resource_type(r) != IORESOURCE_IRQ) 700 continue; 701 702 if (r->flags & IORESOURCE_BITS) { 703 irqd = irq_get_irq_data(r->start); 704 if (!irqd) 705 break; 706 707 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); 708 } 709 710 return r->start; 711 } 712 713 return 0; 714 } 715 716 /** 717 * i2c_new_device - instantiate an i2c device 718 * @adap: the adapter managing the device 719 * @info: describes one I2C device; bus_num is ignored 720 * Context: can sleep 721 * 722 * Create an i2c device. Binding is handled through driver model 723 * probe()/remove() methods. A driver may be bound to this device when we 724 * return from this function, or any later moment (e.g. maybe hotplugging will 725 * load the driver module). This call is not appropriate for use by mainboard 726 * initialization logic, which usually runs during an arch_initcall() long 727 * before any i2c_adapter could exist. 728 * 729 * This returns the new i2c client, which may be saved for later use with 730 * i2c_unregister_device(); or NULL to indicate an error. 731 */ 732 struct i2c_client * 733 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) 734 { 735 struct i2c_client *client; 736 int status; 737 738 client = kzalloc(sizeof *client, GFP_KERNEL); 739 if (!client) 740 return NULL; 741 742 client->adapter = adap; 743 744 client->dev.platform_data = info->platform_data; 745 client->flags = info->flags; 746 client->addr = info->addr; 747 748 client->init_irq = info->irq; 749 if (!client->init_irq) 750 client->init_irq = i2c_dev_irq_from_resources(info->resources, 751 info->num_resources); 752 client->irq = client->init_irq; 753 754 strlcpy(client->name, info->type, sizeof(client->name)); 755 756 status = i2c_check_addr_validity(client->addr, client->flags); 757 if (status) { 758 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", 759 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); 760 goto out_err_silent; 761 } 762 763 /* Check for address business */ 764 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client)); 765 if (status) 766 goto out_err; 767 768 client->dev.parent = &client->adapter->dev; 769 client->dev.bus = &i2c_bus_type; 770 client->dev.type = &i2c_client_type; 771 client->dev.of_node = of_node_get(info->of_node); 772 client->dev.fwnode = info->fwnode; 773 774 i2c_dev_set_name(adap, client, info); 775 776 if (info->properties) { 777 status = device_add_properties(&client->dev, info->properties); 778 if (status) { 779 dev_err(&adap->dev, 780 "Failed to add properties to client %s: %d\n", 781 client->name, status); 782 goto out_err_put_of_node; 783 } 784 } 785 786 status = device_register(&client->dev); 787 if (status) 788 goto out_free_props; 789 790 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", 791 client->name, dev_name(&client->dev)); 792 793 return client; 794 795 out_free_props: 796 if (info->properties) 797 device_remove_properties(&client->dev); 798 out_err_put_of_node: 799 of_node_put(info->of_node); 800 out_err: 801 dev_err(&adap->dev, 802 "Failed to register i2c client %s at 0x%02x (%d)\n", 803 client->name, client->addr, status); 804 out_err_silent: 805 kfree(client); 806 return NULL; 807 } 808 EXPORT_SYMBOL_GPL(i2c_new_device); 809 810 811 /** 812 * i2c_unregister_device - reverse effect of i2c_new_device() 813 * @client: value returned from i2c_new_device() 814 * Context: can sleep 815 */ 816 void i2c_unregister_device(struct i2c_client *client) 817 { 818 if (!client) 819 return; 820 821 if (client->dev.of_node) { 822 of_node_clear_flag(client->dev.of_node, OF_POPULATED); 823 of_node_put(client->dev.of_node); 824 } 825 826 if (ACPI_COMPANION(&client->dev)) 827 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev)); 828 device_unregister(&client->dev); 829 } 830 EXPORT_SYMBOL_GPL(i2c_unregister_device); 831 832 833 static const struct i2c_device_id dummy_id[] = { 834 { "dummy", 0 }, 835 { }, 836 }; 837 838 static int dummy_probe(struct i2c_client *client, 839 const struct i2c_device_id *id) 840 { 841 return 0; 842 } 843 844 static int dummy_remove(struct i2c_client *client) 845 { 846 return 0; 847 } 848 849 static struct i2c_driver dummy_driver = { 850 .driver.name = "dummy", 851 .probe = dummy_probe, 852 .remove = dummy_remove, 853 .id_table = dummy_id, 854 }; 855 856 /** 857 * i2c_new_dummy - return a new i2c device bound to a dummy driver 858 * @adapter: the adapter managing the device 859 * @address: seven bit address to be used 860 * Context: can sleep 861 * 862 * This returns an I2C client bound to the "dummy" driver, intended for use 863 * with devices that consume multiple addresses. Examples of such chips 864 * include various EEPROMS (like 24c04 and 24c08 models). 865 * 866 * These dummy devices have two main uses. First, most I2C and SMBus calls 867 * except i2c_transfer() need a client handle; the dummy will be that handle. 868 * And second, this prevents the specified address from being bound to a 869 * different driver. 870 * 871 * This returns the new i2c client, which should be saved for later use with 872 * i2c_unregister_device(); or NULL to indicate an error. 873 */ 874 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) 875 { 876 struct i2c_board_info info = { 877 I2C_BOARD_INFO("dummy", address), 878 }; 879 880 return i2c_new_device(adapter, &info); 881 } 882 EXPORT_SYMBOL_GPL(i2c_new_dummy); 883 884 /** 885 * i2c_new_secondary_device - Helper to get the instantiated secondary address 886 * and create the associated device 887 * @client: Handle to the primary client 888 * @name: Handle to specify which secondary address to get 889 * @default_addr: Used as a fallback if no secondary address was specified 890 * Context: can sleep 891 * 892 * I2C clients can be composed of multiple I2C slaves bound together in a single 893 * component. The I2C client driver then binds to the master I2C slave and needs 894 * to create I2C dummy clients to communicate with all the other slaves. 895 * 896 * This function creates and returns an I2C dummy client whose I2C address is 897 * retrieved from the platform firmware based on the given slave name. If no 898 * address is specified by the firmware default_addr is used. 899 * 900 * On DT-based platforms the address is retrieved from the "reg" property entry 901 * cell whose "reg-names" value matches the slave name. 902 * 903 * This returns the new i2c client, which should be saved for later use with 904 * i2c_unregister_device(); or NULL to indicate an error. 905 */ 906 struct i2c_client *i2c_new_secondary_device(struct i2c_client *client, 907 const char *name, 908 u16 default_addr) 909 { 910 struct device_node *np = client->dev.of_node; 911 u32 addr = default_addr; 912 int i; 913 914 if (np) { 915 i = of_property_match_string(np, "reg-names", name); 916 if (i >= 0) 917 of_property_read_u32_index(np, "reg", i, &addr); 918 } 919 920 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr); 921 return i2c_new_dummy(client->adapter, addr); 922 } 923 EXPORT_SYMBOL_GPL(i2c_new_secondary_device); 924 925 /* ------------------------------------------------------------------------- */ 926 927 /* I2C bus adapters -- one roots each I2C or SMBUS segment */ 928 929 static void i2c_adapter_dev_release(struct device *dev) 930 { 931 struct i2c_adapter *adap = to_i2c_adapter(dev); 932 complete(&adap->dev_released); 933 } 934 935 unsigned int i2c_adapter_depth(struct i2c_adapter *adapter) 936 { 937 unsigned int depth = 0; 938 939 while ((adapter = i2c_parent_is_i2c_adapter(adapter))) 940 depth++; 941 942 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES, 943 "adapter depth exceeds lockdep subclass limit\n"); 944 945 return depth; 946 } 947 EXPORT_SYMBOL_GPL(i2c_adapter_depth); 948 949 /* 950 * Let users instantiate I2C devices through sysfs. This can be used when 951 * platform initialization code doesn't contain the proper data for 952 * whatever reason. Also useful for drivers that do device detection and 953 * detection fails, either because the device uses an unexpected address, 954 * or this is a compatible device with different ID register values. 955 * 956 * Parameter checking may look overzealous, but we really don't want 957 * the user to provide incorrect parameters. 958 */ 959 static ssize_t 960 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, 961 const char *buf, size_t count) 962 { 963 struct i2c_adapter *adap = to_i2c_adapter(dev); 964 struct i2c_board_info info; 965 struct i2c_client *client; 966 char *blank, end; 967 int res; 968 969 memset(&info, 0, sizeof(struct i2c_board_info)); 970 971 blank = strchr(buf, ' '); 972 if (!blank) { 973 dev_err(dev, "%s: Missing parameters\n", "new_device"); 974 return -EINVAL; 975 } 976 if (blank - buf > I2C_NAME_SIZE - 1) { 977 dev_err(dev, "%s: Invalid device name\n", "new_device"); 978 return -EINVAL; 979 } 980 memcpy(info.type, buf, blank - buf); 981 982 /* Parse remaining parameters, reject extra parameters */ 983 res = sscanf(++blank, "%hi%c", &info.addr, &end); 984 if (res < 1) { 985 dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); 986 return -EINVAL; 987 } 988 if (res > 1 && end != '\n') { 989 dev_err(dev, "%s: Extra parameters\n", "new_device"); 990 return -EINVAL; 991 } 992 993 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) { 994 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT; 995 info.flags |= I2C_CLIENT_TEN; 996 } 997 998 if (info.addr & I2C_ADDR_OFFSET_SLAVE) { 999 info.addr &= ~I2C_ADDR_OFFSET_SLAVE; 1000 info.flags |= I2C_CLIENT_SLAVE; 1001 } 1002 1003 client = i2c_new_device(adap, &info); 1004 if (!client) 1005 return -EINVAL; 1006 1007 /* Keep track of the added device */ 1008 mutex_lock(&adap->userspace_clients_lock); 1009 list_add_tail(&client->detected, &adap->userspace_clients); 1010 mutex_unlock(&adap->userspace_clients_lock); 1011 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", 1012 info.type, info.addr); 1013 1014 return count; 1015 } 1016 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device); 1017 1018 /* 1019 * And of course let the users delete the devices they instantiated, if 1020 * they got it wrong. This interface can only be used to delete devices 1021 * instantiated by i2c_sysfs_new_device above. This guarantees that we 1022 * don't delete devices to which some kernel code still has references. 1023 * 1024 * Parameter checking may look overzealous, but we really don't want 1025 * the user to delete the wrong device. 1026 */ 1027 static ssize_t 1028 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, 1029 const char *buf, size_t count) 1030 { 1031 struct i2c_adapter *adap = to_i2c_adapter(dev); 1032 struct i2c_client *client, *next; 1033 unsigned short addr; 1034 char end; 1035 int res; 1036 1037 /* Parse parameters, reject extra parameters */ 1038 res = sscanf(buf, "%hi%c", &addr, &end); 1039 if (res < 1) { 1040 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); 1041 return -EINVAL; 1042 } 1043 if (res > 1 && end != '\n') { 1044 dev_err(dev, "%s: Extra parameters\n", "delete_device"); 1045 return -EINVAL; 1046 } 1047 1048 /* Make sure the device was added through sysfs */ 1049 res = -ENOENT; 1050 mutex_lock_nested(&adap->userspace_clients_lock, 1051 i2c_adapter_depth(adap)); 1052 list_for_each_entry_safe(client, next, &adap->userspace_clients, 1053 detected) { 1054 if (i2c_encode_flags_to_addr(client) == addr) { 1055 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", 1056 "delete_device", client->name, client->addr); 1057 1058 list_del(&client->detected); 1059 i2c_unregister_device(client); 1060 res = count; 1061 break; 1062 } 1063 } 1064 mutex_unlock(&adap->userspace_clients_lock); 1065 1066 if (res < 0) 1067 dev_err(dev, "%s: Can't find device in list\n", 1068 "delete_device"); 1069 return res; 1070 } 1071 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL, 1072 i2c_sysfs_delete_device); 1073 1074 static struct attribute *i2c_adapter_attrs[] = { 1075 &dev_attr_name.attr, 1076 &dev_attr_new_device.attr, 1077 &dev_attr_delete_device.attr, 1078 NULL 1079 }; 1080 ATTRIBUTE_GROUPS(i2c_adapter); 1081 1082 struct device_type i2c_adapter_type = { 1083 .groups = i2c_adapter_groups, 1084 .release = i2c_adapter_dev_release, 1085 }; 1086 EXPORT_SYMBOL_GPL(i2c_adapter_type); 1087 1088 /** 1089 * i2c_verify_adapter - return parameter as i2c_adapter or NULL 1090 * @dev: device, probably from some driver model iterator 1091 * 1092 * When traversing the driver model tree, perhaps using driver model 1093 * iterators like @device_for_each_child(), you can't assume very much 1094 * about the nodes you find. Use this function to avoid oopses caused 1095 * by wrongly treating some non-I2C device as an i2c_adapter. 1096 */ 1097 struct i2c_adapter *i2c_verify_adapter(struct device *dev) 1098 { 1099 return (dev->type == &i2c_adapter_type) 1100 ? to_i2c_adapter(dev) 1101 : NULL; 1102 } 1103 EXPORT_SYMBOL(i2c_verify_adapter); 1104 1105 #ifdef CONFIG_I2C_COMPAT 1106 static struct class_compat *i2c_adapter_compat_class; 1107 #endif 1108 1109 static void i2c_scan_static_board_info(struct i2c_adapter *adapter) 1110 { 1111 struct i2c_devinfo *devinfo; 1112 1113 down_read(&__i2c_board_lock); 1114 list_for_each_entry(devinfo, &__i2c_board_list, list) { 1115 if (devinfo->busnum == adapter->nr 1116 && !i2c_new_device(adapter, 1117 &devinfo->board_info)) 1118 dev_err(&adapter->dev, 1119 "Can't create device at 0x%02x\n", 1120 devinfo->board_info.addr); 1121 } 1122 up_read(&__i2c_board_lock); 1123 } 1124 1125 static int i2c_do_add_adapter(struct i2c_driver *driver, 1126 struct i2c_adapter *adap) 1127 { 1128 /* Detect supported devices on that bus, and instantiate them */ 1129 i2c_detect(adap, driver); 1130 1131 return 0; 1132 } 1133 1134 static int __process_new_adapter(struct device_driver *d, void *data) 1135 { 1136 return i2c_do_add_adapter(to_i2c_driver(d), data); 1137 } 1138 1139 static const struct i2c_lock_operations i2c_adapter_lock_ops = { 1140 .lock_bus = i2c_adapter_lock_bus, 1141 .trylock_bus = i2c_adapter_trylock_bus, 1142 .unlock_bus = i2c_adapter_unlock_bus, 1143 }; 1144 1145 static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap) 1146 { 1147 struct irq_domain *domain = adap->host_notify_domain; 1148 irq_hw_number_t hwirq; 1149 1150 if (!domain) 1151 return; 1152 1153 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++) 1154 irq_dispose_mapping(irq_find_mapping(domain, hwirq)); 1155 1156 irq_domain_remove(domain); 1157 adap->host_notify_domain = NULL; 1158 } 1159 1160 static int i2c_host_notify_irq_map(struct irq_domain *h, 1161 unsigned int virq, 1162 irq_hw_number_t hw_irq_num) 1163 { 1164 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq); 1165 1166 return 0; 1167 } 1168 1169 static const struct irq_domain_ops i2c_host_notify_irq_ops = { 1170 .map = i2c_host_notify_irq_map, 1171 }; 1172 1173 static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap) 1174 { 1175 struct irq_domain *domain; 1176 1177 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY)) 1178 return 0; 1179 1180 domain = irq_domain_create_linear(adap->dev.fwnode, 1181 I2C_ADDR_7BITS_COUNT, 1182 &i2c_host_notify_irq_ops, adap); 1183 if (!domain) 1184 return -ENOMEM; 1185 1186 adap->host_notify_domain = domain; 1187 1188 return 0; 1189 } 1190 1191 /** 1192 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct 1193 * I2C client. 1194 * @adap: the adapter 1195 * @addr: the I2C address of the notifying device 1196 * Context: can't sleep 1197 * 1198 * Helper function to be called from an I2C bus driver's interrupt 1199 * handler. It will schedule the Host Notify IRQ. 1200 */ 1201 int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr) 1202 { 1203 int irq; 1204 1205 if (!adap) 1206 return -EINVAL; 1207 1208 irq = irq_find_mapping(adap->host_notify_domain, addr); 1209 if (irq <= 0) 1210 return -ENXIO; 1211 1212 generic_handle_irq(irq); 1213 1214 return 0; 1215 } 1216 EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify); 1217 1218 static int i2c_register_adapter(struct i2c_adapter *adap) 1219 { 1220 int res = -EINVAL; 1221 1222 /* Can't register until after driver model init */ 1223 if (WARN_ON(!is_registered)) { 1224 res = -EAGAIN; 1225 goto out_list; 1226 } 1227 1228 /* Sanity checks */ 1229 if (WARN(!adap->name[0], "i2c adapter has no name")) 1230 goto out_list; 1231 1232 if (!adap->algo) { 1233 pr_err("adapter '%s': no algo supplied!\n", adap->name); 1234 goto out_list; 1235 } 1236 1237 if (!adap->lock_ops) 1238 adap->lock_ops = &i2c_adapter_lock_ops; 1239 1240 adap->locked_flags = 0; 1241 rt_mutex_init(&adap->bus_lock); 1242 rt_mutex_init(&adap->mux_lock); 1243 mutex_init(&adap->userspace_clients_lock); 1244 INIT_LIST_HEAD(&adap->userspace_clients); 1245 1246 /* Set default timeout to 1 second if not already set */ 1247 if (adap->timeout == 0) 1248 adap->timeout = HZ; 1249 1250 /* register soft irqs for Host Notify */ 1251 res = i2c_setup_host_notify_irq_domain(adap); 1252 if (res) { 1253 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n", 1254 adap->name, res); 1255 goto out_list; 1256 } 1257 1258 dev_set_name(&adap->dev, "i2c-%d", adap->nr); 1259 adap->dev.bus = &i2c_bus_type; 1260 adap->dev.type = &i2c_adapter_type; 1261 res = device_register(&adap->dev); 1262 if (res) { 1263 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res); 1264 goto out_list; 1265 } 1266 1267 res = of_i2c_setup_smbus_alert(adap); 1268 if (res) 1269 goto out_reg; 1270 1271 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); 1272 1273 pm_runtime_no_callbacks(&adap->dev); 1274 pm_suspend_ignore_children(&adap->dev, true); 1275 pm_runtime_enable(&adap->dev); 1276 1277 #ifdef CONFIG_I2C_COMPAT 1278 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, 1279 adap->dev.parent); 1280 if (res) 1281 dev_warn(&adap->dev, 1282 "Failed to create compatibility class link\n"); 1283 #endif 1284 1285 i2c_init_recovery(adap); 1286 1287 /* create pre-declared device nodes */ 1288 of_i2c_register_devices(adap); 1289 i2c_acpi_register_devices(adap); 1290 i2c_acpi_install_space_handler(adap); 1291 1292 if (adap->nr < __i2c_first_dynamic_bus_num) 1293 i2c_scan_static_board_info(adap); 1294 1295 /* Notify drivers */ 1296 mutex_lock(&core_lock); 1297 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); 1298 mutex_unlock(&core_lock); 1299 1300 return 0; 1301 1302 out_reg: 1303 init_completion(&adap->dev_released); 1304 device_unregister(&adap->dev); 1305 wait_for_completion(&adap->dev_released); 1306 out_list: 1307 mutex_lock(&core_lock); 1308 idr_remove(&i2c_adapter_idr, adap->nr); 1309 mutex_unlock(&core_lock); 1310 return res; 1311 } 1312 1313 /** 1314 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1 1315 * @adap: the adapter to register (with adap->nr initialized) 1316 * Context: can sleep 1317 * 1318 * See i2c_add_numbered_adapter() for details. 1319 */ 1320 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap) 1321 { 1322 int id; 1323 1324 mutex_lock(&core_lock); 1325 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL); 1326 mutex_unlock(&core_lock); 1327 if (WARN(id < 0, "couldn't get idr")) 1328 return id == -ENOSPC ? -EBUSY : id; 1329 1330 return i2c_register_adapter(adap); 1331 } 1332 1333 /** 1334 * i2c_add_adapter - declare i2c adapter, use dynamic bus number 1335 * @adapter: the adapter to add 1336 * Context: can sleep 1337 * 1338 * This routine is used to declare an I2C adapter when its bus number 1339 * doesn't matter or when its bus number is specified by an dt alias. 1340 * Examples of bases when the bus number doesn't matter: I2C adapters 1341 * dynamically added by USB links or PCI plugin cards. 1342 * 1343 * When this returns zero, a new bus number was allocated and stored 1344 * in adap->nr, and the specified adapter became available for clients. 1345 * Otherwise, a negative errno value is returned. 1346 */ 1347 int i2c_add_adapter(struct i2c_adapter *adapter) 1348 { 1349 struct device *dev = &adapter->dev; 1350 int id; 1351 1352 if (dev->of_node) { 1353 id = of_alias_get_id(dev->of_node, "i2c"); 1354 if (id >= 0) { 1355 adapter->nr = id; 1356 return __i2c_add_numbered_adapter(adapter); 1357 } 1358 } 1359 1360 mutex_lock(&core_lock); 1361 id = idr_alloc(&i2c_adapter_idr, adapter, 1362 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); 1363 mutex_unlock(&core_lock); 1364 if (WARN(id < 0, "couldn't get idr")) 1365 return id; 1366 1367 adapter->nr = id; 1368 1369 return i2c_register_adapter(adapter); 1370 } 1371 EXPORT_SYMBOL(i2c_add_adapter); 1372 1373 /** 1374 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number 1375 * @adap: the adapter to register (with adap->nr initialized) 1376 * Context: can sleep 1377 * 1378 * This routine is used to declare an I2C adapter when its bus number 1379 * matters. For example, use it for I2C adapters from system-on-chip CPUs, 1380 * or otherwise built in to the system's mainboard, and where i2c_board_info 1381 * is used to properly configure I2C devices. 1382 * 1383 * If the requested bus number is set to -1, then this function will behave 1384 * identically to i2c_add_adapter, and will dynamically assign a bus number. 1385 * 1386 * If no devices have pre-been declared for this bus, then be sure to 1387 * register the adapter before any dynamically allocated ones. Otherwise 1388 * the required bus ID may not be available. 1389 * 1390 * When this returns zero, the specified adapter became available for 1391 * clients using the bus number provided in adap->nr. Also, the table 1392 * of I2C devices pre-declared using i2c_register_board_info() is scanned, 1393 * and the appropriate driver model device nodes are created. Otherwise, a 1394 * negative errno value is returned. 1395 */ 1396 int i2c_add_numbered_adapter(struct i2c_adapter *adap) 1397 { 1398 if (adap->nr == -1) /* -1 means dynamically assign bus id */ 1399 return i2c_add_adapter(adap); 1400 1401 return __i2c_add_numbered_adapter(adap); 1402 } 1403 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); 1404 1405 static void i2c_do_del_adapter(struct i2c_driver *driver, 1406 struct i2c_adapter *adapter) 1407 { 1408 struct i2c_client *client, *_n; 1409 1410 /* Remove the devices we created ourselves as the result of hardware 1411 * probing (using a driver's detect method) */ 1412 list_for_each_entry_safe(client, _n, &driver->clients, detected) { 1413 if (client->adapter == adapter) { 1414 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", 1415 client->name, client->addr); 1416 list_del(&client->detected); 1417 i2c_unregister_device(client); 1418 } 1419 } 1420 } 1421 1422 static int __unregister_client(struct device *dev, void *dummy) 1423 { 1424 struct i2c_client *client = i2c_verify_client(dev); 1425 if (client && strcmp(client->name, "dummy")) 1426 i2c_unregister_device(client); 1427 return 0; 1428 } 1429 1430 static int __unregister_dummy(struct device *dev, void *dummy) 1431 { 1432 struct i2c_client *client = i2c_verify_client(dev); 1433 i2c_unregister_device(client); 1434 return 0; 1435 } 1436 1437 static int __process_removed_adapter(struct device_driver *d, void *data) 1438 { 1439 i2c_do_del_adapter(to_i2c_driver(d), data); 1440 return 0; 1441 } 1442 1443 /** 1444 * i2c_del_adapter - unregister I2C adapter 1445 * @adap: the adapter being unregistered 1446 * Context: can sleep 1447 * 1448 * This unregisters an I2C adapter which was previously registered 1449 * by @i2c_add_adapter or @i2c_add_numbered_adapter. 1450 */ 1451 void i2c_del_adapter(struct i2c_adapter *adap) 1452 { 1453 struct i2c_adapter *found; 1454 struct i2c_client *client, *next; 1455 1456 /* First make sure that this adapter was ever added */ 1457 mutex_lock(&core_lock); 1458 found = idr_find(&i2c_adapter_idr, adap->nr); 1459 mutex_unlock(&core_lock); 1460 if (found != adap) { 1461 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name); 1462 return; 1463 } 1464 1465 i2c_acpi_remove_space_handler(adap); 1466 /* Tell drivers about this removal */ 1467 mutex_lock(&core_lock); 1468 bus_for_each_drv(&i2c_bus_type, NULL, adap, 1469 __process_removed_adapter); 1470 mutex_unlock(&core_lock); 1471 1472 /* Remove devices instantiated from sysfs */ 1473 mutex_lock_nested(&adap->userspace_clients_lock, 1474 i2c_adapter_depth(adap)); 1475 list_for_each_entry_safe(client, next, &adap->userspace_clients, 1476 detected) { 1477 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, 1478 client->addr); 1479 list_del(&client->detected); 1480 i2c_unregister_device(client); 1481 } 1482 mutex_unlock(&adap->userspace_clients_lock); 1483 1484 /* Detach any active clients. This can't fail, thus we do not 1485 * check the returned value. This is a two-pass process, because 1486 * we can't remove the dummy devices during the first pass: they 1487 * could have been instantiated by real devices wishing to clean 1488 * them up properly, so we give them a chance to do that first. */ 1489 device_for_each_child(&adap->dev, NULL, __unregister_client); 1490 device_for_each_child(&adap->dev, NULL, __unregister_dummy); 1491 1492 #ifdef CONFIG_I2C_COMPAT 1493 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, 1494 adap->dev.parent); 1495 #endif 1496 1497 /* device name is gone after device_unregister */ 1498 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); 1499 1500 pm_runtime_disable(&adap->dev); 1501 1502 i2c_host_notify_irq_teardown(adap); 1503 1504 /* wait until all references to the device are gone 1505 * 1506 * FIXME: This is old code and should ideally be replaced by an 1507 * alternative which results in decoupling the lifetime of the struct 1508 * device from the i2c_adapter, like spi or netdev do. Any solution 1509 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled! 1510 */ 1511 init_completion(&adap->dev_released); 1512 device_unregister(&adap->dev); 1513 wait_for_completion(&adap->dev_released); 1514 1515 /* free bus id */ 1516 mutex_lock(&core_lock); 1517 idr_remove(&i2c_adapter_idr, adap->nr); 1518 mutex_unlock(&core_lock); 1519 1520 /* Clear the device structure in case this adapter is ever going to be 1521 added again */ 1522 memset(&adap->dev, 0, sizeof(adap->dev)); 1523 } 1524 EXPORT_SYMBOL(i2c_del_adapter); 1525 1526 /** 1527 * i2c_parse_fw_timings - get I2C related timing parameters from firmware 1528 * @dev: The device to scan for I2C timing properties 1529 * @t: the i2c_timings struct to be filled with values 1530 * @use_defaults: bool to use sane defaults derived from the I2C specification 1531 * when properties are not found, otherwise use 0 1532 * 1533 * Scan the device for the generic I2C properties describing timing parameters 1534 * for the signal and fill the given struct with the results. If a property was 1535 * not found and use_defaults was true, then maximum timings are assumed which 1536 * are derived from the I2C specification. If use_defaults is not used, the 1537 * results will be 0, so drivers can apply their own defaults later. The latter 1538 * is mainly intended for avoiding regressions of existing drivers which want 1539 * to switch to this function. New drivers almost always should use the defaults. 1540 */ 1541 1542 void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults) 1543 { 1544 int ret; 1545 1546 memset(t, 0, sizeof(*t)); 1547 1548 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz); 1549 if (ret && use_defaults) 1550 t->bus_freq_hz = 100000; 1551 1552 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns); 1553 if (ret && use_defaults) { 1554 if (t->bus_freq_hz <= 100000) 1555 t->scl_rise_ns = 1000; 1556 else if (t->bus_freq_hz <= 400000) 1557 t->scl_rise_ns = 300; 1558 else 1559 t->scl_rise_ns = 120; 1560 } 1561 1562 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns); 1563 if (ret && use_defaults) { 1564 if (t->bus_freq_hz <= 400000) 1565 t->scl_fall_ns = 300; 1566 else 1567 t->scl_fall_ns = 120; 1568 } 1569 1570 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns); 1571 1572 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns); 1573 if (ret && use_defaults) 1574 t->sda_fall_ns = t->scl_fall_ns; 1575 1576 device_property_read_u32(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns); 1577 } 1578 EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); 1579 1580 /* ------------------------------------------------------------------------- */ 1581 1582 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *)) 1583 { 1584 int res; 1585 1586 mutex_lock(&core_lock); 1587 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn); 1588 mutex_unlock(&core_lock); 1589 1590 return res; 1591 } 1592 EXPORT_SYMBOL_GPL(i2c_for_each_dev); 1593 1594 static int __process_new_driver(struct device *dev, void *data) 1595 { 1596 if (dev->type != &i2c_adapter_type) 1597 return 0; 1598 return i2c_do_add_adapter(data, to_i2c_adapter(dev)); 1599 } 1600 1601 /* 1602 * An i2c_driver is used with one or more i2c_client (device) nodes to access 1603 * i2c slave chips, on a bus instance associated with some i2c_adapter. 1604 */ 1605 1606 int i2c_register_driver(struct module *owner, struct i2c_driver *driver) 1607 { 1608 int res; 1609 1610 /* Can't register until after driver model init */ 1611 if (WARN_ON(!is_registered)) 1612 return -EAGAIN; 1613 1614 /* add the driver to the list of i2c drivers in the driver core */ 1615 driver->driver.owner = owner; 1616 driver->driver.bus = &i2c_bus_type; 1617 INIT_LIST_HEAD(&driver->clients); 1618 1619 /* When registration returns, the driver core 1620 * will have called probe() for all matching-but-unbound devices. 1621 */ 1622 res = driver_register(&driver->driver); 1623 if (res) 1624 return res; 1625 1626 pr_debug("driver [%s] registered\n", driver->driver.name); 1627 1628 /* Walk the adapters that are already present */ 1629 i2c_for_each_dev(driver, __process_new_driver); 1630 1631 return 0; 1632 } 1633 EXPORT_SYMBOL(i2c_register_driver); 1634 1635 static int __process_removed_driver(struct device *dev, void *data) 1636 { 1637 if (dev->type == &i2c_adapter_type) 1638 i2c_do_del_adapter(data, to_i2c_adapter(dev)); 1639 return 0; 1640 } 1641 1642 /** 1643 * i2c_del_driver - unregister I2C driver 1644 * @driver: the driver being unregistered 1645 * Context: can sleep 1646 */ 1647 void i2c_del_driver(struct i2c_driver *driver) 1648 { 1649 i2c_for_each_dev(driver, __process_removed_driver); 1650 1651 driver_unregister(&driver->driver); 1652 pr_debug("driver [%s] unregistered\n", driver->driver.name); 1653 } 1654 EXPORT_SYMBOL(i2c_del_driver); 1655 1656 /* ------------------------------------------------------------------------- */ 1657 1658 /** 1659 * i2c_use_client - increments the reference count of the i2c client structure 1660 * @client: the client being referenced 1661 * 1662 * Each live reference to a client should be refcounted. The driver model does 1663 * that automatically as part of driver binding, so that most drivers don't 1664 * need to do this explicitly: they hold a reference until they're unbound 1665 * from the device. 1666 * 1667 * A pointer to the client with the incremented reference counter is returned. 1668 */ 1669 struct i2c_client *i2c_use_client(struct i2c_client *client) 1670 { 1671 if (client && get_device(&client->dev)) 1672 return client; 1673 return NULL; 1674 } 1675 EXPORT_SYMBOL(i2c_use_client); 1676 1677 /** 1678 * i2c_release_client - release a use of the i2c client structure 1679 * @client: the client being no longer referenced 1680 * 1681 * Must be called when a user of a client is finished with it. 1682 */ 1683 void i2c_release_client(struct i2c_client *client) 1684 { 1685 if (client) 1686 put_device(&client->dev); 1687 } 1688 EXPORT_SYMBOL(i2c_release_client); 1689 1690 struct i2c_cmd_arg { 1691 unsigned cmd; 1692 void *arg; 1693 }; 1694 1695 static int i2c_cmd(struct device *dev, void *_arg) 1696 { 1697 struct i2c_client *client = i2c_verify_client(dev); 1698 struct i2c_cmd_arg *arg = _arg; 1699 struct i2c_driver *driver; 1700 1701 if (!client || !client->dev.driver) 1702 return 0; 1703 1704 driver = to_i2c_driver(client->dev.driver); 1705 if (driver->command) 1706 driver->command(client, arg->cmd, arg->arg); 1707 return 0; 1708 } 1709 1710 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) 1711 { 1712 struct i2c_cmd_arg cmd_arg; 1713 1714 cmd_arg.cmd = cmd; 1715 cmd_arg.arg = arg; 1716 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); 1717 } 1718 EXPORT_SYMBOL(i2c_clients_command); 1719 1720 static int __init i2c_init(void) 1721 { 1722 int retval; 1723 1724 retval = of_alias_get_highest_id("i2c"); 1725 1726 down_write(&__i2c_board_lock); 1727 if (retval >= __i2c_first_dynamic_bus_num) 1728 __i2c_first_dynamic_bus_num = retval + 1; 1729 up_write(&__i2c_board_lock); 1730 1731 retval = bus_register(&i2c_bus_type); 1732 if (retval) 1733 return retval; 1734 1735 is_registered = true; 1736 1737 #ifdef CONFIG_I2C_COMPAT 1738 i2c_adapter_compat_class = class_compat_register("i2c-adapter"); 1739 if (!i2c_adapter_compat_class) { 1740 retval = -ENOMEM; 1741 goto bus_err; 1742 } 1743 #endif 1744 retval = i2c_add_driver(&dummy_driver); 1745 if (retval) 1746 goto class_err; 1747 1748 if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 1749 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier)); 1750 if (IS_ENABLED(CONFIG_ACPI)) 1751 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier)); 1752 1753 return 0; 1754 1755 class_err: 1756 #ifdef CONFIG_I2C_COMPAT 1757 class_compat_unregister(i2c_adapter_compat_class); 1758 bus_err: 1759 #endif 1760 is_registered = false; 1761 bus_unregister(&i2c_bus_type); 1762 return retval; 1763 } 1764 1765 static void __exit i2c_exit(void) 1766 { 1767 if (IS_ENABLED(CONFIG_ACPI)) 1768 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier)); 1769 if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 1770 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier)); 1771 i2c_del_driver(&dummy_driver); 1772 #ifdef CONFIG_I2C_COMPAT 1773 class_compat_unregister(i2c_adapter_compat_class); 1774 #endif 1775 bus_unregister(&i2c_bus_type); 1776 tracepoint_synchronize_unregister(); 1777 } 1778 1779 /* We must initialize early, because some subsystems register i2c drivers 1780 * in subsys_initcall() code, but are linked (and initialized) before i2c. 1781 */ 1782 postcore_initcall(i2c_init); 1783 module_exit(i2c_exit); 1784 1785 /* ---------------------------------------------------- 1786 * the functional interface to the i2c busses. 1787 * ---------------------------------------------------- 1788 */ 1789 1790 /* Check if val is exceeding the quirk IFF quirk is non 0 */ 1791 #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk))) 1792 1793 static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg) 1794 { 1795 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n", 1796 err_msg, msg->addr, msg->len, 1797 msg->flags & I2C_M_RD ? "read" : "write"); 1798 return -EOPNOTSUPP; 1799 } 1800 1801 static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 1802 { 1803 const struct i2c_adapter_quirks *q = adap->quirks; 1804 int max_num = q->max_num_msgs, i; 1805 bool do_len_check = true; 1806 1807 if (q->flags & I2C_AQ_COMB) { 1808 max_num = 2; 1809 1810 /* special checks for combined messages */ 1811 if (num == 2) { 1812 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD) 1813 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write"); 1814 1815 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD)) 1816 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read"); 1817 1818 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr) 1819 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr"); 1820 1821 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len)) 1822 return i2c_quirk_error(adap, &msgs[0], "msg too long"); 1823 1824 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len)) 1825 return i2c_quirk_error(adap, &msgs[1], "msg too long"); 1826 1827 do_len_check = false; 1828 } 1829 } 1830 1831 if (i2c_quirk_exceeded(num, max_num)) 1832 return i2c_quirk_error(adap, &msgs[0], "too many messages"); 1833 1834 for (i = 0; i < num; i++) { 1835 u16 len = msgs[i].len; 1836 1837 if (msgs[i].flags & I2C_M_RD) { 1838 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len)) 1839 return i2c_quirk_error(adap, &msgs[i], "msg too long"); 1840 1841 if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0) 1842 return i2c_quirk_error(adap, &msgs[i], "no zero length"); 1843 } else { 1844 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len)) 1845 return i2c_quirk_error(adap, &msgs[i], "msg too long"); 1846 1847 if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0) 1848 return i2c_quirk_error(adap, &msgs[i], "no zero length"); 1849 } 1850 } 1851 1852 return 0; 1853 } 1854 1855 /** 1856 * __i2c_transfer - unlocked flavor of i2c_transfer 1857 * @adap: Handle to I2C bus 1858 * @msgs: One or more messages to execute before STOP is issued to 1859 * terminate the operation; each message begins with a START. 1860 * @num: Number of messages to be executed. 1861 * 1862 * Returns negative errno, else the number of messages executed. 1863 * 1864 * Adapter lock must be held when calling this function. No debug logging 1865 * takes place. adap->algo->master_xfer existence isn't checked. 1866 */ 1867 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 1868 { 1869 unsigned long orig_jiffies; 1870 int ret, try; 1871 1872 if (WARN_ON(!msgs || num < 1)) 1873 return -EINVAL; 1874 1875 ret = __i2c_check_suspended(adap); 1876 if (ret) 1877 return ret; 1878 1879 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) 1880 return -EOPNOTSUPP; 1881 1882 /* 1883 * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets 1884 * enabled. This is an efficient way of keeping the for-loop from 1885 * being executed when not needed. 1886 */ 1887 if (static_branch_unlikely(&i2c_trace_msg_key)) { 1888 int i; 1889 for (i = 0; i < num; i++) 1890 if (msgs[i].flags & I2C_M_RD) 1891 trace_i2c_read(adap, &msgs[i], i); 1892 else 1893 trace_i2c_write(adap, &msgs[i], i); 1894 } 1895 1896 /* Retry automatically on arbitration loss */ 1897 orig_jiffies = jiffies; 1898 for (ret = 0, try = 0; try <= adap->retries; try++) { 1899 if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic) 1900 ret = adap->algo->master_xfer_atomic(adap, msgs, num); 1901 else 1902 ret = adap->algo->master_xfer(adap, msgs, num); 1903 1904 if (ret != -EAGAIN) 1905 break; 1906 if (time_after(jiffies, orig_jiffies + adap->timeout)) 1907 break; 1908 } 1909 1910 if (static_branch_unlikely(&i2c_trace_msg_key)) { 1911 int i; 1912 for (i = 0; i < ret; i++) 1913 if (msgs[i].flags & I2C_M_RD) 1914 trace_i2c_reply(adap, &msgs[i], i); 1915 trace_i2c_result(adap, num, ret); 1916 } 1917 1918 return ret; 1919 } 1920 EXPORT_SYMBOL(__i2c_transfer); 1921 1922 /** 1923 * i2c_transfer - execute a single or combined I2C message 1924 * @adap: Handle to I2C bus 1925 * @msgs: One or more messages to execute before STOP is issued to 1926 * terminate the operation; each message begins with a START. 1927 * @num: Number of messages to be executed. 1928 * 1929 * Returns negative errno, else the number of messages executed. 1930 * 1931 * Note that there is no requirement that each message be sent to 1932 * the same slave address, although that is the most common model. 1933 */ 1934 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 1935 { 1936 int ret; 1937 1938 if (!adap->algo->master_xfer) { 1939 dev_dbg(&adap->dev, "I2C level transfers not supported\n"); 1940 return -EOPNOTSUPP; 1941 } 1942 1943 /* REVISIT the fault reporting model here is weak: 1944 * 1945 * - When we get an error after receiving N bytes from a slave, 1946 * there is no way to report "N". 1947 * 1948 * - When we get a NAK after transmitting N bytes to a slave, 1949 * there is no way to report "N" ... or to let the master 1950 * continue executing the rest of this combined message, if 1951 * that's the appropriate response. 1952 * 1953 * - When for example "num" is two and we successfully complete 1954 * the first message but get an error part way through the 1955 * second, it's unclear whether that should be reported as 1956 * one (discarding status on the second message) or errno 1957 * (discarding status on the first one). 1958 */ 1959 ret = __i2c_lock_bus_helper(adap); 1960 if (ret) 1961 return ret; 1962 1963 ret = __i2c_transfer(adap, msgs, num); 1964 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT); 1965 1966 return ret; 1967 } 1968 EXPORT_SYMBOL(i2c_transfer); 1969 1970 /** 1971 * i2c_transfer_buffer_flags - issue a single I2C message transferring data 1972 * to/from a buffer 1973 * @client: Handle to slave device 1974 * @buf: Where the data is stored 1975 * @count: How many bytes to transfer, must be less than 64k since msg.len is u16 1976 * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads 1977 * 1978 * Returns negative errno, or else the number of bytes transferred. 1979 */ 1980 int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf, 1981 int count, u16 flags) 1982 { 1983 int ret; 1984 struct i2c_msg msg = { 1985 .addr = client->addr, 1986 .flags = flags | (client->flags & I2C_M_TEN), 1987 .len = count, 1988 .buf = buf, 1989 }; 1990 1991 ret = i2c_transfer(client->adapter, &msg, 1); 1992 1993 /* 1994 * If everything went ok (i.e. 1 msg transferred), return #bytes 1995 * transferred, else error code. 1996 */ 1997 return (ret == 1) ? count : ret; 1998 } 1999 EXPORT_SYMBOL(i2c_transfer_buffer_flags); 2000 2001 /** 2002 * i2c_get_device_id - get manufacturer, part id and die revision of a device 2003 * @client: The device to query 2004 * @id: The queried information 2005 * 2006 * Returns negative errno on error, zero on success. 2007 */ 2008 int i2c_get_device_id(const struct i2c_client *client, 2009 struct i2c_device_identity *id) 2010 { 2011 struct i2c_adapter *adap = client->adapter; 2012 union i2c_smbus_data raw_id; 2013 int ret; 2014 2015 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 2016 return -EOPNOTSUPP; 2017 2018 raw_id.block[0] = 3; 2019 ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0, 2020 I2C_SMBUS_READ, client->addr << 1, 2021 I2C_SMBUS_I2C_BLOCK_DATA, &raw_id); 2022 if (ret) 2023 return ret; 2024 2025 id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4); 2026 id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3); 2027 id->die_revision = raw_id.block[3] & 0x7; 2028 return 0; 2029 } 2030 EXPORT_SYMBOL_GPL(i2c_get_device_id); 2031 2032 /* ---------------------------------------------------- 2033 * the i2c address scanning function 2034 * Will not work for 10-bit addresses! 2035 * ---------------------------------------------------- 2036 */ 2037 2038 /* 2039 * Legacy default probe function, mostly relevant for SMBus. The default 2040 * probe method is a quick write, but it is known to corrupt the 24RF08 2041 * EEPROMs due to a state machine bug, and could also irreversibly 2042 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f, 2043 * we use a short byte read instead. Also, some bus drivers don't implement 2044 * quick write, so we fallback to a byte read in that case too. 2045 * On x86, there is another special case for FSC hardware monitoring chips, 2046 * which want regular byte reads (address 0x73.) Fortunately, these are the 2047 * only known chips using this I2C address on PC hardware. 2048 * Returns 1 if probe succeeded, 0 if not. 2049 */ 2050 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr) 2051 { 2052 int err; 2053 union i2c_smbus_data dummy; 2054 2055 #ifdef CONFIG_X86 2056 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON) 2057 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA)) 2058 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 2059 I2C_SMBUS_BYTE_DATA, &dummy); 2060 else 2061 #endif 2062 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50) 2063 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) 2064 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, 2065 I2C_SMBUS_QUICK, NULL); 2066 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) 2067 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 2068 I2C_SMBUS_BYTE, &dummy); 2069 else { 2070 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n", 2071 addr); 2072 err = -EOPNOTSUPP; 2073 } 2074 2075 return err >= 0; 2076 } 2077 2078 static int i2c_detect_address(struct i2c_client *temp_client, 2079 struct i2c_driver *driver) 2080 { 2081 struct i2c_board_info info; 2082 struct i2c_adapter *adapter = temp_client->adapter; 2083 int addr = temp_client->addr; 2084 int err; 2085 2086 /* Make sure the address is valid */ 2087 err = i2c_check_7bit_addr_validity_strict(addr); 2088 if (err) { 2089 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", 2090 addr); 2091 return err; 2092 } 2093 2094 /* Skip if already in use (7 bit, no need to encode flags) */ 2095 if (i2c_check_addr_busy(adapter, addr)) 2096 return 0; 2097 2098 /* Make sure there is something at this address */ 2099 if (!i2c_default_probe(adapter, addr)) 2100 return 0; 2101 2102 /* Finally call the custom detection function */ 2103 memset(&info, 0, sizeof(struct i2c_board_info)); 2104 info.addr = addr; 2105 err = driver->detect(temp_client, &info); 2106 if (err) { 2107 /* -ENODEV is returned if the detection fails. We catch it 2108 here as this isn't an error. */ 2109 return err == -ENODEV ? 0 : err; 2110 } 2111 2112 /* Consistency check */ 2113 if (info.type[0] == '\0') { 2114 dev_err(&adapter->dev, 2115 "%s detection function provided no name for 0x%x\n", 2116 driver->driver.name, addr); 2117 } else { 2118 struct i2c_client *client; 2119 2120 /* Detection succeeded, instantiate the device */ 2121 if (adapter->class & I2C_CLASS_DEPRECATED) 2122 dev_warn(&adapter->dev, 2123 "This adapter will soon drop class based instantiation of devices. " 2124 "Please make sure client 0x%02x gets instantiated by other means. " 2125 "Check 'Documentation/i2c/instantiating-devices' for details.\n", 2126 info.addr); 2127 2128 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", 2129 info.type, info.addr); 2130 client = i2c_new_device(adapter, &info); 2131 if (client) 2132 list_add_tail(&client->detected, &driver->clients); 2133 else 2134 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", 2135 info.type, info.addr); 2136 } 2137 return 0; 2138 } 2139 2140 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) 2141 { 2142 const unsigned short *address_list; 2143 struct i2c_client *temp_client; 2144 int i, err = 0; 2145 int adap_id = i2c_adapter_id(adapter); 2146 2147 address_list = driver->address_list; 2148 if (!driver->detect || !address_list) 2149 return 0; 2150 2151 /* Warn that the adapter lost class based instantiation */ 2152 if (adapter->class == I2C_CLASS_DEPRECATED) { 2153 dev_dbg(&adapter->dev, 2154 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. " 2155 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n", 2156 driver->driver.name); 2157 return 0; 2158 } 2159 2160 /* Stop here if the classes do not match */ 2161 if (!(adapter->class & driver->class)) 2162 return 0; 2163 2164 /* Set up a temporary client to help detect callback */ 2165 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); 2166 if (!temp_client) 2167 return -ENOMEM; 2168 temp_client->adapter = adapter; 2169 2170 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { 2171 dev_dbg(&adapter->dev, 2172 "found normal entry for adapter %d, addr 0x%02x\n", 2173 adap_id, address_list[i]); 2174 temp_client->addr = address_list[i]; 2175 err = i2c_detect_address(temp_client, driver); 2176 if (unlikely(err)) 2177 break; 2178 } 2179 2180 kfree(temp_client); 2181 return err; 2182 } 2183 2184 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr) 2185 { 2186 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 2187 I2C_SMBUS_QUICK, NULL) >= 0; 2188 } 2189 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read); 2190 2191 struct i2c_client * 2192 i2c_new_probed_device(struct i2c_adapter *adap, 2193 struct i2c_board_info *info, 2194 unsigned short const *addr_list, 2195 int (*probe)(struct i2c_adapter *, unsigned short addr)) 2196 { 2197 int i; 2198 2199 if (!probe) 2200 probe = i2c_default_probe; 2201 2202 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { 2203 /* Check address validity */ 2204 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) { 2205 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n", 2206 addr_list[i]); 2207 continue; 2208 } 2209 2210 /* Check address availability (7 bit, no need to encode flags) */ 2211 if (i2c_check_addr_busy(adap, addr_list[i])) { 2212 dev_dbg(&adap->dev, 2213 "Address 0x%02x already in use, not probing\n", 2214 addr_list[i]); 2215 continue; 2216 } 2217 2218 /* Test address responsiveness */ 2219 if (probe(adap, addr_list[i])) 2220 break; 2221 } 2222 2223 if (addr_list[i] == I2C_CLIENT_END) { 2224 dev_dbg(&adap->dev, "Probing failed, no device found\n"); 2225 return NULL; 2226 } 2227 2228 info->addr = addr_list[i]; 2229 return i2c_new_device(adap, info); 2230 } 2231 EXPORT_SYMBOL_GPL(i2c_new_probed_device); 2232 2233 struct i2c_adapter *i2c_get_adapter(int nr) 2234 { 2235 struct i2c_adapter *adapter; 2236 2237 mutex_lock(&core_lock); 2238 adapter = idr_find(&i2c_adapter_idr, nr); 2239 if (!adapter) 2240 goto exit; 2241 2242 if (try_module_get(adapter->owner)) 2243 get_device(&adapter->dev); 2244 else 2245 adapter = NULL; 2246 2247 exit: 2248 mutex_unlock(&core_lock); 2249 return adapter; 2250 } 2251 EXPORT_SYMBOL(i2c_get_adapter); 2252 2253 void i2c_put_adapter(struct i2c_adapter *adap) 2254 { 2255 if (!adap) 2256 return; 2257 2258 put_device(&adap->dev); 2259 module_put(adap->owner); 2260 } 2261 EXPORT_SYMBOL(i2c_put_adapter); 2262 2263 /** 2264 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg 2265 * @msg: the message to be checked 2266 * @threshold: the minimum number of bytes for which using DMA makes sense. 2267 * Should at least be 1. 2268 * 2269 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO. 2270 * Or a valid pointer to be used with DMA. After use, release it by 2271 * calling i2c_put_dma_safe_msg_buf(). 2272 * 2273 * This function must only be called from process context! 2274 */ 2275 u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold) 2276 { 2277 /* also skip 0-length msgs for bogus thresholds of 0 */ 2278 if (!threshold) 2279 pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n", 2280 msg->addr); 2281 if (msg->len < threshold || msg->len == 0) 2282 return NULL; 2283 2284 if (msg->flags & I2C_M_DMA_SAFE) 2285 return msg->buf; 2286 2287 pr_debug("using bounce buffer for addr=0x%02x, len=%d\n", 2288 msg->addr, msg->len); 2289 2290 if (msg->flags & I2C_M_RD) 2291 return kzalloc(msg->len, GFP_KERNEL); 2292 else 2293 return kmemdup(msg->buf, msg->len, GFP_KERNEL); 2294 } 2295 EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf); 2296 2297 /** 2298 * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg 2299 * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL. 2300 * @msg: the message which the buffer corresponds to 2301 * @xferred: bool saying if the message was transferred 2302 */ 2303 void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred) 2304 { 2305 if (!buf || buf == msg->buf) 2306 return; 2307 2308 if (xferred && msg->flags & I2C_M_RD) 2309 memcpy(msg->buf, buf, msg->len); 2310 2311 kfree(buf); 2312 } 2313 EXPORT_SYMBOL_GPL(i2c_put_dma_safe_msg_buf); 2314 2315 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); 2316 MODULE_DESCRIPTION("I2C-Bus main module"); 2317 MODULE_LICENSE("GPL"); 2318