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 dev_dbg(dev, "probe\n");
113 status = driver->probe(client, i2c_match_id(driver->id_table, client));
115 client->driver = NULL;
119 static int i2c_device_remove(struct device *dev)
121 struct i2c_client *client = to_i2c_client(dev);
122 struct i2c_driver *driver;
128 driver = to_i2c_driver(dev->driver);
129 if (driver->remove) {
130 dev_dbg(dev, "remove\n");
131 status = driver->remove(client);
137 client->driver = NULL;
141 static void i2c_device_shutdown(struct device *dev)
143 struct i2c_driver *driver;
147 driver = to_i2c_driver(dev->driver);
148 if (driver->shutdown)
149 driver->shutdown(to_i2c_client(dev));
152 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
154 struct i2c_driver *driver;
158 driver = to_i2c_driver(dev->driver);
159 if (!driver->suspend)
161 return driver->suspend(to_i2c_client(dev), mesg);
164 static int i2c_device_resume(struct device * dev)
166 struct i2c_driver *driver;
170 driver = to_i2c_driver(dev->driver);
173 return driver->resume(to_i2c_client(dev));
176 static void i2c_client_release(struct device *dev)
178 struct i2c_client *client = to_i2c_client(dev);
179 complete(&client->released);
182 static void i2c_client_dev_release(struct device *dev)
184 kfree(to_i2c_client(dev));
187 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
189 struct i2c_client *client = to_i2c_client(dev);
190 return sprintf(buf, "%s\n", client->name);
193 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
195 struct i2c_client *client = to_i2c_client(dev);
196 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
199 static struct device_attribute i2c_dev_attrs[] = {
200 __ATTR(name, S_IRUGO, show_client_name, NULL),
201 /* modalias helps coldplug: modprobe $(cat .../modalias) */
202 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
206 struct bus_type i2c_bus_type = {
208 .dev_attrs = i2c_dev_attrs,
209 .match = i2c_device_match,
210 .uevent = i2c_device_uevent,
211 .probe = i2c_device_probe,
212 .remove = i2c_device_remove,
213 .shutdown = i2c_device_shutdown,
214 .suspend = i2c_device_suspend,
215 .resume = i2c_device_resume,
217 EXPORT_SYMBOL_GPL(i2c_bus_type);
221 * i2c_verify_client - return parameter as i2c_client, or NULL
222 * @dev: device, probably from some driver model iterator
224 * When traversing the driver model tree, perhaps using driver model
225 * iterators like @device_for_each_child(), you can't assume very much
226 * about the nodes you find. Use this function to avoid oopses caused
227 * by wrongly treating some non-I2C device as an i2c_client.
229 struct i2c_client *i2c_verify_client(struct device *dev)
231 return (dev->bus == &i2c_bus_type)
235 EXPORT_SYMBOL(i2c_verify_client);
239 * i2c_new_device - instantiate an i2c device for use with a new style driver
240 * @adap: the adapter managing the device
241 * @info: describes one I2C device; bus_num is ignored
244 * Create a device to work with a new style i2c driver, where binding is
245 * handled through driver model probe()/remove() methods. This call is not
246 * appropriate for use by mainboad initialization logic, which usually runs
247 * during an arch_initcall() long before any i2c_adapter could exist.
249 * This returns the new i2c client, which may be saved for later use with
250 * i2c_unregister_device(); or NULL to indicate an error.
253 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
255 struct i2c_client *client;
258 client = kzalloc(sizeof *client, GFP_KERNEL);
262 client->adapter = adap;
264 client->dev.platform_data = info->platform_data;
265 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
267 client->flags = info->flags & ~I2C_CLIENT_WAKE;
268 client->addr = info->addr;
269 client->irq = info->irq;
271 strlcpy(client->name, info->type, sizeof(client->name));
273 /* a new style driver may be bound to this device when we
274 * return from this function, or any later moment (e.g. maybe
275 * hotplugging will load the driver module). and the device
276 * refcount model is the standard driver model one.
278 status = i2c_attach_client(client);
285 EXPORT_SYMBOL_GPL(i2c_new_device);
289 * i2c_unregister_device - reverse effect of i2c_new_device()
290 * @client: value returned from i2c_new_device()
293 void i2c_unregister_device(struct i2c_client *client)
295 struct i2c_adapter *adapter = client->adapter;
296 struct i2c_driver *driver = client->driver;
298 if (driver && !is_newstyle_driver(driver)) {
299 dev_err(&client->dev, "can't unregister devices "
300 "with legacy drivers\n");
305 if (adapter->client_unregister) {
306 if (adapter->client_unregister(client)) {
307 dev_warn(&client->dev,
308 "client_unregister [%s] failed\n",
313 mutex_lock(&adapter->clist_lock);
314 list_del(&client->list);
315 mutex_unlock(&adapter->clist_lock);
317 device_unregister(&client->dev);
319 EXPORT_SYMBOL_GPL(i2c_unregister_device);
322 static const struct i2c_device_id dummy_id[] = {
327 static int dummy_probe(struct i2c_client *client,
328 const struct i2c_device_id *id)
333 static int dummy_remove(struct i2c_client *client)
338 static struct i2c_driver dummy_driver = {
339 .driver.name = "dummy",
340 .probe = dummy_probe,
341 .remove = dummy_remove,
342 .id_table = dummy_id,
346 * i2c_new_dummy - return a new i2c device bound to a dummy driver
347 * @adapter: the adapter managing the device
348 * @address: seven bit address to be used
351 * This returns an I2C client bound to the "dummy" driver, intended for use
352 * with devices that consume multiple addresses. Examples of such chips
353 * include various EEPROMS (like 24c04 and 24c08 models).
355 * These dummy devices have two main uses. First, most I2C and SMBus calls
356 * except i2c_transfer() need a client handle; the dummy will be that handle.
357 * And second, this prevents the specified address from being bound to a
360 * This returns the new i2c client, which should be saved for later use with
361 * i2c_unregister_device(); or NULL to indicate an error.
364 i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
366 struct i2c_board_info info = {
367 I2C_BOARD_INFO("dummy", address),
370 return i2c_new_device(adapter, &info);
372 EXPORT_SYMBOL_GPL(i2c_new_dummy);
374 /* ------------------------------------------------------------------------- */
376 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
378 static void i2c_adapter_dev_release(struct device *dev)
380 struct i2c_adapter *adap = to_i2c_adapter(dev);
381 complete(&adap->dev_released);
385 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
387 struct i2c_adapter *adap = to_i2c_adapter(dev);
388 return sprintf(buf, "%s\n", adap->name);
391 static struct device_attribute i2c_adapter_attrs[] = {
392 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
396 static struct class i2c_adapter_class = {
397 .owner = THIS_MODULE,
398 .name = "i2c-adapter",
399 .dev_attrs = i2c_adapter_attrs,
402 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
404 struct i2c_devinfo *devinfo;
406 mutex_lock(&__i2c_board_lock);
407 list_for_each_entry(devinfo, &__i2c_board_list, list) {
408 if (devinfo->busnum == adapter->nr
409 && !i2c_new_device(adapter,
410 &devinfo->board_info))
411 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
412 i2c_adapter_id(adapter),
413 devinfo->board_info.addr);
415 mutex_unlock(&__i2c_board_lock);
418 static int i2c_do_add_adapter(struct device_driver *d, void *data)
420 struct i2c_driver *driver = to_i2c_driver(d);
421 struct i2c_adapter *adap = data;
423 /* Detect supported devices on that bus, and instantiate them */
424 i2c_detect(adap, driver);
426 /* Let legacy drivers scan this bus for matching devices */
427 if (driver->attach_adapter) {
428 /* We ignore the return code; if it fails, too bad */
429 driver->attach_adapter(adap);
434 static int i2c_register_adapter(struct i2c_adapter *adap)
438 mutex_init(&adap->bus_lock);
439 mutex_init(&adap->clist_lock);
440 INIT_LIST_HEAD(&adap->clients);
442 mutex_lock(&core_lock);
444 /* Add the adapter to the driver core.
445 * If the parent pointer is not set up,
446 * we add this adapter to the host bus.
448 if (adap->dev.parent == NULL) {
449 adap->dev.parent = &platform_bus;
450 pr_debug("I2C adapter driver [%s] forgot to specify "
451 "physical device\n", adap->name);
453 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
454 adap->dev.release = &i2c_adapter_dev_release;
455 adap->dev.class = &i2c_adapter_class;
456 res = device_register(&adap->dev);
460 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
462 /* create pre-declared device nodes for new-style drivers */
463 if (adap->nr < __i2c_first_dynamic_bus_num)
464 i2c_scan_static_board_info(adap);
467 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
471 mutex_unlock(&core_lock);
475 idr_remove(&i2c_adapter_idr, adap->nr);
480 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
481 * @adapter: the adapter to add
484 * This routine is used to declare an I2C adapter when its bus number
485 * doesn't matter. Examples: for I2C adapters dynamically added by
486 * USB links or PCI plugin cards.
488 * When this returns zero, a new bus number was allocated and stored
489 * in adap->nr, and the specified adapter became available for clients.
490 * Otherwise, a negative errno value is returned.
492 int i2c_add_adapter(struct i2c_adapter *adapter)
497 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
500 mutex_lock(&core_lock);
501 /* "above" here means "above or equal to", sigh */
502 res = idr_get_new_above(&i2c_adapter_idr, adapter,
503 __i2c_first_dynamic_bus_num, &id);
504 mutex_unlock(&core_lock);
513 return i2c_register_adapter(adapter);
515 EXPORT_SYMBOL(i2c_add_adapter);
518 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
519 * @adap: the adapter to register (with adap->nr initialized)
522 * This routine is used to declare an I2C adapter when its bus number
523 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
524 * or otherwise built in to the system's mainboard, and where i2c_board_info
525 * is used to properly configure I2C devices.
527 * If no devices have pre-been declared for this bus, then be sure to
528 * register the adapter before any dynamically allocated ones. Otherwise
529 * the required bus ID may not be available.
531 * When this returns zero, the specified adapter became available for
532 * clients using the bus number provided in adap->nr. Also, the table
533 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
534 * and the appropriate driver model device nodes are created. Otherwise, a
535 * negative errno value is returned.
537 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
542 if (adap->nr & ~MAX_ID_MASK)
546 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
549 mutex_lock(&core_lock);
550 /* "above" here means "above or equal to", sigh;
551 * we need the "equal to" result to force the result
553 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
554 if (status == 0 && id != adap->nr) {
556 idr_remove(&i2c_adapter_idr, id);
558 mutex_unlock(&core_lock);
559 if (status == -EAGAIN)
563 status = i2c_register_adapter(adap);
566 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
568 static int i2c_do_del_adapter(struct device_driver *d, void *data)
570 struct i2c_driver *driver = to_i2c_driver(d);
571 struct i2c_adapter *adapter = data;
572 struct i2c_client *client, *_n;
575 /* Remove the devices we created ourselves */
576 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
577 if (client->adapter == adapter) {
578 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
579 client->name, client->addr);
580 list_del(&client->detected);
581 i2c_unregister_device(client);
585 if (!driver->detach_adapter)
587 res = driver->detach_adapter(adapter);
589 dev_err(&adapter->dev, "detach_adapter failed (%d) "
590 "for driver [%s]\n", res, driver->driver.name);
595 * i2c_del_adapter - unregister I2C adapter
596 * @adap: the adapter being unregistered
599 * This unregisters an I2C adapter which was previously registered
600 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
602 int i2c_del_adapter(struct i2c_adapter *adap)
604 struct i2c_client *client, *_n;
607 mutex_lock(&core_lock);
609 /* First make sure that this adapter was ever added */
610 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
611 pr_debug("i2c-core: attempting to delete unregistered "
612 "adapter [%s]\n", adap->name);
617 /* Tell drivers about this removal */
618 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
623 /* detach any active clients. This must be done first, because
624 * it can fail; in which case we give up. */
625 list_for_each_entry_safe(client, _n, &adap->clients, list) {
626 struct i2c_driver *driver;
628 driver = client->driver;
630 /* new style, follow standard driver model */
631 if (!driver || is_newstyle_driver(driver)) {
632 i2c_unregister_device(client);
636 /* legacy drivers create and remove clients themselves */
637 if ((res = driver->detach_client(client))) {
638 dev_err(&adap->dev, "detach_client failed for client "
639 "[%s] at address 0x%02x\n", client->name,
645 /* clean up the sysfs representation */
646 init_completion(&adap->dev_released);
647 device_unregister(&adap->dev);
649 /* wait for sysfs to drop all references */
650 wait_for_completion(&adap->dev_released);
653 idr_remove(&i2c_adapter_idr, adap->nr);
655 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
657 /* Clear the device structure in case this adapter is ever going to be
659 memset(&adap->dev, 0, sizeof(adap->dev));
662 mutex_unlock(&core_lock);
665 EXPORT_SYMBOL(i2c_del_adapter);
668 /* ------------------------------------------------------------------------- */
670 static int __attach_adapter(struct device *dev, void *data)
672 struct i2c_adapter *adapter = to_i2c_adapter(dev);
673 struct i2c_driver *driver = data;
675 i2c_detect(adapter, driver);
677 /* Legacy drivers scan i2c busses directly */
678 if (driver->attach_adapter)
679 driver->attach_adapter(adapter);
685 * An i2c_driver is used with one or more i2c_client (device) nodes to access
686 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
687 * are two models for binding the driver to its device: "new style" drivers
688 * follow the standard Linux driver model and just respond to probe() calls
689 * issued if the driver core sees they match(); "legacy" drivers create device
693 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
697 /* new style driver methods can't mix with legacy ones */
698 if (is_newstyle_driver(driver)) {
699 if (driver->attach_adapter || driver->detach_adapter
700 || driver->detach_client) {
702 "i2c-core: driver [%s] is confused\n",
703 driver->driver.name);
708 /* add the driver to the list of i2c drivers in the driver core */
709 driver->driver.owner = owner;
710 driver->driver.bus = &i2c_bus_type;
712 /* for new style drivers, when registration returns the driver core
713 * will have called probe() for all matching-but-unbound devices.
715 res = driver_register(&driver->driver);
719 mutex_lock(&core_lock);
721 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
723 INIT_LIST_HEAD(&driver->clients);
724 /* Walk the adapters that are already present */
725 class_for_each_device(&i2c_adapter_class, NULL, driver,
728 mutex_unlock(&core_lock);
731 EXPORT_SYMBOL(i2c_register_driver);
733 static int __detach_adapter(struct device *dev, void *data)
735 struct i2c_adapter *adapter = to_i2c_adapter(dev);
736 struct i2c_driver *driver = data;
737 struct i2c_client *client, *_n;
739 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
740 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
741 client->name, client->addr);
742 list_del(&client->detected);
743 i2c_unregister_device(client);
746 if (is_newstyle_driver(driver))
749 /* Have a look at each adapter, if clients of this driver are still
750 * attached. If so, detach them to be able to kill the driver
753 if (driver->detach_adapter) {
754 if (driver->detach_adapter(adapter))
755 dev_err(&adapter->dev,
756 "detach_adapter failed for driver [%s]\n",
757 driver->driver.name);
759 struct i2c_client *client, *_n;
761 list_for_each_entry_safe(client, _n, &adapter->clients, list) {
762 if (client->driver != driver)
764 dev_dbg(&adapter->dev,
765 "detaching client [%s] at 0x%02x\n",
766 client->name, client->addr);
767 if (driver->detach_client(client))
768 dev_err(&adapter->dev, "detach_client "
769 "failed for client [%s] at 0x%02x\n",
770 client->name, client->addr);
778 * i2c_del_driver - unregister I2C driver
779 * @driver: the driver being unregistered
782 void i2c_del_driver(struct i2c_driver *driver)
784 mutex_lock(&core_lock);
786 class_for_each_device(&i2c_adapter_class, NULL, driver,
789 driver_unregister(&driver->driver);
790 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
792 mutex_unlock(&core_lock);
794 EXPORT_SYMBOL(i2c_del_driver);
796 /* ------------------------------------------------------------------------- */
798 static int __i2c_check_addr(struct device *dev, void *addrp)
800 struct i2c_client *client = i2c_verify_client(dev);
801 int addr = *(int *)addrp;
803 if (client && client->addr == addr)
808 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
810 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
813 int i2c_attach_client(struct i2c_client *client)
815 struct i2c_adapter *adapter = client->adapter;
818 /* Check for address business */
819 res = i2c_check_addr(adapter, client->addr);
823 client->dev.parent = &client->adapter->dev;
824 client->dev.bus = &i2c_bus_type;
827 client->dev.driver = &client->driver->driver;
829 if (client->driver && !is_newstyle_driver(client->driver)) {
830 client->dev.release = i2c_client_release;
831 client->dev.uevent_suppress = 1;
833 client->dev.release = i2c_client_dev_release;
835 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
836 "%d-%04x", i2c_adapter_id(adapter), client->addr);
837 res = device_register(&client->dev);
841 mutex_lock(&adapter->clist_lock);
842 list_add_tail(&client->list, &adapter->clients);
843 mutex_unlock(&adapter->clist_lock);
845 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
846 client->name, client->dev.bus_id);
848 if (adapter->client_register) {
849 if (adapter->client_register(client)) {
850 dev_dbg(&adapter->dev, "client_register "
851 "failed for client [%s] at 0x%02x\n",
852 client->name, client->addr);
859 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
860 "(%d)\n", client->name, client->addr, res);
863 EXPORT_SYMBOL(i2c_attach_client);
865 int i2c_detach_client(struct i2c_client *client)
867 struct i2c_adapter *adapter = client->adapter;
870 if (adapter->client_unregister) {
871 res = adapter->client_unregister(client);
873 dev_err(&client->dev,
874 "client_unregister [%s] failed, "
875 "client not detached\n", client->name);
880 mutex_lock(&adapter->clist_lock);
881 list_del(&client->list);
882 mutex_unlock(&adapter->clist_lock);
884 init_completion(&client->released);
885 device_unregister(&client->dev);
886 wait_for_completion(&client->released);
891 EXPORT_SYMBOL(i2c_detach_client);
894 * i2c_use_client - increments the reference count of the i2c client structure
895 * @client: the client being referenced
897 * Each live reference to a client should be refcounted. The driver model does
898 * that automatically as part of driver binding, so that most drivers don't
899 * need to do this explicitly: they hold a reference until they're unbound
902 * A pointer to the client with the incremented reference counter is returned.
904 struct i2c_client *i2c_use_client(struct i2c_client *client)
906 if (client && get_device(&client->dev))
910 EXPORT_SYMBOL(i2c_use_client);
913 * i2c_release_client - release a use of the i2c client structure
914 * @client: the client being no longer referenced
916 * Must be called when a user of a client is finished with it.
918 void i2c_release_client(struct i2c_client *client)
921 put_device(&client->dev);
923 EXPORT_SYMBOL(i2c_release_client);
930 static int i2c_cmd(struct device *dev, void *_arg)
932 struct i2c_client *client = i2c_verify_client(dev);
933 struct i2c_cmd_arg *arg = _arg;
935 if (client && client->driver && client->driver->command)
936 client->driver->command(client, arg->cmd, arg->arg);
940 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
942 struct i2c_cmd_arg cmd_arg;
946 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
948 EXPORT_SYMBOL(i2c_clients_command);
950 static int __init i2c_init(void)
954 retval = bus_register(&i2c_bus_type);
957 retval = class_register(&i2c_adapter_class);
960 retval = i2c_add_driver(&dummy_driver);
966 class_unregister(&i2c_adapter_class);
968 bus_unregister(&i2c_bus_type);
972 static void __exit i2c_exit(void)
974 i2c_del_driver(&dummy_driver);
975 class_unregister(&i2c_adapter_class);
976 bus_unregister(&i2c_bus_type);
979 subsys_initcall(i2c_init);
980 module_exit(i2c_exit);
982 /* ----------------------------------------------------
983 * the functional interface to the i2c busses.
984 * ----------------------------------------------------
988 * i2c_transfer - execute a single or combined I2C message
989 * @adap: Handle to I2C bus
990 * @msgs: One or more messages to execute before STOP is issued to
991 * terminate the operation; each message begins with a START.
992 * @num: Number of messages to be executed.
994 * Returns negative errno, else the number of messages executed.
996 * Note that there is no requirement that each message be sent to
997 * the same slave address, although that is the most common model.
999 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
1003 /* REVISIT the fault reporting model here is weak:
1005 * - When we get an error after receiving N bytes from a slave,
1006 * there is no way to report "N".
1008 * - When we get a NAK after transmitting N bytes to a slave,
1009 * there is no way to report "N" ... or to let the master
1010 * continue executing the rest of this combined message, if
1011 * that's the appropriate response.
1013 * - When for example "num" is two and we successfully complete
1014 * the first message but get an error part way through the
1015 * second, it's unclear whether that should be reported as
1016 * one (discarding status on the second message) or errno
1017 * (discarding status on the first one).
1020 if (adap->algo->master_xfer) {
1022 for (ret = 0; ret < num; ret++) {
1023 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1024 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1025 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1026 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1030 if (in_atomic() || irqs_disabled()) {
1031 ret = mutex_trylock(&adap->bus_lock);
1033 /* I2C activity is ongoing. */
1036 mutex_lock_nested(&adap->bus_lock, adap->level);
1039 ret = adap->algo->master_xfer(adap,msgs,num);
1040 mutex_unlock(&adap->bus_lock);
1044 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1048 EXPORT_SYMBOL(i2c_transfer);
1051 * i2c_master_send - issue a single I2C message in master transmit mode
1052 * @client: Handle to slave device
1053 * @buf: Data that will be written to the slave
1054 * @count: How many bytes to write
1056 * Returns negative errno, or else the number of bytes written.
1058 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1061 struct i2c_adapter *adap=client->adapter;
1064 msg.addr = client->addr;
1065 msg.flags = client->flags & I2C_M_TEN;
1067 msg.buf = (char *)buf;
1069 ret = i2c_transfer(adap, &msg, 1);
1071 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1072 transmitted, else error code. */
1073 return (ret == 1) ? count : ret;
1075 EXPORT_SYMBOL(i2c_master_send);
1078 * i2c_master_recv - issue a single I2C message in master receive mode
1079 * @client: Handle to slave device
1080 * @buf: Where to store data read from slave
1081 * @count: How many bytes to read
1083 * Returns negative errno, or else the number of bytes read.
1085 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1087 struct i2c_adapter *adap=client->adapter;
1091 msg.addr = client->addr;
1092 msg.flags = client->flags & I2C_M_TEN;
1093 msg.flags |= I2C_M_RD;
1097 ret = i2c_transfer(adap, &msg, 1);
1099 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1100 transmitted, else error code. */
1101 return (ret == 1) ? count : ret;
1103 EXPORT_SYMBOL(i2c_master_recv);
1105 /* ----------------------------------------------------
1106 * the i2c address scanning function
1107 * Will not work for 10-bit addresses!
1108 * ----------------------------------------------------
1110 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1111 int (*found_proc) (struct i2c_adapter *, int, int))
1115 /* Make sure the address is valid */
1116 if (addr < 0x03 || addr > 0x77) {
1117 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1122 /* Skip if already in use */
1123 if (i2c_check_addr(adapter, addr))
1126 /* Make sure there is something at this address, unless forced */
1128 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1129 I2C_SMBUS_QUICK, NULL) < 0)
1132 /* prevent 24RF08 corruption */
1133 if ((addr & ~0x0f) == 0x50)
1134 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1135 I2C_SMBUS_QUICK, NULL);
1138 /* Finally call the custom detection function */
1139 err = found_proc(adapter, addr, kind);
1140 /* -ENODEV can be returned if there is a chip at the given address
1141 but it isn't supported by this chip driver. We catch it here as
1142 this isn't an error. */
1147 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1152 int i2c_probe(struct i2c_adapter *adapter,
1153 const struct i2c_client_address_data *address_data,
1154 int (*found_proc) (struct i2c_adapter *, int, int))
1157 int adap_id = i2c_adapter_id(adapter);
1159 /* Force entries are done first, and are not affected by ignore
1161 if (address_data->forces) {
1162 const unsigned short * const *forces = address_data->forces;
1165 for (kind = 0; forces[kind]; kind++) {
1166 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1168 if (forces[kind][i] == adap_id
1169 || forces[kind][i] == ANY_I2C_BUS) {
1170 dev_dbg(&adapter->dev, "found force "
1171 "parameter for adapter %d, "
1172 "addr 0x%02x, kind %d\n",
1173 adap_id, forces[kind][i + 1],
1175 err = i2c_probe_address(adapter,
1176 forces[kind][i + 1],
1185 /* Stop here if we can't use SMBUS_QUICK */
1186 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1187 if (address_data->probe[0] == I2C_CLIENT_END
1188 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1191 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1192 "can't probe for chips\n");
1196 /* Probe entries are done second, and are not affected by ignore
1198 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1199 if (address_data->probe[i] == adap_id
1200 || address_data->probe[i] == ANY_I2C_BUS) {
1201 dev_dbg(&adapter->dev, "found probe parameter for "
1202 "adapter %d, addr 0x%02x\n", adap_id,
1203 address_data->probe[i + 1]);
1204 err = i2c_probe_address(adapter,
1205 address_data->probe[i + 1],
1212 /* Normal entries are done last, unless shadowed by an ignore entry */
1213 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1217 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1219 if ((address_data->ignore[j] == adap_id ||
1220 address_data->ignore[j] == ANY_I2C_BUS)
1221 && address_data->ignore[j + 1]
1222 == address_data->normal_i2c[i]) {
1223 dev_dbg(&adapter->dev, "found ignore "
1224 "parameter for adapter %d, "
1225 "addr 0x%02x\n", adap_id,
1226 address_data->ignore[j + 1]);
1234 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1235 "addr 0x%02x\n", adap_id,
1236 address_data->normal_i2c[i]);
1237 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1245 EXPORT_SYMBOL(i2c_probe);
1247 /* Separate detection function for new-style drivers */
1248 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1249 struct i2c_driver *driver)
1251 struct i2c_board_info info;
1252 struct i2c_adapter *adapter = temp_client->adapter;
1253 int addr = temp_client->addr;
1256 /* Make sure the address is valid */
1257 if (addr < 0x03 || addr > 0x77) {
1258 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1263 /* Skip if already in use */
1264 if (i2c_check_addr(adapter, addr))
1267 /* Make sure there is something at this address, unless forced */
1269 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1270 I2C_SMBUS_QUICK, NULL) < 0)
1273 /* prevent 24RF08 corruption */
1274 if ((addr & ~0x0f) == 0x50)
1275 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1276 I2C_SMBUS_QUICK, NULL);
1279 /* Finally call the custom detection function */
1280 memset(&info, 0, sizeof(struct i2c_board_info));
1282 err = driver->detect(temp_client, kind, &info);
1284 /* -ENODEV is returned if the detection fails. We catch it
1285 here as this isn't an error. */
1286 return err == -ENODEV ? 0 : err;
1289 /* Consistency check */
1290 if (info.type[0] == '\0') {
1291 dev_err(&adapter->dev, "%s detection function provided "
1292 "no name for 0x%x\n", driver->driver.name,
1295 struct i2c_client *client;
1297 /* Detection succeeded, instantiate the device */
1298 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1299 info.type, info.addr);
1300 client = i2c_new_device(adapter, &info);
1302 list_add_tail(&client->detected, &driver->clients);
1304 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1305 info.type, info.addr);
1310 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1312 const struct i2c_client_address_data *address_data;
1313 struct i2c_client *temp_client;
1315 int adap_id = i2c_adapter_id(adapter);
1317 address_data = driver->address_data;
1318 if (!driver->detect || !address_data)
1321 /* Set up a temporary client to help detect callback */
1322 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1325 temp_client->adapter = adapter;
1327 /* Force entries are done first, and are not affected by ignore
1329 if (address_data->forces) {
1330 const unsigned short * const *forces = address_data->forces;
1333 for (kind = 0; forces[kind]; kind++) {
1334 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1336 if (forces[kind][i] == adap_id
1337 || forces[kind][i] == ANY_I2C_BUS) {
1338 dev_dbg(&adapter->dev, "found force "
1339 "parameter for adapter %d, "
1340 "addr 0x%02x, kind %d\n",
1341 adap_id, forces[kind][i + 1],
1343 temp_client->addr = forces[kind][i + 1];
1344 err = i2c_detect_address(temp_client,
1353 /* Stop here if we can't use SMBUS_QUICK */
1354 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1355 if (address_data->probe[0] == I2C_CLIENT_END
1356 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1359 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1360 "can't probe for chips\n");
1365 /* Stop here if the classes do not match */
1366 if (!(adapter->class & driver->class))
1369 /* Probe entries are done second, and are not affected by ignore
1371 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1372 if (address_data->probe[i] == adap_id
1373 || address_data->probe[i] == ANY_I2C_BUS) {
1374 dev_dbg(&adapter->dev, "found probe parameter for "
1375 "adapter %d, addr 0x%02x\n", adap_id,
1376 address_data->probe[i + 1]);
1377 temp_client->addr = address_data->probe[i + 1];
1378 err = i2c_detect_address(temp_client, -1, driver);
1384 /* Normal entries are done last, unless shadowed by an ignore entry */
1385 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1389 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1391 if ((address_data->ignore[j] == adap_id ||
1392 address_data->ignore[j] == ANY_I2C_BUS)
1393 && address_data->ignore[j + 1]
1394 == address_data->normal_i2c[i]) {
1395 dev_dbg(&adapter->dev, "found ignore "
1396 "parameter for adapter %d, "
1397 "addr 0x%02x\n", adap_id,
1398 address_data->ignore[j + 1]);
1406 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1407 "addr 0x%02x\n", adap_id,
1408 address_data->normal_i2c[i]);
1409 temp_client->addr = address_data->normal_i2c[i];
1410 err = i2c_detect_address(temp_client, -1, driver);
1421 i2c_new_probed_device(struct i2c_adapter *adap,
1422 struct i2c_board_info *info,
1423 unsigned short const *addr_list)
1427 /* Stop here if the bus doesn't support probing */
1428 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1429 dev_err(&adap->dev, "Probing not supported\n");
1433 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1434 /* Check address validity */
1435 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1436 dev_warn(&adap->dev, "Invalid 7-bit address "
1437 "0x%02x\n", addr_list[i]);
1441 /* Check address availability */
1442 if (i2c_check_addr(adap, addr_list[i])) {
1443 dev_dbg(&adap->dev, "Address 0x%02x already in "
1444 "use, not probing\n", addr_list[i]);
1448 /* Test address responsiveness
1449 The default probe method is a quick write, but it is known
1450 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1451 and could also irreversibly write-protect some EEPROMs, so
1452 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1453 read instead. Also, some bus drivers don't implement
1454 quick write, so we fallback to a byte read it that case
1456 if ((addr_list[i] & ~0x07) == 0x30
1457 || (addr_list[i] & ~0x0f) == 0x50
1458 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1459 union i2c_smbus_data data;
1461 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1463 I2C_SMBUS_BYTE, &data) >= 0)
1466 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1468 I2C_SMBUS_QUICK, NULL) >= 0)
1473 if (addr_list[i] == I2C_CLIENT_END) {
1474 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1478 info->addr = addr_list[i];
1479 return i2c_new_device(adap, info);
1481 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1483 struct i2c_adapter* i2c_get_adapter(int id)
1485 struct i2c_adapter *adapter;
1487 mutex_lock(&core_lock);
1488 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1489 if (adapter && !try_module_get(adapter->owner))
1492 mutex_unlock(&core_lock);
1495 EXPORT_SYMBOL(i2c_get_adapter);
1497 void i2c_put_adapter(struct i2c_adapter *adap)
1499 module_put(adap->owner);
1501 EXPORT_SYMBOL(i2c_put_adapter);
1503 /* The SMBus parts */
1505 #define POLY (0x1070U << 3)
1511 for(i = 0; i < 8; i++) {
1516 return (u8)(data >> 8);
1519 /* Incremental CRC8 over count bytes in the array pointed to by p */
1520 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1524 for(i = 0; i < count; i++)
1525 crc = crc8((crc ^ p[i]) << 8);
1529 /* Assume a 7-bit address, which is reasonable for SMBus */
1530 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1532 /* The address will be sent first */
1533 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1534 pec = i2c_smbus_pec(pec, &addr, 1);
1536 /* The data buffer follows */
1537 return i2c_smbus_pec(pec, msg->buf, msg->len);
1540 /* Used for write only transactions */
1541 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1543 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1547 /* Return <0 on CRC error
1548 If there was a write before this read (most cases) we need to take the
1549 partial CRC from the write part into account.
1550 Note that this function does modify the message (we need to decrease the
1551 message length to hide the CRC byte from the caller). */
1552 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1554 u8 rpec = msg->buf[--msg->len];
1555 cpec = i2c_smbus_msg_pec(cpec, msg);
1558 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1566 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1567 * @client: Handle to slave device
1569 * This executes the SMBus "receive byte" protocol, returning negative errno
1570 * else the byte received from the device.
1572 s32 i2c_smbus_read_byte(struct i2c_client *client)
1574 union i2c_smbus_data data;
1577 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1579 I2C_SMBUS_BYTE, &data);
1580 return (status < 0) ? status : data.byte;
1582 EXPORT_SYMBOL(i2c_smbus_read_byte);
1585 * i2c_smbus_write_byte - SMBus "send byte" protocol
1586 * @client: Handle to slave device
1587 * @value: Byte to be sent
1589 * This executes the SMBus "send byte" protocol, returning negative errno
1590 * else zero on success.
1592 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1594 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1595 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1597 EXPORT_SYMBOL(i2c_smbus_write_byte);
1600 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1601 * @client: Handle to slave device
1602 * @command: Byte interpreted by slave
1604 * This executes the SMBus "read byte" protocol, returning negative errno
1605 * else a data byte received from the device.
1607 s32 i2c_smbus_read_byte_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_BYTE_DATA, &data);
1615 return (status < 0) ? status : data.byte;
1617 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1620 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1621 * @client: Handle to slave device
1622 * @command: Byte interpreted by slave
1623 * @value: Byte being written
1625 * This executes the SMBus "write byte" protocol, returning negative errno
1626 * else zero on success.
1628 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1630 union i2c_smbus_data data;
1632 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1633 I2C_SMBUS_WRITE,command,
1634 I2C_SMBUS_BYTE_DATA,&data);
1636 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1639 * i2c_smbus_read_word_data - SMBus "read word" protocol
1640 * @client: Handle to slave device
1641 * @command: Byte interpreted by slave
1643 * This executes the SMBus "read word" protocol, returning negative errno
1644 * else a 16-bit unsigned "word" received from the device.
1646 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1648 union i2c_smbus_data data;
1651 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1652 I2C_SMBUS_READ, command,
1653 I2C_SMBUS_WORD_DATA, &data);
1654 return (status < 0) ? status : data.word;
1656 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1659 * i2c_smbus_write_word_data - SMBus "write word" protocol
1660 * @client: Handle to slave device
1661 * @command: Byte interpreted by slave
1662 * @value: 16-bit "word" being written
1664 * This executes the SMBus "write word" protocol, returning negative errno
1665 * else zero on success.
1667 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1669 union i2c_smbus_data data;
1671 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1672 I2C_SMBUS_WRITE,command,
1673 I2C_SMBUS_WORD_DATA,&data);
1675 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1678 * i2c_smbus_read_block_data - SMBus "block read" protocol
1679 * @client: Handle to slave device
1680 * @command: Byte interpreted by slave
1681 * @values: Byte array into which data will be read; big enough to hold
1682 * the data returned by the slave. SMBus allows at most 32 bytes.
1684 * This executes the SMBus "block read" protocol, returning negative errno
1685 * else the number of data bytes in the slave's response.
1687 * Note that using this function requires that the client's adapter support
1688 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1689 * support this; its emulation through I2C messaging relies on a specific
1690 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1692 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1695 union i2c_smbus_data data;
1698 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1699 I2C_SMBUS_READ, command,
1700 I2C_SMBUS_BLOCK_DATA, &data);
1704 memcpy(values, &data.block[1], data.block[0]);
1705 return data.block[0];
1707 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1710 * i2c_smbus_write_block_data - SMBus "block write" protocol
1711 * @client: Handle to slave device
1712 * @command: Byte interpreted by slave
1713 * @length: Size of data block; SMBus allows at most 32 bytes
1714 * @values: Byte array which will be written.
1716 * This executes the SMBus "block write" protocol, returning negative errno
1717 * else zero on success.
1719 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1720 u8 length, const u8 *values)
1722 union i2c_smbus_data data;
1724 if (length > I2C_SMBUS_BLOCK_MAX)
1725 length = I2C_SMBUS_BLOCK_MAX;
1726 data.block[0] = length;
1727 memcpy(&data.block[1], values, length);
1728 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1729 I2C_SMBUS_WRITE,command,
1730 I2C_SMBUS_BLOCK_DATA,&data);
1732 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1734 /* Returns the number of read bytes */
1735 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1736 u8 length, u8 *values)
1738 union i2c_smbus_data data;
1741 if (length > I2C_SMBUS_BLOCK_MAX)
1742 length = I2C_SMBUS_BLOCK_MAX;
1743 data.block[0] = length;
1744 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1745 I2C_SMBUS_READ, command,
1746 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1750 memcpy(values, &data.block[1], data.block[0]);
1751 return data.block[0];
1753 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1755 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1756 u8 length, const u8 *values)
1758 union i2c_smbus_data data;
1760 if (length > I2C_SMBUS_BLOCK_MAX)
1761 length = I2C_SMBUS_BLOCK_MAX;
1762 data.block[0] = length;
1763 memcpy(data.block + 1, values, length);
1764 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1765 I2C_SMBUS_WRITE, command,
1766 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1768 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1770 /* Simulate a SMBus command using the i2c protocol
1771 No checking of parameters is done! */
1772 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1773 unsigned short flags,
1774 char read_write, u8 command, int size,
1775 union i2c_smbus_data * data)
1777 /* So we need to generate a series of msgs. In the case of writing, we
1778 need to use only one message; when reading, we need two. We initialize
1779 most things with sane defaults, to keep the code below somewhat
1781 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1782 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1783 int num = read_write == I2C_SMBUS_READ?2:1;
1784 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1785 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1791 msgbuf0[0] = command;
1793 case I2C_SMBUS_QUICK:
1795 /* Special case: The read/write field is used as data */
1796 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1799 case I2C_SMBUS_BYTE:
1800 if (read_write == I2C_SMBUS_READ) {
1801 /* Special case: only a read! */
1802 msg[0].flags = I2C_M_RD | flags;
1806 case I2C_SMBUS_BYTE_DATA:
1807 if (read_write == I2C_SMBUS_READ)
1811 msgbuf0[1] = data->byte;
1814 case I2C_SMBUS_WORD_DATA:
1815 if (read_write == I2C_SMBUS_READ)
1819 msgbuf0[1] = data->word & 0xff;
1820 msgbuf0[2] = data->word >> 8;
1823 case I2C_SMBUS_PROC_CALL:
1824 num = 2; /* Special case */
1825 read_write = I2C_SMBUS_READ;
1828 msgbuf0[1] = data->word & 0xff;
1829 msgbuf0[2] = data->word >> 8;
1831 case I2C_SMBUS_BLOCK_DATA:
1832 if (read_write == I2C_SMBUS_READ) {
1833 msg[1].flags |= I2C_M_RECV_LEN;
1834 msg[1].len = 1; /* block length will be added by
1835 the underlying bus driver */
1837 msg[0].len = data->block[0] + 2;
1838 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1839 dev_err(&adapter->dev,
1840 "Invalid block write size %d\n",
1844 for (i = 1; i < msg[0].len; i++)
1845 msgbuf0[i] = data->block[i-1];
1848 case I2C_SMBUS_BLOCK_PROC_CALL:
1849 num = 2; /* Another special case */
1850 read_write = I2C_SMBUS_READ;
1851 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1852 dev_err(&adapter->dev,
1853 "Invalid block write size %d\n",
1857 msg[0].len = data->block[0] + 2;
1858 for (i = 1; i < msg[0].len; i++)
1859 msgbuf0[i] = data->block[i-1];
1860 msg[1].flags |= I2C_M_RECV_LEN;
1861 msg[1].len = 1; /* block length will be added by
1862 the underlying bus driver */
1864 case I2C_SMBUS_I2C_BLOCK_DATA:
1865 if (read_write == I2C_SMBUS_READ) {
1866 msg[1].len = data->block[0];
1868 msg[0].len = data->block[0] + 1;
1869 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1870 dev_err(&adapter->dev,
1871 "Invalid block write size %d\n",
1875 for (i = 1; i <= data->block[0]; i++)
1876 msgbuf0[i] = data->block[i];
1880 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1884 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1885 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1887 /* Compute PEC if first message is a write */
1888 if (!(msg[0].flags & I2C_M_RD)) {
1889 if (num == 1) /* Write only */
1890 i2c_smbus_add_pec(&msg[0]);
1891 else /* Write followed by read */
1892 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1894 /* Ask for PEC if last message is a read */
1895 if (msg[num-1].flags & I2C_M_RD)
1899 status = i2c_transfer(adapter, msg, num);
1903 /* Check PEC if last message is a read */
1904 if (i && (msg[num-1].flags & I2C_M_RD)) {
1905 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1910 if (read_write == I2C_SMBUS_READ)
1912 case I2C_SMBUS_BYTE:
1913 data->byte = msgbuf0[0];
1915 case I2C_SMBUS_BYTE_DATA:
1916 data->byte = msgbuf1[0];
1918 case I2C_SMBUS_WORD_DATA:
1919 case I2C_SMBUS_PROC_CALL:
1920 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1922 case I2C_SMBUS_I2C_BLOCK_DATA:
1923 for (i = 0; i < data->block[0]; i++)
1924 data->block[i+1] = msgbuf1[i];
1926 case I2C_SMBUS_BLOCK_DATA:
1927 case I2C_SMBUS_BLOCK_PROC_CALL:
1928 for (i = 0; i < msgbuf1[0] + 1; i++)
1929 data->block[i] = msgbuf1[i];
1936 * i2c_smbus_xfer - execute SMBus protocol operations
1937 * @adapter: Handle to I2C bus
1938 * @addr: Address of SMBus slave on that bus
1939 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1940 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1941 * @command: Byte interpreted by slave, for protocols which use such bytes
1942 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1943 * @data: Data to be read or written
1945 * This executes an SMBus protocol operation, and returns a negative
1946 * errno code else zero on success.
1948 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1949 char read_write, u8 command, int protocol,
1950 union i2c_smbus_data * data)
1954 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1956 if (adapter->algo->smbus_xfer) {
1957 mutex_lock(&adapter->bus_lock);
1958 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1959 command, protocol, data);
1960 mutex_unlock(&adapter->bus_lock);
1962 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1963 command, protocol, data);
1967 EXPORT_SYMBOL(i2c_smbus_xfer);
1969 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1970 MODULE_DESCRIPTION("I2C-Bus main module");
1971 MODULE_LICENSE("GPL");