Lines Matching +full:ac +full:- +full:detect +full:- +full:gpios
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>
24 #include <linux/i2c-smbus.h>
43 #include "i2c-core.h"
85 while (id->name[0]) { in i2c_match_id()
86 if (strcmp(client->name, id->name) == 0) in i2c_match_id()
101 if (i2c_of_match_device(drv->of_match_table, client)) in i2c_device_match()
111 if (i2c_match_id(driver->id_table, client)) in i2c_device_match()
123 if (rc != -ENODEV) in i2c_device_uevent()
127 if (rc != -ENODEV) in i2c_device_uevent()
130 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name); in i2c_device_uevent()
136 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod); in get_scl_gpio_value()
141 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val); in set_scl_gpio_value()
146 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod); in get_sda_gpio_value()
151 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val); in set_sda_gpio_value()
156 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_generic_bus_free()
157 int ret = -EOPNOTSUPP; in i2c_generic_bus_free()
159 if (bri->get_bus_free) in i2c_generic_bus_free()
160 ret = bri->get_bus_free(adap); in i2c_generic_bus_free()
161 else if (bri->get_sda) in i2c_generic_bus_free()
162 ret = bri->get_sda(adap); in i2c_generic_bus_free()
167 return ret ? 0 : -EBUSY; in i2c_generic_bus_free()
180 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_generic_scl_recovery()
183 if (bri->prepare_recovery) in i2c_generic_scl_recovery()
184 bri->prepare_recovery(adap); in i2c_generic_scl_recovery()
185 if (bri->pinctrl) in i2c_generic_scl_recovery()
186 pinctrl_select_state(bri->pinctrl, bri->pins_gpio); in i2c_generic_scl_recovery()
195 bri->set_scl(adap, scl); in i2c_generic_scl_recovery()
197 if (bri->set_sda) in i2c_generic_scl_recovery()
198 bri->set_sda(adap, scl); in i2c_generic_scl_recovery()
202 * By this time SCL is high, as we need to give 9 falling-rising edges in i2c_generic_scl_recovery()
207 if (!bri->get_scl(adap)) { in i2c_generic_scl_recovery()
208 dev_err(&adap->dev, in i2c_generic_scl_recovery()
210 ret = -EBUSY; in i2c_generic_scl_recovery()
216 bri->set_scl(adap, scl); in i2c_generic_scl_recovery()
225 if (bri->set_sda) in i2c_generic_scl_recovery()
226 bri->set_sda(adap, scl); in i2c_generic_scl_recovery()
237 if (ret == -EOPNOTSUPP) in i2c_generic_scl_recovery()
240 if (bri->unprepare_recovery) in i2c_generic_scl_recovery()
241 bri->unprepare_recovery(adap); in i2c_generic_scl_recovery()
242 if (bri->pinctrl) in i2c_generic_scl_recovery()
243 pinctrl_select_state(bri->pinctrl, bri->pins_default); in i2c_generic_scl_recovery()
251 if (!adap->bus_recovery_info) in i2c_recover_bus()
252 return -EOPNOTSUPP; in i2c_recover_bus()
254 dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); in i2c_recover_bus()
255 return adap->bus_recovery_info->recover_bus(adap); in i2c_recover_bus()
261 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_gpio_init_pinctrl_recovery()
262 struct device *dev = &adap->dev; in i2c_gpio_init_pinctrl_recovery()
263 struct pinctrl *p = bri->pinctrl; in i2c_gpio_init_pinctrl_recovery()
270 bri->pins_default = NULL; in i2c_gpio_init_pinctrl_recovery()
271 bri->pins_gpio = NULL; in i2c_gpio_init_pinctrl_recovery()
275 if (!bri->pins_default) { in i2c_gpio_init_pinctrl_recovery()
276 bri->pins_default = pinctrl_lookup_state(p, in i2c_gpio_init_pinctrl_recovery()
278 if (IS_ERR(bri->pins_default)) { in i2c_gpio_init_pinctrl_recovery()
280 bri->pins_default = NULL; in i2c_gpio_init_pinctrl_recovery()
283 if (!bri->pins_gpio) { in i2c_gpio_init_pinctrl_recovery()
284 bri->pins_gpio = pinctrl_lookup_state(p, "gpio"); in i2c_gpio_init_pinctrl_recovery()
285 if (IS_ERR(bri->pins_gpio)) in i2c_gpio_init_pinctrl_recovery()
286 bri->pins_gpio = pinctrl_lookup_state(p, "recovery"); in i2c_gpio_init_pinctrl_recovery()
288 if (IS_ERR(bri->pins_gpio)) { in i2c_gpio_init_pinctrl_recovery()
290 bri->pins_gpio = NULL; in i2c_gpio_init_pinctrl_recovery()
295 if (bri->pins_default && bri->pins_gpio) { in i2c_gpio_init_pinctrl_recovery()
298 bri->pinctrl = NULL; in i2c_gpio_init_pinctrl_recovery()
299 bri->pins_default = NULL; in i2c_gpio_init_pinctrl_recovery()
300 bri->pins_gpio = NULL; in i2c_gpio_init_pinctrl_recovery()
306 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_gpio_init_generic_recovery()
307 struct device *dev = &adap->dev; in i2c_gpio_init_generic_recovery()
315 if (bri->recover_bus && bri->recover_bus != i2c_generic_scl_recovery) in i2c_gpio_init_generic_recovery()
322 if (bri->pinctrl) in i2c_gpio_init_generic_recovery()
323 pinctrl_select_state(bri->pinctrl, bri->pins_gpio); in i2c_gpio_init_generic_recovery()
329 if (!bri->scl_gpiod) { in i2c_gpio_init_generic_recovery()
331 if (PTR_ERR(gpiod) == -EPROBE_DEFER) { in i2c_gpio_init_generic_recovery()
332 ret = -EPROBE_DEFER; in i2c_gpio_init_generic_recovery()
336 bri->scl_gpiod = gpiod; in i2c_gpio_init_generic_recovery()
337 bri->recover_bus = i2c_generic_scl_recovery; in i2c_gpio_init_generic_recovery()
338 dev_info(dev, "using generic GPIOs for recovery\n"); in i2c_gpio_init_generic_recovery()
343 if (!bri->sda_gpiod) { in i2c_gpio_init_generic_recovery()
348 gpiod_direction_output(bri->scl_gpiod, 0); in i2c_gpio_init_generic_recovery()
354 gpiod_direction_output(bri->scl_gpiod, 1); in i2c_gpio_init_generic_recovery()
356 if (PTR_ERR(gpiod) == -EPROBE_DEFER) { in i2c_gpio_init_generic_recovery()
357 ret = -EPROBE_DEFER; in i2c_gpio_init_generic_recovery()
361 bri->sda_gpiod = gpiod; in i2c_gpio_init_generic_recovery()
366 if (bri->pinctrl) in i2c_gpio_init_generic_recovery()
367 pinctrl_select_state(bri->pinctrl, bri->pins_default); in i2c_gpio_init_generic_recovery()
380 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; in i2c_init_recovery()
386 if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER) in i2c_init_recovery()
387 return -EPROBE_DEFER; in i2c_init_recovery()
389 if (!bri->recover_bus) { in i2c_init_recovery()
394 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) { in i2c_init_recovery()
395 bri->get_scl = get_scl_gpio_value; in i2c_init_recovery()
396 bri->set_scl = set_scl_gpio_value; in i2c_init_recovery()
397 if (bri->sda_gpiod) { in i2c_init_recovery()
398 bri->get_sda = get_sda_gpio_value; in i2c_init_recovery()
400 if (gpiod_get_direction(bri->sda_gpiod) == 0) in i2c_init_recovery()
401 bri->set_sda = set_sda_gpio_value; in i2c_init_recovery()
403 } else if (bri->recover_bus == i2c_generic_scl_recovery) { in i2c_init_recovery()
405 if (!bri->set_scl || !bri->get_scl) { in i2c_init_recovery()
409 if (!bri->set_sda && !bri->get_sda) { in i2c_init_recovery()
417 dev_err(&adap->dev, "Not using recovery: %s\n", err_str); in i2c_init_recovery()
418 adap->bus_recovery_info = NULL; in i2c_init_recovery()
420 return -EINVAL; in i2c_init_recovery()
425 struct i2c_adapter *adap = client->adapter; in i2c_smbus_host_notify_to_irq()
428 if (!adap->host_notify_domain) in i2c_smbus_host_notify_to_irq()
429 return -ENXIO; in i2c_smbus_host_notify_to_irq()
431 if (client->flags & I2C_CLIENT_TEN) in i2c_smbus_host_notify_to_irq()
432 return -EINVAL; in i2c_smbus_host_notify_to_irq()
434 irq = irq_create_mapping(adap->host_notify_domain, client->addr); in i2c_smbus_host_notify_to_irq()
436 return irq > 0 ? irq : -ENXIO; in i2c_smbus_host_notify_to_irq()
448 client->irq = client->init_irq; in i2c_device_probe()
450 if (!client->irq) { in i2c_device_probe()
451 int irq = -ENOENT; in i2c_device_probe()
453 if (client->flags & I2C_CLIENT_HOST_NOTIFY) { in i2c_device_probe()
456 pm_runtime_get_sync(&client->adapter->dev); in i2c_device_probe()
458 } else if (dev->of_node) { in i2c_device_probe()
459 irq = of_irq_get_byname(dev->of_node, "irq"); in i2c_device_probe()
460 if (irq == -EINVAL || irq == -ENODATA) in i2c_device_probe()
461 irq = of_irq_get(dev->of_node, 0); in i2c_device_probe()
465 if (irq == -EPROBE_DEFER) { in i2c_device_probe()
473 client->irq = irq; in i2c_device_probe()
476 driver = to_i2c_driver(dev->driver); in i2c_device_probe()
482 if (!driver->id_table && in i2c_device_probe()
483 !acpi_driver_match_device(dev, dev->driver) && in i2c_device_probe()
484 !i2c_of_match_device(dev->driver->of_match_table, client)) { in i2c_device_probe()
485 status = -ENODEV; in i2c_device_probe()
489 if (client->flags & I2C_CLIENT_WAKE) { in i2c_device_probe()
492 wakeirq = of_irq_get_byname(dev->of_node, "wakeup"); in i2c_device_probe()
493 if (wakeirq == -EPROBE_DEFER) { in i2c_device_probe()
498 device_init_wakeup(&client->dev, true); in i2c_device_probe()
500 if (wakeirq > 0 && wakeirq != client->irq) in i2c_device_probe()
502 else if (client->irq > 0) in i2c_device_probe()
503 status = dev_pm_set_wake_irq(dev, client->irq); in i2c_device_probe()
508 dev_warn(&client->dev, "failed to set up wakeup irq\n"); in i2c_device_probe()
513 status = of_clk_set_defaults(dev->of_node, false); in i2c_device_probe()
517 status = dev_pm_domain_attach(&client->dev, true); in i2c_device_probe()
525 if (driver->probe_new) in i2c_device_probe()
526 status = driver->probe_new(client); in i2c_device_probe()
527 else if (driver->probe) in i2c_device_probe()
528 status = driver->probe(client, in i2c_device_probe()
529 i2c_match_id(driver->id_table, client)); in i2c_device_probe()
531 status = -EINVAL; in i2c_device_probe()
539 dev_pm_domain_detach(&client->dev, true); in i2c_device_probe()
541 dev_pm_clear_wake_irq(&client->dev); in i2c_device_probe()
542 device_init_wakeup(&client->dev, false); in i2c_device_probe()
544 if (client->flags & I2C_CLIENT_HOST_NOTIFY) in i2c_device_probe()
545 pm_runtime_put_sync(&client->adapter->dev); in i2c_device_probe()
556 if (!client || !dev->driver) in i2c_device_remove()
559 driver = to_i2c_driver(dev->driver); in i2c_device_remove()
560 if (driver->remove) { in i2c_device_remove()
562 status = driver->remove(client); in i2c_device_remove()
565 dev_pm_domain_detach(&client->dev, true); in i2c_device_remove()
567 dev_pm_clear_wake_irq(&client->dev); in i2c_device_remove()
568 device_init_wakeup(&client->dev, false); in i2c_device_remove()
570 client->irq = 0; in i2c_device_remove()
571 if (client->flags & I2C_CLIENT_HOST_NOTIFY) in i2c_device_remove()
572 pm_runtime_put(&client->adapter->dev); in i2c_device_remove()
582 if (!client || !dev->driver) in i2c_device_shutdown()
584 driver = to_i2c_driver(dev->driver); in i2c_device_shutdown()
585 if (driver->shutdown) in i2c_device_shutdown()
586 driver->shutdown(client); in i2c_device_shutdown()
597 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? in name_show()
598 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); in name_show()
609 if (len != -ENODEV) in modalias_show()
612 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); in modalias_show()
613 if (len != -ENODEV) in modalias_show()
616 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); in modalias_show()
646 * i2c_verify_client - return parameter as i2c_client, or NULL
652 * by wrongly treating some non-I2C device as an i2c_client.
656 return (dev->type == &i2c_client_type) in i2c_verify_client()
666 unsigned short addr = client->addr; in i2c_encode_flags_to_addr()
669 if (client->flags & I2C_CLIENT_TEN) in i2c_encode_flags_to_addr()
672 if (client->flags & I2C_CLIENT_SLAVE) in i2c_encode_flags_to_addr()
683 /* 10-bit address, all values are valid */ in i2c_check_addr_validity()
685 return -EINVAL; in i2c_check_addr_validity()
687 /* 7-bit address, reject the general call address */ in i2c_check_addr_validity()
689 return -EINVAL; in i2c_check_addr_validity()
695 * device uses a reserved address, then it shouldn't be probed. 7-bit
696 * addressing is assumed, 10-bit address devices are rare and should be
706 * 0x04-0x07 Hs-mode master code in i2c_check_7bit_addr_validity_strict()
707 * 0x78-0x7b 10-bit slave addressing in i2c_check_7bit_addr_validity_strict()
708 * 0x7c-0x7f Reserved for future purposes in i2c_check_7bit_addr_validity_strict()
711 return -EINVAL; in i2c_check_7bit_addr_validity_strict()
721 return -EBUSY; in __i2c_check_addr_busy()
731 result = device_for_each_child(&adapter->dev, &addr, in i2c_check_mux_parents()
745 if (dev->type == &i2c_adapter_type) in i2c_check_mux_children()
763 result = device_for_each_child(&adapter->dev, &addr, in i2c_check_addr_busy()
770 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
778 rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter)); in i2c_adapter_lock_bus()
782 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
790 return rt_mutex_trylock(&adapter->bus_lock); in i2c_adapter_trylock_bus()
794 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
802 rt_mutex_unlock(&adapter->bus_lock); in i2c_adapter_unlock_bus()
809 struct acpi_device *adev = ACPI_COMPANION(&client->dev); in i2c_dev_set_name()
811 if (info && info->dev_name) { in i2c_dev_set_name()
812 dev_set_name(&client->dev, "i2c-%s", info->dev_name); in i2c_dev_set_name()
817 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev)); in i2c_dev_set_name()
821 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), in i2c_dev_set_name()
837 if (r->flags & IORESOURCE_BITS) { in i2c_dev_irq_from_resources()
838 irqd = irq_get_irq_data(r->start); in i2c_dev_irq_from_resources()
842 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); in i2c_dev_irq_from_resources()
845 return r->start; in i2c_dev_irq_from_resources()
852 * i2c_new_client_device - instantiate an i2c device
875 return ERR_PTR(-ENOMEM); in i2c_new_client_device()
877 client->adapter = adap; in i2c_new_client_device()
879 client->dev.platform_data = info->platform_data; in i2c_new_client_device()
880 client->flags = info->flags; in i2c_new_client_device()
881 client->addr = info->addr; in i2c_new_client_device()
883 client->init_irq = info->irq; in i2c_new_client_device()
884 if (!client->init_irq) in i2c_new_client_device()
885 client->init_irq = i2c_dev_irq_from_resources(info->resources, in i2c_new_client_device()
886 info->num_resources); in i2c_new_client_device()
888 strlcpy(client->name, info->type, sizeof(client->name)); in i2c_new_client_device()
890 status = i2c_check_addr_validity(client->addr, client->flags); in i2c_new_client_device()
892 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", in i2c_new_client_device()
893 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); in i2c_new_client_device()
902 client->dev.parent = &client->adapter->dev; in i2c_new_client_device()
903 client->dev.bus = &i2c_bus_type; in i2c_new_client_device()
904 client->dev.type = &i2c_client_type; in i2c_new_client_device()
905 client->dev.of_node = of_node_get(info->of_node); in i2c_new_client_device()
906 client->dev.fwnode = info->fwnode; in i2c_new_client_device()
910 if (info->properties) { in i2c_new_client_device()
911 status = device_add_properties(&client->dev, info->properties); in i2c_new_client_device()
913 dev_err(&adap->dev, in i2c_new_client_device()
915 client->name, status); in i2c_new_client_device()
920 status = device_register(&client->dev); in i2c_new_client_device()
924 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", in i2c_new_client_device()
925 client->name, dev_name(&client->dev)); in i2c_new_client_device()
930 if (info->properties) in i2c_new_client_device()
931 device_remove_properties(&client->dev); in i2c_new_client_device()
933 of_node_put(info->of_node); in i2c_new_client_device()
935 dev_err(&adap->dev, in i2c_new_client_device()
937 client->name, client->addr, status); in i2c_new_client_device()
945 * i2c_unregister_device - reverse effect of i2c_new_*_device()
954 if (client->dev.of_node) { in i2c_unregister_device()
955 of_node_clear_flag(client->dev.of_node, OF_POPULATED); in i2c_unregister_device()
956 of_node_put(client->dev.of_node); in i2c_unregister_device()
959 if (ACPI_COMPANION(&client->dev)) in i2c_unregister_device()
960 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev)); in i2c_unregister_device()
961 device_unregister(&client->dev); in i2c_unregister_device()
990 * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
1025 i2c_unregister_device(this->client); in devm_i2c_release_dummy()
1029 * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
1035 * This is the device-managed version of @i2c_new_dummy_device. It returns the
1047 return ERR_PTR(-ENOMEM); in devm_i2c_new_dummy_device()
1053 dr->client = client; in devm_i2c_new_dummy_device()
1062 * i2c_new_ancillary_device - Helper to get the instantiated secondary address
1077 * On DT-based platforms the address is retrieved from the "reg" property entry
1078 * cell whose "reg-names" value matches the slave name.
1087 struct device_node *np = client->dev.of_node; in i2c_new_ancillary_device()
1092 i = of_property_match_string(np, "reg-names", name); in i2c_new_ancillary_device()
1097 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr); in i2c_new_ancillary_device()
1098 return i2c_new_dummy_device(client->adapter, addr); in i2c_new_ancillary_device()
1102 /* ------------------------------------------------------------------------- */
1104 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1109 complete(&adap->dev_released); in i2c_adapter_dev_release()
1151 return -EINVAL; in new_device_store()
1153 if (blank - buf > I2C_NAME_SIZE - 1) { in new_device_store()
1155 return -EINVAL; in new_device_store()
1157 memcpy(info.type, buf, blank - buf); in new_device_store()
1163 return -EINVAL; in new_device_store()
1167 return -EINVAL; in new_device_store()
1185 mutex_lock(&adap->userspace_clients_lock); in new_device_store()
1186 list_add_tail(&client->detected, &adap->userspace_clients); in new_device_store()
1187 mutex_unlock(&adap->userspace_clients_lock); in new_device_store()
1218 return -EINVAL; in delete_device_store()
1222 return -EINVAL; in delete_device_store()
1226 res = -ENOENT; in delete_device_store()
1227 mutex_lock_nested(&adap->userspace_clients_lock, in delete_device_store()
1229 list_for_each_entry_safe(client, next, &adap->userspace_clients, in delete_device_store()
1233 "delete_device", client->name, client->addr); in delete_device_store()
1235 list_del(&client->detected); in delete_device_store()
1241 mutex_unlock(&adap->userspace_clients_lock); in delete_device_store()
1266 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1272 * by wrongly treating some non-I2C device as an i2c_adapter.
1276 return (dev->type == &i2c_adapter_type) in i2c_verify_adapter()
1292 if (devinfo->busnum == adapter->nr && in i2c_scan_static_board_info()
1293 IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info))) in i2c_scan_static_board_info()
1294 dev_err(&adapter->dev, in i2c_scan_static_board_info()
1296 devinfo->board_info.addr); in i2c_scan_static_board_info()
1304 /* Detect supported devices on that bus, and instantiate them */ in i2c_do_add_adapter()
1323 struct irq_domain *domain = adap->host_notify_domain; in i2c_host_notify_irq_teardown()
1333 adap->host_notify_domain = NULL; in i2c_host_notify_irq_teardown()
1356 domain = irq_domain_create_linear(adap->dev.parent->fwnode, in i2c_setup_host_notify_irq_domain()
1360 return -ENOMEM; in i2c_setup_host_notify_irq_domain()
1362 adap->host_notify_domain = domain; in i2c_setup_host_notify_irq_domain()
1368 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1382 return -EINVAL; in i2c_handle_smbus_host_notify()
1384 irq = irq_find_mapping(adap->host_notify_domain, addr); in i2c_handle_smbus_host_notify()
1386 return -ENXIO; in i2c_handle_smbus_host_notify()
1396 int res = -EINVAL; in i2c_register_adapter()
1400 res = -EAGAIN; in i2c_register_adapter()
1405 if (WARN(!adap->name[0], "i2c adapter has no name")) in i2c_register_adapter()
1408 if (!adap->algo) { in i2c_register_adapter()
1409 pr_err("adapter '%s': no algo supplied!\n", adap->name); in i2c_register_adapter()
1413 if (!adap->lock_ops) in i2c_register_adapter()
1414 adap->lock_ops = &i2c_adapter_lock_ops; in i2c_register_adapter()
1416 adap->locked_flags = 0; in i2c_register_adapter()
1417 rt_mutex_init(&adap->bus_lock); in i2c_register_adapter()
1418 rt_mutex_init(&adap->mux_lock); in i2c_register_adapter()
1419 mutex_init(&adap->userspace_clients_lock); in i2c_register_adapter()
1420 INIT_LIST_HEAD(&adap->userspace_clients); in i2c_register_adapter()
1423 if (adap->timeout == 0) in i2c_register_adapter()
1424 adap->timeout = HZ; in i2c_register_adapter()
1430 adap->name, res); in i2c_register_adapter()
1434 dev_set_name(&adap->dev, "i2c-%d", adap->nr); in i2c_register_adapter()
1435 adap->dev.bus = &i2c_bus_type; in i2c_register_adapter()
1436 adap->dev.type = &i2c_adapter_type; in i2c_register_adapter()
1437 res = device_register(&adap->dev); in i2c_register_adapter()
1439 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res); in i2c_register_adapter()
1447 pm_runtime_no_callbacks(&adap->dev); in i2c_register_adapter()
1448 pm_suspend_ignore_children(&adap->dev, true); in i2c_register_adapter()
1449 pm_runtime_enable(&adap->dev); in i2c_register_adapter()
1452 if (res == -EPROBE_DEFER) in i2c_register_adapter()
1455 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); in i2c_register_adapter()
1458 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, in i2c_register_adapter()
1459 adap->dev.parent); in i2c_register_adapter()
1461 dev_warn(&adap->dev, in i2c_register_adapter()
1465 /* create pre-declared device nodes */ in i2c_register_adapter()
1470 if (adap->nr < __i2c_first_dynamic_bus_num) in i2c_register_adapter()
1481 init_completion(&adap->dev_released); in i2c_register_adapter()
1482 device_unregister(&adap->dev); in i2c_register_adapter()
1483 wait_for_completion(&adap->dev_released); in i2c_register_adapter()
1486 idr_remove(&i2c_adapter_idr, adap->nr); in i2c_register_adapter()
1492 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1493 * @adap: the adapter to register (with adap->nr initialized)
1503 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL); in __i2c_add_numbered_adapter()
1506 return id == -ENOSPC ? -EBUSY : id; in __i2c_add_numbered_adapter()
1512 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1522 * in adap->nr, and the specified adapter became available for clients.
1527 struct device *dev = &adapter->dev; in i2c_add_adapter()
1530 if (dev->of_node) { in i2c_add_adapter()
1531 id = of_alias_get_id(dev->of_node, "i2c"); in i2c_add_adapter()
1533 adapter->nr = id; in i2c_add_adapter()
1545 adapter->nr = id; in i2c_add_adapter()
1552 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1553 * @adap: the adapter to register (with adap->nr initialized)
1557 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1561 * If the requested bus number is set to -1, then this function will behave
1564 * If no devices have pre-been declared for this bus, then be sure to
1569 * clients using the bus number provided in adap->nr. Also, the table
1570 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1576 if (adap->nr == -1) /* -1 means dynamically assign bus id */ in i2c_add_numbered_adapter()
1589 * probing (using a driver's detect method) */ in i2c_do_del_adapter()
1590 list_for_each_entry_safe(client, _n, &driver->clients, detected) { in i2c_do_del_adapter()
1591 if (client->adapter == adapter) { in i2c_do_del_adapter()
1592 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", in i2c_do_del_adapter()
1593 client->name, client->addr); in i2c_do_del_adapter()
1594 list_del(&client->detected); in i2c_do_del_adapter()
1603 if (client && strcmp(client->name, "dummy")) in __unregister_client()
1622 * i2c_del_adapter - unregister I2C adapter
1636 found = idr_find(&i2c_adapter_idr, adap->nr); in i2c_del_adapter()
1639 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name); in i2c_del_adapter()
1651 mutex_lock_nested(&adap->userspace_clients_lock, in i2c_del_adapter()
1653 list_for_each_entry_safe(client, next, &adap->userspace_clients, in i2c_del_adapter()
1655 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, in i2c_del_adapter()
1656 client->addr); in i2c_del_adapter()
1657 list_del(&client->detected); in i2c_del_adapter()
1660 mutex_unlock(&adap->userspace_clients_lock); in i2c_del_adapter()
1663 * check the returned value. This is a two-pass process, because in i2c_del_adapter()
1667 device_for_each_child(&adap->dev, NULL, __unregister_client); in i2c_del_adapter()
1668 device_for_each_child(&adap->dev, NULL, __unregister_dummy); in i2c_del_adapter()
1671 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, in i2c_del_adapter()
1672 adap->dev.parent); in i2c_del_adapter()
1676 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); in i2c_del_adapter()
1678 pm_runtime_disable(&adap->dev); in i2c_del_adapter()
1689 init_completion(&adap->dev_released); in i2c_del_adapter()
1690 device_unregister(&adap->dev); in i2c_del_adapter()
1691 wait_for_completion(&adap->dev_released); in i2c_del_adapter()
1695 idr_remove(&i2c_adapter_idr, adap->nr); in i2c_del_adapter()
1700 memset(&adap->dev, 0, sizeof(adap->dev)); in i2c_del_adapter()
1717 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1737 i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz, in i2c_parse_fw_timings()
1740 d = t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ ? 1000 : in i2c_parse_fw_timings()
1741 t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120; in i2c_parse_fw_timings()
1742 i2c_parse_timing(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns, d, u); in i2c_parse_fw_timings()
1744 d = t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120; in i2c_parse_fw_timings()
1745 i2c_parse_timing(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns, d, u); in i2c_parse_fw_timings()
1747 i2c_parse_timing(dev, "i2c-scl-internal-delay-ns", in i2c_parse_fw_timings()
1748 &t->scl_int_delay_ns, 0, u); in i2c_parse_fw_timings()
1749 i2c_parse_timing(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns, in i2c_parse_fw_timings()
1750 t->scl_fall_ns, u); in i2c_parse_fw_timings()
1751 i2c_parse_timing(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns, 0, u); in i2c_parse_fw_timings()
1752 i2c_parse_timing(dev, "i2c-digital-filter-width-ns", in i2c_parse_fw_timings()
1753 &t->digital_filter_width_ns, 0, u); in i2c_parse_fw_timings()
1754 i2c_parse_timing(dev, "i2c-analog-filter-cutoff-frequency", in i2c_parse_fw_timings()
1755 &t->analog_filter_cutoff_freq_hz, 0, u); in i2c_parse_fw_timings()
1759 /* ------------------------------------------------------------------------- */
1775 if (dev->type != &i2c_adapter_type) in __process_new_driver()
1791 return -EAGAIN; in i2c_register_driver()
1794 driver->driver.owner = owner; in i2c_register_driver()
1795 driver->driver.bus = &i2c_bus_type; in i2c_register_driver()
1796 INIT_LIST_HEAD(&driver->clients); in i2c_register_driver()
1799 * will have called probe() for all matching-but-unbound devices. in i2c_register_driver()
1801 res = driver_register(&driver->driver); in i2c_register_driver()
1805 pr_debug("driver [%s] registered\n", driver->driver.name); in i2c_register_driver()
1816 if (dev->type == &i2c_adapter_type) in __process_removed_driver()
1822 * i2c_del_driver - unregister I2C driver
1830 driver_unregister(&driver->driver); in i2c_del_driver()
1831 pr_debug("driver [%s] unregistered\n", driver->driver.name); in i2c_del_driver()
1835 /* ------------------------------------------------------------------------- */
1848 if (!client || !client->dev.driver) in i2c_cmd()
1851 driver = to_i2c_driver(client->dev.driver); in i2c_cmd()
1852 if (driver->command) in i2c_cmd()
1853 driver->command(client, arg->cmd, arg->arg); in i2c_cmd()
1863 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); in i2c_clients_command()
1885 i2c_adapter_compat_class = class_compat_register("i2c-adapter"); in i2c_init()
1887 retval = -ENOMEM; in i2c_init()
1932 /* ----------------------------------------------------
1934 * ----------------------------------------------------
1942 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n", in i2c_quirk_error()
1943 err_msg, msg->addr, msg->len, in i2c_quirk_error()
1944 msg->flags & I2C_M_RD ? "read" : "write"); in i2c_quirk_error()
1945 return -EOPNOTSUPP; in i2c_quirk_error()
1950 const struct i2c_adapter_quirks *q = adap->quirks; in i2c_check_for_quirks()
1951 int max_num = q->max_num_msgs, i; in i2c_check_for_quirks()
1954 if (q->flags & I2C_AQ_COMB) { in i2c_check_for_quirks()
1959 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD) in i2c_check_for_quirks()
1962 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD)) in i2c_check_for_quirks()
1965 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr) in i2c_check_for_quirks()
1968 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len)) in i2c_check_for_quirks()
1971 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len)) in i2c_check_for_quirks()
1985 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len)) in i2c_check_for_quirks()
1988 if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0) in i2c_check_for_quirks()
1991 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len)) in i2c_check_for_quirks()
1994 if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0) in i2c_check_for_quirks()
2003 * __i2c_transfer - unlocked flavor of i2c_transfer
2012 * takes place. adap->algo->master_xfer existence isn't checked.
2020 return -EINVAL; in __i2c_transfer()
2026 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) in __i2c_transfer()
2027 return -EOPNOTSUPP; in __i2c_transfer()
2031 * enabled. This is an efficient way of keeping the for-loop from in __i2c_transfer()
2045 for (ret = 0, try = 0; try <= adap->retries; try++) { in __i2c_transfer()
2046 if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic) in __i2c_transfer()
2047 ret = adap->algo->master_xfer_atomic(adap, msgs, num); in __i2c_transfer()
2049 ret = adap->algo->master_xfer(adap, msgs, num); in __i2c_transfer()
2051 if (ret != -EAGAIN) in __i2c_transfer()
2053 if (time_after(jiffies, orig_jiffies + adap->timeout)) in __i2c_transfer()
2070 * i2c_transfer - execute a single or combined I2C message
2085 if (!adap->algo->master_xfer) { in i2c_transfer()
2086 dev_dbg(&adap->dev, "I2C level transfers not supported\n"); in i2c_transfer()
2087 return -EOPNOTSUPP; in i2c_transfer()
2092 * - When we get an error after receiving N bytes from a slave, in i2c_transfer()
2095 * - When we get a NAK after transmitting N bytes to a slave, in i2c_transfer()
2100 * - When for example "num" is two and we successfully complete in i2c_transfer()
2118 * i2c_transfer_buffer_flags - issue a single I2C message transferring data
2132 .addr = client->addr, in i2c_transfer_buffer_flags()
2133 .flags = flags | (client->flags & I2C_M_TEN), in i2c_transfer_buffer_flags()
2138 ret = i2c_transfer(client->adapter, &msg, 1); in i2c_transfer_buffer_flags()
2149 * i2c_get_device_id - get manufacturer, part id and die revision of a device
2158 struct i2c_adapter *adap = client->adapter; in i2c_get_device_id()
2163 return -EOPNOTSUPP; in i2c_get_device_id()
2167 I2C_SMBUS_READ, client->addr << 1, in i2c_get_device_id()
2172 id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4); in i2c_get_device_id()
2173 id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3); in i2c_get_device_id()
2174 id->die_revision = raw_id.block[3] & 0x7; in i2c_get_device_id()
2179 /* ----------------------------------------------------
2181 * Will not work for 10-bit addresses!
2182 * ----------------------------------------------------
2189 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2203 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON) in i2c_default_probe()
2217 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n", in i2c_default_probe()
2219 err = -EOPNOTSUPP; in i2c_default_probe()
2229 struct i2c_adapter *adapter = temp_client->adapter; in i2c_detect_address()
2230 int addr = temp_client->addr; in i2c_detect_address()
2236 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", in i2c_detect_address()
2252 err = driver->detect(temp_client, &info); in i2c_detect_address()
2254 /* -ENODEV is returned if the detection fails. We catch it in i2c_detect_address()
2256 return err == -ENODEV ? 0 : err; in i2c_detect_address()
2261 dev_err(&adapter->dev, in i2c_detect_address()
2263 driver->driver.name, addr); in i2c_detect_address()
2268 if (adapter->class & I2C_CLASS_DEPRECATED) in i2c_detect_address()
2269 dev_warn(&adapter->dev, in i2c_detect_address()
2272 "Check 'Documentation/i2c/instantiating-devices.rst' for details.\n", in i2c_detect_address()
2275 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", in i2c_detect_address()
2279 list_add_tail(&client->detected, &driver->clients); in i2c_detect_address()
2281 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", in i2c_detect_address()
2293 address_list = driver->address_list; in i2c_detect()
2294 if (!driver->detect || !address_list) in i2c_detect()
2298 if (adapter->class == I2C_CLASS_DEPRECATED) { in i2c_detect()
2299 dev_dbg(&adapter->dev, in i2c_detect()
2300 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. " in i2c_detect()
2301 "If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n", in i2c_detect()
2302 driver->driver.name); in i2c_detect()
2307 if (!(adapter->class & driver->class)) in i2c_detect()
2310 /* Set up a temporary client to help detect callback */ in i2c_detect()
2313 return -ENOMEM; in i2c_detect()
2314 temp_client->adapter = adapter; in i2c_detect()
2317 dev_dbg(&adapter->dev, in i2c_detect()
2320 temp_client->addr = address_list[i]; in i2c_detect()
2351 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n", in i2c_new_scanned_device()
2358 dev_dbg(&adap->dev, in i2c_new_scanned_device()
2370 dev_dbg(&adap->dev, "Probing failed, no device found\n"); in i2c_new_scanned_device()
2371 return ERR_PTR(-ENODEV); in i2c_new_scanned_device()
2374 info->addr = addr_list[i]; in i2c_new_scanned_device()
2388 if (try_module_get(adapter->owner)) in i2c_get_adapter()
2389 get_device(&adapter->dev); in i2c_get_adapter()
2404 put_device(&adap->dev); in i2c_put_adapter()
2405 module_put(adap->owner); in i2c_put_adapter()
2410 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2415 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2423 /* also skip 0-length msgs for bogus thresholds of 0 */ in i2c_get_dma_safe_msg_buf()
2426 msg->addr); in i2c_get_dma_safe_msg_buf()
2427 if (msg->len < threshold || msg->len == 0) in i2c_get_dma_safe_msg_buf()
2430 if (msg->flags & I2C_M_DMA_SAFE) in i2c_get_dma_safe_msg_buf()
2431 return msg->buf; in i2c_get_dma_safe_msg_buf()
2434 msg->addr, msg->len); in i2c_get_dma_safe_msg_buf()
2436 if (msg->flags & I2C_M_RD) in i2c_get_dma_safe_msg_buf()
2437 return kzalloc(msg->len, GFP_KERNEL); in i2c_get_dma_safe_msg_buf()
2439 return kmemdup(msg->buf, msg->len, GFP_KERNEL); in i2c_get_dma_safe_msg_buf()
2444 * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2451 if (!buf || buf == msg->buf) in i2c_put_dma_safe_msg_buf()
2454 if (xferred && msg->flags & I2C_M_RD) in i2c_put_dma_safe_msg_buf()
2455 memcpy(msg->buf, buf, msg->len); in i2c_put_dma_safe_msg_buf()
2461 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2462 MODULE_DESCRIPTION("I2C-Bus main module");