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 acpi_dev_pm_attach(&client->dev, true);
258 status = driver->probe(client, i2c_match_id(driver->id_table, client));
260 i2c_set_clientdata(client, NULL);
261 acpi_dev_pm_detach(&client->dev, true);
266 static int i2c_device_remove(struct device *dev)
268 struct i2c_client *client = i2c_verify_client(dev);
269 struct i2c_driver *driver;
272 if (!client || !dev->driver)
275 driver = to_i2c_driver(dev->driver);
276 if (driver->remove) {
277 dev_dbg(dev, "remove\n");
278 status = driver->remove(client);
284 i2c_set_clientdata(client, NULL);
285 acpi_dev_pm_detach(&client->dev, true);
289 static void i2c_device_shutdown(struct device *dev)
291 struct i2c_client *client = i2c_verify_client(dev);
292 struct i2c_driver *driver;
294 if (!client || !dev->driver)
296 driver = to_i2c_driver(dev->driver);
297 if (driver->shutdown)
298 driver->shutdown(client);
301 #ifdef CONFIG_PM_SLEEP
302 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
304 struct i2c_client *client = i2c_verify_client(dev);
305 struct i2c_driver *driver;
307 if (!client || !dev->driver)
309 driver = to_i2c_driver(dev->driver);
310 if (!driver->suspend)
312 return driver->suspend(client, mesg);
315 static int i2c_legacy_resume(struct device *dev)
317 struct i2c_client *client = i2c_verify_client(dev);
318 struct i2c_driver *driver;
320 if (!client || !dev->driver)
322 driver = to_i2c_driver(dev->driver);
325 return driver->resume(client);
328 static int i2c_device_pm_suspend(struct device *dev)
330 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
333 return pm_generic_suspend(dev);
335 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
338 static int i2c_device_pm_resume(struct device *dev)
340 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
343 return pm_generic_resume(dev);
345 return i2c_legacy_resume(dev);
348 static int i2c_device_pm_freeze(struct device *dev)
350 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
353 return pm_generic_freeze(dev);
355 return i2c_legacy_suspend(dev, PMSG_FREEZE);
358 static int i2c_device_pm_thaw(struct device *dev)
360 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
363 return pm_generic_thaw(dev);
365 return i2c_legacy_resume(dev);
368 static int i2c_device_pm_poweroff(struct device *dev)
370 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
373 return pm_generic_poweroff(dev);
375 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
378 static int i2c_device_pm_restore(struct device *dev)
380 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
383 return pm_generic_restore(dev);
385 return i2c_legacy_resume(dev);
387 #else /* !CONFIG_PM_SLEEP */
388 #define i2c_device_pm_suspend NULL
389 #define i2c_device_pm_resume NULL
390 #define i2c_device_pm_freeze NULL
391 #define i2c_device_pm_thaw NULL
392 #define i2c_device_pm_poweroff NULL
393 #define i2c_device_pm_restore NULL
394 #endif /* !CONFIG_PM_SLEEP */
396 static void i2c_client_dev_release(struct device *dev)
398 kfree(to_i2c_client(dev));
402 show_name(struct device *dev, struct device_attribute *attr, char *buf)
404 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
405 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
409 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
411 struct i2c_client *client = to_i2c_client(dev);
412 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
415 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
416 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
418 static struct attribute *i2c_dev_attrs[] = {
420 /* modalias helps coldplug: modprobe $(cat .../modalias) */
421 &dev_attr_modalias.attr,
425 static struct attribute_group i2c_dev_attr_group = {
426 .attrs = i2c_dev_attrs,
429 static const struct attribute_group *i2c_dev_attr_groups[] = {
434 static const struct dev_pm_ops i2c_device_pm_ops = {
435 .suspend = i2c_device_pm_suspend,
436 .resume = i2c_device_pm_resume,
437 .freeze = i2c_device_pm_freeze,
438 .thaw = i2c_device_pm_thaw,
439 .poweroff = i2c_device_pm_poweroff,
440 .restore = i2c_device_pm_restore,
442 pm_generic_runtime_suspend,
443 pm_generic_runtime_resume,
448 struct bus_type i2c_bus_type = {
450 .match = i2c_device_match,
451 .probe = i2c_device_probe,
452 .remove = i2c_device_remove,
453 .shutdown = i2c_device_shutdown,
454 .pm = &i2c_device_pm_ops,
456 EXPORT_SYMBOL_GPL(i2c_bus_type);
458 static struct device_type i2c_client_type = {
459 .groups = i2c_dev_attr_groups,
460 .uevent = i2c_device_uevent,
461 .release = i2c_client_dev_release,
466 * i2c_verify_client - return parameter as i2c_client, or NULL
467 * @dev: device, probably from some driver model iterator
469 * When traversing the driver model tree, perhaps using driver model
470 * iterators like @device_for_each_child(), you can't assume very much
471 * about the nodes you find. Use this function to avoid oopses caused
472 * by wrongly treating some non-I2C device as an i2c_client.
474 struct i2c_client *i2c_verify_client(struct device *dev)
476 return (dev->type == &i2c_client_type)
480 EXPORT_SYMBOL(i2c_verify_client);
483 /* This is a permissive address validity check, I2C address map constraints
484 * are purposely not enforced, except for the general call address. */
485 static int i2c_check_client_addr_validity(const struct i2c_client *client)
487 if (client->flags & I2C_CLIENT_TEN) {
488 /* 10-bit address, all values are valid */
489 if (client->addr > 0x3ff)
492 /* 7-bit address, reject the general call address */
493 if (client->addr == 0x00 || client->addr > 0x7f)
499 /* And this is a strict address validity check, used when probing. If a
500 * device uses a reserved address, then it shouldn't be probed. 7-bit
501 * addressing is assumed, 10-bit address devices are rare and should be
502 * explicitly enumerated. */
503 static int i2c_check_addr_validity(unsigned short addr)
506 * Reserved addresses per I2C specification:
507 * 0x00 General call address / START byte
509 * 0x02 Reserved for different bus format
510 * 0x03 Reserved for future purposes
511 * 0x04-0x07 Hs-mode master code
512 * 0x78-0x7b 10-bit slave addressing
513 * 0x7c-0x7f Reserved for future purposes
515 if (addr < 0x08 || addr > 0x77)
520 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
522 struct i2c_client *client = i2c_verify_client(dev);
523 int addr = *(int *)addrp;
525 if (client && client->addr == addr)
530 /* walk up mux tree */
531 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
533 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
536 result = device_for_each_child(&adapter->dev, &addr,
537 __i2c_check_addr_busy);
539 if (!result && parent)
540 result = i2c_check_mux_parents(parent, addr);
545 /* recurse down mux tree */
546 static int i2c_check_mux_children(struct device *dev, void *addrp)
550 if (dev->type == &i2c_adapter_type)
551 result = device_for_each_child(dev, addrp,
552 i2c_check_mux_children);
554 result = __i2c_check_addr_busy(dev, addrp);
559 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
561 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
565 result = i2c_check_mux_parents(parent, addr);
568 result = device_for_each_child(&adapter->dev, &addr,
569 i2c_check_mux_children);
575 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
576 * @adapter: Target I2C bus segment
578 void i2c_lock_adapter(struct i2c_adapter *adapter)
580 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
583 i2c_lock_adapter(parent);
585 rt_mutex_lock(&adapter->bus_lock);
587 EXPORT_SYMBOL_GPL(i2c_lock_adapter);
590 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
591 * @adapter: Target I2C bus segment
593 static int i2c_trylock_adapter(struct i2c_adapter *adapter)
595 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
598 return i2c_trylock_adapter(parent);
600 return rt_mutex_trylock(&adapter->bus_lock);
604 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
605 * @adapter: Target I2C bus segment
607 void i2c_unlock_adapter(struct i2c_adapter *adapter)
609 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
612 i2c_unlock_adapter(parent);
614 rt_mutex_unlock(&adapter->bus_lock);
616 EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
619 * i2c_new_device - instantiate an i2c device
620 * @adap: the adapter managing the device
621 * @info: describes one I2C device; bus_num is ignored
624 * Create an i2c device. Binding is handled through driver model
625 * probe()/remove() methods. A driver may be bound to this device when we
626 * return from this function, or any later moment (e.g. maybe hotplugging will
627 * load the driver module). This call is not appropriate for use by mainboard
628 * initialization logic, which usually runs during an arch_initcall() long
629 * before any i2c_adapter could exist.
631 * This returns the new i2c client, which may be saved for later use with
632 * i2c_unregister_device(); or NULL to indicate an error.
635 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
637 struct i2c_client *client;
640 client = kzalloc(sizeof *client, GFP_KERNEL);
644 client->adapter = adap;
646 client->dev.platform_data = info->platform_data;
649 client->dev.archdata = *info->archdata;
651 client->flags = info->flags;
652 client->addr = info->addr;
653 client->irq = info->irq;
655 strlcpy(client->name, info->type, sizeof(client->name));
657 /* Check for address validity */
658 status = i2c_check_client_addr_validity(client);
660 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
661 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
665 /* Check for address business */
666 status = i2c_check_addr_busy(adap, client->addr);
670 client->dev.parent = &client->adapter->dev;
671 client->dev.bus = &i2c_bus_type;
672 client->dev.type = &i2c_client_type;
673 client->dev.of_node = info->of_node;
674 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
676 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
677 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
678 client->addr | ((client->flags & I2C_CLIENT_TEN)
680 status = device_register(&client->dev);
684 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
685 client->name, dev_name(&client->dev));
690 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
691 "(%d)\n", client->name, client->addr, status);
696 EXPORT_SYMBOL_GPL(i2c_new_device);
700 * i2c_unregister_device - reverse effect of i2c_new_device()
701 * @client: value returned from i2c_new_device()
704 void i2c_unregister_device(struct i2c_client *client)
706 device_unregister(&client->dev);
708 EXPORT_SYMBOL_GPL(i2c_unregister_device);
711 static const struct i2c_device_id dummy_id[] = {
716 static int dummy_probe(struct i2c_client *client,
717 const struct i2c_device_id *id)
722 static int dummy_remove(struct i2c_client *client)
727 static struct i2c_driver dummy_driver = {
728 .driver.name = "dummy",
729 .probe = dummy_probe,
730 .remove = dummy_remove,
731 .id_table = dummy_id,
735 * i2c_new_dummy - return a new i2c device bound to a dummy driver
736 * @adapter: the adapter managing the device
737 * @address: seven bit address to be used
740 * This returns an I2C client bound to the "dummy" driver, intended for use
741 * with devices that consume multiple addresses. Examples of such chips
742 * include various EEPROMS (like 24c04 and 24c08 models).
744 * These dummy devices have two main uses. First, most I2C and SMBus calls
745 * except i2c_transfer() need a client handle; the dummy will be that handle.
746 * And second, this prevents the specified address from being bound to a
749 * This returns the new i2c client, which should be saved for later use with
750 * i2c_unregister_device(); or NULL to indicate an error.
752 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
754 struct i2c_board_info info = {
755 I2C_BOARD_INFO("dummy", address),
758 return i2c_new_device(adapter, &info);
760 EXPORT_SYMBOL_GPL(i2c_new_dummy);
762 /* ------------------------------------------------------------------------- */
764 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
766 static void i2c_adapter_dev_release(struct device *dev)
768 struct i2c_adapter *adap = to_i2c_adapter(dev);
769 complete(&adap->dev_released);
773 * This function is only needed for mutex_lock_nested, so it is never
774 * called unless locking correctness checking is enabled. Thus we
775 * make it inline to avoid a compiler warning. That's what gcc ends up
778 static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
780 unsigned int depth = 0;
782 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
789 * Let users instantiate I2C devices through sysfs. This can be used when
790 * platform initialization code doesn't contain the proper data for
791 * whatever reason. Also useful for drivers that do device detection and
792 * detection fails, either because the device uses an unexpected address,
793 * or this is a compatible device with different ID register values.
795 * Parameter checking may look overzealous, but we really don't want
796 * the user to provide incorrect parameters.
799 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
800 const char *buf, size_t count)
802 struct i2c_adapter *adap = to_i2c_adapter(dev);
803 struct i2c_board_info info;
804 struct i2c_client *client;
808 memset(&info, 0, sizeof(struct i2c_board_info));
810 blank = strchr(buf, ' ');
812 dev_err(dev, "%s: Missing parameters\n", "new_device");
815 if (blank - buf > I2C_NAME_SIZE - 1) {
816 dev_err(dev, "%s: Invalid device name\n", "new_device");
819 memcpy(info.type, buf, blank - buf);
821 /* Parse remaining parameters, reject extra parameters */
822 res = sscanf(++blank, "%hi%c", &info.addr, &end);
824 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
827 if (res > 1 && end != '\n') {
828 dev_err(dev, "%s: Extra parameters\n", "new_device");
832 client = i2c_new_device(adap, &info);
836 /* Keep track of the added device */
837 mutex_lock(&adap->userspace_clients_lock);
838 list_add_tail(&client->detected, &adap->userspace_clients);
839 mutex_unlock(&adap->userspace_clients_lock);
840 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
841 info.type, info.addr);
847 * And of course let the users delete the devices they instantiated, if
848 * they got it wrong. This interface can only be used to delete devices
849 * instantiated by i2c_sysfs_new_device above. This guarantees that we
850 * don't delete devices to which some kernel code still has references.
852 * Parameter checking may look overzealous, but we really don't want
853 * the user to delete the wrong device.
856 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
857 const char *buf, size_t count)
859 struct i2c_adapter *adap = to_i2c_adapter(dev);
860 struct i2c_client *client, *next;
865 /* Parse parameters, reject extra parameters */
866 res = sscanf(buf, "%hi%c", &addr, &end);
868 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
871 if (res > 1 && end != '\n') {
872 dev_err(dev, "%s: Extra parameters\n", "delete_device");
876 /* Make sure the device was added through sysfs */
878 mutex_lock_nested(&adap->userspace_clients_lock,
879 i2c_adapter_depth(adap));
880 list_for_each_entry_safe(client, next, &adap->userspace_clients,
882 if (client->addr == addr) {
883 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
884 "delete_device", client->name, client->addr);
886 list_del(&client->detected);
887 i2c_unregister_device(client);
892 mutex_unlock(&adap->userspace_clients_lock);
895 dev_err(dev, "%s: Can't find device in list\n",
900 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
901 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
902 i2c_sysfs_delete_device);
904 static struct attribute *i2c_adapter_attrs[] = {
906 &dev_attr_new_device.attr,
907 &dev_attr_delete_device.attr,
911 static struct attribute_group i2c_adapter_attr_group = {
912 .attrs = i2c_adapter_attrs,
915 static const struct attribute_group *i2c_adapter_attr_groups[] = {
916 &i2c_adapter_attr_group,
920 struct device_type i2c_adapter_type = {
921 .groups = i2c_adapter_attr_groups,
922 .release = i2c_adapter_dev_release,
924 EXPORT_SYMBOL_GPL(i2c_adapter_type);
927 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
928 * @dev: device, probably from some driver model iterator
930 * When traversing the driver model tree, perhaps using driver model
931 * iterators like @device_for_each_child(), you can't assume very much
932 * about the nodes you find. Use this function to avoid oopses caused
933 * by wrongly treating some non-I2C device as an i2c_adapter.
935 struct i2c_adapter *i2c_verify_adapter(struct device *dev)
937 return (dev->type == &i2c_adapter_type)
938 ? to_i2c_adapter(dev)
941 EXPORT_SYMBOL(i2c_verify_adapter);
943 #ifdef CONFIG_I2C_COMPAT
944 static struct class_compat *i2c_adapter_compat_class;
947 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
949 struct i2c_devinfo *devinfo;
951 down_read(&__i2c_board_lock);
952 list_for_each_entry(devinfo, &__i2c_board_list, list) {
953 if (devinfo->busnum == adapter->nr
954 && !i2c_new_device(adapter,
955 &devinfo->board_info))
956 dev_err(&adapter->dev,
957 "Can't create device at 0x%02x\n",
958 devinfo->board_info.addr);
960 up_read(&__i2c_board_lock);
963 /* OF support code */
965 #if IS_ENABLED(CONFIG_OF)
966 static void of_i2c_register_devices(struct i2c_adapter *adap)
969 struct device_node *node;
971 /* Only register child devices if the adapter has a node pointer set */
972 if (!adap->dev.of_node)
975 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
977 for_each_available_child_of_node(adap->dev.of_node, node) {
978 struct i2c_board_info info = {};
979 struct dev_archdata dev_ad = {};
983 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
985 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
986 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
991 addr = of_get_property(node, "reg", &len);
992 if (!addr || (len < sizeof(int))) {
993 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
998 info.addr = be32_to_cpup(addr);
999 if (info.addr > (1 << 10) - 1) {
1000 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1001 info.addr, node->full_name);
1005 info.irq = irq_of_parse_and_map(node, 0);
1006 info.of_node = of_node_get(node);
1007 info.archdata = &dev_ad;
1009 if (of_get_property(node, "wakeup-source", NULL))
1010 info.flags |= I2C_CLIENT_WAKE;
1012 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1014 result = i2c_new_device(adap, &info);
1015 if (result == NULL) {
1016 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1019 irq_dispose_mapping(info.irq);
1025 static int of_dev_node_match(struct device *dev, void *data)
1027 return dev->of_node == data;
1030 /* must call put_device() when done with returned i2c_client device */
1031 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1035 dev = bus_find_device(&i2c_bus_type, NULL, node,
1040 return i2c_verify_client(dev);
1042 EXPORT_SYMBOL(of_find_i2c_device_by_node);
1044 /* must call put_device() when done with returned i2c_adapter device */
1045 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1049 dev = bus_find_device(&i2c_bus_type, NULL, node,
1054 return i2c_verify_adapter(dev);
1056 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1058 static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1059 #endif /* CONFIG_OF */
1061 /* ACPI support code */
1063 #if IS_ENABLED(CONFIG_ACPI)
1064 static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1066 struct i2c_board_info *info = data;
1068 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1069 struct acpi_resource_i2c_serialbus *sb;
1071 sb = &ares->data.i2c_serial_bus;
1072 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1073 info->addr = sb->slave_address;
1074 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1075 info->flags |= I2C_CLIENT_TEN;
1077 } else if (info->irq < 0) {
1080 if (acpi_dev_resource_interrupt(ares, 0, &r))
1081 info->irq = r.start;
1084 /* Tell the ACPI core to skip this resource */
1088 static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1089 void *data, void **return_value)
1091 struct i2c_adapter *adapter = data;
1092 struct list_head resource_list;
1093 struct i2c_board_info info;
1094 struct acpi_device *adev;
1097 if (acpi_bus_get_device(handle, &adev))
1099 if (acpi_bus_get_status(adev) || !adev->status.present)
1102 memset(&info, 0, sizeof(info));
1103 info.acpi_node.handle = handle;
1106 INIT_LIST_HEAD(&resource_list);
1107 ret = acpi_dev_get_resources(adev, &resource_list,
1108 acpi_i2c_add_resource, &info);
1109 acpi_dev_free_resource_list(&resource_list);
1111 if (ret < 0 || !info.addr)
1114 adev->power.flags.ignore_parent = true;
1115 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1116 if (!i2c_new_device(adapter, &info)) {
1117 adev->power.flags.ignore_parent = false;
1118 dev_err(&adapter->dev,
1119 "failed to add I2C device %s from ACPI\n",
1120 dev_name(&adev->dev));
1127 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1128 * @adap: pointer to adapter
1130 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1131 * namespace. When a device is found it will be added to the Linux device
1132 * model and bound to the corresponding ACPI handle.
1134 static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1139 if (!adap->dev.parent)
1142 handle = ACPI_HANDLE(adap->dev.parent);
1146 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1147 acpi_i2c_add_device, NULL,
1149 if (ACPI_FAILURE(status))
1150 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1153 static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1154 #endif /* CONFIG_ACPI */
1156 static int i2c_do_add_adapter(struct i2c_driver *driver,
1157 struct i2c_adapter *adap)
1159 /* Detect supported devices on that bus, and instantiate them */
1160 i2c_detect(adap, driver);
1162 /* Let legacy drivers scan this bus for matching devices */
1163 if (driver->attach_adapter) {
1164 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1165 driver->driver.name);
1166 dev_warn(&adap->dev, "Please use another way to instantiate "
1167 "your i2c_client\n");
1168 /* We ignore the return code; if it fails, too bad */
1169 driver->attach_adapter(adap);
1174 static int __process_new_adapter(struct device_driver *d, void *data)
1176 return i2c_do_add_adapter(to_i2c_driver(d), data);
1179 static int i2c_register_adapter(struct i2c_adapter *adap)
1183 /* Can't register until after driver model init */
1184 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1190 if (unlikely(adap->name[0] == '\0')) {
1191 pr_err("i2c-core: Attempt to register an adapter with "
1195 if (unlikely(!adap->algo)) {
1196 pr_err("i2c-core: Attempt to register adapter '%s' with "
1197 "no algo!\n", adap->name);
1201 rt_mutex_init(&adap->bus_lock);
1202 mutex_init(&adap->userspace_clients_lock);
1203 INIT_LIST_HEAD(&adap->userspace_clients);
1205 /* Set default timeout to 1 second if not already set */
1206 if (adap->timeout == 0)
1209 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
1210 adap->dev.bus = &i2c_bus_type;
1211 adap->dev.type = &i2c_adapter_type;
1212 res = device_register(&adap->dev);
1216 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1218 #ifdef CONFIG_I2C_COMPAT
1219 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1222 dev_warn(&adap->dev,
1223 "Failed to create compatibility class link\n");
1226 /* bus recovery specific initialization */
1227 if (adap->bus_recovery_info) {
1228 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1230 if (!bri->recover_bus) {
1231 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1232 adap->bus_recovery_info = NULL;
1236 /* Generic GPIO recovery */
1237 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1238 if (!gpio_is_valid(bri->scl_gpio)) {
1239 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1240 adap->bus_recovery_info = NULL;
1244 if (gpio_is_valid(bri->sda_gpio))
1245 bri->get_sda = get_sda_gpio_value;
1247 bri->get_sda = NULL;
1249 bri->get_scl = get_scl_gpio_value;
1250 bri->set_scl = set_scl_gpio_value;
1251 } else if (!bri->set_scl || !bri->get_scl) {
1252 /* Generic SCL recovery */
1253 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1254 adap->bus_recovery_info = NULL;
1259 /* create pre-declared device nodes */
1260 of_i2c_register_devices(adap);
1261 acpi_i2c_register_devices(adap);
1263 if (adap->nr < __i2c_first_dynamic_bus_num)
1264 i2c_scan_static_board_info(adap);
1266 /* Notify drivers */
1267 mutex_lock(&core_lock);
1268 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1269 mutex_unlock(&core_lock);
1274 mutex_lock(&core_lock);
1275 idr_remove(&i2c_adapter_idr, adap->nr);
1276 mutex_unlock(&core_lock);
1281 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1282 * @adap: the adapter to register (with adap->nr initialized)
1283 * Context: can sleep
1285 * See i2c_add_numbered_adapter() for details.
1287 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1291 mutex_lock(&core_lock);
1292 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1294 mutex_unlock(&core_lock);
1296 return id == -ENOSPC ? -EBUSY : id;
1298 return i2c_register_adapter(adap);
1302 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1303 * @adapter: the adapter to add
1304 * Context: can sleep
1306 * This routine is used to declare an I2C adapter when its bus number
1307 * doesn't matter or when its bus number is specified by an dt alias.
1308 * Examples of bases when the bus number doesn't matter: I2C adapters
1309 * dynamically added by USB links or PCI plugin cards.
1311 * When this returns zero, a new bus number was allocated and stored
1312 * in adap->nr, and the specified adapter became available for clients.
1313 * Otherwise, a negative errno value is returned.
1315 int i2c_add_adapter(struct i2c_adapter *adapter)
1317 struct device *dev = &adapter->dev;
1321 id = of_alias_get_id(dev->of_node, "i2c");
1324 return __i2c_add_numbered_adapter(adapter);
1328 mutex_lock(&core_lock);
1329 id = idr_alloc(&i2c_adapter_idr, adapter,
1330 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
1331 mutex_unlock(&core_lock);
1337 return i2c_register_adapter(adapter);
1339 EXPORT_SYMBOL(i2c_add_adapter);
1342 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1343 * @adap: the adapter to register (with adap->nr initialized)
1344 * Context: can sleep
1346 * This routine is used to declare an I2C adapter when its bus number
1347 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1348 * or otherwise built in to the system's mainboard, and where i2c_board_info
1349 * is used to properly configure I2C devices.
1351 * If the requested bus number is set to -1, then this function will behave
1352 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1354 * If no devices have pre-been declared for this bus, then be sure to
1355 * register the adapter before any dynamically allocated ones. Otherwise
1356 * the required bus ID may not be available.
1358 * When this returns zero, the specified adapter became available for
1359 * clients using the bus number provided in adap->nr. Also, the table
1360 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1361 * and the appropriate driver model device nodes are created. Otherwise, a
1362 * negative errno value is returned.
1364 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1366 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1367 return i2c_add_adapter(adap);
1369 return __i2c_add_numbered_adapter(adap);
1371 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1373 static void i2c_do_del_adapter(struct i2c_driver *driver,
1374 struct i2c_adapter *adapter)
1376 struct i2c_client *client, *_n;
1378 /* Remove the devices we created ourselves as the result of hardware
1379 * probing (using a driver's detect method) */
1380 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1381 if (client->adapter == adapter) {
1382 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1383 client->name, client->addr);
1384 list_del(&client->detected);
1385 i2c_unregister_device(client);
1390 static int __unregister_client(struct device *dev, void *dummy)
1392 struct i2c_client *client = i2c_verify_client(dev);
1393 if (client && strcmp(client->name, "dummy"))
1394 i2c_unregister_device(client);
1398 static int __unregister_dummy(struct device *dev, void *dummy)
1400 struct i2c_client *client = i2c_verify_client(dev);
1402 i2c_unregister_device(client);
1406 static int __process_removed_adapter(struct device_driver *d, void *data)
1408 i2c_do_del_adapter(to_i2c_driver(d), data);
1413 * i2c_del_adapter - unregister I2C adapter
1414 * @adap: the adapter being unregistered
1415 * Context: can sleep
1417 * This unregisters an I2C adapter which was previously registered
1418 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1420 void i2c_del_adapter(struct i2c_adapter *adap)
1422 struct i2c_adapter *found;
1423 struct i2c_client *client, *next;
1425 /* First make sure that this adapter was ever added */
1426 mutex_lock(&core_lock);
1427 found = idr_find(&i2c_adapter_idr, adap->nr);
1428 mutex_unlock(&core_lock);
1429 if (found != adap) {
1430 pr_debug("i2c-core: attempting to delete unregistered "
1431 "adapter [%s]\n", adap->name);
1435 /* Tell drivers about this removal */
1436 mutex_lock(&core_lock);
1437 bus_for_each_drv(&i2c_bus_type, NULL, adap,
1438 __process_removed_adapter);
1439 mutex_unlock(&core_lock);
1441 /* Remove devices instantiated from sysfs */
1442 mutex_lock_nested(&adap->userspace_clients_lock,
1443 i2c_adapter_depth(adap));
1444 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1446 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1448 list_del(&client->detected);
1449 i2c_unregister_device(client);
1451 mutex_unlock(&adap->userspace_clients_lock);
1453 /* Detach any active clients. This can't fail, thus we do not
1454 * check the returned value. This is a two-pass process, because
1455 * we can't remove the dummy devices during the first pass: they
1456 * could have been instantiated by real devices wishing to clean
1457 * them up properly, so we give them a chance to do that first. */
1458 device_for_each_child(&adap->dev, NULL, __unregister_client);
1459 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1461 #ifdef CONFIG_I2C_COMPAT
1462 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1466 /* device name is gone after device_unregister */
1467 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1469 /* clean up the sysfs representation */
1470 init_completion(&adap->dev_released);
1471 device_unregister(&adap->dev);
1473 /* wait for sysfs to drop all references */
1474 wait_for_completion(&adap->dev_released);
1477 mutex_lock(&core_lock);
1478 idr_remove(&i2c_adapter_idr, adap->nr);
1479 mutex_unlock(&core_lock);
1481 /* Clear the device structure in case this adapter is ever going to be
1483 memset(&adap->dev, 0, sizeof(adap->dev));
1485 EXPORT_SYMBOL(i2c_del_adapter);
1487 /* ------------------------------------------------------------------------- */
1489 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1493 mutex_lock(&core_lock);
1494 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1495 mutex_unlock(&core_lock);
1499 EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1501 static int __process_new_driver(struct device *dev, void *data)
1503 if (dev->type != &i2c_adapter_type)
1505 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1509 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1510 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1513 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1517 /* Can't register until after driver model init */
1518 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1521 /* add the driver to the list of i2c drivers in the driver core */
1522 driver->driver.owner = owner;
1523 driver->driver.bus = &i2c_bus_type;
1525 /* When registration returns, the driver core
1526 * will have called probe() for all matching-but-unbound devices.
1528 res = driver_register(&driver->driver);
1532 /* Drivers should switch to dev_pm_ops instead. */
1533 if (driver->suspend)
1534 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1535 driver->driver.name);
1537 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1538 driver->driver.name);
1540 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1542 INIT_LIST_HEAD(&driver->clients);
1543 /* Walk the adapters that are already present */
1544 i2c_for_each_dev(driver, __process_new_driver);
1548 EXPORT_SYMBOL(i2c_register_driver);
1550 static int __process_removed_driver(struct device *dev, void *data)
1552 if (dev->type == &i2c_adapter_type)
1553 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1558 * i2c_del_driver - unregister I2C driver
1559 * @driver: the driver being unregistered
1560 * Context: can sleep
1562 void i2c_del_driver(struct i2c_driver *driver)
1564 i2c_for_each_dev(driver, __process_removed_driver);
1566 driver_unregister(&driver->driver);
1567 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1569 EXPORT_SYMBOL(i2c_del_driver);
1571 /* ------------------------------------------------------------------------- */
1574 * i2c_use_client - increments the reference count of the i2c client structure
1575 * @client: the client being referenced
1577 * Each live reference to a client should be refcounted. The driver model does
1578 * that automatically as part of driver binding, so that most drivers don't
1579 * need to do this explicitly: they hold a reference until they're unbound
1582 * A pointer to the client with the incremented reference counter is returned.
1584 struct i2c_client *i2c_use_client(struct i2c_client *client)
1586 if (client && get_device(&client->dev))
1590 EXPORT_SYMBOL(i2c_use_client);
1593 * i2c_release_client - release a use of the i2c client structure
1594 * @client: the client being no longer referenced
1596 * Must be called when a user of a client is finished with it.
1598 void i2c_release_client(struct i2c_client *client)
1601 put_device(&client->dev);
1603 EXPORT_SYMBOL(i2c_release_client);
1605 struct i2c_cmd_arg {
1610 static int i2c_cmd(struct device *dev, void *_arg)
1612 struct i2c_client *client = i2c_verify_client(dev);
1613 struct i2c_cmd_arg *arg = _arg;
1614 struct i2c_driver *driver;
1616 if (!client || !client->dev.driver)
1619 driver = to_i2c_driver(client->dev.driver);
1620 if (driver->command)
1621 driver->command(client, arg->cmd, arg->arg);
1625 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1627 struct i2c_cmd_arg cmd_arg;
1631 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1633 EXPORT_SYMBOL(i2c_clients_command);
1635 static int __init i2c_init(void)
1639 retval = bus_register(&i2c_bus_type);
1642 #ifdef CONFIG_I2C_COMPAT
1643 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1644 if (!i2c_adapter_compat_class) {
1649 retval = i2c_add_driver(&dummy_driver);
1655 #ifdef CONFIG_I2C_COMPAT
1656 class_compat_unregister(i2c_adapter_compat_class);
1659 bus_unregister(&i2c_bus_type);
1663 static void __exit i2c_exit(void)
1665 i2c_del_driver(&dummy_driver);
1666 #ifdef CONFIG_I2C_COMPAT
1667 class_compat_unregister(i2c_adapter_compat_class);
1669 bus_unregister(&i2c_bus_type);
1672 /* We must initialize early, because some subsystems register i2c drivers
1673 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1675 postcore_initcall(i2c_init);
1676 module_exit(i2c_exit);
1678 /* ----------------------------------------------------
1679 * the functional interface to the i2c busses.
1680 * ----------------------------------------------------
1684 * __i2c_transfer - unlocked flavor of i2c_transfer
1685 * @adap: Handle to I2C bus
1686 * @msgs: One or more messages to execute before STOP is issued to
1687 * terminate the operation; each message begins with a START.
1688 * @num: Number of messages to be executed.
1690 * Returns negative errno, else the number of messages executed.
1692 * Adapter lock must be held when calling this function. No debug logging
1693 * takes place. adap->algo->master_xfer existence isn't checked.
1695 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1697 unsigned long orig_jiffies;
1700 /* Retry automatically on arbitration loss */
1701 orig_jiffies = jiffies;
1702 for (ret = 0, try = 0; try <= adap->retries; try++) {
1703 ret = adap->algo->master_xfer(adap, msgs, num);
1706 if (time_after(jiffies, orig_jiffies + adap->timeout))
1712 EXPORT_SYMBOL(__i2c_transfer);
1715 * i2c_transfer - execute a single or combined I2C message
1716 * @adap: Handle to I2C bus
1717 * @msgs: One or more messages to execute before STOP is issued to
1718 * terminate the operation; each message begins with a START.
1719 * @num: Number of messages to be executed.
1721 * Returns negative errno, else the number of messages executed.
1723 * Note that there is no requirement that each message be sent to
1724 * the same slave address, although that is the most common model.
1726 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1730 /* REVISIT the fault reporting model here is weak:
1732 * - When we get an error after receiving N bytes from a slave,
1733 * there is no way to report "N".
1735 * - When we get a NAK after transmitting N bytes to a slave,
1736 * there is no way to report "N" ... or to let the master
1737 * continue executing the rest of this combined message, if
1738 * that's the appropriate response.
1740 * - When for example "num" is two and we successfully complete
1741 * the first message but get an error part way through the
1742 * second, it's unclear whether that should be reported as
1743 * one (discarding status on the second message) or errno
1744 * (discarding status on the first one).
1747 if (adap->algo->master_xfer) {
1749 for (ret = 0; ret < num; ret++) {
1750 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1751 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1752 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1753 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1757 if (in_atomic() || irqs_disabled()) {
1758 ret = i2c_trylock_adapter(adap);
1760 /* I2C activity is ongoing. */
1763 i2c_lock_adapter(adap);
1766 ret = __i2c_transfer(adap, msgs, num);
1767 i2c_unlock_adapter(adap);
1771 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1775 EXPORT_SYMBOL(i2c_transfer);
1778 * i2c_master_send - issue a single I2C message in master transmit mode
1779 * @client: Handle to slave device
1780 * @buf: Data that will be written to the slave
1781 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1783 * Returns negative errno, or else the number of bytes written.
1785 int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
1788 struct i2c_adapter *adap = client->adapter;
1791 msg.addr = client->addr;
1792 msg.flags = client->flags & I2C_M_TEN;
1794 msg.buf = (char *)buf;
1796 ret = i2c_transfer(adap, &msg, 1);
1799 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1800 * transmitted, else error code.
1802 return (ret == 1) ? count : ret;
1804 EXPORT_SYMBOL(i2c_master_send);
1807 * i2c_master_recv - issue a single I2C message in master receive mode
1808 * @client: Handle to slave device
1809 * @buf: Where to store data read from slave
1810 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1812 * Returns negative errno, or else the number of bytes read.
1814 int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
1816 struct i2c_adapter *adap = client->adapter;
1820 msg.addr = client->addr;
1821 msg.flags = client->flags & I2C_M_TEN;
1822 msg.flags |= I2C_M_RD;
1826 ret = i2c_transfer(adap, &msg, 1);
1829 * If everything went ok (i.e. 1 msg received), return #bytes received,
1832 return (ret == 1) ? count : ret;
1834 EXPORT_SYMBOL(i2c_master_recv);
1836 /* ----------------------------------------------------
1837 * the i2c address scanning function
1838 * Will not work for 10-bit addresses!
1839 * ----------------------------------------------------
1843 * Legacy default probe function, mostly relevant for SMBus. The default
1844 * probe method is a quick write, but it is known to corrupt the 24RF08
1845 * EEPROMs due to a state machine bug, and could also irreversibly
1846 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1847 * we use a short byte read instead. Also, some bus drivers don't implement
1848 * quick write, so we fallback to a byte read in that case too.
1849 * On x86, there is another special case for FSC hardware monitoring chips,
1850 * which want regular byte reads (address 0x73.) Fortunately, these are the
1851 * only known chips using this I2C address on PC hardware.
1852 * Returns 1 if probe succeeded, 0 if not.
1854 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1857 union i2c_smbus_data dummy;
1860 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1861 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1862 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1863 I2C_SMBUS_BYTE_DATA, &dummy);
1866 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1867 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1868 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1869 I2C_SMBUS_QUICK, NULL);
1870 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1871 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1872 I2C_SMBUS_BYTE, &dummy);
1874 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1882 static int i2c_detect_address(struct i2c_client *temp_client,
1883 struct i2c_driver *driver)
1885 struct i2c_board_info info;
1886 struct i2c_adapter *adapter = temp_client->adapter;
1887 int addr = temp_client->addr;
1890 /* Make sure the address is valid */
1891 err = i2c_check_addr_validity(addr);
1893 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1898 /* Skip if already in use */
1899 if (i2c_check_addr_busy(adapter, addr))
1902 /* Make sure there is something at this address */
1903 if (!i2c_default_probe(adapter, addr))
1906 /* Finally call the custom detection function */
1907 memset(&info, 0, sizeof(struct i2c_board_info));
1909 err = driver->detect(temp_client, &info);
1911 /* -ENODEV is returned if the detection fails. We catch it
1912 here as this isn't an error. */
1913 return err == -ENODEV ? 0 : err;
1916 /* Consistency check */
1917 if (info.type[0] == '\0') {
1918 dev_err(&adapter->dev, "%s detection function provided "
1919 "no name for 0x%x\n", driver->driver.name,
1922 struct i2c_client *client;
1924 /* Detection succeeded, instantiate the device */
1925 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1926 info.type, info.addr);
1927 client = i2c_new_device(adapter, &info);
1929 list_add_tail(&client->detected, &driver->clients);
1931 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1932 info.type, info.addr);
1937 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1939 const unsigned short *address_list;
1940 struct i2c_client *temp_client;
1942 int adap_id = i2c_adapter_id(adapter);
1944 address_list = driver->address_list;
1945 if (!driver->detect || !address_list)
1948 /* Stop here if the classes do not match */
1949 if (!(adapter->class & driver->class))
1952 /* Set up a temporary client to help detect callback */
1953 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1956 temp_client->adapter = adapter;
1958 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1959 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1960 "addr 0x%02x\n", adap_id, address_list[i]);
1961 temp_client->addr = address_list[i];
1962 err = i2c_detect_address(temp_client, driver);
1971 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1973 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1974 I2C_SMBUS_QUICK, NULL) >= 0;
1976 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1979 i2c_new_probed_device(struct i2c_adapter *adap,
1980 struct i2c_board_info *info,
1981 unsigned short const *addr_list,
1982 int (*probe)(struct i2c_adapter *, unsigned short addr))
1987 probe = i2c_default_probe;
1989 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1990 /* Check address validity */
1991 if (i2c_check_addr_validity(addr_list[i]) < 0) {
1992 dev_warn(&adap->dev, "Invalid 7-bit address "
1993 "0x%02x\n", addr_list[i]);
1997 /* Check address availability */
1998 if (i2c_check_addr_busy(adap, addr_list[i])) {
1999 dev_dbg(&adap->dev, "Address 0x%02x already in "
2000 "use, not probing\n", addr_list[i]);
2004 /* Test address responsiveness */
2005 if (probe(adap, addr_list[i]))
2009 if (addr_list[i] == I2C_CLIENT_END) {
2010 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2014 info->addr = addr_list[i];
2015 return i2c_new_device(adap, info);
2017 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2019 struct i2c_adapter *i2c_get_adapter(int nr)
2021 struct i2c_adapter *adapter;
2023 mutex_lock(&core_lock);
2024 adapter = idr_find(&i2c_adapter_idr, nr);
2025 if (adapter && !try_module_get(adapter->owner))
2028 mutex_unlock(&core_lock);
2031 EXPORT_SYMBOL(i2c_get_adapter);
2033 void i2c_put_adapter(struct i2c_adapter *adap)
2036 module_put(adap->owner);
2038 EXPORT_SYMBOL(i2c_put_adapter);
2040 /* The SMBus parts */
2042 #define POLY (0x1070U << 3)
2043 static u8 crc8(u16 data)
2047 for (i = 0; i < 8; i++) {
2052 return (u8)(data >> 8);
2055 /* Incremental CRC8 over count bytes in the array pointed to by p */
2056 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
2060 for (i = 0; i < count; i++)
2061 crc = crc8((crc ^ p[i]) << 8);
2065 /* Assume a 7-bit address, which is reasonable for SMBus */
2066 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
2068 /* The address will be sent first */
2069 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2070 pec = i2c_smbus_pec(pec, &addr, 1);
2072 /* The data buffer follows */
2073 return i2c_smbus_pec(pec, msg->buf, msg->len);
2076 /* Used for write only transactions */
2077 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
2079 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2083 /* Return <0 on CRC error
2084 If there was a write before this read (most cases) we need to take the
2085 partial CRC from the write part into account.
2086 Note that this function does modify the message (we need to decrease the
2087 message length to hide the CRC byte from the caller). */
2088 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
2090 u8 rpec = msg->buf[--msg->len];
2091 cpec = i2c_smbus_msg_pec(cpec, msg);
2094 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2102 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2103 * @client: Handle to slave device
2105 * This executes the SMBus "receive byte" protocol, returning negative errno
2106 * else the byte received from the device.
2108 s32 i2c_smbus_read_byte(const struct i2c_client *client)
2110 union i2c_smbus_data data;
2113 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2115 I2C_SMBUS_BYTE, &data);
2116 return (status < 0) ? status : data.byte;
2118 EXPORT_SYMBOL(i2c_smbus_read_byte);
2121 * i2c_smbus_write_byte - SMBus "send byte" protocol
2122 * @client: Handle to slave device
2123 * @value: Byte to be sent
2125 * This executes the SMBus "send byte" protocol, returning negative errno
2126 * else zero on success.
2128 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
2130 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2131 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
2133 EXPORT_SYMBOL(i2c_smbus_write_byte);
2136 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2137 * @client: Handle to slave device
2138 * @command: Byte interpreted by slave
2140 * This executes the SMBus "read byte" protocol, returning negative errno
2141 * else a data byte received from the device.
2143 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
2145 union i2c_smbus_data data;
2148 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2149 I2C_SMBUS_READ, command,
2150 I2C_SMBUS_BYTE_DATA, &data);
2151 return (status < 0) ? status : data.byte;
2153 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
2156 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2157 * @client: Handle to slave device
2158 * @command: Byte interpreted by slave
2159 * @value: Byte being written
2161 * This executes the SMBus "write byte" protocol, returning negative errno
2162 * else zero on success.
2164 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2167 union i2c_smbus_data data;
2169 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2170 I2C_SMBUS_WRITE, command,
2171 I2C_SMBUS_BYTE_DATA, &data);
2173 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
2176 * i2c_smbus_read_word_data - SMBus "read word" protocol
2177 * @client: Handle to slave device
2178 * @command: Byte interpreted by slave
2180 * This executes the SMBus "read word" protocol, returning negative errno
2181 * else a 16-bit unsigned "word" received from the device.
2183 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
2185 union i2c_smbus_data data;
2188 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2189 I2C_SMBUS_READ, command,
2190 I2C_SMBUS_WORD_DATA, &data);
2191 return (status < 0) ? status : data.word;
2193 EXPORT_SYMBOL(i2c_smbus_read_word_data);
2196 * i2c_smbus_write_word_data - SMBus "write word" protocol
2197 * @client: Handle to slave device
2198 * @command: Byte interpreted by slave
2199 * @value: 16-bit "word" being written
2201 * This executes the SMBus "write word" protocol, returning negative errno
2202 * else zero on success.
2204 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2207 union i2c_smbus_data data;
2209 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2210 I2C_SMBUS_WRITE, command,
2211 I2C_SMBUS_WORD_DATA, &data);
2213 EXPORT_SYMBOL(i2c_smbus_write_word_data);
2216 * i2c_smbus_read_block_data - SMBus "block read" protocol
2217 * @client: Handle to slave device
2218 * @command: Byte interpreted by slave
2219 * @values: Byte array into which data will be read; big enough to hold
2220 * the data returned by the slave. SMBus allows at most 32 bytes.
2222 * This executes the SMBus "block read" protocol, returning negative errno
2223 * else the number of data bytes in the slave's response.
2225 * Note that using this function requires that the client's adapter support
2226 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2227 * support this; its emulation through I2C messaging relies on a specific
2228 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2230 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
2233 union i2c_smbus_data data;
2236 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2237 I2C_SMBUS_READ, command,
2238 I2C_SMBUS_BLOCK_DATA, &data);
2242 memcpy(values, &data.block[1], data.block[0]);
2243 return data.block[0];
2245 EXPORT_SYMBOL(i2c_smbus_read_block_data);
2248 * i2c_smbus_write_block_data - SMBus "block write" protocol
2249 * @client: Handle to slave device
2250 * @command: Byte interpreted by slave
2251 * @length: Size of data block; SMBus allows at most 32 bytes
2252 * @values: Byte array which will be written.
2254 * This executes the SMBus "block write" protocol, returning negative errno
2255 * else zero on success.
2257 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
2258 u8 length, const u8 *values)
2260 union i2c_smbus_data data;
2262 if (length > I2C_SMBUS_BLOCK_MAX)
2263 length = I2C_SMBUS_BLOCK_MAX;
2264 data.block[0] = length;
2265 memcpy(&data.block[1], values, length);
2266 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2267 I2C_SMBUS_WRITE, command,
2268 I2C_SMBUS_BLOCK_DATA, &data);
2270 EXPORT_SYMBOL(i2c_smbus_write_block_data);
2272 /* Returns the number of read bytes */
2273 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
2274 u8 length, u8 *values)
2276 union i2c_smbus_data data;
2279 if (length > I2C_SMBUS_BLOCK_MAX)
2280 length = I2C_SMBUS_BLOCK_MAX;
2281 data.block[0] = length;
2282 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2283 I2C_SMBUS_READ, command,
2284 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2288 memcpy(values, &data.block[1], data.block[0]);
2289 return data.block[0];
2291 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
2293 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
2294 u8 length, const u8 *values)
2296 union i2c_smbus_data data;
2298 if (length > I2C_SMBUS_BLOCK_MAX)
2299 length = I2C_SMBUS_BLOCK_MAX;
2300 data.block[0] = length;
2301 memcpy(data.block + 1, values, length);
2302 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2303 I2C_SMBUS_WRITE, command,
2304 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2306 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
2308 /* Simulate a SMBus command using the i2c protocol
2309 No checking of parameters is done! */
2310 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2311 unsigned short flags,
2312 char read_write, u8 command, int size,
2313 union i2c_smbus_data *data)
2315 /* So we need to generate a series of msgs. In the case of writing, we
2316 need to use only one message; when reading, we need two. We initialize
2317 most things with sane defaults, to keep the code below somewhat
2319 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2320 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
2321 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
2325 struct i2c_msg msg[2] = {
2333 .flags = flags | I2C_M_RD,
2339 msgbuf0[0] = command;
2341 case I2C_SMBUS_QUICK:
2343 /* Special case: The read/write field is used as data */
2344 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2348 case I2C_SMBUS_BYTE:
2349 if (read_write == I2C_SMBUS_READ) {
2350 /* Special case: only a read! */
2351 msg[0].flags = I2C_M_RD | flags;
2355 case I2C_SMBUS_BYTE_DATA:
2356 if (read_write == I2C_SMBUS_READ)
2360 msgbuf0[1] = data->byte;
2363 case I2C_SMBUS_WORD_DATA:
2364 if (read_write == I2C_SMBUS_READ)
2368 msgbuf0[1] = data->word & 0xff;
2369 msgbuf0[2] = data->word >> 8;
2372 case I2C_SMBUS_PROC_CALL:
2373 num = 2; /* Special case */
2374 read_write = I2C_SMBUS_READ;
2377 msgbuf0[1] = data->word & 0xff;
2378 msgbuf0[2] = data->word >> 8;
2380 case I2C_SMBUS_BLOCK_DATA:
2381 if (read_write == I2C_SMBUS_READ) {
2382 msg[1].flags |= I2C_M_RECV_LEN;
2383 msg[1].len = 1; /* block length will be added by
2384 the underlying bus driver */
2386 msg[0].len = data->block[0] + 2;
2387 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
2388 dev_err(&adapter->dev,
2389 "Invalid block write size %d\n",
2393 for (i = 1; i < msg[0].len; i++)
2394 msgbuf0[i] = data->block[i-1];
2397 case I2C_SMBUS_BLOCK_PROC_CALL:
2398 num = 2; /* Another special case */
2399 read_write = I2C_SMBUS_READ;
2400 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
2401 dev_err(&adapter->dev,
2402 "Invalid block write size %d\n",
2406 msg[0].len = data->block[0] + 2;
2407 for (i = 1; i < msg[0].len; i++)
2408 msgbuf0[i] = data->block[i-1];
2409 msg[1].flags |= I2C_M_RECV_LEN;
2410 msg[1].len = 1; /* block length will be added by
2411 the underlying bus driver */
2413 case I2C_SMBUS_I2C_BLOCK_DATA:
2414 if (read_write == I2C_SMBUS_READ) {
2415 msg[1].len = data->block[0];
2417 msg[0].len = data->block[0] + 1;
2418 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
2419 dev_err(&adapter->dev,
2420 "Invalid block write size %d\n",
2424 for (i = 1; i <= data->block[0]; i++)
2425 msgbuf0[i] = data->block[i];
2429 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2433 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2434 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2436 /* Compute PEC if first message is a write */
2437 if (!(msg[0].flags & I2C_M_RD)) {
2438 if (num == 1) /* Write only */
2439 i2c_smbus_add_pec(&msg[0]);
2440 else /* Write followed by read */
2441 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2443 /* Ask for PEC if last message is a read */
2444 if (msg[num-1].flags & I2C_M_RD)
2448 status = i2c_transfer(adapter, msg, num);
2452 /* Check PEC if last message is a read */
2453 if (i && (msg[num-1].flags & I2C_M_RD)) {
2454 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2459 if (read_write == I2C_SMBUS_READ)
2461 case I2C_SMBUS_BYTE:
2462 data->byte = msgbuf0[0];
2464 case I2C_SMBUS_BYTE_DATA:
2465 data->byte = msgbuf1[0];
2467 case I2C_SMBUS_WORD_DATA:
2468 case I2C_SMBUS_PROC_CALL:
2469 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2471 case I2C_SMBUS_I2C_BLOCK_DATA:
2472 for (i = 0; i < data->block[0]; i++)
2473 data->block[i+1] = msgbuf1[i];
2475 case I2C_SMBUS_BLOCK_DATA:
2476 case I2C_SMBUS_BLOCK_PROC_CALL:
2477 for (i = 0; i < msgbuf1[0] + 1; i++)
2478 data->block[i] = msgbuf1[i];
2485 * i2c_smbus_xfer - execute SMBus protocol operations
2486 * @adapter: Handle to I2C bus
2487 * @addr: Address of SMBus slave on that bus
2488 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2489 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2490 * @command: Byte interpreted by slave, for protocols which use such bytes
2491 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2492 * @data: Data to be read or written
2494 * This executes an SMBus protocol operation, and returns a negative
2495 * errno code else zero on success.
2497 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2498 char read_write, u8 command, int protocol,
2499 union i2c_smbus_data *data)
2501 unsigned long orig_jiffies;
2505 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
2507 if (adapter->algo->smbus_xfer) {
2508 i2c_lock_adapter(adapter);
2510 /* Retry automatically on arbitration loss */
2511 orig_jiffies = jiffies;
2512 for (res = 0, try = 0; try <= adapter->retries; try++) {
2513 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2514 read_write, command,
2518 if (time_after(jiffies,
2519 orig_jiffies + adapter->timeout))
2522 i2c_unlock_adapter(adapter);
2524 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2527 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2528 * implement native support for the SMBus operation.
2532 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2533 command, protocol, data);
2535 EXPORT_SYMBOL(i2c_smbus_xfer);
2537 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2538 MODULE_DESCRIPTION("I2C-Bus main module");
2539 MODULE_LICENSE("GPL");