Lines Matching +full:wait +full:- +full:monitoring +full:- +full:ns
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 1995-99 Simon G. Vogl
10 * Copyright (C) 2013-2017 Wolfram Sang <wsa@kernel.org>
13 #define pr_fmt(fmt) "i2c-core: " fmt
15 #include <dt-bindings/i2c/i2c.h>
17 #include <linux/clk/clk-conf.h>
25 #include <linux/i2c-smbus.h>
46 #include "i2c-core.h"
111 while (id->name[0]) { in i2c_match_id()
112 if (strcmp(client->name, id->name) == 0) in i2c_match_id()
122 struct i2c_driver *driver = to_i2c_driver(client->dev.driver); in i2c_get_match_data()
126 data = device_get_match_data(&client->dev); in i2c_get_match_data()
128 match = i2c_match_id(driver->id_table, client); in i2c_get_match_data()
132 data = (const void *)match->driver_data; in i2c_get_match_data()
146 if (i2c_of_match_device(drv->of_match_table, client)) in i2c_device_match()
156 if (i2c_match_id(driver->id_table, client)) in i2c_device_match()
168 if (rc != -ENODEV) in i2c_device_uevent()
172 if (rc != -ENODEV) in i2c_device_uevent()
175 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name); in i2c_device_uevent()
181 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod); in get_scl_gpio_value()
186 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val); in set_scl_gpio_value()
191 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod); in get_sda_gpio_value()
196 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val); in set_sda_gpio_value()
201 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_generic_bus_free()
202 int ret = -EOPNOTSUPP; in i2c_generic_bus_free()
204 if (bri->get_bus_free) in i2c_generic_bus_free()
205 ret = bri->get_bus_free(adap); in i2c_generic_bus_free()
206 else if (bri->get_sda) in i2c_generic_bus_free()
207 ret = bri->get_sda(adap); in i2c_generic_bus_free()
212 return ret ? 0 : -EBUSY; in i2c_generic_bus_free()
218 * is: delay in ns = (10^6 / 100) / 2
225 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_generic_scl_recovery()
228 if (bri->prepare_recovery) in i2c_generic_scl_recovery()
229 bri->prepare_recovery(adap); in i2c_generic_scl_recovery()
230 if (bri->pinctrl) in i2c_generic_scl_recovery()
231 pinctrl_select_state(bri->pinctrl, bri->pins_gpio); in i2c_generic_scl_recovery()
240 bri->set_scl(adap, scl); in i2c_generic_scl_recovery()
242 if (bri->set_sda) in i2c_generic_scl_recovery()
243 bri->set_sda(adap, scl); in i2c_generic_scl_recovery()
247 * By this time SCL is high, as we need to give 9 falling-rising edges in i2c_generic_scl_recovery()
252 if (!bri->get_scl(adap)) { in i2c_generic_scl_recovery()
253 dev_err(&adap->dev, in i2c_generic_scl_recovery()
255 ret = -EBUSY; in i2c_generic_scl_recovery()
261 bri->set_scl(adap, scl); in i2c_generic_scl_recovery()
270 if (bri->set_sda) in i2c_generic_scl_recovery()
271 bri->set_sda(adap, scl); in i2c_generic_scl_recovery()
282 if (ret == -EOPNOTSUPP) in i2c_generic_scl_recovery()
285 if (bri->unprepare_recovery) in i2c_generic_scl_recovery()
286 bri->unprepare_recovery(adap); in i2c_generic_scl_recovery()
287 if (bri->pinctrl) in i2c_generic_scl_recovery()
288 pinctrl_select_state(bri->pinctrl, bri->pins_default); in i2c_generic_scl_recovery()
296 if (!adap->bus_recovery_info) in i2c_recover_bus()
297 return -EBUSY; in i2c_recover_bus()
299 dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); in i2c_recover_bus()
300 return adap->bus_recovery_info->recover_bus(adap); in i2c_recover_bus()
306 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_gpio_init_pinctrl_recovery()
307 struct device *dev = &adap->dev; in i2c_gpio_init_pinctrl_recovery()
308 struct pinctrl *p = bri->pinctrl ?: dev_pinctrl(dev->parent); in i2c_gpio_init_pinctrl_recovery()
310 bri->pinctrl = p; in i2c_gpio_init_pinctrl_recovery()
317 bri->pins_default = NULL; in i2c_gpio_init_pinctrl_recovery()
318 bri->pins_gpio = NULL; in i2c_gpio_init_pinctrl_recovery()
322 if (!bri->pins_default) { in i2c_gpio_init_pinctrl_recovery()
323 bri->pins_default = pinctrl_lookup_state(p, in i2c_gpio_init_pinctrl_recovery()
325 if (IS_ERR(bri->pins_default)) { in i2c_gpio_init_pinctrl_recovery()
327 bri->pins_default = NULL; in i2c_gpio_init_pinctrl_recovery()
330 if (!bri->pins_gpio) { in i2c_gpio_init_pinctrl_recovery()
331 bri->pins_gpio = pinctrl_lookup_state(p, "gpio"); in i2c_gpio_init_pinctrl_recovery()
332 if (IS_ERR(bri->pins_gpio)) in i2c_gpio_init_pinctrl_recovery()
333 bri->pins_gpio = pinctrl_lookup_state(p, "recovery"); in i2c_gpio_init_pinctrl_recovery()
335 if (IS_ERR(bri->pins_gpio)) { in i2c_gpio_init_pinctrl_recovery()
337 bri->pins_gpio = NULL; in i2c_gpio_init_pinctrl_recovery()
342 if (bri->pins_default && bri->pins_gpio) { in i2c_gpio_init_pinctrl_recovery()
345 bri->pinctrl = NULL; in i2c_gpio_init_pinctrl_recovery()
346 bri->pins_default = NULL; in i2c_gpio_init_pinctrl_recovery()
347 bri->pins_gpio = NULL; in i2c_gpio_init_pinctrl_recovery()
353 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_gpio_init_generic_recovery()
354 struct device *dev = &adap->dev; in i2c_gpio_init_generic_recovery()
362 if (bri->recover_bus && bri->recover_bus != i2c_generic_scl_recovery) in i2c_gpio_init_generic_recovery()
369 if (bri->pinctrl) in i2c_gpio_init_generic_recovery()
370 pinctrl_select_state(bri->pinctrl, bri->pins_gpio); in i2c_gpio_init_generic_recovery()
376 if (!bri->scl_gpiod) { in i2c_gpio_init_generic_recovery()
378 if (PTR_ERR(gpiod) == -EPROBE_DEFER) { in i2c_gpio_init_generic_recovery()
379 ret = -EPROBE_DEFER; in i2c_gpio_init_generic_recovery()
383 bri->scl_gpiod = gpiod; in i2c_gpio_init_generic_recovery()
384 bri->recover_bus = i2c_generic_scl_recovery; in i2c_gpio_init_generic_recovery()
390 if (!bri->sda_gpiod) { in i2c_gpio_init_generic_recovery()
392 * We have SCL. Pull SCL low and wait a bit so that SDA glitches in i2c_gpio_init_generic_recovery()
395 gpiod_direction_output(bri->scl_gpiod, 0); in i2c_gpio_init_generic_recovery()
399 /* Wait a bit in case of a SDA glitch, and then release SCL. */ in i2c_gpio_init_generic_recovery()
401 gpiod_direction_output(bri->scl_gpiod, 1); in i2c_gpio_init_generic_recovery()
403 if (PTR_ERR(gpiod) == -EPROBE_DEFER) { in i2c_gpio_init_generic_recovery()
404 ret = -EPROBE_DEFER; in i2c_gpio_init_generic_recovery()
408 bri->sda_gpiod = gpiod; in i2c_gpio_init_generic_recovery()
413 if (bri->pinctrl) in i2c_gpio_init_generic_recovery()
414 pinctrl_select_state(bri->pinctrl, bri->pins_default); in i2c_gpio_init_generic_recovery()
427 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_init_recovery()
434 if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER) in i2c_init_recovery()
435 return -EPROBE_DEFER; in i2c_init_recovery()
437 if (!bri->recover_bus) { in i2c_init_recovery()
443 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) { in i2c_init_recovery()
444 bri->get_scl = get_scl_gpio_value; in i2c_init_recovery()
445 bri->set_scl = set_scl_gpio_value; in i2c_init_recovery()
446 if (bri->sda_gpiod) { in i2c_init_recovery()
447 bri->get_sda = get_sda_gpio_value; in i2c_init_recovery()
449 if (gpiod_get_direction(bri->sda_gpiod) == 0) in i2c_init_recovery()
450 bri->set_sda = set_sda_gpio_value; in i2c_init_recovery()
452 } else if (bri->recover_bus == i2c_generic_scl_recovery) { in i2c_init_recovery()
454 if (!bri->set_scl || !bri->get_scl) { in i2c_init_recovery()
458 if (!bri->set_sda && !bri->get_sda) { in i2c_init_recovery()
467 dev_err(&adap->dev, "Not using recovery: %s\n", err_str); in i2c_init_recovery()
469 dev_dbg(&adap->dev, "Not using recovery: %s\n", err_str); in i2c_init_recovery()
470 adap->bus_recovery_info = NULL; in i2c_init_recovery()
472 return -EINVAL; in i2c_init_recovery()
477 struct i2c_adapter *adap = client->adapter; in i2c_smbus_host_notify_to_irq()
480 if (!adap->host_notify_domain) in i2c_smbus_host_notify_to_irq()
481 return -ENXIO; in i2c_smbus_host_notify_to_irq()
483 if (client->flags & I2C_CLIENT_TEN) in i2c_smbus_host_notify_to_irq()
484 return -EINVAL; in i2c_smbus_host_notify_to_irq()
486 irq = irq_create_mapping(adap->host_notify_domain, client->addr); in i2c_smbus_host_notify_to_irq()
488 return irq > 0 ? irq : -ENXIO; in i2c_smbus_host_notify_to_irq()
501 client->irq = client->init_irq; in i2c_device_probe()
503 if (!client->irq) { in i2c_device_probe()
504 int irq = -ENOENT; in i2c_device_probe()
506 if (client->flags & I2C_CLIENT_HOST_NOTIFY) { in i2c_device_probe()
509 pm_runtime_get_sync(&client->adapter->dev); in i2c_device_probe()
511 } else if (dev->of_node) { in i2c_device_probe()
512 irq = of_irq_get_byname(dev->of_node, "irq"); in i2c_device_probe()
513 if (irq == -EINVAL || irq == -ENODATA) in i2c_device_probe()
514 irq = of_irq_get(dev->of_node, 0); in i2c_device_probe()
520 client->flags |= I2C_CLIENT_WAKE; in i2c_device_probe()
522 if (irq == -EPROBE_DEFER) { in i2c_device_probe()
530 client->irq = irq; in i2c_device_probe()
533 driver = to_i2c_driver(dev->driver); in i2c_device_probe()
539 if (!driver->id_table && in i2c_device_probe()
540 !acpi_driver_match_device(dev, dev->driver) && in i2c_device_probe()
541 !i2c_of_match_device(dev->driver->of_match_table, client)) { in i2c_device_probe()
542 status = -ENODEV; in i2c_device_probe()
546 if (client->flags & I2C_CLIENT_WAKE) { in i2c_device_probe()
549 wakeirq = of_irq_get_byname(dev->of_node, "wakeup"); in i2c_device_probe()
550 if (wakeirq == -EPROBE_DEFER) { in i2c_device_probe()
555 device_init_wakeup(&client->dev, true); in i2c_device_probe()
557 if (wakeirq > 0 && wakeirq != client->irq) in i2c_device_probe()
559 else if (client->irq > 0) in i2c_device_probe()
560 status = dev_pm_set_wake_irq(dev, client->irq); in i2c_device_probe()
565 dev_warn(&client->dev, "failed to set up wakeup irq\n"); in i2c_device_probe()
570 status = of_clk_set_defaults(dev->of_node, false); in i2c_device_probe()
575 status = dev_pm_domain_attach(&client->dev, do_power_on); in i2c_device_probe()
579 client->devres_group_id = devres_open_group(&client->dev, NULL, in i2c_device_probe()
581 if (!client->devres_group_id) { in i2c_device_probe()
582 status = -ENOMEM; in i2c_device_probe()
586 if (driver->probe) in i2c_device_probe()
587 status = driver->probe(client); in i2c_device_probe()
589 status = -EINVAL; in i2c_device_probe()
605 devres_release_group(&client->dev, client->devres_group_id); in i2c_device_probe()
607 dev_pm_domain_detach(&client->dev, do_power_on); in i2c_device_probe()
609 dev_pm_clear_wake_irq(&client->dev); in i2c_device_probe()
610 device_init_wakeup(&client->dev, false); in i2c_device_probe()
612 if (client->flags & I2C_CLIENT_HOST_NOTIFY) in i2c_device_probe()
613 pm_runtime_put_sync(&client->adapter->dev); in i2c_device_probe()
623 driver = to_i2c_driver(dev->driver); in i2c_device_remove()
624 if (driver->remove) { in i2c_device_remove()
627 driver->remove(client); in i2c_device_remove()
630 devres_release_group(&client->dev, client->devres_group_id); in i2c_device_remove()
632 dev_pm_domain_detach(&client->dev, true); in i2c_device_remove()
634 dev_pm_clear_wake_irq(&client->dev); in i2c_device_remove()
635 device_init_wakeup(&client->dev, false); in i2c_device_remove()
637 client->irq = 0; in i2c_device_remove()
638 if (client->flags & I2C_CLIENT_HOST_NOTIFY) in i2c_device_remove()
639 pm_runtime_put(&client->adapter->dev); in i2c_device_remove()
647 if (!client || !dev->driver) in i2c_device_shutdown()
649 driver = to_i2c_driver(dev->driver); in i2c_device_shutdown()
650 if (driver->shutdown) in i2c_device_shutdown()
651 driver->shutdown(client); in i2c_device_shutdown()
652 else if (client->irq > 0) in i2c_device_shutdown()
653 disable_irq(client->irq); in i2c_device_shutdown()
664 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? in name_show()
665 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); in name_show()
676 if (len != -ENODEV) in modalias_show()
679 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); in modalias_show()
680 if (len != -ENODEV) in modalias_show()
683 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); in modalias_show()
713 * i2c_verify_client - return parameter as i2c_client, or NULL
719 * by wrongly treating some non-I2C device as an i2c_client.
723 return (dev->type == &i2c_client_type) in i2c_verify_client()
733 unsigned short addr = client->addr; in i2c_encode_flags_to_addr()
736 if (client->flags & I2C_CLIENT_TEN) in i2c_encode_flags_to_addr()
739 if (client->flags & I2C_CLIENT_SLAVE) in i2c_encode_flags_to_addr()
750 /* 10-bit address, all values are valid */ in i2c_check_addr_validity()
752 return -EINVAL; in i2c_check_addr_validity()
754 /* 7-bit address, reject the general call address */ in i2c_check_addr_validity()
756 return -EINVAL; in i2c_check_addr_validity()
762 * device uses a reserved address, then it shouldn't be probed. 7-bit
763 * addressing is assumed, 10-bit address devices are rare and should be
773 * 0x04-0x07 Hs-mode master code in i2c_check_7bit_addr_validity_strict()
774 * 0x78-0x7b 10-bit slave addressing in i2c_check_7bit_addr_validity_strict()
775 * 0x7c-0x7f Reserved for future purposes in i2c_check_7bit_addr_validity_strict()
778 return -EINVAL; in i2c_check_7bit_addr_validity_strict()
788 return -EBUSY; in __i2c_check_addr_busy()
798 result = device_for_each_child(&adapter->dev, &addr, in i2c_check_mux_parents()
812 if (dev->type == &i2c_adapter_type) in i2c_check_mux_children()
830 result = device_for_each_child(&adapter->dev, &addr, in i2c_check_addr_busy()
837 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
845 rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter)); in i2c_adapter_lock_bus()
849 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
857 return rt_mutex_trylock(&adapter->bus_lock); in i2c_adapter_trylock_bus()
861 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
869 rt_mutex_unlock(&adapter->bus_lock); in i2c_adapter_unlock_bus()
876 struct acpi_device *adev = ACPI_COMPANION(&client->dev); in i2c_dev_set_name()
878 if (info && info->dev_name) { in i2c_dev_set_name()
879 dev_set_name(&client->dev, "i2c-%s", info->dev_name); in i2c_dev_set_name()
884 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev)); in i2c_dev_set_name()
888 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), in i2c_dev_set_name()
904 if (r->flags & IORESOURCE_BITS) { in i2c_dev_irq_from_resources()
905 irqd = irq_get_irq_data(r->start); in i2c_dev_irq_from_resources()
909 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); in i2c_dev_irq_from_resources()
912 return r->start; in i2c_dev_irq_from_resources()
920 * and by auto-detection
926 test_and_set_bit(addr, adap->addrs_in_instantiation)) in i2c_lock_addr()
927 return -EBUSY; in i2c_lock_addr()
936 clear_bit(addr, adap->addrs_in_instantiation); in i2c_unlock_addr()
940 * i2c_new_client_device - instantiate an i2c device
964 return ERR_PTR(-ENOMEM); in i2c_new_client_device()
966 client->adapter = adap; in i2c_new_client_device()
968 client->dev.platform_data = info->platform_data; in i2c_new_client_device()
969 client->flags = info->flags; in i2c_new_client_device()
970 client->addr = info->addr; in i2c_new_client_device()
972 client->init_irq = info->irq; in i2c_new_client_device()
973 if (!client->init_irq) in i2c_new_client_device()
974 client->init_irq = i2c_dev_irq_from_resources(info->resources, in i2c_new_client_device()
975 info->num_resources); in i2c_new_client_device()
977 strscpy(client->name, info->type, sizeof(client->name)); in i2c_new_client_device()
979 status = i2c_check_addr_validity(client->addr, client->flags); in i2c_new_client_device()
981 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", in i2c_new_client_device()
982 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); in i2c_new_client_device()
986 status = i2c_lock_addr(adap, client->addr, client->flags); in i2c_new_client_device()
995 client->dev.parent = &client->adapter->dev; in i2c_new_client_device()
996 client->dev.bus = &i2c_bus_type; in i2c_new_client_device()
997 client->dev.type = &i2c_client_type; in i2c_new_client_device()
998 client->dev.of_node = of_node_get(info->of_node); in i2c_new_client_device()
999 client->dev.fwnode = info->fwnode; in i2c_new_client_device()
1001 device_enable_async_suspend(&client->dev); in i2c_new_client_device()
1003 if (info->swnode) { in i2c_new_client_device()
1004 status = device_add_software_node(&client->dev, info->swnode); in i2c_new_client_device()
1006 dev_err(&adap->dev, in i2c_new_client_device()
1008 client->name, status); in i2c_new_client_device()
1014 status = device_register(&client->dev); in i2c_new_client_device()
1018 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", in i2c_new_client_device()
1019 client->name, dev_name(&client->dev)); in i2c_new_client_device()
1021 i2c_unlock_addr(adap, client->addr, client->flags); in i2c_new_client_device()
1026 device_remove_software_node(&client->dev); in i2c_new_client_device()
1029 of_node_put(info->of_node); in i2c_new_client_device()
1031 dev_err(&adap->dev, in i2c_new_client_device()
1033 client->name, client->addr, status); in i2c_new_client_device()
1034 i2c_unlock_addr(adap, client->addr, client->flags); in i2c_new_client_device()
1037 put_device(&client->dev); in i2c_new_client_device()
1045 * i2c_unregister_device - reverse effect of i2c_new_*_device()
1054 if (client->dev.of_node) { in i2c_unregister_device()
1055 of_node_clear_flag(client->dev.of_node, OF_POPULATED); in i2c_unregister_device()
1056 of_node_put(client->dev.of_node); in i2c_unregister_device()
1059 if (ACPI_COMPANION(&client->dev)) in i2c_unregister_device()
1060 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev)); in i2c_unregister_device()
1061 device_remove_software_node(&client->dev); in i2c_unregister_device()
1062 device_unregister(&client->dev); in i2c_unregister_device()
1067 * i2c_find_device_by_fwnode() - find an i2c_client for the fwnode
1073 * The user must call put_device(&client->dev) once done with the i2c client.
1114 * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
1147 * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
1153 * This is the device-managed version of @i2c_new_dummy_device. It returns the
1176 * i2c_new_ancillary_device - Helper to get the instantiated secondary address
1191 * On DT-based platforms the address is retrieved from the "reg" property entry
1192 * cell whose "reg-names" value matches the slave name.
1201 struct device_node *np = client->dev.of_node; in i2c_new_ancillary_device()
1206 i = of_property_match_string(np, "reg-names", name); in i2c_new_ancillary_device()
1211 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr); in i2c_new_ancillary_device()
1212 return i2c_new_dummy_device(client->adapter, addr); in i2c_new_ancillary_device()
1216 /* ------------------------------------------------------------------------- */
1218 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1223 complete(&adap->dev_released); in i2c_adapter_dev_release()
1231 for (parent = adapter->dev.parent; parent; parent = parent->parent) in i2c_adapter_depth()
1232 if (parent->type == &i2c_adapter_type) in i2c_adapter_depth()
1267 return -EINVAL; in new_device_store()
1269 if (blank - buf > I2C_NAME_SIZE - 1) { in new_device_store()
1271 return -EINVAL; in new_device_store()
1273 memcpy(info.type, buf, blank - buf); in new_device_store()
1279 return -EINVAL; in new_device_store()
1283 return -EINVAL; in new_device_store()
1301 mutex_lock(&adap->userspace_clients_lock); in new_device_store()
1302 list_add_tail(&client->detected, &adap->userspace_clients); in new_device_store()
1303 mutex_unlock(&adap->userspace_clients_lock); in new_device_store()
1334 return -EINVAL; in delete_device_store()
1338 return -EINVAL; in delete_device_store()
1342 res = -ENOENT; in delete_device_store()
1343 mutex_lock_nested(&adap->userspace_clients_lock, in delete_device_store()
1345 list_for_each_entry_safe(client, next, &adap->userspace_clients, in delete_device_store()
1349 "delete_device", client->name, client->addr); in delete_device_store()
1351 list_del(&client->detected); in delete_device_store()
1357 mutex_unlock(&adap->userspace_clients_lock); in delete_device_store()
1382 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1388 * by wrongly treating some non-I2C device as an i2c_adapter.
1392 return (dev->type == &i2c_adapter_type) in i2c_verify_adapter()
1404 if (devinfo->busnum == adapter->nr && in i2c_scan_static_board_info()
1405 IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info))) in i2c_scan_static_board_info()
1406 dev_err(&adapter->dev, in i2c_scan_static_board_info()
1408 devinfo->board_info.addr); in i2c_scan_static_board_info()
1435 struct irq_domain *domain = adap->host_notify_domain; in i2c_host_notify_irq_teardown()
1445 adap->host_notify_domain = NULL; in i2c_host_notify_irq_teardown()
1468 domain = irq_domain_create_linear(adap->dev.parent->fwnode, in i2c_setup_host_notify_irq_domain()
1472 return -ENOMEM; in i2c_setup_host_notify_irq_domain()
1474 adap->host_notify_domain = domain; in i2c_setup_host_notify_irq_domain()
1480 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1494 return -EINVAL; in i2c_handle_smbus_host_notify()
1496 dev_dbg(&adap->dev, "Detected HostNotify from address 0x%02x", addr); in i2c_handle_smbus_host_notify()
1498 irq = irq_find_mapping(adap->host_notify_domain, addr); in i2c_handle_smbus_host_notify()
1500 return -ENXIO; in i2c_handle_smbus_host_notify()
1510 int res = -EINVAL; in i2c_register_adapter()
1514 res = -EAGAIN; in i2c_register_adapter()
1519 if (WARN(!adap->name[0], "i2c adapter has no name")) in i2c_register_adapter()
1522 if (!adap->algo) { in i2c_register_adapter()
1523 pr_err("adapter '%s': no algo supplied!\n", adap->name); in i2c_register_adapter()
1527 if (!adap->lock_ops) in i2c_register_adapter()
1528 adap->lock_ops = &i2c_adapter_lock_ops; in i2c_register_adapter()
1530 adap->locked_flags = 0; in i2c_register_adapter()
1531 rt_mutex_init(&adap->bus_lock); in i2c_register_adapter()
1532 rt_mutex_init(&adap->mux_lock); in i2c_register_adapter()
1533 mutex_init(&adap->userspace_clients_lock); in i2c_register_adapter()
1534 INIT_LIST_HEAD(&adap->userspace_clients); in i2c_register_adapter()
1537 if (adap->timeout == 0) in i2c_register_adapter()
1538 adap->timeout = HZ; in i2c_register_adapter()
1544 adap->name, res); in i2c_register_adapter()
1548 dev_set_name(&adap->dev, "i2c-%d", adap->nr); in i2c_register_adapter()
1549 adap->dev.bus = &i2c_bus_type; in i2c_register_adapter()
1550 adap->dev.type = &i2c_adapter_type; in i2c_register_adapter()
1551 device_initialize(&adap->dev); in i2c_register_adapter()
1555 * setup runtime-pm (especially ignore-children) before hand. in i2c_register_adapter()
1557 device_enable_async_suspend(&adap->dev); in i2c_register_adapter()
1558 pm_runtime_no_callbacks(&adap->dev); in i2c_register_adapter()
1559 pm_suspend_ignore_children(&adap->dev, true); in i2c_register_adapter()
1560 pm_runtime_enable(&adap->dev); in i2c_register_adapter()
1562 res = device_add(&adap->dev); in i2c_register_adapter()
1564 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res); in i2c_register_adapter()
1568 adap->debugfs = debugfs_create_dir(dev_name(&adap->dev), i2c_debugfs_root); in i2c_register_adapter()
1575 if (res == -EPROBE_DEFER) in i2c_register_adapter()
1578 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); in i2c_register_adapter()
1580 /* create pre-declared device nodes */ in i2c_register_adapter()
1585 if (adap->nr < __i2c_first_dynamic_bus_num) in i2c_register_adapter()
1596 debugfs_remove_recursive(adap->debugfs); in i2c_register_adapter()
1597 init_completion(&adap->dev_released); in i2c_register_adapter()
1598 device_unregister(&adap->dev); in i2c_register_adapter()
1599 wait_for_completion(&adap->dev_released); in i2c_register_adapter()
1602 idr_remove(&i2c_adapter_idr, adap->nr); in i2c_register_adapter()
1608 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1609 * @adap: the adapter to register (with adap->nr initialized)
1619 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL); in __i2c_add_numbered_adapter()
1622 return id == -ENOSPC ? -EBUSY : id; in __i2c_add_numbered_adapter()
1628 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1638 * in adap->nr, and the specified adapter became available for clients.
1643 struct device *dev = &adapter->dev; in i2c_add_adapter()
1646 if (dev->of_node) { in i2c_add_adapter()
1647 id = of_alias_get_id(dev->of_node, "i2c"); in i2c_add_adapter()
1649 adapter->nr = id; in i2c_add_adapter()
1661 adapter->nr = id; in i2c_add_adapter()
1668 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1669 * @adap: the adapter to register (with adap->nr initialized)
1673 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1677 * If the requested bus number is set to -1, then this function will behave
1680 * If no devices have pre-been declared for this bus, then be sure to
1685 * clients using the bus number provided in adap->nr. Also, the table
1686 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1692 if (adap->nr == -1) /* -1 means dynamically assign bus id */ in i2c_add_numbered_adapter()
1706 list_for_each_entry_safe(client, _n, &driver->clients, detected) { in i2c_do_del_adapter()
1707 if (client->adapter == adapter) { in i2c_do_del_adapter()
1708 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", in i2c_do_del_adapter()
1709 client->name, client->addr); in i2c_do_del_adapter()
1710 list_del(&client->detected); in i2c_do_del_adapter()
1719 if (client && strcmp(client->name, "dummy")) in __unregister_client()
1738 * i2c_del_adapter - unregister I2C adapter
1752 found = idr_find(&i2c_adapter_idr, adap->nr); in i2c_del_adapter()
1755 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name); in i2c_del_adapter()
1767 mutex_lock_nested(&adap->userspace_clients_lock, in i2c_del_adapter()
1769 list_for_each_entry_safe(client, next, &adap->userspace_clients, in i2c_del_adapter()
1771 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, in i2c_del_adapter()
1772 client->addr); in i2c_del_adapter()
1773 list_del(&client->detected); in i2c_del_adapter()
1776 mutex_unlock(&adap->userspace_clients_lock); in i2c_del_adapter()
1779 * check the returned value. This is a two-pass process, because in i2c_del_adapter()
1783 device_for_each_child(&adap->dev, NULL, __unregister_client); in i2c_del_adapter()
1784 device_for_each_child(&adap->dev, NULL, __unregister_dummy); in i2c_del_adapter()
1787 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); in i2c_del_adapter()
1789 pm_runtime_disable(&adap->dev); in i2c_del_adapter()
1793 debugfs_remove_recursive(adap->debugfs); in i2c_del_adapter()
1795 /* wait until all references to the device are gone in i2c_del_adapter()
1802 init_completion(&adap->dev_released); in i2c_del_adapter()
1803 device_unregister(&adap->dev); in i2c_del_adapter()
1804 wait_for_completion(&adap->dev_released); in i2c_del_adapter()
1808 idr_remove(&i2c_adapter_idr, adap->nr); in i2c_del_adapter()
1813 memset(&adap->dev, 0, sizeof(adap->dev)); in i2c_del_adapter()
1823 * devm_i2c_add_adapter - device-managed variant of i2c_add_adapter()
1848 if (dev->parent && dev_fwnode(dev->parent) == data) in i2c_dev_or_parent_fwnode_match()
1855 * i2c_find_adapter_by_fwnode() - find an i2c_adapter for the fwnode
1861 * The user must call put_device(&adapter->dev) once done with the i2c adapter.
1885 * i2c_get_adapter_by_fwnode() - find an i2c_adapter for the fwnode
1903 if (!try_module_get(adapter->owner)) { in i2c_get_adapter_by_fwnode()
1904 put_device(&adapter->dev); in i2c_get_adapter_by_fwnode()
1925 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1945 i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz, in i2c_parse_fw_timings()
1948 d = t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ ? 1000 : in i2c_parse_fw_timings()
1949 t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120; in i2c_parse_fw_timings()
1950 i2c_parse_timing(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns, d, u); in i2c_parse_fw_timings()
1952 d = t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120; in i2c_parse_fw_timings()
1953 i2c_parse_timing(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns, d, u); in i2c_parse_fw_timings()
1955 i2c_parse_timing(dev, "i2c-scl-internal-delay-ns", in i2c_parse_fw_timings()
1956 &t->scl_int_delay_ns, 0, u); in i2c_parse_fw_timings()
1957 i2c_parse_timing(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns, in i2c_parse_fw_timings()
1958 t->scl_fall_ns, u); in i2c_parse_fw_timings()
1959 i2c_parse_timing(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns, 0, u); in i2c_parse_fw_timings()
1960 i2c_parse_timing(dev, "i2c-digital-filter-width-ns", in i2c_parse_fw_timings()
1961 &t->digital_filter_width_ns, 0, u); in i2c_parse_fw_timings()
1962 i2c_parse_timing(dev, "i2c-analog-filter-cutoff-frequency", in i2c_parse_fw_timings()
1963 &t->analog_filter_cutoff_freq_hz, 0, u); in i2c_parse_fw_timings()
1967 /* ------------------------------------------------------------------------- */
1983 if (dev->type != &i2c_adapter_type) in __process_new_driver()
1999 return -EAGAIN; in i2c_register_driver()
2002 driver->driver.owner = owner; in i2c_register_driver()
2003 driver->driver.bus = &i2c_bus_type; in i2c_register_driver()
2004 INIT_LIST_HEAD(&driver->clients); in i2c_register_driver()
2007 * will have called probe() for all matching-but-unbound devices. in i2c_register_driver()
2009 res = driver_register(&driver->driver); in i2c_register_driver()
2013 pr_debug("driver [%s] registered\n", driver->driver.name); in i2c_register_driver()
2024 if (dev->type == &i2c_adapter_type) in __process_removed_driver()
2030 * i2c_del_driver - unregister I2C driver
2038 driver_unregister(&driver->driver); in i2c_del_driver()
2039 pr_debug("driver [%s] unregistered\n", driver->driver.name); in i2c_del_driver()
2043 /* ------------------------------------------------------------------------- */
2056 if (!client || !client->dev.driver) in i2c_cmd()
2059 driver = to_i2c_driver(client->dev.driver); in i2c_cmd()
2060 if (driver->command) in i2c_cmd()
2061 driver->command(client, arg->cmd, arg->arg); in i2c_cmd()
2071 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); in i2c_clients_command()
2129 /* ----------------------------------------------------
2131 * ----------------------------------------------------
2139 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n", in i2c_quirk_error()
2140 err_msg, msg->addr, msg->len, in i2c_quirk_error()
2141 msg->flags & I2C_M_RD ? "read" : "write"); in i2c_quirk_error()
2142 return -EOPNOTSUPP; in i2c_quirk_error()
2147 const struct i2c_adapter_quirks *q = adap->quirks; in i2c_check_for_quirks()
2148 int max_num = q->max_num_msgs, i; in i2c_check_for_quirks()
2151 if (q->flags & I2C_AQ_COMB) { in i2c_check_for_quirks()
2156 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD) in i2c_check_for_quirks()
2159 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD)) in i2c_check_for_quirks()
2162 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr) in i2c_check_for_quirks()
2165 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len)) in i2c_check_for_quirks()
2168 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len)) in i2c_check_for_quirks()
2182 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len)) in i2c_check_for_quirks()
2185 if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0) in i2c_check_for_quirks()
2188 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len)) in i2c_check_for_quirks()
2191 if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0) in i2c_check_for_quirks()
2200 * __i2c_transfer - unlocked flavor of i2c_transfer
2216 if (!adap->algo->master_xfer) { in __i2c_transfer()
2217 dev_dbg(&adap->dev, "I2C level transfers not supported\n"); in __i2c_transfer()
2218 return -EOPNOTSUPP; in __i2c_transfer()
2222 return -EINVAL; in __i2c_transfer()
2228 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) in __i2c_transfer()
2229 return -EOPNOTSUPP; in __i2c_transfer()
2233 * enabled. This is an efficient way of keeping the for-loop from in __i2c_transfer()
2247 for (ret = 0, try = 0; try <= adap->retries; try++) { in __i2c_transfer()
2248 if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic) in __i2c_transfer()
2249 ret = adap->algo->master_xfer_atomic(adap, msgs, num); in __i2c_transfer()
2251 ret = adap->algo->master_xfer(adap, msgs, num); in __i2c_transfer()
2253 if (ret != -EAGAIN) in __i2c_transfer()
2255 if (time_after(jiffies, orig_jiffies + adap->timeout)) in __i2c_transfer()
2272 * i2c_transfer - execute a single or combined I2C message
2289 * - When we get an error after receiving N bytes from a slave, in i2c_transfer()
2292 * - When we get a NAK after transmitting N bytes to a slave, in i2c_transfer()
2297 * - When for example "num" is two and we successfully complete in i2c_transfer()
2315 * i2c_transfer_buffer_flags - issue a single I2C message transferring data
2329 .addr = client->addr, in i2c_transfer_buffer_flags()
2330 .flags = flags | (client->flags & I2C_M_TEN), in i2c_transfer_buffer_flags()
2335 ret = i2c_transfer(client->adapter, &msg, 1); in i2c_transfer_buffer_flags()
2346 * i2c_get_device_id - get manufacturer, part id and die revision of a device
2355 struct i2c_adapter *adap = client->adapter; in i2c_get_device_id()
2360 return -EOPNOTSUPP; in i2c_get_device_id()
2364 I2C_SMBUS_READ, client->addr << 1, in i2c_get_device_id()
2369 id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4); in i2c_get_device_id()
2370 id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3); in i2c_get_device_id()
2371 id->die_revision = raw_id.block[3] & 0x7; in i2c_get_device_id()
2377 * i2c_client_get_device_id - get the driver match table entry of a device
2384 const struct i2c_driver *drv = to_i2c_driver(client->dev.driver); in i2c_client_get_device_id()
2386 return i2c_match_id(drv->id_table, client); in i2c_client_get_device_id()
2390 /* ----------------------------------------------------
2392 * Will not work for 10-bit addresses!
2393 * ----------------------------------------------------
2400 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2403 * On x86, there is another special case for FSC hardware monitoring chips,
2414 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON) in i2c_default_probe()
2428 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n", in i2c_default_probe()
2430 err = -EOPNOTSUPP; in i2c_default_probe()
2440 struct i2c_adapter *adapter = temp_client->adapter; in i2c_detect_address()
2441 int addr = temp_client->addr; in i2c_detect_address()
2447 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", in i2c_detect_address()
2463 err = driver->detect(temp_client, &info); in i2c_detect_address()
2465 /* -ENODEV is returned if the detection fails. We catch it in i2c_detect_address()
2467 return err == -ENODEV ? 0 : err; in i2c_detect_address()
2472 dev_err(&adapter->dev, in i2c_detect_address()
2474 driver->driver.name, addr); in i2c_detect_address()
2479 if (adapter->class & I2C_CLASS_DEPRECATED) in i2c_detect_address()
2480 dev_warn(&adapter->dev, in i2c_detect_address()
2483 "Check 'Documentation/i2c/instantiating-devices.rst' for details.\n", in i2c_detect_address()
2486 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", in i2c_detect_address()
2490 list_add_tail(&client->detected, &driver->clients); in i2c_detect_address()
2492 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", in i2c_detect_address()
2504 address_list = driver->address_list; in i2c_detect()
2505 if (!driver->detect || !address_list) in i2c_detect()
2509 if (adapter->class == I2C_CLASS_DEPRECATED) { in i2c_detect()
2510 dev_dbg(&adapter->dev, in i2c_detect()
2511 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. " in i2c_detect()
2512 "If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n", in i2c_detect()
2513 driver->driver.name); in i2c_detect()
2518 if (!(adapter->class & driver->class)) in i2c_detect()
2524 return -ENOMEM; in i2c_detect()
2525 temp_client->adapter = adapter; in i2c_detect()
2528 dev_dbg(&adapter->dev, in i2c_detect()
2531 temp_client->addr = address_list[i]; in i2c_detect()
2562 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n", in i2c_new_scanned_device()
2569 dev_dbg(&adap->dev, in i2c_new_scanned_device()
2581 dev_dbg(&adap->dev, "Probing failed, no device found\n"); in i2c_new_scanned_device()
2582 return ERR_PTR(-ENODEV); in i2c_new_scanned_device()
2585 info->addr = addr_list[i]; in i2c_new_scanned_device()
2599 if (try_module_get(adapter->owner)) in i2c_get_adapter()
2600 get_device(&adapter->dev); in i2c_get_adapter()
2615 module_put(adap->owner); in i2c_put_adapter()
2616 /* Should be last, otherwise we risk use-after-free with 'adap' */ in i2c_put_adapter()
2617 put_device(&adap->dev); in i2c_put_adapter()
2622 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2627 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2635 /* also skip 0-length msgs for bogus thresholds of 0 */ in i2c_get_dma_safe_msg_buf()
2638 msg->addr); in i2c_get_dma_safe_msg_buf()
2639 if (msg->len < threshold || msg->len == 0) in i2c_get_dma_safe_msg_buf()
2642 if (msg->flags & I2C_M_DMA_SAFE) in i2c_get_dma_safe_msg_buf()
2643 return msg->buf; in i2c_get_dma_safe_msg_buf()
2646 msg->addr, msg->len); in i2c_get_dma_safe_msg_buf()
2648 if (msg->flags & I2C_M_RD) in i2c_get_dma_safe_msg_buf()
2649 return kzalloc(msg->len, GFP_KERNEL); in i2c_get_dma_safe_msg_buf()
2651 return kmemdup(msg->buf, msg->len, GFP_KERNEL); in i2c_get_dma_safe_msg_buf()
2656 * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2663 if (!buf || buf == msg->buf) in i2c_put_dma_safe_msg_buf()
2666 if (xferred && msg->flags & I2C_M_RD) in i2c_put_dma_safe_msg_buf()
2667 memcpy(msg->buf, buf, msg->len); in i2c_put_dma_safe_msg_buf()
2673 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2674 MODULE_DESCRIPTION("I2C-Bus main module");