1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. */
14 /* ------------------------------------------------------------------------- */
25 I2C ACPI code Copyright (C) 2014 Intel Corp
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/gpio.h>
35 #include <linux/slab.h>
36 #include <linux/i2c.h>
37 #include <linux/init.h>
38 #include <linux/idr.h>
39 #include <linux/mutex.h>
41 #include <linux/of_device.h>
42 #include <linux/of_irq.h>
43 #include <linux/clk/clk-conf.h>
44 #include <linux/completion.h>
45 #include <linux/hardirq.h>
46 #include <linux/irqflags.h>
47 #include <linux/rwsem.h>
48 #include <linux/pm_runtime.h>
49 #include <linux/pm_domain.h>
50 #include <linux/acpi.h>
51 #include <linux/jump_label.h>
52 #include <asm/uaccess.h>
53 #include <linux/err.h>
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/i2c.h>
60 /* core_lock protects i2c_adapter_idr, and guarantees
61 that device detection, deletion of detected devices, and attach_adapter
62 calls are serialized */
63 static DEFINE_MUTEX(core_lock);
64 static DEFINE_IDR(i2c_adapter_idr);
66 static struct device_type i2c_client_type;
67 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
69 static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
71 void i2c_transfer_trace_reg(void)
73 static_key_slow_inc(&i2c_trace_msg);
76 void i2c_transfer_trace_unreg(void)
78 static_key_slow_dec(&i2c_trace_msg);
81 #if defined(CONFIG_ACPI)
82 struct acpi_i2c_handler_data {
83 struct acpi_connection_info info;
84 struct i2c_adapter *adapter;
97 static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
99 struct i2c_board_info *info = data;
101 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
102 struct acpi_resource_i2c_serialbus *sb;
104 sb = &ares->data.i2c_serial_bus;
105 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
106 info->addr = sb->slave_address;
107 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
108 info->flags |= I2C_CLIENT_TEN;
110 } else if (info->irq < 0) {
113 if (acpi_dev_resource_interrupt(ares, 0, &r))
117 /* Tell the ACPI core to skip this resource */
121 static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
122 void *data, void **return_value)
124 struct i2c_adapter *adapter = data;
125 struct list_head resource_list;
126 struct i2c_board_info info;
127 struct acpi_device *adev;
130 if (acpi_bus_get_device(handle, &adev))
132 if (acpi_bus_get_status(adev) || !adev->status.present)
135 memset(&info, 0, sizeof(info));
136 info.acpi_node.companion = adev;
139 INIT_LIST_HEAD(&resource_list);
140 ret = acpi_dev_get_resources(adev, &resource_list,
141 acpi_i2c_add_resource, &info);
142 acpi_dev_free_resource_list(&resource_list);
144 if (ret < 0 || !info.addr)
147 adev->power.flags.ignore_parent = true;
148 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
149 if (!i2c_new_device(adapter, &info)) {
150 adev->power.flags.ignore_parent = false;
151 dev_err(&adapter->dev,
152 "failed to add I2C device %s from ACPI\n",
153 dev_name(&adev->dev));
160 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
161 * @adap: pointer to adapter
163 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
164 * namespace. When a device is found it will be added to the Linux device
165 * model and bound to the corresponding ACPI handle.
167 static void acpi_i2c_register_devices(struct i2c_adapter *adap)
172 if (!adap->dev.parent)
175 handle = ACPI_HANDLE(adap->dev.parent);
179 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
180 acpi_i2c_add_device, NULL,
182 if (ACPI_FAILURE(status))
183 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
186 #else /* CONFIG_ACPI */
187 static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
188 #endif /* CONFIG_ACPI */
190 #ifdef CONFIG_ACPI_I2C_OPREGION
191 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
192 u8 cmd, u8 *data, u8 data_len)
195 struct i2c_msg msgs[2];
199 buffer = kzalloc(data_len, GFP_KERNEL);
203 msgs[0].addr = client->addr;
204 msgs[0].flags = client->flags;
208 msgs[1].addr = client->addr;
209 msgs[1].flags = client->flags | I2C_M_RD;
210 msgs[1].len = data_len;
211 msgs[1].buf = buffer;
213 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
215 dev_err(&client->adapter->dev, "i2c read failed\n");
217 memcpy(data, buffer, data_len);
223 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
224 u8 cmd, u8 *data, u8 data_len)
227 struct i2c_msg msgs[1];
231 buffer = kzalloc(data_len + 1, GFP_KERNEL);
236 memcpy(buffer + 1, data, data_len);
238 msgs[0].addr = client->addr;
239 msgs[0].flags = client->flags;
240 msgs[0].len = data_len + 1;
241 msgs[0].buf = buffer;
243 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
245 dev_err(&client->adapter->dev, "i2c write failed\n");
252 acpi_i2c_space_handler(u32 function, acpi_physical_address command,
253 u32 bits, u64 *value64,
254 void *handler_context, void *region_context)
256 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
257 struct acpi_i2c_handler_data *data = handler_context;
258 struct acpi_connection_info *info = &data->info;
259 struct acpi_resource_i2c_serialbus *sb;
260 struct i2c_adapter *adapter = data->adapter;
261 struct i2c_client client;
262 struct acpi_resource *ares;
263 u32 accessor_type = function >> 16;
264 u8 action = function & ACPI_IO_MASK;
268 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
269 if (ACPI_FAILURE(ret))
272 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
273 ret = AE_BAD_PARAMETER;
277 sb = &ares->data.i2c_serial_bus;
278 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
279 ret = AE_BAD_PARAMETER;
283 memset(&client, 0, sizeof(client));
284 client.adapter = adapter;
285 client.addr = sb->slave_address;
288 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
289 client.flags |= I2C_CLIENT_TEN;
291 switch (accessor_type) {
292 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
293 if (action == ACPI_READ) {
294 status = i2c_smbus_read_byte(&client);
300 status = i2c_smbus_write_byte(&client, gsb->bdata);
304 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
305 if (action == ACPI_READ) {
306 status = i2c_smbus_read_byte_data(&client, command);
312 status = i2c_smbus_write_byte_data(&client, command,
317 case ACPI_GSB_ACCESS_ATTRIB_WORD:
318 if (action == ACPI_READ) {
319 status = i2c_smbus_read_word_data(&client, command);
325 status = i2c_smbus_write_word_data(&client, command,
330 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
331 if (action == ACPI_READ) {
332 status = i2c_smbus_read_block_data(&client, command,
339 status = i2c_smbus_write_block_data(&client, command,
340 gsb->len, gsb->data);
344 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
345 if (action == ACPI_READ) {
346 status = acpi_gsb_i2c_read_bytes(&client, command,
347 gsb->data, info->access_length);
351 status = acpi_gsb_i2c_write_bytes(&client, command,
352 gsb->data, info->access_length);
357 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
358 ret = AE_BAD_PARAMETER;
362 gsb->status = status;
370 static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
373 struct acpi_i2c_handler_data *data;
376 if (!adapter->dev.parent)
379 handle = ACPI_HANDLE(adapter->dev.parent);
384 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
389 data->adapter = adapter;
390 status = acpi_bus_attach_private_data(handle, (void *)data);
391 if (ACPI_FAILURE(status)) {
396 status = acpi_install_address_space_handler(handle,
397 ACPI_ADR_SPACE_GSBUS,
398 &acpi_i2c_space_handler,
401 if (ACPI_FAILURE(status)) {
402 dev_err(&adapter->dev, "Error installing i2c space handler\n");
403 acpi_bus_detach_private_data(handle);
408 acpi_walk_dep_device_list(handle);
412 static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
415 struct acpi_i2c_handler_data *data;
418 if (!adapter->dev.parent)
421 handle = ACPI_HANDLE(adapter->dev.parent);
426 acpi_remove_address_space_handler(handle,
427 ACPI_ADR_SPACE_GSBUS,
428 &acpi_i2c_space_handler);
430 status = acpi_bus_get_private_data(handle, (void **)&data);
431 if (ACPI_SUCCESS(status))
434 acpi_bus_detach_private_data(handle);
436 #else /* CONFIG_ACPI_I2C_OPREGION */
437 static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
440 static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
442 #endif /* CONFIG_ACPI_I2C_OPREGION */
444 /* ------------------------------------------------------------------------- */
446 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
447 const struct i2c_client *client)
449 while (id->name[0]) {
450 if (strcmp(client->name, id->name) == 0)
457 static int i2c_device_match(struct device *dev, struct device_driver *drv)
459 struct i2c_client *client = i2c_verify_client(dev);
460 struct i2c_driver *driver;
465 /* Attempt an OF style match */
466 if (of_driver_match_device(dev, drv))
469 /* Then ACPI style match */
470 if (acpi_driver_match_device(dev, drv))
473 driver = to_i2c_driver(drv);
474 /* match on an id table if there is one */
475 if (driver->id_table)
476 return i2c_match_id(driver->id_table, client) != NULL;
482 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
483 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
485 struct i2c_client *client = to_i2c_client(dev);
488 rc = acpi_device_uevent_modalias(dev, env);
492 if (add_uevent_var(env, "MODALIAS=%s%s",
493 I2C_MODULE_PREFIX, client->name))
495 dev_dbg(dev, "uevent\n");
499 /* i2c bus recovery routines */
500 static int get_scl_gpio_value(struct i2c_adapter *adap)
502 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
505 static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
507 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
510 static int get_sda_gpio_value(struct i2c_adapter *adap)
512 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
515 static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
517 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
518 struct device *dev = &adap->dev;
521 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
522 GPIOF_OUT_INIT_HIGH, "i2c-scl");
524 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
529 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
530 /* work without SDA polling */
531 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
540 static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
542 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
545 gpio_free(bri->sda_gpio);
547 gpio_free(bri->scl_gpio);
551 * We are generating clock pulses. ndelay() determines durating of clk pulses.
552 * We will generate clock with rate 100 KHz and so duration of both clock levels
553 * is: delay in ns = (10^6 / 100) / 2
555 #define RECOVERY_NDELAY 5000
556 #define RECOVERY_CLK_CNT 9
558 static int i2c_generic_recovery(struct i2c_adapter *adap)
560 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
561 int i = 0, val = 1, ret = 0;
563 if (bri->prepare_recovery)
564 bri->prepare_recovery(bri);
567 * By this time SCL is high, as we need to give 9 falling-rising edges
569 while (i++ < RECOVERY_CLK_CNT * 2) {
571 /* Break if SDA is high */
572 if (bri->get_sda && bri->get_sda(adap))
574 /* SCL shouldn't be low here */
575 if (!bri->get_scl(adap)) {
577 "SCL is stuck low, exit recovery\n");
584 bri->set_scl(adap, val);
585 ndelay(RECOVERY_NDELAY);
588 if (bri->unprepare_recovery)
589 bri->unprepare_recovery(bri);
594 int i2c_generic_scl_recovery(struct i2c_adapter *adap)
596 adap->bus_recovery_info->set_scl(adap, 1);
597 return i2c_generic_recovery(adap);
600 int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
604 ret = i2c_get_gpios_for_recovery(adap);
608 ret = i2c_generic_recovery(adap);
609 i2c_put_gpios_for_recovery(adap);
614 int i2c_recover_bus(struct i2c_adapter *adap)
616 if (!adap->bus_recovery_info)
619 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
620 return adap->bus_recovery_info->recover_bus(adap);
623 static int i2c_device_probe(struct device *dev)
625 struct i2c_client *client = i2c_verify_client(dev);
626 struct i2c_driver *driver;
632 if (!client->irq && dev->of_node) {
633 int irq = of_irq_get(dev->of_node, 0);
635 if (irq == -EPROBE_DEFER)
643 driver = to_i2c_driver(dev->driver);
644 if (!driver->probe || !driver->id_table)
647 if (!device_can_wakeup(&client->dev))
648 device_init_wakeup(&client->dev,
649 client->flags & I2C_CLIENT_WAKE);
650 dev_dbg(dev, "probe\n");
652 status = of_clk_set_defaults(dev->of_node, false);
656 status = dev_pm_domain_attach(&client->dev, true);
657 if (status != -EPROBE_DEFER) {
658 status = driver->probe(client, i2c_match_id(driver->id_table,
661 dev_pm_domain_detach(&client->dev, true);
667 static int i2c_device_remove(struct device *dev)
669 struct i2c_client *client = i2c_verify_client(dev);
670 struct i2c_driver *driver;
673 if (!client || !dev->driver)
676 driver = to_i2c_driver(dev->driver);
677 if (driver->remove) {
678 dev_dbg(dev, "remove\n");
679 status = driver->remove(client);
683 irq_dispose_mapping(client->irq);
685 dev_pm_domain_detach(&client->dev, true);
689 static void i2c_device_shutdown(struct device *dev)
691 struct i2c_client *client = i2c_verify_client(dev);
692 struct i2c_driver *driver;
694 if (!client || !dev->driver)
696 driver = to_i2c_driver(dev->driver);
697 if (driver->shutdown)
698 driver->shutdown(client);
701 #ifdef CONFIG_PM_SLEEP
702 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
704 struct i2c_client *client = i2c_verify_client(dev);
705 struct i2c_driver *driver;
707 if (!client || !dev->driver)
709 driver = to_i2c_driver(dev->driver);
710 if (!driver->suspend)
712 return driver->suspend(client, mesg);
715 static int i2c_legacy_resume(struct device *dev)
717 struct i2c_client *client = i2c_verify_client(dev);
718 struct i2c_driver *driver;
720 if (!client || !dev->driver)
722 driver = to_i2c_driver(dev->driver);
725 return driver->resume(client);
728 static int i2c_device_pm_suspend(struct device *dev)
730 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
733 return pm_generic_suspend(dev);
735 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
738 static int i2c_device_pm_resume(struct device *dev)
740 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
743 return pm_generic_resume(dev);
745 return i2c_legacy_resume(dev);
748 static int i2c_device_pm_freeze(struct device *dev)
750 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
753 return pm_generic_freeze(dev);
755 return i2c_legacy_suspend(dev, PMSG_FREEZE);
758 static int i2c_device_pm_thaw(struct device *dev)
760 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
763 return pm_generic_thaw(dev);
765 return i2c_legacy_resume(dev);
768 static int i2c_device_pm_poweroff(struct device *dev)
770 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
773 return pm_generic_poweroff(dev);
775 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
778 static int i2c_device_pm_restore(struct device *dev)
780 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
783 return pm_generic_restore(dev);
785 return i2c_legacy_resume(dev);
787 #else /* !CONFIG_PM_SLEEP */
788 #define i2c_device_pm_suspend NULL
789 #define i2c_device_pm_resume NULL
790 #define i2c_device_pm_freeze NULL
791 #define i2c_device_pm_thaw NULL
792 #define i2c_device_pm_poweroff NULL
793 #define i2c_device_pm_restore NULL
794 #endif /* !CONFIG_PM_SLEEP */
796 static void i2c_client_dev_release(struct device *dev)
798 kfree(to_i2c_client(dev));
802 show_name(struct device *dev, struct device_attribute *attr, char *buf)
804 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
805 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
809 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
811 struct i2c_client *client = to_i2c_client(dev);
814 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
818 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
821 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
822 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
824 static struct attribute *i2c_dev_attrs[] = {
826 /* modalias helps coldplug: modprobe $(cat .../modalias) */
827 &dev_attr_modalias.attr,
831 static struct attribute_group i2c_dev_attr_group = {
832 .attrs = i2c_dev_attrs,
835 static const struct attribute_group *i2c_dev_attr_groups[] = {
840 static const struct dev_pm_ops i2c_device_pm_ops = {
841 .suspend = i2c_device_pm_suspend,
842 .resume = i2c_device_pm_resume,
843 .freeze = i2c_device_pm_freeze,
844 .thaw = i2c_device_pm_thaw,
845 .poweroff = i2c_device_pm_poweroff,
846 .restore = i2c_device_pm_restore,
848 pm_generic_runtime_suspend,
849 pm_generic_runtime_resume,
854 struct bus_type i2c_bus_type = {
856 .match = i2c_device_match,
857 .probe = i2c_device_probe,
858 .remove = i2c_device_remove,
859 .shutdown = i2c_device_shutdown,
860 .pm = &i2c_device_pm_ops,
862 EXPORT_SYMBOL_GPL(i2c_bus_type);
864 static struct device_type i2c_client_type = {
865 .groups = i2c_dev_attr_groups,
866 .uevent = i2c_device_uevent,
867 .release = i2c_client_dev_release,
872 * i2c_verify_client - return parameter as i2c_client, or NULL
873 * @dev: device, probably from some driver model iterator
875 * When traversing the driver model tree, perhaps using driver model
876 * iterators like @device_for_each_child(), you can't assume very much
877 * about the nodes you find. Use this function to avoid oopses caused
878 * by wrongly treating some non-I2C device as an i2c_client.
880 struct i2c_client *i2c_verify_client(struct device *dev)
882 return (dev->type == &i2c_client_type)
886 EXPORT_SYMBOL(i2c_verify_client);
889 /* This is a permissive address validity check, I2C address map constraints
890 * are purposely not enforced, except for the general call address. */
891 static int i2c_check_client_addr_validity(const struct i2c_client *client)
893 if (client->flags & I2C_CLIENT_TEN) {
894 /* 10-bit address, all values are valid */
895 if (client->addr > 0x3ff)
898 /* 7-bit address, reject the general call address */
899 if (client->addr == 0x00 || client->addr > 0x7f)
905 /* And this is a strict address validity check, used when probing. If a
906 * device uses a reserved address, then it shouldn't be probed. 7-bit
907 * addressing is assumed, 10-bit address devices are rare and should be
908 * explicitly enumerated. */
909 static int i2c_check_addr_validity(unsigned short addr)
912 * Reserved addresses per I2C specification:
913 * 0x00 General call address / START byte
915 * 0x02 Reserved for different bus format
916 * 0x03 Reserved for future purposes
917 * 0x04-0x07 Hs-mode master code
918 * 0x78-0x7b 10-bit slave addressing
919 * 0x7c-0x7f Reserved for future purposes
921 if (addr < 0x08 || addr > 0x77)
926 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
928 struct i2c_client *client = i2c_verify_client(dev);
929 int addr = *(int *)addrp;
931 if (client && client->addr == addr)
936 /* walk up mux tree */
937 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
939 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
942 result = device_for_each_child(&adapter->dev, &addr,
943 __i2c_check_addr_busy);
945 if (!result && parent)
946 result = i2c_check_mux_parents(parent, addr);
951 /* recurse down mux tree */
952 static int i2c_check_mux_children(struct device *dev, void *addrp)
956 if (dev->type == &i2c_adapter_type)
957 result = device_for_each_child(dev, addrp,
958 i2c_check_mux_children);
960 result = __i2c_check_addr_busy(dev, addrp);
965 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
967 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
971 result = i2c_check_mux_parents(parent, addr);
974 result = device_for_each_child(&adapter->dev, &addr,
975 i2c_check_mux_children);
981 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
982 * @adapter: Target I2C bus segment
984 void i2c_lock_adapter(struct i2c_adapter *adapter)
986 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
989 i2c_lock_adapter(parent);
991 rt_mutex_lock(&adapter->bus_lock);
993 EXPORT_SYMBOL_GPL(i2c_lock_adapter);
996 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
997 * @adapter: Target I2C bus segment
999 static int i2c_trylock_adapter(struct i2c_adapter *adapter)
1001 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1004 return i2c_trylock_adapter(parent);
1006 return rt_mutex_trylock(&adapter->bus_lock);
1010 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
1011 * @adapter: Target I2C bus segment
1013 void i2c_unlock_adapter(struct i2c_adapter *adapter)
1015 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1018 i2c_unlock_adapter(parent);
1020 rt_mutex_unlock(&adapter->bus_lock);
1022 EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1024 static void i2c_dev_set_name(struct i2c_adapter *adap,
1025 struct i2c_client *client)
1027 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1030 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1034 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1035 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1036 client->addr | ((client->flags & I2C_CLIENT_TEN)
1041 * i2c_new_device - instantiate an i2c device
1042 * @adap: the adapter managing the device
1043 * @info: describes one I2C device; bus_num is ignored
1044 * Context: can sleep
1046 * Create an i2c device. Binding is handled through driver model
1047 * probe()/remove() methods. A driver may be bound to this device when we
1048 * return from this function, or any later moment (e.g. maybe hotplugging will
1049 * load the driver module). This call is not appropriate for use by mainboard
1050 * initialization logic, which usually runs during an arch_initcall() long
1051 * before any i2c_adapter could exist.
1053 * This returns the new i2c client, which may be saved for later use with
1054 * i2c_unregister_device(); or NULL to indicate an error.
1057 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1059 struct i2c_client *client;
1062 client = kzalloc(sizeof *client, GFP_KERNEL);
1066 client->adapter = adap;
1068 client->dev.platform_data = info->platform_data;
1071 client->dev.archdata = *info->archdata;
1073 client->flags = info->flags;
1074 client->addr = info->addr;
1075 client->irq = info->irq;
1077 strlcpy(client->name, info->type, sizeof(client->name));
1079 /* Check for address validity */
1080 status = i2c_check_client_addr_validity(client);
1082 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1083 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1084 goto out_err_silent;
1087 /* Check for address business */
1088 status = i2c_check_addr_busy(adap, client->addr);
1092 client->dev.parent = &client->adapter->dev;
1093 client->dev.bus = &i2c_bus_type;
1094 client->dev.type = &i2c_client_type;
1095 client->dev.of_node = info->of_node;
1096 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
1098 i2c_dev_set_name(adap, client);
1099 status = device_register(&client->dev);
1103 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1104 client->name, dev_name(&client->dev));
1109 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1110 "(%d)\n", client->name, client->addr, status);
1115 EXPORT_SYMBOL_GPL(i2c_new_device);
1119 * i2c_unregister_device - reverse effect of i2c_new_device()
1120 * @client: value returned from i2c_new_device()
1121 * Context: can sleep
1123 void i2c_unregister_device(struct i2c_client *client)
1125 device_unregister(&client->dev);
1127 EXPORT_SYMBOL_GPL(i2c_unregister_device);
1130 static const struct i2c_device_id dummy_id[] = {
1135 static int dummy_probe(struct i2c_client *client,
1136 const struct i2c_device_id *id)
1141 static int dummy_remove(struct i2c_client *client)
1146 static struct i2c_driver dummy_driver = {
1147 .driver.name = "dummy",
1148 .probe = dummy_probe,
1149 .remove = dummy_remove,
1150 .id_table = dummy_id,
1154 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1155 * @adapter: the adapter managing the device
1156 * @address: seven bit address to be used
1157 * Context: can sleep
1159 * This returns an I2C client bound to the "dummy" driver, intended for use
1160 * with devices that consume multiple addresses. Examples of such chips
1161 * include various EEPROMS (like 24c04 and 24c08 models).
1163 * These dummy devices have two main uses. First, most I2C and SMBus calls
1164 * except i2c_transfer() need a client handle; the dummy will be that handle.
1165 * And second, this prevents the specified address from being bound to a
1168 * This returns the new i2c client, which should be saved for later use with
1169 * i2c_unregister_device(); or NULL to indicate an error.
1171 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
1173 struct i2c_board_info info = {
1174 I2C_BOARD_INFO("dummy", address),
1177 return i2c_new_device(adapter, &info);
1179 EXPORT_SYMBOL_GPL(i2c_new_dummy);
1181 /* ------------------------------------------------------------------------- */
1183 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1185 static void i2c_adapter_dev_release(struct device *dev)
1187 struct i2c_adapter *adap = to_i2c_adapter(dev);
1188 complete(&adap->dev_released);
1192 * This function is only needed for mutex_lock_nested, so it is never
1193 * called unless locking correctness checking is enabled. Thus we
1194 * make it inline to avoid a compiler warning. That's what gcc ends up
1197 static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1199 unsigned int depth = 0;
1201 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1208 * Let users instantiate I2C devices through sysfs. This can be used when
1209 * platform initialization code doesn't contain the proper data for
1210 * whatever reason. Also useful for drivers that do device detection and
1211 * detection fails, either because the device uses an unexpected address,
1212 * or this is a compatible device with different ID register values.
1214 * Parameter checking may look overzealous, but we really don't want
1215 * the user to provide incorrect parameters.
1218 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1219 const char *buf, size_t count)
1221 struct i2c_adapter *adap = to_i2c_adapter(dev);
1222 struct i2c_board_info info;
1223 struct i2c_client *client;
1227 memset(&info, 0, sizeof(struct i2c_board_info));
1229 blank = strchr(buf, ' ');
1231 dev_err(dev, "%s: Missing parameters\n", "new_device");
1234 if (blank - buf > I2C_NAME_SIZE - 1) {
1235 dev_err(dev, "%s: Invalid device name\n", "new_device");
1238 memcpy(info.type, buf, blank - buf);
1240 /* Parse remaining parameters, reject extra parameters */
1241 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1243 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1246 if (res > 1 && end != '\n') {
1247 dev_err(dev, "%s: Extra parameters\n", "new_device");
1251 client = i2c_new_device(adap, &info);
1255 /* Keep track of the added device */
1256 mutex_lock(&adap->userspace_clients_lock);
1257 list_add_tail(&client->detected, &adap->userspace_clients);
1258 mutex_unlock(&adap->userspace_clients_lock);
1259 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1260 info.type, info.addr);
1266 * And of course let the users delete the devices they instantiated, if
1267 * they got it wrong. This interface can only be used to delete devices
1268 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1269 * don't delete devices to which some kernel code still has references.
1271 * Parameter checking may look overzealous, but we really don't want
1272 * the user to delete the wrong device.
1275 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1276 const char *buf, size_t count)
1278 struct i2c_adapter *adap = to_i2c_adapter(dev);
1279 struct i2c_client *client, *next;
1280 unsigned short addr;
1284 /* Parse parameters, reject extra parameters */
1285 res = sscanf(buf, "%hi%c", &addr, &end);
1287 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1290 if (res > 1 && end != '\n') {
1291 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1295 /* Make sure the device was added through sysfs */
1297 mutex_lock_nested(&adap->userspace_clients_lock,
1298 i2c_adapter_depth(adap));
1299 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1301 if (client->addr == addr) {
1302 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1303 "delete_device", client->name, client->addr);
1305 list_del(&client->detected);
1306 i2c_unregister_device(client);
1311 mutex_unlock(&adap->userspace_clients_lock);
1314 dev_err(dev, "%s: Can't find device in list\n",
1319 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
1320 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1321 i2c_sysfs_delete_device);
1323 static struct attribute *i2c_adapter_attrs[] = {
1324 &dev_attr_name.attr,
1325 &dev_attr_new_device.attr,
1326 &dev_attr_delete_device.attr,
1330 static struct attribute_group i2c_adapter_attr_group = {
1331 .attrs = i2c_adapter_attrs,
1334 static const struct attribute_group *i2c_adapter_attr_groups[] = {
1335 &i2c_adapter_attr_group,
1339 struct device_type i2c_adapter_type = {
1340 .groups = i2c_adapter_attr_groups,
1341 .release = i2c_adapter_dev_release,
1343 EXPORT_SYMBOL_GPL(i2c_adapter_type);
1346 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1347 * @dev: device, probably from some driver model iterator
1349 * When traversing the driver model tree, perhaps using driver model
1350 * iterators like @device_for_each_child(), you can't assume very much
1351 * about the nodes you find. Use this function to avoid oopses caused
1352 * by wrongly treating some non-I2C device as an i2c_adapter.
1354 struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1356 return (dev->type == &i2c_adapter_type)
1357 ? to_i2c_adapter(dev)
1360 EXPORT_SYMBOL(i2c_verify_adapter);
1362 #ifdef CONFIG_I2C_COMPAT
1363 static struct class_compat *i2c_adapter_compat_class;
1366 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1368 struct i2c_devinfo *devinfo;
1370 down_read(&__i2c_board_lock);
1371 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1372 if (devinfo->busnum == adapter->nr
1373 && !i2c_new_device(adapter,
1374 &devinfo->board_info))
1375 dev_err(&adapter->dev,
1376 "Can't create device at 0x%02x\n",
1377 devinfo->board_info.addr);
1379 up_read(&__i2c_board_lock);
1382 /* OF support code */
1384 #if IS_ENABLED(CONFIG_OF)
1385 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1386 struct device_node *node)
1388 struct i2c_client *result;
1389 struct i2c_board_info info = {};
1390 struct dev_archdata dev_ad = {};
1394 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1396 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1397 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1399 return ERR_PTR(-EINVAL);
1402 addr = of_get_property(node, "reg", &len);
1403 if (!addr || (len < sizeof(int))) {
1404 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1406 return ERR_PTR(-EINVAL);
1409 info.addr = be32_to_cpup(addr);
1410 if (info.addr > (1 << 10) - 1) {
1411 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1412 info.addr, node->full_name);
1413 return ERR_PTR(-EINVAL);
1416 info.of_node = of_node_get(node);
1417 info.archdata = &dev_ad;
1419 if (of_get_property(node, "wakeup-source", NULL))
1420 info.flags |= I2C_CLIENT_WAKE;
1422 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1424 result = i2c_new_device(adap, &info);
1425 if (result == NULL) {
1426 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1429 return ERR_PTR(-EINVAL);
1434 static void of_i2c_register_devices(struct i2c_adapter *adap)
1436 struct device_node *node;
1438 /* Only register child devices if the adapter has a node pointer set */
1439 if (!adap->dev.of_node)
1442 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1444 for_each_available_child_of_node(adap->dev.of_node, node)
1445 of_i2c_register_device(adap, node);
1448 static int of_dev_node_match(struct device *dev, void *data)
1450 return dev->of_node == data;
1453 /* must call put_device() when done with returned i2c_client device */
1454 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1458 dev = bus_find_device(&i2c_bus_type, NULL, node,
1463 return i2c_verify_client(dev);
1465 EXPORT_SYMBOL(of_find_i2c_device_by_node);
1467 /* must call put_device() when done with returned i2c_adapter device */
1468 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1472 dev = bus_find_device(&i2c_bus_type, NULL, node,
1477 return i2c_verify_adapter(dev);
1479 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1481 static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1482 #endif /* CONFIG_OF */
1484 static int i2c_do_add_adapter(struct i2c_driver *driver,
1485 struct i2c_adapter *adap)
1487 /* Detect supported devices on that bus, and instantiate them */
1488 i2c_detect(adap, driver);
1490 /* Let legacy drivers scan this bus for matching devices */
1491 if (driver->attach_adapter) {
1492 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1493 driver->driver.name);
1494 dev_warn(&adap->dev, "Please use another way to instantiate "
1495 "your i2c_client\n");
1496 /* We ignore the return code; if it fails, too bad */
1497 driver->attach_adapter(adap);
1502 static int __process_new_adapter(struct device_driver *d, void *data)
1504 return i2c_do_add_adapter(to_i2c_driver(d), data);
1507 static int i2c_register_adapter(struct i2c_adapter *adap)
1511 /* Can't register until after driver model init */
1512 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1518 if (unlikely(adap->name[0] == '\0')) {
1519 pr_err("i2c-core: Attempt to register an adapter with "
1523 if (unlikely(!adap->algo)) {
1524 pr_err("i2c-core: Attempt to register adapter '%s' with "
1525 "no algo!\n", adap->name);
1529 rt_mutex_init(&adap->bus_lock);
1530 mutex_init(&adap->userspace_clients_lock);
1531 INIT_LIST_HEAD(&adap->userspace_clients);
1533 /* Set default timeout to 1 second if not already set */
1534 if (adap->timeout == 0)
1537 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
1538 adap->dev.bus = &i2c_bus_type;
1539 adap->dev.type = &i2c_adapter_type;
1540 res = device_register(&adap->dev);
1544 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1546 #ifdef CONFIG_I2C_COMPAT
1547 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1550 dev_warn(&adap->dev,
1551 "Failed to create compatibility class link\n");
1554 /* bus recovery specific initialization */
1555 if (adap->bus_recovery_info) {
1556 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1558 if (!bri->recover_bus) {
1559 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1560 adap->bus_recovery_info = NULL;
1564 /* Generic GPIO recovery */
1565 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1566 if (!gpio_is_valid(bri->scl_gpio)) {
1567 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1568 adap->bus_recovery_info = NULL;
1572 if (gpio_is_valid(bri->sda_gpio))
1573 bri->get_sda = get_sda_gpio_value;
1575 bri->get_sda = NULL;
1577 bri->get_scl = get_scl_gpio_value;
1578 bri->set_scl = set_scl_gpio_value;
1579 } else if (!bri->set_scl || !bri->get_scl) {
1580 /* Generic SCL recovery */
1581 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1582 adap->bus_recovery_info = NULL;
1587 /* create pre-declared device nodes */
1588 of_i2c_register_devices(adap);
1589 acpi_i2c_register_devices(adap);
1590 acpi_i2c_install_space_handler(adap);
1592 if (adap->nr < __i2c_first_dynamic_bus_num)
1593 i2c_scan_static_board_info(adap);
1595 /* Notify drivers */
1596 mutex_lock(&core_lock);
1597 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1598 mutex_unlock(&core_lock);
1603 mutex_lock(&core_lock);
1604 idr_remove(&i2c_adapter_idr, adap->nr);
1605 mutex_unlock(&core_lock);
1610 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1611 * @adap: the adapter to register (with adap->nr initialized)
1612 * Context: can sleep
1614 * See i2c_add_numbered_adapter() for details.
1616 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1620 mutex_lock(&core_lock);
1621 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1623 mutex_unlock(&core_lock);
1625 return id == -ENOSPC ? -EBUSY : id;
1627 return i2c_register_adapter(adap);
1631 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1632 * @adapter: the adapter to add
1633 * Context: can sleep
1635 * This routine is used to declare an I2C adapter when its bus number
1636 * doesn't matter or when its bus number is specified by an dt alias.
1637 * Examples of bases when the bus number doesn't matter: I2C adapters
1638 * dynamically added by USB links or PCI plugin cards.
1640 * When this returns zero, a new bus number was allocated and stored
1641 * in adap->nr, and the specified adapter became available for clients.
1642 * Otherwise, a negative errno value is returned.
1644 int i2c_add_adapter(struct i2c_adapter *adapter)
1646 struct device *dev = &adapter->dev;
1650 id = of_alias_get_id(dev->of_node, "i2c");
1653 return __i2c_add_numbered_adapter(adapter);
1657 mutex_lock(&core_lock);
1658 id = idr_alloc(&i2c_adapter_idr, adapter,
1659 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
1660 mutex_unlock(&core_lock);
1666 return i2c_register_adapter(adapter);
1668 EXPORT_SYMBOL(i2c_add_adapter);
1671 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1672 * @adap: the adapter to register (with adap->nr initialized)
1673 * Context: can sleep
1675 * This routine is used to declare an I2C adapter when its bus number
1676 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1677 * or otherwise built in to the system's mainboard, and where i2c_board_info
1678 * is used to properly configure I2C devices.
1680 * If the requested bus number is set to -1, then this function will behave
1681 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1683 * If no devices have pre-been declared for this bus, then be sure to
1684 * register the adapter before any dynamically allocated ones. Otherwise
1685 * the required bus ID may not be available.
1687 * When this returns zero, the specified adapter became available for
1688 * clients using the bus number provided in adap->nr. Also, the table
1689 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1690 * and the appropriate driver model device nodes are created. Otherwise, a
1691 * negative errno value is returned.
1693 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1695 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1696 return i2c_add_adapter(adap);
1698 return __i2c_add_numbered_adapter(adap);
1700 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1702 static void i2c_do_del_adapter(struct i2c_driver *driver,
1703 struct i2c_adapter *adapter)
1705 struct i2c_client *client, *_n;
1707 /* Remove the devices we created ourselves as the result of hardware
1708 * probing (using a driver's detect method) */
1709 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1710 if (client->adapter == adapter) {
1711 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1712 client->name, client->addr);
1713 list_del(&client->detected);
1714 i2c_unregister_device(client);
1719 static int __unregister_client(struct device *dev, void *dummy)
1721 struct i2c_client *client = i2c_verify_client(dev);
1722 if (client && strcmp(client->name, "dummy"))
1723 i2c_unregister_device(client);
1727 static int __unregister_dummy(struct device *dev, void *dummy)
1729 struct i2c_client *client = i2c_verify_client(dev);
1731 i2c_unregister_device(client);
1735 static int __process_removed_adapter(struct device_driver *d, void *data)
1737 i2c_do_del_adapter(to_i2c_driver(d), data);
1742 * i2c_del_adapter - unregister I2C adapter
1743 * @adap: the adapter being unregistered
1744 * Context: can sleep
1746 * This unregisters an I2C adapter which was previously registered
1747 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1749 void i2c_del_adapter(struct i2c_adapter *adap)
1751 struct i2c_adapter *found;
1752 struct i2c_client *client, *next;
1754 /* First make sure that this adapter was ever added */
1755 mutex_lock(&core_lock);
1756 found = idr_find(&i2c_adapter_idr, adap->nr);
1757 mutex_unlock(&core_lock);
1758 if (found != adap) {
1759 pr_debug("i2c-core: attempting to delete unregistered "
1760 "adapter [%s]\n", adap->name);
1764 acpi_i2c_remove_space_handler(adap);
1765 /* Tell drivers about this removal */
1766 mutex_lock(&core_lock);
1767 bus_for_each_drv(&i2c_bus_type, NULL, adap,
1768 __process_removed_adapter);
1769 mutex_unlock(&core_lock);
1771 /* Remove devices instantiated from sysfs */
1772 mutex_lock_nested(&adap->userspace_clients_lock,
1773 i2c_adapter_depth(adap));
1774 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1776 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1778 list_del(&client->detected);
1779 i2c_unregister_device(client);
1781 mutex_unlock(&adap->userspace_clients_lock);
1783 /* Detach any active clients. This can't fail, thus we do not
1784 * check the returned value. This is a two-pass process, because
1785 * we can't remove the dummy devices during the first pass: they
1786 * could have been instantiated by real devices wishing to clean
1787 * them up properly, so we give them a chance to do that first. */
1788 device_for_each_child(&adap->dev, NULL, __unregister_client);
1789 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1791 #ifdef CONFIG_I2C_COMPAT
1792 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1796 /* device name is gone after device_unregister */
1797 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1799 /* clean up the sysfs representation */
1800 init_completion(&adap->dev_released);
1801 device_unregister(&adap->dev);
1803 /* wait for sysfs to drop all references */
1804 wait_for_completion(&adap->dev_released);
1807 mutex_lock(&core_lock);
1808 idr_remove(&i2c_adapter_idr, adap->nr);
1809 mutex_unlock(&core_lock);
1811 /* Clear the device structure in case this adapter is ever going to be
1813 memset(&adap->dev, 0, sizeof(adap->dev));
1815 EXPORT_SYMBOL(i2c_del_adapter);
1817 /* ------------------------------------------------------------------------- */
1819 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1823 mutex_lock(&core_lock);
1824 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1825 mutex_unlock(&core_lock);
1829 EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1831 static int __process_new_driver(struct device *dev, void *data)
1833 if (dev->type != &i2c_adapter_type)
1835 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1839 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1840 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1843 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1847 /* Can't register until after driver model init */
1848 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1851 /* add the driver to the list of i2c drivers in the driver core */
1852 driver->driver.owner = owner;
1853 driver->driver.bus = &i2c_bus_type;
1855 /* When registration returns, the driver core
1856 * will have called probe() for all matching-but-unbound devices.
1858 res = driver_register(&driver->driver);
1862 /* Drivers should switch to dev_pm_ops instead. */
1863 if (driver->suspend)
1864 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1865 driver->driver.name);
1867 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1868 driver->driver.name);
1870 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1872 INIT_LIST_HEAD(&driver->clients);
1873 /* Walk the adapters that are already present */
1874 i2c_for_each_dev(driver, __process_new_driver);
1878 EXPORT_SYMBOL(i2c_register_driver);
1880 static int __process_removed_driver(struct device *dev, void *data)
1882 if (dev->type == &i2c_adapter_type)
1883 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1888 * i2c_del_driver - unregister I2C driver
1889 * @driver: the driver being unregistered
1890 * Context: can sleep
1892 void i2c_del_driver(struct i2c_driver *driver)
1894 i2c_for_each_dev(driver, __process_removed_driver);
1896 driver_unregister(&driver->driver);
1897 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1899 EXPORT_SYMBOL(i2c_del_driver);
1901 /* ------------------------------------------------------------------------- */
1904 * i2c_use_client - increments the reference count of the i2c client structure
1905 * @client: the client being referenced
1907 * Each live reference to a client should be refcounted. The driver model does
1908 * that automatically as part of driver binding, so that most drivers don't
1909 * need to do this explicitly: they hold a reference until they're unbound
1912 * A pointer to the client with the incremented reference counter is returned.
1914 struct i2c_client *i2c_use_client(struct i2c_client *client)
1916 if (client && get_device(&client->dev))
1920 EXPORT_SYMBOL(i2c_use_client);
1923 * i2c_release_client - release a use of the i2c client structure
1924 * @client: the client being no longer referenced
1926 * Must be called when a user of a client is finished with it.
1928 void i2c_release_client(struct i2c_client *client)
1931 put_device(&client->dev);
1933 EXPORT_SYMBOL(i2c_release_client);
1935 struct i2c_cmd_arg {
1940 static int i2c_cmd(struct device *dev, void *_arg)
1942 struct i2c_client *client = i2c_verify_client(dev);
1943 struct i2c_cmd_arg *arg = _arg;
1944 struct i2c_driver *driver;
1946 if (!client || !client->dev.driver)
1949 driver = to_i2c_driver(client->dev.driver);
1950 if (driver->command)
1951 driver->command(client, arg->cmd, arg->arg);
1955 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1957 struct i2c_cmd_arg cmd_arg;
1961 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1963 EXPORT_SYMBOL(i2c_clients_command);
1965 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
1966 static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1969 struct of_reconfig_data *rd = arg;
1970 struct i2c_adapter *adap;
1971 struct i2c_client *client;
1973 switch (of_reconfig_get_state_change(action, rd)) {
1974 case OF_RECONFIG_CHANGE_ADD:
1975 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1977 return NOTIFY_OK; /* not for us */
1979 client = of_i2c_register_device(adap, rd->dn);
1980 put_device(&adap->dev);
1982 if (IS_ERR(client)) {
1983 pr_err("%s: failed to create for '%s'\n",
1984 __func__, rd->dn->full_name);
1985 return notifier_from_errno(PTR_ERR(client));
1988 case OF_RECONFIG_CHANGE_REMOVE:
1989 /* find our device by node */
1990 client = of_find_i2c_device_by_node(rd->dn);
1992 return NOTIFY_OK; /* no? not meant for us */
1994 /* unregister takes one ref away */
1995 i2c_unregister_device(client);
1997 /* and put the reference of the find */
1998 put_device(&client->dev);
2004 static struct notifier_block i2c_of_notifier = {
2005 .notifier_call = of_i2c_notify,
2008 extern struct notifier_block i2c_of_notifier;
2009 #endif /* CONFIG_OF_DYNAMIC */
2011 static int __init i2c_init(void)
2015 retval = bus_register(&i2c_bus_type);
2018 #ifdef CONFIG_I2C_COMPAT
2019 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2020 if (!i2c_adapter_compat_class) {
2025 retval = i2c_add_driver(&dummy_driver);
2029 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2030 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
2035 #ifdef CONFIG_I2C_COMPAT
2036 class_compat_unregister(i2c_adapter_compat_class);
2039 bus_unregister(&i2c_bus_type);
2043 static void __exit i2c_exit(void)
2045 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2046 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
2047 i2c_del_driver(&dummy_driver);
2048 #ifdef CONFIG_I2C_COMPAT
2049 class_compat_unregister(i2c_adapter_compat_class);
2051 bus_unregister(&i2c_bus_type);
2052 tracepoint_synchronize_unregister();
2055 /* We must initialize early, because some subsystems register i2c drivers
2056 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2058 postcore_initcall(i2c_init);
2059 module_exit(i2c_exit);
2061 /* ----------------------------------------------------
2062 * the functional interface to the i2c busses.
2063 * ----------------------------------------------------
2067 * __i2c_transfer - unlocked flavor of i2c_transfer
2068 * @adap: Handle to I2C bus
2069 * @msgs: One or more messages to execute before STOP is issued to
2070 * terminate the operation; each message begins with a START.
2071 * @num: Number of messages to be executed.
2073 * Returns negative errno, else the number of messages executed.
2075 * Adapter lock must be held when calling this function. No debug logging
2076 * takes place. adap->algo->master_xfer existence isn't checked.
2078 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2080 unsigned long orig_jiffies;
2083 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2084 * enabled. This is an efficient way of keeping the for-loop from
2085 * being executed when not needed.
2087 if (static_key_false(&i2c_trace_msg)) {
2089 for (i = 0; i < num; i++)
2090 if (msgs[i].flags & I2C_M_RD)
2091 trace_i2c_read(adap, &msgs[i], i);
2093 trace_i2c_write(adap, &msgs[i], i);
2096 /* Retry automatically on arbitration loss */
2097 orig_jiffies = jiffies;
2098 for (ret = 0, try = 0; try <= adap->retries; try++) {
2099 ret = adap->algo->master_xfer(adap, msgs, num);
2102 if (time_after(jiffies, orig_jiffies + adap->timeout))
2106 if (static_key_false(&i2c_trace_msg)) {
2108 for (i = 0; i < ret; i++)
2109 if (msgs[i].flags & I2C_M_RD)
2110 trace_i2c_reply(adap, &msgs[i], i);
2111 trace_i2c_result(adap, i, ret);
2116 EXPORT_SYMBOL(__i2c_transfer);
2119 * i2c_transfer - execute a single or combined I2C message
2120 * @adap: Handle to I2C bus
2121 * @msgs: One or more messages to execute before STOP is issued to
2122 * terminate the operation; each message begins with a START.
2123 * @num: Number of messages to be executed.
2125 * Returns negative errno, else the number of messages executed.
2127 * Note that there is no requirement that each message be sent to
2128 * the same slave address, although that is the most common model.
2130 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2134 /* REVISIT the fault reporting model here is weak:
2136 * - When we get an error after receiving N bytes from a slave,
2137 * there is no way to report "N".
2139 * - When we get a NAK after transmitting N bytes to a slave,
2140 * there is no way to report "N" ... or to let the master
2141 * continue executing the rest of this combined message, if
2142 * that's the appropriate response.
2144 * - When for example "num" is two and we successfully complete
2145 * the first message but get an error part way through the
2146 * second, it's unclear whether that should be reported as
2147 * one (discarding status on the second message) or errno
2148 * (discarding status on the first one).
2151 if (adap->algo->master_xfer) {
2153 for (ret = 0; ret < num; ret++) {
2154 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
2155 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2156 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2157 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
2161 if (in_atomic() || irqs_disabled()) {
2162 ret = i2c_trylock_adapter(adap);
2164 /* I2C activity is ongoing. */
2167 i2c_lock_adapter(adap);
2170 ret = __i2c_transfer(adap, msgs, num);
2171 i2c_unlock_adapter(adap);
2175 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
2179 EXPORT_SYMBOL(i2c_transfer);
2182 * i2c_master_send - issue a single I2C message in master transmit mode
2183 * @client: Handle to slave device
2184 * @buf: Data that will be written to the slave
2185 * @count: How many bytes to write, must be less than 64k since msg.len is u16
2187 * Returns negative errno, or else the number of bytes written.
2189 int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
2192 struct i2c_adapter *adap = client->adapter;
2195 msg.addr = client->addr;
2196 msg.flags = client->flags & I2C_M_TEN;
2198 msg.buf = (char *)buf;
2200 ret = i2c_transfer(adap, &msg, 1);
2203 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2204 * transmitted, else error code.
2206 return (ret == 1) ? count : ret;
2208 EXPORT_SYMBOL(i2c_master_send);
2211 * i2c_master_recv - issue a single I2C message in master receive mode
2212 * @client: Handle to slave device
2213 * @buf: Where to store data read from slave
2214 * @count: How many bytes to read, must be less than 64k since msg.len is u16
2216 * Returns negative errno, or else the number of bytes read.
2218 int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
2220 struct i2c_adapter *adap = client->adapter;
2224 msg.addr = client->addr;
2225 msg.flags = client->flags & I2C_M_TEN;
2226 msg.flags |= I2C_M_RD;
2230 ret = i2c_transfer(adap, &msg, 1);
2233 * If everything went ok (i.e. 1 msg received), return #bytes received,
2236 return (ret == 1) ? count : ret;
2238 EXPORT_SYMBOL(i2c_master_recv);
2240 /* ----------------------------------------------------
2241 * the i2c address scanning function
2242 * Will not work for 10-bit addresses!
2243 * ----------------------------------------------------
2247 * Legacy default probe function, mostly relevant for SMBus. The default
2248 * probe method is a quick write, but it is known to corrupt the 24RF08
2249 * EEPROMs due to a state machine bug, and could also irreversibly
2250 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2251 * we use a short byte read instead. Also, some bus drivers don't implement
2252 * quick write, so we fallback to a byte read in that case too.
2253 * On x86, there is another special case for FSC hardware monitoring chips,
2254 * which want regular byte reads (address 0x73.) Fortunately, these are the
2255 * only known chips using this I2C address on PC hardware.
2256 * Returns 1 if probe succeeded, 0 if not.
2258 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2261 union i2c_smbus_data dummy;
2264 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2265 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2266 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2267 I2C_SMBUS_BYTE_DATA, &dummy);
2270 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2271 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
2272 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2273 I2C_SMBUS_QUICK, NULL);
2274 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2275 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2276 I2C_SMBUS_BYTE, &dummy);
2278 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2286 static int i2c_detect_address(struct i2c_client *temp_client,
2287 struct i2c_driver *driver)
2289 struct i2c_board_info info;
2290 struct i2c_adapter *adapter = temp_client->adapter;
2291 int addr = temp_client->addr;
2294 /* Make sure the address is valid */
2295 err = i2c_check_addr_validity(addr);
2297 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2302 /* Skip if already in use */
2303 if (i2c_check_addr_busy(adapter, addr))
2306 /* Make sure there is something at this address */
2307 if (!i2c_default_probe(adapter, addr))
2310 /* Finally call the custom detection function */
2311 memset(&info, 0, sizeof(struct i2c_board_info));
2313 err = driver->detect(temp_client, &info);
2315 /* -ENODEV is returned if the detection fails. We catch it
2316 here as this isn't an error. */
2317 return err == -ENODEV ? 0 : err;
2320 /* Consistency check */
2321 if (info.type[0] == '\0') {
2322 dev_err(&adapter->dev, "%s detection function provided "
2323 "no name for 0x%x\n", driver->driver.name,
2326 struct i2c_client *client;
2328 /* Detection succeeded, instantiate the device */
2329 if (adapter->class & I2C_CLASS_DEPRECATED)
2330 dev_warn(&adapter->dev,
2331 "This adapter will soon drop class based instantiation of devices. "
2332 "Please make sure client 0x%02x gets instantiated by other means. "
2333 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2336 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2337 info.type, info.addr);
2338 client = i2c_new_device(adapter, &info);
2340 list_add_tail(&client->detected, &driver->clients);
2342 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2343 info.type, info.addr);
2348 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2350 const unsigned short *address_list;
2351 struct i2c_client *temp_client;
2353 int adap_id = i2c_adapter_id(adapter);
2355 address_list = driver->address_list;
2356 if (!driver->detect || !address_list)
2359 /* Warn that the adapter lost class based instantiation */
2360 if (adapter->class == I2C_CLASS_DEPRECATED) {
2361 dev_dbg(&adapter->dev,
2362 "This adapter dropped support for I2C classes and "
2363 "won't auto-detect %s devices anymore. If you need it, check "
2364 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2365 driver->driver.name);
2369 /* Stop here if the classes do not match */
2370 if (!(adapter->class & driver->class))
2373 /* Set up a temporary client to help detect callback */
2374 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2377 temp_client->adapter = adapter;
2379 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
2380 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
2381 "addr 0x%02x\n", adap_id, address_list[i]);
2382 temp_client->addr = address_list[i];
2383 err = i2c_detect_address(temp_client, driver);
2392 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2394 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2395 I2C_SMBUS_QUICK, NULL) >= 0;
2397 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2400 i2c_new_probed_device(struct i2c_adapter *adap,
2401 struct i2c_board_info *info,
2402 unsigned short const *addr_list,
2403 int (*probe)(struct i2c_adapter *, unsigned short addr))
2408 probe = i2c_default_probe;
2410 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2411 /* Check address validity */
2412 if (i2c_check_addr_validity(addr_list[i]) < 0) {
2413 dev_warn(&adap->dev, "Invalid 7-bit address "
2414 "0x%02x\n", addr_list[i]);
2418 /* Check address availability */
2419 if (i2c_check_addr_busy(adap, addr_list[i])) {
2420 dev_dbg(&adap->dev, "Address 0x%02x already in "
2421 "use, not probing\n", addr_list[i]);
2425 /* Test address responsiveness */
2426 if (probe(adap, addr_list[i]))
2430 if (addr_list[i] == I2C_CLIENT_END) {
2431 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2435 info->addr = addr_list[i];
2436 return i2c_new_device(adap, info);
2438 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2440 struct i2c_adapter *i2c_get_adapter(int nr)
2442 struct i2c_adapter *adapter;
2444 mutex_lock(&core_lock);
2445 adapter = idr_find(&i2c_adapter_idr, nr);
2446 if (adapter && !try_module_get(adapter->owner))
2449 mutex_unlock(&core_lock);
2452 EXPORT_SYMBOL(i2c_get_adapter);
2454 void i2c_put_adapter(struct i2c_adapter *adap)
2457 module_put(adap->owner);
2459 EXPORT_SYMBOL(i2c_put_adapter);
2461 /* The SMBus parts */
2463 #define POLY (0x1070U << 3)
2464 static u8 crc8(u16 data)
2468 for (i = 0; i < 8; i++) {
2473 return (u8)(data >> 8);
2476 /* Incremental CRC8 over count bytes in the array pointed to by p */
2477 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
2481 for (i = 0; i < count; i++)
2482 crc = crc8((crc ^ p[i]) << 8);
2486 /* Assume a 7-bit address, which is reasonable for SMBus */
2487 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
2489 /* The address will be sent first */
2490 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2491 pec = i2c_smbus_pec(pec, &addr, 1);
2493 /* The data buffer follows */
2494 return i2c_smbus_pec(pec, msg->buf, msg->len);
2497 /* Used for write only transactions */
2498 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
2500 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2504 /* Return <0 on CRC error
2505 If there was a write before this read (most cases) we need to take the
2506 partial CRC from the write part into account.
2507 Note that this function does modify the message (we need to decrease the
2508 message length to hide the CRC byte from the caller). */
2509 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
2511 u8 rpec = msg->buf[--msg->len];
2512 cpec = i2c_smbus_msg_pec(cpec, msg);
2515 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2523 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2524 * @client: Handle to slave device
2526 * This executes the SMBus "receive byte" protocol, returning negative errno
2527 * else the byte received from the device.
2529 s32 i2c_smbus_read_byte(const struct i2c_client *client)
2531 union i2c_smbus_data data;
2534 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2536 I2C_SMBUS_BYTE, &data);
2537 return (status < 0) ? status : data.byte;
2539 EXPORT_SYMBOL(i2c_smbus_read_byte);
2542 * i2c_smbus_write_byte - SMBus "send byte" protocol
2543 * @client: Handle to slave device
2544 * @value: Byte to be sent
2546 * This executes the SMBus "send byte" protocol, returning negative errno
2547 * else zero on success.
2549 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
2551 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2552 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
2554 EXPORT_SYMBOL(i2c_smbus_write_byte);
2557 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2558 * @client: Handle to slave device
2559 * @command: Byte interpreted by slave
2561 * This executes the SMBus "read byte" protocol, returning negative errno
2562 * else a data byte received from the device.
2564 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
2566 union i2c_smbus_data data;
2569 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2570 I2C_SMBUS_READ, command,
2571 I2C_SMBUS_BYTE_DATA, &data);
2572 return (status < 0) ? status : data.byte;
2574 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
2577 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2578 * @client: Handle to slave device
2579 * @command: Byte interpreted by slave
2580 * @value: Byte being written
2582 * This executes the SMBus "write byte" protocol, returning negative errno
2583 * else zero on success.
2585 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2588 union i2c_smbus_data data;
2590 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2591 I2C_SMBUS_WRITE, command,
2592 I2C_SMBUS_BYTE_DATA, &data);
2594 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
2597 * i2c_smbus_read_word_data - SMBus "read word" protocol
2598 * @client: Handle to slave device
2599 * @command: Byte interpreted by slave
2601 * This executes the SMBus "read word" protocol, returning negative errno
2602 * else a 16-bit unsigned "word" received from the device.
2604 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
2606 union i2c_smbus_data data;
2609 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2610 I2C_SMBUS_READ, command,
2611 I2C_SMBUS_WORD_DATA, &data);
2612 return (status < 0) ? status : data.word;
2614 EXPORT_SYMBOL(i2c_smbus_read_word_data);
2617 * i2c_smbus_write_word_data - SMBus "write word" protocol
2618 * @client: Handle to slave device
2619 * @command: Byte interpreted by slave
2620 * @value: 16-bit "word" being written
2622 * This executes the SMBus "write word" protocol, returning negative errno
2623 * else zero on success.
2625 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2628 union i2c_smbus_data data;
2630 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2631 I2C_SMBUS_WRITE, command,
2632 I2C_SMBUS_WORD_DATA, &data);
2634 EXPORT_SYMBOL(i2c_smbus_write_word_data);
2637 * i2c_smbus_read_block_data - SMBus "block read" protocol
2638 * @client: Handle to slave device
2639 * @command: Byte interpreted by slave
2640 * @values: Byte array into which data will be read; big enough to hold
2641 * the data returned by the slave. SMBus allows at most 32 bytes.
2643 * This executes the SMBus "block read" protocol, returning negative errno
2644 * else the number of data bytes in the slave's response.
2646 * Note that using this function requires that the client's adapter support
2647 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2648 * support this; its emulation through I2C messaging relies on a specific
2649 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2651 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
2654 union i2c_smbus_data data;
2657 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2658 I2C_SMBUS_READ, command,
2659 I2C_SMBUS_BLOCK_DATA, &data);
2663 memcpy(values, &data.block[1], data.block[0]);
2664 return data.block[0];
2666 EXPORT_SYMBOL(i2c_smbus_read_block_data);
2669 * i2c_smbus_write_block_data - SMBus "block write" protocol
2670 * @client: Handle to slave device
2671 * @command: Byte interpreted by slave
2672 * @length: Size of data block; SMBus allows at most 32 bytes
2673 * @values: Byte array which will be written.
2675 * This executes the SMBus "block write" protocol, returning negative errno
2676 * else zero on success.
2678 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
2679 u8 length, const u8 *values)
2681 union i2c_smbus_data data;
2683 if (length > I2C_SMBUS_BLOCK_MAX)
2684 length = I2C_SMBUS_BLOCK_MAX;
2685 data.block[0] = length;
2686 memcpy(&data.block[1], values, length);
2687 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2688 I2C_SMBUS_WRITE, command,
2689 I2C_SMBUS_BLOCK_DATA, &data);
2691 EXPORT_SYMBOL(i2c_smbus_write_block_data);
2693 /* Returns the number of read bytes */
2694 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
2695 u8 length, u8 *values)
2697 union i2c_smbus_data data;
2700 if (length > I2C_SMBUS_BLOCK_MAX)
2701 length = I2C_SMBUS_BLOCK_MAX;
2702 data.block[0] = length;
2703 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2704 I2C_SMBUS_READ, command,
2705 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2709 memcpy(values, &data.block[1], data.block[0]);
2710 return data.block[0];
2712 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
2714 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
2715 u8 length, const u8 *values)
2717 union i2c_smbus_data data;
2719 if (length > I2C_SMBUS_BLOCK_MAX)
2720 length = I2C_SMBUS_BLOCK_MAX;
2721 data.block[0] = length;
2722 memcpy(data.block + 1, values, length);
2723 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2724 I2C_SMBUS_WRITE, command,
2725 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2727 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
2729 /* Simulate a SMBus command using the i2c protocol
2730 No checking of parameters is done! */
2731 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2732 unsigned short flags,
2733 char read_write, u8 command, int size,
2734 union i2c_smbus_data *data)
2736 /* So we need to generate a series of msgs. In the case of writing, we
2737 need to use only one message; when reading, we need two. We initialize
2738 most things with sane defaults, to keep the code below somewhat
2740 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2741 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
2742 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
2746 struct i2c_msg msg[2] = {
2754 .flags = flags | I2C_M_RD,
2760 msgbuf0[0] = command;
2762 case I2C_SMBUS_QUICK:
2764 /* Special case: The read/write field is used as data */
2765 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2769 case I2C_SMBUS_BYTE:
2770 if (read_write == I2C_SMBUS_READ) {
2771 /* Special case: only a read! */
2772 msg[0].flags = I2C_M_RD | flags;
2776 case I2C_SMBUS_BYTE_DATA:
2777 if (read_write == I2C_SMBUS_READ)
2781 msgbuf0[1] = data->byte;
2784 case I2C_SMBUS_WORD_DATA:
2785 if (read_write == I2C_SMBUS_READ)
2789 msgbuf0[1] = data->word & 0xff;
2790 msgbuf0[2] = data->word >> 8;
2793 case I2C_SMBUS_PROC_CALL:
2794 num = 2; /* Special case */
2795 read_write = I2C_SMBUS_READ;
2798 msgbuf0[1] = data->word & 0xff;
2799 msgbuf0[2] = data->word >> 8;
2801 case I2C_SMBUS_BLOCK_DATA:
2802 if (read_write == I2C_SMBUS_READ) {
2803 msg[1].flags |= I2C_M_RECV_LEN;
2804 msg[1].len = 1; /* block length will be added by
2805 the underlying bus driver */
2807 msg[0].len = data->block[0] + 2;
2808 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
2809 dev_err(&adapter->dev,
2810 "Invalid block write size %d\n",
2814 for (i = 1; i < msg[0].len; i++)
2815 msgbuf0[i] = data->block[i-1];
2818 case I2C_SMBUS_BLOCK_PROC_CALL:
2819 num = 2; /* Another special case */
2820 read_write = I2C_SMBUS_READ;
2821 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
2822 dev_err(&adapter->dev,
2823 "Invalid block write size %d\n",
2827 msg[0].len = data->block[0] + 2;
2828 for (i = 1; i < msg[0].len; i++)
2829 msgbuf0[i] = data->block[i-1];
2830 msg[1].flags |= I2C_M_RECV_LEN;
2831 msg[1].len = 1; /* block length will be added by
2832 the underlying bus driver */
2834 case I2C_SMBUS_I2C_BLOCK_DATA:
2835 if (read_write == I2C_SMBUS_READ) {
2836 msg[1].len = data->block[0];
2838 msg[0].len = data->block[0] + 1;
2839 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
2840 dev_err(&adapter->dev,
2841 "Invalid block write size %d\n",
2845 for (i = 1; i <= data->block[0]; i++)
2846 msgbuf0[i] = data->block[i];
2850 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2854 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2855 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2857 /* Compute PEC if first message is a write */
2858 if (!(msg[0].flags & I2C_M_RD)) {
2859 if (num == 1) /* Write only */
2860 i2c_smbus_add_pec(&msg[0]);
2861 else /* Write followed by read */
2862 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2864 /* Ask for PEC if last message is a read */
2865 if (msg[num-1].flags & I2C_M_RD)
2869 status = i2c_transfer(adapter, msg, num);
2873 /* Check PEC if last message is a read */
2874 if (i && (msg[num-1].flags & I2C_M_RD)) {
2875 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2880 if (read_write == I2C_SMBUS_READ)
2882 case I2C_SMBUS_BYTE:
2883 data->byte = msgbuf0[0];
2885 case I2C_SMBUS_BYTE_DATA:
2886 data->byte = msgbuf1[0];
2888 case I2C_SMBUS_WORD_DATA:
2889 case I2C_SMBUS_PROC_CALL:
2890 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2892 case I2C_SMBUS_I2C_BLOCK_DATA:
2893 for (i = 0; i < data->block[0]; i++)
2894 data->block[i+1] = msgbuf1[i];
2896 case I2C_SMBUS_BLOCK_DATA:
2897 case I2C_SMBUS_BLOCK_PROC_CALL:
2898 for (i = 0; i < msgbuf1[0] + 1; i++)
2899 data->block[i] = msgbuf1[i];
2906 * i2c_smbus_xfer - execute SMBus protocol operations
2907 * @adapter: Handle to I2C bus
2908 * @addr: Address of SMBus slave on that bus
2909 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2910 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2911 * @command: Byte interpreted by slave, for protocols which use such bytes
2912 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2913 * @data: Data to be read or written
2915 * This executes an SMBus protocol operation, and returns a negative
2916 * errno code else zero on success.
2918 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2919 char read_write, u8 command, int protocol,
2920 union i2c_smbus_data *data)
2922 unsigned long orig_jiffies;
2926 /* If enabled, the following two tracepoints are conditional on
2927 * read_write and protocol.
2929 trace_smbus_write(adapter, addr, flags, read_write,
2930 command, protocol, data);
2931 trace_smbus_read(adapter, addr, flags, read_write,
2934 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
2936 if (adapter->algo->smbus_xfer) {
2937 i2c_lock_adapter(adapter);
2939 /* Retry automatically on arbitration loss */
2940 orig_jiffies = jiffies;
2941 for (res = 0, try = 0; try <= adapter->retries; try++) {
2942 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2943 read_write, command,
2947 if (time_after(jiffies,
2948 orig_jiffies + adapter->timeout))
2951 i2c_unlock_adapter(adapter);
2953 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2956 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2957 * implement native support for the SMBus operation.
2961 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2962 command, protocol, data);
2965 /* If enabled, the reply tracepoint is conditional on read_write. */
2966 trace_smbus_reply(adapter, addr, flags, read_write,
2967 command, protocol, data);
2968 trace_smbus_result(adapter, addr, flags, read_write,
2969 command, protocol, res);
2973 EXPORT_SYMBOL(i2c_smbus_xfer);
2975 int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2979 if (!client || !slave_cb)
2982 if (!(client->flags & I2C_CLIENT_TEN)) {
2983 /* Enforce stricter address checking */
2984 ret = i2c_check_addr_validity(client->addr);
2989 if (!client->adapter->algo->reg_slave)
2992 client->slave_cb = slave_cb;
2994 i2c_lock_adapter(client->adapter);
2995 ret = client->adapter->algo->reg_slave(client);
2996 i2c_unlock_adapter(client->adapter);
2999 client->slave_cb = NULL;
3003 EXPORT_SYMBOL_GPL(i2c_slave_register);
3005 int i2c_slave_unregister(struct i2c_client *client)
3009 if (!client->adapter->algo->unreg_slave)
3012 i2c_lock_adapter(client->adapter);
3013 ret = client->adapter->algo->unreg_slave(client);
3014 i2c_unlock_adapter(client->adapter);
3017 client->slave_cb = NULL;
3021 EXPORT_SYMBOL_GPL(i2c_slave_unregister);
3024 MODULE_DESCRIPTION("I2C-Bus main module");
3025 MODULE_LICENSE("GPL");