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/seq_file.h>
33 #include <linux/platform_device.h>
34 #include <linux/mutex.h>
35 #include <linux/completion.h>
36 #include <linux/hardirq.h>
37 #include <linux/irqflags.h>
38 #include <linux/semaphore.h>
39 #include <asm/uaccess.h>
44 static DEFINE_MUTEX(core_lock);
45 static DEFINE_IDR(i2c_adapter_idr);
47 #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
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);
106 const struct i2c_device_id *id;
111 client->driver = driver;
112 dev_dbg(dev, "probe\n");
114 if (driver->id_table)
115 id = i2c_match_id(driver->id_table, client);
118 status = driver->probe(client, id);
120 client->driver = NULL;
124 static int i2c_device_remove(struct device *dev)
126 struct i2c_client *client = to_i2c_client(dev);
127 struct i2c_driver *driver;
133 driver = to_i2c_driver(dev->driver);
134 if (driver->remove) {
135 dev_dbg(dev, "remove\n");
136 status = driver->remove(client);
142 client->driver = NULL;
146 static void i2c_device_shutdown(struct device *dev)
148 struct i2c_driver *driver;
152 driver = to_i2c_driver(dev->driver);
153 if (driver->shutdown)
154 driver->shutdown(to_i2c_client(dev));
157 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
159 struct i2c_driver *driver;
163 driver = to_i2c_driver(dev->driver);
164 if (!driver->suspend)
166 return driver->suspend(to_i2c_client(dev), mesg);
169 static int i2c_device_resume(struct device * dev)
171 struct i2c_driver *driver;
175 driver = to_i2c_driver(dev->driver);
178 return driver->resume(to_i2c_client(dev));
181 static void i2c_client_release(struct device *dev)
183 struct i2c_client *client = to_i2c_client(dev);
184 complete(&client->released);
187 static void i2c_client_dev_release(struct device *dev)
189 kfree(to_i2c_client(dev));
192 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
194 struct i2c_client *client = to_i2c_client(dev);
195 return sprintf(buf, "%s\n", client->name);
198 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
200 struct i2c_client *client = to_i2c_client(dev);
201 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
204 static struct device_attribute i2c_dev_attrs[] = {
205 __ATTR(name, S_IRUGO, show_client_name, NULL),
206 /* modalias helps coldplug: modprobe $(cat .../modalias) */
207 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
211 static struct bus_type i2c_bus_type = {
213 .dev_attrs = i2c_dev_attrs,
214 .match = i2c_device_match,
215 .uevent = i2c_device_uevent,
216 .probe = i2c_device_probe,
217 .remove = i2c_device_remove,
218 .shutdown = i2c_device_shutdown,
219 .suspend = i2c_device_suspend,
220 .resume = i2c_device_resume,
225 * i2c_verify_client - return parameter as i2c_client, or NULL
226 * @dev: device, probably from some driver model iterator
228 * When traversing the driver model tree, perhaps using driver model
229 * iterators like @device_for_each_child(), you can't assume very much
230 * about the nodes you find. Use this function to avoid oopses caused
231 * by wrongly treating some non-I2C device as an i2c_client.
233 struct i2c_client *i2c_verify_client(struct device *dev)
235 return (dev->bus == &i2c_bus_type)
239 EXPORT_SYMBOL(i2c_verify_client);
243 * i2c_new_device - instantiate an i2c device for use with a new style driver
244 * @adap: the adapter managing the device
245 * @info: describes one I2C device; bus_num is ignored
248 * Create a device to work with a new style i2c driver, where binding is
249 * handled through driver model probe()/remove() methods. This call is not
250 * appropriate for use by mainboad initialization logic, which usually runs
251 * during an arch_initcall() long before any i2c_adapter could exist.
253 * This returns the new i2c client, which may be saved for later use with
254 * i2c_unregister_device(); or NULL to indicate an error.
257 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
259 struct i2c_client *client;
262 client = kzalloc(sizeof *client, GFP_KERNEL);
266 client->adapter = adap;
268 client->dev.platform_data = info->platform_data;
269 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
271 client->flags = info->flags & ~I2C_CLIENT_WAKE;
272 client->addr = info->addr;
273 client->irq = info->irq;
275 strlcpy(client->name, info->type, sizeof(client->name));
277 /* a new style driver may be bound to this device when we
278 * return from this function, or any later moment (e.g. maybe
279 * hotplugging will load the driver module). and the device
280 * refcount model is the standard driver model one.
282 status = i2c_attach_client(client);
289 EXPORT_SYMBOL_GPL(i2c_new_device);
293 * i2c_unregister_device - reverse effect of i2c_new_device()
294 * @client: value returned from i2c_new_device()
297 void i2c_unregister_device(struct i2c_client *client)
299 struct i2c_adapter *adapter = client->adapter;
300 struct i2c_driver *driver = client->driver;
302 if (driver && !is_newstyle_driver(driver)) {
303 dev_err(&client->dev, "can't unregister devices "
304 "with legacy drivers\n");
309 mutex_lock(&adapter->clist_lock);
310 list_del(&client->list);
311 mutex_unlock(&adapter->clist_lock);
313 device_unregister(&client->dev);
315 EXPORT_SYMBOL_GPL(i2c_unregister_device);
318 static const struct i2c_device_id dummy_id[] = {
323 static int dummy_probe(struct i2c_client *client,
324 const struct i2c_device_id *id)
329 static int dummy_remove(struct i2c_client *client)
334 static struct i2c_driver dummy_driver = {
335 .driver.name = "dummy",
336 .probe = dummy_probe,
337 .remove = dummy_remove,
338 .id_table = dummy_id,
342 * i2c_new_dummy - return a new i2c device bound to a dummy driver
343 * @adapter: the adapter managing the device
344 * @address: seven bit address to be used
347 * This returns an I2C client bound to the "dummy" driver, intended for use
348 * with devices that consume multiple addresses. Examples of such chips
349 * include various EEPROMS (like 24c04 and 24c08 models).
351 * These dummy devices have two main uses. First, most I2C and SMBus calls
352 * except i2c_transfer() need a client handle; the dummy will be that handle.
353 * And second, this prevents the specified address from being bound to a
356 * This returns the new i2c client, which should be saved for later use with
357 * i2c_unregister_device(); or NULL to indicate an error.
360 i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
362 struct i2c_board_info info = {
363 I2C_BOARD_INFO("dummy", address),
366 return i2c_new_device(adapter, &info);
368 EXPORT_SYMBOL_GPL(i2c_new_dummy);
370 /* ------------------------------------------------------------------------- */
372 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
374 static void i2c_adapter_dev_release(struct device *dev)
376 struct i2c_adapter *adap = to_i2c_adapter(dev);
377 complete(&adap->dev_released);
381 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
383 struct i2c_adapter *adap = to_i2c_adapter(dev);
384 return sprintf(buf, "%s\n", adap->name);
387 static struct device_attribute i2c_adapter_attrs[] = {
388 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
392 static struct class i2c_adapter_class = {
393 .owner = THIS_MODULE,
394 .name = "i2c-adapter",
395 .dev_attrs = i2c_adapter_attrs,
398 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
400 struct i2c_devinfo *devinfo;
402 mutex_lock(&__i2c_board_lock);
403 list_for_each_entry(devinfo, &__i2c_board_list, list) {
404 if (devinfo->busnum == adapter->nr
405 && !i2c_new_device(adapter,
406 &devinfo->board_info))
407 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
408 i2c_adapter_id(adapter),
409 devinfo->board_info.addr);
411 mutex_unlock(&__i2c_board_lock);
414 static int i2c_do_add_adapter(struct device_driver *d, void *data)
416 struct i2c_driver *driver = to_i2c_driver(d);
417 struct i2c_adapter *adap = data;
419 if (driver->attach_adapter) {
420 /* We ignore the return code; if it fails, too bad */
421 driver->attach_adapter(adap);
426 static int i2c_register_adapter(struct i2c_adapter *adap)
430 mutex_init(&adap->bus_lock);
431 mutex_init(&adap->clist_lock);
432 INIT_LIST_HEAD(&adap->clients);
434 mutex_lock(&core_lock);
436 /* Add the adapter to the driver core.
437 * If the parent pointer is not set up,
438 * we add this adapter to the host bus.
440 if (adap->dev.parent == NULL) {
441 adap->dev.parent = &platform_bus;
442 pr_debug("I2C adapter driver [%s] forgot to specify "
443 "physical device\n", adap->name);
445 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
446 adap->dev.release = &i2c_adapter_dev_release;
447 adap->dev.class = &i2c_adapter_class;
448 res = device_register(&adap->dev);
452 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
454 /* create pre-declared device nodes for new-style drivers */
455 if (adap->nr < __i2c_first_dynamic_bus_num)
456 i2c_scan_static_board_info(adap);
458 /* let legacy drivers scan this bus for matching devices */
459 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
463 mutex_unlock(&core_lock);
467 idr_remove(&i2c_adapter_idr, adap->nr);
472 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
473 * @adapter: the adapter to add
476 * This routine is used to declare an I2C adapter when its bus number
477 * doesn't matter. Examples: for I2C adapters dynamically added by
478 * USB links or PCI plugin cards.
480 * When this returns zero, a new bus number was allocated and stored
481 * in adap->nr, and the specified adapter became available for clients.
482 * Otherwise, a negative errno value is returned.
484 int i2c_add_adapter(struct i2c_adapter *adapter)
489 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
492 mutex_lock(&core_lock);
493 /* "above" here means "above or equal to", sigh */
494 res = idr_get_new_above(&i2c_adapter_idr, adapter,
495 __i2c_first_dynamic_bus_num, &id);
496 mutex_unlock(&core_lock);
505 return i2c_register_adapter(adapter);
507 EXPORT_SYMBOL(i2c_add_adapter);
510 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
511 * @adap: the adapter to register (with adap->nr initialized)
514 * This routine is used to declare an I2C adapter when its bus number
515 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
516 * or otherwise built in to the system's mainboard, and where i2c_board_info
517 * is used to properly configure I2C devices.
519 * If no devices have pre-been declared for this bus, then be sure to
520 * register the adapter before any dynamically allocated ones. Otherwise
521 * the required bus ID may not be available.
523 * When this returns zero, the specified adapter became available for
524 * clients using the bus number provided in adap->nr. Also, the table
525 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
526 * and the appropriate driver model device nodes are created. Otherwise, a
527 * negative errno value is returned.
529 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
534 if (adap->nr & ~MAX_ID_MASK)
538 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
541 mutex_lock(&core_lock);
542 /* "above" here means "above or equal to", sigh;
543 * we need the "equal to" result to force the result
545 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
546 if (status == 0 && id != adap->nr) {
548 idr_remove(&i2c_adapter_idr, id);
550 mutex_unlock(&core_lock);
551 if (status == -EAGAIN)
555 status = i2c_register_adapter(adap);
558 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
560 static int i2c_do_del_adapter(struct device_driver *d, void *data)
562 struct i2c_driver *driver = to_i2c_driver(d);
563 struct i2c_adapter *adapter = data;
566 if (!driver->detach_adapter)
568 res = driver->detach_adapter(adapter);
570 dev_err(&adapter->dev, "detach_adapter failed (%d) "
571 "for driver [%s]\n", res, driver->driver.name);
576 * i2c_del_adapter - unregister I2C adapter
577 * @adap: the adapter being unregistered
580 * This unregisters an I2C adapter which was previously registered
581 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
583 int i2c_del_adapter(struct i2c_adapter *adap)
585 struct list_head *item, *_n;
586 struct i2c_client *client;
589 mutex_lock(&core_lock);
591 /* First make sure that this adapter was ever added */
592 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
593 pr_debug("i2c-core: attempting to delete unregistered "
594 "adapter [%s]\n", adap->name);
599 /* Tell drivers about this removal */
600 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
605 /* detach any active clients. This must be done first, because
606 * it can fail; in which case we give up. */
607 list_for_each_safe(item, _n, &adap->clients) {
608 struct i2c_driver *driver;
610 client = list_entry(item, struct i2c_client, list);
611 driver = client->driver;
613 /* new style, follow standard driver model */
614 if (!driver || is_newstyle_driver(driver)) {
615 i2c_unregister_device(client);
619 /* legacy drivers create and remove clients themselves */
620 if ((res = driver->detach_client(client))) {
621 dev_err(&adap->dev, "detach_client failed for client "
622 "[%s] at address 0x%02x\n", client->name,
628 /* clean up the sysfs representation */
629 init_completion(&adap->dev_released);
630 device_unregister(&adap->dev);
632 /* wait for sysfs to drop all references */
633 wait_for_completion(&adap->dev_released);
636 idr_remove(&i2c_adapter_idr, adap->nr);
638 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
641 mutex_unlock(&core_lock);
644 EXPORT_SYMBOL(i2c_del_adapter);
647 /* ------------------------------------------------------------------------- */
650 * An i2c_driver is used with one or more i2c_client (device) nodes to access
651 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
652 * are two models for binding the driver to its device: "new style" drivers
653 * follow the standard Linux driver model and just respond to probe() calls
654 * issued if the driver core sees they match(); "legacy" drivers create device
658 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
662 /* new style driver methods can't mix with legacy ones */
663 if (is_newstyle_driver(driver)) {
664 if (driver->attach_adapter || driver->detach_adapter
665 || driver->detach_client) {
667 "i2c-core: driver [%s] is confused\n",
668 driver->driver.name);
673 /* add the driver to the list of i2c drivers in the driver core */
674 driver->driver.owner = owner;
675 driver->driver.bus = &i2c_bus_type;
677 /* for new style drivers, when registration returns the driver core
678 * will have called probe() for all matching-but-unbound devices.
680 res = driver_register(&driver->driver);
684 mutex_lock(&core_lock);
686 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
688 /* legacy drivers scan i2c busses directly */
689 if (driver->attach_adapter) {
690 struct i2c_adapter *adapter;
692 down(&i2c_adapter_class.sem);
693 list_for_each_entry(adapter, &i2c_adapter_class.devices,
695 driver->attach_adapter(adapter);
697 up(&i2c_adapter_class.sem);
700 mutex_unlock(&core_lock);
703 EXPORT_SYMBOL(i2c_register_driver);
706 * i2c_del_driver - unregister I2C driver
707 * @driver: the driver being unregistered
710 void i2c_del_driver(struct i2c_driver *driver)
712 struct list_head *item2, *_n;
713 struct i2c_client *client;
714 struct i2c_adapter *adap;
716 mutex_lock(&core_lock);
718 /* new-style driver? */
719 if (is_newstyle_driver(driver))
722 /* Have a look at each adapter, if clients of this driver are still
723 * attached. If so, detach them to be able to kill the driver
726 down(&i2c_adapter_class.sem);
727 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
728 if (driver->detach_adapter) {
729 if (driver->detach_adapter(adap)) {
730 dev_err(&adap->dev, "detach_adapter failed "
732 driver->driver.name);
735 list_for_each_safe(item2, _n, &adap->clients) {
736 client = list_entry(item2, struct i2c_client, list);
737 if (client->driver != driver)
739 dev_dbg(&adap->dev, "detaching client [%s] "
740 "at 0x%02x\n", client->name,
742 if (driver->detach_client(client)) {
743 dev_err(&adap->dev, "detach_client "
744 "failed for client [%s] at "
745 "0x%02x\n", client->name,
751 up(&i2c_adapter_class.sem);
754 driver_unregister(&driver->driver);
755 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
757 mutex_unlock(&core_lock);
759 EXPORT_SYMBOL(i2c_del_driver);
761 /* ------------------------------------------------------------------------- */
763 static int __i2c_check_addr(struct device *dev, void *addrp)
765 struct i2c_client *client = i2c_verify_client(dev);
766 int addr = *(int *)addrp;
768 if (client && client->addr == addr)
773 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
775 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
778 int i2c_attach_client(struct i2c_client *client)
780 struct i2c_adapter *adapter = client->adapter;
783 client->dev.parent = &client->adapter->dev;
784 client->dev.bus = &i2c_bus_type;
787 client->dev.driver = &client->driver->driver;
789 if (client->driver && !is_newstyle_driver(client->driver)) {
790 client->dev.release = i2c_client_release;
791 client->dev.uevent_suppress = 1;
793 client->dev.release = i2c_client_dev_release;
795 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
796 "%d-%04x", i2c_adapter_id(adapter), client->addr);
797 res = device_register(&client->dev);
801 mutex_lock(&adapter->clist_lock);
802 list_add_tail(&client->list, &adapter->clients);
803 mutex_unlock(&adapter->clist_lock);
805 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
806 client->name, client->dev.bus_id);
808 if (adapter->client_register) {
809 if (adapter->client_register(client)) {
810 dev_dbg(&adapter->dev, "client_register "
811 "failed for client [%s] at 0x%02x\n",
812 client->name, client->addr);
819 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
820 "(%d)\n", client->name, client->addr, res);
823 EXPORT_SYMBOL(i2c_attach_client);
825 int i2c_detach_client(struct i2c_client *client)
827 struct i2c_adapter *adapter = client->adapter;
830 if (adapter->client_unregister) {
831 res = adapter->client_unregister(client);
833 dev_err(&client->dev,
834 "client_unregister [%s] failed, "
835 "client not detached\n", client->name);
840 mutex_lock(&adapter->clist_lock);
841 list_del(&client->list);
842 mutex_unlock(&adapter->clist_lock);
844 init_completion(&client->released);
845 device_unregister(&client->dev);
846 wait_for_completion(&client->released);
851 EXPORT_SYMBOL(i2c_detach_client);
854 * i2c_use_client - increments the reference count of the i2c client structure
855 * @client: the client being referenced
857 * Each live reference to a client should be refcounted. The driver model does
858 * that automatically as part of driver binding, so that most drivers don't
859 * need to do this explicitly: they hold a reference until they're unbound
862 * A pointer to the client with the incremented reference counter is returned.
864 struct i2c_client *i2c_use_client(struct i2c_client *client)
866 get_device(&client->dev);
869 EXPORT_SYMBOL(i2c_use_client);
872 * i2c_release_client - release a use of the i2c client structure
873 * @client: the client being no longer referenced
875 * Must be called when a user of a client is finished with it.
877 void i2c_release_client(struct i2c_client *client)
879 put_device(&client->dev);
881 EXPORT_SYMBOL(i2c_release_client);
888 static int i2c_cmd(struct device *dev, void *_arg)
890 struct i2c_client *client = i2c_verify_client(dev);
891 struct i2c_cmd_arg *arg = _arg;
893 if (client && client->driver && client->driver->command)
894 client->driver->command(client, arg->cmd, arg->arg);
898 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
900 struct i2c_cmd_arg cmd_arg;
904 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
906 EXPORT_SYMBOL(i2c_clients_command);
908 static int __init i2c_init(void)
912 retval = bus_register(&i2c_bus_type);
915 retval = class_register(&i2c_adapter_class);
918 retval = i2c_add_driver(&dummy_driver);
924 class_unregister(&i2c_adapter_class);
926 bus_unregister(&i2c_bus_type);
930 static void __exit i2c_exit(void)
932 i2c_del_driver(&dummy_driver);
933 class_unregister(&i2c_adapter_class);
934 bus_unregister(&i2c_bus_type);
937 subsys_initcall(i2c_init);
938 module_exit(i2c_exit);
940 /* ----------------------------------------------------
941 * the functional interface to the i2c busses.
942 * ----------------------------------------------------
945 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
949 if (adap->algo->master_xfer) {
951 for (ret = 0; ret < num; ret++) {
952 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
953 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
954 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
955 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
959 if (in_atomic() || irqs_disabled()) {
960 ret = mutex_trylock(&adap->bus_lock);
962 /* I2C activity is ongoing. */
965 mutex_lock_nested(&adap->bus_lock, adap->level);
968 ret = adap->algo->master_xfer(adap,msgs,num);
969 mutex_unlock(&adap->bus_lock);
973 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
977 EXPORT_SYMBOL(i2c_transfer);
979 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
982 struct i2c_adapter *adap=client->adapter;
985 msg.addr = client->addr;
986 msg.flags = client->flags & I2C_M_TEN;
988 msg.buf = (char *)buf;
990 ret = i2c_transfer(adap, &msg, 1);
992 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
993 transmitted, else error code. */
994 return (ret == 1) ? count : ret;
996 EXPORT_SYMBOL(i2c_master_send);
998 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1000 struct i2c_adapter *adap=client->adapter;
1004 msg.addr = client->addr;
1005 msg.flags = client->flags & I2C_M_TEN;
1006 msg.flags |= I2C_M_RD;
1010 ret = i2c_transfer(adap, &msg, 1);
1012 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1013 transmitted, else error code. */
1014 return (ret == 1) ? count : ret;
1016 EXPORT_SYMBOL(i2c_master_recv);
1018 /* ----------------------------------------------------
1019 * the i2c address scanning function
1020 * Will not work for 10-bit addresses!
1021 * ----------------------------------------------------
1023 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1024 int (*found_proc) (struct i2c_adapter *, int, int))
1028 /* Make sure the address is valid */
1029 if (addr < 0x03 || addr > 0x77) {
1030 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1035 /* Skip if already in use */
1036 if (i2c_check_addr(adapter, addr))
1039 /* Make sure there is something at this address, unless forced */
1041 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1042 I2C_SMBUS_QUICK, NULL) < 0)
1045 /* prevent 24RF08 corruption */
1046 if ((addr & ~0x0f) == 0x50)
1047 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1048 I2C_SMBUS_QUICK, NULL);
1051 /* Finally call the custom detection function */
1052 err = found_proc(adapter, addr, kind);
1053 /* -ENODEV can be returned if there is a chip at the given address
1054 but it isn't supported by this chip driver. We catch it here as
1055 this isn't an error. */
1060 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1065 int i2c_probe(struct i2c_adapter *adapter,
1066 const struct i2c_client_address_data *address_data,
1067 int (*found_proc) (struct i2c_adapter *, int, int))
1070 int adap_id = i2c_adapter_id(adapter);
1072 /* Force entries are done first, and are not affected by ignore
1074 if (address_data->forces) {
1075 const unsigned short * const *forces = address_data->forces;
1078 for (kind = 0; forces[kind]; kind++) {
1079 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1081 if (forces[kind][i] == adap_id
1082 || forces[kind][i] == ANY_I2C_BUS) {
1083 dev_dbg(&adapter->dev, "found force "
1084 "parameter for adapter %d, "
1085 "addr 0x%02x, kind %d\n",
1086 adap_id, forces[kind][i + 1],
1088 err = i2c_probe_address(adapter,
1089 forces[kind][i + 1],
1098 /* Stop here if we can't use SMBUS_QUICK */
1099 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1100 if (address_data->probe[0] == I2C_CLIENT_END
1101 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1104 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1105 "can't probe for chips\n");
1109 /* Probe entries are done second, and are not affected by ignore
1111 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1112 if (address_data->probe[i] == adap_id
1113 || address_data->probe[i] == ANY_I2C_BUS) {
1114 dev_dbg(&adapter->dev, "found probe parameter for "
1115 "adapter %d, addr 0x%02x\n", adap_id,
1116 address_data->probe[i + 1]);
1117 err = i2c_probe_address(adapter,
1118 address_data->probe[i + 1],
1125 /* Normal entries are done last, unless shadowed by an ignore entry */
1126 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1130 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1132 if ((address_data->ignore[j] == adap_id ||
1133 address_data->ignore[j] == ANY_I2C_BUS)
1134 && address_data->ignore[j + 1]
1135 == address_data->normal_i2c[i]) {
1136 dev_dbg(&adapter->dev, "found ignore "
1137 "parameter for adapter %d, "
1138 "addr 0x%02x\n", adap_id,
1139 address_data->ignore[j + 1]);
1147 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1148 "addr 0x%02x\n", adap_id,
1149 address_data->normal_i2c[i]);
1150 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1158 EXPORT_SYMBOL(i2c_probe);
1161 i2c_new_probed_device(struct i2c_adapter *adap,
1162 struct i2c_board_info *info,
1163 unsigned short const *addr_list)
1167 /* Stop here if the bus doesn't support probing */
1168 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1169 dev_err(&adap->dev, "Probing not supported\n");
1173 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1174 /* Check address validity */
1175 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1176 dev_warn(&adap->dev, "Invalid 7-bit address "
1177 "0x%02x\n", addr_list[i]);
1181 /* Check address availability */
1182 if (i2c_check_addr(adap, addr_list[i])) {
1183 dev_dbg(&adap->dev, "Address 0x%02x already in "
1184 "use, not probing\n", addr_list[i]);
1188 /* Test address responsiveness
1189 The default probe method is a quick write, but it is known
1190 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1191 and could also irreversibly write-protect some EEPROMs, so
1192 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1193 read instead. Also, some bus drivers don't implement
1194 quick write, so we fallback to a byte read it that case
1196 if ((addr_list[i] & ~0x07) == 0x30
1197 || (addr_list[i] & ~0x0f) == 0x50
1198 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1199 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1201 I2C_SMBUS_BYTE, NULL) >= 0)
1204 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1206 I2C_SMBUS_QUICK, NULL) >= 0)
1211 if (addr_list[i] == I2C_CLIENT_END) {
1212 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1216 info->addr = addr_list[i];
1217 return i2c_new_device(adap, info);
1219 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1221 struct i2c_adapter* i2c_get_adapter(int id)
1223 struct i2c_adapter *adapter;
1225 mutex_lock(&core_lock);
1226 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1227 if (adapter && !try_module_get(adapter->owner))
1230 mutex_unlock(&core_lock);
1233 EXPORT_SYMBOL(i2c_get_adapter);
1235 void i2c_put_adapter(struct i2c_adapter *adap)
1237 module_put(adap->owner);
1239 EXPORT_SYMBOL(i2c_put_adapter);
1241 /* The SMBus parts */
1243 #define POLY (0x1070U << 3)
1249 for(i = 0; i < 8; i++) {
1254 return (u8)(data >> 8);
1257 /* Incremental CRC8 over count bytes in the array pointed to by p */
1258 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1262 for(i = 0; i < count; i++)
1263 crc = crc8((crc ^ p[i]) << 8);
1267 /* Assume a 7-bit address, which is reasonable for SMBus */
1268 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1270 /* The address will be sent first */
1271 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1272 pec = i2c_smbus_pec(pec, &addr, 1);
1274 /* The data buffer follows */
1275 return i2c_smbus_pec(pec, msg->buf, msg->len);
1278 /* Used for write only transactions */
1279 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1281 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1285 /* Return <0 on CRC error
1286 If there was a write before this read (most cases) we need to take the
1287 partial CRC from the write part into account.
1288 Note that this function does modify the message (we need to decrease the
1289 message length to hide the CRC byte from the caller). */
1290 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1292 u8 rpec = msg->buf[--msg->len];
1293 cpec = i2c_smbus_msg_pec(cpec, msg);
1296 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1303 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1305 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1306 value,0,I2C_SMBUS_QUICK,NULL);
1308 EXPORT_SYMBOL(i2c_smbus_write_quick);
1310 s32 i2c_smbus_read_byte(struct i2c_client *client)
1312 union i2c_smbus_data data;
1313 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1314 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1319 EXPORT_SYMBOL(i2c_smbus_read_byte);
1321 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1323 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1324 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1326 EXPORT_SYMBOL(i2c_smbus_write_byte);
1328 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1330 union i2c_smbus_data data;
1331 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1332 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1337 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1339 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1341 union i2c_smbus_data data;
1343 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1344 I2C_SMBUS_WRITE,command,
1345 I2C_SMBUS_BYTE_DATA,&data);
1347 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1349 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1351 union i2c_smbus_data data;
1352 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1353 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1358 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1360 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1362 union i2c_smbus_data data;
1364 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1365 I2C_SMBUS_WRITE,command,
1366 I2C_SMBUS_WORD_DATA,&data);
1368 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1371 * i2c_smbus_read_block_data - SMBus block read request
1372 * @client: Handle to slave device
1373 * @command: Command byte issued to let the slave know what data should
1375 * @values: Byte array into which data will be read; big enough to hold
1376 * the data returned by the slave. SMBus allows at most 32 bytes.
1378 * Returns the number of bytes read in the slave's response, else a
1379 * negative number to indicate some kind of error.
1381 * Note that using this function requires that the client's adapter support
1382 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1383 * support this; its emulation through I2C messaging relies on a specific
1384 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1386 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1389 union i2c_smbus_data data;
1391 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1392 I2C_SMBUS_READ, command,
1393 I2C_SMBUS_BLOCK_DATA, &data))
1396 memcpy(values, &data.block[1], data.block[0]);
1397 return data.block[0];
1399 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1401 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1402 u8 length, const u8 *values)
1404 union i2c_smbus_data data;
1406 if (length > I2C_SMBUS_BLOCK_MAX)
1407 length = I2C_SMBUS_BLOCK_MAX;
1408 data.block[0] = length;
1409 memcpy(&data.block[1], values, length);
1410 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1411 I2C_SMBUS_WRITE,command,
1412 I2C_SMBUS_BLOCK_DATA,&data);
1414 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1416 /* Returns the number of read bytes */
1417 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1418 u8 length, u8 *values)
1420 union i2c_smbus_data data;
1422 if (length > I2C_SMBUS_BLOCK_MAX)
1423 length = I2C_SMBUS_BLOCK_MAX;
1424 data.block[0] = length;
1425 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1426 I2C_SMBUS_READ,command,
1427 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1430 memcpy(values, &data.block[1], data.block[0]);
1431 return data.block[0];
1433 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1435 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1436 u8 length, const u8 *values)
1438 union i2c_smbus_data data;
1440 if (length > I2C_SMBUS_BLOCK_MAX)
1441 length = I2C_SMBUS_BLOCK_MAX;
1442 data.block[0] = length;
1443 memcpy(data.block + 1, values, length);
1444 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1445 I2C_SMBUS_WRITE, command,
1446 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1448 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1450 /* Simulate a SMBus command using the i2c protocol
1451 No checking of parameters is done! */
1452 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1453 unsigned short flags,
1454 char read_write, u8 command, int size,
1455 union i2c_smbus_data * data)
1457 /* So we need to generate a series of msgs. In the case of writing, we
1458 need to use only one message; when reading, we need two. We initialize
1459 most things with sane defaults, to keep the code below somewhat
1461 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1462 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1463 int num = read_write == I2C_SMBUS_READ?2:1;
1464 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1465 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1470 msgbuf0[0] = command;
1472 case I2C_SMBUS_QUICK:
1474 /* Special case: The read/write field is used as data */
1475 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1478 case I2C_SMBUS_BYTE:
1479 if (read_write == I2C_SMBUS_READ) {
1480 /* Special case: only a read! */
1481 msg[0].flags = I2C_M_RD | flags;
1485 case I2C_SMBUS_BYTE_DATA:
1486 if (read_write == I2C_SMBUS_READ)
1490 msgbuf0[1] = data->byte;
1493 case I2C_SMBUS_WORD_DATA:
1494 if (read_write == I2C_SMBUS_READ)
1498 msgbuf0[1] = data->word & 0xff;
1499 msgbuf0[2] = data->word >> 8;
1502 case I2C_SMBUS_PROC_CALL:
1503 num = 2; /* Special case */
1504 read_write = I2C_SMBUS_READ;
1507 msgbuf0[1] = data->word & 0xff;
1508 msgbuf0[2] = data->word >> 8;
1510 case I2C_SMBUS_BLOCK_DATA:
1511 if (read_write == I2C_SMBUS_READ) {
1512 msg[1].flags |= I2C_M_RECV_LEN;
1513 msg[1].len = 1; /* block length will be added by
1514 the underlying bus driver */
1516 msg[0].len = data->block[0] + 2;
1517 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1518 dev_err(&adapter->dev, "smbus_access called with "
1519 "invalid block write size (%d)\n",
1523 for (i = 1; i < msg[0].len; i++)
1524 msgbuf0[i] = data->block[i-1];
1527 case I2C_SMBUS_BLOCK_PROC_CALL:
1528 num = 2; /* Another special case */
1529 read_write = I2C_SMBUS_READ;
1530 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1531 dev_err(&adapter->dev, "%s called with invalid "
1532 "block proc call size (%d)\n", __func__,
1536 msg[0].len = data->block[0] + 2;
1537 for (i = 1; i < msg[0].len; i++)
1538 msgbuf0[i] = data->block[i-1];
1539 msg[1].flags |= I2C_M_RECV_LEN;
1540 msg[1].len = 1; /* block length will be added by
1541 the underlying bus driver */
1543 case I2C_SMBUS_I2C_BLOCK_DATA:
1544 if (read_write == I2C_SMBUS_READ) {
1545 msg[1].len = data->block[0];
1547 msg[0].len = data->block[0] + 1;
1548 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1549 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1550 "invalid block write size (%d)\n",
1554 for (i = 1; i <= data->block[0]; i++)
1555 msgbuf0[i] = data->block[i];
1559 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1564 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1565 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1567 /* Compute PEC if first message is a write */
1568 if (!(msg[0].flags & I2C_M_RD)) {
1569 if (num == 1) /* Write only */
1570 i2c_smbus_add_pec(&msg[0]);
1571 else /* Write followed by read */
1572 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1574 /* Ask for PEC if last message is a read */
1575 if (msg[num-1].flags & I2C_M_RD)
1579 if (i2c_transfer(adapter, msg, num) < 0)
1582 /* Check PEC if last message is a read */
1583 if (i && (msg[num-1].flags & I2C_M_RD)) {
1584 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1588 if (read_write == I2C_SMBUS_READ)
1590 case I2C_SMBUS_BYTE:
1591 data->byte = msgbuf0[0];
1593 case I2C_SMBUS_BYTE_DATA:
1594 data->byte = msgbuf1[0];
1596 case I2C_SMBUS_WORD_DATA:
1597 case I2C_SMBUS_PROC_CALL:
1598 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1600 case I2C_SMBUS_I2C_BLOCK_DATA:
1601 for (i = 0; i < data->block[0]; i++)
1602 data->block[i+1] = msgbuf1[i];
1604 case I2C_SMBUS_BLOCK_DATA:
1605 case I2C_SMBUS_BLOCK_PROC_CALL:
1606 for (i = 0; i < msgbuf1[0] + 1; i++)
1607 data->block[i] = msgbuf1[i];
1614 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1615 char read_write, u8 command, int size,
1616 union i2c_smbus_data * data)
1620 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1622 if (adapter->algo->smbus_xfer) {
1623 mutex_lock(&adapter->bus_lock);
1624 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1626 mutex_unlock(&adapter->bus_lock);
1628 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1633 EXPORT_SYMBOL(i2c_smbus_xfer);
1635 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1636 MODULE_DESCRIPTION("I2C-Bus main module");
1637 MODULE_LICENSE("GPL");