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 <khali@linux-fr.org>
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>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/errno.h>
36 #include <linux/gpio.h>
37 #include <linux/slab.h>
38 #include <linux/i2c.h>
39 #include <linux/init.h>
40 #include <linux/idr.h>
41 #include <linux/mutex.h>
43 #include <linux/of_device.h>
44 #include <linux/of_irq.h>
45 #include <linux/completion.h>
46 #include <linux/hardirq.h>
47 #include <linux/irqflags.h>
48 #include <linux/rwsem.h>
49 #include <linux/pm_runtime.h>
50 #include <linux/acpi.h>
51 #include <asm/uaccess.h>
56 /* core_lock protects i2c_adapter_idr, and guarantees
57 that device detection, deletion of detected devices, and attach_adapter
58 calls are serialized */
59 static DEFINE_MUTEX(core_lock);
60 static DEFINE_IDR(i2c_adapter_idr);
62 static struct device_type i2c_client_type;
63 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
65 /* ------------------------------------------------------------------------- */
67 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
68 const struct i2c_client *client)
71 if (strcmp(client->name, id->name) == 0)
78 static int i2c_device_match(struct device *dev, struct device_driver *drv)
80 struct i2c_client *client = i2c_verify_client(dev);
81 struct i2c_driver *driver;
86 /* Attempt an OF style match */
87 if (of_driver_match_device(dev, drv))
90 /* Then ACPI style match */
91 if (acpi_driver_match_device(dev, drv))
94 driver = to_i2c_driver(drv);
95 /* match on an id table if there is one */
97 return i2c_match_id(driver->id_table, client) != NULL;
103 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
104 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
106 struct i2c_client *client = to_i2c_client(dev);
108 if (add_uevent_var(env, "MODALIAS=%s%s",
109 I2C_MODULE_PREFIX, client->name))
111 dev_dbg(dev, "uevent\n");
115 /* i2c bus recovery routines */
116 static int get_scl_gpio_value(struct i2c_adapter *adap)
118 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
121 static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
123 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
126 static int get_sda_gpio_value(struct i2c_adapter *adap)
128 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
131 static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
133 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
134 struct device *dev = &adap->dev;
137 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
138 GPIOF_OUT_INIT_HIGH, "i2c-scl");
140 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
145 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
146 /* work without SDA polling */
147 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
156 static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
158 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
161 gpio_free(bri->sda_gpio);
163 gpio_free(bri->scl_gpio);
167 * We are generating clock pulses. ndelay() determines durating of clk pulses.
168 * We will generate clock with rate 100 KHz and so duration of both clock levels
169 * is: delay in ns = (10^6 / 100) / 2
171 #define RECOVERY_NDELAY 5000
172 #define RECOVERY_CLK_CNT 9
174 static int i2c_generic_recovery(struct i2c_adapter *adap)
176 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
177 int i = 0, val = 1, ret = 0;
179 if (bri->prepare_recovery)
180 bri->prepare_recovery(bri);
183 * By this time SCL is high, as we need to give 9 falling-rising edges
185 while (i++ < RECOVERY_CLK_CNT * 2) {
187 /* Break if SDA is high */
188 if (bri->get_sda && bri->get_sda(adap))
190 /* SCL shouldn't be low here */
191 if (!bri->get_scl(adap)) {
193 "SCL is stuck low, exit recovery\n");
200 bri->set_scl(adap, val);
201 ndelay(RECOVERY_NDELAY);
204 if (bri->unprepare_recovery)
205 bri->unprepare_recovery(bri);
210 int i2c_generic_scl_recovery(struct i2c_adapter *adap)
212 adap->bus_recovery_info->set_scl(adap, 1);
213 return i2c_generic_recovery(adap);
216 int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
220 ret = i2c_get_gpios_for_recovery(adap);
224 ret = i2c_generic_recovery(adap);
225 i2c_put_gpios_for_recovery(adap);
230 int i2c_recover_bus(struct i2c_adapter *adap)
232 if (!adap->bus_recovery_info)
235 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
236 return adap->bus_recovery_info->recover_bus(adap);
239 static int i2c_device_probe(struct device *dev)
241 struct i2c_client *client = i2c_verify_client(dev);
242 struct i2c_driver *driver;
248 driver = to_i2c_driver(dev->driver);
249 if (!driver->probe || !driver->id_table)
252 if (!device_can_wakeup(&client->dev))
253 device_init_wakeup(&client->dev,
254 client->flags & I2C_CLIENT_WAKE);
255 dev_dbg(dev, "probe\n");
257 status = driver->probe(client, i2c_match_id(driver->id_table, client));
259 i2c_set_clientdata(client, NULL);
264 static int i2c_device_remove(struct device *dev)
266 struct i2c_client *client = i2c_verify_client(dev);
267 struct i2c_driver *driver;
270 if (!client || !dev->driver)
273 driver = to_i2c_driver(dev->driver);
274 if (driver->remove) {
275 dev_dbg(dev, "remove\n");
276 status = driver->remove(client);
282 i2c_set_clientdata(client, NULL);
287 static void i2c_device_shutdown(struct device *dev)
289 struct i2c_client *client = i2c_verify_client(dev);
290 struct i2c_driver *driver;
292 if (!client || !dev->driver)
294 driver = to_i2c_driver(dev->driver);
295 if (driver->shutdown)
296 driver->shutdown(client);
299 #ifdef CONFIG_PM_SLEEP
300 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
302 struct i2c_client *client = i2c_verify_client(dev);
303 struct i2c_driver *driver;
305 if (!client || !dev->driver)
307 driver = to_i2c_driver(dev->driver);
308 if (!driver->suspend)
310 return driver->suspend(client, mesg);
313 static int i2c_legacy_resume(struct device *dev)
315 struct i2c_client *client = i2c_verify_client(dev);
316 struct i2c_driver *driver;
318 if (!client || !dev->driver)
320 driver = to_i2c_driver(dev->driver);
323 return driver->resume(client);
326 static int i2c_device_pm_suspend(struct device *dev)
328 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
331 return pm_generic_suspend(dev);
333 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
336 static int i2c_device_pm_resume(struct device *dev)
338 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
341 return pm_generic_resume(dev);
343 return i2c_legacy_resume(dev);
346 static int i2c_device_pm_freeze(struct device *dev)
348 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
351 return pm_generic_freeze(dev);
353 return i2c_legacy_suspend(dev, PMSG_FREEZE);
356 static int i2c_device_pm_thaw(struct device *dev)
358 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
361 return pm_generic_thaw(dev);
363 return i2c_legacy_resume(dev);
366 static int i2c_device_pm_poweroff(struct device *dev)
368 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
371 return pm_generic_poweroff(dev);
373 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
376 static int i2c_device_pm_restore(struct device *dev)
378 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
381 return pm_generic_restore(dev);
383 return i2c_legacy_resume(dev);
385 #else /* !CONFIG_PM_SLEEP */
386 #define i2c_device_pm_suspend NULL
387 #define i2c_device_pm_resume NULL
388 #define i2c_device_pm_freeze NULL
389 #define i2c_device_pm_thaw NULL
390 #define i2c_device_pm_poweroff NULL
391 #define i2c_device_pm_restore NULL
392 #endif /* !CONFIG_PM_SLEEP */
394 static void i2c_client_dev_release(struct device *dev)
396 kfree(to_i2c_client(dev));
400 show_name(struct device *dev, struct device_attribute *attr, char *buf)
402 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
403 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
407 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
409 struct i2c_client *client = to_i2c_client(dev);
410 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
413 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
414 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
416 static struct attribute *i2c_dev_attrs[] = {
418 /* modalias helps coldplug: modprobe $(cat .../modalias) */
419 &dev_attr_modalias.attr,
423 static struct attribute_group i2c_dev_attr_group = {
424 .attrs = i2c_dev_attrs,
427 static const struct attribute_group *i2c_dev_attr_groups[] = {
432 static const struct dev_pm_ops i2c_device_pm_ops = {
433 .suspend = i2c_device_pm_suspend,
434 .resume = i2c_device_pm_resume,
435 .freeze = i2c_device_pm_freeze,
436 .thaw = i2c_device_pm_thaw,
437 .poweroff = i2c_device_pm_poweroff,
438 .restore = i2c_device_pm_restore,
440 pm_generic_runtime_suspend,
441 pm_generic_runtime_resume,
446 struct bus_type i2c_bus_type = {
448 .match = i2c_device_match,
449 .probe = i2c_device_probe,
450 .remove = i2c_device_remove,
451 .shutdown = i2c_device_shutdown,
452 .pm = &i2c_device_pm_ops,
454 EXPORT_SYMBOL_GPL(i2c_bus_type);
456 static struct device_type i2c_client_type = {
457 .groups = i2c_dev_attr_groups,
458 .uevent = i2c_device_uevent,
459 .release = i2c_client_dev_release,
464 * i2c_verify_client - return parameter as i2c_client, or NULL
465 * @dev: device, probably from some driver model iterator
467 * When traversing the driver model tree, perhaps using driver model
468 * iterators like @device_for_each_child(), you can't assume very much
469 * about the nodes you find. Use this function to avoid oopses caused
470 * by wrongly treating some non-I2C device as an i2c_client.
472 struct i2c_client *i2c_verify_client(struct device *dev)
474 return (dev->type == &i2c_client_type)
478 EXPORT_SYMBOL(i2c_verify_client);
481 /* This is a permissive address validity check, I2C address map constraints
482 * are purposely not enforced, except for the general call address. */
483 static int i2c_check_client_addr_validity(const struct i2c_client *client)
485 if (client->flags & I2C_CLIENT_TEN) {
486 /* 10-bit address, all values are valid */
487 if (client->addr > 0x3ff)
490 /* 7-bit address, reject the general call address */
491 if (client->addr == 0x00 || client->addr > 0x7f)
497 /* And this is a strict address validity check, used when probing. If a
498 * device uses a reserved address, then it shouldn't be probed. 7-bit
499 * addressing is assumed, 10-bit address devices are rare and should be
500 * explicitly enumerated. */
501 static int i2c_check_addr_validity(unsigned short addr)
504 * Reserved addresses per I2C specification:
505 * 0x00 General call address / START byte
507 * 0x02 Reserved for different bus format
508 * 0x03 Reserved for future purposes
509 * 0x04-0x07 Hs-mode master code
510 * 0x78-0x7b 10-bit slave addressing
511 * 0x7c-0x7f Reserved for future purposes
513 if (addr < 0x08 || addr > 0x77)
518 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
520 struct i2c_client *client = i2c_verify_client(dev);
521 int addr = *(int *)addrp;
523 if (client && client->addr == addr)
528 /* walk up mux tree */
529 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
531 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
534 result = device_for_each_child(&adapter->dev, &addr,
535 __i2c_check_addr_busy);
537 if (!result && parent)
538 result = i2c_check_mux_parents(parent, addr);
543 /* recurse down mux tree */
544 static int i2c_check_mux_children(struct device *dev, void *addrp)
548 if (dev->type == &i2c_adapter_type)
549 result = device_for_each_child(dev, addrp,
550 i2c_check_mux_children);
552 result = __i2c_check_addr_busy(dev, addrp);
557 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
559 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
563 result = i2c_check_mux_parents(parent, addr);
566 result = device_for_each_child(&adapter->dev, &addr,
567 i2c_check_mux_children);
573 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
574 * @adapter: Target I2C bus segment
576 void i2c_lock_adapter(struct i2c_adapter *adapter)
578 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
581 i2c_lock_adapter(parent);
583 rt_mutex_lock(&adapter->bus_lock);
585 EXPORT_SYMBOL_GPL(i2c_lock_adapter);
588 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
589 * @adapter: Target I2C bus segment
591 static int i2c_trylock_adapter(struct i2c_adapter *adapter)
593 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
596 return i2c_trylock_adapter(parent);
598 return rt_mutex_trylock(&adapter->bus_lock);
602 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
603 * @adapter: Target I2C bus segment
605 void i2c_unlock_adapter(struct i2c_adapter *adapter)
607 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
610 i2c_unlock_adapter(parent);
612 rt_mutex_unlock(&adapter->bus_lock);
614 EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
617 * i2c_new_device - instantiate an i2c device
618 * @adap: the adapter managing the device
619 * @info: describes one I2C device; bus_num is ignored
622 * Create an i2c device. Binding is handled through driver model
623 * probe()/remove() methods. A driver may be bound to this device when we
624 * return from this function, or any later moment (e.g. maybe hotplugging will
625 * load the driver module). This call is not appropriate for use by mainboard
626 * initialization logic, which usually runs during an arch_initcall() long
627 * before any i2c_adapter could exist.
629 * This returns the new i2c client, which may be saved for later use with
630 * i2c_unregister_device(); or NULL to indicate an error.
633 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
635 struct i2c_client *client;
638 client = kzalloc(sizeof *client, GFP_KERNEL);
642 client->adapter = adap;
644 client->dev.platform_data = info->platform_data;
647 client->dev.archdata = *info->archdata;
649 client->flags = info->flags;
650 client->addr = info->addr;
651 client->irq = info->irq;
653 strlcpy(client->name, info->type, sizeof(client->name));
655 /* Check for address validity */
656 status = i2c_check_client_addr_validity(client);
658 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
659 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
663 /* Check for address business */
664 status = i2c_check_addr_busy(adap, client->addr);
668 client->dev.parent = &client->adapter->dev;
669 client->dev.bus = &i2c_bus_type;
670 client->dev.type = &i2c_client_type;
671 client->dev.of_node = info->of_node;
672 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
674 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
675 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
676 client->addr | ((client->flags & I2C_CLIENT_TEN)
678 status = device_register(&client->dev);
682 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
683 client->name, dev_name(&client->dev));
688 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
689 "(%d)\n", client->name, client->addr, status);
694 EXPORT_SYMBOL_GPL(i2c_new_device);
698 * i2c_unregister_device - reverse effect of i2c_new_device()
699 * @client: value returned from i2c_new_device()
702 void i2c_unregister_device(struct i2c_client *client)
704 device_unregister(&client->dev);
706 EXPORT_SYMBOL_GPL(i2c_unregister_device);
709 static const struct i2c_device_id dummy_id[] = {
714 static int dummy_probe(struct i2c_client *client,
715 const struct i2c_device_id *id)
720 static int dummy_remove(struct i2c_client *client)
725 static struct i2c_driver dummy_driver = {
726 .driver.name = "dummy",
727 .probe = dummy_probe,
728 .remove = dummy_remove,
729 .id_table = dummy_id,
733 * i2c_new_dummy - return a new i2c device bound to a dummy driver
734 * @adapter: the adapter managing the device
735 * @address: seven bit address to be used
738 * This returns an I2C client bound to the "dummy" driver, intended for use
739 * with devices that consume multiple addresses. Examples of such chips
740 * include various EEPROMS (like 24c04 and 24c08 models).
742 * These dummy devices have two main uses. First, most I2C and SMBus calls
743 * except i2c_transfer() need a client handle; the dummy will be that handle.
744 * And second, this prevents the specified address from being bound to a
747 * This returns the new i2c client, which should be saved for later use with
748 * i2c_unregister_device(); or NULL to indicate an error.
750 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
752 struct i2c_board_info info = {
753 I2C_BOARD_INFO("dummy", address),
756 return i2c_new_device(adapter, &info);
758 EXPORT_SYMBOL_GPL(i2c_new_dummy);
760 /* ------------------------------------------------------------------------- */
762 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
764 static void i2c_adapter_dev_release(struct device *dev)
766 struct i2c_adapter *adap = to_i2c_adapter(dev);
767 complete(&adap->dev_released);
771 * This function is only needed for mutex_lock_nested, so it is never
772 * called unless locking correctness checking is enabled. Thus we
773 * make it inline to avoid a compiler warning. That's what gcc ends up
776 static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
778 unsigned int depth = 0;
780 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
787 * Let users instantiate I2C devices through sysfs. This can be used when
788 * platform initialization code doesn't contain the proper data for
789 * whatever reason. Also useful for drivers that do device detection and
790 * detection fails, either because the device uses an unexpected address,
791 * or this is a compatible device with different ID register values.
793 * Parameter checking may look overzealous, but we really don't want
794 * the user to provide incorrect parameters.
797 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
798 const char *buf, size_t count)
800 struct i2c_adapter *adap = to_i2c_adapter(dev);
801 struct i2c_board_info info;
802 struct i2c_client *client;
806 memset(&info, 0, sizeof(struct i2c_board_info));
808 blank = strchr(buf, ' ');
810 dev_err(dev, "%s: Missing parameters\n", "new_device");
813 if (blank - buf > I2C_NAME_SIZE - 1) {
814 dev_err(dev, "%s: Invalid device name\n", "new_device");
817 memcpy(info.type, buf, blank - buf);
819 /* Parse remaining parameters, reject extra parameters */
820 res = sscanf(++blank, "%hi%c", &info.addr, &end);
822 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
825 if (res > 1 && end != '\n') {
826 dev_err(dev, "%s: Extra parameters\n", "new_device");
830 client = i2c_new_device(adap, &info);
834 /* Keep track of the added device */
835 mutex_lock(&adap->userspace_clients_lock);
836 list_add_tail(&client->detected, &adap->userspace_clients);
837 mutex_unlock(&adap->userspace_clients_lock);
838 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
839 info.type, info.addr);
845 * And of course let the users delete the devices they instantiated, if
846 * they got it wrong. This interface can only be used to delete devices
847 * instantiated by i2c_sysfs_new_device above. This guarantees that we
848 * don't delete devices to which some kernel code still has references.
850 * Parameter checking may look overzealous, but we really don't want
851 * the user to delete the wrong device.
854 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
855 const char *buf, size_t count)
857 struct i2c_adapter *adap = to_i2c_adapter(dev);
858 struct i2c_client *client, *next;
863 /* Parse parameters, reject extra parameters */
864 res = sscanf(buf, "%hi%c", &addr, &end);
866 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
869 if (res > 1 && end != '\n') {
870 dev_err(dev, "%s: Extra parameters\n", "delete_device");
874 /* Make sure the device was added through sysfs */
876 mutex_lock_nested(&adap->userspace_clients_lock,
877 i2c_adapter_depth(adap));
878 list_for_each_entry_safe(client, next, &adap->userspace_clients,
880 if (client->addr == addr) {
881 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
882 "delete_device", client->name, client->addr);
884 list_del(&client->detected);
885 i2c_unregister_device(client);
890 mutex_unlock(&adap->userspace_clients_lock);
893 dev_err(dev, "%s: Can't find device in list\n",
898 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
899 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
900 i2c_sysfs_delete_device);
902 static struct attribute *i2c_adapter_attrs[] = {
904 &dev_attr_new_device.attr,
905 &dev_attr_delete_device.attr,
909 static struct attribute_group i2c_adapter_attr_group = {
910 .attrs = i2c_adapter_attrs,
913 static const struct attribute_group *i2c_adapter_attr_groups[] = {
914 &i2c_adapter_attr_group,
918 struct device_type i2c_adapter_type = {
919 .groups = i2c_adapter_attr_groups,
920 .release = i2c_adapter_dev_release,
922 EXPORT_SYMBOL_GPL(i2c_adapter_type);
925 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
926 * @dev: device, probably from some driver model iterator
928 * When traversing the driver model tree, perhaps using driver model
929 * iterators like @device_for_each_child(), you can't assume very much
930 * about the nodes you find. Use this function to avoid oopses caused
931 * by wrongly treating some non-I2C device as an i2c_adapter.
933 struct i2c_adapter *i2c_verify_adapter(struct device *dev)
935 return (dev->type == &i2c_adapter_type)
936 ? to_i2c_adapter(dev)
939 EXPORT_SYMBOL(i2c_verify_adapter);
941 #ifdef CONFIG_I2C_COMPAT
942 static struct class_compat *i2c_adapter_compat_class;
945 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
947 struct i2c_devinfo *devinfo;
949 down_read(&__i2c_board_lock);
950 list_for_each_entry(devinfo, &__i2c_board_list, list) {
951 if (devinfo->busnum == adapter->nr
952 && !i2c_new_device(adapter,
953 &devinfo->board_info))
954 dev_err(&adapter->dev,
955 "Can't create device at 0x%02x\n",
956 devinfo->board_info.addr);
958 up_read(&__i2c_board_lock);
961 /* OF support code */
963 #if IS_ENABLED(CONFIG_OF)
964 static void of_i2c_register_devices(struct i2c_adapter *adap)
967 struct device_node *node;
969 /* Only register child devices if the adapter has a node pointer set */
970 if (!adap->dev.of_node)
973 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
975 for_each_available_child_of_node(adap->dev.of_node, node) {
976 struct i2c_board_info info = {};
977 struct dev_archdata dev_ad = {};
981 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
983 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
984 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
989 addr = of_get_property(node, "reg", &len);
990 if (!addr || (len < sizeof(int))) {
991 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
996 info.addr = be32_to_cpup(addr);
997 if (info.addr > (1 << 10) - 1) {
998 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
999 info.addr, node->full_name);
1003 info.irq = irq_of_parse_and_map(node, 0);
1004 info.of_node = of_node_get(node);
1005 info.archdata = &dev_ad;
1007 if (of_get_property(node, "wakeup-source", NULL))
1008 info.flags |= I2C_CLIENT_WAKE;
1010 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1012 result = i2c_new_device(adap, &info);
1013 if (result == NULL) {
1014 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1017 irq_dispose_mapping(info.irq);
1023 static int of_dev_node_match(struct device *dev, void *data)
1025 return dev->of_node == data;
1028 /* must call put_device() when done with returned i2c_client device */
1029 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1033 dev = bus_find_device(&i2c_bus_type, NULL, node,
1038 return i2c_verify_client(dev);
1040 EXPORT_SYMBOL(of_find_i2c_device_by_node);
1042 /* must call put_device() when done with returned i2c_adapter device */
1043 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1047 dev = bus_find_device(&i2c_bus_type, NULL, node,
1052 return i2c_verify_adapter(dev);
1054 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1056 static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1057 #endif /* CONFIG_OF */
1059 /* ACPI support code */
1061 #if IS_ENABLED(CONFIG_ACPI)
1062 static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1064 struct i2c_board_info *info = data;
1066 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1067 struct acpi_resource_i2c_serialbus *sb;
1069 sb = &ares->data.i2c_serial_bus;
1070 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1071 info->addr = sb->slave_address;
1072 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1073 info->flags |= I2C_CLIENT_TEN;
1075 } else if (info->irq < 0) {
1078 if (acpi_dev_resource_interrupt(ares, 0, &r))
1079 info->irq = r.start;
1082 /* Tell the ACPI core to skip this resource */
1086 static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1087 void *data, void **return_value)
1089 struct i2c_adapter *adapter = data;
1090 struct list_head resource_list;
1091 struct i2c_board_info info;
1092 struct acpi_device *adev;
1095 if (acpi_bus_get_device(handle, &adev))
1097 if (acpi_bus_get_status(adev) || !adev->status.present)
1100 memset(&info, 0, sizeof(info));
1101 info.acpi_node.handle = handle;
1104 INIT_LIST_HEAD(&resource_list);
1105 ret = acpi_dev_get_resources(adev, &resource_list,
1106 acpi_i2c_add_resource, &info);
1107 acpi_dev_free_resource_list(&resource_list);
1109 if (ret < 0 || !info.addr)
1112 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1113 if (!i2c_new_device(adapter, &info)) {
1114 dev_err(&adapter->dev,
1115 "failed to add I2C device %s from ACPI\n",
1116 dev_name(&adev->dev));
1123 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1124 * @adap: pointer to adapter
1126 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1127 * namespace. When a device is found it will be added to the Linux device
1128 * model and bound to the corresponding ACPI handle.
1130 static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1135 handle = ACPI_HANDLE(adap->dev.parent);
1139 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1140 acpi_i2c_add_device, NULL,
1142 if (ACPI_FAILURE(status))
1143 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1146 static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1147 #endif /* CONFIG_ACPI */
1149 static int i2c_do_add_adapter(struct i2c_driver *driver,
1150 struct i2c_adapter *adap)
1152 /* Detect supported devices on that bus, and instantiate them */
1153 i2c_detect(adap, driver);
1155 /* Let legacy drivers scan this bus for matching devices */
1156 if (driver->attach_adapter) {
1157 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1158 driver->driver.name);
1159 dev_warn(&adap->dev, "Please use another way to instantiate "
1160 "your i2c_client\n");
1161 /* We ignore the return code; if it fails, too bad */
1162 driver->attach_adapter(adap);
1167 static int __process_new_adapter(struct device_driver *d, void *data)
1169 return i2c_do_add_adapter(to_i2c_driver(d), data);
1172 static int i2c_register_adapter(struct i2c_adapter *adap)
1176 /* Can't register until after driver model init */
1177 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1183 if (unlikely(adap->name[0] == '\0')) {
1184 pr_err("i2c-core: Attempt to register an adapter with "
1188 if (unlikely(!adap->algo)) {
1189 pr_err("i2c-core: Attempt to register adapter '%s' with "
1190 "no algo!\n", adap->name);
1194 rt_mutex_init(&adap->bus_lock);
1195 mutex_init(&adap->userspace_clients_lock);
1196 INIT_LIST_HEAD(&adap->userspace_clients);
1198 /* Set default timeout to 1 second if not already set */
1199 if (adap->timeout == 0)
1202 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
1203 adap->dev.bus = &i2c_bus_type;
1204 adap->dev.type = &i2c_adapter_type;
1205 res = device_register(&adap->dev);
1209 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1211 #ifdef CONFIG_I2C_COMPAT
1212 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1215 dev_warn(&adap->dev,
1216 "Failed to create compatibility class link\n");
1219 /* bus recovery specific initialization */
1220 if (adap->bus_recovery_info) {
1221 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1223 if (!bri->recover_bus) {
1224 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1225 adap->bus_recovery_info = NULL;
1229 /* Generic GPIO recovery */
1230 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1231 if (!gpio_is_valid(bri->scl_gpio)) {
1232 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1233 adap->bus_recovery_info = NULL;
1237 if (gpio_is_valid(bri->sda_gpio))
1238 bri->get_sda = get_sda_gpio_value;
1240 bri->get_sda = NULL;
1242 bri->get_scl = get_scl_gpio_value;
1243 bri->set_scl = set_scl_gpio_value;
1244 } else if (!bri->set_scl || !bri->get_scl) {
1245 /* Generic SCL recovery */
1246 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1247 adap->bus_recovery_info = NULL;
1252 /* create pre-declared device nodes */
1253 of_i2c_register_devices(adap);
1254 acpi_i2c_register_devices(adap);
1256 if (adap->nr < __i2c_first_dynamic_bus_num)
1257 i2c_scan_static_board_info(adap);
1259 /* Notify drivers */
1260 mutex_lock(&core_lock);
1261 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1262 mutex_unlock(&core_lock);
1267 mutex_lock(&core_lock);
1268 idr_remove(&i2c_adapter_idr, adap->nr);
1269 mutex_unlock(&core_lock);
1274 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1275 * @adap: the adapter to register (with adap->nr initialized)
1276 * Context: can sleep
1278 * See i2c_add_numbered_adapter() for details.
1280 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1284 mutex_lock(&core_lock);
1285 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1287 mutex_unlock(&core_lock);
1289 return id == -ENOSPC ? -EBUSY : id;
1291 return i2c_register_adapter(adap);
1295 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1296 * @adapter: the adapter to add
1297 * Context: can sleep
1299 * This routine is used to declare an I2C adapter when its bus number
1300 * doesn't matter or when its bus number is specified by an dt alias.
1301 * Examples of bases when the bus number doesn't matter: I2C adapters
1302 * dynamically added by USB links or PCI plugin cards.
1304 * When this returns zero, a new bus number was allocated and stored
1305 * in adap->nr, and the specified adapter became available for clients.
1306 * Otherwise, a negative errno value is returned.
1308 int i2c_add_adapter(struct i2c_adapter *adapter)
1310 struct device *dev = &adapter->dev;
1314 id = of_alias_get_id(dev->of_node, "i2c");
1317 return __i2c_add_numbered_adapter(adapter);
1321 mutex_lock(&core_lock);
1322 id = idr_alloc(&i2c_adapter_idr, adapter,
1323 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
1324 mutex_unlock(&core_lock);
1330 return i2c_register_adapter(adapter);
1332 EXPORT_SYMBOL(i2c_add_adapter);
1335 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1336 * @adap: the adapter to register (with adap->nr initialized)
1337 * Context: can sleep
1339 * This routine is used to declare an I2C adapter when its bus number
1340 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1341 * or otherwise built in to the system's mainboard, and where i2c_board_info
1342 * is used to properly configure I2C devices.
1344 * If the requested bus number is set to -1, then this function will behave
1345 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1347 * If no devices have pre-been declared for this bus, then be sure to
1348 * register the adapter before any dynamically allocated ones. Otherwise
1349 * the required bus ID may not be available.
1351 * When this returns zero, the specified adapter became available for
1352 * clients using the bus number provided in adap->nr. Also, the table
1353 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1354 * and the appropriate driver model device nodes are created. Otherwise, a
1355 * negative errno value is returned.
1357 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1359 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1360 return i2c_add_adapter(adap);
1362 return __i2c_add_numbered_adapter(adap);
1364 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1366 static void i2c_do_del_adapter(struct i2c_driver *driver,
1367 struct i2c_adapter *adapter)
1369 struct i2c_client *client, *_n;
1371 /* Remove the devices we created ourselves as the result of hardware
1372 * probing (using a driver's detect method) */
1373 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1374 if (client->adapter == adapter) {
1375 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1376 client->name, client->addr);
1377 list_del(&client->detected);
1378 i2c_unregister_device(client);
1383 static int __unregister_client(struct device *dev, void *dummy)
1385 struct i2c_client *client = i2c_verify_client(dev);
1386 if (client && strcmp(client->name, "dummy"))
1387 i2c_unregister_device(client);
1391 static int __unregister_dummy(struct device *dev, void *dummy)
1393 struct i2c_client *client = i2c_verify_client(dev);
1395 i2c_unregister_device(client);
1399 static int __process_removed_adapter(struct device_driver *d, void *data)
1401 i2c_do_del_adapter(to_i2c_driver(d), data);
1406 * i2c_del_adapter - unregister I2C adapter
1407 * @adap: the adapter being unregistered
1408 * Context: can sleep
1410 * This unregisters an I2C adapter which was previously registered
1411 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1413 void i2c_del_adapter(struct i2c_adapter *adap)
1415 struct i2c_adapter *found;
1416 struct i2c_client *client, *next;
1418 /* First make sure that this adapter was ever added */
1419 mutex_lock(&core_lock);
1420 found = idr_find(&i2c_adapter_idr, adap->nr);
1421 mutex_unlock(&core_lock);
1422 if (found != adap) {
1423 pr_debug("i2c-core: attempting to delete unregistered "
1424 "adapter [%s]\n", adap->name);
1428 /* Tell drivers about this removal */
1429 mutex_lock(&core_lock);
1430 bus_for_each_drv(&i2c_bus_type, NULL, adap,
1431 __process_removed_adapter);
1432 mutex_unlock(&core_lock);
1434 /* Remove devices instantiated from sysfs */
1435 mutex_lock_nested(&adap->userspace_clients_lock,
1436 i2c_adapter_depth(adap));
1437 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1439 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1441 list_del(&client->detected);
1442 i2c_unregister_device(client);
1444 mutex_unlock(&adap->userspace_clients_lock);
1446 /* Detach any active clients. This can't fail, thus we do not
1447 * check the returned value. This is a two-pass process, because
1448 * we can't remove the dummy devices during the first pass: they
1449 * could have been instantiated by real devices wishing to clean
1450 * them up properly, so we give them a chance to do that first. */
1451 device_for_each_child(&adap->dev, NULL, __unregister_client);
1452 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1454 #ifdef CONFIG_I2C_COMPAT
1455 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1459 /* device name is gone after device_unregister */
1460 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1462 /* clean up the sysfs representation */
1463 init_completion(&adap->dev_released);
1464 device_unregister(&adap->dev);
1466 /* wait for sysfs to drop all references */
1467 wait_for_completion(&adap->dev_released);
1470 mutex_lock(&core_lock);
1471 idr_remove(&i2c_adapter_idr, adap->nr);
1472 mutex_unlock(&core_lock);
1474 /* Clear the device structure in case this adapter is ever going to be
1476 memset(&adap->dev, 0, sizeof(adap->dev));
1478 EXPORT_SYMBOL(i2c_del_adapter);
1480 /* ------------------------------------------------------------------------- */
1482 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1486 mutex_lock(&core_lock);
1487 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1488 mutex_unlock(&core_lock);
1492 EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1494 static int __process_new_driver(struct device *dev, void *data)
1496 if (dev->type != &i2c_adapter_type)
1498 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1502 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1503 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1506 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1510 /* Can't register until after driver model init */
1511 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1514 /* add the driver to the list of i2c drivers in the driver core */
1515 driver->driver.owner = owner;
1516 driver->driver.bus = &i2c_bus_type;
1518 /* When registration returns, the driver core
1519 * will have called probe() for all matching-but-unbound devices.
1521 res = driver_register(&driver->driver);
1525 /* Drivers should switch to dev_pm_ops instead. */
1526 if (driver->suspend)
1527 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1528 driver->driver.name);
1530 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1531 driver->driver.name);
1533 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1535 INIT_LIST_HEAD(&driver->clients);
1536 /* Walk the adapters that are already present */
1537 i2c_for_each_dev(driver, __process_new_driver);
1541 EXPORT_SYMBOL(i2c_register_driver);
1543 static int __process_removed_driver(struct device *dev, void *data)
1545 if (dev->type == &i2c_adapter_type)
1546 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1551 * i2c_del_driver - unregister I2C driver
1552 * @driver: the driver being unregistered
1553 * Context: can sleep
1555 void i2c_del_driver(struct i2c_driver *driver)
1557 i2c_for_each_dev(driver, __process_removed_driver);
1559 driver_unregister(&driver->driver);
1560 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1562 EXPORT_SYMBOL(i2c_del_driver);
1564 /* ------------------------------------------------------------------------- */
1567 * i2c_use_client - increments the reference count of the i2c client structure
1568 * @client: the client being referenced
1570 * Each live reference to a client should be refcounted. The driver model does
1571 * that automatically as part of driver binding, so that most drivers don't
1572 * need to do this explicitly: they hold a reference until they're unbound
1575 * A pointer to the client with the incremented reference counter is returned.
1577 struct i2c_client *i2c_use_client(struct i2c_client *client)
1579 if (client && get_device(&client->dev))
1583 EXPORT_SYMBOL(i2c_use_client);
1586 * i2c_release_client - release a use of the i2c client structure
1587 * @client: the client being no longer referenced
1589 * Must be called when a user of a client is finished with it.
1591 void i2c_release_client(struct i2c_client *client)
1594 put_device(&client->dev);
1596 EXPORT_SYMBOL(i2c_release_client);
1598 struct i2c_cmd_arg {
1603 static int i2c_cmd(struct device *dev, void *_arg)
1605 struct i2c_client *client = i2c_verify_client(dev);
1606 struct i2c_cmd_arg *arg = _arg;
1607 struct i2c_driver *driver;
1609 if (!client || !client->dev.driver)
1612 driver = to_i2c_driver(client->dev.driver);
1613 if (driver->command)
1614 driver->command(client, arg->cmd, arg->arg);
1618 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1620 struct i2c_cmd_arg cmd_arg;
1624 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1626 EXPORT_SYMBOL(i2c_clients_command);
1628 static int __init i2c_init(void)
1632 retval = bus_register(&i2c_bus_type);
1635 #ifdef CONFIG_I2C_COMPAT
1636 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1637 if (!i2c_adapter_compat_class) {
1642 retval = i2c_add_driver(&dummy_driver);
1648 #ifdef CONFIG_I2C_COMPAT
1649 class_compat_unregister(i2c_adapter_compat_class);
1652 bus_unregister(&i2c_bus_type);
1656 static void __exit i2c_exit(void)
1658 i2c_del_driver(&dummy_driver);
1659 #ifdef CONFIG_I2C_COMPAT
1660 class_compat_unregister(i2c_adapter_compat_class);
1662 bus_unregister(&i2c_bus_type);
1665 /* We must initialize early, because some subsystems register i2c drivers
1666 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1668 postcore_initcall(i2c_init);
1669 module_exit(i2c_exit);
1671 /* ----------------------------------------------------
1672 * the functional interface to the i2c busses.
1673 * ----------------------------------------------------
1677 * __i2c_transfer - unlocked flavor of i2c_transfer
1678 * @adap: Handle to I2C bus
1679 * @msgs: One or more messages to execute before STOP is issued to
1680 * terminate the operation; each message begins with a START.
1681 * @num: Number of messages to be executed.
1683 * Returns negative errno, else the number of messages executed.
1685 * Adapter lock must be held when calling this function. No debug logging
1686 * takes place. adap->algo->master_xfer existence isn't checked.
1688 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1690 unsigned long orig_jiffies;
1693 /* Retry automatically on arbitration loss */
1694 orig_jiffies = jiffies;
1695 for (ret = 0, try = 0; try <= adap->retries; try++) {
1696 ret = adap->algo->master_xfer(adap, msgs, num);
1699 if (time_after(jiffies, orig_jiffies + adap->timeout))
1705 EXPORT_SYMBOL(__i2c_transfer);
1708 * i2c_transfer - execute a single or combined I2C message
1709 * @adap: Handle to I2C bus
1710 * @msgs: One or more messages to execute before STOP is issued to
1711 * terminate the operation; each message begins with a START.
1712 * @num: Number of messages to be executed.
1714 * Returns negative errno, else the number of messages executed.
1716 * Note that there is no requirement that each message be sent to
1717 * the same slave address, although that is the most common model.
1719 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1723 /* REVISIT the fault reporting model here is weak:
1725 * - When we get an error after receiving N bytes from a slave,
1726 * there is no way to report "N".
1728 * - When we get a NAK after transmitting N bytes to a slave,
1729 * there is no way to report "N" ... or to let the master
1730 * continue executing the rest of this combined message, if
1731 * that's the appropriate response.
1733 * - When for example "num" is two and we successfully complete
1734 * the first message but get an error part way through the
1735 * second, it's unclear whether that should be reported as
1736 * one (discarding status on the second message) or errno
1737 * (discarding status on the first one).
1740 if (adap->algo->master_xfer) {
1742 for (ret = 0; ret < num; ret++) {
1743 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1744 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1745 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1746 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1750 if (in_atomic() || irqs_disabled()) {
1751 ret = i2c_trylock_adapter(adap);
1753 /* I2C activity is ongoing. */
1756 i2c_lock_adapter(adap);
1759 ret = __i2c_transfer(adap, msgs, num);
1760 i2c_unlock_adapter(adap);
1764 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1768 EXPORT_SYMBOL(i2c_transfer);
1771 * i2c_master_send - issue a single I2C message in master transmit mode
1772 * @client: Handle to slave device
1773 * @buf: Data that will be written to the slave
1774 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1776 * Returns negative errno, or else the number of bytes written.
1778 int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
1781 struct i2c_adapter *adap = client->adapter;
1784 msg.addr = client->addr;
1785 msg.flags = client->flags & I2C_M_TEN;
1787 msg.buf = (char *)buf;
1789 ret = i2c_transfer(adap, &msg, 1);
1792 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1793 * transmitted, else error code.
1795 return (ret == 1) ? count : ret;
1797 EXPORT_SYMBOL(i2c_master_send);
1800 * i2c_master_recv - issue a single I2C message in master receive mode
1801 * @client: Handle to slave device
1802 * @buf: Where to store data read from slave
1803 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1805 * Returns negative errno, or else the number of bytes read.
1807 int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
1809 struct i2c_adapter *adap = client->adapter;
1813 msg.addr = client->addr;
1814 msg.flags = client->flags & I2C_M_TEN;
1815 msg.flags |= I2C_M_RD;
1819 ret = i2c_transfer(adap, &msg, 1);
1822 * If everything went ok (i.e. 1 msg received), return #bytes received,
1825 return (ret == 1) ? count : ret;
1827 EXPORT_SYMBOL(i2c_master_recv);
1829 /* ----------------------------------------------------
1830 * the i2c address scanning function
1831 * Will not work for 10-bit addresses!
1832 * ----------------------------------------------------
1836 * Legacy default probe function, mostly relevant for SMBus. The default
1837 * probe method is a quick write, but it is known to corrupt the 24RF08
1838 * EEPROMs due to a state machine bug, and could also irreversibly
1839 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1840 * we use a short byte read instead. Also, some bus drivers don't implement
1841 * quick write, so we fallback to a byte read in that case too.
1842 * On x86, there is another special case for FSC hardware monitoring chips,
1843 * which want regular byte reads (address 0x73.) Fortunately, these are the
1844 * only known chips using this I2C address on PC hardware.
1845 * Returns 1 if probe succeeded, 0 if not.
1847 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1850 union i2c_smbus_data dummy;
1853 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1854 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1855 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1856 I2C_SMBUS_BYTE_DATA, &dummy);
1859 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1860 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1861 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1862 I2C_SMBUS_QUICK, NULL);
1863 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1864 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1865 I2C_SMBUS_BYTE, &dummy);
1867 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1875 static int i2c_detect_address(struct i2c_client *temp_client,
1876 struct i2c_driver *driver)
1878 struct i2c_board_info info;
1879 struct i2c_adapter *adapter = temp_client->adapter;
1880 int addr = temp_client->addr;
1883 /* Make sure the address is valid */
1884 err = i2c_check_addr_validity(addr);
1886 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1891 /* Skip if already in use */
1892 if (i2c_check_addr_busy(adapter, addr))
1895 /* Make sure there is something at this address */
1896 if (!i2c_default_probe(adapter, addr))
1899 /* Finally call the custom detection function */
1900 memset(&info, 0, sizeof(struct i2c_board_info));
1902 err = driver->detect(temp_client, &info);
1904 /* -ENODEV is returned if the detection fails. We catch it
1905 here as this isn't an error. */
1906 return err == -ENODEV ? 0 : err;
1909 /* Consistency check */
1910 if (info.type[0] == '\0') {
1911 dev_err(&adapter->dev, "%s detection function provided "
1912 "no name for 0x%x\n", driver->driver.name,
1915 struct i2c_client *client;
1917 /* Detection succeeded, instantiate the device */
1918 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1919 info.type, info.addr);
1920 client = i2c_new_device(adapter, &info);
1922 list_add_tail(&client->detected, &driver->clients);
1924 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1925 info.type, info.addr);
1930 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1932 const unsigned short *address_list;
1933 struct i2c_client *temp_client;
1935 int adap_id = i2c_adapter_id(adapter);
1937 address_list = driver->address_list;
1938 if (!driver->detect || !address_list)
1941 /* Stop here if the classes do not match */
1942 if (!(adapter->class & driver->class))
1945 /* Set up a temporary client to help detect callback */
1946 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1949 temp_client->adapter = adapter;
1951 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1952 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1953 "addr 0x%02x\n", adap_id, address_list[i]);
1954 temp_client->addr = address_list[i];
1955 err = i2c_detect_address(temp_client, driver);
1964 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1966 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1967 I2C_SMBUS_QUICK, NULL) >= 0;
1969 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1972 i2c_new_probed_device(struct i2c_adapter *adap,
1973 struct i2c_board_info *info,
1974 unsigned short const *addr_list,
1975 int (*probe)(struct i2c_adapter *, unsigned short addr))
1980 probe = i2c_default_probe;
1982 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1983 /* Check address validity */
1984 if (i2c_check_addr_validity(addr_list[i]) < 0) {
1985 dev_warn(&adap->dev, "Invalid 7-bit address "
1986 "0x%02x\n", addr_list[i]);
1990 /* Check address availability */
1991 if (i2c_check_addr_busy(adap, addr_list[i])) {
1992 dev_dbg(&adap->dev, "Address 0x%02x already in "
1993 "use, not probing\n", addr_list[i]);
1997 /* Test address responsiveness */
1998 if (probe(adap, addr_list[i]))
2002 if (addr_list[i] == I2C_CLIENT_END) {
2003 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2007 info->addr = addr_list[i];
2008 return i2c_new_device(adap, info);
2010 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2012 struct i2c_adapter *i2c_get_adapter(int nr)
2014 struct i2c_adapter *adapter;
2016 mutex_lock(&core_lock);
2017 adapter = idr_find(&i2c_adapter_idr, nr);
2018 if (adapter && !try_module_get(adapter->owner))
2021 mutex_unlock(&core_lock);
2024 EXPORT_SYMBOL(i2c_get_adapter);
2026 void i2c_put_adapter(struct i2c_adapter *adap)
2029 module_put(adap->owner);
2031 EXPORT_SYMBOL(i2c_put_adapter);
2033 /* The SMBus parts */
2035 #define POLY (0x1070U << 3)
2036 static u8 crc8(u16 data)
2040 for (i = 0; i < 8; i++) {
2045 return (u8)(data >> 8);
2048 /* Incremental CRC8 over count bytes in the array pointed to by p */
2049 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
2053 for (i = 0; i < count; i++)
2054 crc = crc8((crc ^ p[i]) << 8);
2058 /* Assume a 7-bit address, which is reasonable for SMBus */
2059 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
2061 /* The address will be sent first */
2062 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2063 pec = i2c_smbus_pec(pec, &addr, 1);
2065 /* The data buffer follows */
2066 return i2c_smbus_pec(pec, msg->buf, msg->len);
2069 /* Used for write only transactions */
2070 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
2072 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2076 /* Return <0 on CRC error
2077 If there was a write before this read (most cases) we need to take the
2078 partial CRC from the write part into account.
2079 Note that this function does modify the message (we need to decrease the
2080 message length to hide the CRC byte from the caller). */
2081 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
2083 u8 rpec = msg->buf[--msg->len];
2084 cpec = i2c_smbus_msg_pec(cpec, msg);
2087 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2095 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2096 * @client: Handle to slave device
2098 * This executes the SMBus "receive byte" protocol, returning negative errno
2099 * else the byte received from the device.
2101 s32 i2c_smbus_read_byte(const struct i2c_client *client)
2103 union i2c_smbus_data data;
2106 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2108 I2C_SMBUS_BYTE, &data);
2109 return (status < 0) ? status : data.byte;
2111 EXPORT_SYMBOL(i2c_smbus_read_byte);
2114 * i2c_smbus_write_byte - SMBus "send byte" protocol
2115 * @client: Handle to slave device
2116 * @value: Byte to be sent
2118 * This executes the SMBus "send byte" protocol, returning negative errno
2119 * else zero on success.
2121 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
2123 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2124 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
2126 EXPORT_SYMBOL(i2c_smbus_write_byte);
2129 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2130 * @client: Handle to slave device
2131 * @command: Byte interpreted by slave
2133 * This executes the SMBus "read byte" protocol, returning negative errno
2134 * else a data byte received from the device.
2136 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
2138 union i2c_smbus_data data;
2141 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2142 I2C_SMBUS_READ, command,
2143 I2C_SMBUS_BYTE_DATA, &data);
2144 return (status < 0) ? status : data.byte;
2146 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
2149 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2150 * @client: Handle to slave device
2151 * @command: Byte interpreted by slave
2152 * @value: Byte being written
2154 * This executes the SMBus "write byte" protocol, returning negative errno
2155 * else zero on success.
2157 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2160 union i2c_smbus_data data;
2162 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2163 I2C_SMBUS_WRITE, command,
2164 I2C_SMBUS_BYTE_DATA, &data);
2166 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
2169 * i2c_smbus_read_word_data - SMBus "read word" protocol
2170 * @client: Handle to slave device
2171 * @command: Byte interpreted by slave
2173 * This executes the SMBus "read word" protocol, returning negative errno
2174 * else a 16-bit unsigned "word" received from the device.
2176 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
2178 union i2c_smbus_data data;
2181 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2182 I2C_SMBUS_READ, command,
2183 I2C_SMBUS_WORD_DATA, &data);
2184 return (status < 0) ? status : data.word;
2186 EXPORT_SYMBOL(i2c_smbus_read_word_data);
2189 * i2c_smbus_write_word_data - SMBus "write word" protocol
2190 * @client: Handle to slave device
2191 * @command: Byte interpreted by slave
2192 * @value: 16-bit "word" being written
2194 * This executes the SMBus "write word" protocol, returning negative errno
2195 * else zero on success.
2197 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2200 union i2c_smbus_data data;
2202 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2203 I2C_SMBUS_WRITE, command,
2204 I2C_SMBUS_WORD_DATA, &data);
2206 EXPORT_SYMBOL(i2c_smbus_write_word_data);
2209 * i2c_smbus_read_block_data - SMBus "block read" protocol
2210 * @client: Handle to slave device
2211 * @command: Byte interpreted by slave
2212 * @values: Byte array into which data will be read; big enough to hold
2213 * the data returned by the slave. SMBus allows at most 32 bytes.
2215 * This executes the SMBus "block read" protocol, returning negative errno
2216 * else the number of data bytes in the slave's response.
2218 * Note that using this function requires that the client's adapter support
2219 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2220 * support this; its emulation through I2C messaging relies on a specific
2221 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2223 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
2226 union i2c_smbus_data data;
2229 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2230 I2C_SMBUS_READ, command,
2231 I2C_SMBUS_BLOCK_DATA, &data);
2235 memcpy(values, &data.block[1], data.block[0]);
2236 return data.block[0];
2238 EXPORT_SYMBOL(i2c_smbus_read_block_data);
2241 * i2c_smbus_write_block_data - SMBus "block write" protocol
2242 * @client: Handle to slave device
2243 * @command: Byte interpreted by slave
2244 * @length: Size of data block; SMBus allows at most 32 bytes
2245 * @values: Byte array which will be written.
2247 * This executes the SMBus "block write" protocol, returning negative errno
2248 * else zero on success.
2250 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
2251 u8 length, const u8 *values)
2253 union i2c_smbus_data data;
2255 if (length > I2C_SMBUS_BLOCK_MAX)
2256 length = I2C_SMBUS_BLOCK_MAX;
2257 data.block[0] = length;
2258 memcpy(&data.block[1], values, length);
2259 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2260 I2C_SMBUS_WRITE, command,
2261 I2C_SMBUS_BLOCK_DATA, &data);
2263 EXPORT_SYMBOL(i2c_smbus_write_block_data);
2265 /* Returns the number of read bytes */
2266 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
2267 u8 length, u8 *values)
2269 union i2c_smbus_data data;
2272 if (length > I2C_SMBUS_BLOCK_MAX)
2273 length = I2C_SMBUS_BLOCK_MAX;
2274 data.block[0] = length;
2275 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2276 I2C_SMBUS_READ, command,
2277 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2281 memcpy(values, &data.block[1], data.block[0]);
2282 return data.block[0];
2284 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
2286 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
2287 u8 length, const u8 *values)
2289 union i2c_smbus_data data;
2291 if (length > I2C_SMBUS_BLOCK_MAX)
2292 length = I2C_SMBUS_BLOCK_MAX;
2293 data.block[0] = length;
2294 memcpy(data.block + 1, values, length);
2295 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2296 I2C_SMBUS_WRITE, command,
2297 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2299 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
2301 /* Simulate a SMBus command using the i2c protocol
2302 No checking of parameters is done! */
2303 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2304 unsigned short flags,
2305 char read_write, u8 command, int size,
2306 union i2c_smbus_data *data)
2308 /* So we need to generate a series of msgs. In the case of writing, we
2309 need to use only one message; when reading, we need two. We initialize
2310 most things with sane defaults, to keep the code below somewhat
2312 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2313 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
2314 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
2318 struct i2c_msg msg[2] = {
2326 .flags = flags | I2C_M_RD,
2332 msgbuf0[0] = command;
2334 case I2C_SMBUS_QUICK:
2336 /* Special case: The read/write field is used as data */
2337 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2341 case I2C_SMBUS_BYTE:
2342 if (read_write == I2C_SMBUS_READ) {
2343 /* Special case: only a read! */
2344 msg[0].flags = I2C_M_RD | flags;
2348 case I2C_SMBUS_BYTE_DATA:
2349 if (read_write == I2C_SMBUS_READ)
2353 msgbuf0[1] = data->byte;
2356 case I2C_SMBUS_WORD_DATA:
2357 if (read_write == I2C_SMBUS_READ)
2361 msgbuf0[1] = data->word & 0xff;
2362 msgbuf0[2] = data->word >> 8;
2365 case I2C_SMBUS_PROC_CALL:
2366 num = 2; /* Special case */
2367 read_write = I2C_SMBUS_READ;
2370 msgbuf0[1] = data->word & 0xff;
2371 msgbuf0[2] = data->word >> 8;
2373 case I2C_SMBUS_BLOCK_DATA:
2374 if (read_write == I2C_SMBUS_READ) {
2375 msg[1].flags |= I2C_M_RECV_LEN;
2376 msg[1].len = 1; /* block length will be added by
2377 the underlying bus driver */
2379 msg[0].len = data->block[0] + 2;
2380 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
2381 dev_err(&adapter->dev,
2382 "Invalid block write size %d\n",
2386 for (i = 1; i < msg[0].len; i++)
2387 msgbuf0[i] = data->block[i-1];
2390 case I2C_SMBUS_BLOCK_PROC_CALL:
2391 num = 2; /* Another special case */
2392 read_write = I2C_SMBUS_READ;
2393 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
2394 dev_err(&adapter->dev,
2395 "Invalid block write size %d\n",
2399 msg[0].len = data->block[0] + 2;
2400 for (i = 1; i < msg[0].len; i++)
2401 msgbuf0[i] = data->block[i-1];
2402 msg[1].flags |= I2C_M_RECV_LEN;
2403 msg[1].len = 1; /* block length will be added by
2404 the underlying bus driver */
2406 case I2C_SMBUS_I2C_BLOCK_DATA:
2407 if (read_write == I2C_SMBUS_READ) {
2408 msg[1].len = data->block[0];
2410 msg[0].len = data->block[0] + 1;
2411 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
2412 dev_err(&adapter->dev,
2413 "Invalid block write size %d\n",
2417 for (i = 1; i <= data->block[0]; i++)
2418 msgbuf0[i] = data->block[i];
2422 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2426 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2427 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2429 /* Compute PEC if first message is a write */
2430 if (!(msg[0].flags & I2C_M_RD)) {
2431 if (num == 1) /* Write only */
2432 i2c_smbus_add_pec(&msg[0]);
2433 else /* Write followed by read */
2434 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2436 /* Ask for PEC if last message is a read */
2437 if (msg[num-1].flags & I2C_M_RD)
2441 status = i2c_transfer(adapter, msg, num);
2445 /* Check PEC if last message is a read */
2446 if (i && (msg[num-1].flags & I2C_M_RD)) {
2447 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2452 if (read_write == I2C_SMBUS_READ)
2454 case I2C_SMBUS_BYTE:
2455 data->byte = msgbuf0[0];
2457 case I2C_SMBUS_BYTE_DATA:
2458 data->byte = msgbuf1[0];
2460 case I2C_SMBUS_WORD_DATA:
2461 case I2C_SMBUS_PROC_CALL:
2462 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2464 case I2C_SMBUS_I2C_BLOCK_DATA:
2465 for (i = 0; i < data->block[0]; i++)
2466 data->block[i+1] = msgbuf1[i];
2468 case I2C_SMBUS_BLOCK_DATA:
2469 case I2C_SMBUS_BLOCK_PROC_CALL:
2470 for (i = 0; i < msgbuf1[0] + 1; i++)
2471 data->block[i] = msgbuf1[i];
2478 * i2c_smbus_xfer - execute SMBus protocol operations
2479 * @adapter: Handle to I2C bus
2480 * @addr: Address of SMBus slave on that bus
2481 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2482 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2483 * @command: Byte interpreted by slave, for protocols which use such bytes
2484 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2485 * @data: Data to be read or written
2487 * This executes an SMBus protocol operation, and returns a negative
2488 * errno code else zero on success.
2490 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2491 char read_write, u8 command, int protocol,
2492 union i2c_smbus_data *data)
2494 unsigned long orig_jiffies;
2498 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
2500 if (adapter->algo->smbus_xfer) {
2501 i2c_lock_adapter(adapter);
2503 /* Retry automatically on arbitration loss */
2504 orig_jiffies = jiffies;
2505 for (res = 0, try = 0; try <= adapter->retries; try++) {
2506 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2507 read_write, command,
2511 if (time_after(jiffies,
2512 orig_jiffies + adapter->timeout))
2515 i2c_unlock_adapter(adapter);
2517 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2520 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2521 * implement native support for the SMBus operation.
2525 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2526 command, protocol, data);
2528 EXPORT_SYMBOL(i2c_smbus_xfer);
2530 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2531 MODULE_DESCRIPTION("I2C-Bus main module");
2532 MODULE_LICENSE("GPL");