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/platform_device.h>
33 #include <linux/mutex.h>
34 #include <linux/completion.h>
35 #include <linux/hardirq.h>
36 #include <linux/irqflags.h>
37 #include <asm/uaccess.h>
42 static DEFINE_MUTEX(core_lock);
43 static DEFINE_IDR(i2c_adapter_idr);
45 #define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
47 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
49 /* ------------------------------------------------------------------------- */
51 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
52 const struct i2c_client *client)
55 if (strcmp(client->name, id->name) == 0)
62 static int i2c_device_match(struct device *dev, struct device_driver *drv)
64 struct i2c_client *client = to_i2c_client(dev);
65 struct i2c_driver *driver = to_i2c_driver(drv);
67 /* make legacy i2c drivers bypass driver model probing entirely;
68 * such drivers scan each i2c adapter/bus themselves.
70 if (!is_newstyle_driver(driver))
73 /* match on an id table if there is one */
75 return i2c_match_id(driver->id_table, client) != NULL;
82 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
83 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
85 struct i2c_client *client = to_i2c_client(dev);
87 /* by definition, legacy drivers can't hotplug */
91 if (add_uevent_var(env, "MODALIAS=%s%s",
92 I2C_MODULE_PREFIX, client->name))
94 dev_dbg(dev, "uevent\n");
99 #define i2c_device_uevent NULL
100 #endif /* CONFIG_HOTPLUG */
102 static int i2c_device_probe(struct device *dev)
104 struct i2c_client *client = to_i2c_client(dev);
105 struct i2c_driver *driver = to_i2c_driver(dev->driver);
108 if (!driver->probe || !driver->id_table)
110 client->driver = driver;
111 if (!device_can_wakeup(&client->dev))
112 device_init_wakeup(&client->dev,
113 client->flags & I2C_CLIENT_WAKE);
114 dev_dbg(dev, "probe\n");
116 status = driver->probe(client, i2c_match_id(driver->id_table, client));
118 client->driver = NULL;
122 static int i2c_device_remove(struct device *dev)
124 struct i2c_client *client = to_i2c_client(dev);
125 struct i2c_driver *driver;
131 driver = to_i2c_driver(dev->driver);
132 if (driver->remove) {
133 dev_dbg(dev, "remove\n");
134 status = driver->remove(client);
140 client->driver = NULL;
144 static void i2c_device_shutdown(struct device *dev)
146 struct i2c_driver *driver;
150 driver = to_i2c_driver(dev->driver);
151 if (driver->shutdown)
152 driver->shutdown(to_i2c_client(dev));
155 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
157 struct i2c_driver *driver;
161 driver = to_i2c_driver(dev->driver);
162 if (!driver->suspend)
164 return driver->suspend(to_i2c_client(dev), mesg);
167 static int i2c_device_resume(struct device * dev)
169 struct i2c_driver *driver;
173 driver = to_i2c_driver(dev->driver);
176 return driver->resume(to_i2c_client(dev));
179 static void i2c_client_release(struct device *dev)
181 struct i2c_client *client = to_i2c_client(dev);
182 complete(&client->released);
185 static void i2c_client_dev_release(struct device *dev)
187 kfree(to_i2c_client(dev));
190 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
192 struct i2c_client *client = to_i2c_client(dev);
193 return sprintf(buf, "%s\n", client->name);
196 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
198 struct i2c_client *client = to_i2c_client(dev);
199 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
202 static struct device_attribute i2c_dev_attrs[] = {
203 __ATTR(name, S_IRUGO, show_client_name, NULL),
204 /* modalias helps coldplug: modprobe $(cat .../modalias) */
205 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
209 struct bus_type i2c_bus_type = {
211 .dev_attrs = i2c_dev_attrs,
212 .match = i2c_device_match,
213 .uevent = i2c_device_uevent,
214 .probe = i2c_device_probe,
215 .remove = i2c_device_remove,
216 .shutdown = i2c_device_shutdown,
217 .suspend = i2c_device_suspend,
218 .resume = i2c_device_resume,
220 EXPORT_SYMBOL_GPL(i2c_bus_type);
224 * i2c_verify_client - return parameter as i2c_client, or NULL
225 * @dev: device, probably from some driver model iterator
227 * When traversing the driver model tree, perhaps using driver model
228 * iterators like @device_for_each_child(), you can't assume very much
229 * about the nodes you find. Use this function to avoid oopses caused
230 * by wrongly treating some non-I2C device as an i2c_client.
232 struct i2c_client *i2c_verify_client(struct device *dev)
234 return (dev->bus == &i2c_bus_type)
238 EXPORT_SYMBOL(i2c_verify_client);
242 * i2c_new_device - instantiate an i2c device for use with a new style driver
243 * @adap: the adapter managing the device
244 * @info: describes one I2C device; bus_num is ignored
247 * Create a device to work with a new style i2c driver, where binding is
248 * handled through driver model probe()/remove() methods. This call is not
249 * appropriate for use by mainboad initialization logic, which usually runs
250 * during an arch_initcall() long before any i2c_adapter could exist.
252 * This returns the new i2c client, which may be saved for later use with
253 * i2c_unregister_device(); or NULL to indicate an error.
256 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
258 struct i2c_client *client;
261 client = kzalloc(sizeof *client, GFP_KERNEL);
265 client->adapter = adap;
267 client->dev.platform_data = info->platform_data;
270 client->dev.archdata = *info->archdata;
272 client->flags = info->flags;
273 client->addr = info->addr;
274 client->irq = info->irq;
276 strlcpy(client->name, info->type, sizeof(client->name));
278 /* a new style driver may be bound to this device when we
279 * return from this function, or any later moment (e.g. maybe
280 * hotplugging will load the driver module). and the device
281 * refcount model is the standard driver model one.
283 status = i2c_attach_client(client);
290 EXPORT_SYMBOL_GPL(i2c_new_device);
294 * i2c_unregister_device - reverse effect of i2c_new_device()
295 * @client: value returned from i2c_new_device()
298 void i2c_unregister_device(struct i2c_client *client)
300 struct i2c_adapter *adapter = client->adapter;
301 struct i2c_driver *driver = client->driver;
303 if (driver && !is_newstyle_driver(driver)) {
304 dev_err(&client->dev, "can't unregister devices "
305 "with legacy drivers\n");
310 if (adapter->client_unregister) {
311 if (adapter->client_unregister(client)) {
312 dev_warn(&client->dev,
313 "client_unregister [%s] failed\n",
318 mutex_lock(&adapter->clist_lock);
319 list_del(&client->list);
320 mutex_unlock(&adapter->clist_lock);
322 device_unregister(&client->dev);
324 EXPORT_SYMBOL_GPL(i2c_unregister_device);
327 static const struct i2c_device_id dummy_id[] = {
332 static int dummy_probe(struct i2c_client *client,
333 const struct i2c_device_id *id)
338 static int dummy_remove(struct i2c_client *client)
343 static struct i2c_driver dummy_driver = {
344 .driver.name = "dummy",
345 .probe = dummy_probe,
346 .remove = dummy_remove,
347 .id_table = dummy_id,
351 * i2c_new_dummy - return a new i2c device bound to a dummy driver
352 * @adapter: the adapter managing the device
353 * @address: seven bit address to be used
356 * This returns an I2C client bound to the "dummy" driver, intended for use
357 * with devices that consume multiple addresses. Examples of such chips
358 * include various EEPROMS (like 24c04 and 24c08 models).
360 * These dummy devices have two main uses. First, most I2C and SMBus calls
361 * except i2c_transfer() need a client handle; the dummy will be that handle.
362 * And second, this prevents the specified address from being bound to a
365 * This returns the new i2c client, which should be saved for later use with
366 * i2c_unregister_device(); or NULL to indicate an error.
369 i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
371 struct i2c_board_info info = {
372 I2C_BOARD_INFO("dummy", address),
375 return i2c_new_device(adapter, &info);
377 EXPORT_SYMBOL_GPL(i2c_new_dummy);
379 /* ------------------------------------------------------------------------- */
381 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
383 static void i2c_adapter_dev_release(struct device *dev)
385 struct i2c_adapter *adap = to_i2c_adapter(dev);
386 complete(&adap->dev_released);
390 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
392 struct i2c_adapter *adap = to_i2c_adapter(dev);
393 return sprintf(buf, "%s\n", adap->name);
396 static struct device_attribute i2c_adapter_attrs[] = {
397 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
401 static struct class i2c_adapter_class = {
402 .owner = THIS_MODULE,
403 .name = "i2c-adapter",
404 .dev_attrs = i2c_adapter_attrs,
407 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
409 struct i2c_devinfo *devinfo;
411 mutex_lock(&__i2c_board_lock);
412 list_for_each_entry(devinfo, &__i2c_board_list, list) {
413 if (devinfo->busnum == adapter->nr
414 && !i2c_new_device(adapter,
415 &devinfo->board_info))
416 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
417 i2c_adapter_id(adapter),
418 devinfo->board_info.addr);
420 mutex_unlock(&__i2c_board_lock);
423 static int i2c_do_add_adapter(struct device_driver *d, void *data)
425 struct i2c_driver *driver = to_i2c_driver(d);
426 struct i2c_adapter *adap = data;
428 /* Detect supported devices on that bus, and instantiate them */
429 i2c_detect(adap, driver);
431 /* Let legacy drivers scan this bus for matching devices */
432 if (driver->attach_adapter) {
433 /* We ignore the return code; if it fails, too bad */
434 driver->attach_adapter(adap);
439 static int i2c_register_adapter(struct i2c_adapter *adap)
443 /* Can't register until after driver model init */
444 if (unlikely(WARN_ON(!i2c_bus_type.p)))
447 mutex_init(&adap->bus_lock);
448 mutex_init(&adap->clist_lock);
449 INIT_LIST_HEAD(&adap->clients);
451 mutex_lock(&core_lock);
453 /* Add the adapter to the driver core.
454 * If the parent pointer is not set up,
455 * we add this adapter to the host bus.
457 if (adap->dev.parent == NULL) {
458 adap->dev.parent = &platform_bus;
459 pr_debug("I2C adapter driver [%s] forgot to specify "
460 "physical device\n", adap->name);
462 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
463 adap->dev.release = &i2c_adapter_dev_release;
464 adap->dev.class = &i2c_adapter_class;
465 res = device_register(&adap->dev);
469 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
471 /* create pre-declared device nodes for new-style drivers */
472 if (adap->nr < __i2c_first_dynamic_bus_num)
473 i2c_scan_static_board_info(adap);
476 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
480 mutex_unlock(&core_lock);
484 idr_remove(&i2c_adapter_idr, adap->nr);
489 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
490 * @adapter: the adapter to add
493 * This routine is used to declare an I2C adapter when its bus number
494 * doesn't matter. Examples: for I2C adapters dynamically added by
495 * USB links or PCI plugin cards.
497 * When this returns zero, a new bus number was allocated and stored
498 * in adap->nr, and the specified adapter became available for clients.
499 * Otherwise, a negative errno value is returned.
501 int i2c_add_adapter(struct i2c_adapter *adapter)
506 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
509 mutex_lock(&core_lock);
510 /* "above" here means "above or equal to", sigh */
511 res = idr_get_new_above(&i2c_adapter_idr, adapter,
512 __i2c_first_dynamic_bus_num, &id);
513 mutex_unlock(&core_lock);
522 return i2c_register_adapter(adapter);
524 EXPORT_SYMBOL(i2c_add_adapter);
527 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
528 * @adap: the adapter to register (with adap->nr initialized)
531 * This routine is used to declare an I2C adapter when its bus number
532 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
533 * or otherwise built in to the system's mainboard, and where i2c_board_info
534 * is used to properly configure I2C devices.
536 * If no devices have pre-been declared for this bus, then be sure to
537 * register the adapter before any dynamically allocated ones. Otherwise
538 * the required bus ID may not be available.
540 * When this returns zero, the specified adapter became available for
541 * clients using the bus number provided in adap->nr. Also, the table
542 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
543 * and the appropriate driver model device nodes are created. Otherwise, a
544 * negative errno value is returned.
546 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
551 if (adap->nr & ~MAX_ID_MASK)
555 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
558 mutex_lock(&core_lock);
559 /* "above" here means "above or equal to", sigh;
560 * we need the "equal to" result to force the result
562 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
563 if (status == 0 && id != adap->nr) {
565 idr_remove(&i2c_adapter_idr, id);
567 mutex_unlock(&core_lock);
568 if (status == -EAGAIN)
572 status = i2c_register_adapter(adap);
575 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
577 static int i2c_do_del_adapter(struct device_driver *d, void *data)
579 struct i2c_driver *driver = to_i2c_driver(d);
580 struct i2c_adapter *adapter = data;
581 struct i2c_client *client, *_n;
584 /* Remove the devices we created ourselves */
585 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
586 if (client->adapter == adapter) {
587 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
588 client->name, client->addr);
589 list_del(&client->detected);
590 i2c_unregister_device(client);
594 if (!driver->detach_adapter)
596 res = driver->detach_adapter(adapter);
598 dev_err(&adapter->dev, "detach_adapter failed (%d) "
599 "for driver [%s]\n", res, driver->driver.name);
604 * i2c_del_adapter - unregister I2C adapter
605 * @adap: the adapter being unregistered
608 * This unregisters an I2C adapter which was previously registered
609 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
611 int i2c_del_adapter(struct i2c_adapter *adap)
613 struct i2c_client *client, *_n;
616 mutex_lock(&core_lock);
618 /* First make sure that this adapter was ever added */
619 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
620 pr_debug("i2c-core: attempting to delete unregistered "
621 "adapter [%s]\n", adap->name);
626 /* Tell drivers about this removal */
627 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
632 /* detach any active clients. This must be done first, because
633 * it can fail; in which case we give up. */
634 list_for_each_entry_safe(client, _n, &adap->clients, list) {
635 struct i2c_driver *driver;
637 driver = client->driver;
639 /* new style, follow standard driver model */
640 if (!driver || is_newstyle_driver(driver)) {
641 i2c_unregister_device(client);
645 /* legacy drivers create and remove clients themselves */
646 if ((res = driver->detach_client(client))) {
647 dev_err(&adap->dev, "detach_client failed for client "
648 "[%s] at address 0x%02x\n", client->name,
654 /* clean up the sysfs representation */
655 init_completion(&adap->dev_released);
656 device_unregister(&adap->dev);
658 /* wait for sysfs to drop all references */
659 wait_for_completion(&adap->dev_released);
662 idr_remove(&i2c_adapter_idr, adap->nr);
664 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
666 /* Clear the device structure in case this adapter is ever going to be
668 memset(&adap->dev, 0, sizeof(adap->dev));
671 mutex_unlock(&core_lock);
674 EXPORT_SYMBOL(i2c_del_adapter);
677 /* ------------------------------------------------------------------------- */
679 static int __attach_adapter(struct device *dev, void *data)
681 struct i2c_adapter *adapter = to_i2c_adapter(dev);
682 struct i2c_driver *driver = data;
684 i2c_detect(adapter, driver);
686 /* Legacy drivers scan i2c busses directly */
687 if (driver->attach_adapter)
688 driver->attach_adapter(adapter);
694 * An i2c_driver is used with one or more i2c_client (device) nodes to access
695 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
696 * are two models for binding the driver to its device: "new style" drivers
697 * follow the standard Linux driver model and just respond to probe() calls
698 * issued if the driver core sees they match(); "legacy" drivers create device
702 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
706 /* Can't register until after driver model init */
707 if (unlikely(WARN_ON(!i2c_bus_type.p)))
710 /* new style driver methods can't mix with legacy ones */
711 if (is_newstyle_driver(driver)) {
712 if (driver->attach_adapter || driver->detach_adapter
713 || driver->detach_client) {
715 "i2c-core: driver [%s] is confused\n",
716 driver->driver.name);
721 /* add the driver to the list of i2c drivers in the driver core */
722 driver->driver.owner = owner;
723 driver->driver.bus = &i2c_bus_type;
725 /* for new style drivers, when registration returns the driver core
726 * will have called probe() for all matching-but-unbound devices.
728 res = driver_register(&driver->driver);
732 mutex_lock(&core_lock);
734 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
736 INIT_LIST_HEAD(&driver->clients);
737 /* Walk the adapters that are already present */
738 class_for_each_device(&i2c_adapter_class, NULL, driver,
741 mutex_unlock(&core_lock);
744 EXPORT_SYMBOL(i2c_register_driver);
746 static int __detach_adapter(struct device *dev, void *data)
748 struct i2c_adapter *adapter = to_i2c_adapter(dev);
749 struct i2c_driver *driver = data;
750 struct i2c_client *client, *_n;
752 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
753 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
754 client->name, client->addr);
755 list_del(&client->detected);
756 i2c_unregister_device(client);
759 if (is_newstyle_driver(driver))
762 /* Have a look at each adapter, if clients of this driver are still
763 * attached. If so, detach them to be able to kill the driver
766 if (driver->detach_adapter) {
767 if (driver->detach_adapter(adapter))
768 dev_err(&adapter->dev,
769 "detach_adapter failed for driver [%s]\n",
770 driver->driver.name);
772 struct i2c_client *client, *_n;
774 list_for_each_entry_safe(client, _n, &adapter->clients, list) {
775 if (client->driver != driver)
777 dev_dbg(&adapter->dev,
778 "detaching client [%s] at 0x%02x\n",
779 client->name, client->addr);
780 if (driver->detach_client(client))
781 dev_err(&adapter->dev, "detach_client "
782 "failed for client [%s] at 0x%02x\n",
783 client->name, client->addr);
791 * i2c_del_driver - unregister I2C driver
792 * @driver: the driver being unregistered
795 void i2c_del_driver(struct i2c_driver *driver)
797 mutex_lock(&core_lock);
799 class_for_each_device(&i2c_adapter_class, NULL, driver,
802 driver_unregister(&driver->driver);
803 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
805 mutex_unlock(&core_lock);
807 EXPORT_SYMBOL(i2c_del_driver);
809 /* ------------------------------------------------------------------------- */
811 static int __i2c_check_addr(struct device *dev, void *addrp)
813 struct i2c_client *client = i2c_verify_client(dev);
814 int addr = *(int *)addrp;
816 if (client && client->addr == addr)
821 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
823 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
826 int i2c_attach_client(struct i2c_client *client)
828 struct i2c_adapter *adapter = client->adapter;
831 /* Check for address business */
832 res = i2c_check_addr(adapter, client->addr);
836 client->dev.parent = &client->adapter->dev;
837 client->dev.bus = &i2c_bus_type;
840 client->dev.driver = &client->driver->driver;
842 if (client->driver && !is_newstyle_driver(client->driver)) {
843 client->dev.release = i2c_client_release;
844 client->dev.uevent_suppress = 1;
846 client->dev.release = i2c_client_dev_release;
848 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
849 "%d-%04x", i2c_adapter_id(adapter), client->addr);
850 res = device_register(&client->dev);
854 mutex_lock(&adapter->clist_lock);
855 list_add_tail(&client->list, &adapter->clients);
856 mutex_unlock(&adapter->clist_lock);
858 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
859 client->name, client->dev.bus_id);
861 if (adapter->client_register) {
862 if (adapter->client_register(client)) {
863 dev_dbg(&adapter->dev, "client_register "
864 "failed for client [%s] at 0x%02x\n",
865 client->name, client->addr);
872 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
873 "(%d)\n", client->name, client->addr, res);
876 EXPORT_SYMBOL(i2c_attach_client);
878 int i2c_detach_client(struct i2c_client *client)
880 struct i2c_adapter *adapter = client->adapter;
883 if (adapter->client_unregister) {
884 res = adapter->client_unregister(client);
886 dev_err(&client->dev,
887 "client_unregister [%s] failed, "
888 "client not detached\n", client->name);
893 mutex_lock(&adapter->clist_lock);
894 list_del(&client->list);
895 mutex_unlock(&adapter->clist_lock);
897 init_completion(&client->released);
898 device_unregister(&client->dev);
899 wait_for_completion(&client->released);
904 EXPORT_SYMBOL(i2c_detach_client);
907 * i2c_use_client - increments the reference count of the i2c client structure
908 * @client: the client being referenced
910 * Each live reference to a client should be refcounted. The driver model does
911 * that automatically as part of driver binding, so that most drivers don't
912 * need to do this explicitly: they hold a reference until they're unbound
915 * A pointer to the client with the incremented reference counter is returned.
917 struct i2c_client *i2c_use_client(struct i2c_client *client)
919 if (client && get_device(&client->dev))
923 EXPORT_SYMBOL(i2c_use_client);
926 * i2c_release_client - release a use of the i2c client structure
927 * @client: the client being no longer referenced
929 * Must be called when a user of a client is finished with it.
931 void i2c_release_client(struct i2c_client *client)
934 put_device(&client->dev);
936 EXPORT_SYMBOL(i2c_release_client);
943 static int i2c_cmd(struct device *dev, void *_arg)
945 struct i2c_client *client = i2c_verify_client(dev);
946 struct i2c_cmd_arg *arg = _arg;
948 if (client && client->driver && client->driver->command)
949 client->driver->command(client, arg->cmd, arg->arg);
953 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
955 struct i2c_cmd_arg cmd_arg;
959 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
961 EXPORT_SYMBOL(i2c_clients_command);
963 static int __init i2c_init(void)
967 retval = bus_register(&i2c_bus_type);
970 retval = class_register(&i2c_adapter_class);
973 retval = i2c_add_driver(&dummy_driver);
979 class_unregister(&i2c_adapter_class);
981 bus_unregister(&i2c_bus_type);
985 static void __exit i2c_exit(void)
987 i2c_del_driver(&dummy_driver);
988 class_unregister(&i2c_adapter_class);
989 bus_unregister(&i2c_bus_type);
992 /* We must initialize early, because some subsystems register i2c drivers
993 * in subsys_initcall() code, but are linked (and initialized) before i2c.
995 postcore_initcall(i2c_init);
996 module_exit(i2c_exit);
998 /* ----------------------------------------------------
999 * the functional interface to the i2c busses.
1000 * ----------------------------------------------------
1004 * i2c_transfer - execute a single or combined I2C message
1005 * @adap: Handle to I2C bus
1006 * @msgs: One or more messages to execute before STOP is issued to
1007 * terminate the operation; each message begins with a START.
1008 * @num: Number of messages to be executed.
1010 * Returns negative errno, else the number of messages executed.
1012 * Note that there is no requirement that each message be sent to
1013 * the same slave address, although that is the most common model.
1015 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
1019 /* REVISIT the fault reporting model here is weak:
1021 * - When we get an error after receiving N bytes from a slave,
1022 * there is no way to report "N".
1024 * - When we get a NAK after transmitting N bytes to a slave,
1025 * there is no way to report "N" ... or to let the master
1026 * continue executing the rest of this combined message, if
1027 * that's the appropriate response.
1029 * - When for example "num" is two and we successfully complete
1030 * the first message but get an error part way through the
1031 * second, it's unclear whether that should be reported as
1032 * one (discarding status on the second message) or errno
1033 * (discarding status on the first one).
1036 if (adap->algo->master_xfer) {
1038 for (ret = 0; ret < num; ret++) {
1039 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1040 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1041 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1042 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1046 if (in_atomic() || irqs_disabled()) {
1047 ret = mutex_trylock(&adap->bus_lock);
1049 /* I2C activity is ongoing. */
1052 mutex_lock_nested(&adap->bus_lock, adap->level);
1055 ret = adap->algo->master_xfer(adap,msgs,num);
1056 mutex_unlock(&adap->bus_lock);
1060 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1064 EXPORT_SYMBOL(i2c_transfer);
1067 * i2c_master_send - issue a single I2C message in master transmit mode
1068 * @client: Handle to slave device
1069 * @buf: Data that will be written to the slave
1070 * @count: How many bytes to write
1072 * Returns negative errno, or else the number of bytes written.
1074 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1077 struct i2c_adapter *adap=client->adapter;
1080 msg.addr = client->addr;
1081 msg.flags = client->flags & I2C_M_TEN;
1083 msg.buf = (char *)buf;
1085 ret = i2c_transfer(adap, &msg, 1);
1087 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1088 transmitted, else error code. */
1089 return (ret == 1) ? count : ret;
1091 EXPORT_SYMBOL(i2c_master_send);
1094 * i2c_master_recv - issue a single I2C message in master receive mode
1095 * @client: Handle to slave device
1096 * @buf: Where to store data read from slave
1097 * @count: How many bytes to read
1099 * Returns negative errno, or else the number of bytes read.
1101 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1103 struct i2c_adapter *adap=client->adapter;
1107 msg.addr = client->addr;
1108 msg.flags = client->flags & I2C_M_TEN;
1109 msg.flags |= I2C_M_RD;
1113 ret = i2c_transfer(adap, &msg, 1);
1115 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1116 transmitted, else error code. */
1117 return (ret == 1) ? count : ret;
1119 EXPORT_SYMBOL(i2c_master_recv);
1121 /* ----------------------------------------------------
1122 * the i2c address scanning function
1123 * Will not work for 10-bit addresses!
1124 * ----------------------------------------------------
1126 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1127 int (*found_proc) (struct i2c_adapter *, int, int))
1131 /* Make sure the address is valid */
1132 if (addr < 0x03 || addr > 0x77) {
1133 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1138 /* Skip if already in use */
1139 if (i2c_check_addr(adapter, addr))
1142 /* Make sure there is something at this address, unless forced */
1144 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1145 I2C_SMBUS_QUICK, NULL) < 0)
1148 /* prevent 24RF08 corruption */
1149 if ((addr & ~0x0f) == 0x50)
1150 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1151 I2C_SMBUS_QUICK, NULL);
1154 /* Finally call the custom detection function */
1155 err = found_proc(adapter, addr, kind);
1156 /* -ENODEV can be returned if there is a chip at the given address
1157 but it isn't supported by this chip driver. We catch it here as
1158 this isn't an error. */
1163 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1168 int i2c_probe(struct i2c_adapter *adapter,
1169 const struct i2c_client_address_data *address_data,
1170 int (*found_proc) (struct i2c_adapter *, int, int))
1173 int adap_id = i2c_adapter_id(adapter);
1175 /* Force entries are done first, and are not affected by ignore
1177 if (address_data->forces) {
1178 const unsigned short * const *forces = address_data->forces;
1181 for (kind = 0; forces[kind]; kind++) {
1182 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1184 if (forces[kind][i] == adap_id
1185 || forces[kind][i] == ANY_I2C_BUS) {
1186 dev_dbg(&adapter->dev, "found force "
1187 "parameter for adapter %d, "
1188 "addr 0x%02x, kind %d\n",
1189 adap_id, forces[kind][i + 1],
1191 err = i2c_probe_address(adapter,
1192 forces[kind][i + 1],
1201 /* Stop here if we can't use SMBUS_QUICK */
1202 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1203 if (address_data->probe[0] == I2C_CLIENT_END
1204 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1207 dev_dbg(&adapter->dev, "SMBus Quick command not supported, "
1208 "can't probe for chips\n");
1212 /* Probe entries are done second, and are not affected by ignore
1214 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1215 if (address_data->probe[i] == adap_id
1216 || address_data->probe[i] == ANY_I2C_BUS) {
1217 dev_dbg(&adapter->dev, "found probe parameter for "
1218 "adapter %d, addr 0x%02x\n", adap_id,
1219 address_data->probe[i + 1]);
1220 err = i2c_probe_address(adapter,
1221 address_data->probe[i + 1],
1228 /* Normal entries are done last, unless shadowed by an ignore entry */
1229 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1233 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1235 if ((address_data->ignore[j] == adap_id ||
1236 address_data->ignore[j] == ANY_I2C_BUS)
1237 && address_data->ignore[j + 1]
1238 == address_data->normal_i2c[i]) {
1239 dev_dbg(&adapter->dev, "found ignore "
1240 "parameter for adapter %d, "
1241 "addr 0x%02x\n", adap_id,
1242 address_data->ignore[j + 1]);
1250 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1251 "addr 0x%02x\n", adap_id,
1252 address_data->normal_i2c[i]);
1253 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1261 EXPORT_SYMBOL(i2c_probe);
1263 /* Separate detection function for new-style drivers */
1264 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1265 struct i2c_driver *driver)
1267 struct i2c_board_info info;
1268 struct i2c_adapter *adapter = temp_client->adapter;
1269 int addr = temp_client->addr;
1272 /* Make sure the address is valid */
1273 if (addr < 0x03 || addr > 0x77) {
1274 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1279 /* Skip if already in use */
1280 if (i2c_check_addr(adapter, addr))
1283 /* Make sure there is something at this address, unless forced */
1285 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1286 I2C_SMBUS_QUICK, NULL) < 0)
1289 /* prevent 24RF08 corruption */
1290 if ((addr & ~0x0f) == 0x50)
1291 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1292 I2C_SMBUS_QUICK, NULL);
1295 /* Finally call the custom detection function */
1296 memset(&info, 0, sizeof(struct i2c_board_info));
1298 err = driver->detect(temp_client, kind, &info);
1300 /* -ENODEV is returned if the detection fails. We catch it
1301 here as this isn't an error. */
1302 return err == -ENODEV ? 0 : err;
1305 /* Consistency check */
1306 if (info.type[0] == '\0') {
1307 dev_err(&adapter->dev, "%s detection function provided "
1308 "no name for 0x%x\n", driver->driver.name,
1311 struct i2c_client *client;
1313 /* Detection succeeded, instantiate the device */
1314 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1315 info.type, info.addr);
1316 client = i2c_new_device(adapter, &info);
1318 list_add_tail(&client->detected, &driver->clients);
1320 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1321 info.type, info.addr);
1326 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1328 const struct i2c_client_address_data *address_data;
1329 struct i2c_client *temp_client;
1331 int adap_id = i2c_adapter_id(adapter);
1333 address_data = driver->address_data;
1334 if (!driver->detect || !address_data)
1337 /* Set up a temporary client to help detect callback */
1338 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1341 temp_client->adapter = adapter;
1343 /* Force entries are done first, and are not affected by ignore
1345 if (address_data->forces) {
1346 const unsigned short * const *forces = address_data->forces;
1349 for (kind = 0; forces[kind]; kind++) {
1350 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1352 if (forces[kind][i] == adap_id
1353 || forces[kind][i] == ANY_I2C_BUS) {
1354 dev_dbg(&adapter->dev, "found force "
1355 "parameter for adapter %d, "
1356 "addr 0x%02x, kind %d\n",
1357 adap_id, forces[kind][i + 1],
1359 temp_client->addr = forces[kind][i + 1];
1360 err = i2c_detect_address(temp_client,
1369 /* Stop here if the classes do not match */
1370 if (!(adapter->class & driver->class))
1373 /* Stop here if we can't use SMBUS_QUICK */
1374 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1375 if (address_data->probe[0] == I2C_CLIENT_END
1376 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1379 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1380 "can't probe for chips\n");
1385 /* Probe entries are done second, and are not affected by ignore
1387 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1388 if (address_data->probe[i] == adap_id
1389 || address_data->probe[i] == ANY_I2C_BUS) {
1390 dev_dbg(&adapter->dev, "found probe parameter for "
1391 "adapter %d, addr 0x%02x\n", adap_id,
1392 address_data->probe[i + 1]);
1393 temp_client->addr = address_data->probe[i + 1];
1394 err = i2c_detect_address(temp_client, -1, driver);
1400 /* Normal entries are done last, unless shadowed by an ignore entry */
1401 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1405 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1407 if ((address_data->ignore[j] == adap_id ||
1408 address_data->ignore[j] == ANY_I2C_BUS)
1409 && address_data->ignore[j + 1]
1410 == address_data->normal_i2c[i]) {
1411 dev_dbg(&adapter->dev, "found ignore "
1412 "parameter for adapter %d, "
1413 "addr 0x%02x\n", adap_id,
1414 address_data->ignore[j + 1]);
1422 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1423 "addr 0x%02x\n", adap_id,
1424 address_data->normal_i2c[i]);
1425 temp_client->addr = address_data->normal_i2c[i];
1426 err = i2c_detect_address(temp_client, -1, driver);
1437 i2c_new_probed_device(struct i2c_adapter *adap,
1438 struct i2c_board_info *info,
1439 unsigned short const *addr_list)
1443 /* Stop here if the bus doesn't support probing */
1444 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1445 dev_err(&adap->dev, "Probing not supported\n");
1449 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1450 /* Check address validity */
1451 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1452 dev_warn(&adap->dev, "Invalid 7-bit address "
1453 "0x%02x\n", addr_list[i]);
1457 /* Check address availability */
1458 if (i2c_check_addr(adap, addr_list[i])) {
1459 dev_dbg(&adap->dev, "Address 0x%02x already in "
1460 "use, not probing\n", addr_list[i]);
1464 /* Test address responsiveness
1465 The default probe method is a quick write, but it is known
1466 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1467 and could also irreversibly write-protect some EEPROMs, so
1468 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1469 read instead. Also, some bus drivers don't implement
1470 quick write, so we fallback to a byte read it that case
1472 if ((addr_list[i] & ~0x07) == 0x30
1473 || (addr_list[i] & ~0x0f) == 0x50
1474 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1475 union i2c_smbus_data data;
1477 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1479 I2C_SMBUS_BYTE, &data) >= 0)
1482 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1484 I2C_SMBUS_QUICK, NULL) >= 0)
1489 if (addr_list[i] == I2C_CLIENT_END) {
1490 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1494 info->addr = addr_list[i];
1495 return i2c_new_device(adap, info);
1497 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1499 struct i2c_adapter* i2c_get_adapter(int id)
1501 struct i2c_adapter *adapter;
1503 mutex_lock(&core_lock);
1504 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1505 if (adapter && !try_module_get(adapter->owner))
1508 mutex_unlock(&core_lock);
1511 EXPORT_SYMBOL(i2c_get_adapter);
1513 void i2c_put_adapter(struct i2c_adapter *adap)
1515 module_put(adap->owner);
1517 EXPORT_SYMBOL(i2c_put_adapter);
1519 /* The SMBus parts */
1521 #define POLY (0x1070U << 3)
1527 for(i = 0; i < 8; i++) {
1532 return (u8)(data >> 8);
1535 /* Incremental CRC8 over count bytes in the array pointed to by p */
1536 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1540 for(i = 0; i < count; i++)
1541 crc = crc8((crc ^ p[i]) << 8);
1545 /* Assume a 7-bit address, which is reasonable for SMBus */
1546 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1548 /* The address will be sent first */
1549 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1550 pec = i2c_smbus_pec(pec, &addr, 1);
1552 /* The data buffer follows */
1553 return i2c_smbus_pec(pec, msg->buf, msg->len);
1556 /* Used for write only transactions */
1557 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1559 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1563 /* Return <0 on CRC error
1564 If there was a write before this read (most cases) we need to take the
1565 partial CRC from the write part into account.
1566 Note that this function does modify the message (we need to decrease the
1567 message length to hide the CRC byte from the caller). */
1568 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1570 u8 rpec = msg->buf[--msg->len];
1571 cpec = i2c_smbus_msg_pec(cpec, msg);
1574 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1582 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1583 * @client: Handle to slave device
1585 * This executes the SMBus "receive byte" protocol, returning negative errno
1586 * else the byte received from the device.
1588 s32 i2c_smbus_read_byte(struct i2c_client *client)
1590 union i2c_smbus_data data;
1593 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1595 I2C_SMBUS_BYTE, &data);
1596 return (status < 0) ? status : data.byte;
1598 EXPORT_SYMBOL(i2c_smbus_read_byte);
1601 * i2c_smbus_write_byte - SMBus "send byte" protocol
1602 * @client: Handle to slave device
1603 * @value: Byte to be sent
1605 * This executes the SMBus "send byte" protocol, returning negative errno
1606 * else zero on success.
1608 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1610 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1611 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1613 EXPORT_SYMBOL(i2c_smbus_write_byte);
1616 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1617 * @client: Handle to slave device
1618 * @command: Byte interpreted by slave
1620 * This executes the SMBus "read byte" protocol, returning negative errno
1621 * else a data byte received from the device.
1623 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1625 union i2c_smbus_data data;
1628 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1629 I2C_SMBUS_READ, command,
1630 I2C_SMBUS_BYTE_DATA, &data);
1631 return (status < 0) ? status : data.byte;
1633 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1636 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1637 * @client: Handle to slave device
1638 * @command: Byte interpreted by slave
1639 * @value: Byte being written
1641 * This executes the SMBus "write byte" protocol, returning negative errno
1642 * else zero on success.
1644 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1646 union i2c_smbus_data data;
1648 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1649 I2C_SMBUS_WRITE,command,
1650 I2C_SMBUS_BYTE_DATA,&data);
1652 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1655 * i2c_smbus_read_word_data - SMBus "read word" protocol
1656 * @client: Handle to slave device
1657 * @command: Byte interpreted by slave
1659 * This executes the SMBus "read word" protocol, returning negative errno
1660 * else a 16-bit unsigned "word" received from the device.
1662 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1664 union i2c_smbus_data data;
1667 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1668 I2C_SMBUS_READ, command,
1669 I2C_SMBUS_WORD_DATA, &data);
1670 return (status < 0) ? status : data.word;
1672 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1675 * i2c_smbus_write_word_data - SMBus "write word" protocol
1676 * @client: Handle to slave device
1677 * @command: Byte interpreted by slave
1678 * @value: 16-bit "word" being written
1680 * This executes the SMBus "write word" protocol, returning negative errno
1681 * else zero on success.
1683 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1685 union i2c_smbus_data data;
1687 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1688 I2C_SMBUS_WRITE,command,
1689 I2C_SMBUS_WORD_DATA,&data);
1691 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1694 * i2c_smbus_process_call - SMBus "process call" protocol
1695 * @client: Handle to slave device
1696 * @command: Byte interpreted by slave
1697 * @value: 16-bit "word" being written
1699 * This executes the SMBus "process call" protocol, returning negative errno
1700 * else a 16-bit unsigned "word" received from the device.
1702 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1704 union i2c_smbus_data data;
1708 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1709 I2C_SMBUS_WRITE, command,
1710 I2C_SMBUS_PROC_CALL, &data);
1711 return (status < 0) ? status : data.word;
1713 EXPORT_SYMBOL(i2c_smbus_process_call);
1716 * i2c_smbus_read_block_data - SMBus "block read" protocol
1717 * @client: Handle to slave device
1718 * @command: Byte interpreted by slave
1719 * @values: Byte array into which data will be read; big enough to hold
1720 * the data returned by the slave. SMBus allows at most 32 bytes.
1722 * This executes the SMBus "block read" protocol, returning negative errno
1723 * else the number of data bytes in the slave's response.
1725 * Note that using this function requires that the client's adapter support
1726 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1727 * support this; its emulation through I2C messaging relies on a specific
1728 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1730 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1733 union i2c_smbus_data data;
1736 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1737 I2C_SMBUS_READ, command,
1738 I2C_SMBUS_BLOCK_DATA, &data);
1742 memcpy(values, &data.block[1], data.block[0]);
1743 return data.block[0];
1745 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1748 * i2c_smbus_write_block_data - SMBus "block write" protocol
1749 * @client: Handle to slave device
1750 * @command: Byte interpreted by slave
1751 * @length: Size of data block; SMBus allows at most 32 bytes
1752 * @values: Byte array which will be written.
1754 * This executes the SMBus "block write" protocol, returning negative errno
1755 * else zero on success.
1757 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1758 u8 length, const u8 *values)
1760 union i2c_smbus_data data;
1762 if (length > I2C_SMBUS_BLOCK_MAX)
1763 length = I2C_SMBUS_BLOCK_MAX;
1764 data.block[0] = length;
1765 memcpy(&data.block[1], values, length);
1766 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1767 I2C_SMBUS_WRITE,command,
1768 I2C_SMBUS_BLOCK_DATA,&data);
1770 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1772 /* Returns the number of read bytes */
1773 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1774 u8 length, u8 *values)
1776 union i2c_smbus_data data;
1779 if (length > I2C_SMBUS_BLOCK_MAX)
1780 length = I2C_SMBUS_BLOCK_MAX;
1781 data.block[0] = length;
1782 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1783 I2C_SMBUS_READ, command,
1784 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1788 memcpy(values, &data.block[1], data.block[0]);
1789 return data.block[0];
1791 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1793 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1794 u8 length, const u8 *values)
1796 union i2c_smbus_data data;
1798 if (length > I2C_SMBUS_BLOCK_MAX)
1799 length = I2C_SMBUS_BLOCK_MAX;
1800 data.block[0] = length;
1801 memcpy(data.block + 1, values, length);
1802 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1803 I2C_SMBUS_WRITE, command,
1804 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1806 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1808 /* Simulate a SMBus command using the i2c protocol
1809 No checking of parameters is done! */
1810 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1811 unsigned short flags,
1812 char read_write, u8 command, int size,
1813 union i2c_smbus_data * data)
1815 /* So we need to generate a series of msgs. In the case of writing, we
1816 need to use only one message; when reading, we need two. We initialize
1817 most things with sane defaults, to keep the code below somewhat
1819 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1820 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1821 int num = read_write == I2C_SMBUS_READ?2:1;
1822 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1823 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1829 msgbuf0[0] = command;
1831 case I2C_SMBUS_QUICK:
1833 /* Special case: The read/write field is used as data */
1834 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1837 case I2C_SMBUS_BYTE:
1838 if (read_write == I2C_SMBUS_READ) {
1839 /* Special case: only a read! */
1840 msg[0].flags = I2C_M_RD | flags;
1844 case I2C_SMBUS_BYTE_DATA:
1845 if (read_write == I2C_SMBUS_READ)
1849 msgbuf0[1] = data->byte;
1852 case I2C_SMBUS_WORD_DATA:
1853 if (read_write == I2C_SMBUS_READ)
1857 msgbuf0[1] = data->word & 0xff;
1858 msgbuf0[2] = data->word >> 8;
1861 case I2C_SMBUS_PROC_CALL:
1862 num = 2; /* Special case */
1863 read_write = I2C_SMBUS_READ;
1866 msgbuf0[1] = data->word & 0xff;
1867 msgbuf0[2] = data->word >> 8;
1869 case I2C_SMBUS_BLOCK_DATA:
1870 if (read_write == I2C_SMBUS_READ) {
1871 msg[1].flags |= I2C_M_RECV_LEN;
1872 msg[1].len = 1; /* block length will be added by
1873 the underlying bus driver */
1875 msg[0].len = data->block[0] + 2;
1876 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1877 dev_err(&adapter->dev,
1878 "Invalid block write size %d\n",
1882 for (i = 1; i < msg[0].len; i++)
1883 msgbuf0[i] = data->block[i-1];
1886 case I2C_SMBUS_BLOCK_PROC_CALL:
1887 num = 2; /* Another special case */
1888 read_write = I2C_SMBUS_READ;
1889 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1890 dev_err(&adapter->dev,
1891 "Invalid block write size %d\n",
1895 msg[0].len = data->block[0] + 2;
1896 for (i = 1; i < msg[0].len; i++)
1897 msgbuf0[i] = data->block[i-1];
1898 msg[1].flags |= I2C_M_RECV_LEN;
1899 msg[1].len = 1; /* block length will be added by
1900 the underlying bus driver */
1902 case I2C_SMBUS_I2C_BLOCK_DATA:
1903 if (read_write == I2C_SMBUS_READ) {
1904 msg[1].len = data->block[0];
1906 msg[0].len = data->block[0] + 1;
1907 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1908 dev_err(&adapter->dev,
1909 "Invalid block write size %d\n",
1913 for (i = 1; i <= data->block[0]; i++)
1914 msgbuf0[i] = data->block[i];
1918 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1922 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1923 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1925 /* Compute PEC if first message is a write */
1926 if (!(msg[0].flags & I2C_M_RD)) {
1927 if (num == 1) /* Write only */
1928 i2c_smbus_add_pec(&msg[0]);
1929 else /* Write followed by read */
1930 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1932 /* Ask for PEC if last message is a read */
1933 if (msg[num-1].flags & I2C_M_RD)
1937 status = i2c_transfer(adapter, msg, num);
1941 /* Check PEC if last message is a read */
1942 if (i && (msg[num-1].flags & I2C_M_RD)) {
1943 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1948 if (read_write == I2C_SMBUS_READ)
1950 case I2C_SMBUS_BYTE:
1951 data->byte = msgbuf0[0];
1953 case I2C_SMBUS_BYTE_DATA:
1954 data->byte = msgbuf1[0];
1956 case I2C_SMBUS_WORD_DATA:
1957 case I2C_SMBUS_PROC_CALL:
1958 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1960 case I2C_SMBUS_I2C_BLOCK_DATA:
1961 for (i = 0; i < data->block[0]; i++)
1962 data->block[i+1] = msgbuf1[i];
1964 case I2C_SMBUS_BLOCK_DATA:
1965 case I2C_SMBUS_BLOCK_PROC_CALL:
1966 for (i = 0; i < msgbuf1[0] + 1; i++)
1967 data->block[i] = msgbuf1[i];
1974 * i2c_smbus_xfer - execute SMBus protocol operations
1975 * @adapter: Handle to I2C bus
1976 * @addr: Address of SMBus slave on that bus
1977 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1978 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1979 * @command: Byte interpreted by slave, for protocols which use such bytes
1980 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1981 * @data: Data to be read or written
1983 * This executes an SMBus protocol operation, and returns a negative
1984 * errno code else zero on success.
1986 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1987 char read_write, u8 command, int protocol,
1988 union i2c_smbus_data * data)
1992 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1994 if (adapter->algo->smbus_xfer) {
1995 mutex_lock(&adapter->bus_lock);
1996 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1997 command, protocol, data);
1998 mutex_unlock(&adapter->bus_lock);
2000 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
2001 command, protocol, data);
2005 EXPORT_SYMBOL(i2c_smbus_xfer);
2007 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2008 MODULE_DESCRIPTION("I2C-Bus main module");
2009 MODULE_LICENSE("GPL");