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.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 /* 62 * core_lock protects i2c_adapter_idr, and guarantees that device detection, 63 * deletion of detected devices, and attach_adapter calls are serialized 64 */ 65 static DEFINE_MUTEX(core_lock); 66 static DEFINE_IDR(i2c_adapter_idr); 67 68 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); 69 70 static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE; 71 static bool is_registered; 72 73 int i2c_transfer_trace_reg(void) 74 { 75 static_key_slow_inc(&i2c_trace_msg); 76 return 0; 77 } 78 79 void i2c_transfer_trace_unreg(void) 80 { 81 static_key_slow_dec(&i2c_trace_msg); 82 } 83 84 const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 85 const struct i2c_client *client) 86 { 87 if (!(id && client)) 88 return NULL; 89 90 while (id->name[0]) { 91 if (strcmp(client->name, id->name) == 0) 92 return id; 93 id++; 94 } 95 return NULL; 96 } 97 EXPORT_SYMBOL_GPL(i2c_match_id); 98 99 static int i2c_device_match(struct device *dev, struct device_driver *drv) 100 { 101 struct i2c_client *client = i2c_verify_client(dev); 102 struct i2c_driver *driver; 103 104 105 /* Attempt an OF style match */ 106 if (i2c_of_match_device(drv->of_match_table, client)) 107 return 1; 108 109 /* Then ACPI style match */ 110 if (acpi_driver_match_device(dev, drv)) 111 return 1; 112 113 driver = to_i2c_driver(drv); 114 115 /* Finally an I2C match */ 116 if (i2c_match_id(driver->id_table, client)) 117 return 1; 118 119 return 0; 120 } 121 122 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) 123 { 124 struct i2c_client *client = to_i2c_client(dev); 125 int rc; 126 127 rc = acpi_device_uevent_modalias(dev, env); 128 if (rc != -ENODEV) 129 return rc; 130 131 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name); 132 } 133 134 /* i2c bus recovery routines */ 135 static int get_scl_gpio_value(struct i2c_adapter *adap) 136 { 137 return gpio_get_value(adap->bus_recovery_info->scl_gpio); 138 } 139 140 static void set_scl_gpio_value(struct i2c_adapter *adap, int val) 141 { 142 gpio_set_value(adap->bus_recovery_info->scl_gpio, val); 143 } 144 145 static int get_sda_gpio_value(struct i2c_adapter *adap) 146 { 147 return gpio_get_value(adap->bus_recovery_info->sda_gpio); 148 } 149 150 static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap) 151 { 152 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 153 struct device *dev = &adap->dev; 154 int ret = 0; 155 156 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN | 157 GPIOF_OUT_INIT_HIGH, "i2c-scl"); 158 if (ret) { 159 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio); 160 return ret; 161 } 162 163 if (bri->get_sda) { 164 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) { 165 /* work without SDA polling */ 166 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n", 167 bri->sda_gpio); 168 bri->get_sda = NULL; 169 } 170 } 171 172 return ret; 173 } 174 175 static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap) 176 { 177 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 178 179 if (bri->get_sda) 180 gpio_free(bri->sda_gpio); 181 182 gpio_free(bri->scl_gpio); 183 } 184 185 /* 186 * We are generating clock pulses. ndelay() determines durating of clk pulses. 187 * We will generate clock with rate 100 KHz and so duration of both clock levels 188 * is: delay in ns = (10^6 / 100) / 2 189 */ 190 #define RECOVERY_NDELAY 5000 191 #define RECOVERY_CLK_CNT 9 192 193 static int i2c_generic_recovery(struct i2c_adapter *adap) 194 { 195 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 196 int i = 0, val = 1, ret = 0; 197 198 if (bri->prepare_recovery) 199 bri->prepare_recovery(adap); 200 201 bri->set_scl(adap, val); 202 ndelay(RECOVERY_NDELAY); 203 204 /* 205 * By this time SCL is high, as we need to give 9 falling-rising edges 206 */ 207 while (i++ < RECOVERY_CLK_CNT * 2) { 208 if (val) { 209 /* SCL shouldn't be low here */ 210 if (!bri->get_scl(adap)) { 211 dev_err(&adap->dev, 212 "SCL is stuck low, exit recovery\n"); 213 ret = -EBUSY; 214 break; 215 } 216 /* Break if SDA is high */ 217 if (bri->get_sda && bri->get_sda(adap)) 218 break; 219 } 220 221 val = !val; 222 bri->set_scl(adap, val); 223 ndelay(RECOVERY_NDELAY); 224 } 225 226 /* check if recovery actually succeeded */ 227 if (bri->get_sda && !bri->get_sda(adap)) 228 ret = -EBUSY; 229 230 if (bri->unprepare_recovery) 231 bri->unprepare_recovery(adap); 232 233 return ret; 234 } 235 236 int i2c_generic_scl_recovery(struct i2c_adapter *adap) 237 { 238 return i2c_generic_recovery(adap); 239 } 240 EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery); 241 242 int i2c_generic_gpio_recovery(struct i2c_adapter *adap) 243 { 244 int ret; 245 246 ret = i2c_get_gpios_for_recovery(adap); 247 if (ret) 248 return ret; 249 250 ret = i2c_generic_recovery(adap); 251 i2c_put_gpios_for_recovery(adap); 252 253 return ret; 254 } 255 EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery); 256 257 int i2c_recover_bus(struct i2c_adapter *adap) 258 { 259 if (!adap->bus_recovery_info) 260 return -EOPNOTSUPP; 261 262 dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); 263 return adap->bus_recovery_info->recover_bus(adap); 264 } 265 EXPORT_SYMBOL_GPL(i2c_recover_bus); 266 267 static void i2c_init_recovery(struct i2c_adapter *adap) 268 { 269 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 270 char *err_str; 271 272 if (!bri) 273 return; 274 275 if (!bri->recover_bus) { 276 err_str = "no recover_bus() found"; 277 goto err; 278 } 279 280 /* Generic GPIO recovery */ 281 if (bri->recover_bus == i2c_generic_gpio_recovery) { 282 if (!gpio_is_valid(bri->scl_gpio)) { 283 err_str = "invalid SCL gpio"; 284 goto err; 285 } 286 287 if (gpio_is_valid(bri->sda_gpio)) 288 bri->get_sda = get_sda_gpio_value; 289 else 290 bri->get_sda = NULL; 291 292 bri->get_scl = get_scl_gpio_value; 293 bri->set_scl = set_scl_gpio_value; 294 } else if (bri->recover_bus == i2c_generic_scl_recovery) { 295 /* Generic SCL recovery */ 296 if (!bri->set_scl || !bri->get_scl) { 297 err_str = "no {get|set}_scl() found"; 298 goto err; 299 } 300 } 301 302 return; 303 err: 304 dev_err(&adap->dev, "Not using recovery: %s\n", err_str); 305 adap->bus_recovery_info = NULL; 306 } 307 308 static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client) 309 { 310 struct i2c_adapter *adap = client->adapter; 311 unsigned int irq; 312 313 if (!adap->host_notify_domain) 314 return -ENXIO; 315 316 if (client->flags & I2C_CLIENT_TEN) 317 return -EINVAL; 318 319 irq = irq_find_mapping(adap->host_notify_domain, client->addr); 320 if (!irq) 321 irq = irq_create_mapping(adap->host_notify_domain, 322 client->addr); 323 324 return irq > 0 ? irq : -ENXIO; 325 } 326 327 static int i2c_device_probe(struct device *dev) 328 { 329 struct i2c_client *client = i2c_verify_client(dev); 330 struct i2c_driver *driver; 331 int status; 332 333 if (!client) 334 return 0; 335 336 driver = to_i2c_driver(dev->driver); 337 338 if (!client->irq && !driver->disable_i2c_core_irq_mapping) { 339 int irq = -ENOENT; 340 341 if (client->flags & I2C_CLIENT_HOST_NOTIFY) { 342 dev_dbg(dev, "Using Host Notify IRQ\n"); 343 irq = i2c_smbus_host_notify_to_irq(client); 344 } else if (dev->of_node) { 345 irq = of_irq_get_byname(dev->of_node, "irq"); 346 if (irq == -EINVAL || irq == -ENODATA) 347 irq = of_irq_get(dev->of_node, 0); 348 } else if (ACPI_COMPANION(dev)) { 349 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0); 350 } 351 if (irq == -EPROBE_DEFER) 352 return irq; 353 354 if (irq < 0) 355 irq = 0; 356 357 client->irq = irq; 358 } 359 360 /* 361 * An I2C ID table is not mandatory, if and only if, a suitable OF 362 * or ACPI ID table is supplied for the probing device. 363 */ 364 if (!driver->id_table && 365 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) && 366 !i2c_of_match_device(dev->driver->of_match_table, client)) 367 return -ENODEV; 368 369 if (client->flags & I2C_CLIENT_WAKE) { 370 int wakeirq = -ENOENT; 371 372 if (dev->of_node) { 373 wakeirq = of_irq_get_byname(dev->of_node, "wakeup"); 374 if (wakeirq == -EPROBE_DEFER) 375 return wakeirq; 376 } 377 378 device_init_wakeup(&client->dev, true); 379 380 if (wakeirq > 0 && wakeirq != client->irq) 381 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq); 382 else if (client->irq > 0) 383 status = dev_pm_set_wake_irq(dev, client->irq); 384 else 385 status = 0; 386 387 if (status) 388 dev_warn(&client->dev, "failed to set up wakeup irq\n"); 389 } 390 391 dev_dbg(dev, "probe\n"); 392 393 status = of_clk_set_defaults(dev->of_node, false); 394 if (status < 0) 395 goto err_clear_wakeup_irq; 396 397 status = dev_pm_domain_attach(&client->dev, true); 398 if (status == -EPROBE_DEFER) 399 goto err_clear_wakeup_irq; 400 401 /* 402 * When there are no more users of probe(), 403 * rename probe_new to probe. 404 */ 405 if (driver->probe_new) 406 status = driver->probe_new(client); 407 else if (driver->probe) 408 status = driver->probe(client, 409 i2c_match_id(driver->id_table, client)); 410 else 411 status = -EINVAL; 412 413 if (status) 414 goto err_detach_pm_domain; 415 416 return 0; 417 418 err_detach_pm_domain: 419 dev_pm_domain_detach(&client->dev, true); 420 err_clear_wakeup_irq: 421 dev_pm_clear_wake_irq(&client->dev); 422 device_init_wakeup(&client->dev, false); 423 return status; 424 } 425 426 static int i2c_device_remove(struct device *dev) 427 { 428 struct i2c_client *client = i2c_verify_client(dev); 429 struct i2c_driver *driver; 430 int status = 0; 431 432 if (!client || !dev->driver) 433 return 0; 434 435 driver = to_i2c_driver(dev->driver); 436 if (driver->remove) { 437 dev_dbg(dev, "remove\n"); 438 status = driver->remove(client); 439 } 440 441 dev_pm_domain_detach(&client->dev, true); 442 443 dev_pm_clear_wake_irq(&client->dev); 444 device_init_wakeup(&client->dev, false); 445 446 return status; 447 } 448 449 static void i2c_device_shutdown(struct device *dev) 450 { 451 struct i2c_client *client = i2c_verify_client(dev); 452 struct i2c_driver *driver; 453 454 if (!client || !dev->driver) 455 return; 456 driver = to_i2c_driver(dev->driver); 457 if (driver->shutdown) 458 driver->shutdown(client); 459 } 460 461 static void i2c_client_dev_release(struct device *dev) 462 { 463 kfree(to_i2c_client(dev)); 464 } 465 466 static ssize_t 467 show_name(struct device *dev, struct device_attribute *attr, char *buf) 468 { 469 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? 470 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); 471 } 472 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 473 474 static ssize_t 475 show_modalias(struct device *dev, struct device_attribute *attr, char *buf) 476 { 477 struct i2c_client *client = to_i2c_client(dev); 478 int len; 479 480 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); 481 if (len != -ENODEV) 482 return len; 483 484 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); 485 } 486 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); 487 488 static struct attribute *i2c_dev_attrs[] = { 489 &dev_attr_name.attr, 490 /* modalias helps coldplug: modprobe $(cat .../modalias) */ 491 &dev_attr_modalias.attr, 492 NULL 493 }; 494 ATTRIBUTE_GROUPS(i2c_dev); 495 496 struct bus_type i2c_bus_type = { 497 .name = "i2c", 498 .match = i2c_device_match, 499 .probe = i2c_device_probe, 500 .remove = i2c_device_remove, 501 .shutdown = i2c_device_shutdown, 502 }; 503 EXPORT_SYMBOL_GPL(i2c_bus_type); 504 505 struct device_type i2c_client_type = { 506 .groups = i2c_dev_groups, 507 .uevent = i2c_device_uevent, 508 .release = i2c_client_dev_release, 509 }; 510 EXPORT_SYMBOL_GPL(i2c_client_type); 511 512 513 /** 514 * i2c_verify_client - return parameter as i2c_client, or NULL 515 * @dev: device, probably from some driver model iterator 516 * 517 * When traversing the driver model tree, perhaps using driver model 518 * iterators like @device_for_each_child(), you can't assume very much 519 * about the nodes you find. Use this function to avoid oopses caused 520 * by wrongly treating some non-I2C device as an i2c_client. 521 */ 522 struct i2c_client *i2c_verify_client(struct device *dev) 523 { 524 return (dev->type == &i2c_client_type) 525 ? to_i2c_client(dev) 526 : NULL; 527 } 528 EXPORT_SYMBOL(i2c_verify_client); 529 530 531 /* Return a unique address which takes the flags of the client into account */ 532 static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client) 533 { 534 unsigned short addr = client->addr; 535 536 /* For some client flags, add an arbitrary offset to avoid collisions */ 537 if (client->flags & I2C_CLIENT_TEN) 538 addr |= I2C_ADDR_OFFSET_TEN_BIT; 539 540 if (client->flags & I2C_CLIENT_SLAVE) 541 addr |= I2C_ADDR_OFFSET_SLAVE; 542 543 return addr; 544 } 545 546 /* This is a permissive address validity check, I2C address map constraints 547 * are purposely not enforced, except for the general call address. */ 548 int i2c_check_addr_validity(unsigned addr, unsigned short flags) 549 { 550 if (flags & I2C_CLIENT_TEN) { 551 /* 10-bit address, all values are valid */ 552 if (addr > 0x3ff) 553 return -EINVAL; 554 } else { 555 /* 7-bit address, reject the general call address */ 556 if (addr == 0x00 || addr > 0x7f) 557 return -EINVAL; 558 } 559 return 0; 560 } 561 562 /* And this is a strict address validity check, used when probing. If a 563 * device uses a reserved address, then it shouldn't be probed. 7-bit 564 * addressing is assumed, 10-bit address devices are rare and should be 565 * explicitly enumerated. */ 566 int i2c_check_7bit_addr_validity_strict(unsigned short addr) 567 { 568 /* 569 * Reserved addresses per I2C specification: 570 * 0x00 General call address / START byte 571 * 0x01 CBUS address 572 * 0x02 Reserved for different bus format 573 * 0x03 Reserved for future purposes 574 * 0x04-0x07 Hs-mode master code 575 * 0x78-0x7b 10-bit slave addressing 576 * 0x7c-0x7f Reserved for future purposes 577 */ 578 if (addr < 0x08 || addr > 0x77) 579 return -EINVAL; 580 return 0; 581 } 582 583 static int __i2c_check_addr_busy(struct device *dev, void *addrp) 584 { 585 struct i2c_client *client = i2c_verify_client(dev); 586 int addr = *(int *)addrp; 587 588 if (client && i2c_encode_flags_to_addr(client) == addr) 589 return -EBUSY; 590 return 0; 591 } 592 593 /* walk up mux tree */ 594 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr) 595 { 596 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); 597 int result; 598 599 result = device_for_each_child(&adapter->dev, &addr, 600 __i2c_check_addr_busy); 601 602 if (!result && parent) 603 result = i2c_check_mux_parents(parent, addr); 604 605 return result; 606 } 607 608 /* recurse down mux tree */ 609 static int i2c_check_mux_children(struct device *dev, void *addrp) 610 { 611 int result; 612 613 if (dev->type == &i2c_adapter_type) 614 result = device_for_each_child(dev, addrp, 615 i2c_check_mux_children); 616 else 617 result = __i2c_check_addr_busy(dev, addrp); 618 619 return result; 620 } 621 622 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) 623 { 624 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); 625 int result = 0; 626 627 if (parent) 628 result = i2c_check_mux_parents(parent, addr); 629 630 if (!result) 631 result = device_for_each_child(&adapter->dev, &addr, 632 i2c_check_mux_children); 633 634 return result; 635 } 636 637 /** 638 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment 639 * @adapter: Target I2C bus segment 640 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT 641 * locks only this branch in the adapter tree 642 */ 643 static void i2c_adapter_lock_bus(struct i2c_adapter *adapter, 644 unsigned int flags) 645 { 646 rt_mutex_lock(&adapter->bus_lock); 647 } 648 649 /** 650 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment 651 * @adapter: Target I2C bus segment 652 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT 653 * trylocks only this branch in the adapter tree 654 */ 655 static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter, 656 unsigned int flags) 657 { 658 return rt_mutex_trylock(&adapter->bus_lock); 659 } 660 661 /** 662 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment 663 * @adapter: Target I2C bus segment 664 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT 665 * unlocks only this branch in the adapter tree 666 */ 667 static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter, 668 unsigned int flags) 669 { 670 rt_mutex_unlock(&adapter->bus_lock); 671 } 672 673 static void i2c_dev_set_name(struct i2c_adapter *adap, 674 struct i2c_client *client, 675 struct i2c_board_info const *info) 676 { 677 struct acpi_device *adev = ACPI_COMPANION(&client->dev); 678 679 if (info && info->dev_name) { 680 dev_set_name(&client->dev, "i2c-%s", info->dev_name); 681 return; 682 } 683 684 if (adev) { 685 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev)); 686 return; 687 } 688 689 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), 690 i2c_encode_flags_to_addr(client)); 691 } 692 693 static int i2c_dev_irq_from_resources(const struct resource *resources, 694 unsigned int num_resources) 695 { 696 struct irq_data *irqd; 697 int i; 698 699 for (i = 0; i < num_resources; i++) { 700 const struct resource *r = &resources[i]; 701 702 if (resource_type(r) != IORESOURCE_IRQ) 703 continue; 704 705 if (r->flags & IORESOURCE_BITS) { 706 irqd = irq_get_irq_data(r->start); 707 if (!irqd) 708 break; 709 710 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); 711 } 712 713 return r->start; 714 } 715 716 return 0; 717 } 718 719 /** 720 * i2c_new_device - instantiate an i2c device 721 * @adap: the adapter managing the device 722 * @info: describes one I2C device; bus_num is ignored 723 * Context: can sleep 724 * 725 * Create an i2c device. Binding is handled through driver model 726 * probe()/remove() methods. A driver may be bound to this device when we 727 * return from this function, or any later moment (e.g. maybe hotplugging will 728 * load the driver module). This call is not appropriate for use by mainboard 729 * initialization logic, which usually runs during an arch_initcall() long 730 * before any i2c_adapter could exist. 731 * 732 * This returns the new i2c client, which may be saved for later use with 733 * i2c_unregister_device(); or NULL to indicate an error. 734 */ 735 struct i2c_client * 736 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) 737 { 738 struct i2c_client *client; 739 int status; 740 741 client = kzalloc(sizeof *client, GFP_KERNEL); 742 if (!client) 743 return NULL; 744 745 client->adapter = adap; 746 747 client->dev.platform_data = info->platform_data; 748 749 if (info->archdata) 750 client->dev.archdata = *info->archdata; 751 752 client->flags = info->flags; 753 client->addr = info->addr; 754 755 client->irq = info->irq; 756 if (!client->irq) 757 client->irq = i2c_dev_irq_from_resources(info->resources, 758 info->num_resources); 759 760 strlcpy(client->name, info->type, sizeof(client->name)); 761 762 status = i2c_check_addr_validity(client->addr, client->flags); 763 if (status) { 764 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", 765 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); 766 goto out_err_silent; 767 } 768 769 /* Check for address business */ 770 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client)); 771 if (status) 772 goto out_err; 773 774 client->dev.parent = &client->adapter->dev; 775 client->dev.bus = &i2c_bus_type; 776 client->dev.type = &i2c_client_type; 777 client->dev.of_node = info->of_node; 778 client->dev.fwnode = info->fwnode; 779 780 i2c_dev_set_name(adap, client, info); 781 782 if (info->properties) { 783 status = device_add_properties(&client->dev, info->properties); 784 if (status) { 785 dev_err(&adap->dev, 786 "Failed to add properties to client %s: %d\n", 787 client->name, status); 788 goto out_err; 789 } 790 } 791 792 status = device_register(&client->dev); 793 if (status) 794 goto out_free_props; 795 796 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", 797 client->name, dev_name(&client->dev)); 798 799 return client; 800 801 out_free_props: 802 if (info->properties) 803 device_remove_properties(&client->dev); 804 out_err: 805 dev_err(&adap->dev, 806 "Failed to register i2c client %s at 0x%02x (%d)\n", 807 client->name, client->addr, status); 808 out_err_silent: 809 kfree(client); 810 return NULL; 811 } 812 EXPORT_SYMBOL_GPL(i2c_new_device); 813 814 815 /** 816 * i2c_unregister_device - reverse effect of i2c_new_device() 817 * @client: value returned from i2c_new_device() 818 * Context: can sleep 819 */ 820 void i2c_unregister_device(struct i2c_client *client) 821 { 822 if (!client) 823 return; 824 if (client->dev.of_node) 825 of_node_clear_flag(client->dev.of_node, OF_POPULATED); 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 /* Let legacy drivers scan this bus for matching devices */ 1132 if (driver->attach_adapter) { 1133 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n", 1134 driver->driver.name); 1135 dev_warn(&adap->dev, 1136 "Please use another way to instantiate your i2c_client\n"); 1137 /* We ignore the return code; if it fails, too bad */ 1138 driver->attach_adapter(adap); 1139 } 1140 return 0; 1141 } 1142 1143 static int __process_new_adapter(struct device_driver *d, void *data) 1144 { 1145 return i2c_do_add_adapter(to_i2c_driver(d), data); 1146 } 1147 1148 static const struct i2c_lock_operations i2c_adapter_lock_ops = { 1149 .lock_bus = i2c_adapter_lock_bus, 1150 .trylock_bus = i2c_adapter_trylock_bus, 1151 .unlock_bus = i2c_adapter_unlock_bus, 1152 }; 1153 1154 static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap) 1155 { 1156 struct irq_domain *domain = adap->host_notify_domain; 1157 irq_hw_number_t hwirq; 1158 1159 if (!domain) 1160 return; 1161 1162 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++) 1163 irq_dispose_mapping(irq_find_mapping(domain, hwirq)); 1164 1165 irq_domain_remove(domain); 1166 adap->host_notify_domain = NULL; 1167 } 1168 1169 static int i2c_host_notify_irq_map(struct irq_domain *h, 1170 unsigned int virq, 1171 irq_hw_number_t hw_irq_num) 1172 { 1173 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq); 1174 1175 return 0; 1176 } 1177 1178 static const struct irq_domain_ops i2c_host_notify_irq_ops = { 1179 .map = i2c_host_notify_irq_map, 1180 }; 1181 1182 static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap) 1183 { 1184 struct irq_domain *domain; 1185 1186 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY)) 1187 return 0; 1188 1189 domain = irq_domain_create_linear(adap->dev.fwnode, 1190 I2C_ADDR_7BITS_COUNT, 1191 &i2c_host_notify_irq_ops, adap); 1192 if (!domain) 1193 return -ENOMEM; 1194 1195 adap->host_notify_domain = domain; 1196 1197 return 0; 1198 } 1199 1200 /** 1201 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct 1202 * I2C client. 1203 * @adap: the adapter 1204 * @addr: the I2C address of the notifying device 1205 * Context: can't sleep 1206 * 1207 * Helper function to be called from an I2C bus driver's interrupt 1208 * handler. It will schedule the Host Notify IRQ. 1209 */ 1210 int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr) 1211 { 1212 int irq; 1213 1214 if (!adap) 1215 return -EINVAL; 1216 1217 irq = irq_find_mapping(adap->host_notify_domain, addr); 1218 if (irq <= 0) 1219 return -ENXIO; 1220 1221 generic_handle_irq(irq); 1222 1223 return 0; 1224 } 1225 EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify); 1226 1227 static int i2c_register_adapter(struct i2c_adapter *adap) 1228 { 1229 int res = -EINVAL; 1230 1231 /* Can't register until after driver model init */ 1232 if (WARN_ON(!is_registered)) { 1233 res = -EAGAIN; 1234 goto out_list; 1235 } 1236 1237 /* Sanity checks */ 1238 if (WARN(!adap->name[0], "i2c adapter has no name")) 1239 goto out_list; 1240 1241 if (!adap->algo) { 1242 pr_err("adapter '%s': no algo supplied!\n", adap->name); 1243 goto out_list; 1244 } 1245 1246 if (!adap->lock_ops) 1247 adap->lock_ops = &i2c_adapter_lock_ops; 1248 1249 rt_mutex_init(&adap->bus_lock); 1250 rt_mutex_init(&adap->mux_lock); 1251 mutex_init(&adap->userspace_clients_lock); 1252 INIT_LIST_HEAD(&adap->userspace_clients); 1253 1254 /* Set default timeout to 1 second if not already set */ 1255 if (adap->timeout == 0) 1256 adap->timeout = HZ; 1257 1258 /* register soft irqs for Host Notify */ 1259 res = i2c_setup_host_notify_irq_domain(adap); 1260 if (res) { 1261 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n", 1262 adap->name, res); 1263 goto out_list; 1264 } 1265 1266 dev_set_name(&adap->dev, "i2c-%d", adap->nr); 1267 adap->dev.bus = &i2c_bus_type; 1268 adap->dev.type = &i2c_adapter_type; 1269 res = device_register(&adap->dev); 1270 if (res) { 1271 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res); 1272 goto out_list; 1273 } 1274 1275 res = of_i2c_setup_smbus_alert(adap); 1276 if (res) 1277 goto out_reg; 1278 1279 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); 1280 1281 pm_runtime_no_callbacks(&adap->dev); 1282 pm_suspend_ignore_children(&adap->dev, true); 1283 pm_runtime_enable(&adap->dev); 1284 1285 #ifdef CONFIG_I2C_COMPAT 1286 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, 1287 adap->dev.parent); 1288 if (res) 1289 dev_warn(&adap->dev, 1290 "Failed to create compatibility class link\n"); 1291 #endif 1292 1293 i2c_init_recovery(adap); 1294 1295 /* create pre-declared device nodes */ 1296 of_i2c_register_devices(adap); 1297 i2c_acpi_register_devices(adap); 1298 i2c_acpi_install_space_handler(adap); 1299 1300 if (adap->nr < __i2c_first_dynamic_bus_num) 1301 i2c_scan_static_board_info(adap); 1302 1303 /* Notify drivers */ 1304 mutex_lock(&core_lock); 1305 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); 1306 mutex_unlock(&core_lock); 1307 1308 return 0; 1309 1310 out_reg: 1311 init_completion(&adap->dev_released); 1312 device_unregister(&adap->dev); 1313 wait_for_completion(&adap->dev_released); 1314 out_list: 1315 mutex_lock(&core_lock); 1316 idr_remove(&i2c_adapter_idr, adap->nr); 1317 mutex_unlock(&core_lock); 1318 return res; 1319 } 1320 1321 /** 1322 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1 1323 * @adap: the adapter to register (with adap->nr initialized) 1324 * Context: can sleep 1325 * 1326 * See i2c_add_numbered_adapter() for details. 1327 */ 1328 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap) 1329 { 1330 int id; 1331 1332 mutex_lock(&core_lock); 1333 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL); 1334 mutex_unlock(&core_lock); 1335 if (WARN(id < 0, "couldn't get idr")) 1336 return id == -ENOSPC ? -EBUSY : id; 1337 1338 return i2c_register_adapter(adap); 1339 } 1340 1341 /** 1342 * i2c_add_adapter - declare i2c adapter, use dynamic bus number 1343 * @adapter: the adapter to add 1344 * Context: can sleep 1345 * 1346 * This routine is used to declare an I2C adapter when its bus number 1347 * doesn't matter or when its bus number is specified by an dt alias. 1348 * Examples of bases when the bus number doesn't matter: I2C adapters 1349 * dynamically added by USB links or PCI plugin cards. 1350 * 1351 * When this returns zero, a new bus number was allocated and stored 1352 * in adap->nr, and the specified adapter became available for clients. 1353 * Otherwise, a negative errno value is returned. 1354 */ 1355 int i2c_add_adapter(struct i2c_adapter *adapter) 1356 { 1357 struct device *dev = &adapter->dev; 1358 int id; 1359 1360 if (dev->of_node) { 1361 id = of_alias_get_id(dev->of_node, "i2c"); 1362 if (id >= 0) { 1363 adapter->nr = id; 1364 return __i2c_add_numbered_adapter(adapter); 1365 } 1366 } 1367 1368 mutex_lock(&core_lock); 1369 id = idr_alloc(&i2c_adapter_idr, adapter, 1370 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); 1371 mutex_unlock(&core_lock); 1372 if (WARN(id < 0, "couldn't get idr")) 1373 return id; 1374 1375 adapter->nr = id; 1376 1377 return i2c_register_adapter(adapter); 1378 } 1379 EXPORT_SYMBOL(i2c_add_adapter); 1380 1381 /** 1382 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number 1383 * @adap: the adapter to register (with adap->nr initialized) 1384 * Context: can sleep 1385 * 1386 * This routine is used to declare an I2C adapter when its bus number 1387 * matters. For example, use it for I2C adapters from system-on-chip CPUs, 1388 * or otherwise built in to the system's mainboard, and where i2c_board_info 1389 * is used to properly configure I2C devices. 1390 * 1391 * If the requested bus number is set to -1, then this function will behave 1392 * identically to i2c_add_adapter, and will dynamically assign a bus number. 1393 * 1394 * If no devices have pre-been declared for this bus, then be sure to 1395 * register the adapter before any dynamically allocated ones. Otherwise 1396 * the required bus ID may not be available. 1397 * 1398 * When this returns zero, the specified adapter became available for 1399 * clients using the bus number provided in adap->nr. Also, the table 1400 * of I2C devices pre-declared using i2c_register_board_info() is scanned, 1401 * and the appropriate driver model device nodes are created. Otherwise, a 1402 * negative errno value is returned. 1403 */ 1404 int i2c_add_numbered_adapter(struct i2c_adapter *adap) 1405 { 1406 if (adap->nr == -1) /* -1 means dynamically assign bus id */ 1407 return i2c_add_adapter(adap); 1408 1409 return __i2c_add_numbered_adapter(adap); 1410 } 1411 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); 1412 1413 static void i2c_do_del_adapter(struct i2c_driver *driver, 1414 struct i2c_adapter *adapter) 1415 { 1416 struct i2c_client *client, *_n; 1417 1418 /* Remove the devices we created ourselves as the result of hardware 1419 * probing (using a driver's detect method) */ 1420 list_for_each_entry_safe(client, _n, &driver->clients, detected) { 1421 if (client->adapter == adapter) { 1422 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", 1423 client->name, client->addr); 1424 list_del(&client->detected); 1425 i2c_unregister_device(client); 1426 } 1427 } 1428 } 1429 1430 static int __unregister_client(struct device *dev, void *dummy) 1431 { 1432 struct i2c_client *client = i2c_verify_client(dev); 1433 if (client && strcmp(client->name, "dummy")) 1434 i2c_unregister_device(client); 1435 return 0; 1436 } 1437 1438 static int __unregister_dummy(struct device *dev, void *dummy) 1439 { 1440 struct i2c_client *client = i2c_verify_client(dev); 1441 i2c_unregister_device(client); 1442 return 0; 1443 } 1444 1445 static int __process_removed_adapter(struct device_driver *d, void *data) 1446 { 1447 i2c_do_del_adapter(to_i2c_driver(d), data); 1448 return 0; 1449 } 1450 1451 /** 1452 * i2c_del_adapter - unregister I2C adapter 1453 * @adap: the adapter being unregistered 1454 * Context: can sleep 1455 * 1456 * This unregisters an I2C adapter which was previously registered 1457 * by @i2c_add_adapter or @i2c_add_numbered_adapter. 1458 */ 1459 void i2c_del_adapter(struct i2c_adapter *adap) 1460 { 1461 struct i2c_adapter *found; 1462 struct i2c_client *client, *next; 1463 1464 /* First make sure that this adapter was ever added */ 1465 mutex_lock(&core_lock); 1466 found = idr_find(&i2c_adapter_idr, adap->nr); 1467 mutex_unlock(&core_lock); 1468 if (found != adap) { 1469 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name); 1470 return; 1471 } 1472 1473 i2c_acpi_remove_space_handler(adap); 1474 /* Tell drivers about this removal */ 1475 mutex_lock(&core_lock); 1476 bus_for_each_drv(&i2c_bus_type, NULL, adap, 1477 __process_removed_adapter); 1478 mutex_unlock(&core_lock); 1479 1480 /* Remove devices instantiated from sysfs */ 1481 mutex_lock_nested(&adap->userspace_clients_lock, 1482 i2c_adapter_depth(adap)); 1483 list_for_each_entry_safe(client, next, &adap->userspace_clients, 1484 detected) { 1485 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, 1486 client->addr); 1487 list_del(&client->detected); 1488 i2c_unregister_device(client); 1489 } 1490 mutex_unlock(&adap->userspace_clients_lock); 1491 1492 /* Detach any active clients. This can't fail, thus we do not 1493 * check the returned value. This is a two-pass process, because 1494 * we can't remove the dummy devices during the first pass: they 1495 * could have been instantiated by real devices wishing to clean 1496 * them up properly, so we give them a chance to do that first. */ 1497 device_for_each_child(&adap->dev, NULL, __unregister_client); 1498 device_for_each_child(&adap->dev, NULL, __unregister_dummy); 1499 1500 #ifdef CONFIG_I2C_COMPAT 1501 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, 1502 adap->dev.parent); 1503 #endif 1504 1505 /* device name is gone after device_unregister */ 1506 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); 1507 1508 pm_runtime_disable(&adap->dev); 1509 1510 i2c_host_notify_irq_teardown(adap); 1511 1512 /* wait until all references to the device are gone 1513 * 1514 * FIXME: This is old code and should ideally be replaced by an 1515 * alternative which results in decoupling the lifetime of the struct 1516 * device from the i2c_adapter, like spi or netdev do. Any solution 1517 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled! 1518 */ 1519 init_completion(&adap->dev_released); 1520 device_unregister(&adap->dev); 1521 wait_for_completion(&adap->dev_released); 1522 1523 /* free bus id */ 1524 mutex_lock(&core_lock); 1525 idr_remove(&i2c_adapter_idr, adap->nr); 1526 mutex_unlock(&core_lock); 1527 1528 /* Clear the device structure in case this adapter is ever going to be 1529 added again */ 1530 memset(&adap->dev, 0, sizeof(adap->dev)); 1531 } 1532 EXPORT_SYMBOL(i2c_del_adapter); 1533 1534 /** 1535 * i2c_parse_fw_timings - get I2C related timing parameters from firmware 1536 * @dev: The device to scan for I2C timing properties 1537 * @t: the i2c_timings struct to be filled with values 1538 * @use_defaults: bool to use sane defaults derived from the I2C specification 1539 * when properties are not found, otherwise use 0 1540 * 1541 * Scan the device for the generic I2C properties describing timing parameters 1542 * for the signal and fill the given struct with the results. If a property was 1543 * not found and use_defaults was true, then maximum timings are assumed which 1544 * are derived from the I2C specification. If use_defaults is not used, the 1545 * results will be 0, so drivers can apply their own defaults later. The latter 1546 * is mainly intended for avoiding regressions of existing drivers which want 1547 * to switch to this function. New drivers almost always should use the defaults. 1548 */ 1549 1550 void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults) 1551 { 1552 int ret; 1553 1554 memset(t, 0, sizeof(*t)); 1555 1556 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz); 1557 if (ret && use_defaults) 1558 t->bus_freq_hz = 100000; 1559 1560 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns); 1561 if (ret && use_defaults) { 1562 if (t->bus_freq_hz <= 100000) 1563 t->scl_rise_ns = 1000; 1564 else if (t->bus_freq_hz <= 400000) 1565 t->scl_rise_ns = 300; 1566 else 1567 t->scl_rise_ns = 120; 1568 } 1569 1570 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns); 1571 if (ret && use_defaults) { 1572 if (t->bus_freq_hz <= 400000) 1573 t->scl_fall_ns = 300; 1574 else 1575 t->scl_fall_ns = 120; 1576 } 1577 1578 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns); 1579 1580 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns); 1581 if (ret && use_defaults) 1582 t->sda_fall_ns = t->scl_fall_ns; 1583 } 1584 EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); 1585 1586 /* ------------------------------------------------------------------------- */ 1587 1588 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *)) 1589 { 1590 int res; 1591 1592 mutex_lock(&core_lock); 1593 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn); 1594 mutex_unlock(&core_lock); 1595 1596 return res; 1597 } 1598 EXPORT_SYMBOL_GPL(i2c_for_each_dev); 1599 1600 static int __process_new_driver(struct device *dev, void *data) 1601 { 1602 if (dev->type != &i2c_adapter_type) 1603 return 0; 1604 return i2c_do_add_adapter(data, to_i2c_adapter(dev)); 1605 } 1606 1607 /* 1608 * An i2c_driver is used with one or more i2c_client (device) nodes to access 1609 * i2c slave chips, on a bus instance associated with some i2c_adapter. 1610 */ 1611 1612 int i2c_register_driver(struct module *owner, struct i2c_driver *driver) 1613 { 1614 int res; 1615 1616 /* Can't register until after driver model init */ 1617 if (WARN_ON(!is_registered)) 1618 return -EAGAIN; 1619 1620 /* add the driver to the list of i2c drivers in the driver core */ 1621 driver->driver.owner = owner; 1622 driver->driver.bus = &i2c_bus_type; 1623 INIT_LIST_HEAD(&driver->clients); 1624 1625 /* When registration returns, the driver core 1626 * will have called probe() for all matching-but-unbound devices. 1627 */ 1628 res = driver_register(&driver->driver); 1629 if (res) 1630 return res; 1631 1632 pr_debug("driver [%s] registered\n", driver->driver.name); 1633 1634 /* Walk the adapters that are already present */ 1635 i2c_for_each_dev(driver, __process_new_driver); 1636 1637 return 0; 1638 } 1639 EXPORT_SYMBOL(i2c_register_driver); 1640 1641 static int __process_removed_driver(struct device *dev, void *data) 1642 { 1643 if (dev->type == &i2c_adapter_type) 1644 i2c_do_del_adapter(data, to_i2c_adapter(dev)); 1645 return 0; 1646 } 1647 1648 /** 1649 * i2c_del_driver - unregister I2C driver 1650 * @driver: the driver being unregistered 1651 * Context: can sleep 1652 */ 1653 void i2c_del_driver(struct i2c_driver *driver) 1654 { 1655 i2c_for_each_dev(driver, __process_removed_driver); 1656 1657 driver_unregister(&driver->driver); 1658 pr_debug("driver [%s] unregistered\n", driver->driver.name); 1659 } 1660 EXPORT_SYMBOL(i2c_del_driver); 1661 1662 /* ------------------------------------------------------------------------- */ 1663 1664 /** 1665 * i2c_use_client - increments the reference count of the i2c client structure 1666 * @client: the client being referenced 1667 * 1668 * Each live reference to a client should be refcounted. The driver model does 1669 * that automatically as part of driver binding, so that most drivers don't 1670 * need to do this explicitly: they hold a reference until they're unbound 1671 * from the device. 1672 * 1673 * A pointer to the client with the incremented reference counter is returned. 1674 */ 1675 struct i2c_client *i2c_use_client(struct i2c_client *client) 1676 { 1677 if (client && get_device(&client->dev)) 1678 return client; 1679 return NULL; 1680 } 1681 EXPORT_SYMBOL(i2c_use_client); 1682 1683 /** 1684 * i2c_release_client - release a use of the i2c client structure 1685 * @client: the client being no longer referenced 1686 * 1687 * Must be called when a user of a client is finished with it. 1688 */ 1689 void i2c_release_client(struct i2c_client *client) 1690 { 1691 if (client) 1692 put_device(&client->dev); 1693 } 1694 EXPORT_SYMBOL(i2c_release_client); 1695 1696 struct i2c_cmd_arg { 1697 unsigned cmd; 1698 void *arg; 1699 }; 1700 1701 static int i2c_cmd(struct device *dev, void *_arg) 1702 { 1703 struct i2c_client *client = i2c_verify_client(dev); 1704 struct i2c_cmd_arg *arg = _arg; 1705 struct i2c_driver *driver; 1706 1707 if (!client || !client->dev.driver) 1708 return 0; 1709 1710 driver = to_i2c_driver(client->dev.driver); 1711 if (driver->command) 1712 driver->command(client, arg->cmd, arg->arg); 1713 return 0; 1714 } 1715 1716 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) 1717 { 1718 struct i2c_cmd_arg cmd_arg; 1719 1720 cmd_arg.cmd = cmd; 1721 cmd_arg.arg = arg; 1722 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); 1723 } 1724 EXPORT_SYMBOL(i2c_clients_command); 1725 1726 static int __init i2c_init(void) 1727 { 1728 int retval; 1729 1730 retval = of_alias_get_highest_id("i2c"); 1731 1732 down_write(&__i2c_board_lock); 1733 if (retval >= __i2c_first_dynamic_bus_num) 1734 __i2c_first_dynamic_bus_num = retval + 1; 1735 up_write(&__i2c_board_lock); 1736 1737 retval = bus_register(&i2c_bus_type); 1738 if (retval) 1739 return retval; 1740 1741 is_registered = true; 1742 1743 #ifdef CONFIG_I2C_COMPAT 1744 i2c_adapter_compat_class = class_compat_register("i2c-adapter"); 1745 if (!i2c_adapter_compat_class) { 1746 retval = -ENOMEM; 1747 goto bus_err; 1748 } 1749 #endif 1750 retval = i2c_add_driver(&dummy_driver); 1751 if (retval) 1752 goto class_err; 1753 1754 if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 1755 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier)); 1756 if (IS_ENABLED(CONFIG_ACPI)) 1757 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier)); 1758 1759 return 0; 1760 1761 class_err: 1762 #ifdef CONFIG_I2C_COMPAT 1763 class_compat_unregister(i2c_adapter_compat_class); 1764 bus_err: 1765 #endif 1766 is_registered = false; 1767 bus_unregister(&i2c_bus_type); 1768 return retval; 1769 } 1770 1771 static void __exit i2c_exit(void) 1772 { 1773 if (IS_ENABLED(CONFIG_ACPI)) 1774 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier)); 1775 if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 1776 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier)); 1777 i2c_del_driver(&dummy_driver); 1778 #ifdef CONFIG_I2C_COMPAT 1779 class_compat_unregister(i2c_adapter_compat_class); 1780 #endif 1781 bus_unregister(&i2c_bus_type); 1782 tracepoint_synchronize_unregister(); 1783 } 1784 1785 /* We must initialize early, because some subsystems register i2c drivers 1786 * in subsys_initcall() code, but are linked (and initialized) before i2c. 1787 */ 1788 postcore_initcall(i2c_init); 1789 module_exit(i2c_exit); 1790 1791 /* ---------------------------------------------------- 1792 * the functional interface to the i2c busses. 1793 * ---------------------------------------------------- 1794 */ 1795 1796 /* Check if val is exceeding the quirk IFF quirk is non 0 */ 1797 #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk))) 1798 1799 static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg) 1800 { 1801 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n", 1802 err_msg, msg->addr, msg->len, 1803 msg->flags & I2C_M_RD ? "read" : "write"); 1804 return -EOPNOTSUPP; 1805 } 1806 1807 static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 1808 { 1809 const struct i2c_adapter_quirks *q = adap->quirks; 1810 int max_num = q->max_num_msgs, i; 1811 bool do_len_check = true; 1812 1813 if (q->flags & I2C_AQ_COMB) { 1814 max_num = 2; 1815 1816 /* special checks for combined messages */ 1817 if (num == 2) { 1818 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD) 1819 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write"); 1820 1821 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD)) 1822 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read"); 1823 1824 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr) 1825 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr"); 1826 1827 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len)) 1828 return i2c_quirk_error(adap, &msgs[0], "msg too long"); 1829 1830 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len)) 1831 return i2c_quirk_error(adap, &msgs[1], "msg too long"); 1832 1833 do_len_check = false; 1834 } 1835 } 1836 1837 if (i2c_quirk_exceeded(num, max_num)) 1838 return i2c_quirk_error(adap, &msgs[0], "too many messages"); 1839 1840 for (i = 0; i < num; i++) { 1841 u16 len = msgs[i].len; 1842 1843 if (msgs[i].flags & I2C_M_RD) { 1844 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len)) 1845 return i2c_quirk_error(adap, &msgs[i], "msg too long"); 1846 } else { 1847 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len)) 1848 return i2c_quirk_error(adap, &msgs[i], "msg too long"); 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 (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) 1873 return -EOPNOTSUPP; 1874 1875 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets 1876 * enabled. This is an efficient way of keeping the for-loop from 1877 * being executed when not needed. 1878 */ 1879 if (static_key_false(&i2c_trace_msg)) { 1880 int i; 1881 for (i = 0; i < num; i++) 1882 if (msgs[i].flags & I2C_M_RD) 1883 trace_i2c_read(adap, &msgs[i], i); 1884 else 1885 trace_i2c_write(adap, &msgs[i], i); 1886 } 1887 1888 /* Retry automatically on arbitration loss */ 1889 orig_jiffies = jiffies; 1890 for (ret = 0, try = 0; try <= adap->retries; try++) { 1891 ret = adap->algo->master_xfer(adap, msgs, num); 1892 if (ret != -EAGAIN) 1893 break; 1894 if (time_after(jiffies, orig_jiffies + adap->timeout)) 1895 break; 1896 } 1897 1898 if (static_key_false(&i2c_trace_msg)) { 1899 int i; 1900 for (i = 0; i < ret; i++) 1901 if (msgs[i].flags & I2C_M_RD) 1902 trace_i2c_reply(adap, &msgs[i], i); 1903 trace_i2c_result(adap, i, ret); 1904 } 1905 1906 return ret; 1907 } 1908 EXPORT_SYMBOL(__i2c_transfer); 1909 1910 /** 1911 * i2c_transfer - execute a single or combined I2C message 1912 * @adap: Handle to I2C bus 1913 * @msgs: One or more messages to execute before STOP is issued to 1914 * terminate the operation; each message begins with a START. 1915 * @num: Number of messages to be executed. 1916 * 1917 * Returns negative errno, else the number of messages executed. 1918 * 1919 * Note that there is no requirement that each message be sent to 1920 * the same slave address, although that is the most common model. 1921 */ 1922 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 1923 { 1924 int ret; 1925 1926 /* REVISIT the fault reporting model here is weak: 1927 * 1928 * - When we get an error after receiving N bytes from a slave, 1929 * there is no way to report "N". 1930 * 1931 * - When we get a NAK after transmitting N bytes to a slave, 1932 * there is no way to report "N" ... or to let the master 1933 * continue executing the rest of this combined message, if 1934 * that's the appropriate response. 1935 * 1936 * - When for example "num" is two and we successfully complete 1937 * the first message but get an error part way through the 1938 * second, it's unclear whether that should be reported as 1939 * one (discarding status on the second message) or errno 1940 * (discarding status on the first one). 1941 */ 1942 1943 if (adap->algo->master_xfer) { 1944 #ifdef DEBUG 1945 for (ret = 0; ret < num; ret++) { 1946 dev_dbg(&adap->dev, 1947 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n", 1948 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W', 1949 msgs[ret].addr, msgs[ret].len, 1950 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); 1951 } 1952 #endif 1953 1954 if (in_atomic() || irqs_disabled()) { 1955 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT); 1956 if (!ret) 1957 /* I2C activity is ongoing. */ 1958 return -EAGAIN; 1959 } else { 1960 i2c_lock_bus(adap, I2C_LOCK_SEGMENT); 1961 } 1962 1963 ret = __i2c_transfer(adap, msgs, num); 1964 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT); 1965 1966 return ret; 1967 } else { 1968 dev_dbg(&adap->dev, "I2C level transfers not supported\n"); 1969 return -EOPNOTSUPP; 1970 } 1971 } 1972 EXPORT_SYMBOL(i2c_transfer); 1973 1974 /** 1975 * i2c_master_send - issue a single I2C message in master transmit mode 1976 * @client: Handle to slave device 1977 * @buf: Data that will be written to the slave 1978 * @count: How many bytes to write, must be less than 64k since msg.len is u16 1979 * 1980 * Returns negative errno, or else the number of bytes written. 1981 */ 1982 int i2c_master_send(const struct i2c_client *client, const char *buf, int count) 1983 { 1984 int ret; 1985 struct i2c_adapter *adap = client->adapter; 1986 struct i2c_msg msg; 1987 1988 msg.addr = client->addr; 1989 msg.flags = client->flags & I2C_M_TEN; 1990 msg.len = count; 1991 msg.buf = (char *)buf; 1992 1993 ret = i2c_transfer(adap, &msg, 1); 1994 1995 /* 1996 * If everything went ok (i.e. 1 msg transmitted), return #bytes 1997 * transmitted, else error code. 1998 */ 1999 return (ret == 1) ? count : ret; 2000 } 2001 EXPORT_SYMBOL(i2c_master_send); 2002 2003 /** 2004 * i2c_master_recv - issue a single I2C message in master receive mode 2005 * @client: Handle to slave device 2006 * @buf: Where to store data read from slave 2007 * @count: How many bytes to read, must be less than 64k since msg.len is u16 2008 * 2009 * Returns negative errno, or else the number of bytes read. 2010 */ 2011 int i2c_master_recv(const struct i2c_client *client, char *buf, int count) 2012 { 2013 struct i2c_adapter *adap = client->adapter; 2014 struct i2c_msg msg; 2015 int ret; 2016 2017 msg.addr = client->addr; 2018 msg.flags = client->flags & I2C_M_TEN; 2019 msg.flags |= I2C_M_RD; 2020 msg.len = count; 2021 msg.buf = buf; 2022 2023 ret = i2c_transfer(adap, &msg, 1); 2024 2025 /* 2026 * If everything went ok (i.e. 1 msg received), return #bytes received, 2027 * else error code. 2028 */ 2029 return (ret == 1) ? count : ret; 2030 } 2031 EXPORT_SYMBOL(i2c_master_recv); 2032 2033 /* ---------------------------------------------------- 2034 * the i2c address scanning function 2035 * Will not work for 10-bit addresses! 2036 * ---------------------------------------------------- 2037 */ 2038 2039 /* 2040 * Legacy default probe function, mostly relevant for SMBus. The default 2041 * probe method is a quick write, but it is known to corrupt the 24RF08 2042 * EEPROMs due to a state machine bug, and could also irreversibly 2043 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f, 2044 * we use a short byte read instead. Also, some bus drivers don't implement 2045 * quick write, so we fallback to a byte read in that case too. 2046 * On x86, there is another special case for FSC hardware monitoring chips, 2047 * which want regular byte reads (address 0x73.) Fortunately, these are the 2048 * only known chips using this I2C address on PC hardware. 2049 * Returns 1 if probe succeeded, 0 if not. 2050 */ 2051 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr) 2052 { 2053 int err; 2054 union i2c_smbus_data dummy; 2055 2056 #ifdef CONFIG_X86 2057 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON) 2058 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA)) 2059 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 2060 I2C_SMBUS_BYTE_DATA, &dummy); 2061 else 2062 #endif 2063 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50) 2064 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) 2065 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, 2066 I2C_SMBUS_QUICK, NULL); 2067 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) 2068 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 2069 I2C_SMBUS_BYTE, &dummy); 2070 else { 2071 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n", 2072 addr); 2073 err = -EOPNOTSUPP; 2074 } 2075 2076 return err >= 0; 2077 } 2078 2079 static int i2c_detect_address(struct i2c_client *temp_client, 2080 struct i2c_driver *driver) 2081 { 2082 struct i2c_board_info info; 2083 struct i2c_adapter *adapter = temp_client->adapter; 2084 int addr = temp_client->addr; 2085 int err; 2086 2087 /* Make sure the address is valid */ 2088 err = i2c_check_7bit_addr_validity_strict(addr); 2089 if (err) { 2090 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", 2091 addr); 2092 return err; 2093 } 2094 2095 /* Skip if already in use (7 bit, no need to encode flags) */ 2096 if (i2c_check_addr_busy(adapter, addr)) 2097 return 0; 2098 2099 /* Make sure there is something at this address */ 2100 if (!i2c_default_probe(adapter, addr)) 2101 return 0; 2102 2103 /* Finally call the custom detection function */ 2104 memset(&info, 0, sizeof(struct i2c_board_info)); 2105 info.addr = addr; 2106 err = driver->detect(temp_client, &info); 2107 if (err) { 2108 /* -ENODEV is returned if the detection fails. We catch it 2109 here as this isn't an error. */ 2110 return err == -ENODEV ? 0 : err; 2111 } 2112 2113 /* Consistency check */ 2114 if (info.type[0] == '\0') { 2115 dev_err(&adapter->dev, 2116 "%s detection function provided no name for 0x%x\n", 2117 driver->driver.name, addr); 2118 } else { 2119 struct i2c_client *client; 2120 2121 /* Detection succeeded, instantiate the device */ 2122 if (adapter->class & I2C_CLASS_DEPRECATED) 2123 dev_warn(&adapter->dev, 2124 "This adapter will soon drop class based instantiation of devices. " 2125 "Please make sure client 0x%02x gets instantiated by other means. " 2126 "Check 'Documentation/i2c/instantiating-devices' for details.\n", 2127 info.addr); 2128 2129 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", 2130 info.type, info.addr); 2131 client = i2c_new_device(adapter, &info); 2132 if (client) 2133 list_add_tail(&client->detected, &driver->clients); 2134 else 2135 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", 2136 info.type, info.addr); 2137 } 2138 return 0; 2139 } 2140 2141 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) 2142 { 2143 const unsigned short *address_list; 2144 struct i2c_client *temp_client; 2145 int i, err = 0; 2146 int adap_id = i2c_adapter_id(adapter); 2147 2148 address_list = driver->address_list; 2149 if (!driver->detect || !address_list) 2150 return 0; 2151 2152 /* Warn that the adapter lost class based instantiation */ 2153 if (adapter->class == I2C_CLASS_DEPRECATED) { 2154 dev_dbg(&adapter->dev, 2155 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. " 2156 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n", 2157 driver->driver.name); 2158 return 0; 2159 } 2160 2161 /* Stop here if the classes do not match */ 2162 if (!(adapter->class & driver->class)) 2163 return 0; 2164 2165 /* Set up a temporary client to help detect callback */ 2166 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); 2167 if (!temp_client) 2168 return -ENOMEM; 2169 temp_client->adapter = adapter; 2170 2171 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { 2172 dev_dbg(&adapter->dev, 2173 "found normal entry for adapter %d, addr 0x%02x\n", 2174 adap_id, address_list[i]); 2175 temp_client->addr = address_list[i]; 2176 err = i2c_detect_address(temp_client, driver); 2177 if (unlikely(err)) 2178 break; 2179 } 2180 2181 kfree(temp_client); 2182 return err; 2183 } 2184 2185 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr) 2186 { 2187 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 2188 I2C_SMBUS_QUICK, NULL) >= 0; 2189 } 2190 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read); 2191 2192 struct i2c_client * 2193 i2c_new_probed_device(struct i2c_adapter *adap, 2194 struct i2c_board_info *info, 2195 unsigned short const *addr_list, 2196 int (*probe)(struct i2c_adapter *, unsigned short addr)) 2197 { 2198 int i; 2199 2200 if (!probe) 2201 probe = i2c_default_probe; 2202 2203 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { 2204 /* Check address validity */ 2205 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) { 2206 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n", 2207 addr_list[i]); 2208 continue; 2209 } 2210 2211 /* Check address availability (7 bit, no need to encode flags) */ 2212 if (i2c_check_addr_busy(adap, addr_list[i])) { 2213 dev_dbg(&adap->dev, 2214 "Address 0x%02x already in use, not probing\n", 2215 addr_list[i]); 2216 continue; 2217 } 2218 2219 /* Test address responsiveness */ 2220 if (probe(adap, addr_list[i])) 2221 break; 2222 } 2223 2224 if (addr_list[i] == I2C_CLIENT_END) { 2225 dev_dbg(&adap->dev, "Probing failed, no device found\n"); 2226 return NULL; 2227 } 2228 2229 info->addr = addr_list[i]; 2230 return i2c_new_device(adap, info); 2231 } 2232 EXPORT_SYMBOL_GPL(i2c_new_probed_device); 2233 2234 struct i2c_adapter *i2c_get_adapter(int nr) 2235 { 2236 struct i2c_adapter *adapter; 2237 2238 mutex_lock(&core_lock); 2239 adapter = idr_find(&i2c_adapter_idr, nr); 2240 if (!adapter) 2241 goto exit; 2242 2243 if (try_module_get(adapter->owner)) 2244 get_device(&adapter->dev); 2245 else 2246 adapter = NULL; 2247 2248 exit: 2249 mutex_unlock(&core_lock); 2250 return adapter; 2251 } 2252 EXPORT_SYMBOL(i2c_get_adapter); 2253 2254 void i2c_put_adapter(struct i2c_adapter *adap) 2255 { 2256 if (!adap) 2257 return; 2258 2259 put_device(&adap->dev); 2260 module_put(adap->owner); 2261 } 2262 EXPORT_SYMBOL(i2c_put_adapter); 2263 2264 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); 2265 MODULE_DESCRIPTION("I2C-Bus main module"); 2266 MODULE_LICENSE("GPL"); 2267