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 /* ------------------------------------------------------------------------- */
21 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
22 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
23 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
24 Jean Delvare <jdelvare@suse.de>
25 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
26 Michael Lawnick <michael.lawnick.ext@nsn.com>
27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
29 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
30 I2C ACPI code Copyright (C) 2014 Intel Corp
31 Author: Lan Tianyu <tianyu.lan@intel.com>
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/acpi.h>
54 #include <linux/jump_label.h>
55 #include <asm/uaccess.h>
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/i2c.h>
62 /* core_lock protects i2c_adapter_idr, and guarantees
63 that device detection, deletion of detected devices, and attach_adapter
64 calls are serialized */
65 static DEFINE_MUTEX(core_lock);
66 static DEFINE_IDR(i2c_adapter_idr);
68 static struct device_type i2c_client_type;
69 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
71 static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
73 void i2c_transfer_trace_reg(void)
75 static_key_slow_inc(&i2c_trace_msg);
78 void i2c_transfer_trace_unreg(void)
80 static_key_slow_dec(&i2c_trace_msg);
83 #if defined(CONFIG_ACPI)
84 struct acpi_i2c_handler_data {
85 struct acpi_connection_info info;
86 struct i2c_adapter *adapter;
99 static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
101 struct i2c_board_info *info = data;
103 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
104 struct acpi_resource_i2c_serialbus *sb;
106 sb = &ares->data.i2c_serial_bus;
107 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
108 info->addr = sb->slave_address;
109 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
110 info->flags |= I2C_CLIENT_TEN;
112 } else if (info->irq < 0) {
115 if (acpi_dev_resource_interrupt(ares, 0, &r))
119 /* Tell the ACPI core to skip this resource */
123 static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
124 void *data, void **return_value)
126 struct i2c_adapter *adapter = data;
127 struct list_head resource_list;
128 struct i2c_board_info info;
129 struct acpi_device *adev;
132 if (acpi_bus_get_device(handle, &adev))
134 if (acpi_bus_get_status(adev) || !adev->status.present)
137 memset(&info, 0, sizeof(info));
138 info.acpi_node.companion = adev;
141 INIT_LIST_HEAD(&resource_list);
142 ret = acpi_dev_get_resources(adev, &resource_list,
143 acpi_i2c_add_resource, &info);
144 acpi_dev_free_resource_list(&resource_list);
146 if (ret < 0 || !info.addr)
149 adev->power.flags.ignore_parent = true;
150 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
151 if (!i2c_new_device(adapter, &info)) {
152 adev->power.flags.ignore_parent = false;
153 dev_err(&adapter->dev,
154 "failed to add I2C device %s from ACPI\n",
155 dev_name(&adev->dev));
162 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
163 * @adap: pointer to adapter
165 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
166 * namespace. When a device is found it will be added to the Linux device
167 * model and bound to the corresponding ACPI handle.
169 static void acpi_i2c_register_devices(struct i2c_adapter *adap)
174 if (!adap->dev.parent)
177 handle = ACPI_HANDLE(adap->dev.parent);
181 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
182 acpi_i2c_add_device, NULL,
184 if (ACPI_FAILURE(status))
185 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
188 #else /* CONFIG_ACPI */
189 static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
190 #endif /* CONFIG_ACPI */
192 #ifdef CONFIG_ACPI_I2C_OPREGION
193 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
194 u8 cmd, u8 *data, u8 data_len)
197 struct i2c_msg msgs[2];
201 buffer = kzalloc(data_len, GFP_KERNEL);
205 msgs[0].addr = client->addr;
206 msgs[0].flags = client->flags;
210 msgs[1].addr = client->addr;
211 msgs[1].flags = client->flags | I2C_M_RD;
212 msgs[1].len = data_len;
213 msgs[1].buf = buffer;
215 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
217 dev_err(&client->adapter->dev, "i2c read failed\n");
219 memcpy(data, buffer, data_len);
225 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
226 u8 cmd, u8 *data, u8 data_len)
229 struct i2c_msg msgs[1];
233 buffer = kzalloc(data_len + 1, GFP_KERNEL);
238 memcpy(buffer + 1, data, data_len);
240 msgs[0].addr = client->addr;
241 msgs[0].flags = client->flags;
242 msgs[0].len = data_len + 1;
243 msgs[0].buf = buffer;
245 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
247 dev_err(&client->adapter->dev, "i2c write failed\n");
254 acpi_i2c_space_handler(u32 function, acpi_physical_address command,
255 u32 bits, u64 *value64,
256 void *handler_context, void *region_context)
258 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
259 struct acpi_i2c_handler_data *data = handler_context;
260 struct acpi_connection_info *info = &data->info;
261 struct acpi_resource_i2c_serialbus *sb;
262 struct i2c_adapter *adapter = data->adapter;
263 struct i2c_client client;
264 struct acpi_resource *ares;
265 u32 accessor_type = function >> 16;
266 u8 action = function & ACPI_IO_MASK;
267 acpi_status ret = AE_OK;
270 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
271 if (ACPI_FAILURE(ret))
274 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
275 ret = AE_BAD_PARAMETER;
279 sb = &ares->data.i2c_serial_bus;
280 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
281 ret = AE_BAD_PARAMETER;
285 memset(&client, 0, sizeof(client));
286 client.adapter = adapter;
287 client.addr = sb->slave_address;
290 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
291 client.flags |= I2C_CLIENT_TEN;
293 switch (accessor_type) {
294 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
295 if (action == ACPI_READ) {
296 status = i2c_smbus_read_byte(&client);
302 status = i2c_smbus_write_byte(&client, gsb->bdata);
306 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
307 if (action == ACPI_READ) {
308 status = i2c_smbus_read_byte_data(&client, command);
314 status = i2c_smbus_write_byte_data(&client, command,
319 case ACPI_GSB_ACCESS_ATTRIB_WORD:
320 if (action == ACPI_READ) {
321 status = i2c_smbus_read_word_data(&client, command);
327 status = i2c_smbus_write_word_data(&client, command,
332 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
333 if (action == ACPI_READ) {
334 status = i2c_smbus_read_block_data(&client, command,
341 status = i2c_smbus_write_block_data(&client, command,
342 gsb->len, gsb->data);
346 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
347 if (action == ACPI_READ) {
348 status = acpi_gsb_i2c_read_bytes(&client, command,
349 gsb->data, info->access_length);
353 status = acpi_gsb_i2c_write_bytes(&client, command,
354 gsb->data, info->access_length);
359 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
360 ret = AE_BAD_PARAMETER;
364 gsb->status = status;
372 static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
375 struct acpi_i2c_handler_data *data;
378 if (!adapter->dev.parent)
381 handle = ACPI_HANDLE(adapter->dev.parent);
386 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
391 data->adapter = adapter;
392 status = acpi_bus_attach_private_data(handle, (void *)data);
393 if (ACPI_FAILURE(status)) {
398 status = acpi_install_address_space_handler(handle,
399 ACPI_ADR_SPACE_GSBUS,
400 &acpi_i2c_space_handler,
403 if (ACPI_FAILURE(status)) {
404 dev_err(&adapter->dev, "Error installing i2c space handler\n");
405 acpi_bus_detach_private_data(handle);
413 static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
416 struct acpi_i2c_handler_data *data;
419 if (!adapter->dev.parent)
422 handle = ACPI_HANDLE(adapter->dev.parent);
427 acpi_remove_address_space_handler(handle,
428 ACPI_ADR_SPACE_GSBUS,
429 &acpi_i2c_space_handler);
431 status = acpi_bus_get_private_data(handle, (void **)&data);
432 if (ACPI_SUCCESS(status))
435 acpi_bus_detach_private_data(handle);
437 #else /* CONFIG_ACPI_I2C_OPREGION */
438 static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
441 static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
443 #endif /* CONFIG_ACPI_I2C_OPREGION */
445 /* ------------------------------------------------------------------------- */
447 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
448 const struct i2c_client *client)
450 while (id->name[0]) {
451 if (strcmp(client->name, id->name) == 0)
458 static int i2c_device_match(struct device *dev, struct device_driver *drv)
460 struct i2c_client *client = i2c_verify_client(dev);
461 struct i2c_driver *driver;
466 /* Attempt an OF style match */
467 if (of_driver_match_device(dev, drv))
470 /* Then ACPI style match */
471 if (acpi_driver_match_device(dev, drv))
474 driver = to_i2c_driver(drv);
475 /* match on an id table if there is one */
476 if (driver->id_table)
477 return i2c_match_id(driver->id_table, client) != NULL;
483 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
484 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
486 struct i2c_client *client = to_i2c_client(dev);
489 rc = acpi_device_uevent_modalias(dev, env);
493 if (add_uevent_var(env, "MODALIAS=%s%s",
494 I2C_MODULE_PREFIX, client->name))
496 dev_dbg(dev, "uevent\n");
500 /* i2c bus recovery routines */
501 static int get_scl_gpio_value(struct i2c_adapter *adap)
503 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
506 static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
508 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
511 static int get_sda_gpio_value(struct i2c_adapter *adap)
513 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
516 static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
518 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
519 struct device *dev = &adap->dev;
522 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
523 GPIOF_OUT_INIT_HIGH, "i2c-scl");
525 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
530 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
531 /* work without SDA polling */
532 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
541 static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
543 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
546 gpio_free(bri->sda_gpio);
548 gpio_free(bri->scl_gpio);
552 * We are generating clock pulses. ndelay() determines durating of clk pulses.
553 * We will generate clock with rate 100 KHz and so duration of both clock levels
554 * is: delay in ns = (10^6 / 100) / 2
556 #define RECOVERY_NDELAY 5000
557 #define RECOVERY_CLK_CNT 9
559 static int i2c_generic_recovery(struct i2c_adapter *adap)
561 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
562 int i = 0, val = 1, ret = 0;
564 if (bri->prepare_recovery)
565 bri->prepare_recovery(bri);
568 * By this time SCL is high, as we need to give 9 falling-rising edges
570 while (i++ < RECOVERY_CLK_CNT * 2) {
572 /* Break if SDA is high */
573 if (bri->get_sda && bri->get_sda(adap))
575 /* SCL shouldn't be low here */
576 if (!bri->get_scl(adap)) {
578 "SCL is stuck low, exit recovery\n");
585 bri->set_scl(adap, val);
586 ndelay(RECOVERY_NDELAY);
589 if (bri->unprepare_recovery)
590 bri->unprepare_recovery(bri);
595 int i2c_generic_scl_recovery(struct i2c_adapter *adap)
597 adap->bus_recovery_info->set_scl(adap, 1);
598 return i2c_generic_recovery(adap);
601 int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
605 ret = i2c_get_gpios_for_recovery(adap);
609 ret = i2c_generic_recovery(adap);
610 i2c_put_gpios_for_recovery(adap);
615 int i2c_recover_bus(struct i2c_adapter *adap)
617 if (!adap->bus_recovery_info)
620 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
621 return adap->bus_recovery_info->recover_bus(adap);
624 static int i2c_device_probe(struct device *dev)
626 struct i2c_client *client = i2c_verify_client(dev);
627 struct i2c_driver *driver;
633 driver = to_i2c_driver(dev->driver);
634 if (!driver->probe || !driver->id_table)
637 if (!device_can_wakeup(&client->dev))
638 device_init_wakeup(&client->dev,
639 client->flags & I2C_CLIENT_WAKE);
640 dev_dbg(dev, "probe\n");
642 status = of_clk_set_defaults(dev->of_node, false);
646 acpi_dev_pm_attach(&client->dev, true);
647 status = driver->probe(client, i2c_match_id(driver->id_table, client));
649 acpi_dev_pm_detach(&client->dev, true);
654 static int i2c_device_remove(struct device *dev)
656 struct i2c_client *client = i2c_verify_client(dev);
657 struct i2c_driver *driver;
660 if (!client || !dev->driver)
663 driver = to_i2c_driver(dev->driver);
664 if (driver->remove) {
665 dev_dbg(dev, "remove\n");
666 status = driver->remove(client);
669 acpi_dev_pm_detach(&client->dev, true);
673 static void i2c_device_shutdown(struct device *dev)
675 struct i2c_client *client = i2c_verify_client(dev);
676 struct i2c_driver *driver;
678 if (!client || !dev->driver)
680 driver = to_i2c_driver(dev->driver);
681 if (driver->shutdown)
682 driver->shutdown(client);
685 #ifdef CONFIG_PM_SLEEP
686 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
688 struct i2c_client *client = i2c_verify_client(dev);
689 struct i2c_driver *driver;
691 if (!client || !dev->driver)
693 driver = to_i2c_driver(dev->driver);
694 if (!driver->suspend)
696 return driver->suspend(client, mesg);
699 static int i2c_legacy_resume(struct device *dev)
701 struct i2c_client *client = i2c_verify_client(dev);
702 struct i2c_driver *driver;
704 if (!client || !dev->driver)
706 driver = to_i2c_driver(dev->driver);
709 return driver->resume(client);
712 static int i2c_device_pm_suspend(struct device *dev)
714 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
717 return pm_generic_suspend(dev);
719 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
722 static int i2c_device_pm_resume(struct device *dev)
724 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
727 return pm_generic_resume(dev);
729 return i2c_legacy_resume(dev);
732 static int i2c_device_pm_freeze(struct device *dev)
734 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
737 return pm_generic_freeze(dev);
739 return i2c_legacy_suspend(dev, PMSG_FREEZE);
742 static int i2c_device_pm_thaw(struct device *dev)
744 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
747 return pm_generic_thaw(dev);
749 return i2c_legacy_resume(dev);
752 static int i2c_device_pm_poweroff(struct device *dev)
754 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
757 return pm_generic_poweroff(dev);
759 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
762 static int i2c_device_pm_restore(struct device *dev)
764 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
767 return pm_generic_restore(dev);
769 return i2c_legacy_resume(dev);
771 #else /* !CONFIG_PM_SLEEP */
772 #define i2c_device_pm_suspend NULL
773 #define i2c_device_pm_resume NULL
774 #define i2c_device_pm_freeze NULL
775 #define i2c_device_pm_thaw NULL
776 #define i2c_device_pm_poweroff NULL
777 #define i2c_device_pm_restore NULL
778 #endif /* !CONFIG_PM_SLEEP */
780 static void i2c_client_dev_release(struct device *dev)
782 kfree(to_i2c_client(dev));
786 show_name(struct device *dev, struct device_attribute *attr, char *buf)
788 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
789 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
793 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
795 struct i2c_client *client = to_i2c_client(dev);
798 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
802 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
805 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
806 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
808 static struct attribute *i2c_dev_attrs[] = {
810 /* modalias helps coldplug: modprobe $(cat .../modalias) */
811 &dev_attr_modalias.attr,
815 static struct attribute_group i2c_dev_attr_group = {
816 .attrs = i2c_dev_attrs,
819 static const struct attribute_group *i2c_dev_attr_groups[] = {
824 static const struct dev_pm_ops i2c_device_pm_ops = {
825 .suspend = i2c_device_pm_suspend,
826 .resume = i2c_device_pm_resume,
827 .freeze = i2c_device_pm_freeze,
828 .thaw = i2c_device_pm_thaw,
829 .poweroff = i2c_device_pm_poweroff,
830 .restore = i2c_device_pm_restore,
832 pm_generic_runtime_suspend,
833 pm_generic_runtime_resume,
838 struct bus_type i2c_bus_type = {
840 .match = i2c_device_match,
841 .probe = i2c_device_probe,
842 .remove = i2c_device_remove,
843 .shutdown = i2c_device_shutdown,
844 .pm = &i2c_device_pm_ops,
846 EXPORT_SYMBOL_GPL(i2c_bus_type);
848 static struct device_type i2c_client_type = {
849 .groups = i2c_dev_attr_groups,
850 .uevent = i2c_device_uevent,
851 .release = i2c_client_dev_release,
856 * i2c_verify_client - return parameter as i2c_client, or NULL
857 * @dev: device, probably from some driver model iterator
859 * When traversing the driver model tree, perhaps using driver model
860 * iterators like @device_for_each_child(), you can't assume very much
861 * about the nodes you find. Use this function to avoid oopses caused
862 * by wrongly treating some non-I2C device as an i2c_client.
864 struct i2c_client *i2c_verify_client(struct device *dev)
866 return (dev->type == &i2c_client_type)
870 EXPORT_SYMBOL(i2c_verify_client);
873 /* This is a permissive address validity check, I2C address map constraints
874 * are purposely not enforced, except for the general call address. */
875 static int i2c_check_client_addr_validity(const struct i2c_client *client)
877 if (client->flags & I2C_CLIENT_TEN) {
878 /* 10-bit address, all values are valid */
879 if (client->addr > 0x3ff)
882 /* 7-bit address, reject the general call address */
883 if (client->addr == 0x00 || client->addr > 0x7f)
889 /* And this is a strict address validity check, used when probing. If a
890 * device uses a reserved address, then it shouldn't be probed. 7-bit
891 * addressing is assumed, 10-bit address devices are rare and should be
892 * explicitly enumerated. */
893 static int i2c_check_addr_validity(unsigned short addr)
896 * Reserved addresses per I2C specification:
897 * 0x00 General call address / START byte
899 * 0x02 Reserved for different bus format
900 * 0x03 Reserved for future purposes
901 * 0x04-0x07 Hs-mode master code
902 * 0x78-0x7b 10-bit slave addressing
903 * 0x7c-0x7f Reserved for future purposes
905 if (addr < 0x08 || addr > 0x77)
910 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
912 struct i2c_client *client = i2c_verify_client(dev);
913 int addr = *(int *)addrp;
915 if (client && client->addr == addr)
920 /* walk up mux tree */
921 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
923 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
926 result = device_for_each_child(&adapter->dev, &addr,
927 __i2c_check_addr_busy);
929 if (!result && parent)
930 result = i2c_check_mux_parents(parent, addr);
935 /* recurse down mux tree */
936 static int i2c_check_mux_children(struct device *dev, void *addrp)
940 if (dev->type == &i2c_adapter_type)
941 result = device_for_each_child(dev, addrp,
942 i2c_check_mux_children);
944 result = __i2c_check_addr_busy(dev, addrp);
949 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
951 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
955 result = i2c_check_mux_parents(parent, addr);
958 result = device_for_each_child(&adapter->dev, &addr,
959 i2c_check_mux_children);
965 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
966 * @adapter: Target I2C bus segment
968 void i2c_lock_adapter(struct i2c_adapter *adapter)
970 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
973 i2c_lock_adapter(parent);
975 rt_mutex_lock(&adapter->bus_lock);
977 EXPORT_SYMBOL_GPL(i2c_lock_adapter);
980 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
981 * @adapter: Target I2C bus segment
983 static int i2c_trylock_adapter(struct i2c_adapter *adapter)
985 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
988 return i2c_trylock_adapter(parent);
990 return rt_mutex_trylock(&adapter->bus_lock);
994 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
995 * @adapter: Target I2C bus segment
997 void i2c_unlock_adapter(struct i2c_adapter *adapter)
999 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1002 i2c_unlock_adapter(parent);
1004 rt_mutex_unlock(&adapter->bus_lock);
1006 EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1008 static void i2c_dev_set_name(struct i2c_adapter *adap,
1009 struct i2c_client *client)
1011 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1014 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1018 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1019 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1020 client->addr | ((client->flags & I2C_CLIENT_TEN)
1025 * i2c_new_device - instantiate an i2c device
1026 * @adap: the adapter managing the device
1027 * @info: describes one I2C device; bus_num is ignored
1028 * Context: can sleep
1030 * Create an i2c device. Binding is handled through driver model
1031 * probe()/remove() methods. A driver may be bound to this device when we
1032 * return from this function, or any later moment (e.g. maybe hotplugging will
1033 * load the driver module). This call is not appropriate for use by mainboard
1034 * initialization logic, which usually runs during an arch_initcall() long
1035 * before any i2c_adapter could exist.
1037 * This returns the new i2c client, which may be saved for later use with
1038 * i2c_unregister_device(); or NULL to indicate an error.
1041 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1043 struct i2c_client *client;
1046 client = kzalloc(sizeof *client, GFP_KERNEL);
1050 client->adapter = adap;
1052 client->dev.platform_data = info->platform_data;
1055 client->dev.archdata = *info->archdata;
1057 client->flags = info->flags;
1058 client->addr = info->addr;
1059 client->irq = info->irq;
1061 strlcpy(client->name, info->type, sizeof(client->name));
1063 /* Check for address validity */
1064 status = i2c_check_client_addr_validity(client);
1066 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1067 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1068 goto out_err_silent;
1071 /* Check for address business */
1072 status = i2c_check_addr_busy(adap, client->addr);
1076 client->dev.parent = &client->adapter->dev;
1077 client->dev.bus = &i2c_bus_type;
1078 client->dev.type = &i2c_client_type;
1079 client->dev.of_node = info->of_node;
1080 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
1082 i2c_dev_set_name(adap, client);
1083 status = device_register(&client->dev);
1087 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1088 client->name, dev_name(&client->dev));
1093 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1094 "(%d)\n", client->name, client->addr, status);
1099 EXPORT_SYMBOL_GPL(i2c_new_device);
1103 * i2c_unregister_device - reverse effect of i2c_new_device()
1104 * @client: value returned from i2c_new_device()
1105 * Context: can sleep
1107 void i2c_unregister_device(struct i2c_client *client)
1109 device_unregister(&client->dev);
1111 EXPORT_SYMBOL_GPL(i2c_unregister_device);
1114 static const struct i2c_device_id dummy_id[] = {
1119 static int dummy_probe(struct i2c_client *client,
1120 const struct i2c_device_id *id)
1125 static int dummy_remove(struct i2c_client *client)
1130 static struct i2c_driver dummy_driver = {
1131 .driver.name = "dummy",
1132 .probe = dummy_probe,
1133 .remove = dummy_remove,
1134 .id_table = dummy_id,
1138 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1139 * @adapter: the adapter managing the device
1140 * @address: seven bit address to be used
1141 * Context: can sleep
1143 * This returns an I2C client bound to the "dummy" driver, intended for use
1144 * with devices that consume multiple addresses. Examples of such chips
1145 * include various EEPROMS (like 24c04 and 24c08 models).
1147 * These dummy devices have two main uses. First, most I2C and SMBus calls
1148 * except i2c_transfer() need a client handle; the dummy will be that handle.
1149 * And second, this prevents the specified address from being bound to a
1152 * This returns the new i2c client, which should be saved for later use with
1153 * i2c_unregister_device(); or NULL to indicate an error.
1155 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
1157 struct i2c_board_info info = {
1158 I2C_BOARD_INFO("dummy", address),
1161 return i2c_new_device(adapter, &info);
1163 EXPORT_SYMBOL_GPL(i2c_new_dummy);
1165 /* ------------------------------------------------------------------------- */
1167 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1169 static void i2c_adapter_dev_release(struct device *dev)
1171 struct i2c_adapter *adap = to_i2c_adapter(dev);
1172 complete(&adap->dev_released);
1176 * This function is only needed for mutex_lock_nested, so it is never
1177 * called unless locking correctness checking is enabled. Thus we
1178 * make it inline to avoid a compiler warning. That's what gcc ends up
1181 static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1183 unsigned int depth = 0;
1185 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1192 * Let users instantiate I2C devices through sysfs. This can be used when
1193 * platform initialization code doesn't contain the proper data for
1194 * whatever reason. Also useful for drivers that do device detection and
1195 * detection fails, either because the device uses an unexpected address,
1196 * or this is a compatible device with different ID register values.
1198 * Parameter checking may look overzealous, but we really don't want
1199 * the user to provide incorrect parameters.
1202 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1203 const char *buf, size_t count)
1205 struct i2c_adapter *adap = to_i2c_adapter(dev);
1206 struct i2c_board_info info;
1207 struct i2c_client *client;
1211 memset(&info, 0, sizeof(struct i2c_board_info));
1213 blank = strchr(buf, ' ');
1215 dev_err(dev, "%s: Missing parameters\n", "new_device");
1218 if (blank - buf > I2C_NAME_SIZE - 1) {
1219 dev_err(dev, "%s: Invalid device name\n", "new_device");
1222 memcpy(info.type, buf, blank - buf);
1224 /* Parse remaining parameters, reject extra parameters */
1225 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1227 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1230 if (res > 1 && end != '\n') {
1231 dev_err(dev, "%s: Extra parameters\n", "new_device");
1235 client = i2c_new_device(adap, &info);
1239 /* Keep track of the added device */
1240 mutex_lock(&adap->userspace_clients_lock);
1241 list_add_tail(&client->detected, &adap->userspace_clients);
1242 mutex_unlock(&adap->userspace_clients_lock);
1243 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1244 info.type, info.addr);
1250 * And of course let the users delete the devices they instantiated, if
1251 * they got it wrong. This interface can only be used to delete devices
1252 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1253 * don't delete devices to which some kernel code still has references.
1255 * Parameter checking may look overzealous, but we really don't want
1256 * the user to delete the wrong device.
1259 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1260 const char *buf, size_t count)
1262 struct i2c_adapter *adap = to_i2c_adapter(dev);
1263 struct i2c_client *client, *next;
1264 unsigned short addr;
1268 /* Parse parameters, reject extra parameters */
1269 res = sscanf(buf, "%hi%c", &addr, &end);
1271 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1274 if (res > 1 && end != '\n') {
1275 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1279 /* Make sure the device was added through sysfs */
1281 mutex_lock_nested(&adap->userspace_clients_lock,
1282 i2c_adapter_depth(adap));
1283 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1285 if (client->addr == addr) {
1286 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1287 "delete_device", client->name, client->addr);
1289 list_del(&client->detected);
1290 i2c_unregister_device(client);
1295 mutex_unlock(&adap->userspace_clients_lock);
1298 dev_err(dev, "%s: Can't find device in list\n",
1303 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
1304 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1305 i2c_sysfs_delete_device);
1307 static struct attribute *i2c_adapter_attrs[] = {
1308 &dev_attr_name.attr,
1309 &dev_attr_new_device.attr,
1310 &dev_attr_delete_device.attr,
1314 static struct attribute_group i2c_adapter_attr_group = {
1315 .attrs = i2c_adapter_attrs,
1318 static const struct attribute_group *i2c_adapter_attr_groups[] = {
1319 &i2c_adapter_attr_group,
1323 struct device_type i2c_adapter_type = {
1324 .groups = i2c_adapter_attr_groups,
1325 .release = i2c_adapter_dev_release,
1327 EXPORT_SYMBOL_GPL(i2c_adapter_type);
1330 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1331 * @dev: device, probably from some driver model iterator
1333 * When traversing the driver model tree, perhaps using driver model
1334 * iterators like @device_for_each_child(), you can't assume very much
1335 * about the nodes you find. Use this function to avoid oopses caused
1336 * by wrongly treating some non-I2C device as an i2c_adapter.
1338 struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1340 return (dev->type == &i2c_adapter_type)
1341 ? to_i2c_adapter(dev)
1344 EXPORT_SYMBOL(i2c_verify_adapter);
1346 #ifdef CONFIG_I2C_COMPAT
1347 static struct class_compat *i2c_adapter_compat_class;
1350 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1352 struct i2c_devinfo *devinfo;
1354 down_read(&__i2c_board_lock);
1355 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1356 if (devinfo->busnum == adapter->nr
1357 && !i2c_new_device(adapter,
1358 &devinfo->board_info))
1359 dev_err(&adapter->dev,
1360 "Can't create device at 0x%02x\n",
1361 devinfo->board_info.addr);
1363 up_read(&__i2c_board_lock);
1366 /* OF support code */
1368 #if IS_ENABLED(CONFIG_OF)
1369 static void of_i2c_register_devices(struct i2c_adapter *adap)
1372 struct device_node *node;
1374 /* Only register child devices if the adapter has a node pointer set */
1375 if (!adap->dev.of_node)
1378 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1380 for_each_available_child_of_node(adap->dev.of_node, node) {
1381 struct i2c_board_info info = {};
1382 struct dev_archdata dev_ad = {};
1386 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1388 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1389 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1394 addr = of_get_property(node, "reg", &len);
1395 if (!addr || (len < sizeof(int))) {
1396 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1401 info.addr = be32_to_cpup(addr);
1402 if (info.addr > (1 << 10) - 1) {
1403 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1404 info.addr, node->full_name);
1408 info.irq = irq_of_parse_and_map(node, 0);
1409 info.of_node = of_node_get(node);
1410 info.archdata = &dev_ad;
1412 if (of_get_property(node, "wakeup-source", NULL))
1413 info.flags |= I2C_CLIENT_WAKE;
1415 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1417 result = i2c_new_device(adap, &info);
1418 if (result == NULL) {
1419 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1422 irq_dispose_mapping(info.irq);
1428 static int of_dev_node_match(struct device *dev, void *data)
1430 return dev->of_node == data;
1433 /* must call put_device() when done with returned i2c_client device */
1434 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1438 dev = bus_find_device(&i2c_bus_type, NULL, node,
1443 return i2c_verify_client(dev);
1445 EXPORT_SYMBOL(of_find_i2c_device_by_node);
1447 /* must call put_device() when done with returned i2c_adapter device */
1448 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1452 dev = bus_find_device(&i2c_bus_type, NULL, node,
1457 return i2c_verify_adapter(dev);
1459 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1461 static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1462 #endif /* CONFIG_OF */
1464 static int i2c_do_add_adapter(struct i2c_driver *driver,
1465 struct i2c_adapter *adap)
1467 /* Detect supported devices on that bus, and instantiate them */
1468 i2c_detect(adap, driver);
1470 /* Let legacy drivers scan this bus for matching devices */
1471 if (driver->attach_adapter) {
1472 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1473 driver->driver.name);
1474 dev_warn(&adap->dev, "Please use another way to instantiate "
1475 "your i2c_client\n");
1476 /* We ignore the return code; if it fails, too bad */
1477 driver->attach_adapter(adap);
1482 static int __process_new_adapter(struct device_driver *d, void *data)
1484 return i2c_do_add_adapter(to_i2c_driver(d), data);
1487 static int i2c_register_adapter(struct i2c_adapter *adap)
1491 /* Can't register until after driver model init */
1492 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1498 if (unlikely(adap->name[0] == '\0')) {
1499 pr_err("i2c-core: Attempt to register an adapter with "
1503 if (unlikely(!adap->algo)) {
1504 pr_err("i2c-core: Attempt to register adapter '%s' with "
1505 "no algo!\n", adap->name);
1509 rt_mutex_init(&adap->bus_lock);
1510 mutex_init(&adap->userspace_clients_lock);
1511 INIT_LIST_HEAD(&adap->userspace_clients);
1513 /* Set default timeout to 1 second if not already set */
1514 if (adap->timeout == 0)
1517 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
1518 adap->dev.bus = &i2c_bus_type;
1519 adap->dev.type = &i2c_adapter_type;
1520 res = device_register(&adap->dev);
1524 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1526 #ifdef CONFIG_I2C_COMPAT
1527 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1530 dev_warn(&adap->dev,
1531 "Failed to create compatibility class link\n");
1534 /* bus recovery specific initialization */
1535 if (adap->bus_recovery_info) {
1536 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1538 if (!bri->recover_bus) {
1539 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1540 adap->bus_recovery_info = NULL;
1544 /* Generic GPIO recovery */
1545 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1546 if (!gpio_is_valid(bri->scl_gpio)) {
1547 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1548 adap->bus_recovery_info = NULL;
1552 if (gpio_is_valid(bri->sda_gpio))
1553 bri->get_sda = get_sda_gpio_value;
1555 bri->get_sda = NULL;
1557 bri->get_scl = get_scl_gpio_value;
1558 bri->set_scl = set_scl_gpio_value;
1559 } else if (!bri->set_scl || !bri->get_scl) {
1560 /* Generic SCL recovery */
1561 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1562 adap->bus_recovery_info = NULL;
1567 /* create pre-declared device nodes */
1568 of_i2c_register_devices(adap);
1569 acpi_i2c_register_devices(adap);
1570 acpi_i2c_install_space_handler(adap);
1572 if (adap->nr < __i2c_first_dynamic_bus_num)
1573 i2c_scan_static_board_info(adap);
1575 /* Notify drivers */
1576 mutex_lock(&core_lock);
1577 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1578 mutex_unlock(&core_lock);
1583 mutex_lock(&core_lock);
1584 idr_remove(&i2c_adapter_idr, adap->nr);
1585 mutex_unlock(&core_lock);
1590 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1591 * @adap: the adapter to register (with adap->nr initialized)
1592 * Context: can sleep
1594 * See i2c_add_numbered_adapter() for details.
1596 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1600 mutex_lock(&core_lock);
1601 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1603 mutex_unlock(&core_lock);
1605 return id == -ENOSPC ? -EBUSY : id;
1607 return i2c_register_adapter(adap);
1611 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1612 * @adapter: the adapter to add
1613 * Context: can sleep
1615 * This routine is used to declare an I2C adapter when its bus number
1616 * doesn't matter or when its bus number is specified by an dt alias.
1617 * Examples of bases when the bus number doesn't matter: I2C adapters
1618 * dynamically added by USB links or PCI plugin cards.
1620 * When this returns zero, a new bus number was allocated and stored
1621 * in adap->nr, and the specified adapter became available for clients.
1622 * Otherwise, a negative errno value is returned.
1624 int i2c_add_adapter(struct i2c_adapter *adapter)
1626 struct device *dev = &adapter->dev;
1630 id = of_alias_get_id(dev->of_node, "i2c");
1633 return __i2c_add_numbered_adapter(adapter);
1637 mutex_lock(&core_lock);
1638 id = idr_alloc(&i2c_adapter_idr, adapter,
1639 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
1640 mutex_unlock(&core_lock);
1646 return i2c_register_adapter(adapter);
1648 EXPORT_SYMBOL(i2c_add_adapter);
1651 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1652 * @adap: the adapter to register (with adap->nr initialized)
1653 * Context: can sleep
1655 * This routine is used to declare an I2C adapter when its bus number
1656 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1657 * or otherwise built in to the system's mainboard, and where i2c_board_info
1658 * is used to properly configure I2C devices.
1660 * If the requested bus number is set to -1, then this function will behave
1661 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1663 * If no devices have pre-been declared for this bus, then be sure to
1664 * register the adapter before any dynamically allocated ones. Otherwise
1665 * the required bus ID may not be available.
1667 * When this returns zero, the specified adapter became available for
1668 * clients using the bus number provided in adap->nr. Also, the table
1669 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1670 * and the appropriate driver model device nodes are created. Otherwise, a
1671 * negative errno value is returned.
1673 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1675 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1676 return i2c_add_adapter(adap);
1678 return __i2c_add_numbered_adapter(adap);
1680 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1682 static void i2c_do_del_adapter(struct i2c_driver *driver,
1683 struct i2c_adapter *adapter)
1685 struct i2c_client *client, *_n;
1687 /* Remove the devices we created ourselves as the result of hardware
1688 * probing (using a driver's detect method) */
1689 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1690 if (client->adapter == adapter) {
1691 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1692 client->name, client->addr);
1693 list_del(&client->detected);
1694 i2c_unregister_device(client);
1699 static int __unregister_client(struct device *dev, void *dummy)
1701 struct i2c_client *client = i2c_verify_client(dev);
1702 if (client && strcmp(client->name, "dummy"))
1703 i2c_unregister_device(client);
1707 static int __unregister_dummy(struct device *dev, void *dummy)
1709 struct i2c_client *client = i2c_verify_client(dev);
1711 i2c_unregister_device(client);
1715 static int __process_removed_adapter(struct device_driver *d, void *data)
1717 i2c_do_del_adapter(to_i2c_driver(d), data);
1722 * i2c_del_adapter - unregister I2C adapter
1723 * @adap: the adapter being unregistered
1724 * Context: can sleep
1726 * This unregisters an I2C adapter which was previously registered
1727 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1729 void i2c_del_adapter(struct i2c_adapter *adap)
1731 struct i2c_adapter *found;
1732 struct i2c_client *client, *next;
1734 /* First make sure that this adapter was ever added */
1735 mutex_lock(&core_lock);
1736 found = idr_find(&i2c_adapter_idr, adap->nr);
1737 mutex_unlock(&core_lock);
1738 if (found != adap) {
1739 pr_debug("i2c-core: attempting to delete unregistered "
1740 "adapter [%s]\n", adap->name);
1744 acpi_i2c_remove_space_handler(adap);
1745 /* Tell drivers about this removal */
1746 mutex_lock(&core_lock);
1747 bus_for_each_drv(&i2c_bus_type, NULL, adap,
1748 __process_removed_adapter);
1749 mutex_unlock(&core_lock);
1751 /* Remove devices instantiated from sysfs */
1752 mutex_lock_nested(&adap->userspace_clients_lock,
1753 i2c_adapter_depth(adap));
1754 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1756 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1758 list_del(&client->detected);
1759 i2c_unregister_device(client);
1761 mutex_unlock(&adap->userspace_clients_lock);
1763 /* Detach any active clients. This can't fail, thus we do not
1764 * check the returned value. This is a two-pass process, because
1765 * we can't remove the dummy devices during the first pass: they
1766 * could have been instantiated by real devices wishing to clean
1767 * them up properly, so we give them a chance to do that first. */
1768 device_for_each_child(&adap->dev, NULL, __unregister_client);
1769 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1771 #ifdef CONFIG_I2C_COMPAT
1772 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1776 /* device name is gone after device_unregister */
1777 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1779 /* clean up the sysfs representation */
1780 init_completion(&adap->dev_released);
1781 device_unregister(&adap->dev);
1783 /* wait for sysfs to drop all references */
1784 wait_for_completion(&adap->dev_released);
1787 mutex_lock(&core_lock);
1788 idr_remove(&i2c_adapter_idr, adap->nr);
1789 mutex_unlock(&core_lock);
1791 /* Clear the device structure in case this adapter is ever going to be
1793 memset(&adap->dev, 0, sizeof(adap->dev));
1795 EXPORT_SYMBOL(i2c_del_adapter);
1797 /* ------------------------------------------------------------------------- */
1799 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1803 mutex_lock(&core_lock);
1804 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1805 mutex_unlock(&core_lock);
1809 EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1811 static int __process_new_driver(struct device *dev, void *data)
1813 if (dev->type != &i2c_adapter_type)
1815 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1819 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1820 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1823 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1827 /* Can't register until after driver model init */
1828 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1831 /* add the driver to the list of i2c drivers in the driver core */
1832 driver->driver.owner = owner;
1833 driver->driver.bus = &i2c_bus_type;
1835 /* When registration returns, the driver core
1836 * will have called probe() for all matching-but-unbound devices.
1838 res = driver_register(&driver->driver);
1842 /* Drivers should switch to dev_pm_ops instead. */
1843 if (driver->suspend)
1844 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1845 driver->driver.name);
1847 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1848 driver->driver.name);
1850 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1852 INIT_LIST_HEAD(&driver->clients);
1853 /* Walk the adapters that are already present */
1854 i2c_for_each_dev(driver, __process_new_driver);
1858 EXPORT_SYMBOL(i2c_register_driver);
1860 static int __process_removed_driver(struct device *dev, void *data)
1862 if (dev->type == &i2c_adapter_type)
1863 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1868 * i2c_del_driver - unregister I2C driver
1869 * @driver: the driver being unregistered
1870 * Context: can sleep
1872 void i2c_del_driver(struct i2c_driver *driver)
1874 i2c_for_each_dev(driver, __process_removed_driver);
1876 driver_unregister(&driver->driver);
1877 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1879 EXPORT_SYMBOL(i2c_del_driver);
1881 /* ------------------------------------------------------------------------- */
1884 * i2c_use_client - increments the reference count of the i2c client structure
1885 * @client: the client being referenced
1887 * Each live reference to a client should be refcounted. The driver model does
1888 * that automatically as part of driver binding, so that most drivers don't
1889 * need to do this explicitly: they hold a reference until they're unbound
1892 * A pointer to the client with the incremented reference counter is returned.
1894 struct i2c_client *i2c_use_client(struct i2c_client *client)
1896 if (client && get_device(&client->dev))
1900 EXPORT_SYMBOL(i2c_use_client);
1903 * i2c_release_client - release a use of the i2c client structure
1904 * @client: the client being no longer referenced
1906 * Must be called when a user of a client is finished with it.
1908 void i2c_release_client(struct i2c_client *client)
1911 put_device(&client->dev);
1913 EXPORT_SYMBOL(i2c_release_client);
1915 struct i2c_cmd_arg {
1920 static int i2c_cmd(struct device *dev, void *_arg)
1922 struct i2c_client *client = i2c_verify_client(dev);
1923 struct i2c_cmd_arg *arg = _arg;
1924 struct i2c_driver *driver;
1926 if (!client || !client->dev.driver)
1929 driver = to_i2c_driver(client->dev.driver);
1930 if (driver->command)
1931 driver->command(client, arg->cmd, arg->arg);
1935 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1937 struct i2c_cmd_arg cmd_arg;
1941 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1943 EXPORT_SYMBOL(i2c_clients_command);
1945 static int __init i2c_init(void)
1949 retval = bus_register(&i2c_bus_type);
1952 #ifdef CONFIG_I2C_COMPAT
1953 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1954 if (!i2c_adapter_compat_class) {
1959 retval = i2c_add_driver(&dummy_driver);
1965 #ifdef CONFIG_I2C_COMPAT
1966 class_compat_unregister(i2c_adapter_compat_class);
1969 bus_unregister(&i2c_bus_type);
1973 static void __exit i2c_exit(void)
1975 i2c_del_driver(&dummy_driver);
1976 #ifdef CONFIG_I2C_COMPAT
1977 class_compat_unregister(i2c_adapter_compat_class);
1979 bus_unregister(&i2c_bus_type);
1980 tracepoint_synchronize_unregister();
1983 /* We must initialize early, because some subsystems register i2c drivers
1984 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1986 postcore_initcall(i2c_init);
1987 module_exit(i2c_exit);
1989 /* ----------------------------------------------------
1990 * the functional interface to the i2c busses.
1991 * ----------------------------------------------------
1995 * __i2c_transfer - unlocked flavor of i2c_transfer
1996 * @adap: Handle to I2C bus
1997 * @msgs: One or more messages to execute before STOP is issued to
1998 * terminate the operation; each message begins with a START.
1999 * @num: Number of messages to be executed.
2001 * Returns negative errno, else the number of messages executed.
2003 * Adapter lock must be held when calling this function. No debug logging
2004 * takes place. adap->algo->master_xfer existence isn't checked.
2006 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2008 unsigned long orig_jiffies;
2011 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2012 * enabled. This is an efficient way of keeping the for-loop from
2013 * being executed when not needed.
2015 if (static_key_false(&i2c_trace_msg)) {
2017 for (i = 0; i < num; i++)
2018 if (msgs[i].flags & I2C_M_RD)
2019 trace_i2c_read(adap, &msgs[i], i);
2021 trace_i2c_write(adap, &msgs[i], i);
2024 /* Retry automatically on arbitration loss */
2025 orig_jiffies = jiffies;
2026 for (ret = 0, try = 0; try <= adap->retries; try++) {
2027 ret = adap->algo->master_xfer(adap, msgs, num);
2030 if (time_after(jiffies, orig_jiffies + adap->timeout))
2034 if (static_key_false(&i2c_trace_msg)) {
2036 for (i = 0; i < ret; i++)
2037 if (msgs[i].flags & I2C_M_RD)
2038 trace_i2c_reply(adap, &msgs[i], i);
2039 trace_i2c_result(adap, i, ret);
2044 EXPORT_SYMBOL(__i2c_transfer);
2047 * i2c_transfer - execute a single or combined I2C message
2048 * @adap: Handle to I2C bus
2049 * @msgs: One or more messages to execute before STOP is issued to
2050 * terminate the operation; each message begins with a START.
2051 * @num: Number of messages to be executed.
2053 * Returns negative errno, else the number of messages executed.
2055 * Note that there is no requirement that each message be sent to
2056 * the same slave address, although that is the most common model.
2058 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2062 /* REVISIT the fault reporting model here is weak:
2064 * - When we get an error after receiving N bytes from a slave,
2065 * there is no way to report "N".
2067 * - When we get a NAK after transmitting N bytes to a slave,
2068 * there is no way to report "N" ... or to let the master
2069 * continue executing the rest of this combined message, if
2070 * that's the appropriate response.
2072 * - When for example "num" is two and we successfully complete
2073 * the first message but get an error part way through the
2074 * second, it's unclear whether that should be reported as
2075 * one (discarding status on the second message) or errno
2076 * (discarding status on the first one).
2079 if (adap->algo->master_xfer) {
2081 for (ret = 0; ret < num; ret++) {
2082 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
2083 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2084 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2085 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
2089 if (in_atomic() || irqs_disabled()) {
2090 ret = i2c_trylock_adapter(adap);
2092 /* I2C activity is ongoing. */
2095 i2c_lock_adapter(adap);
2098 ret = __i2c_transfer(adap, msgs, num);
2099 i2c_unlock_adapter(adap);
2103 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
2107 EXPORT_SYMBOL(i2c_transfer);
2110 * i2c_master_send - issue a single I2C message in master transmit mode
2111 * @client: Handle to slave device
2112 * @buf: Data that will be written to the slave
2113 * @count: How many bytes to write, must be less than 64k since msg.len is u16
2115 * Returns negative errno, or else the number of bytes written.
2117 int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
2120 struct i2c_adapter *adap = client->adapter;
2123 msg.addr = client->addr;
2124 msg.flags = client->flags & I2C_M_TEN;
2126 msg.buf = (char *)buf;
2128 ret = i2c_transfer(adap, &msg, 1);
2131 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2132 * transmitted, else error code.
2134 return (ret == 1) ? count : ret;
2136 EXPORT_SYMBOL(i2c_master_send);
2139 * i2c_master_recv - issue a single I2C message in master receive mode
2140 * @client: Handle to slave device
2141 * @buf: Where to store data read from slave
2142 * @count: How many bytes to read, must be less than 64k since msg.len is u16
2144 * Returns negative errno, or else the number of bytes read.
2146 int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
2148 struct i2c_adapter *adap = client->adapter;
2152 msg.addr = client->addr;
2153 msg.flags = client->flags & I2C_M_TEN;
2154 msg.flags |= I2C_M_RD;
2158 ret = i2c_transfer(adap, &msg, 1);
2161 * If everything went ok (i.e. 1 msg received), return #bytes received,
2164 return (ret == 1) ? count : ret;
2166 EXPORT_SYMBOL(i2c_master_recv);
2168 /* ----------------------------------------------------
2169 * the i2c address scanning function
2170 * Will not work for 10-bit addresses!
2171 * ----------------------------------------------------
2175 * Legacy default probe function, mostly relevant for SMBus. The default
2176 * probe method is a quick write, but it is known to corrupt the 24RF08
2177 * EEPROMs due to a state machine bug, and could also irreversibly
2178 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2179 * we use a short byte read instead. Also, some bus drivers don't implement
2180 * quick write, so we fallback to a byte read in that case too.
2181 * On x86, there is another special case for FSC hardware monitoring chips,
2182 * which want regular byte reads (address 0x73.) Fortunately, these are the
2183 * only known chips using this I2C address on PC hardware.
2184 * Returns 1 if probe succeeded, 0 if not.
2186 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2189 union i2c_smbus_data dummy;
2192 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2193 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2194 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2195 I2C_SMBUS_BYTE_DATA, &dummy);
2198 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2199 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
2200 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2201 I2C_SMBUS_QUICK, NULL);
2202 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2203 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2204 I2C_SMBUS_BYTE, &dummy);
2206 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2214 static int i2c_detect_address(struct i2c_client *temp_client,
2215 struct i2c_driver *driver)
2217 struct i2c_board_info info;
2218 struct i2c_adapter *adapter = temp_client->adapter;
2219 int addr = temp_client->addr;
2222 /* Make sure the address is valid */
2223 err = i2c_check_addr_validity(addr);
2225 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2230 /* Skip if already in use */
2231 if (i2c_check_addr_busy(adapter, addr))
2234 /* Make sure there is something at this address */
2235 if (!i2c_default_probe(adapter, addr))
2238 /* Finally call the custom detection function */
2239 memset(&info, 0, sizeof(struct i2c_board_info));
2241 err = driver->detect(temp_client, &info);
2243 /* -ENODEV is returned if the detection fails. We catch it
2244 here as this isn't an error. */
2245 return err == -ENODEV ? 0 : err;
2248 /* Consistency check */
2249 if (info.type[0] == '\0') {
2250 dev_err(&adapter->dev, "%s detection function provided "
2251 "no name for 0x%x\n", driver->driver.name,
2254 struct i2c_client *client;
2256 /* Detection succeeded, instantiate the device */
2257 if (adapter->class & I2C_CLASS_DEPRECATED)
2258 dev_warn(&adapter->dev,
2259 "This adapter will soon drop class based instantiation of devices. "
2260 "Please make sure client 0x%02x gets instantiated by other means. "
2261 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2264 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2265 info.type, info.addr);
2266 client = i2c_new_device(adapter, &info);
2268 list_add_tail(&client->detected, &driver->clients);
2270 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2271 info.type, info.addr);
2276 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2278 const unsigned short *address_list;
2279 struct i2c_client *temp_client;
2281 int adap_id = i2c_adapter_id(adapter);
2283 address_list = driver->address_list;
2284 if (!driver->detect || !address_list)
2287 /* Warn that the adapter lost class based instantiation */
2288 if (adapter->class == I2C_CLASS_DEPRECATED) {
2289 dev_dbg(&adapter->dev,
2290 "This adapter dropped support for I2C classes and "
2291 "won't auto-detect %s devices anymore. If you need it, check "
2292 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2293 driver->driver.name);
2297 /* Stop here if the classes do not match */
2298 if (!(adapter->class & driver->class))
2301 /* Set up a temporary client to help detect callback */
2302 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2305 temp_client->adapter = adapter;
2307 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
2308 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
2309 "addr 0x%02x\n", adap_id, address_list[i]);
2310 temp_client->addr = address_list[i];
2311 err = i2c_detect_address(temp_client, driver);
2320 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2322 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2323 I2C_SMBUS_QUICK, NULL) >= 0;
2325 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2328 i2c_new_probed_device(struct i2c_adapter *adap,
2329 struct i2c_board_info *info,
2330 unsigned short const *addr_list,
2331 int (*probe)(struct i2c_adapter *, unsigned short addr))
2336 probe = i2c_default_probe;
2338 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2339 /* Check address validity */
2340 if (i2c_check_addr_validity(addr_list[i]) < 0) {
2341 dev_warn(&adap->dev, "Invalid 7-bit address "
2342 "0x%02x\n", addr_list[i]);
2346 /* Check address availability */
2347 if (i2c_check_addr_busy(adap, addr_list[i])) {
2348 dev_dbg(&adap->dev, "Address 0x%02x already in "
2349 "use, not probing\n", addr_list[i]);
2353 /* Test address responsiveness */
2354 if (probe(adap, addr_list[i]))
2358 if (addr_list[i] == I2C_CLIENT_END) {
2359 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2363 info->addr = addr_list[i];
2364 return i2c_new_device(adap, info);
2366 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2368 struct i2c_adapter *i2c_get_adapter(int nr)
2370 struct i2c_adapter *adapter;
2372 mutex_lock(&core_lock);
2373 adapter = idr_find(&i2c_adapter_idr, nr);
2374 if (adapter && !try_module_get(adapter->owner))
2377 mutex_unlock(&core_lock);
2380 EXPORT_SYMBOL(i2c_get_adapter);
2382 void i2c_put_adapter(struct i2c_adapter *adap)
2385 module_put(adap->owner);
2387 EXPORT_SYMBOL(i2c_put_adapter);
2389 /* The SMBus parts */
2391 #define POLY (0x1070U << 3)
2392 static u8 crc8(u16 data)
2396 for (i = 0; i < 8; i++) {
2401 return (u8)(data >> 8);
2404 /* Incremental CRC8 over count bytes in the array pointed to by p */
2405 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
2409 for (i = 0; i < count; i++)
2410 crc = crc8((crc ^ p[i]) << 8);
2414 /* Assume a 7-bit address, which is reasonable for SMBus */
2415 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
2417 /* The address will be sent first */
2418 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2419 pec = i2c_smbus_pec(pec, &addr, 1);
2421 /* The data buffer follows */
2422 return i2c_smbus_pec(pec, msg->buf, msg->len);
2425 /* Used for write only transactions */
2426 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
2428 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2432 /* Return <0 on CRC error
2433 If there was a write before this read (most cases) we need to take the
2434 partial CRC from the write part into account.
2435 Note that this function does modify the message (we need to decrease the
2436 message length to hide the CRC byte from the caller). */
2437 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
2439 u8 rpec = msg->buf[--msg->len];
2440 cpec = i2c_smbus_msg_pec(cpec, msg);
2443 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2451 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2452 * @client: Handle to slave device
2454 * This executes the SMBus "receive byte" protocol, returning negative errno
2455 * else the byte received from the device.
2457 s32 i2c_smbus_read_byte(const struct i2c_client *client)
2459 union i2c_smbus_data data;
2462 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2464 I2C_SMBUS_BYTE, &data);
2465 return (status < 0) ? status : data.byte;
2467 EXPORT_SYMBOL(i2c_smbus_read_byte);
2470 * i2c_smbus_write_byte - SMBus "send byte" protocol
2471 * @client: Handle to slave device
2472 * @value: Byte to be sent
2474 * This executes the SMBus "send byte" protocol, returning negative errno
2475 * else zero on success.
2477 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
2479 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2480 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
2482 EXPORT_SYMBOL(i2c_smbus_write_byte);
2485 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2486 * @client: Handle to slave device
2487 * @command: Byte interpreted by slave
2489 * This executes the SMBus "read byte" protocol, returning negative errno
2490 * else a data byte received from the device.
2492 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
2494 union i2c_smbus_data data;
2497 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2498 I2C_SMBUS_READ, command,
2499 I2C_SMBUS_BYTE_DATA, &data);
2500 return (status < 0) ? status : data.byte;
2502 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
2505 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2506 * @client: Handle to slave device
2507 * @command: Byte interpreted by slave
2508 * @value: Byte being written
2510 * This executes the SMBus "write byte" protocol, returning negative errno
2511 * else zero on success.
2513 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2516 union i2c_smbus_data data;
2518 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2519 I2C_SMBUS_WRITE, command,
2520 I2C_SMBUS_BYTE_DATA, &data);
2522 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
2525 * i2c_smbus_read_word_data - SMBus "read word" protocol
2526 * @client: Handle to slave device
2527 * @command: Byte interpreted by slave
2529 * This executes the SMBus "read word" protocol, returning negative errno
2530 * else a 16-bit unsigned "word" received from the device.
2532 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
2534 union i2c_smbus_data data;
2537 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2538 I2C_SMBUS_READ, command,
2539 I2C_SMBUS_WORD_DATA, &data);
2540 return (status < 0) ? status : data.word;
2542 EXPORT_SYMBOL(i2c_smbus_read_word_data);
2545 * i2c_smbus_write_word_data - SMBus "write word" protocol
2546 * @client: Handle to slave device
2547 * @command: Byte interpreted by slave
2548 * @value: 16-bit "word" being written
2550 * This executes the SMBus "write word" protocol, returning negative errno
2551 * else zero on success.
2553 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2556 union i2c_smbus_data data;
2558 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2559 I2C_SMBUS_WRITE, command,
2560 I2C_SMBUS_WORD_DATA, &data);
2562 EXPORT_SYMBOL(i2c_smbus_write_word_data);
2565 * i2c_smbus_read_block_data - SMBus "block read" protocol
2566 * @client: Handle to slave device
2567 * @command: Byte interpreted by slave
2568 * @values: Byte array into which data will be read; big enough to hold
2569 * the data returned by the slave. SMBus allows at most 32 bytes.
2571 * This executes the SMBus "block read" protocol, returning negative errno
2572 * else the number of data bytes in the slave's response.
2574 * Note that using this function requires that the client's adapter support
2575 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2576 * support this; its emulation through I2C messaging relies on a specific
2577 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2579 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
2582 union i2c_smbus_data data;
2585 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2586 I2C_SMBUS_READ, command,
2587 I2C_SMBUS_BLOCK_DATA, &data);
2591 memcpy(values, &data.block[1], data.block[0]);
2592 return data.block[0];
2594 EXPORT_SYMBOL(i2c_smbus_read_block_data);
2597 * i2c_smbus_write_block_data - SMBus "block write" protocol
2598 * @client: Handle to slave device
2599 * @command: Byte interpreted by slave
2600 * @length: Size of data block; SMBus allows at most 32 bytes
2601 * @values: Byte array which will be written.
2603 * This executes the SMBus "block write" protocol, returning negative errno
2604 * else zero on success.
2606 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
2607 u8 length, const u8 *values)
2609 union i2c_smbus_data data;
2611 if (length > I2C_SMBUS_BLOCK_MAX)
2612 length = I2C_SMBUS_BLOCK_MAX;
2613 data.block[0] = length;
2614 memcpy(&data.block[1], values, length);
2615 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2616 I2C_SMBUS_WRITE, command,
2617 I2C_SMBUS_BLOCK_DATA, &data);
2619 EXPORT_SYMBOL(i2c_smbus_write_block_data);
2621 /* Returns the number of read bytes */
2622 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
2623 u8 length, u8 *values)
2625 union i2c_smbus_data data;
2628 if (length > I2C_SMBUS_BLOCK_MAX)
2629 length = I2C_SMBUS_BLOCK_MAX;
2630 data.block[0] = length;
2631 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2632 I2C_SMBUS_READ, command,
2633 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2637 memcpy(values, &data.block[1], data.block[0]);
2638 return data.block[0];
2640 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
2642 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
2643 u8 length, const u8 *values)
2645 union i2c_smbus_data data;
2647 if (length > I2C_SMBUS_BLOCK_MAX)
2648 length = I2C_SMBUS_BLOCK_MAX;
2649 data.block[0] = length;
2650 memcpy(data.block + 1, values, length);
2651 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2652 I2C_SMBUS_WRITE, command,
2653 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2655 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
2657 /* Simulate a SMBus command using the i2c protocol
2658 No checking of parameters is done! */
2659 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2660 unsigned short flags,
2661 char read_write, u8 command, int size,
2662 union i2c_smbus_data *data)
2664 /* So we need to generate a series of msgs. In the case of writing, we
2665 need to use only one message; when reading, we need two. We initialize
2666 most things with sane defaults, to keep the code below somewhat
2668 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2669 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
2670 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
2674 struct i2c_msg msg[2] = {
2682 .flags = flags | I2C_M_RD,
2688 msgbuf0[0] = command;
2690 case I2C_SMBUS_QUICK:
2692 /* Special case: The read/write field is used as data */
2693 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2697 case I2C_SMBUS_BYTE:
2698 if (read_write == I2C_SMBUS_READ) {
2699 /* Special case: only a read! */
2700 msg[0].flags = I2C_M_RD | flags;
2704 case I2C_SMBUS_BYTE_DATA:
2705 if (read_write == I2C_SMBUS_READ)
2709 msgbuf0[1] = data->byte;
2712 case I2C_SMBUS_WORD_DATA:
2713 if (read_write == I2C_SMBUS_READ)
2717 msgbuf0[1] = data->word & 0xff;
2718 msgbuf0[2] = data->word >> 8;
2721 case I2C_SMBUS_PROC_CALL:
2722 num = 2; /* Special case */
2723 read_write = I2C_SMBUS_READ;
2726 msgbuf0[1] = data->word & 0xff;
2727 msgbuf0[2] = data->word >> 8;
2729 case I2C_SMBUS_BLOCK_DATA:
2730 if (read_write == I2C_SMBUS_READ) {
2731 msg[1].flags |= I2C_M_RECV_LEN;
2732 msg[1].len = 1; /* block length will be added by
2733 the underlying bus driver */
2735 msg[0].len = data->block[0] + 2;
2736 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
2737 dev_err(&adapter->dev,
2738 "Invalid block write size %d\n",
2742 for (i = 1; i < msg[0].len; i++)
2743 msgbuf0[i] = data->block[i-1];
2746 case I2C_SMBUS_BLOCK_PROC_CALL:
2747 num = 2; /* Another special case */
2748 read_write = I2C_SMBUS_READ;
2749 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
2750 dev_err(&adapter->dev,
2751 "Invalid block write size %d\n",
2755 msg[0].len = data->block[0] + 2;
2756 for (i = 1; i < msg[0].len; i++)
2757 msgbuf0[i] = data->block[i-1];
2758 msg[1].flags |= I2C_M_RECV_LEN;
2759 msg[1].len = 1; /* block length will be added by
2760 the underlying bus driver */
2762 case I2C_SMBUS_I2C_BLOCK_DATA:
2763 if (read_write == I2C_SMBUS_READ) {
2764 msg[1].len = data->block[0];
2766 msg[0].len = data->block[0] + 1;
2767 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
2768 dev_err(&adapter->dev,
2769 "Invalid block write size %d\n",
2773 for (i = 1; i <= data->block[0]; i++)
2774 msgbuf0[i] = data->block[i];
2778 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2782 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2783 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2785 /* Compute PEC if first message is a write */
2786 if (!(msg[0].flags & I2C_M_RD)) {
2787 if (num == 1) /* Write only */
2788 i2c_smbus_add_pec(&msg[0]);
2789 else /* Write followed by read */
2790 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2792 /* Ask for PEC if last message is a read */
2793 if (msg[num-1].flags & I2C_M_RD)
2797 status = i2c_transfer(adapter, msg, num);
2801 /* Check PEC if last message is a read */
2802 if (i && (msg[num-1].flags & I2C_M_RD)) {
2803 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2808 if (read_write == I2C_SMBUS_READ)
2810 case I2C_SMBUS_BYTE:
2811 data->byte = msgbuf0[0];
2813 case I2C_SMBUS_BYTE_DATA:
2814 data->byte = msgbuf1[0];
2816 case I2C_SMBUS_WORD_DATA:
2817 case I2C_SMBUS_PROC_CALL:
2818 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2820 case I2C_SMBUS_I2C_BLOCK_DATA:
2821 for (i = 0; i < data->block[0]; i++)
2822 data->block[i+1] = msgbuf1[i];
2824 case I2C_SMBUS_BLOCK_DATA:
2825 case I2C_SMBUS_BLOCK_PROC_CALL:
2826 for (i = 0; i < msgbuf1[0] + 1; i++)
2827 data->block[i] = msgbuf1[i];
2834 * i2c_smbus_xfer - execute SMBus protocol operations
2835 * @adapter: Handle to I2C bus
2836 * @addr: Address of SMBus slave on that bus
2837 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2838 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2839 * @command: Byte interpreted by slave, for protocols which use such bytes
2840 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2841 * @data: Data to be read or written
2843 * This executes an SMBus protocol operation, and returns a negative
2844 * errno code else zero on success.
2846 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2847 char read_write, u8 command, int protocol,
2848 union i2c_smbus_data *data)
2850 unsigned long orig_jiffies;
2854 /* If enabled, the following two tracepoints are conditional on
2855 * read_write and protocol.
2857 trace_smbus_write(adapter, addr, flags, read_write,
2858 command, protocol, data);
2859 trace_smbus_read(adapter, addr, flags, read_write,
2862 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
2864 if (adapter->algo->smbus_xfer) {
2865 i2c_lock_adapter(adapter);
2867 /* Retry automatically on arbitration loss */
2868 orig_jiffies = jiffies;
2869 for (res = 0, try = 0; try <= adapter->retries; try++) {
2870 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2871 read_write, command,
2875 if (time_after(jiffies,
2876 orig_jiffies + adapter->timeout))
2879 i2c_unlock_adapter(adapter);
2881 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2884 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2885 * implement native support for the SMBus operation.
2889 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2890 command, protocol, data);
2893 /* If enabled, the reply tracepoint is conditional on read_write. */
2894 trace_smbus_reply(adapter, addr, flags, read_write,
2895 command, protocol, data);
2896 trace_smbus_result(adapter, addr, flags, read_write,
2897 command, protocol, res);
2901 EXPORT_SYMBOL(i2c_smbus_xfer);
2903 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2904 MODULE_DESCRIPTION("I2C-Bus main module");
2905 MODULE_LICENSE("GPL");