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);
109 rc = acpi_device_uevent_modalias(dev, env);
113 if (add_uevent_var(env, "MODALIAS=%s%s",
114 I2C_MODULE_PREFIX, client->name))
116 dev_dbg(dev, "uevent\n");
120 /* i2c bus recovery routines */
121 static int get_scl_gpio_value(struct i2c_adapter *adap)
123 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
126 static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
128 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
131 static int get_sda_gpio_value(struct i2c_adapter *adap)
133 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
136 static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
138 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
139 struct device *dev = &adap->dev;
142 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
143 GPIOF_OUT_INIT_HIGH, "i2c-scl");
145 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
150 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
151 /* work without SDA polling */
152 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
161 static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
163 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
166 gpio_free(bri->sda_gpio);
168 gpio_free(bri->scl_gpio);
172 * We are generating clock pulses. ndelay() determines durating of clk pulses.
173 * We will generate clock with rate 100 KHz and so duration of both clock levels
174 * is: delay in ns = (10^6 / 100) / 2
176 #define RECOVERY_NDELAY 5000
177 #define RECOVERY_CLK_CNT 9
179 static int i2c_generic_recovery(struct i2c_adapter *adap)
181 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
182 int i = 0, val = 1, ret = 0;
184 if (bri->prepare_recovery)
185 bri->prepare_recovery(bri);
188 * By this time SCL is high, as we need to give 9 falling-rising edges
190 while (i++ < RECOVERY_CLK_CNT * 2) {
192 /* Break if SDA is high */
193 if (bri->get_sda && bri->get_sda(adap))
195 /* SCL shouldn't be low here */
196 if (!bri->get_scl(adap)) {
198 "SCL is stuck low, exit recovery\n");
205 bri->set_scl(adap, val);
206 ndelay(RECOVERY_NDELAY);
209 if (bri->unprepare_recovery)
210 bri->unprepare_recovery(bri);
215 int i2c_generic_scl_recovery(struct i2c_adapter *adap)
217 adap->bus_recovery_info->set_scl(adap, 1);
218 return i2c_generic_recovery(adap);
221 int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
225 ret = i2c_get_gpios_for_recovery(adap);
229 ret = i2c_generic_recovery(adap);
230 i2c_put_gpios_for_recovery(adap);
235 int i2c_recover_bus(struct i2c_adapter *adap)
237 if (!adap->bus_recovery_info)
240 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
241 return adap->bus_recovery_info->recover_bus(adap);
244 static int i2c_device_probe(struct device *dev)
246 struct i2c_client *client = i2c_verify_client(dev);
247 struct i2c_driver *driver;
253 driver = to_i2c_driver(dev->driver);
254 if (!driver->probe || !driver->id_table)
257 if (!device_can_wakeup(&client->dev))
258 device_init_wakeup(&client->dev,
259 client->flags & I2C_CLIENT_WAKE);
260 dev_dbg(dev, "probe\n");
262 acpi_dev_pm_attach(&client->dev, true);
263 status = driver->probe(client, i2c_match_id(driver->id_table, client));
265 i2c_set_clientdata(client, NULL);
266 acpi_dev_pm_detach(&client->dev, true);
271 static int i2c_device_remove(struct device *dev)
273 struct i2c_client *client = i2c_verify_client(dev);
274 struct i2c_driver *driver;
277 if (!client || !dev->driver)
280 driver = to_i2c_driver(dev->driver);
281 if (driver->remove) {
282 dev_dbg(dev, "remove\n");
283 status = driver->remove(client);
289 i2c_set_clientdata(client, NULL);
290 acpi_dev_pm_detach(&client->dev, true);
294 static void i2c_device_shutdown(struct device *dev)
296 struct i2c_client *client = i2c_verify_client(dev);
297 struct i2c_driver *driver;
299 if (!client || !dev->driver)
301 driver = to_i2c_driver(dev->driver);
302 if (driver->shutdown)
303 driver->shutdown(client);
306 #ifdef CONFIG_PM_SLEEP
307 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
309 struct i2c_client *client = i2c_verify_client(dev);
310 struct i2c_driver *driver;
312 if (!client || !dev->driver)
314 driver = to_i2c_driver(dev->driver);
315 if (!driver->suspend)
317 return driver->suspend(client, mesg);
320 static int i2c_legacy_resume(struct device *dev)
322 struct i2c_client *client = i2c_verify_client(dev);
323 struct i2c_driver *driver;
325 if (!client || !dev->driver)
327 driver = to_i2c_driver(dev->driver);
330 return driver->resume(client);
333 static int i2c_device_pm_suspend(struct device *dev)
335 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
338 return pm_generic_suspend(dev);
340 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
343 static int i2c_device_pm_resume(struct device *dev)
345 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
348 return pm_generic_resume(dev);
350 return i2c_legacy_resume(dev);
353 static int i2c_device_pm_freeze(struct device *dev)
355 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
358 return pm_generic_freeze(dev);
360 return i2c_legacy_suspend(dev, PMSG_FREEZE);
363 static int i2c_device_pm_thaw(struct device *dev)
365 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
368 return pm_generic_thaw(dev);
370 return i2c_legacy_resume(dev);
373 static int i2c_device_pm_poweroff(struct device *dev)
375 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
378 return pm_generic_poweroff(dev);
380 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
383 static int i2c_device_pm_restore(struct device *dev)
385 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
388 return pm_generic_restore(dev);
390 return i2c_legacy_resume(dev);
392 #else /* !CONFIG_PM_SLEEP */
393 #define i2c_device_pm_suspend NULL
394 #define i2c_device_pm_resume NULL
395 #define i2c_device_pm_freeze NULL
396 #define i2c_device_pm_thaw NULL
397 #define i2c_device_pm_poweroff NULL
398 #define i2c_device_pm_restore NULL
399 #endif /* !CONFIG_PM_SLEEP */
401 static void i2c_client_dev_release(struct device *dev)
403 kfree(to_i2c_client(dev));
407 show_name(struct device *dev, struct device_attribute *attr, char *buf)
409 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
410 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
414 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
416 struct i2c_client *client = to_i2c_client(dev);
419 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
423 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
426 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
427 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
429 static struct attribute *i2c_dev_attrs[] = {
431 /* modalias helps coldplug: modprobe $(cat .../modalias) */
432 &dev_attr_modalias.attr,
436 static struct attribute_group i2c_dev_attr_group = {
437 .attrs = i2c_dev_attrs,
440 static const struct attribute_group *i2c_dev_attr_groups[] = {
445 static const struct dev_pm_ops i2c_device_pm_ops = {
446 .suspend = i2c_device_pm_suspend,
447 .resume = i2c_device_pm_resume,
448 .freeze = i2c_device_pm_freeze,
449 .thaw = i2c_device_pm_thaw,
450 .poweroff = i2c_device_pm_poweroff,
451 .restore = i2c_device_pm_restore,
453 pm_generic_runtime_suspend,
454 pm_generic_runtime_resume,
459 struct bus_type i2c_bus_type = {
461 .match = i2c_device_match,
462 .probe = i2c_device_probe,
463 .remove = i2c_device_remove,
464 .shutdown = i2c_device_shutdown,
465 .pm = &i2c_device_pm_ops,
467 EXPORT_SYMBOL_GPL(i2c_bus_type);
469 static struct device_type i2c_client_type = {
470 .groups = i2c_dev_attr_groups,
471 .uevent = i2c_device_uevent,
472 .release = i2c_client_dev_release,
477 * i2c_verify_client - return parameter as i2c_client, or NULL
478 * @dev: device, probably from some driver model iterator
480 * When traversing the driver model tree, perhaps using driver model
481 * iterators like @device_for_each_child(), you can't assume very much
482 * about the nodes you find. Use this function to avoid oopses caused
483 * by wrongly treating some non-I2C device as an i2c_client.
485 struct i2c_client *i2c_verify_client(struct device *dev)
487 return (dev->type == &i2c_client_type)
491 EXPORT_SYMBOL(i2c_verify_client);
494 /* This is a permissive address validity check, I2C address map constraints
495 * are purposely not enforced, except for the general call address. */
496 static int i2c_check_client_addr_validity(const struct i2c_client *client)
498 if (client->flags & I2C_CLIENT_TEN) {
499 /* 10-bit address, all values are valid */
500 if (client->addr > 0x3ff)
503 /* 7-bit address, reject the general call address */
504 if (client->addr == 0x00 || client->addr > 0x7f)
510 /* And this is a strict address validity check, used when probing. If a
511 * device uses a reserved address, then it shouldn't be probed. 7-bit
512 * addressing is assumed, 10-bit address devices are rare and should be
513 * explicitly enumerated. */
514 static int i2c_check_addr_validity(unsigned short addr)
517 * Reserved addresses per I2C specification:
518 * 0x00 General call address / START byte
520 * 0x02 Reserved for different bus format
521 * 0x03 Reserved for future purposes
522 * 0x04-0x07 Hs-mode master code
523 * 0x78-0x7b 10-bit slave addressing
524 * 0x7c-0x7f Reserved for future purposes
526 if (addr < 0x08 || addr > 0x77)
531 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
533 struct i2c_client *client = i2c_verify_client(dev);
534 int addr = *(int *)addrp;
536 if (client && client->addr == addr)
541 /* walk up mux tree */
542 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
544 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
547 result = device_for_each_child(&adapter->dev, &addr,
548 __i2c_check_addr_busy);
550 if (!result && parent)
551 result = i2c_check_mux_parents(parent, addr);
556 /* recurse down mux tree */
557 static int i2c_check_mux_children(struct device *dev, void *addrp)
561 if (dev->type == &i2c_adapter_type)
562 result = device_for_each_child(dev, addrp,
563 i2c_check_mux_children);
565 result = __i2c_check_addr_busy(dev, addrp);
570 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
572 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
576 result = i2c_check_mux_parents(parent, addr);
579 result = device_for_each_child(&adapter->dev, &addr,
580 i2c_check_mux_children);
586 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
587 * @adapter: Target I2C bus segment
589 void i2c_lock_adapter(struct i2c_adapter *adapter)
591 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
594 i2c_lock_adapter(parent);
596 rt_mutex_lock(&adapter->bus_lock);
598 EXPORT_SYMBOL_GPL(i2c_lock_adapter);
601 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
602 * @adapter: Target I2C bus segment
604 static int i2c_trylock_adapter(struct i2c_adapter *adapter)
606 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
609 return i2c_trylock_adapter(parent);
611 return rt_mutex_trylock(&adapter->bus_lock);
615 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
616 * @adapter: Target I2C bus segment
618 void i2c_unlock_adapter(struct i2c_adapter *adapter)
620 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
623 i2c_unlock_adapter(parent);
625 rt_mutex_unlock(&adapter->bus_lock);
627 EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
629 static void i2c_dev_set_name(struct i2c_adapter *adap,
630 struct i2c_client *client)
632 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
635 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
639 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
640 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
641 client->addr | ((client->flags & I2C_CLIENT_TEN)
646 * i2c_new_device - instantiate an i2c device
647 * @adap: the adapter managing the device
648 * @info: describes one I2C device; bus_num is ignored
651 * Create an i2c device. Binding is handled through driver model
652 * probe()/remove() methods. A driver may be bound to this device when we
653 * return from this function, or any later moment (e.g. maybe hotplugging will
654 * load the driver module). This call is not appropriate for use by mainboard
655 * initialization logic, which usually runs during an arch_initcall() long
656 * before any i2c_adapter could exist.
658 * This returns the new i2c client, which may be saved for later use with
659 * i2c_unregister_device(); or NULL to indicate an error.
662 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
664 struct i2c_client *client;
667 client = kzalloc(sizeof *client, GFP_KERNEL);
671 client->adapter = adap;
673 client->dev.platform_data = info->platform_data;
676 client->dev.archdata = *info->archdata;
678 client->flags = info->flags;
679 client->addr = info->addr;
680 client->irq = info->irq;
682 strlcpy(client->name, info->type, sizeof(client->name));
684 /* Check for address validity */
685 status = i2c_check_client_addr_validity(client);
687 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
688 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
692 /* Check for address business */
693 status = i2c_check_addr_busy(adap, client->addr);
697 client->dev.parent = &client->adapter->dev;
698 client->dev.bus = &i2c_bus_type;
699 client->dev.type = &i2c_client_type;
700 client->dev.of_node = info->of_node;
701 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
703 i2c_dev_set_name(adap, client);
704 status = device_register(&client->dev);
708 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
709 client->name, dev_name(&client->dev));
714 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
715 "(%d)\n", client->name, client->addr, status);
720 EXPORT_SYMBOL_GPL(i2c_new_device);
724 * i2c_unregister_device - reverse effect of i2c_new_device()
725 * @client: value returned from i2c_new_device()
728 void i2c_unregister_device(struct i2c_client *client)
730 device_unregister(&client->dev);
732 EXPORT_SYMBOL_GPL(i2c_unregister_device);
735 static const struct i2c_device_id dummy_id[] = {
740 static int dummy_probe(struct i2c_client *client,
741 const struct i2c_device_id *id)
746 static int dummy_remove(struct i2c_client *client)
751 static struct i2c_driver dummy_driver = {
752 .driver.name = "dummy",
753 .probe = dummy_probe,
754 .remove = dummy_remove,
755 .id_table = dummy_id,
759 * i2c_new_dummy - return a new i2c device bound to a dummy driver
760 * @adapter: the adapter managing the device
761 * @address: seven bit address to be used
764 * This returns an I2C client bound to the "dummy" driver, intended for use
765 * with devices that consume multiple addresses. Examples of such chips
766 * include various EEPROMS (like 24c04 and 24c08 models).
768 * These dummy devices have two main uses. First, most I2C and SMBus calls
769 * except i2c_transfer() need a client handle; the dummy will be that handle.
770 * And second, this prevents the specified address from being bound to a
773 * This returns the new i2c client, which should be saved for later use with
774 * i2c_unregister_device(); or NULL to indicate an error.
776 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
778 struct i2c_board_info info = {
779 I2C_BOARD_INFO("dummy", address),
782 return i2c_new_device(adapter, &info);
784 EXPORT_SYMBOL_GPL(i2c_new_dummy);
786 /* ------------------------------------------------------------------------- */
788 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
790 static void i2c_adapter_dev_release(struct device *dev)
792 struct i2c_adapter *adap = to_i2c_adapter(dev);
793 complete(&adap->dev_released);
797 * This function is only needed for mutex_lock_nested, so it is never
798 * called unless locking correctness checking is enabled. Thus we
799 * make it inline to avoid a compiler warning. That's what gcc ends up
802 static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
804 unsigned int depth = 0;
806 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
813 * Let users instantiate I2C devices through sysfs. This can be used when
814 * platform initialization code doesn't contain the proper data for
815 * whatever reason. Also useful for drivers that do device detection and
816 * detection fails, either because the device uses an unexpected address,
817 * or this is a compatible device with different ID register values.
819 * Parameter checking may look overzealous, but we really don't want
820 * the user to provide incorrect parameters.
823 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
824 const char *buf, size_t count)
826 struct i2c_adapter *adap = to_i2c_adapter(dev);
827 struct i2c_board_info info;
828 struct i2c_client *client;
832 memset(&info, 0, sizeof(struct i2c_board_info));
834 blank = strchr(buf, ' ');
836 dev_err(dev, "%s: Missing parameters\n", "new_device");
839 if (blank - buf > I2C_NAME_SIZE - 1) {
840 dev_err(dev, "%s: Invalid device name\n", "new_device");
843 memcpy(info.type, buf, blank - buf);
845 /* Parse remaining parameters, reject extra parameters */
846 res = sscanf(++blank, "%hi%c", &info.addr, &end);
848 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
851 if (res > 1 && end != '\n') {
852 dev_err(dev, "%s: Extra parameters\n", "new_device");
856 client = i2c_new_device(adap, &info);
860 /* Keep track of the added device */
861 mutex_lock(&adap->userspace_clients_lock);
862 list_add_tail(&client->detected, &adap->userspace_clients);
863 mutex_unlock(&adap->userspace_clients_lock);
864 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
865 info.type, info.addr);
871 * And of course let the users delete the devices they instantiated, if
872 * they got it wrong. This interface can only be used to delete devices
873 * instantiated by i2c_sysfs_new_device above. This guarantees that we
874 * don't delete devices to which some kernel code still has references.
876 * Parameter checking may look overzealous, but we really don't want
877 * the user to delete the wrong device.
880 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
881 const char *buf, size_t count)
883 struct i2c_adapter *adap = to_i2c_adapter(dev);
884 struct i2c_client *client, *next;
889 /* Parse parameters, reject extra parameters */
890 res = sscanf(buf, "%hi%c", &addr, &end);
892 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
895 if (res > 1 && end != '\n') {
896 dev_err(dev, "%s: Extra parameters\n", "delete_device");
900 /* Make sure the device was added through sysfs */
902 mutex_lock_nested(&adap->userspace_clients_lock,
903 i2c_adapter_depth(adap));
904 list_for_each_entry_safe(client, next, &adap->userspace_clients,
906 if (client->addr == addr) {
907 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
908 "delete_device", client->name, client->addr);
910 list_del(&client->detected);
911 i2c_unregister_device(client);
916 mutex_unlock(&adap->userspace_clients_lock);
919 dev_err(dev, "%s: Can't find device in list\n",
924 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
925 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
926 i2c_sysfs_delete_device);
928 static struct attribute *i2c_adapter_attrs[] = {
930 &dev_attr_new_device.attr,
931 &dev_attr_delete_device.attr,
935 static struct attribute_group i2c_adapter_attr_group = {
936 .attrs = i2c_adapter_attrs,
939 static const struct attribute_group *i2c_adapter_attr_groups[] = {
940 &i2c_adapter_attr_group,
944 struct device_type i2c_adapter_type = {
945 .groups = i2c_adapter_attr_groups,
946 .release = i2c_adapter_dev_release,
948 EXPORT_SYMBOL_GPL(i2c_adapter_type);
951 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
952 * @dev: device, probably from some driver model iterator
954 * When traversing the driver model tree, perhaps using driver model
955 * iterators like @device_for_each_child(), you can't assume very much
956 * about the nodes you find. Use this function to avoid oopses caused
957 * by wrongly treating some non-I2C device as an i2c_adapter.
959 struct i2c_adapter *i2c_verify_adapter(struct device *dev)
961 return (dev->type == &i2c_adapter_type)
962 ? to_i2c_adapter(dev)
965 EXPORT_SYMBOL(i2c_verify_adapter);
967 #ifdef CONFIG_I2C_COMPAT
968 static struct class_compat *i2c_adapter_compat_class;
971 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
973 struct i2c_devinfo *devinfo;
975 down_read(&__i2c_board_lock);
976 list_for_each_entry(devinfo, &__i2c_board_list, list) {
977 if (devinfo->busnum == adapter->nr
978 && !i2c_new_device(adapter,
979 &devinfo->board_info))
980 dev_err(&adapter->dev,
981 "Can't create device at 0x%02x\n",
982 devinfo->board_info.addr);
984 up_read(&__i2c_board_lock);
987 /* OF support code */
989 #if IS_ENABLED(CONFIG_OF)
990 static void of_i2c_register_devices(struct i2c_adapter *adap)
993 struct device_node *node;
995 /* Only register child devices if the adapter has a node pointer set */
996 if (!adap->dev.of_node)
999 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1001 for_each_available_child_of_node(adap->dev.of_node, node) {
1002 struct i2c_board_info info = {};
1003 struct dev_archdata dev_ad = {};
1007 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1009 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1010 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1015 addr = of_get_property(node, "reg", &len);
1016 if (!addr || (len < sizeof(int))) {
1017 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1022 info.addr = be32_to_cpup(addr);
1023 if (info.addr > (1 << 10) - 1) {
1024 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1025 info.addr, node->full_name);
1029 info.irq = irq_of_parse_and_map(node, 0);
1030 info.of_node = of_node_get(node);
1031 info.archdata = &dev_ad;
1033 if (of_get_property(node, "wakeup-source", NULL))
1034 info.flags |= I2C_CLIENT_WAKE;
1036 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1038 result = i2c_new_device(adap, &info);
1039 if (result == NULL) {
1040 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1043 irq_dispose_mapping(info.irq);
1049 static int of_dev_node_match(struct device *dev, void *data)
1051 return dev->of_node == data;
1054 /* must call put_device() when done with returned i2c_client device */
1055 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1059 dev = bus_find_device(&i2c_bus_type, NULL, node,
1064 return i2c_verify_client(dev);
1066 EXPORT_SYMBOL(of_find_i2c_device_by_node);
1068 /* must call put_device() when done with returned i2c_adapter device */
1069 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1073 dev = bus_find_device(&i2c_bus_type, NULL, node,
1078 return i2c_verify_adapter(dev);
1080 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1082 static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1083 #endif /* CONFIG_OF */
1085 /* ACPI support code */
1087 #if IS_ENABLED(CONFIG_ACPI)
1088 static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1090 struct i2c_board_info *info = data;
1092 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1093 struct acpi_resource_i2c_serialbus *sb;
1095 sb = &ares->data.i2c_serial_bus;
1096 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1097 info->addr = sb->slave_address;
1098 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1099 info->flags |= I2C_CLIENT_TEN;
1101 } else if (info->irq < 0) {
1104 if (acpi_dev_resource_interrupt(ares, 0, &r))
1105 info->irq = r.start;
1108 /* Tell the ACPI core to skip this resource */
1112 static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1113 void *data, void **return_value)
1115 struct i2c_adapter *adapter = data;
1116 struct list_head resource_list;
1117 struct i2c_board_info info;
1118 struct acpi_device *adev;
1121 if (acpi_bus_get_device(handle, &adev))
1123 if (acpi_bus_get_status(adev) || !adev->status.present)
1126 memset(&info, 0, sizeof(info));
1127 info.acpi_node.companion = adev;
1130 INIT_LIST_HEAD(&resource_list);
1131 ret = acpi_dev_get_resources(adev, &resource_list,
1132 acpi_i2c_add_resource, &info);
1133 acpi_dev_free_resource_list(&resource_list);
1135 if (ret < 0 || !info.addr)
1138 adev->power.flags.ignore_parent = true;
1139 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1140 if (!i2c_new_device(adapter, &info)) {
1141 adev->power.flags.ignore_parent = false;
1142 dev_err(&adapter->dev,
1143 "failed to add I2C device %s from ACPI\n",
1144 dev_name(&adev->dev));
1151 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1152 * @adap: pointer to adapter
1154 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1155 * namespace. When a device is found it will be added to the Linux device
1156 * model and bound to the corresponding ACPI handle.
1158 static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1163 if (!adap->dev.parent)
1166 handle = ACPI_HANDLE(adap->dev.parent);
1170 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1171 acpi_i2c_add_device, NULL,
1173 if (ACPI_FAILURE(status))
1174 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1177 static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1178 #endif /* CONFIG_ACPI */
1180 static int i2c_do_add_adapter(struct i2c_driver *driver,
1181 struct i2c_adapter *adap)
1183 /* Detect supported devices on that bus, and instantiate them */
1184 i2c_detect(adap, driver);
1186 /* Let legacy drivers scan this bus for matching devices */
1187 if (driver->attach_adapter) {
1188 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1189 driver->driver.name);
1190 dev_warn(&adap->dev, "Please use another way to instantiate "
1191 "your i2c_client\n");
1192 /* We ignore the return code; if it fails, too bad */
1193 driver->attach_adapter(adap);
1198 static int __process_new_adapter(struct device_driver *d, void *data)
1200 return i2c_do_add_adapter(to_i2c_driver(d), data);
1203 static int i2c_register_adapter(struct i2c_adapter *adap)
1207 /* Can't register until after driver model init */
1208 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1214 if (unlikely(adap->name[0] == '\0')) {
1215 pr_err("i2c-core: Attempt to register an adapter with "
1219 if (unlikely(!adap->algo)) {
1220 pr_err("i2c-core: Attempt to register adapter '%s' with "
1221 "no algo!\n", adap->name);
1225 rt_mutex_init(&adap->bus_lock);
1226 mutex_init(&adap->userspace_clients_lock);
1227 INIT_LIST_HEAD(&adap->userspace_clients);
1229 /* Set default timeout to 1 second if not already set */
1230 if (adap->timeout == 0)
1233 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
1234 adap->dev.bus = &i2c_bus_type;
1235 adap->dev.type = &i2c_adapter_type;
1236 res = device_register(&adap->dev);
1240 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1242 #ifdef CONFIG_I2C_COMPAT
1243 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1246 dev_warn(&adap->dev,
1247 "Failed to create compatibility class link\n");
1250 /* bus recovery specific initialization */
1251 if (adap->bus_recovery_info) {
1252 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1254 if (!bri->recover_bus) {
1255 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1256 adap->bus_recovery_info = NULL;
1260 /* Generic GPIO recovery */
1261 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1262 if (!gpio_is_valid(bri->scl_gpio)) {
1263 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1264 adap->bus_recovery_info = NULL;
1268 if (gpio_is_valid(bri->sda_gpio))
1269 bri->get_sda = get_sda_gpio_value;
1271 bri->get_sda = NULL;
1273 bri->get_scl = get_scl_gpio_value;
1274 bri->set_scl = set_scl_gpio_value;
1275 } else if (!bri->set_scl || !bri->get_scl) {
1276 /* Generic SCL recovery */
1277 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1278 adap->bus_recovery_info = NULL;
1283 /* create pre-declared device nodes */
1284 of_i2c_register_devices(adap);
1285 acpi_i2c_register_devices(adap);
1287 if (adap->nr < __i2c_first_dynamic_bus_num)
1288 i2c_scan_static_board_info(adap);
1290 /* Notify drivers */
1291 mutex_lock(&core_lock);
1292 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1293 mutex_unlock(&core_lock);
1298 mutex_lock(&core_lock);
1299 idr_remove(&i2c_adapter_idr, adap->nr);
1300 mutex_unlock(&core_lock);
1305 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1306 * @adap: the adapter to register (with adap->nr initialized)
1307 * Context: can sleep
1309 * See i2c_add_numbered_adapter() for details.
1311 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1315 mutex_lock(&core_lock);
1316 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1318 mutex_unlock(&core_lock);
1320 return id == -ENOSPC ? -EBUSY : id;
1322 return i2c_register_adapter(adap);
1326 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1327 * @adapter: the adapter to add
1328 * Context: can sleep
1330 * This routine is used to declare an I2C adapter when its bus number
1331 * doesn't matter or when its bus number is specified by an dt alias.
1332 * Examples of bases when the bus number doesn't matter: I2C adapters
1333 * dynamically added by USB links or PCI plugin cards.
1335 * When this returns zero, a new bus number was allocated and stored
1336 * in adap->nr, and the specified adapter became available for clients.
1337 * Otherwise, a negative errno value is returned.
1339 int i2c_add_adapter(struct i2c_adapter *adapter)
1341 struct device *dev = &adapter->dev;
1345 id = of_alias_get_id(dev->of_node, "i2c");
1348 return __i2c_add_numbered_adapter(adapter);
1352 mutex_lock(&core_lock);
1353 id = idr_alloc(&i2c_adapter_idr, adapter,
1354 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
1355 mutex_unlock(&core_lock);
1361 return i2c_register_adapter(adapter);
1363 EXPORT_SYMBOL(i2c_add_adapter);
1366 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1367 * @adap: the adapter to register (with adap->nr initialized)
1368 * Context: can sleep
1370 * This routine is used to declare an I2C adapter when its bus number
1371 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1372 * or otherwise built in to the system's mainboard, and where i2c_board_info
1373 * is used to properly configure I2C devices.
1375 * If the requested bus number is set to -1, then this function will behave
1376 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1378 * If no devices have pre-been declared for this bus, then be sure to
1379 * register the adapter before any dynamically allocated ones. Otherwise
1380 * the required bus ID may not be available.
1382 * When this returns zero, the specified adapter became available for
1383 * clients using the bus number provided in adap->nr. Also, the table
1384 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1385 * and the appropriate driver model device nodes are created. Otherwise, a
1386 * negative errno value is returned.
1388 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1390 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1391 return i2c_add_adapter(adap);
1393 return __i2c_add_numbered_adapter(adap);
1395 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1397 static void i2c_do_del_adapter(struct i2c_driver *driver,
1398 struct i2c_adapter *adapter)
1400 struct i2c_client *client, *_n;
1402 /* Remove the devices we created ourselves as the result of hardware
1403 * probing (using a driver's detect method) */
1404 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1405 if (client->adapter == adapter) {
1406 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1407 client->name, client->addr);
1408 list_del(&client->detected);
1409 i2c_unregister_device(client);
1414 static int __unregister_client(struct device *dev, void *dummy)
1416 struct i2c_client *client = i2c_verify_client(dev);
1417 if (client && strcmp(client->name, "dummy"))
1418 i2c_unregister_device(client);
1422 static int __unregister_dummy(struct device *dev, void *dummy)
1424 struct i2c_client *client = i2c_verify_client(dev);
1426 i2c_unregister_device(client);
1430 static int __process_removed_adapter(struct device_driver *d, void *data)
1432 i2c_do_del_adapter(to_i2c_driver(d), data);
1437 * i2c_del_adapter - unregister I2C adapter
1438 * @adap: the adapter being unregistered
1439 * Context: can sleep
1441 * This unregisters an I2C adapter which was previously registered
1442 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1444 void i2c_del_adapter(struct i2c_adapter *adap)
1446 struct i2c_adapter *found;
1447 struct i2c_client *client, *next;
1449 /* First make sure that this adapter was ever added */
1450 mutex_lock(&core_lock);
1451 found = idr_find(&i2c_adapter_idr, adap->nr);
1452 mutex_unlock(&core_lock);
1453 if (found != adap) {
1454 pr_debug("i2c-core: attempting to delete unregistered "
1455 "adapter [%s]\n", adap->name);
1459 /* Tell drivers about this removal */
1460 mutex_lock(&core_lock);
1461 bus_for_each_drv(&i2c_bus_type, NULL, adap,
1462 __process_removed_adapter);
1463 mutex_unlock(&core_lock);
1465 /* Remove devices instantiated from sysfs */
1466 mutex_lock_nested(&adap->userspace_clients_lock,
1467 i2c_adapter_depth(adap));
1468 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1470 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1472 list_del(&client->detected);
1473 i2c_unregister_device(client);
1475 mutex_unlock(&adap->userspace_clients_lock);
1477 /* Detach any active clients. This can't fail, thus we do not
1478 * check the returned value. This is a two-pass process, because
1479 * we can't remove the dummy devices during the first pass: they
1480 * could have been instantiated by real devices wishing to clean
1481 * them up properly, so we give them a chance to do that first. */
1482 device_for_each_child(&adap->dev, NULL, __unregister_client);
1483 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1485 #ifdef CONFIG_I2C_COMPAT
1486 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1490 /* device name is gone after device_unregister */
1491 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1493 /* clean up the sysfs representation */
1494 init_completion(&adap->dev_released);
1495 device_unregister(&adap->dev);
1497 /* wait for sysfs to drop all references */
1498 wait_for_completion(&adap->dev_released);
1501 mutex_lock(&core_lock);
1502 idr_remove(&i2c_adapter_idr, adap->nr);
1503 mutex_unlock(&core_lock);
1505 /* Clear the device structure in case this adapter is ever going to be
1507 memset(&adap->dev, 0, sizeof(adap->dev));
1509 EXPORT_SYMBOL(i2c_del_adapter);
1511 /* ------------------------------------------------------------------------- */
1513 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1517 mutex_lock(&core_lock);
1518 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1519 mutex_unlock(&core_lock);
1523 EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1525 static int __process_new_driver(struct device *dev, void *data)
1527 if (dev->type != &i2c_adapter_type)
1529 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1533 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1534 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1537 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1541 /* Can't register until after driver model init */
1542 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1545 /* add the driver to the list of i2c drivers in the driver core */
1546 driver->driver.owner = owner;
1547 driver->driver.bus = &i2c_bus_type;
1549 /* When registration returns, the driver core
1550 * will have called probe() for all matching-but-unbound devices.
1552 res = driver_register(&driver->driver);
1556 /* Drivers should switch to dev_pm_ops instead. */
1557 if (driver->suspend)
1558 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1559 driver->driver.name);
1561 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1562 driver->driver.name);
1564 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1566 INIT_LIST_HEAD(&driver->clients);
1567 /* Walk the adapters that are already present */
1568 i2c_for_each_dev(driver, __process_new_driver);
1572 EXPORT_SYMBOL(i2c_register_driver);
1574 static int __process_removed_driver(struct device *dev, void *data)
1576 if (dev->type == &i2c_adapter_type)
1577 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1582 * i2c_del_driver - unregister I2C driver
1583 * @driver: the driver being unregistered
1584 * Context: can sleep
1586 void i2c_del_driver(struct i2c_driver *driver)
1588 i2c_for_each_dev(driver, __process_removed_driver);
1590 driver_unregister(&driver->driver);
1591 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1593 EXPORT_SYMBOL(i2c_del_driver);
1595 /* ------------------------------------------------------------------------- */
1598 * i2c_use_client - increments the reference count of the i2c client structure
1599 * @client: the client being referenced
1601 * Each live reference to a client should be refcounted. The driver model does
1602 * that automatically as part of driver binding, so that most drivers don't
1603 * need to do this explicitly: they hold a reference until they're unbound
1606 * A pointer to the client with the incremented reference counter is returned.
1608 struct i2c_client *i2c_use_client(struct i2c_client *client)
1610 if (client && get_device(&client->dev))
1614 EXPORT_SYMBOL(i2c_use_client);
1617 * i2c_release_client - release a use of the i2c client structure
1618 * @client: the client being no longer referenced
1620 * Must be called when a user of a client is finished with it.
1622 void i2c_release_client(struct i2c_client *client)
1625 put_device(&client->dev);
1627 EXPORT_SYMBOL(i2c_release_client);
1629 struct i2c_cmd_arg {
1634 static int i2c_cmd(struct device *dev, void *_arg)
1636 struct i2c_client *client = i2c_verify_client(dev);
1637 struct i2c_cmd_arg *arg = _arg;
1638 struct i2c_driver *driver;
1640 if (!client || !client->dev.driver)
1643 driver = to_i2c_driver(client->dev.driver);
1644 if (driver->command)
1645 driver->command(client, arg->cmd, arg->arg);
1649 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1651 struct i2c_cmd_arg cmd_arg;
1655 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1657 EXPORT_SYMBOL(i2c_clients_command);
1659 static int __init i2c_init(void)
1663 retval = bus_register(&i2c_bus_type);
1666 #ifdef CONFIG_I2C_COMPAT
1667 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1668 if (!i2c_adapter_compat_class) {
1673 retval = i2c_add_driver(&dummy_driver);
1679 #ifdef CONFIG_I2C_COMPAT
1680 class_compat_unregister(i2c_adapter_compat_class);
1683 bus_unregister(&i2c_bus_type);
1687 static void __exit i2c_exit(void)
1689 i2c_del_driver(&dummy_driver);
1690 #ifdef CONFIG_I2C_COMPAT
1691 class_compat_unregister(i2c_adapter_compat_class);
1693 bus_unregister(&i2c_bus_type);
1696 /* We must initialize early, because some subsystems register i2c drivers
1697 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1699 postcore_initcall(i2c_init);
1700 module_exit(i2c_exit);
1702 /* ----------------------------------------------------
1703 * the functional interface to the i2c busses.
1704 * ----------------------------------------------------
1708 * __i2c_transfer - unlocked flavor of i2c_transfer
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 * Adapter lock must be held when calling this function. No debug logging
1717 * takes place. adap->algo->master_xfer existence isn't checked.
1719 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1721 unsigned long orig_jiffies;
1724 /* Retry automatically on arbitration loss */
1725 orig_jiffies = jiffies;
1726 for (ret = 0, try = 0; try <= adap->retries; try++) {
1727 ret = adap->algo->master_xfer(adap, msgs, num);
1730 if (time_after(jiffies, orig_jiffies + adap->timeout))
1736 EXPORT_SYMBOL(__i2c_transfer);
1739 * i2c_transfer - execute a single or combined I2C message
1740 * @adap: Handle to I2C bus
1741 * @msgs: One or more messages to execute before STOP is issued to
1742 * terminate the operation; each message begins with a START.
1743 * @num: Number of messages to be executed.
1745 * Returns negative errno, else the number of messages executed.
1747 * Note that there is no requirement that each message be sent to
1748 * the same slave address, although that is the most common model.
1750 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1754 /* REVISIT the fault reporting model here is weak:
1756 * - When we get an error after receiving N bytes from a slave,
1757 * there is no way to report "N".
1759 * - When we get a NAK after transmitting N bytes to a slave,
1760 * there is no way to report "N" ... or to let the master
1761 * continue executing the rest of this combined message, if
1762 * that's the appropriate response.
1764 * - When for example "num" is two and we successfully complete
1765 * the first message but get an error part way through the
1766 * second, it's unclear whether that should be reported as
1767 * one (discarding status on the second message) or errno
1768 * (discarding status on the first one).
1771 if (adap->algo->master_xfer) {
1773 for (ret = 0; ret < num; ret++) {
1774 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1775 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1776 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1777 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1781 if (in_atomic() || irqs_disabled()) {
1782 ret = i2c_trylock_adapter(adap);
1784 /* I2C activity is ongoing. */
1787 i2c_lock_adapter(adap);
1790 ret = __i2c_transfer(adap, msgs, num);
1791 i2c_unlock_adapter(adap);
1795 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1799 EXPORT_SYMBOL(i2c_transfer);
1802 * i2c_master_send - issue a single I2C message in master transmit mode
1803 * @client: Handle to slave device
1804 * @buf: Data that will be written to the slave
1805 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1807 * Returns negative errno, or else the number of bytes written.
1809 int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
1812 struct i2c_adapter *adap = client->adapter;
1815 msg.addr = client->addr;
1816 msg.flags = client->flags & I2C_M_TEN;
1818 msg.buf = (char *)buf;
1820 ret = i2c_transfer(adap, &msg, 1);
1823 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1824 * transmitted, else error code.
1826 return (ret == 1) ? count : ret;
1828 EXPORT_SYMBOL(i2c_master_send);
1831 * i2c_master_recv - issue a single I2C message in master receive mode
1832 * @client: Handle to slave device
1833 * @buf: Where to store data read from slave
1834 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1836 * Returns negative errno, or else the number of bytes read.
1838 int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
1840 struct i2c_adapter *adap = client->adapter;
1844 msg.addr = client->addr;
1845 msg.flags = client->flags & I2C_M_TEN;
1846 msg.flags |= I2C_M_RD;
1850 ret = i2c_transfer(adap, &msg, 1);
1853 * If everything went ok (i.e. 1 msg received), return #bytes received,
1856 return (ret == 1) ? count : ret;
1858 EXPORT_SYMBOL(i2c_master_recv);
1860 /* ----------------------------------------------------
1861 * the i2c address scanning function
1862 * Will not work for 10-bit addresses!
1863 * ----------------------------------------------------
1867 * Legacy default probe function, mostly relevant for SMBus. The default
1868 * probe method is a quick write, but it is known to corrupt the 24RF08
1869 * EEPROMs due to a state machine bug, and could also irreversibly
1870 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1871 * we use a short byte read instead. Also, some bus drivers don't implement
1872 * quick write, so we fallback to a byte read in that case too.
1873 * On x86, there is another special case for FSC hardware monitoring chips,
1874 * which want regular byte reads (address 0x73.) Fortunately, these are the
1875 * only known chips using this I2C address on PC hardware.
1876 * Returns 1 if probe succeeded, 0 if not.
1878 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1881 union i2c_smbus_data dummy;
1884 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1885 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1886 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1887 I2C_SMBUS_BYTE_DATA, &dummy);
1890 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1891 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1892 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1893 I2C_SMBUS_QUICK, NULL);
1894 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1895 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1896 I2C_SMBUS_BYTE, &dummy);
1898 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1906 static int i2c_detect_address(struct i2c_client *temp_client,
1907 struct i2c_driver *driver)
1909 struct i2c_board_info info;
1910 struct i2c_adapter *adapter = temp_client->adapter;
1911 int addr = temp_client->addr;
1914 /* Make sure the address is valid */
1915 err = i2c_check_addr_validity(addr);
1917 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1922 /* Skip if already in use */
1923 if (i2c_check_addr_busy(adapter, addr))
1926 /* Make sure there is something at this address */
1927 if (!i2c_default_probe(adapter, addr))
1930 /* Finally call the custom detection function */
1931 memset(&info, 0, sizeof(struct i2c_board_info));
1933 err = driver->detect(temp_client, &info);
1935 /* -ENODEV is returned if the detection fails. We catch it
1936 here as this isn't an error. */
1937 return err == -ENODEV ? 0 : err;
1940 /* Consistency check */
1941 if (info.type[0] == '\0') {
1942 dev_err(&adapter->dev, "%s detection function provided "
1943 "no name for 0x%x\n", driver->driver.name,
1946 struct i2c_client *client;
1948 /* Detection succeeded, instantiate the device */
1949 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1950 info.type, info.addr);
1951 client = i2c_new_device(adapter, &info);
1953 list_add_tail(&client->detected, &driver->clients);
1955 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1956 info.type, info.addr);
1961 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1963 const unsigned short *address_list;
1964 struct i2c_client *temp_client;
1966 int adap_id = i2c_adapter_id(adapter);
1968 address_list = driver->address_list;
1969 if (!driver->detect || !address_list)
1972 /* Stop here if the classes do not match */
1973 if (!(adapter->class & driver->class))
1976 /* Set up a temporary client to help detect callback */
1977 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1980 temp_client->adapter = adapter;
1982 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1983 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1984 "addr 0x%02x\n", adap_id, address_list[i]);
1985 temp_client->addr = address_list[i];
1986 err = i2c_detect_address(temp_client, driver);
1995 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1997 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1998 I2C_SMBUS_QUICK, NULL) >= 0;
2000 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2003 i2c_new_probed_device(struct i2c_adapter *adap,
2004 struct i2c_board_info *info,
2005 unsigned short const *addr_list,
2006 int (*probe)(struct i2c_adapter *, unsigned short addr))
2011 probe = i2c_default_probe;
2013 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2014 /* Check address validity */
2015 if (i2c_check_addr_validity(addr_list[i]) < 0) {
2016 dev_warn(&adap->dev, "Invalid 7-bit address "
2017 "0x%02x\n", addr_list[i]);
2021 /* Check address availability */
2022 if (i2c_check_addr_busy(adap, addr_list[i])) {
2023 dev_dbg(&adap->dev, "Address 0x%02x already in "
2024 "use, not probing\n", addr_list[i]);
2028 /* Test address responsiveness */
2029 if (probe(adap, addr_list[i]))
2033 if (addr_list[i] == I2C_CLIENT_END) {
2034 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2038 info->addr = addr_list[i];
2039 return i2c_new_device(adap, info);
2041 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2043 struct i2c_adapter *i2c_get_adapter(int nr)
2045 struct i2c_adapter *adapter;
2047 mutex_lock(&core_lock);
2048 adapter = idr_find(&i2c_adapter_idr, nr);
2049 if (adapter && !try_module_get(adapter->owner))
2052 mutex_unlock(&core_lock);
2055 EXPORT_SYMBOL(i2c_get_adapter);
2057 void i2c_put_adapter(struct i2c_adapter *adap)
2060 module_put(adap->owner);
2062 EXPORT_SYMBOL(i2c_put_adapter);
2064 /* The SMBus parts */
2066 #define POLY (0x1070U << 3)
2067 static u8 crc8(u16 data)
2071 for (i = 0; i < 8; i++) {
2076 return (u8)(data >> 8);
2079 /* Incremental CRC8 over count bytes in the array pointed to by p */
2080 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
2084 for (i = 0; i < count; i++)
2085 crc = crc8((crc ^ p[i]) << 8);
2089 /* Assume a 7-bit address, which is reasonable for SMBus */
2090 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
2092 /* The address will be sent first */
2093 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2094 pec = i2c_smbus_pec(pec, &addr, 1);
2096 /* The data buffer follows */
2097 return i2c_smbus_pec(pec, msg->buf, msg->len);
2100 /* Used for write only transactions */
2101 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
2103 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2107 /* Return <0 on CRC error
2108 If there was a write before this read (most cases) we need to take the
2109 partial CRC from the write part into account.
2110 Note that this function does modify the message (we need to decrease the
2111 message length to hide the CRC byte from the caller). */
2112 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
2114 u8 rpec = msg->buf[--msg->len];
2115 cpec = i2c_smbus_msg_pec(cpec, msg);
2118 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2126 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2127 * @client: Handle to slave device
2129 * This executes the SMBus "receive byte" protocol, returning negative errno
2130 * else the byte received from the device.
2132 s32 i2c_smbus_read_byte(const struct i2c_client *client)
2134 union i2c_smbus_data data;
2137 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2139 I2C_SMBUS_BYTE, &data);
2140 return (status < 0) ? status : data.byte;
2142 EXPORT_SYMBOL(i2c_smbus_read_byte);
2145 * i2c_smbus_write_byte - SMBus "send byte" protocol
2146 * @client: Handle to slave device
2147 * @value: Byte to be sent
2149 * This executes the SMBus "send byte" protocol, returning negative errno
2150 * else zero on success.
2152 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
2154 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2155 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
2157 EXPORT_SYMBOL(i2c_smbus_write_byte);
2160 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2161 * @client: Handle to slave device
2162 * @command: Byte interpreted by slave
2164 * This executes the SMBus "read byte" protocol, returning negative errno
2165 * else a data byte received from the device.
2167 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
2169 union i2c_smbus_data data;
2172 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2173 I2C_SMBUS_READ, command,
2174 I2C_SMBUS_BYTE_DATA, &data);
2175 return (status < 0) ? status : data.byte;
2177 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
2180 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2181 * @client: Handle to slave device
2182 * @command: Byte interpreted by slave
2183 * @value: Byte being written
2185 * This executes the SMBus "write byte" protocol, returning negative errno
2186 * else zero on success.
2188 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2191 union i2c_smbus_data data;
2193 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2194 I2C_SMBUS_WRITE, command,
2195 I2C_SMBUS_BYTE_DATA, &data);
2197 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
2200 * i2c_smbus_read_word_data - SMBus "read word" protocol
2201 * @client: Handle to slave device
2202 * @command: Byte interpreted by slave
2204 * This executes the SMBus "read word" protocol, returning negative errno
2205 * else a 16-bit unsigned "word" received from the device.
2207 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
2209 union i2c_smbus_data data;
2212 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2213 I2C_SMBUS_READ, command,
2214 I2C_SMBUS_WORD_DATA, &data);
2215 return (status < 0) ? status : data.word;
2217 EXPORT_SYMBOL(i2c_smbus_read_word_data);
2220 * i2c_smbus_write_word_data - SMBus "write word" protocol
2221 * @client: Handle to slave device
2222 * @command: Byte interpreted by slave
2223 * @value: 16-bit "word" being written
2225 * This executes the SMBus "write word" protocol, returning negative errno
2226 * else zero on success.
2228 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2231 union i2c_smbus_data data;
2233 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2234 I2C_SMBUS_WRITE, command,
2235 I2C_SMBUS_WORD_DATA, &data);
2237 EXPORT_SYMBOL(i2c_smbus_write_word_data);
2240 * i2c_smbus_read_block_data - SMBus "block read" protocol
2241 * @client: Handle to slave device
2242 * @command: Byte interpreted by slave
2243 * @values: Byte array into which data will be read; big enough to hold
2244 * the data returned by the slave. SMBus allows at most 32 bytes.
2246 * This executes the SMBus "block read" protocol, returning negative errno
2247 * else the number of data bytes in the slave's response.
2249 * Note that using this function requires that the client's adapter support
2250 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2251 * support this; its emulation through I2C messaging relies on a specific
2252 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2254 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
2257 union i2c_smbus_data data;
2260 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2261 I2C_SMBUS_READ, command,
2262 I2C_SMBUS_BLOCK_DATA, &data);
2266 memcpy(values, &data.block[1], data.block[0]);
2267 return data.block[0];
2269 EXPORT_SYMBOL(i2c_smbus_read_block_data);
2272 * i2c_smbus_write_block_data - SMBus "block write" protocol
2273 * @client: Handle to slave device
2274 * @command: Byte interpreted by slave
2275 * @length: Size of data block; SMBus allows at most 32 bytes
2276 * @values: Byte array which will be written.
2278 * This executes the SMBus "block write" protocol, returning negative errno
2279 * else zero on success.
2281 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
2282 u8 length, const u8 *values)
2284 union i2c_smbus_data data;
2286 if (length > I2C_SMBUS_BLOCK_MAX)
2287 length = I2C_SMBUS_BLOCK_MAX;
2288 data.block[0] = length;
2289 memcpy(&data.block[1], values, length);
2290 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2291 I2C_SMBUS_WRITE, command,
2292 I2C_SMBUS_BLOCK_DATA, &data);
2294 EXPORT_SYMBOL(i2c_smbus_write_block_data);
2296 /* Returns the number of read bytes */
2297 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
2298 u8 length, u8 *values)
2300 union i2c_smbus_data data;
2303 if (length > I2C_SMBUS_BLOCK_MAX)
2304 length = I2C_SMBUS_BLOCK_MAX;
2305 data.block[0] = length;
2306 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2307 I2C_SMBUS_READ, command,
2308 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2312 memcpy(values, &data.block[1], data.block[0]);
2313 return data.block[0];
2315 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
2317 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
2318 u8 length, const u8 *values)
2320 union i2c_smbus_data data;
2322 if (length > I2C_SMBUS_BLOCK_MAX)
2323 length = I2C_SMBUS_BLOCK_MAX;
2324 data.block[0] = length;
2325 memcpy(data.block + 1, values, length);
2326 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2327 I2C_SMBUS_WRITE, command,
2328 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2330 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
2332 /* Simulate a SMBus command using the i2c protocol
2333 No checking of parameters is done! */
2334 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2335 unsigned short flags,
2336 char read_write, u8 command, int size,
2337 union i2c_smbus_data *data)
2339 /* So we need to generate a series of msgs. In the case of writing, we
2340 need to use only one message; when reading, we need two. We initialize
2341 most things with sane defaults, to keep the code below somewhat
2343 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2344 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
2345 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
2349 struct i2c_msg msg[2] = {
2357 .flags = flags | I2C_M_RD,
2363 msgbuf0[0] = command;
2365 case I2C_SMBUS_QUICK:
2367 /* Special case: The read/write field is used as data */
2368 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2372 case I2C_SMBUS_BYTE:
2373 if (read_write == I2C_SMBUS_READ) {
2374 /* Special case: only a read! */
2375 msg[0].flags = I2C_M_RD | flags;
2379 case I2C_SMBUS_BYTE_DATA:
2380 if (read_write == I2C_SMBUS_READ)
2384 msgbuf0[1] = data->byte;
2387 case I2C_SMBUS_WORD_DATA:
2388 if (read_write == I2C_SMBUS_READ)
2392 msgbuf0[1] = data->word & 0xff;
2393 msgbuf0[2] = data->word >> 8;
2396 case I2C_SMBUS_PROC_CALL:
2397 num = 2; /* Special case */
2398 read_write = I2C_SMBUS_READ;
2401 msgbuf0[1] = data->word & 0xff;
2402 msgbuf0[2] = data->word >> 8;
2404 case I2C_SMBUS_BLOCK_DATA:
2405 if (read_write == I2C_SMBUS_READ) {
2406 msg[1].flags |= I2C_M_RECV_LEN;
2407 msg[1].len = 1; /* block length will be added by
2408 the underlying bus driver */
2410 msg[0].len = data->block[0] + 2;
2411 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
2412 dev_err(&adapter->dev,
2413 "Invalid block write size %d\n",
2417 for (i = 1; i < msg[0].len; i++)
2418 msgbuf0[i] = data->block[i-1];
2421 case I2C_SMBUS_BLOCK_PROC_CALL:
2422 num = 2; /* Another special case */
2423 read_write = I2C_SMBUS_READ;
2424 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
2425 dev_err(&adapter->dev,
2426 "Invalid block write size %d\n",
2430 msg[0].len = data->block[0] + 2;
2431 for (i = 1; i < msg[0].len; i++)
2432 msgbuf0[i] = data->block[i-1];
2433 msg[1].flags |= I2C_M_RECV_LEN;
2434 msg[1].len = 1; /* block length will be added by
2435 the underlying bus driver */
2437 case I2C_SMBUS_I2C_BLOCK_DATA:
2438 if (read_write == I2C_SMBUS_READ) {
2439 msg[1].len = data->block[0];
2441 msg[0].len = data->block[0] + 1;
2442 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
2443 dev_err(&adapter->dev,
2444 "Invalid block write size %d\n",
2448 for (i = 1; i <= data->block[0]; i++)
2449 msgbuf0[i] = data->block[i];
2453 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2457 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2458 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2460 /* Compute PEC if first message is a write */
2461 if (!(msg[0].flags & I2C_M_RD)) {
2462 if (num == 1) /* Write only */
2463 i2c_smbus_add_pec(&msg[0]);
2464 else /* Write followed by read */
2465 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2467 /* Ask for PEC if last message is a read */
2468 if (msg[num-1].flags & I2C_M_RD)
2472 status = i2c_transfer(adapter, msg, num);
2476 /* Check PEC if last message is a read */
2477 if (i && (msg[num-1].flags & I2C_M_RD)) {
2478 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2483 if (read_write == I2C_SMBUS_READ)
2485 case I2C_SMBUS_BYTE:
2486 data->byte = msgbuf0[0];
2488 case I2C_SMBUS_BYTE_DATA:
2489 data->byte = msgbuf1[0];
2491 case I2C_SMBUS_WORD_DATA:
2492 case I2C_SMBUS_PROC_CALL:
2493 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2495 case I2C_SMBUS_I2C_BLOCK_DATA:
2496 for (i = 0; i < data->block[0]; i++)
2497 data->block[i+1] = msgbuf1[i];
2499 case I2C_SMBUS_BLOCK_DATA:
2500 case I2C_SMBUS_BLOCK_PROC_CALL:
2501 for (i = 0; i < msgbuf1[0] + 1; i++)
2502 data->block[i] = msgbuf1[i];
2509 * i2c_smbus_xfer - execute SMBus protocol operations
2510 * @adapter: Handle to I2C bus
2511 * @addr: Address of SMBus slave on that bus
2512 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2513 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2514 * @command: Byte interpreted by slave, for protocols which use such bytes
2515 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2516 * @data: Data to be read or written
2518 * This executes an SMBus protocol operation, and returns a negative
2519 * errno code else zero on success.
2521 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2522 char read_write, u8 command, int protocol,
2523 union i2c_smbus_data *data)
2525 unsigned long orig_jiffies;
2529 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
2531 if (adapter->algo->smbus_xfer) {
2532 i2c_lock_adapter(adapter);
2534 /* Retry automatically on arbitration loss */
2535 orig_jiffies = jiffies;
2536 for (res = 0, try = 0; try <= adapter->retries; try++) {
2537 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2538 read_write, command,
2542 if (time_after(jiffies,
2543 orig_jiffies + adapter->timeout))
2546 i2c_unlock_adapter(adapter);
2548 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2551 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2552 * implement native support for the SMBus operation.
2556 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2557 command, protocol, data);
2559 EXPORT_SYMBOL(i2c_smbus_xfer);
2561 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2562 MODULE_DESCRIPTION("I2C-Bus main module");
2563 MODULE_LICENSE("GPL");