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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* ------------------------------------------------------------------------- */
20 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/init.h>
31 #include <linux/idr.h>
32 #include <linux/mutex.h>
33 #include <linux/completion.h>
34 #include <linux/hardirq.h>
35 #include <linux/irqflags.h>
36 #include <linux/rwsem.h>
37 #include <asm/uaccess.h>
42 /* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
43 that device detection, deletion of detected devices, and attach_adapter
44 and detach_adapter calls are serialized */
45 static DEFINE_MUTEX(core_lock);
46 static DEFINE_IDR(i2c_adapter_idr);
47 static LIST_HEAD(userspace_devices);
49 static struct device_type i2c_client_type;
50 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
51 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
53 /* ------------------------------------------------------------------------- */
55 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
56 const struct i2c_client *client)
59 if (strcmp(client->name, id->name) == 0)
66 static int i2c_device_match(struct device *dev, struct device_driver *drv)
68 struct i2c_client *client = i2c_verify_client(dev);
69 struct i2c_driver *driver;
74 driver = to_i2c_driver(drv);
75 /* match on an id table if there is one */
77 return i2c_match_id(driver->id_table, client) != NULL;
84 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
85 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
87 struct i2c_client *client = to_i2c_client(dev);
89 if (add_uevent_var(env, "MODALIAS=%s%s",
90 I2C_MODULE_PREFIX, client->name))
92 dev_dbg(dev, "uevent\n");
97 #define i2c_device_uevent NULL
98 #endif /* CONFIG_HOTPLUG */
100 static int i2c_device_probe(struct device *dev)
102 struct i2c_client *client = i2c_verify_client(dev);
103 struct i2c_driver *driver;
109 driver = to_i2c_driver(dev->driver);
110 if (!driver->probe || !driver->id_table)
112 client->driver = driver;
113 if (!device_can_wakeup(&client->dev))
114 device_init_wakeup(&client->dev,
115 client->flags & I2C_CLIENT_WAKE);
116 dev_dbg(dev, "probe\n");
118 status = driver->probe(client, i2c_match_id(driver->id_table, client));
120 client->driver = NULL;
124 static int i2c_device_remove(struct device *dev)
126 struct i2c_client *client = i2c_verify_client(dev);
127 struct i2c_driver *driver;
130 if (!client || !dev->driver)
133 driver = to_i2c_driver(dev->driver);
134 if (driver->remove) {
135 dev_dbg(dev, "remove\n");
136 status = driver->remove(client);
142 client->driver = NULL;
146 static void i2c_device_shutdown(struct device *dev)
148 struct i2c_client *client = i2c_verify_client(dev);
149 struct i2c_driver *driver;
151 if (!client || !dev->driver)
153 driver = to_i2c_driver(dev->driver);
154 if (driver->shutdown)
155 driver->shutdown(client);
158 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
160 struct i2c_client *client = i2c_verify_client(dev);
161 struct i2c_driver *driver;
163 if (!client || !dev->driver)
165 driver = to_i2c_driver(dev->driver);
166 if (!driver->suspend)
168 return driver->suspend(client, mesg);
171 static int i2c_device_resume(struct device *dev)
173 struct i2c_client *client = i2c_verify_client(dev);
174 struct i2c_driver *driver;
176 if (!client || !dev->driver)
178 driver = to_i2c_driver(dev->driver);
181 return driver->resume(client);
184 static void i2c_client_dev_release(struct device *dev)
186 kfree(to_i2c_client(dev));
190 show_name(struct device *dev, struct device_attribute *attr, char *buf)
192 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
193 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
197 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
199 struct i2c_client *client = to_i2c_client(dev);
200 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
203 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
204 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
206 static struct attribute *i2c_dev_attrs[] = {
208 /* modalias helps coldplug: modprobe $(cat .../modalias) */
209 &dev_attr_modalias.attr,
213 static struct attribute_group i2c_dev_attr_group = {
214 .attrs = i2c_dev_attrs,
217 static const struct attribute_group *i2c_dev_attr_groups[] = {
222 struct bus_type i2c_bus_type = {
224 .match = i2c_device_match,
225 .probe = i2c_device_probe,
226 .remove = i2c_device_remove,
227 .shutdown = i2c_device_shutdown,
228 .suspend = i2c_device_suspend,
229 .resume = i2c_device_resume,
231 EXPORT_SYMBOL_GPL(i2c_bus_type);
233 static struct device_type i2c_client_type = {
234 .groups = i2c_dev_attr_groups,
235 .uevent = i2c_device_uevent,
236 .release = i2c_client_dev_release,
241 * i2c_verify_client - return parameter as i2c_client, or NULL
242 * @dev: device, probably from some driver model iterator
244 * When traversing the driver model tree, perhaps using driver model
245 * iterators like @device_for_each_child(), you can't assume very much
246 * about the nodes you find. Use this function to avoid oopses caused
247 * by wrongly treating some non-I2C device as an i2c_client.
249 struct i2c_client *i2c_verify_client(struct device *dev)
251 return (dev->type == &i2c_client_type)
255 EXPORT_SYMBOL(i2c_verify_client);
259 * i2c_new_device - instantiate an i2c device
260 * @adap: the adapter managing the device
261 * @info: describes one I2C device; bus_num is ignored
264 * Create an i2c device. Binding is handled through driver model
265 * probe()/remove() methods. A driver may be bound to this device when we
266 * return from this function, or any later moment (e.g. maybe hotplugging will
267 * load the driver module). This call is not appropriate for use by mainboard
268 * initialization logic, which usually runs during an arch_initcall() long
269 * before any i2c_adapter could exist.
271 * This returns the new i2c client, which may be saved for later use with
272 * i2c_unregister_device(); or NULL to indicate an error.
275 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
277 struct i2c_client *client;
280 client = kzalloc(sizeof *client, GFP_KERNEL);
284 client->adapter = adap;
286 client->dev.platform_data = info->platform_data;
289 client->dev.archdata = *info->archdata;
291 client->flags = info->flags;
292 client->addr = info->addr;
293 client->irq = info->irq;
295 strlcpy(client->name, info->type, sizeof(client->name));
297 /* Check for address business */
298 status = i2c_check_addr(adap, client->addr);
302 client->dev.parent = &client->adapter->dev;
303 client->dev.bus = &i2c_bus_type;
304 client->dev.type = &i2c_client_type;
306 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
308 status = device_register(&client->dev);
312 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
313 client->name, dev_name(&client->dev));
318 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
319 "(%d)\n", client->name, client->addr, status);
323 EXPORT_SYMBOL_GPL(i2c_new_device);
327 * i2c_unregister_device - reverse effect of i2c_new_device()
328 * @client: value returned from i2c_new_device()
331 void i2c_unregister_device(struct i2c_client *client)
333 device_unregister(&client->dev);
335 EXPORT_SYMBOL_GPL(i2c_unregister_device);
338 static const struct i2c_device_id dummy_id[] = {
343 static int dummy_probe(struct i2c_client *client,
344 const struct i2c_device_id *id)
349 static int dummy_remove(struct i2c_client *client)
354 static struct i2c_driver dummy_driver = {
355 .driver.name = "dummy",
356 .probe = dummy_probe,
357 .remove = dummy_remove,
358 .id_table = dummy_id,
362 * i2c_new_dummy - return a new i2c device bound to a dummy driver
363 * @adapter: the adapter managing the device
364 * @address: seven bit address to be used
367 * This returns an I2C client bound to the "dummy" driver, intended for use
368 * with devices that consume multiple addresses. Examples of such chips
369 * include various EEPROMS (like 24c04 and 24c08 models).
371 * These dummy devices have two main uses. First, most I2C and SMBus calls
372 * except i2c_transfer() need a client handle; the dummy will be that handle.
373 * And second, this prevents the specified address from being bound to a
376 * This returns the new i2c client, which should be saved for later use with
377 * i2c_unregister_device(); or NULL to indicate an error.
379 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
381 struct i2c_board_info info = {
382 I2C_BOARD_INFO("dummy", address),
385 return i2c_new_device(adapter, &info);
387 EXPORT_SYMBOL_GPL(i2c_new_dummy);
389 /* ------------------------------------------------------------------------- */
391 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
393 static void i2c_adapter_dev_release(struct device *dev)
395 struct i2c_adapter *adap = to_i2c_adapter(dev);
396 complete(&adap->dev_released);
400 * Let users instantiate I2C devices through sysfs. This can be used when
401 * platform initialization code doesn't contain the proper data for
402 * whatever reason. Also useful for drivers that do device detection and
403 * detection fails, either because the device uses an unexpected address,
404 * or this is a compatible device with different ID register values.
406 * Parameter checking may look overzealous, but we really don't want
407 * the user to provide incorrect parameters.
410 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
411 const char *buf, size_t count)
413 struct i2c_adapter *adap = to_i2c_adapter(dev);
414 struct i2c_board_info info;
415 struct i2c_client *client;
419 dev_warn(dev, "The new_device interface is still experimental "
420 "and may change in a near future\n");
421 memset(&info, 0, sizeof(struct i2c_board_info));
423 blank = strchr(buf, ' ');
425 dev_err(dev, "%s: Missing parameters\n", "new_device");
428 if (blank - buf > I2C_NAME_SIZE - 1) {
429 dev_err(dev, "%s: Invalid device name\n", "new_device");
432 memcpy(info.type, buf, blank - buf);
434 /* Parse remaining parameters, reject extra parameters */
435 res = sscanf(++blank, "%hi%c", &info.addr, &end);
437 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
440 if (res > 1 && end != '\n') {
441 dev_err(dev, "%s: Extra parameters\n", "new_device");
445 if (info.addr < 0x03 || info.addr > 0x77) {
446 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
451 client = i2c_new_device(adap, &info);
455 /* Keep track of the added device */
456 mutex_lock(&core_lock);
457 list_add_tail(&client->detected, &userspace_devices);
458 mutex_unlock(&core_lock);
459 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
460 info.type, info.addr);
466 * And of course let the users delete the devices they instantiated, if
467 * they got it wrong. This interface can only be used to delete devices
468 * instantiated by i2c_sysfs_new_device above. This guarantees that we
469 * don't delete devices to which some kernel code still has references.
471 * Parameter checking may look overzealous, but we really don't want
472 * the user to delete the wrong device.
475 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
478 struct i2c_adapter *adap = to_i2c_adapter(dev);
479 struct i2c_client *client, *next;
484 /* Parse parameters, reject extra parameters */
485 res = sscanf(buf, "%hi%c", &addr, &end);
487 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
490 if (res > 1 && end != '\n') {
491 dev_err(dev, "%s: Extra parameters\n", "delete_device");
495 /* Make sure the device was added through sysfs */
497 mutex_lock(&core_lock);
498 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
499 if (client->addr == addr && client->adapter == adap) {
500 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
501 "delete_device", client->name, client->addr);
503 list_del(&client->detected);
504 i2c_unregister_device(client);
509 mutex_unlock(&core_lock);
512 dev_err(dev, "%s: Can't find device in list\n",
517 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
518 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
520 static struct attribute *i2c_adapter_attrs[] = {
522 &dev_attr_new_device.attr,
523 &dev_attr_delete_device.attr,
527 static struct attribute_group i2c_adapter_attr_group = {
528 .attrs = i2c_adapter_attrs,
531 static const struct attribute_group *i2c_adapter_attr_groups[] = {
532 &i2c_adapter_attr_group,
536 static struct device_type i2c_adapter_type = {
537 .groups = i2c_adapter_attr_groups,
538 .release = i2c_adapter_dev_release,
541 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
543 struct i2c_devinfo *devinfo;
545 down_read(&__i2c_board_lock);
546 list_for_each_entry(devinfo, &__i2c_board_list, list) {
547 if (devinfo->busnum == adapter->nr
548 && !i2c_new_device(adapter,
549 &devinfo->board_info))
550 dev_err(&adapter->dev,
551 "Can't create device at 0x%02x\n",
552 devinfo->board_info.addr);
554 up_read(&__i2c_board_lock);
557 static int i2c_do_add_adapter(struct device_driver *d, void *data)
559 struct i2c_driver *driver = to_i2c_driver(d);
560 struct i2c_adapter *adap = data;
562 /* Detect supported devices on that bus, and instantiate them */
563 i2c_detect(adap, driver);
565 /* Let legacy drivers scan this bus for matching devices */
566 if (driver->attach_adapter) {
567 /* We ignore the return code; if it fails, too bad */
568 driver->attach_adapter(adap);
573 static int i2c_register_adapter(struct i2c_adapter *adap)
577 /* Can't register until after driver model init */
578 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
583 mutex_init(&adap->bus_lock);
585 /* Set default timeout to 1 second if not already set */
586 if (adap->timeout == 0)
589 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
590 adap->dev.bus = &i2c_bus_type;
591 adap->dev.type = &i2c_adapter_type;
592 res = device_register(&adap->dev);
596 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
598 /* create pre-declared device nodes */
599 if (adap->nr < __i2c_first_dynamic_bus_num)
600 i2c_scan_static_board_info(adap);
603 mutex_lock(&core_lock);
604 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
606 mutex_unlock(&core_lock);
611 mutex_lock(&core_lock);
612 idr_remove(&i2c_adapter_idr, adap->nr);
613 mutex_unlock(&core_lock);
618 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
619 * @adapter: the adapter to add
622 * This routine is used to declare an I2C adapter when its bus number
623 * doesn't matter. Examples: for I2C adapters dynamically added by
624 * USB links or PCI plugin cards.
626 * When this returns zero, a new bus number was allocated and stored
627 * in adap->nr, and the specified adapter became available for clients.
628 * Otherwise, a negative errno value is returned.
630 int i2c_add_adapter(struct i2c_adapter *adapter)
635 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
638 mutex_lock(&core_lock);
639 /* "above" here means "above or equal to", sigh */
640 res = idr_get_new_above(&i2c_adapter_idr, adapter,
641 __i2c_first_dynamic_bus_num, &id);
642 mutex_unlock(&core_lock);
651 return i2c_register_adapter(adapter);
653 EXPORT_SYMBOL(i2c_add_adapter);
656 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
657 * @adap: the adapter to register (with adap->nr initialized)
660 * This routine is used to declare an I2C adapter when its bus number
661 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
662 * or otherwise built in to the system's mainboard, and where i2c_board_info
663 * is used to properly configure I2C devices.
665 * If no devices have pre-been declared for this bus, then be sure to
666 * register the adapter before any dynamically allocated ones. Otherwise
667 * the required bus ID may not be available.
669 * When this returns zero, the specified adapter became available for
670 * clients using the bus number provided in adap->nr. Also, the table
671 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
672 * and the appropriate driver model device nodes are created. Otherwise, a
673 * negative errno value is returned.
675 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
680 if (adap->nr & ~MAX_ID_MASK)
684 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
687 mutex_lock(&core_lock);
688 /* "above" here means "above or equal to", sigh;
689 * we need the "equal to" result to force the result
691 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
692 if (status == 0 && id != adap->nr) {
694 idr_remove(&i2c_adapter_idr, id);
696 mutex_unlock(&core_lock);
697 if (status == -EAGAIN)
701 status = i2c_register_adapter(adap);
704 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
706 static int i2c_do_del_adapter(struct device_driver *d, void *data)
708 struct i2c_driver *driver = to_i2c_driver(d);
709 struct i2c_adapter *adapter = data;
710 struct i2c_client *client, *_n;
713 /* Remove the devices we created ourselves as the result of hardware
714 * probing (using a driver's detect method) */
715 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
716 if (client->adapter == adapter) {
717 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
718 client->name, client->addr);
719 list_del(&client->detected);
720 i2c_unregister_device(client);
724 if (!driver->detach_adapter)
726 res = driver->detach_adapter(adapter);
728 dev_err(&adapter->dev, "detach_adapter failed (%d) "
729 "for driver [%s]\n", res, driver->driver.name);
733 static int __unregister_client(struct device *dev, void *dummy)
735 struct i2c_client *client = i2c_verify_client(dev);
737 i2c_unregister_device(client);
742 * i2c_del_adapter - unregister I2C adapter
743 * @adap: the adapter being unregistered
746 * This unregisters an I2C adapter which was previously registered
747 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
749 int i2c_del_adapter(struct i2c_adapter *adap)
752 struct i2c_adapter *found;
754 /* First make sure that this adapter was ever added */
755 mutex_lock(&core_lock);
756 found = idr_find(&i2c_adapter_idr, adap->nr);
757 mutex_unlock(&core_lock);
759 pr_debug("i2c-core: attempting to delete unregistered "
760 "adapter [%s]\n", adap->name);
764 /* Tell drivers about this removal */
765 mutex_lock(&core_lock);
766 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
768 mutex_unlock(&core_lock);
772 /* Detach any active clients. This can't fail, thus we do not
773 checking the returned value. */
774 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
776 /* clean up the sysfs representation */
777 init_completion(&adap->dev_released);
778 device_unregister(&adap->dev);
780 /* wait for sysfs to drop all references */
781 wait_for_completion(&adap->dev_released);
784 mutex_lock(&core_lock);
785 idr_remove(&i2c_adapter_idr, adap->nr);
786 mutex_unlock(&core_lock);
788 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
790 /* Clear the device structure in case this adapter is ever going to be
792 memset(&adap->dev, 0, sizeof(adap->dev));
796 EXPORT_SYMBOL(i2c_del_adapter);
799 /* ------------------------------------------------------------------------- */
801 static int __attach_adapter(struct device *dev, void *data)
803 struct i2c_adapter *adapter;
804 struct i2c_driver *driver = data;
806 if (dev->type != &i2c_adapter_type)
808 adapter = to_i2c_adapter(dev);
810 i2c_detect(adapter, driver);
812 /* Legacy drivers scan i2c busses directly */
813 if (driver->attach_adapter)
814 driver->attach_adapter(adapter);
820 * An i2c_driver is used with one or more i2c_client (device) nodes to access
821 * i2c slave chips, on a bus instance associated with some i2c_adapter.
824 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
828 /* Can't register until after driver model init */
829 if (unlikely(WARN_ON(!i2c_bus_type.p)))
832 /* add the driver to the list of i2c drivers in the driver core */
833 driver->driver.owner = owner;
834 driver->driver.bus = &i2c_bus_type;
836 /* When registration returns, the driver core
837 * will have called probe() for all matching-but-unbound devices.
839 res = driver_register(&driver->driver);
843 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
845 INIT_LIST_HEAD(&driver->clients);
846 /* Walk the adapters that are already present */
847 mutex_lock(&core_lock);
848 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter);
849 mutex_unlock(&core_lock);
853 EXPORT_SYMBOL(i2c_register_driver);
855 static int __detach_adapter(struct device *dev, void *data)
857 struct i2c_adapter *adapter;
858 struct i2c_driver *driver = data;
859 struct i2c_client *client, *_n;
861 if (dev->type != &i2c_adapter_type)
863 adapter = to_i2c_adapter(dev);
865 /* Remove the devices we created ourselves as the result of hardware
866 * probing (using a driver's detect method) */
867 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
868 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
869 client->name, client->addr);
870 list_del(&client->detected);
871 i2c_unregister_device(client);
874 if (driver->detach_adapter) {
875 if (driver->detach_adapter(adapter))
876 dev_err(&adapter->dev,
877 "detach_adapter failed for driver [%s]\n",
878 driver->driver.name);
885 * i2c_del_driver - unregister I2C driver
886 * @driver: the driver being unregistered
889 void i2c_del_driver(struct i2c_driver *driver)
891 mutex_lock(&core_lock);
892 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter);
893 mutex_unlock(&core_lock);
895 driver_unregister(&driver->driver);
896 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
898 EXPORT_SYMBOL(i2c_del_driver);
900 /* ------------------------------------------------------------------------- */
902 static int __i2c_check_addr(struct device *dev, void *addrp)
904 struct i2c_client *client = i2c_verify_client(dev);
905 int addr = *(int *)addrp;
907 if (client && client->addr == addr)
912 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
914 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
918 * i2c_use_client - increments the reference count of the i2c client structure
919 * @client: the client being referenced
921 * Each live reference to a client should be refcounted. The driver model does
922 * that automatically as part of driver binding, so that most drivers don't
923 * need to do this explicitly: they hold a reference until they're unbound
926 * A pointer to the client with the incremented reference counter is returned.
928 struct i2c_client *i2c_use_client(struct i2c_client *client)
930 if (client && get_device(&client->dev))
934 EXPORT_SYMBOL(i2c_use_client);
937 * i2c_release_client - release a use of the i2c client structure
938 * @client: the client being no longer referenced
940 * Must be called when a user of a client is finished with it.
942 void i2c_release_client(struct i2c_client *client)
945 put_device(&client->dev);
947 EXPORT_SYMBOL(i2c_release_client);
954 static int i2c_cmd(struct device *dev, void *_arg)
956 struct i2c_client *client = i2c_verify_client(dev);
957 struct i2c_cmd_arg *arg = _arg;
959 if (client && client->driver && client->driver->command)
960 client->driver->command(client, arg->cmd, arg->arg);
964 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
966 struct i2c_cmd_arg cmd_arg;
970 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
972 EXPORT_SYMBOL(i2c_clients_command);
974 static int __init i2c_init(void)
978 retval = bus_register(&i2c_bus_type);
981 retval = i2c_add_driver(&dummy_driver);
987 bus_unregister(&i2c_bus_type);
991 static void __exit i2c_exit(void)
993 i2c_del_driver(&dummy_driver);
994 bus_unregister(&i2c_bus_type);
997 /* We must initialize early, because some subsystems register i2c drivers
998 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1000 postcore_initcall(i2c_init);
1001 module_exit(i2c_exit);
1003 /* ----------------------------------------------------
1004 * the functional interface to the i2c busses.
1005 * ----------------------------------------------------
1009 * i2c_transfer - execute a single or combined I2C message
1010 * @adap: Handle to I2C bus
1011 * @msgs: One or more messages to execute before STOP is issued to
1012 * terminate the operation; each message begins with a START.
1013 * @num: Number of messages to be executed.
1015 * Returns negative errno, else the number of messages executed.
1017 * Note that there is no requirement that each message be sent to
1018 * the same slave address, although that is the most common model.
1020 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1022 unsigned long orig_jiffies;
1025 /* REVISIT the fault reporting model here is weak:
1027 * - When we get an error after receiving N bytes from a slave,
1028 * there is no way to report "N".
1030 * - When we get a NAK after transmitting N bytes to a slave,
1031 * there is no way to report "N" ... or to let the master
1032 * continue executing the rest of this combined message, if
1033 * that's the appropriate response.
1035 * - When for example "num" is two and we successfully complete
1036 * the first message but get an error part way through the
1037 * second, it's unclear whether that should be reported as
1038 * one (discarding status on the second message) or errno
1039 * (discarding status on the first one).
1042 if (adap->algo->master_xfer) {
1044 for (ret = 0; ret < num; ret++) {
1045 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1046 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1047 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1048 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1052 if (in_atomic() || irqs_disabled()) {
1053 ret = mutex_trylock(&adap->bus_lock);
1055 /* I2C activity is ongoing. */
1058 mutex_lock_nested(&adap->bus_lock, adap->level);
1061 /* Retry automatically on arbitration loss */
1062 orig_jiffies = jiffies;
1063 for (ret = 0, try = 0; try <= adap->retries; try++) {
1064 ret = adap->algo->master_xfer(adap, msgs, num);
1067 if (time_after(jiffies, orig_jiffies + adap->timeout))
1070 mutex_unlock(&adap->bus_lock);
1074 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1078 EXPORT_SYMBOL(i2c_transfer);
1081 * i2c_master_send - issue a single I2C message in master transmit mode
1082 * @client: Handle to slave device
1083 * @buf: Data that will be written to the slave
1084 * @count: How many bytes to write
1086 * Returns negative errno, or else the number of bytes written.
1088 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1091 struct i2c_adapter *adap=client->adapter;
1094 msg.addr = client->addr;
1095 msg.flags = client->flags & I2C_M_TEN;
1097 msg.buf = (char *)buf;
1099 ret = i2c_transfer(adap, &msg, 1);
1101 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1102 transmitted, else error code. */
1103 return (ret == 1) ? count : ret;
1105 EXPORT_SYMBOL(i2c_master_send);
1108 * i2c_master_recv - issue a single I2C message in master receive mode
1109 * @client: Handle to slave device
1110 * @buf: Where to store data read from slave
1111 * @count: How many bytes to read
1113 * Returns negative errno, or else the number of bytes read.
1115 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1117 struct i2c_adapter *adap=client->adapter;
1121 msg.addr = client->addr;
1122 msg.flags = client->flags & I2C_M_TEN;
1123 msg.flags |= I2C_M_RD;
1127 ret = i2c_transfer(adap, &msg, 1);
1129 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1130 transmitted, else error code. */
1131 return (ret == 1) ? count : ret;
1133 EXPORT_SYMBOL(i2c_master_recv);
1135 /* ----------------------------------------------------
1136 * the i2c address scanning function
1137 * Will not work for 10-bit addresses!
1138 * ----------------------------------------------------
1141 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1142 struct i2c_driver *driver)
1144 struct i2c_board_info info;
1145 struct i2c_adapter *adapter = temp_client->adapter;
1146 int addr = temp_client->addr;
1149 /* Make sure the address is valid */
1150 if (addr < 0x03 || addr > 0x77) {
1151 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1156 /* Skip if already in use */
1157 if (i2c_check_addr(adapter, addr))
1160 /* Make sure there is something at this address, unless forced */
1162 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1163 I2C_SMBUS_QUICK, NULL) < 0)
1166 /* prevent 24RF08 corruption */
1167 if ((addr & ~0x0f) == 0x50)
1168 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1169 I2C_SMBUS_QUICK, NULL);
1172 /* Finally call the custom detection function */
1173 memset(&info, 0, sizeof(struct i2c_board_info));
1175 err = driver->detect(temp_client, kind, &info);
1177 /* -ENODEV is returned if the detection fails. We catch it
1178 here as this isn't an error. */
1179 return err == -ENODEV ? 0 : err;
1182 /* Consistency check */
1183 if (info.type[0] == '\0') {
1184 dev_err(&adapter->dev, "%s detection function provided "
1185 "no name for 0x%x\n", driver->driver.name,
1188 struct i2c_client *client;
1190 /* Detection succeeded, instantiate the device */
1191 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1192 info.type, info.addr);
1193 client = i2c_new_device(adapter, &info);
1195 list_add_tail(&client->detected, &driver->clients);
1197 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1198 info.type, info.addr);
1203 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1205 const struct i2c_client_address_data *address_data;
1206 struct i2c_client *temp_client;
1208 int adap_id = i2c_adapter_id(adapter);
1210 address_data = driver->address_data;
1211 if (!driver->detect || !address_data)
1214 /* Set up a temporary client to help detect callback */
1215 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1218 temp_client->adapter = adapter;
1220 /* Force entries are done first, and are not affected by ignore
1222 if (address_data->forces) {
1223 const unsigned short * const *forces = address_data->forces;
1226 for (kind = 0; forces[kind]; kind++) {
1227 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1229 if (forces[kind][i] == adap_id
1230 || forces[kind][i] == ANY_I2C_BUS) {
1231 dev_dbg(&adapter->dev, "found force "
1232 "parameter for adapter %d, "
1233 "addr 0x%02x, kind %d\n",
1234 adap_id, forces[kind][i + 1],
1236 temp_client->addr = forces[kind][i + 1];
1237 err = i2c_detect_address(temp_client,
1246 /* Stop here if the classes do not match */
1247 if (!(adapter->class & driver->class))
1250 /* Stop here if we can't use SMBUS_QUICK */
1251 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1252 if (address_data->probe[0] == I2C_CLIENT_END
1253 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1256 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1257 "can't probe for chips\n");
1262 /* Probe entries are done second, and are not affected by ignore
1264 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1265 if (address_data->probe[i] == adap_id
1266 || address_data->probe[i] == ANY_I2C_BUS) {
1267 dev_dbg(&adapter->dev, "found probe parameter for "
1268 "adapter %d, addr 0x%02x\n", adap_id,
1269 address_data->probe[i + 1]);
1270 temp_client->addr = address_data->probe[i + 1];
1271 err = i2c_detect_address(temp_client, -1, driver);
1277 /* Normal entries are done last, unless shadowed by an ignore entry */
1278 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1282 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1284 if ((address_data->ignore[j] == adap_id ||
1285 address_data->ignore[j] == ANY_I2C_BUS)
1286 && address_data->ignore[j + 1]
1287 == address_data->normal_i2c[i]) {
1288 dev_dbg(&adapter->dev, "found ignore "
1289 "parameter for adapter %d, "
1290 "addr 0x%02x\n", adap_id,
1291 address_data->ignore[j + 1]);
1299 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1300 "addr 0x%02x\n", adap_id,
1301 address_data->normal_i2c[i]);
1302 temp_client->addr = address_data->normal_i2c[i];
1303 err = i2c_detect_address(temp_client, -1, driver);
1314 i2c_new_probed_device(struct i2c_adapter *adap,
1315 struct i2c_board_info *info,
1316 unsigned short const *addr_list)
1320 /* Stop here if the bus doesn't support probing */
1321 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1322 dev_err(&adap->dev, "Probing not supported\n");
1326 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1327 /* Check address validity */
1328 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1329 dev_warn(&adap->dev, "Invalid 7-bit address "
1330 "0x%02x\n", addr_list[i]);
1334 /* Check address availability */
1335 if (i2c_check_addr(adap, addr_list[i])) {
1336 dev_dbg(&adap->dev, "Address 0x%02x already in "
1337 "use, not probing\n", addr_list[i]);
1341 /* Test address responsiveness
1342 The default probe method is a quick write, but it is known
1343 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1344 and could also irreversibly write-protect some EEPROMs, so
1345 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1346 read instead. Also, some bus drivers don't implement
1347 quick write, so we fallback to a byte read it that case
1349 if ((addr_list[i] & ~0x07) == 0x30
1350 || (addr_list[i] & ~0x0f) == 0x50
1351 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1352 union i2c_smbus_data data;
1354 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1356 I2C_SMBUS_BYTE, &data) >= 0)
1359 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1361 I2C_SMBUS_QUICK, NULL) >= 0)
1366 if (addr_list[i] == I2C_CLIENT_END) {
1367 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1371 info->addr = addr_list[i];
1372 return i2c_new_device(adap, info);
1374 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1376 struct i2c_adapter* i2c_get_adapter(int id)
1378 struct i2c_adapter *adapter;
1380 mutex_lock(&core_lock);
1381 adapter = idr_find(&i2c_adapter_idr, id);
1382 if (adapter && !try_module_get(adapter->owner))
1385 mutex_unlock(&core_lock);
1388 EXPORT_SYMBOL(i2c_get_adapter);
1390 void i2c_put_adapter(struct i2c_adapter *adap)
1392 module_put(adap->owner);
1394 EXPORT_SYMBOL(i2c_put_adapter);
1396 /* The SMBus parts */
1398 #define POLY (0x1070U << 3)
1399 static u8 crc8(u16 data)
1403 for(i = 0; i < 8; i++) {
1408 return (u8)(data >> 8);
1411 /* Incremental CRC8 over count bytes in the array pointed to by p */
1412 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1416 for(i = 0; i < count; i++)
1417 crc = crc8((crc ^ p[i]) << 8);
1421 /* Assume a 7-bit address, which is reasonable for SMBus */
1422 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1424 /* The address will be sent first */
1425 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1426 pec = i2c_smbus_pec(pec, &addr, 1);
1428 /* The data buffer follows */
1429 return i2c_smbus_pec(pec, msg->buf, msg->len);
1432 /* Used for write only transactions */
1433 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1435 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1439 /* Return <0 on CRC error
1440 If there was a write before this read (most cases) we need to take the
1441 partial CRC from the write part into account.
1442 Note that this function does modify the message (we need to decrease the
1443 message length to hide the CRC byte from the caller). */
1444 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1446 u8 rpec = msg->buf[--msg->len];
1447 cpec = i2c_smbus_msg_pec(cpec, msg);
1450 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1458 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1459 * @client: Handle to slave device
1461 * This executes the SMBus "receive byte" protocol, returning negative errno
1462 * else the byte received from the device.
1464 s32 i2c_smbus_read_byte(struct i2c_client *client)
1466 union i2c_smbus_data data;
1469 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1471 I2C_SMBUS_BYTE, &data);
1472 return (status < 0) ? status : data.byte;
1474 EXPORT_SYMBOL(i2c_smbus_read_byte);
1477 * i2c_smbus_write_byte - SMBus "send byte" protocol
1478 * @client: Handle to slave device
1479 * @value: Byte to be sent
1481 * This executes the SMBus "send byte" protocol, returning negative errno
1482 * else zero on success.
1484 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1486 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1487 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1489 EXPORT_SYMBOL(i2c_smbus_write_byte);
1492 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1493 * @client: Handle to slave device
1494 * @command: Byte interpreted by slave
1496 * This executes the SMBus "read byte" protocol, returning negative errno
1497 * else a data byte received from the device.
1499 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1501 union i2c_smbus_data data;
1504 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1505 I2C_SMBUS_READ, command,
1506 I2C_SMBUS_BYTE_DATA, &data);
1507 return (status < 0) ? status : data.byte;
1509 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1512 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1513 * @client: Handle to slave device
1514 * @command: Byte interpreted by slave
1515 * @value: Byte being written
1517 * This executes the SMBus "write byte" protocol, returning negative errno
1518 * else zero on success.
1520 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1522 union i2c_smbus_data data;
1524 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1525 I2C_SMBUS_WRITE,command,
1526 I2C_SMBUS_BYTE_DATA,&data);
1528 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1531 * i2c_smbus_read_word_data - SMBus "read word" protocol
1532 * @client: Handle to slave device
1533 * @command: Byte interpreted by slave
1535 * This executes the SMBus "read word" protocol, returning negative errno
1536 * else a 16-bit unsigned "word" received from the device.
1538 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1540 union i2c_smbus_data data;
1543 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1544 I2C_SMBUS_READ, command,
1545 I2C_SMBUS_WORD_DATA, &data);
1546 return (status < 0) ? status : data.word;
1548 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1551 * i2c_smbus_write_word_data - SMBus "write word" protocol
1552 * @client: Handle to slave device
1553 * @command: Byte interpreted by slave
1554 * @value: 16-bit "word" being written
1556 * This executes the SMBus "write word" protocol, returning negative errno
1557 * else zero on success.
1559 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1561 union i2c_smbus_data data;
1563 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1564 I2C_SMBUS_WRITE,command,
1565 I2C_SMBUS_WORD_DATA,&data);
1567 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1570 * i2c_smbus_process_call - SMBus "process call" protocol
1571 * @client: Handle to slave device
1572 * @command: Byte interpreted by slave
1573 * @value: 16-bit "word" being written
1575 * This executes the SMBus "process call" protocol, returning negative errno
1576 * else a 16-bit unsigned "word" received from the device.
1578 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1580 union i2c_smbus_data data;
1584 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1585 I2C_SMBUS_WRITE, command,
1586 I2C_SMBUS_PROC_CALL, &data);
1587 return (status < 0) ? status : data.word;
1589 EXPORT_SYMBOL(i2c_smbus_process_call);
1592 * i2c_smbus_read_block_data - SMBus "block read" protocol
1593 * @client: Handle to slave device
1594 * @command: Byte interpreted by slave
1595 * @values: Byte array into which data will be read; big enough to hold
1596 * the data returned by the slave. SMBus allows at most 32 bytes.
1598 * This executes the SMBus "block read" protocol, returning negative errno
1599 * else the number of data bytes in the slave's response.
1601 * Note that using this function requires that the client's adapter support
1602 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1603 * support this; its emulation through I2C messaging relies on a specific
1604 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1606 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1609 union i2c_smbus_data data;
1612 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1613 I2C_SMBUS_READ, command,
1614 I2C_SMBUS_BLOCK_DATA, &data);
1618 memcpy(values, &data.block[1], data.block[0]);
1619 return data.block[0];
1621 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1624 * i2c_smbus_write_block_data - SMBus "block write" protocol
1625 * @client: Handle to slave device
1626 * @command: Byte interpreted by slave
1627 * @length: Size of data block; SMBus allows at most 32 bytes
1628 * @values: Byte array which will be written.
1630 * This executes the SMBus "block write" protocol, returning negative errno
1631 * else zero on success.
1633 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1634 u8 length, const u8 *values)
1636 union i2c_smbus_data data;
1638 if (length > I2C_SMBUS_BLOCK_MAX)
1639 length = I2C_SMBUS_BLOCK_MAX;
1640 data.block[0] = length;
1641 memcpy(&data.block[1], values, length);
1642 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1643 I2C_SMBUS_WRITE,command,
1644 I2C_SMBUS_BLOCK_DATA,&data);
1646 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1648 /* Returns the number of read bytes */
1649 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1650 u8 length, u8 *values)
1652 union i2c_smbus_data data;
1655 if (length > I2C_SMBUS_BLOCK_MAX)
1656 length = I2C_SMBUS_BLOCK_MAX;
1657 data.block[0] = length;
1658 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1659 I2C_SMBUS_READ, command,
1660 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1664 memcpy(values, &data.block[1], data.block[0]);
1665 return data.block[0];
1667 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1669 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1670 u8 length, const u8 *values)
1672 union i2c_smbus_data data;
1674 if (length > I2C_SMBUS_BLOCK_MAX)
1675 length = I2C_SMBUS_BLOCK_MAX;
1676 data.block[0] = length;
1677 memcpy(data.block + 1, values, length);
1678 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1679 I2C_SMBUS_WRITE, command,
1680 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1682 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1684 /* Simulate a SMBus command using the i2c protocol
1685 No checking of parameters is done! */
1686 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1687 unsigned short flags,
1688 char read_write, u8 command, int size,
1689 union i2c_smbus_data * data)
1691 /* So we need to generate a series of msgs. In the case of writing, we
1692 need to use only one message; when reading, we need two. We initialize
1693 most things with sane defaults, to keep the code below somewhat
1695 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1696 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1697 int num = read_write == I2C_SMBUS_READ?2:1;
1698 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1699 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1705 msgbuf0[0] = command;
1707 case I2C_SMBUS_QUICK:
1709 /* Special case: The read/write field is used as data */
1710 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1714 case I2C_SMBUS_BYTE:
1715 if (read_write == I2C_SMBUS_READ) {
1716 /* Special case: only a read! */
1717 msg[0].flags = I2C_M_RD | flags;
1721 case I2C_SMBUS_BYTE_DATA:
1722 if (read_write == I2C_SMBUS_READ)
1726 msgbuf0[1] = data->byte;
1729 case I2C_SMBUS_WORD_DATA:
1730 if (read_write == I2C_SMBUS_READ)
1734 msgbuf0[1] = data->word & 0xff;
1735 msgbuf0[2] = data->word >> 8;
1738 case I2C_SMBUS_PROC_CALL:
1739 num = 2; /* Special case */
1740 read_write = I2C_SMBUS_READ;
1743 msgbuf0[1] = data->word & 0xff;
1744 msgbuf0[2] = data->word >> 8;
1746 case I2C_SMBUS_BLOCK_DATA:
1747 if (read_write == I2C_SMBUS_READ) {
1748 msg[1].flags |= I2C_M_RECV_LEN;
1749 msg[1].len = 1; /* block length will be added by
1750 the underlying bus driver */
1752 msg[0].len = data->block[0] + 2;
1753 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1754 dev_err(&adapter->dev,
1755 "Invalid block write size %d\n",
1759 for (i = 1; i < msg[0].len; i++)
1760 msgbuf0[i] = data->block[i-1];
1763 case I2C_SMBUS_BLOCK_PROC_CALL:
1764 num = 2; /* Another special case */
1765 read_write = I2C_SMBUS_READ;
1766 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1767 dev_err(&adapter->dev,
1768 "Invalid block write size %d\n",
1772 msg[0].len = data->block[0] + 2;
1773 for (i = 1; i < msg[0].len; i++)
1774 msgbuf0[i] = data->block[i-1];
1775 msg[1].flags |= I2C_M_RECV_LEN;
1776 msg[1].len = 1; /* block length will be added by
1777 the underlying bus driver */
1779 case I2C_SMBUS_I2C_BLOCK_DATA:
1780 if (read_write == I2C_SMBUS_READ) {
1781 msg[1].len = data->block[0];
1783 msg[0].len = data->block[0] + 1;
1784 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1785 dev_err(&adapter->dev,
1786 "Invalid block write size %d\n",
1790 for (i = 1; i <= data->block[0]; i++)
1791 msgbuf0[i] = data->block[i];
1795 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1799 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1800 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1802 /* Compute PEC if first message is a write */
1803 if (!(msg[0].flags & I2C_M_RD)) {
1804 if (num == 1) /* Write only */
1805 i2c_smbus_add_pec(&msg[0]);
1806 else /* Write followed by read */
1807 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1809 /* Ask for PEC if last message is a read */
1810 if (msg[num-1].flags & I2C_M_RD)
1814 status = i2c_transfer(adapter, msg, num);
1818 /* Check PEC if last message is a read */
1819 if (i && (msg[num-1].flags & I2C_M_RD)) {
1820 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1825 if (read_write == I2C_SMBUS_READ)
1827 case I2C_SMBUS_BYTE:
1828 data->byte = msgbuf0[0];
1830 case I2C_SMBUS_BYTE_DATA:
1831 data->byte = msgbuf1[0];
1833 case I2C_SMBUS_WORD_DATA:
1834 case I2C_SMBUS_PROC_CALL:
1835 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1837 case I2C_SMBUS_I2C_BLOCK_DATA:
1838 for (i = 0; i < data->block[0]; i++)
1839 data->block[i+1] = msgbuf1[i];
1841 case I2C_SMBUS_BLOCK_DATA:
1842 case I2C_SMBUS_BLOCK_PROC_CALL:
1843 for (i = 0; i < msgbuf1[0] + 1; i++)
1844 data->block[i] = msgbuf1[i];
1851 * i2c_smbus_xfer - execute SMBus protocol operations
1852 * @adapter: Handle to I2C bus
1853 * @addr: Address of SMBus slave on that bus
1854 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1855 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1856 * @command: Byte interpreted by slave, for protocols which use such bytes
1857 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1858 * @data: Data to be read or written
1860 * This executes an SMBus protocol operation, and returns a negative
1861 * errno code else zero on success.
1863 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1864 char read_write, u8 command, int protocol,
1865 union i2c_smbus_data *data)
1867 unsigned long orig_jiffies;
1871 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1873 if (adapter->algo->smbus_xfer) {
1874 mutex_lock(&adapter->bus_lock);
1876 /* Retry automatically on arbitration loss */
1877 orig_jiffies = jiffies;
1878 for (res = 0, try = 0; try <= adapter->retries; try++) {
1879 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1880 read_write, command,
1884 if (time_after(jiffies,
1885 orig_jiffies + adapter->timeout))
1888 mutex_unlock(&adapter->bus_lock);
1890 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1891 command, protocol, data);
1895 EXPORT_SYMBOL(i2c_smbus_xfer);
1897 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1898 MODULE_DESCRIPTION("I2C-Bus main module");
1899 MODULE_LICENSE("GPL");