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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 /* ------------------------------------------------------------------------- */
30 I2C ACPI code Copyright (C) 2014 Intel Corp
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/gpio.h>
39 #include <linux/slab.h>
40 #include <linux/i2c.h>
41 #include <linux/init.h>
42 #include <linux/idr.h>
43 #include <linux/mutex.h>
45 #include <linux/of_device.h>
46 #include <linux/of_irq.h>
47 #include <linux/clk/clk-conf.h>
48 #include <linux/completion.h>
49 #include <linux/hardirq.h>
50 #include <linux/irqflags.h>
51 #include <linux/rwsem.h>
52 #include <linux/pm_runtime.h>
53 #include <linux/pm_domain.h>
54 #include <linux/acpi.h>
55 #include <linux/jump_label.h>
56 #include <asm/uaccess.h>
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/i2c.h>
63 /* core_lock protects i2c_adapter_idr, and guarantees
64 that device detection, deletion of detected devices, and attach_adapter
65 calls are serialized */
66 static DEFINE_MUTEX(core_lock);
67 static DEFINE_IDR(i2c_adapter_idr);
69 static struct device_type i2c_client_type;
70 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
72 static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
74 void i2c_transfer_trace_reg(void)
76 static_key_slow_inc(&i2c_trace_msg);
79 void i2c_transfer_trace_unreg(void)
81 static_key_slow_dec(&i2c_trace_msg);
84 #if defined(CONFIG_ACPI)
85 struct acpi_i2c_handler_data {
86 struct acpi_connection_info info;
87 struct i2c_adapter *adapter;
100 static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
102 struct i2c_board_info *info = data;
104 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
105 struct acpi_resource_i2c_serialbus *sb;
107 sb = &ares->data.i2c_serial_bus;
108 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
109 info->addr = sb->slave_address;
110 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
111 info->flags |= I2C_CLIENT_TEN;
113 } else if (info->irq < 0) {
116 if (acpi_dev_resource_interrupt(ares, 0, &r))
120 /* Tell the ACPI core to skip this resource */
124 static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
125 void *data, void **return_value)
127 struct i2c_adapter *adapter = data;
128 struct list_head resource_list;
129 struct i2c_board_info info;
130 struct acpi_device *adev;
133 if (acpi_bus_get_device(handle, &adev))
135 if (acpi_bus_get_status(adev) || !adev->status.present)
138 memset(&info, 0, sizeof(info));
139 info.acpi_node.companion = adev;
142 INIT_LIST_HEAD(&resource_list);
143 ret = acpi_dev_get_resources(adev, &resource_list,
144 acpi_i2c_add_resource, &info);
145 acpi_dev_free_resource_list(&resource_list);
147 if (ret < 0 || !info.addr)
150 adev->power.flags.ignore_parent = true;
151 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
152 if (!i2c_new_device(adapter, &info)) {
153 adev->power.flags.ignore_parent = false;
154 dev_err(&adapter->dev,
155 "failed to add I2C device %s from ACPI\n",
156 dev_name(&adev->dev));
163 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
164 * @adap: pointer to adapter
166 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
167 * namespace. When a device is found it will be added to the Linux device
168 * model and bound to the corresponding ACPI handle.
170 static void acpi_i2c_register_devices(struct i2c_adapter *adap)
175 if (!adap->dev.parent)
178 handle = ACPI_HANDLE(adap->dev.parent);
182 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
183 acpi_i2c_add_device, NULL,
185 if (ACPI_FAILURE(status))
186 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
189 #else /* CONFIG_ACPI */
190 static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
191 #endif /* CONFIG_ACPI */
193 #ifdef CONFIG_ACPI_I2C_OPREGION
194 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
195 u8 cmd, u8 *data, u8 data_len)
198 struct i2c_msg msgs[2];
202 buffer = kzalloc(data_len, GFP_KERNEL);
206 msgs[0].addr = client->addr;
207 msgs[0].flags = client->flags;
211 msgs[1].addr = client->addr;
212 msgs[1].flags = client->flags | I2C_M_RD;
213 msgs[1].len = data_len;
214 msgs[1].buf = buffer;
216 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
218 dev_err(&client->adapter->dev, "i2c read failed\n");
220 memcpy(data, buffer, data_len);
226 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
227 u8 cmd, u8 *data, u8 data_len)
230 struct i2c_msg msgs[1];
234 buffer = kzalloc(data_len + 1, GFP_KERNEL);
239 memcpy(buffer + 1, data, data_len);
241 msgs[0].addr = client->addr;
242 msgs[0].flags = client->flags;
243 msgs[0].len = data_len + 1;
244 msgs[0].buf = buffer;
246 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
248 dev_err(&client->adapter->dev, "i2c write failed\n");
255 acpi_i2c_space_handler(u32 function, acpi_physical_address command,
256 u32 bits, u64 *value64,
257 void *handler_context, void *region_context)
259 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
260 struct acpi_i2c_handler_data *data = handler_context;
261 struct acpi_connection_info *info = &data->info;
262 struct acpi_resource_i2c_serialbus *sb;
263 struct i2c_adapter *adapter = data->adapter;
264 struct i2c_client client;
265 struct acpi_resource *ares;
266 u32 accessor_type = function >> 16;
267 u8 action = function & ACPI_IO_MASK;
268 acpi_status ret = AE_OK;
271 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
272 if (ACPI_FAILURE(ret))
275 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
276 ret = AE_BAD_PARAMETER;
280 sb = &ares->data.i2c_serial_bus;
281 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
282 ret = AE_BAD_PARAMETER;
286 memset(&client, 0, sizeof(client));
287 client.adapter = adapter;
288 client.addr = sb->slave_address;
291 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
292 client.flags |= I2C_CLIENT_TEN;
294 switch (accessor_type) {
295 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
296 if (action == ACPI_READ) {
297 status = i2c_smbus_read_byte(&client);
303 status = i2c_smbus_write_byte(&client, gsb->bdata);
307 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
308 if (action == ACPI_READ) {
309 status = i2c_smbus_read_byte_data(&client, command);
315 status = i2c_smbus_write_byte_data(&client, command,
320 case ACPI_GSB_ACCESS_ATTRIB_WORD:
321 if (action == ACPI_READ) {
322 status = i2c_smbus_read_word_data(&client, command);
328 status = i2c_smbus_write_word_data(&client, command,
333 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
334 if (action == ACPI_READ) {
335 status = i2c_smbus_read_block_data(&client, command,
342 status = i2c_smbus_write_block_data(&client, command,
343 gsb->len, gsb->data);
347 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
348 if (action == ACPI_READ) {
349 status = acpi_gsb_i2c_read_bytes(&client, command,
350 gsb->data, info->access_length);
354 status = acpi_gsb_i2c_write_bytes(&client, command,
355 gsb->data, info->access_length);
360 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
361 ret = AE_BAD_PARAMETER;
365 gsb->status = status;
373 static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
376 struct acpi_i2c_handler_data *data;
379 if (!adapter->dev.parent)
382 handle = ACPI_HANDLE(adapter->dev.parent);
387 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
392 data->adapter = adapter;
393 status = acpi_bus_attach_private_data(handle, (void *)data);
394 if (ACPI_FAILURE(status)) {
399 status = acpi_install_address_space_handler(handle,
400 ACPI_ADR_SPACE_GSBUS,
401 &acpi_i2c_space_handler,
404 if (ACPI_FAILURE(status)) {
405 dev_err(&adapter->dev, "Error installing i2c space handler\n");
406 acpi_bus_detach_private_data(handle);
414 static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
417 struct acpi_i2c_handler_data *data;
420 if (!adapter->dev.parent)
423 handle = ACPI_HANDLE(adapter->dev.parent);
428 acpi_remove_address_space_handler(handle,
429 ACPI_ADR_SPACE_GSBUS,
430 &acpi_i2c_space_handler);
432 status = acpi_bus_get_private_data(handle, (void **)&data);
433 if (ACPI_SUCCESS(status))
436 acpi_bus_detach_private_data(handle);
438 #else /* CONFIG_ACPI_I2C_OPREGION */
439 static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
442 static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
444 #endif /* CONFIG_ACPI_I2C_OPREGION */
446 /* ------------------------------------------------------------------------- */
448 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
449 const struct i2c_client *client)
451 while (id->name[0]) {
452 if (strcmp(client->name, id->name) == 0)
459 static int i2c_device_match(struct device *dev, struct device_driver *drv)
461 struct i2c_client *client = i2c_verify_client(dev);
462 struct i2c_driver *driver;
467 /* Attempt an OF style match */
468 if (of_driver_match_device(dev, drv))
471 /* Then ACPI style match */
472 if (acpi_driver_match_device(dev, drv))
475 driver = to_i2c_driver(drv);
476 /* match on an id table if there is one */
477 if (driver->id_table)
478 return i2c_match_id(driver->id_table, client) != NULL;
484 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
485 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
487 struct i2c_client *client = to_i2c_client(dev);
490 rc = acpi_device_uevent_modalias(dev, env);
494 if (add_uevent_var(env, "MODALIAS=%s%s",
495 I2C_MODULE_PREFIX, client->name))
497 dev_dbg(dev, "uevent\n");
501 /* i2c bus recovery routines */
502 static int get_scl_gpio_value(struct i2c_adapter *adap)
504 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
507 static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
509 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
512 static int get_sda_gpio_value(struct i2c_adapter *adap)
514 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
517 static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
519 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
520 struct device *dev = &adap->dev;
523 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
524 GPIOF_OUT_INIT_HIGH, "i2c-scl");
526 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
531 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
532 /* work without SDA polling */
533 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
542 static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
544 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
547 gpio_free(bri->sda_gpio);
549 gpio_free(bri->scl_gpio);
553 * We are generating clock pulses. ndelay() determines durating of clk pulses.
554 * We will generate clock with rate 100 KHz and so duration of both clock levels
555 * is: delay in ns = (10^6 / 100) / 2
557 #define RECOVERY_NDELAY 5000
558 #define RECOVERY_CLK_CNT 9
560 static int i2c_generic_recovery(struct i2c_adapter *adap)
562 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
563 int i = 0, val = 1, ret = 0;
565 if (bri->prepare_recovery)
566 bri->prepare_recovery(bri);
569 * By this time SCL is high, as we need to give 9 falling-rising edges
571 while (i++ < RECOVERY_CLK_CNT * 2) {
573 /* Break if SDA is high */
574 if (bri->get_sda && bri->get_sda(adap))
576 /* SCL shouldn't be low here */
577 if (!bri->get_scl(adap)) {
579 "SCL is stuck low, exit recovery\n");
586 bri->set_scl(adap, val);
587 ndelay(RECOVERY_NDELAY);
590 if (bri->unprepare_recovery)
591 bri->unprepare_recovery(bri);
596 int i2c_generic_scl_recovery(struct i2c_adapter *adap)
598 adap->bus_recovery_info->set_scl(adap, 1);
599 return i2c_generic_recovery(adap);
602 int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
606 ret = i2c_get_gpios_for_recovery(adap);
610 ret = i2c_generic_recovery(adap);
611 i2c_put_gpios_for_recovery(adap);
616 int i2c_recover_bus(struct i2c_adapter *adap)
618 if (!adap->bus_recovery_info)
621 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
622 return adap->bus_recovery_info->recover_bus(adap);
625 static int i2c_device_probe(struct device *dev)
627 struct i2c_client *client = i2c_verify_client(dev);
628 struct i2c_driver *driver;
634 driver = to_i2c_driver(dev->driver);
635 if (!driver->probe || !driver->id_table)
638 if (!device_can_wakeup(&client->dev))
639 device_init_wakeup(&client->dev,
640 client->flags & I2C_CLIENT_WAKE);
641 dev_dbg(dev, "probe\n");
643 status = of_clk_set_defaults(dev->of_node, false);
647 status = dev_pm_domain_attach(&client->dev, true);
648 if (status != -EPROBE_DEFER) {
649 status = driver->probe(client, i2c_match_id(driver->id_table,
652 dev_pm_domain_detach(&client->dev, true);
658 static int i2c_device_remove(struct device *dev)
660 struct i2c_client *client = i2c_verify_client(dev);
661 struct i2c_driver *driver;
664 if (!client || !dev->driver)
667 driver = to_i2c_driver(dev->driver);
668 if (driver->remove) {
669 dev_dbg(dev, "remove\n");
670 status = driver->remove(client);
673 dev_pm_domain_detach(&client->dev, true);
677 static void i2c_device_shutdown(struct device *dev)
679 struct i2c_client *client = i2c_verify_client(dev);
680 struct i2c_driver *driver;
682 if (!client || !dev->driver)
684 driver = to_i2c_driver(dev->driver);
685 if (driver->shutdown)
686 driver->shutdown(client);
689 #ifdef CONFIG_PM_SLEEP
690 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
692 struct i2c_client *client = i2c_verify_client(dev);
693 struct i2c_driver *driver;
695 if (!client || !dev->driver)
697 driver = to_i2c_driver(dev->driver);
698 if (!driver->suspend)
700 return driver->suspend(client, mesg);
703 static int i2c_legacy_resume(struct device *dev)
705 struct i2c_client *client = i2c_verify_client(dev);
706 struct i2c_driver *driver;
708 if (!client || !dev->driver)
710 driver = to_i2c_driver(dev->driver);
713 return driver->resume(client);
716 static int i2c_device_pm_suspend(struct device *dev)
718 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
721 return pm_generic_suspend(dev);
723 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
726 static int i2c_device_pm_resume(struct device *dev)
728 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
731 return pm_generic_resume(dev);
733 return i2c_legacy_resume(dev);
736 static int i2c_device_pm_freeze(struct device *dev)
738 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
741 return pm_generic_freeze(dev);
743 return i2c_legacy_suspend(dev, PMSG_FREEZE);
746 static int i2c_device_pm_thaw(struct device *dev)
748 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
751 return pm_generic_thaw(dev);
753 return i2c_legacy_resume(dev);
756 static int i2c_device_pm_poweroff(struct device *dev)
758 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
761 return pm_generic_poweroff(dev);
763 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
766 static int i2c_device_pm_restore(struct device *dev)
768 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
771 return pm_generic_restore(dev);
773 return i2c_legacy_resume(dev);
775 #else /* !CONFIG_PM_SLEEP */
776 #define i2c_device_pm_suspend NULL
777 #define i2c_device_pm_resume NULL
778 #define i2c_device_pm_freeze NULL
779 #define i2c_device_pm_thaw NULL
780 #define i2c_device_pm_poweroff NULL
781 #define i2c_device_pm_restore NULL
782 #endif /* !CONFIG_PM_SLEEP */
784 static void i2c_client_dev_release(struct device *dev)
786 kfree(to_i2c_client(dev));
790 show_name(struct device *dev, struct device_attribute *attr, char *buf)
792 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
793 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
797 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
799 struct i2c_client *client = to_i2c_client(dev);
802 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
806 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
809 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
810 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
812 static struct attribute *i2c_dev_attrs[] = {
814 /* modalias helps coldplug: modprobe $(cat .../modalias) */
815 &dev_attr_modalias.attr,
819 static struct attribute_group i2c_dev_attr_group = {
820 .attrs = i2c_dev_attrs,
823 static const struct attribute_group *i2c_dev_attr_groups[] = {
828 static const struct dev_pm_ops i2c_device_pm_ops = {
829 .suspend = i2c_device_pm_suspend,
830 .resume = i2c_device_pm_resume,
831 .freeze = i2c_device_pm_freeze,
832 .thaw = i2c_device_pm_thaw,
833 .poweroff = i2c_device_pm_poweroff,
834 .restore = i2c_device_pm_restore,
836 pm_generic_runtime_suspend,
837 pm_generic_runtime_resume,
842 struct bus_type i2c_bus_type = {
844 .match = i2c_device_match,
845 .probe = i2c_device_probe,
846 .remove = i2c_device_remove,
847 .shutdown = i2c_device_shutdown,
848 .pm = &i2c_device_pm_ops,
850 EXPORT_SYMBOL_GPL(i2c_bus_type);
852 static struct device_type i2c_client_type = {
853 .groups = i2c_dev_attr_groups,
854 .uevent = i2c_device_uevent,
855 .release = i2c_client_dev_release,
860 * i2c_verify_client - return parameter as i2c_client, or NULL
861 * @dev: device, probably from some driver model iterator
863 * When traversing the driver model tree, perhaps using driver model
864 * iterators like @device_for_each_child(), you can't assume very much
865 * about the nodes you find. Use this function to avoid oopses caused
866 * by wrongly treating some non-I2C device as an i2c_client.
868 struct i2c_client *i2c_verify_client(struct device *dev)
870 return (dev->type == &i2c_client_type)
874 EXPORT_SYMBOL(i2c_verify_client);
877 /* This is a permissive address validity check, I2C address map constraints
878 * are purposely not enforced, except for the general call address. */
879 static int i2c_check_client_addr_validity(const struct i2c_client *client)
881 if (client->flags & I2C_CLIENT_TEN) {
882 /* 10-bit address, all values are valid */
883 if (client->addr > 0x3ff)
886 /* 7-bit address, reject the general call address */
887 if (client->addr == 0x00 || client->addr > 0x7f)
893 /* And this is a strict address validity check, used when probing. If a
894 * device uses a reserved address, then it shouldn't be probed. 7-bit
895 * addressing is assumed, 10-bit address devices are rare and should be
896 * explicitly enumerated. */
897 static int i2c_check_addr_validity(unsigned short addr)
900 * Reserved addresses per I2C specification:
901 * 0x00 General call address / START byte
903 * 0x02 Reserved for different bus format
904 * 0x03 Reserved for future purposes
905 * 0x04-0x07 Hs-mode master code
906 * 0x78-0x7b 10-bit slave addressing
907 * 0x7c-0x7f Reserved for future purposes
909 if (addr < 0x08 || addr > 0x77)
914 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
916 struct i2c_client *client = i2c_verify_client(dev);
917 int addr = *(int *)addrp;
919 if (client && client->addr == addr)
924 /* walk up mux tree */
925 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
927 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
930 result = device_for_each_child(&adapter->dev, &addr,
931 __i2c_check_addr_busy);
933 if (!result && parent)
934 result = i2c_check_mux_parents(parent, addr);
939 /* recurse down mux tree */
940 static int i2c_check_mux_children(struct device *dev, void *addrp)
944 if (dev->type == &i2c_adapter_type)
945 result = device_for_each_child(dev, addrp,
946 i2c_check_mux_children);
948 result = __i2c_check_addr_busy(dev, addrp);
953 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
955 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
959 result = i2c_check_mux_parents(parent, addr);
962 result = device_for_each_child(&adapter->dev, &addr,
963 i2c_check_mux_children);
969 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
970 * @adapter: Target I2C bus segment
972 void i2c_lock_adapter(struct i2c_adapter *adapter)
974 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
977 i2c_lock_adapter(parent);
979 rt_mutex_lock(&adapter->bus_lock);
981 EXPORT_SYMBOL_GPL(i2c_lock_adapter);
984 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
985 * @adapter: Target I2C bus segment
987 static int i2c_trylock_adapter(struct i2c_adapter *adapter)
989 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
992 return i2c_trylock_adapter(parent);
994 return rt_mutex_trylock(&adapter->bus_lock);
998 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
999 * @adapter: Target I2C bus segment
1001 void i2c_unlock_adapter(struct i2c_adapter *adapter)
1003 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1006 i2c_unlock_adapter(parent);
1008 rt_mutex_unlock(&adapter->bus_lock);
1010 EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1012 static void i2c_dev_set_name(struct i2c_adapter *adap,
1013 struct i2c_client *client)
1015 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1018 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1022 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1023 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1024 client->addr | ((client->flags & I2C_CLIENT_TEN)
1029 * i2c_new_device - instantiate an i2c device
1030 * @adap: the adapter managing the device
1031 * @info: describes one I2C device; bus_num is ignored
1032 * Context: can sleep
1034 * Create an i2c device. Binding is handled through driver model
1035 * probe()/remove() methods. A driver may be bound to this device when we
1036 * return from this function, or any later moment (e.g. maybe hotplugging will
1037 * load the driver module). This call is not appropriate for use by mainboard
1038 * initialization logic, which usually runs during an arch_initcall() long
1039 * before any i2c_adapter could exist.
1041 * This returns the new i2c client, which may be saved for later use with
1042 * i2c_unregister_device(); or NULL to indicate an error.
1045 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1047 struct i2c_client *client;
1050 client = kzalloc(sizeof *client, GFP_KERNEL);
1054 client->adapter = adap;
1056 client->dev.platform_data = info->platform_data;
1059 client->dev.archdata = *info->archdata;
1061 client->flags = info->flags;
1062 client->addr = info->addr;
1063 client->irq = info->irq;
1065 strlcpy(client->name, info->type, sizeof(client->name));
1067 /* Check for address validity */
1068 status = i2c_check_client_addr_validity(client);
1070 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1071 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1072 goto out_err_silent;
1075 /* Check for address business */
1076 status = i2c_check_addr_busy(adap, client->addr);
1080 client->dev.parent = &client->adapter->dev;
1081 client->dev.bus = &i2c_bus_type;
1082 client->dev.type = &i2c_client_type;
1083 client->dev.of_node = info->of_node;
1084 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
1086 i2c_dev_set_name(adap, client);
1087 status = device_register(&client->dev);
1091 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1092 client->name, dev_name(&client->dev));
1097 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1098 "(%d)\n", client->name, client->addr, status);
1103 EXPORT_SYMBOL_GPL(i2c_new_device);
1107 * i2c_unregister_device - reverse effect of i2c_new_device()
1108 * @client: value returned from i2c_new_device()
1109 * Context: can sleep
1111 void i2c_unregister_device(struct i2c_client *client)
1113 device_unregister(&client->dev);
1115 EXPORT_SYMBOL_GPL(i2c_unregister_device);
1118 static const struct i2c_device_id dummy_id[] = {
1123 static int dummy_probe(struct i2c_client *client,
1124 const struct i2c_device_id *id)
1129 static int dummy_remove(struct i2c_client *client)
1134 static struct i2c_driver dummy_driver = {
1135 .driver.name = "dummy",
1136 .probe = dummy_probe,
1137 .remove = dummy_remove,
1138 .id_table = dummy_id,
1142 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1143 * @adapter: the adapter managing the device
1144 * @address: seven bit address to be used
1145 * Context: can sleep
1147 * This returns an I2C client bound to the "dummy" driver, intended for use
1148 * with devices that consume multiple addresses. Examples of such chips
1149 * include various EEPROMS (like 24c04 and 24c08 models).
1151 * These dummy devices have two main uses. First, most I2C and SMBus calls
1152 * except i2c_transfer() need a client handle; the dummy will be that handle.
1153 * And second, this prevents the specified address from being bound to a
1156 * This returns the new i2c client, which should be saved for later use with
1157 * i2c_unregister_device(); or NULL to indicate an error.
1159 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
1161 struct i2c_board_info info = {
1162 I2C_BOARD_INFO("dummy", address),
1165 return i2c_new_device(adapter, &info);
1167 EXPORT_SYMBOL_GPL(i2c_new_dummy);
1169 /* ------------------------------------------------------------------------- */
1171 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1173 static void i2c_adapter_dev_release(struct device *dev)
1175 struct i2c_adapter *adap = to_i2c_adapter(dev);
1176 complete(&adap->dev_released);
1180 * This function is only needed for mutex_lock_nested, so it is never
1181 * called unless locking correctness checking is enabled. Thus we
1182 * make it inline to avoid a compiler warning. That's what gcc ends up
1185 static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1187 unsigned int depth = 0;
1189 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1196 * Let users instantiate I2C devices through sysfs. This can be used when
1197 * platform initialization code doesn't contain the proper data for
1198 * whatever reason. Also useful for drivers that do device detection and
1199 * detection fails, either because the device uses an unexpected address,
1200 * or this is a compatible device with different ID register values.
1202 * Parameter checking may look overzealous, but we really don't want
1203 * the user to provide incorrect parameters.
1206 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1207 const char *buf, size_t count)
1209 struct i2c_adapter *adap = to_i2c_adapter(dev);
1210 struct i2c_board_info info;
1211 struct i2c_client *client;
1215 memset(&info, 0, sizeof(struct i2c_board_info));
1217 blank = strchr(buf, ' ');
1219 dev_err(dev, "%s: Missing parameters\n", "new_device");
1222 if (blank - buf > I2C_NAME_SIZE - 1) {
1223 dev_err(dev, "%s: Invalid device name\n", "new_device");
1226 memcpy(info.type, buf, blank - buf);
1228 /* Parse remaining parameters, reject extra parameters */
1229 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1231 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1234 if (res > 1 && end != '\n') {
1235 dev_err(dev, "%s: Extra parameters\n", "new_device");
1239 client = i2c_new_device(adap, &info);
1243 /* Keep track of the added device */
1244 mutex_lock(&adap->userspace_clients_lock);
1245 list_add_tail(&client->detected, &adap->userspace_clients);
1246 mutex_unlock(&adap->userspace_clients_lock);
1247 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1248 info.type, info.addr);
1254 * And of course let the users delete the devices they instantiated, if
1255 * they got it wrong. This interface can only be used to delete devices
1256 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1257 * don't delete devices to which some kernel code still has references.
1259 * Parameter checking may look overzealous, but we really don't want
1260 * the user to delete the wrong device.
1263 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1264 const char *buf, size_t count)
1266 struct i2c_adapter *adap = to_i2c_adapter(dev);
1267 struct i2c_client *client, *next;
1268 unsigned short addr;
1272 /* Parse parameters, reject extra parameters */
1273 res = sscanf(buf, "%hi%c", &addr, &end);
1275 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1278 if (res > 1 && end != '\n') {
1279 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1283 /* Make sure the device was added through sysfs */
1285 mutex_lock_nested(&adap->userspace_clients_lock,
1286 i2c_adapter_depth(adap));
1287 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1289 if (client->addr == addr) {
1290 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1291 "delete_device", client->name, client->addr);
1293 list_del(&client->detected);
1294 i2c_unregister_device(client);
1299 mutex_unlock(&adap->userspace_clients_lock);
1302 dev_err(dev, "%s: Can't find device in list\n",
1307 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
1308 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1309 i2c_sysfs_delete_device);
1311 static struct attribute *i2c_adapter_attrs[] = {
1312 &dev_attr_name.attr,
1313 &dev_attr_new_device.attr,
1314 &dev_attr_delete_device.attr,
1318 static struct attribute_group i2c_adapter_attr_group = {
1319 .attrs = i2c_adapter_attrs,
1322 static const struct attribute_group *i2c_adapter_attr_groups[] = {
1323 &i2c_adapter_attr_group,
1327 struct device_type i2c_adapter_type = {
1328 .groups = i2c_adapter_attr_groups,
1329 .release = i2c_adapter_dev_release,
1331 EXPORT_SYMBOL_GPL(i2c_adapter_type);
1334 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1335 * @dev: device, probably from some driver model iterator
1337 * When traversing the driver model tree, perhaps using driver model
1338 * iterators like @device_for_each_child(), you can't assume very much
1339 * about the nodes you find. Use this function to avoid oopses caused
1340 * by wrongly treating some non-I2C device as an i2c_adapter.
1342 struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1344 return (dev->type == &i2c_adapter_type)
1345 ? to_i2c_adapter(dev)
1348 EXPORT_SYMBOL(i2c_verify_adapter);
1350 #ifdef CONFIG_I2C_COMPAT
1351 static struct class_compat *i2c_adapter_compat_class;
1354 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1356 struct i2c_devinfo *devinfo;
1358 down_read(&__i2c_board_lock);
1359 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1360 if (devinfo->busnum == adapter->nr
1361 && !i2c_new_device(adapter,
1362 &devinfo->board_info))
1363 dev_err(&adapter->dev,
1364 "Can't create device at 0x%02x\n",
1365 devinfo->board_info.addr);
1367 up_read(&__i2c_board_lock);
1370 /* OF support code */
1372 #if IS_ENABLED(CONFIG_OF)
1373 static void of_i2c_register_devices(struct i2c_adapter *adap)
1376 struct device_node *node;
1378 /* Only register child devices if the adapter has a node pointer set */
1379 if (!adap->dev.of_node)
1382 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1384 for_each_available_child_of_node(adap->dev.of_node, node) {
1385 struct i2c_board_info info = {};
1386 struct dev_archdata dev_ad = {};
1390 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1392 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1393 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1398 addr = of_get_property(node, "reg", &len);
1399 if (!addr || (len < sizeof(int))) {
1400 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1405 info.addr = be32_to_cpup(addr);
1406 if (info.addr > (1 << 10) - 1) {
1407 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1408 info.addr, node->full_name);
1412 info.irq = irq_of_parse_and_map(node, 0);
1413 info.of_node = of_node_get(node);
1414 info.archdata = &dev_ad;
1416 if (of_get_property(node, "wakeup-source", NULL))
1417 info.flags |= I2C_CLIENT_WAKE;
1419 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1421 result = i2c_new_device(adap, &info);
1422 if (result == NULL) {
1423 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1426 irq_dispose_mapping(info.irq);
1432 static int of_dev_node_match(struct device *dev, void *data)
1434 return dev->of_node == data;
1437 /* must call put_device() when done with returned i2c_client device */
1438 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1442 dev = bus_find_device(&i2c_bus_type, NULL, node,
1447 return i2c_verify_client(dev);
1449 EXPORT_SYMBOL(of_find_i2c_device_by_node);
1451 /* must call put_device() when done with returned i2c_adapter device */
1452 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1456 dev = bus_find_device(&i2c_bus_type, NULL, node,
1461 return i2c_verify_adapter(dev);
1463 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1465 static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1466 #endif /* CONFIG_OF */
1468 static int i2c_do_add_adapter(struct i2c_driver *driver,
1469 struct i2c_adapter *adap)
1471 /* Detect supported devices on that bus, and instantiate them */
1472 i2c_detect(adap, driver);
1474 /* Let legacy drivers scan this bus for matching devices */
1475 if (driver->attach_adapter) {
1476 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1477 driver->driver.name);
1478 dev_warn(&adap->dev, "Please use another way to instantiate "
1479 "your i2c_client\n");
1480 /* We ignore the return code; if it fails, too bad */
1481 driver->attach_adapter(adap);
1486 static int __process_new_adapter(struct device_driver *d, void *data)
1488 return i2c_do_add_adapter(to_i2c_driver(d), data);
1491 static int i2c_register_adapter(struct i2c_adapter *adap)
1495 /* Can't register until after driver model init */
1496 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1502 if (unlikely(adap->name[0] == '\0')) {
1503 pr_err("i2c-core: Attempt to register an adapter with "
1507 if (unlikely(!adap->algo)) {
1508 pr_err("i2c-core: Attempt to register adapter '%s' with "
1509 "no algo!\n", adap->name);
1513 rt_mutex_init(&adap->bus_lock);
1514 mutex_init(&adap->userspace_clients_lock);
1515 INIT_LIST_HEAD(&adap->userspace_clients);
1517 /* Set default timeout to 1 second if not already set */
1518 if (adap->timeout == 0)
1521 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
1522 adap->dev.bus = &i2c_bus_type;
1523 adap->dev.type = &i2c_adapter_type;
1524 res = device_register(&adap->dev);
1528 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1530 #ifdef CONFIG_I2C_COMPAT
1531 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1534 dev_warn(&adap->dev,
1535 "Failed to create compatibility class link\n");
1538 /* bus recovery specific initialization */
1539 if (adap->bus_recovery_info) {
1540 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1542 if (!bri->recover_bus) {
1543 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1544 adap->bus_recovery_info = NULL;
1548 /* Generic GPIO recovery */
1549 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1550 if (!gpio_is_valid(bri->scl_gpio)) {
1551 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1552 adap->bus_recovery_info = NULL;
1556 if (gpio_is_valid(bri->sda_gpio))
1557 bri->get_sda = get_sda_gpio_value;
1559 bri->get_sda = NULL;
1561 bri->get_scl = get_scl_gpio_value;
1562 bri->set_scl = set_scl_gpio_value;
1563 } else if (!bri->set_scl || !bri->get_scl) {
1564 /* Generic SCL recovery */
1565 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1566 adap->bus_recovery_info = NULL;
1571 /* create pre-declared device nodes */
1572 of_i2c_register_devices(adap);
1573 acpi_i2c_register_devices(adap);
1574 acpi_i2c_install_space_handler(adap);
1576 if (adap->nr < __i2c_first_dynamic_bus_num)
1577 i2c_scan_static_board_info(adap);
1579 /* Notify drivers */
1580 mutex_lock(&core_lock);
1581 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1582 mutex_unlock(&core_lock);
1587 mutex_lock(&core_lock);
1588 idr_remove(&i2c_adapter_idr, adap->nr);
1589 mutex_unlock(&core_lock);
1594 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1595 * @adap: the adapter to register (with adap->nr initialized)
1596 * Context: can sleep
1598 * See i2c_add_numbered_adapter() for details.
1600 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1604 mutex_lock(&core_lock);
1605 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1607 mutex_unlock(&core_lock);
1609 return id == -ENOSPC ? -EBUSY : id;
1611 return i2c_register_adapter(adap);
1615 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1616 * @adapter: the adapter to add
1617 * Context: can sleep
1619 * This routine is used to declare an I2C adapter when its bus number
1620 * doesn't matter or when its bus number is specified by an dt alias.
1621 * Examples of bases when the bus number doesn't matter: I2C adapters
1622 * dynamically added by USB links or PCI plugin cards.
1624 * When this returns zero, a new bus number was allocated and stored
1625 * in adap->nr, and the specified adapter became available for clients.
1626 * Otherwise, a negative errno value is returned.
1628 int i2c_add_adapter(struct i2c_adapter *adapter)
1630 struct device *dev = &adapter->dev;
1634 id = of_alias_get_id(dev->of_node, "i2c");
1637 return __i2c_add_numbered_adapter(adapter);
1641 mutex_lock(&core_lock);
1642 id = idr_alloc(&i2c_adapter_idr, adapter,
1643 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
1644 mutex_unlock(&core_lock);
1650 return i2c_register_adapter(adapter);
1652 EXPORT_SYMBOL(i2c_add_adapter);
1655 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1656 * @adap: the adapter to register (with adap->nr initialized)
1657 * Context: can sleep
1659 * This routine is used to declare an I2C adapter when its bus number
1660 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1661 * or otherwise built in to the system's mainboard, and where i2c_board_info
1662 * is used to properly configure I2C devices.
1664 * If the requested bus number is set to -1, then this function will behave
1665 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1667 * If no devices have pre-been declared for this bus, then be sure to
1668 * register the adapter before any dynamically allocated ones. Otherwise
1669 * the required bus ID may not be available.
1671 * When this returns zero, the specified adapter became available for
1672 * clients using the bus number provided in adap->nr. Also, the table
1673 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1674 * and the appropriate driver model device nodes are created. Otherwise, a
1675 * negative errno value is returned.
1677 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1679 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1680 return i2c_add_adapter(adap);
1682 return __i2c_add_numbered_adapter(adap);
1684 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1686 static void i2c_do_del_adapter(struct i2c_driver *driver,
1687 struct i2c_adapter *adapter)
1689 struct i2c_client *client, *_n;
1691 /* Remove the devices we created ourselves as the result of hardware
1692 * probing (using a driver's detect method) */
1693 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1694 if (client->adapter == adapter) {
1695 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1696 client->name, client->addr);
1697 list_del(&client->detected);
1698 i2c_unregister_device(client);
1703 static int __unregister_client(struct device *dev, void *dummy)
1705 struct i2c_client *client = i2c_verify_client(dev);
1706 if (client && strcmp(client->name, "dummy"))
1707 i2c_unregister_device(client);
1711 static int __unregister_dummy(struct device *dev, void *dummy)
1713 struct i2c_client *client = i2c_verify_client(dev);
1715 i2c_unregister_device(client);
1719 static int __process_removed_adapter(struct device_driver *d, void *data)
1721 i2c_do_del_adapter(to_i2c_driver(d), data);
1726 * i2c_del_adapter - unregister I2C adapter
1727 * @adap: the adapter being unregistered
1728 * Context: can sleep
1730 * This unregisters an I2C adapter which was previously registered
1731 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1733 void i2c_del_adapter(struct i2c_adapter *adap)
1735 struct i2c_adapter *found;
1736 struct i2c_client *client, *next;
1738 /* First make sure that this adapter was ever added */
1739 mutex_lock(&core_lock);
1740 found = idr_find(&i2c_adapter_idr, adap->nr);
1741 mutex_unlock(&core_lock);
1742 if (found != adap) {
1743 pr_debug("i2c-core: attempting to delete unregistered "
1744 "adapter [%s]\n", adap->name);
1748 acpi_i2c_remove_space_handler(adap);
1749 /* Tell drivers about this removal */
1750 mutex_lock(&core_lock);
1751 bus_for_each_drv(&i2c_bus_type, NULL, adap,
1752 __process_removed_adapter);
1753 mutex_unlock(&core_lock);
1755 /* Remove devices instantiated from sysfs */
1756 mutex_lock_nested(&adap->userspace_clients_lock,
1757 i2c_adapter_depth(adap));
1758 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1760 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1762 list_del(&client->detected);
1763 i2c_unregister_device(client);
1765 mutex_unlock(&adap->userspace_clients_lock);
1767 /* Detach any active clients. This can't fail, thus we do not
1768 * check the returned value. This is a two-pass process, because
1769 * we can't remove the dummy devices during the first pass: they
1770 * could have been instantiated by real devices wishing to clean
1771 * them up properly, so we give them a chance to do that first. */
1772 device_for_each_child(&adap->dev, NULL, __unregister_client);
1773 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1775 #ifdef CONFIG_I2C_COMPAT
1776 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1780 /* device name is gone after device_unregister */
1781 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1783 /* clean up the sysfs representation */
1784 init_completion(&adap->dev_released);
1785 device_unregister(&adap->dev);
1787 /* wait for sysfs to drop all references */
1788 wait_for_completion(&adap->dev_released);
1791 mutex_lock(&core_lock);
1792 idr_remove(&i2c_adapter_idr, adap->nr);
1793 mutex_unlock(&core_lock);
1795 /* Clear the device structure in case this adapter is ever going to be
1797 memset(&adap->dev, 0, sizeof(adap->dev));
1799 EXPORT_SYMBOL(i2c_del_adapter);
1801 /* ------------------------------------------------------------------------- */
1803 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1807 mutex_lock(&core_lock);
1808 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1809 mutex_unlock(&core_lock);
1813 EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1815 static int __process_new_driver(struct device *dev, void *data)
1817 if (dev->type != &i2c_adapter_type)
1819 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1823 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1824 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1827 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1831 /* Can't register until after driver model init */
1832 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1835 /* add the driver to the list of i2c drivers in the driver core */
1836 driver->driver.owner = owner;
1837 driver->driver.bus = &i2c_bus_type;
1839 /* When registration returns, the driver core
1840 * will have called probe() for all matching-but-unbound devices.
1842 res = driver_register(&driver->driver);
1846 /* Drivers should switch to dev_pm_ops instead. */
1847 if (driver->suspend)
1848 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1849 driver->driver.name);
1851 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1852 driver->driver.name);
1854 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1856 INIT_LIST_HEAD(&driver->clients);
1857 /* Walk the adapters that are already present */
1858 i2c_for_each_dev(driver, __process_new_driver);
1862 EXPORT_SYMBOL(i2c_register_driver);
1864 static int __process_removed_driver(struct device *dev, void *data)
1866 if (dev->type == &i2c_adapter_type)
1867 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1872 * i2c_del_driver - unregister I2C driver
1873 * @driver: the driver being unregistered
1874 * Context: can sleep
1876 void i2c_del_driver(struct i2c_driver *driver)
1878 i2c_for_each_dev(driver, __process_removed_driver);
1880 driver_unregister(&driver->driver);
1881 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1883 EXPORT_SYMBOL(i2c_del_driver);
1885 /* ------------------------------------------------------------------------- */
1888 * i2c_use_client - increments the reference count of the i2c client structure
1889 * @client: the client being referenced
1891 * Each live reference to a client should be refcounted. The driver model does
1892 * that automatically as part of driver binding, so that most drivers don't
1893 * need to do this explicitly: they hold a reference until they're unbound
1896 * A pointer to the client with the incremented reference counter is returned.
1898 struct i2c_client *i2c_use_client(struct i2c_client *client)
1900 if (client && get_device(&client->dev))
1904 EXPORT_SYMBOL(i2c_use_client);
1907 * i2c_release_client - release a use of the i2c client structure
1908 * @client: the client being no longer referenced
1910 * Must be called when a user of a client is finished with it.
1912 void i2c_release_client(struct i2c_client *client)
1915 put_device(&client->dev);
1917 EXPORT_SYMBOL(i2c_release_client);
1919 struct i2c_cmd_arg {
1924 static int i2c_cmd(struct device *dev, void *_arg)
1926 struct i2c_client *client = i2c_verify_client(dev);
1927 struct i2c_cmd_arg *arg = _arg;
1928 struct i2c_driver *driver;
1930 if (!client || !client->dev.driver)
1933 driver = to_i2c_driver(client->dev.driver);
1934 if (driver->command)
1935 driver->command(client, arg->cmd, arg->arg);
1939 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1941 struct i2c_cmd_arg cmd_arg;
1945 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1947 EXPORT_SYMBOL(i2c_clients_command);
1949 static int __init i2c_init(void)
1953 retval = bus_register(&i2c_bus_type);
1956 #ifdef CONFIG_I2C_COMPAT
1957 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1958 if (!i2c_adapter_compat_class) {
1963 retval = i2c_add_driver(&dummy_driver);
1969 #ifdef CONFIG_I2C_COMPAT
1970 class_compat_unregister(i2c_adapter_compat_class);
1973 bus_unregister(&i2c_bus_type);
1977 static void __exit i2c_exit(void)
1979 i2c_del_driver(&dummy_driver);
1980 #ifdef CONFIG_I2C_COMPAT
1981 class_compat_unregister(i2c_adapter_compat_class);
1983 bus_unregister(&i2c_bus_type);
1984 tracepoint_synchronize_unregister();
1987 /* We must initialize early, because some subsystems register i2c drivers
1988 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1990 postcore_initcall(i2c_init);
1991 module_exit(i2c_exit);
1993 /* ----------------------------------------------------
1994 * the functional interface to the i2c busses.
1995 * ----------------------------------------------------
1999 * __i2c_transfer - unlocked flavor of i2c_transfer
2000 * @adap: Handle to I2C bus
2001 * @msgs: One or more messages to execute before STOP is issued to
2002 * terminate the operation; each message begins with a START.
2003 * @num: Number of messages to be executed.
2005 * Returns negative errno, else the number of messages executed.
2007 * Adapter lock must be held when calling this function. No debug logging
2008 * takes place. adap->algo->master_xfer existence isn't checked.
2010 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2012 unsigned long orig_jiffies;
2015 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2016 * enabled. This is an efficient way of keeping the for-loop from
2017 * being executed when not needed.
2019 if (static_key_false(&i2c_trace_msg)) {
2021 for (i = 0; i < num; i++)
2022 if (msgs[i].flags & I2C_M_RD)
2023 trace_i2c_read(adap, &msgs[i], i);
2025 trace_i2c_write(adap, &msgs[i], i);
2028 /* Retry automatically on arbitration loss */
2029 orig_jiffies = jiffies;
2030 for (ret = 0, try = 0; try <= adap->retries; try++) {
2031 ret = adap->algo->master_xfer(adap, msgs, num);
2034 if (time_after(jiffies, orig_jiffies + adap->timeout))
2038 if (static_key_false(&i2c_trace_msg)) {
2040 for (i = 0; i < ret; i++)
2041 if (msgs[i].flags & I2C_M_RD)
2042 trace_i2c_reply(adap, &msgs[i], i);
2043 trace_i2c_result(adap, i, ret);
2048 EXPORT_SYMBOL(__i2c_transfer);
2051 * i2c_transfer - execute a single or combined I2C message
2052 * @adap: Handle to I2C bus
2053 * @msgs: One or more messages to execute before STOP is issued to
2054 * terminate the operation; each message begins with a START.
2055 * @num: Number of messages to be executed.
2057 * Returns negative errno, else the number of messages executed.
2059 * Note that there is no requirement that each message be sent to
2060 * the same slave address, although that is the most common model.
2062 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2066 /* REVISIT the fault reporting model here is weak:
2068 * - When we get an error after receiving N bytes from a slave,
2069 * there is no way to report "N".
2071 * - When we get a NAK after transmitting N bytes to a slave,
2072 * there is no way to report "N" ... or to let the master
2073 * continue executing the rest of this combined message, if
2074 * that's the appropriate response.
2076 * - When for example "num" is two and we successfully complete
2077 * the first message but get an error part way through the
2078 * second, it's unclear whether that should be reported as
2079 * one (discarding status on the second message) or errno
2080 * (discarding status on the first one).
2083 if (adap->algo->master_xfer) {
2085 for (ret = 0; ret < num; ret++) {
2086 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
2087 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2088 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2089 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
2093 if (in_atomic() || irqs_disabled()) {
2094 ret = i2c_trylock_adapter(adap);
2096 /* I2C activity is ongoing. */
2099 i2c_lock_adapter(adap);
2102 ret = __i2c_transfer(adap, msgs, num);
2103 i2c_unlock_adapter(adap);
2107 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
2111 EXPORT_SYMBOL(i2c_transfer);
2114 * i2c_master_send - issue a single I2C message in master transmit mode
2115 * @client: Handle to slave device
2116 * @buf: Data that will be written to the slave
2117 * @count: How many bytes to write, must be less than 64k since msg.len is u16
2119 * Returns negative errno, or else the number of bytes written.
2121 int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
2124 struct i2c_adapter *adap = client->adapter;
2127 msg.addr = client->addr;
2128 msg.flags = client->flags & I2C_M_TEN;
2130 msg.buf = (char *)buf;
2132 ret = i2c_transfer(adap, &msg, 1);
2135 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2136 * transmitted, else error code.
2138 return (ret == 1) ? count : ret;
2140 EXPORT_SYMBOL(i2c_master_send);
2143 * i2c_master_recv - issue a single I2C message in master receive mode
2144 * @client: Handle to slave device
2145 * @buf: Where to store data read from slave
2146 * @count: How many bytes to read, must be less than 64k since msg.len is u16
2148 * Returns negative errno, or else the number of bytes read.
2150 int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
2152 struct i2c_adapter *adap = client->adapter;
2156 msg.addr = client->addr;
2157 msg.flags = client->flags & I2C_M_TEN;
2158 msg.flags |= I2C_M_RD;
2162 ret = i2c_transfer(adap, &msg, 1);
2165 * If everything went ok (i.e. 1 msg received), return #bytes received,
2168 return (ret == 1) ? count : ret;
2170 EXPORT_SYMBOL(i2c_master_recv);
2172 /* ----------------------------------------------------
2173 * the i2c address scanning function
2174 * Will not work for 10-bit addresses!
2175 * ----------------------------------------------------
2179 * Legacy default probe function, mostly relevant for SMBus. The default
2180 * probe method is a quick write, but it is known to corrupt the 24RF08
2181 * EEPROMs due to a state machine bug, and could also irreversibly
2182 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2183 * we use a short byte read instead. Also, some bus drivers don't implement
2184 * quick write, so we fallback to a byte read in that case too.
2185 * On x86, there is another special case for FSC hardware monitoring chips,
2186 * which want regular byte reads (address 0x73.) Fortunately, these are the
2187 * only known chips using this I2C address on PC hardware.
2188 * Returns 1 if probe succeeded, 0 if not.
2190 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2193 union i2c_smbus_data dummy;
2196 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2197 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2198 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2199 I2C_SMBUS_BYTE_DATA, &dummy);
2202 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2203 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
2204 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2205 I2C_SMBUS_QUICK, NULL);
2206 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2207 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2208 I2C_SMBUS_BYTE, &dummy);
2210 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2218 static int i2c_detect_address(struct i2c_client *temp_client,
2219 struct i2c_driver *driver)
2221 struct i2c_board_info info;
2222 struct i2c_adapter *adapter = temp_client->adapter;
2223 int addr = temp_client->addr;
2226 /* Make sure the address is valid */
2227 err = i2c_check_addr_validity(addr);
2229 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2234 /* Skip if already in use */
2235 if (i2c_check_addr_busy(adapter, addr))
2238 /* Make sure there is something at this address */
2239 if (!i2c_default_probe(adapter, addr))
2242 /* Finally call the custom detection function */
2243 memset(&info, 0, sizeof(struct i2c_board_info));
2245 err = driver->detect(temp_client, &info);
2247 /* -ENODEV is returned if the detection fails. We catch it
2248 here as this isn't an error. */
2249 return err == -ENODEV ? 0 : err;
2252 /* Consistency check */
2253 if (info.type[0] == '\0') {
2254 dev_err(&adapter->dev, "%s detection function provided "
2255 "no name for 0x%x\n", driver->driver.name,
2258 struct i2c_client *client;
2260 /* Detection succeeded, instantiate the device */
2261 if (adapter->class & I2C_CLASS_DEPRECATED)
2262 dev_warn(&adapter->dev,
2263 "This adapter will soon drop class based instantiation of devices. "
2264 "Please make sure client 0x%02x gets instantiated by other means. "
2265 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2268 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2269 info.type, info.addr);
2270 client = i2c_new_device(adapter, &info);
2272 list_add_tail(&client->detected, &driver->clients);
2274 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2275 info.type, info.addr);
2280 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2282 const unsigned short *address_list;
2283 struct i2c_client *temp_client;
2285 int adap_id = i2c_adapter_id(adapter);
2287 address_list = driver->address_list;
2288 if (!driver->detect || !address_list)
2291 /* Warn that the adapter lost class based instantiation */
2292 if (adapter->class == I2C_CLASS_DEPRECATED) {
2293 dev_dbg(&adapter->dev,
2294 "This adapter dropped support for I2C classes and "
2295 "won't auto-detect %s devices anymore. If you need it, check "
2296 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2297 driver->driver.name);
2301 /* Stop here if the classes do not match */
2302 if (!(adapter->class & driver->class))
2305 /* Set up a temporary client to help detect callback */
2306 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2309 temp_client->adapter = adapter;
2311 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
2312 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
2313 "addr 0x%02x\n", adap_id, address_list[i]);
2314 temp_client->addr = address_list[i];
2315 err = i2c_detect_address(temp_client, driver);
2324 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2326 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2327 I2C_SMBUS_QUICK, NULL) >= 0;
2329 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2332 i2c_new_probed_device(struct i2c_adapter *adap,
2333 struct i2c_board_info *info,
2334 unsigned short const *addr_list,
2335 int (*probe)(struct i2c_adapter *, unsigned short addr))
2340 probe = i2c_default_probe;
2342 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2343 /* Check address validity */
2344 if (i2c_check_addr_validity(addr_list[i]) < 0) {
2345 dev_warn(&adap->dev, "Invalid 7-bit address "
2346 "0x%02x\n", addr_list[i]);
2350 /* Check address availability */
2351 if (i2c_check_addr_busy(adap, addr_list[i])) {
2352 dev_dbg(&adap->dev, "Address 0x%02x already in "
2353 "use, not probing\n", addr_list[i]);
2357 /* Test address responsiveness */
2358 if (probe(adap, addr_list[i]))
2362 if (addr_list[i] == I2C_CLIENT_END) {
2363 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2367 info->addr = addr_list[i];
2368 return i2c_new_device(adap, info);
2370 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2372 struct i2c_adapter *i2c_get_adapter(int nr)
2374 struct i2c_adapter *adapter;
2376 mutex_lock(&core_lock);
2377 adapter = idr_find(&i2c_adapter_idr, nr);
2378 if (adapter && !try_module_get(adapter->owner))
2381 mutex_unlock(&core_lock);
2384 EXPORT_SYMBOL(i2c_get_adapter);
2386 void i2c_put_adapter(struct i2c_adapter *adap)
2389 module_put(adap->owner);
2391 EXPORT_SYMBOL(i2c_put_adapter);
2393 /* The SMBus parts */
2395 #define POLY (0x1070U << 3)
2396 static u8 crc8(u16 data)
2400 for (i = 0; i < 8; i++) {
2405 return (u8)(data >> 8);
2408 /* Incremental CRC8 over count bytes in the array pointed to by p */
2409 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
2413 for (i = 0; i < count; i++)
2414 crc = crc8((crc ^ p[i]) << 8);
2418 /* Assume a 7-bit address, which is reasonable for SMBus */
2419 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
2421 /* The address will be sent first */
2422 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2423 pec = i2c_smbus_pec(pec, &addr, 1);
2425 /* The data buffer follows */
2426 return i2c_smbus_pec(pec, msg->buf, msg->len);
2429 /* Used for write only transactions */
2430 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
2432 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2436 /* Return <0 on CRC error
2437 If there was a write before this read (most cases) we need to take the
2438 partial CRC from the write part into account.
2439 Note that this function does modify the message (we need to decrease the
2440 message length to hide the CRC byte from the caller). */
2441 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
2443 u8 rpec = msg->buf[--msg->len];
2444 cpec = i2c_smbus_msg_pec(cpec, msg);
2447 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2455 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2456 * @client: Handle to slave device
2458 * This executes the SMBus "receive byte" protocol, returning negative errno
2459 * else the byte received from the device.
2461 s32 i2c_smbus_read_byte(const struct i2c_client *client)
2463 union i2c_smbus_data data;
2466 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2468 I2C_SMBUS_BYTE, &data);
2469 return (status < 0) ? status : data.byte;
2471 EXPORT_SYMBOL(i2c_smbus_read_byte);
2474 * i2c_smbus_write_byte - SMBus "send byte" protocol
2475 * @client: Handle to slave device
2476 * @value: Byte to be sent
2478 * This executes the SMBus "send byte" protocol, returning negative errno
2479 * else zero on success.
2481 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
2483 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2484 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
2486 EXPORT_SYMBOL(i2c_smbus_write_byte);
2489 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2490 * @client: Handle to slave device
2491 * @command: Byte interpreted by slave
2493 * This executes the SMBus "read byte" protocol, returning negative errno
2494 * else a data byte received from the device.
2496 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
2498 union i2c_smbus_data data;
2501 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2502 I2C_SMBUS_READ, command,
2503 I2C_SMBUS_BYTE_DATA, &data);
2504 return (status < 0) ? status : data.byte;
2506 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
2509 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2510 * @client: Handle to slave device
2511 * @command: Byte interpreted by slave
2512 * @value: Byte being written
2514 * This executes the SMBus "write byte" protocol, returning negative errno
2515 * else zero on success.
2517 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2520 union i2c_smbus_data data;
2522 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2523 I2C_SMBUS_WRITE, command,
2524 I2C_SMBUS_BYTE_DATA, &data);
2526 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
2529 * i2c_smbus_read_word_data - SMBus "read word" protocol
2530 * @client: Handle to slave device
2531 * @command: Byte interpreted by slave
2533 * This executes the SMBus "read word" protocol, returning negative errno
2534 * else a 16-bit unsigned "word" received from the device.
2536 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
2538 union i2c_smbus_data data;
2541 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2542 I2C_SMBUS_READ, command,
2543 I2C_SMBUS_WORD_DATA, &data);
2544 return (status < 0) ? status : data.word;
2546 EXPORT_SYMBOL(i2c_smbus_read_word_data);
2549 * i2c_smbus_write_word_data - SMBus "write word" protocol
2550 * @client: Handle to slave device
2551 * @command: Byte interpreted by slave
2552 * @value: 16-bit "word" being written
2554 * This executes the SMBus "write word" protocol, returning negative errno
2555 * else zero on success.
2557 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2560 union i2c_smbus_data data;
2562 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2563 I2C_SMBUS_WRITE, command,
2564 I2C_SMBUS_WORD_DATA, &data);
2566 EXPORT_SYMBOL(i2c_smbus_write_word_data);
2569 * i2c_smbus_read_block_data - SMBus "block read" protocol
2570 * @client: Handle to slave device
2571 * @command: Byte interpreted by slave
2572 * @values: Byte array into which data will be read; big enough to hold
2573 * the data returned by the slave. SMBus allows at most 32 bytes.
2575 * This executes the SMBus "block read" protocol, returning negative errno
2576 * else the number of data bytes in the slave's response.
2578 * Note that using this function requires that the client's adapter support
2579 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2580 * support this; its emulation through I2C messaging relies on a specific
2581 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2583 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
2586 union i2c_smbus_data data;
2589 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2590 I2C_SMBUS_READ, command,
2591 I2C_SMBUS_BLOCK_DATA, &data);
2595 memcpy(values, &data.block[1], data.block[0]);
2596 return data.block[0];
2598 EXPORT_SYMBOL(i2c_smbus_read_block_data);
2601 * i2c_smbus_write_block_data - SMBus "block write" protocol
2602 * @client: Handle to slave device
2603 * @command: Byte interpreted by slave
2604 * @length: Size of data block; SMBus allows at most 32 bytes
2605 * @values: Byte array which will be written.
2607 * This executes the SMBus "block write" protocol, returning negative errno
2608 * else zero on success.
2610 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
2611 u8 length, const u8 *values)
2613 union i2c_smbus_data data;
2615 if (length > I2C_SMBUS_BLOCK_MAX)
2616 length = I2C_SMBUS_BLOCK_MAX;
2617 data.block[0] = length;
2618 memcpy(&data.block[1], values, length);
2619 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2620 I2C_SMBUS_WRITE, command,
2621 I2C_SMBUS_BLOCK_DATA, &data);
2623 EXPORT_SYMBOL(i2c_smbus_write_block_data);
2625 /* Returns the number of read bytes */
2626 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
2627 u8 length, u8 *values)
2629 union i2c_smbus_data data;
2632 if (length > I2C_SMBUS_BLOCK_MAX)
2633 length = I2C_SMBUS_BLOCK_MAX;
2634 data.block[0] = length;
2635 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2636 I2C_SMBUS_READ, command,
2637 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2641 memcpy(values, &data.block[1], data.block[0]);
2642 return data.block[0];
2644 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
2646 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
2647 u8 length, const u8 *values)
2649 union i2c_smbus_data data;
2651 if (length > I2C_SMBUS_BLOCK_MAX)
2652 length = I2C_SMBUS_BLOCK_MAX;
2653 data.block[0] = length;
2654 memcpy(data.block + 1, values, length);
2655 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2656 I2C_SMBUS_WRITE, command,
2657 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2659 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
2661 /* Simulate a SMBus command using the i2c protocol
2662 No checking of parameters is done! */
2663 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2664 unsigned short flags,
2665 char read_write, u8 command, int size,
2666 union i2c_smbus_data *data)
2668 /* So we need to generate a series of msgs. In the case of writing, we
2669 need to use only one message; when reading, we need two. We initialize
2670 most things with sane defaults, to keep the code below somewhat
2672 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2673 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
2674 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
2678 struct i2c_msg msg[2] = {
2686 .flags = flags | I2C_M_RD,
2692 msgbuf0[0] = command;
2694 case I2C_SMBUS_QUICK:
2696 /* Special case: The read/write field is used as data */
2697 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2701 case I2C_SMBUS_BYTE:
2702 if (read_write == I2C_SMBUS_READ) {
2703 /* Special case: only a read! */
2704 msg[0].flags = I2C_M_RD | flags;
2708 case I2C_SMBUS_BYTE_DATA:
2709 if (read_write == I2C_SMBUS_READ)
2713 msgbuf0[1] = data->byte;
2716 case I2C_SMBUS_WORD_DATA:
2717 if (read_write == I2C_SMBUS_READ)
2721 msgbuf0[1] = data->word & 0xff;
2722 msgbuf0[2] = data->word >> 8;
2725 case I2C_SMBUS_PROC_CALL:
2726 num = 2; /* Special case */
2727 read_write = I2C_SMBUS_READ;
2730 msgbuf0[1] = data->word & 0xff;
2731 msgbuf0[2] = data->word >> 8;
2733 case I2C_SMBUS_BLOCK_DATA:
2734 if (read_write == I2C_SMBUS_READ) {
2735 msg[1].flags |= I2C_M_RECV_LEN;
2736 msg[1].len = 1; /* block length will be added by
2737 the underlying bus driver */
2739 msg[0].len = data->block[0] + 2;
2740 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
2741 dev_err(&adapter->dev,
2742 "Invalid block write size %d\n",
2746 for (i = 1; i < msg[0].len; i++)
2747 msgbuf0[i] = data->block[i-1];
2750 case I2C_SMBUS_BLOCK_PROC_CALL:
2751 num = 2; /* Another special case */
2752 read_write = I2C_SMBUS_READ;
2753 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
2754 dev_err(&adapter->dev,
2755 "Invalid block write size %d\n",
2759 msg[0].len = data->block[0] + 2;
2760 for (i = 1; i < msg[0].len; i++)
2761 msgbuf0[i] = data->block[i-1];
2762 msg[1].flags |= I2C_M_RECV_LEN;
2763 msg[1].len = 1; /* block length will be added by
2764 the underlying bus driver */
2766 case I2C_SMBUS_I2C_BLOCK_DATA:
2767 if (read_write == I2C_SMBUS_READ) {
2768 msg[1].len = data->block[0];
2770 msg[0].len = data->block[0] + 1;
2771 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
2772 dev_err(&adapter->dev,
2773 "Invalid block write size %d\n",
2777 for (i = 1; i <= data->block[0]; i++)
2778 msgbuf0[i] = data->block[i];
2782 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2786 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2787 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2789 /* Compute PEC if first message is a write */
2790 if (!(msg[0].flags & I2C_M_RD)) {
2791 if (num == 1) /* Write only */
2792 i2c_smbus_add_pec(&msg[0]);
2793 else /* Write followed by read */
2794 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2796 /* Ask for PEC if last message is a read */
2797 if (msg[num-1].flags & I2C_M_RD)
2801 status = i2c_transfer(adapter, msg, num);
2805 /* Check PEC if last message is a read */
2806 if (i && (msg[num-1].flags & I2C_M_RD)) {
2807 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2812 if (read_write == I2C_SMBUS_READ)
2814 case I2C_SMBUS_BYTE:
2815 data->byte = msgbuf0[0];
2817 case I2C_SMBUS_BYTE_DATA:
2818 data->byte = msgbuf1[0];
2820 case I2C_SMBUS_WORD_DATA:
2821 case I2C_SMBUS_PROC_CALL:
2822 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2824 case I2C_SMBUS_I2C_BLOCK_DATA:
2825 for (i = 0; i < data->block[0]; i++)
2826 data->block[i+1] = msgbuf1[i];
2828 case I2C_SMBUS_BLOCK_DATA:
2829 case I2C_SMBUS_BLOCK_PROC_CALL:
2830 for (i = 0; i < msgbuf1[0] + 1; i++)
2831 data->block[i] = msgbuf1[i];
2838 * i2c_smbus_xfer - execute SMBus protocol operations
2839 * @adapter: Handle to I2C bus
2840 * @addr: Address of SMBus slave on that bus
2841 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2842 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2843 * @command: Byte interpreted by slave, for protocols which use such bytes
2844 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2845 * @data: Data to be read or written
2847 * This executes an SMBus protocol operation, and returns a negative
2848 * errno code else zero on success.
2850 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2851 char read_write, u8 command, int protocol,
2852 union i2c_smbus_data *data)
2854 unsigned long orig_jiffies;
2858 /* If enabled, the following two tracepoints are conditional on
2859 * read_write and protocol.
2861 trace_smbus_write(adapter, addr, flags, read_write,
2862 command, protocol, data);
2863 trace_smbus_read(adapter, addr, flags, read_write,
2866 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
2868 if (adapter->algo->smbus_xfer) {
2869 i2c_lock_adapter(adapter);
2871 /* Retry automatically on arbitration loss */
2872 orig_jiffies = jiffies;
2873 for (res = 0, try = 0; try <= adapter->retries; try++) {
2874 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2875 read_write, command,
2879 if (time_after(jiffies,
2880 orig_jiffies + adapter->timeout))
2883 i2c_unlock_adapter(adapter);
2885 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2888 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2889 * implement native support for the SMBus operation.
2893 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2894 command, protocol, data);
2897 /* If enabled, the reply tracepoint is conditional on read_write. */
2898 trace_smbus_reply(adapter, addr, flags, read_write,
2899 command, protocol, data);
2900 trace_smbus_result(adapter, addr, flags, read_write,
2901 command, protocol, res);
2905 EXPORT_SYMBOL(i2c_smbus_xfer);
2908 MODULE_DESCRIPTION("I2C-Bus main module");
2909 MODULE_LICENSE("GPL");