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 <asm/uaccess.h>
39 #include <asm/semaphore.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 int i2c_device_match(struct device *dev, struct device_driver *drv)
53 struct i2c_client *client = to_i2c_client(dev);
54 struct i2c_driver *driver = to_i2c_driver(drv);
56 /* make legacy i2c drivers bypass driver model probing entirely;
57 * such drivers scan each i2c adapter/bus themselves.
59 if (!is_newstyle_driver(driver))
62 /* new style drivers use the same kind of driver matching policy
63 * as platform devices or SPI: compare device and driver IDs.
65 return strcmp(client->driver_name, drv->name) == 0;
70 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
71 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
73 struct i2c_client *client = to_i2c_client(dev);
75 /* by definition, legacy drivers can't hotplug */
76 if (dev->driver || !client->driver_name)
79 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
81 dev_dbg(dev, "uevent\n");
86 #define i2c_device_uevent NULL
87 #endif /* CONFIG_HOTPLUG */
89 static int i2c_device_probe(struct device *dev)
91 struct i2c_client *client = to_i2c_client(dev);
92 struct i2c_driver *driver = to_i2c_driver(dev->driver);
97 client->driver = driver;
98 dev_dbg(dev, "probe\n");
99 status = driver->probe(client);
101 client->driver = NULL;
105 static int i2c_device_remove(struct device *dev)
107 struct i2c_client *client = to_i2c_client(dev);
108 struct i2c_driver *driver;
114 driver = to_i2c_driver(dev->driver);
115 if (driver->remove) {
116 dev_dbg(dev, "remove\n");
117 status = driver->remove(client);
123 client->driver = NULL;
127 static void i2c_device_shutdown(struct device *dev)
129 struct i2c_driver *driver;
133 driver = to_i2c_driver(dev->driver);
134 if (driver->shutdown)
135 driver->shutdown(to_i2c_client(dev));
138 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
140 struct i2c_driver *driver;
144 driver = to_i2c_driver(dev->driver);
145 if (!driver->suspend)
147 return driver->suspend(to_i2c_client(dev), mesg);
150 static int i2c_device_resume(struct device * dev)
152 struct i2c_driver *driver;
156 driver = to_i2c_driver(dev->driver);
159 return driver->resume(to_i2c_client(dev));
162 static void i2c_client_release(struct device *dev)
164 struct i2c_client *client = to_i2c_client(dev);
165 complete(&client->released);
168 static void i2c_client_dev_release(struct device *dev)
170 kfree(to_i2c_client(dev));
173 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
175 struct i2c_client *client = to_i2c_client(dev);
176 return sprintf(buf, "%s\n", client->name);
179 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
181 struct i2c_client *client = to_i2c_client(dev);
182 return client->driver_name
183 ? sprintf(buf, "%s\n", client->driver_name)
187 static struct device_attribute i2c_dev_attrs[] = {
188 __ATTR(name, S_IRUGO, show_client_name, NULL),
189 /* modalias helps coldplug: modprobe $(cat .../modalias) */
190 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
194 static struct bus_type i2c_bus_type = {
196 .dev_attrs = i2c_dev_attrs,
197 .match = i2c_device_match,
198 .uevent = i2c_device_uevent,
199 .probe = i2c_device_probe,
200 .remove = i2c_device_remove,
201 .shutdown = i2c_device_shutdown,
202 .suspend = i2c_device_suspend,
203 .resume = i2c_device_resume,
208 * i2c_verify_client - return parameter as i2c_client, or NULL
209 * @dev: device, probably from some driver model iterator
211 * When traversing the driver model tree, perhaps using driver model
212 * iterators like @device_for_each_child(), you can't assume very much
213 * about the nodes you find. Use this function to avoid oopses caused
214 * by wrongly treating some non-I2C device as an i2c_client.
216 struct i2c_client *i2c_verify_client(struct device *dev)
218 return (dev->bus == &i2c_bus_type)
222 EXPORT_SYMBOL(i2c_verify_client);
226 * i2c_new_device - instantiate an i2c device for use with a new style driver
227 * @adap: the adapter managing the device
228 * @info: describes one I2C device; bus_num is ignored
231 * Create a device to work with a new style i2c driver, where binding is
232 * handled through driver model probe()/remove() methods. This call is not
233 * appropriate for use by mainboad initialization logic, which usually runs
234 * during an arch_initcall() long before any i2c_adapter could exist.
236 * This returns the new i2c client, which may be saved for later use with
237 * i2c_unregister_device(); or NULL to indicate an error.
240 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
242 struct i2c_client *client;
245 client = kzalloc(sizeof *client, GFP_KERNEL);
249 client->adapter = adap;
251 client->dev.platform_data = info->platform_data;
252 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
254 client->flags = info->flags & ~I2C_CLIENT_WAKE;
255 client->addr = info->addr;
256 client->irq = info->irq;
258 strlcpy(client->driver_name, info->driver_name,
259 sizeof(client->driver_name));
260 strlcpy(client->name, info->type, sizeof(client->name));
262 /* a new style driver may be bound to this device when we
263 * return from this function, or any later moment (e.g. maybe
264 * hotplugging will load the driver module). and the device
265 * refcount model is the standard driver model one.
267 status = i2c_attach_client(client);
274 EXPORT_SYMBOL_GPL(i2c_new_device);
278 * i2c_unregister_device - reverse effect of i2c_new_device()
279 * @client: value returned from i2c_new_device()
282 void i2c_unregister_device(struct i2c_client *client)
284 struct i2c_adapter *adapter = client->adapter;
285 struct i2c_driver *driver = client->driver;
287 if (driver && !is_newstyle_driver(driver)) {
288 dev_err(&client->dev, "can't unregister devices "
289 "with legacy drivers\n");
294 mutex_lock(&adapter->clist_lock);
295 list_del(&client->list);
296 mutex_unlock(&adapter->clist_lock);
298 device_unregister(&client->dev);
300 EXPORT_SYMBOL_GPL(i2c_unregister_device);
303 static int dummy_nop(struct i2c_client *client)
308 static struct i2c_driver dummy_driver = {
309 .driver.name = "dummy",
315 * i2c_new_dummy - return a new i2c device bound to a dummy driver
316 * @adapter: the adapter managing the device
317 * @address: seven bit address to be used
318 * @type: optional label used for i2c_client.name
321 * This returns an I2C client bound to the "dummy" driver, intended for use
322 * with devices that consume multiple addresses. Examples of such chips
323 * include various EEPROMS (like 24c04 and 24c08 models).
325 * These dummy devices have two main uses. First, most I2C and SMBus calls
326 * except i2c_transfer() need a client handle; the dummy will be that handle.
327 * And second, this prevents the specified address from being bound to a
330 * This returns the new i2c client, which should be saved for later use with
331 * i2c_unregister_device(); or NULL to indicate an error.
334 i2c_new_dummy(struct i2c_adapter *adapter, u16 address, const char *type)
336 struct i2c_board_info info = {
337 .driver_name = "dummy",
342 strlcpy(info.type, type, sizeof info.type);
343 return i2c_new_device(adapter, &info);
345 EXPORT_SYMBOL_GPL(i2c_new_dummy);
347 /* ------------------------------------------------------------------------- */
349 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
351 static void i2c_adapter_dev_release(struct device *dev)
353 struct i2c_adapter *adap = to_i2c_adapter(dev);
354 complete(&adap->dev_released);
358 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
360 struct i2c_adapter *adap = to_i2c_adapter(dev);
361 return sprintf(buf, "%s\n", adap->name);
364 static struct device_attribute i2c_adapter_attrs[] = {
365 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
369 static struct class i2c_adapter_class = {
370 .owner = THIS_MODULE,
371 .name = "i2c-adapter",
372 .dev_attrs = i2c_adapter_attrs,
375 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
377 struct i2c_devinfo *devinfo;
379 mutex_lock(&__i2c_board_lock);
380 list_for_each_entry(devinfo, &__i2c_board_list, list) {
381 if (devinfo->busnum == adapter->nr
382 && !i2c_new_device(adapter,
383 &devinfo->board_info))
384 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
385 i2c_adapter_id(adapter),
386 devinfo->board_info.addr);
388 mutex_unlock(&__i2c_board_lock);
391 static int i2c_do_add_adapter(struct device_driver *d, void *data)
393 struct i2c_driver *driver = to_i2c_driver(d);
394 struct i2c_adapter *adap = data;
396 if (driver->attach_adapter) {
397 /* We ignore the return code; if it fails, too bad */
398 driver->attach_adapter(adap);
403 static int i2c_register_adapter(struct i2c_adapter *adap)
407 mutex_init(&adap->bus_lock);
408 mutex_init(&adap->clist_lock);
409 INIT_LIST_HEAD(&adap->clients);
411 mutex_lock(&core_lock);
413 /* Add the adapter to the driver core.
414 * If the parent pointer is not set up,
415 * we add this adapter to the host bus.
417 if (adap->dev.parent == NULL) {
418 adap->dev.parent = &platform_bus;
419 pr_debug("I2C adapter driver [%s] forgot to specify "
420 "physical device\n", adap->name);
422 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
423 adap->dev.release = &i2c_adapter_dev_release;
424 adap->dev.class = &i2c_adapter_class;
425 res = device_register(&adap->dev);
429 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
431 /* create pre-declared device nodes for new-style drivers */
432 if (adap->nr < __i2c_first_dynamic_bus_num)
433 i2c_scan_static_board_info(adap);
435 /* let legacy drivers scan this bus for matching devices */
436 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
440 mutex_unlock(&core_lock);
444 idr_remove(&i2c_adapter_idr, adap->nr);
449 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
450 * @adapter: the adapter to add
453 * This routine is used to declare an I2C adapter when its bus number
454 * doesn't matter. Examples: for I2C adapters dynamically added by
455 * USB links or PCI plugin cards.
457 * When this returns zero, a new bus number was allocated and stored
458 * in adap->nr, and the specified adapter became available for clients.
459 * Otherwise, a negative errno value is returned.
461 int i2c_add_adapter(struct i2c_adapter *adapter)
466 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
469 mutex_lock(&core_lock);
470 /* "above" here means "above or equal to", sigh */
471 res = idr_get_new_above(&i2c_adapter_idr, adapter,
472 __i2c_first_dynamic_bus_num, &id);
473 mutex_unlock(&core_lock);
482 return i2c_register_adapter(adapter);
484 EXPORT_SYMBOL(i2c_add_adapter);
487 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
488 * @adap: the adapter to register (with adap->nr initialized)
491 * This routine is used to declare an I2C adapter when its bus number
492 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
493 * or otherwise built in to the system's mainboard, and where i2c_board_info
494 * is used to properly configure I2C devices.
496 * If no devices have pre-been declared for this bus, then be sure to
497 * register the adapter before any dynamically allocated ones. Otherwise
498 * the required bus ID may not be available.
500 * When this returns zero, the specified adapter became available for
501 * clients using the bus number provided in adap->nr. Also, the table
502 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
503 * and the appropriate driver model device nodes are created. Otherwise, a
504 * negative errno value is returned.
506 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
511 if (adap->nr & ~MAX_ID_MASK)
515 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
518 mutex_lock(&core_lock);
519 /* "above" here means "above or equal to", sigh;
520 * we need the "equal to" result to force the result
522 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
523 if (status == 0 && id != adap->nr) {
525 idr_remove(&i2c_adapter_idr, id);
527 mutex_unlock(&core_lock);
528 if (status == -EAGAIN)
532 status = i2c_register_adapter(adap);
535 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
537 static int i2c_do_del_adapter(struct device_driver *d, void *data)
539 struct i2c_driver *driver = to_i2c_driver(d);
540 struct i2c_adapter *adapter = data;
543 if (!driver->detach_adapter)
545 res = driver->detach_adapter(adapter);
547 dev_err(&adapter->dev, "detach_adapter failed (%d) "
548 "for driver [%s]\n", res, driver->driver.name);
553 * i2c_del_adapter - unregister I2C adapter
554 * @adap: the adapter being unregistered
557 * This unregisters an I2C adapter which was previously registered
558 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
560 int i2c_del_adapter(struct i2c_adapter *adap)
562 struct list_head *item, *_n;
563 struct i2c_client *client;
566 mutex_lock(&core_lock);
568 /* First make sure that this adapter was ever added */
569 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
570 pr_debug("i2c-core: attempting to delete unregistered "
571 "adapter [%s]\n", adap->name);
576 /* Tell drivers about this removal */
577 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
582 /* detach any active clients. This must be done first, because
583 * it can fail; in which case we give up. */
584 list_for_each_safe(item, _n, &adap->clients) {
585 struct i2c_driver *driver;
587 client = list_entry(item, struct i2c_client, list);
588 driver = client->driver;
590 /* new style, follow standard driver model */
591 if (!driver || is_newstyle_driver(driver)) {
592 i2c_unregister_device(client);
596 /* legacy drivers create and remove clients themselves */
597 if ((res = driver->detach_client(client))) {
598 dev_err(&adap->dev, "detach_client failed for client "
599 "[%s] at address 0x%02x\n", client->name,
605 /* clean up the sysfs representation */
606 init_completion(&adap->dev_released);
607 device_unregister(&adap->dev);
609 /* wait for sysfs to drop all references */
610 wait_for_completion(&adap->dev_released);
613 idr_remove(&i2c_adapter_idr, adap->nr);
615 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
618 mutex_unlock(&core_lock);
621 EXPORT_SYMBOL(i2c_del_adapter);
624 /* ------------------------------------------------------------------------- */
627 * An i2c_driver is used with one or more i2c_client (device) nodes to access
628 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
629 * are two models for binding the driver to its device: "new style" drivers
630 * follow the standard Linux driver model and just respond to probe() calls
631 * issued if the driver core sees they match(); "legacy" drivers create device
635 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
639 /* new style driver methods can't mix with legacy ones */
640 if (is_newstyle_driver(driver)) {
641 if (driver->attach_adapter || driver->detach_adapter
642 || driver->detach_client) {
644 "i2c-core: driver [%s] is confused\n",
645 driver->driver.name);
650 /* add the driver to the list of i2c drivers in the driver core */
651 driver->driver.owner = owner;
652 driver->driver.bus = &i2c_bus_type;
654 /* for new style drivers, when registration returns the driver core
655 * will have called probe() for all matching-but-unbound devices.
657 res = driver_register(&driver->driver);
661 mutex_lock(&core_lock);
663 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
665 /* legacy drivers scan i2c busses directly */
666 if (driver->attach_adapter) {
667 struct i2c_adapter *adapter;
669 down(&i2c_adapter_class.sem);
670 list_for_each_entry(adapter, &i2c_adapter_class.devices,
672 driver->attach_adapter(adapter);
674 up(&i2c_adapter_class.sem);
677 mutex_unlock(&core_lock);
680 EXPORT_SYMBOL(i2c_register_driver);
683 * i2c_del_driver - unregister I2C driver
684 * @driver: the driver being unregistered
687 void i2c_del_driver(struct i2c_driver *driver)
689 struct list_head *item2, *_n;
690 struct i2c_client *client;
691 struct i2c_adapter *adap;
693 mutex_lock(&core_lock);
695 /* new-style driver? */
696 if (is_newstyle_driver(driver))
699 /* Have a look at each adapter, if clients of this driver are still
700 * attached. If so, detach them to be able to kill the driver
703 down(&i2c_adapter_class.sem);
704 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
705 if (driver->detach_adapter) {
706 if (driver->detach_adapter(adap)) {
707 dev_err(&adap->dev, "detach_adapter failed "
709 driver->driver.name);
712 list_for_each_safe(item2, _n, &adap->clients) {
713 client = list_entry(item2, struct i2c_client, list);
714 if (client->driver != driver)
716 dev_dbg(&adap->dev, "detaching client [%s] "
717 "at 0x%02x\n", client->name,
719 if (driver->detach_client(client)) {
720 dev_err(&adap->dev, "detach_client "
721 "failed for client [%s] at "
722 "0x%02x\n", client->name,
728 up(&i2c_adapter_class.sem);
731 driver_unregister(&driver->driver);
732 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
734 mutex_unlock(&core_lock);
736 EXPORT_SYMBOL(i2c_del_driver);
738 /* ------------------------------------------------------------------------- */
740 static int __i2c_check_addr(struct device *dev, void *addrp)
742 struct i2c_client *client = i2c_verify_client(dev);
743 int addr = *(int *)addrp;
745 if (client && client->addr == addr)
750 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
752 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
755 int i2c_attach_client(struct i2c_client *client)
757 struct i2c_adapter *adapter = client->adapter;
760 client->dev.parent = &client->adapter->dev;
761 client->dev.bus = &i2c_bus_type;
764 client->dev.driver = &client->driver->driver;
766 if (client->driver && !is_newstyle_driver(client->driver)) {
767 client->dev.release = i2c_client_release;
768 client->dev.uevent_suppress = 1;
770 client->dev.release = i2c_client_dev_release;
772 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
773 "%d-%04x", i2c_adapter_id(adapter), client->addr);
774 res = device_register(&client->dev);
778 mutex_lock(&adapter->clist_lock);
779 list_add_tail(&client->list, &adapter->clients);
780 mutex_unlock(&adapter->clist_lock);
782 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
783 client->name, client->dev.bus_id);
785 if (adapter->client_register) {
786 if (adapter->client_register(client)) {
787 dev_dbg(&adapter->dev, "client_register "
788 "failed for client [%s] at 0x%02x\n",
789 client->name, client->addr);
796 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
797 "(%d)\n", client->name, client->addr, res);
800 EXPORT_SYMBOL(i2c_attach_client);
802 int i2c_detach_client(struct i2c_client *client)
804 struct i2c_adapter *adapter = client->adapter;
807 if (adapter->client_unregister) {
808 res = adapter->client_unregister(client);
810 dev_err(&client->dev,
811 "client_unregister [%s] failed, "
812 "client not detached\n", client->name);
817 mutex_lock(&adapter->clist_lock);
818 list_del(&client->list);
819 mutex_unlock(&adapter->clist_lock);
821 init_completion(&client->released);
822 device_unregister(&client->dev);
823 wait_for_completion(&client->released);
828 EXPORT_SYMBOL(i2c_detach_client);
831 * i2c_use_client - increments the reference count of the i2c client structure
832 * @client: the client being referenced
834 * Each live reference to a client should be refcounted. The driver model does
835 * that automatically as part of driver binding, so that most drivers don't
836 * need to do this explicitly: they hold a reference until they're unbound
839 * A pointer to the client with the incremented reference counter is returned.
841 struct i2c_client *i2c_use_client(struct i2c_client *client)
843 get_device(&client->dev);
846 EXPORT_SYMBOL(i2c_use_client);
849 * i2c_release_client - release a use of the i2c client structure
850 * @client: the client being no longer referenced
852 * Must be called when a user of a client is finished with it.
854 void i2c_release_client(struct i2c_client *client)
856 put_device(&client->dev);
858 EXPORT_SYMBOL(i2c_release_client);
865 static int i2c_cmd(struct device *dev, void *_arg)
867 struct i2c_client *client = i2c_verify_client(dev);
868 struct i2c_cmd_arg *arg = _arg;
870 if (client && client->driver && client->driver->command)
871 client->driver->command(client, arg->cmd, arg->arg);
875 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
877 struct i2c_cmd_arg cmd_arg;
881 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
883 EXPORT_SYMBOL(i2c_clients_command);
885 static int __init i2c_init(void)
889 retval = bus_register(&i2c_bus_type);
892 retval = class_register(&i2c_adapter_class);
895 retval = i2c_add_driver(&dummy_driver);
901 class_unregister(&i2c_adapter_class);
903 bus_unregister(&i2c_bus_type);
907 static void __exit i2c_exit(void)
909 i2c_del_driver(&dummy_driver);
910 class_unregister(&i2c_adapter_class);
911 bus_unregister(&i2c_bus_type);
914 subsys_initcall(i2c_init);
915 module_exit(i2c_exit);
917 /* ----------------------------------------------------
918 * the functional interface to the i2c busses.
919 * ----------------------------------------------------
922 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
926 if (adap->algo->master_xfer) {
928 for (ret = 0; ret < num; ret++) {
929 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
930 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
931 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
932 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
936 if (in_atomic() || irqs_disabled()) {
937 ret = mutex_trylock(&adap->bus_lock);
939 /* I2C activity is ongoing. */
942 mutex_lock_nested(&adap->bus_lock, adap->level);
945 ret = adap->algo->master_xfer(adap,msgs,num);
946 mutex_unlock(&adap->bus_lock);
950 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
954 EXPORT_SYMBOL(i2c_transfer);
956 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
959 struct i2c_adapter *adap=client->adapter;
962 msg.addr = client->addr;
963 msg.flags = client->flags & I2C_M_TEN;
965 msg.buf = (char *)buf;
967 ret = i2c_transfer(adap, &msg, 1);
969 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
970 transmitted, else error code. */
971 return (ret == 1) ? count : ret;
973 EXPORT_SYMBOL(i2c_master_send);
975 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
977 struct i2c_adapter *adap=client->adapter;
981 msg.addr = client->addr;
982 msg.flags = client->flags & I2C_M_TEN;
983 msg.flags |= I2C_M_RD;
987 ret = i2c_transfer(adap, &msg, 1);
989 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
990 transmitted, else error code. */
991 return (ret == 1) ? count : ret;
993 EXPORT_SYMBOL(i2c_master_recv);
995 /* ----------------------------------------------------
996 * the i2c address scanning function
997 * Will not work for 10-bit addresses!
998 * ----------------------------------------------------
1000 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1001 int (*found_proc) (struct i2c_adapter *, int, int))
1005 /* Make sure the address is valid */
1006 if (addr < 0x03 || addr > 0x77) {
1007 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1012 /* Skip if already in use */
1013 if (i2c_check_addr(adapter, addr))
1016 /* Make sure there is something at this address, unless forced */
1018 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1019 I2C_SMBUS_QUICK, NULL) < 0)
1022 /* prevent 24RF08 corruption */
1023 if ((addr & ~0x0f) == 0x50)
1024 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1025 I2C_SMBUS_QUICK, NULL);
1028 /* Finally call the custom detection function */
1029 err = found_proc(adapter, addr, kind);
1030 /* -ENODEV can be returned if there is a chip at the given address
1031 but it isn't supported by this chip driver. We catch it here as
1032 this isn't an error. */
1037 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1042 int i2c_probe(struct i2c_adapter *adapter,
1043 const struct i2c_client_address_data *address_data,
1044 int (*found_proc) (struct i2c_adapter *, int, int))
1047 int adap_id = i2c_adapter_id(adapter);
1049 /* Force entries are done first, and are not affected by ignore
1051 if (address_data->forces) {
1052 const unsigned short * const *forces = address_data->forces;
1055 for (kind = 0; forces[kind]; kind++) {
1056 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1058 if (forces[kind][i] == adap_id
1059 || forces[kind][i] == ANY_I2C_BUS) {
1060 dev_dbg(&adapter->dev, "found force "
1061 "parameter for adapter %d, "
1062 "addr 0x%02x, kind %d\n",
1063 adap_id, forces[kind][i + 1],
1065 err = i2c_probe_address(adapter,
1066 forces[kind][i + 1],
1075 /* Stop here if we can't use SMBUS_QUICK */
1076 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1077 if (address_data->probe[0] == I2C_CLIENT_END
1078 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1081 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1082 "can't probe for chips\n");
1086 /* Probe entries are done second, and are not affected by ignore
1088 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1089 if (address_data->probe[i] == adap_id
1090 || address_data->probe[i] == ANY_I2C_BUS) {
1091 dev_dbg(&adapter->dev, "found probe parameter for "
1092 "adapter %d, addr 0x%02x\n", adap_id,
1093 address_data->probe[i + 1]);
1094 err = i2c_probe_address(adapter,
1095 address_data->probe[i + 1],
1102 /* Normal entries are done last, unless shadowed by an ignore entry */
1103 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1107 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1109 if ((address_data->ignore[j] == adap_id ||
1110 address_data->ignore[j] == ANY_I2C_BUS)
1111 && address_data->ignore[j + 1]
1112 == address_data->normal_i2c[i]) {
1113 dev_dbg(&adapter->dev, "found ignore "
1114 "parameter for adapter %d, "
1115 "addr 0x%02x\n", adap_id,
1116 address_data->ignore[j + 1]);
1124 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1125 "addr 0x%02x\n", adap_id,
1126 address_data->normal_i2c[i]);
1127 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1135 EXPORT_SYMBOL(i2c_probe);
1138 i2c_new_probed_device(struct i2c_adapter *adap,
1139 struct i2c_board_info *info,
1140 unsigned short const *addr_list)
1144 /* Stop here if the bus doesn't support probing */
1145 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1146 dev_err(&adap->dev, "Probing not supported\n");
1150 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1151 /* Check address validity */
1152 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1153 dev_warn(&adap->dev, "Invalid 7-bit address "
1154 "0x%02x\n", addr_list[i]);
1158 /* Check address availability */
1159 if (i2c_check_addr(adap, addr_list[i])) {
1160 dev_dbg(&adap->dev, "Address 0x%02x already in "
1161 "use, not probing\n", addr_list[i]);
1165 /* Test address responsiveness
1166 The default probe method is a quick write, but it is known
1167 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1168 and could also irreversibly write-protect some EEPROMs, so
1169 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1170 read instead. Also, some bus drivers don't implement
1171 quick write, so we fallback to a byte read it that case
1173 if ((addr_list[i] & ~0x07) == 0x30
1174 || (addr_list[i] & ~0x0f) == 0x50
1175 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1176 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1178 I2C_SMBUS_BYTE, NULL) >= 0)
1181 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1183 I2C_SMBUS_QUICK, NULL) >= 0)
1188 if (addr_list[i] == I2C_CLIENT_END) {
1189 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1193 info->addr = addr_list[i];
1194 return i2c_new_device(adap, info);
1196 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1198 struct i2c_adapter* i2c_get_adapter(int id)
1200 struct i2c_adapter *adapter;
1202 mutex_lock(&core_lock);
1203 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1204 if (adapter && !try_module_get(adapter->owner))
1207 mutex_unlock(&core_lock);
1210 EXPORT_SYMBOL(i2c_get_adapter);
1212 void i2c_put_adapter(struct i2c_adapter *adap)
1214 module_put(adap->owner);
1216 EXPORT_SYMBOL(i2c_put_adapter);
1218 /* The SMBus parts */
1220 #define POLY (0x1070U << 3)
1226 for(i = 0; i < 8; i++) {
1231 return (u8)(data >> 8);
1234 /* Incremental CRC8 over count bytes in the array pointed to by p */
1235 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1239 for(i = 0; i < count; i++)
1240 crc = crc8((crc ^ p[i]) << 8);
1244 /* Assume a 7-bit address, which is reasonable for SMBus */
1245 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1247 /* The address will be sent first */
1248 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1249 pec = i2c_smbus_pec(pec, &addr, 1);
1251 /* The data buffer follows */
1252 return i2c_smbus_pec(pec, msg->buf, msg->len);
1255 /* Used for write only transactions */
1256 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1258 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1262 /* Return <0 on CRC error
1263 If there was a write before this read (most cases) we need to take the
1264 partial CRC from the write part into account.
1265 Note that this function does modify the message (we need to decrease the
1266 message length to hide the CRC byte from the caller). */
1267 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1269 u8 rpec = msg->buf[--msg->len];
1270 cpec = i2c_smbus_msg_pec(cpec, msg);
1273 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1280 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1282 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1283 value,0,I2C_SMBUS_QUICK,NULL);
1285 EXPORT_SYMBOL(i2c_smbus_write_quick);
1287 s32 i2c_smbus_read_byte(struct i2c_client *client)
1289 union i2c_smbus_data data;
1290 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1291 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1296 EXPORT_SYMBOL(i2c_smbus_read_byte);
1298 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1300 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1301 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1303 EXPORT_SYMBOL(i2c_smbus_write_byte);
1305 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1307 union i2c_smbus_data data;
1308 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1309 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1314 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1316 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1318 union i2c_smbus_data data;
1320 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1321 I2C_SMBUS_WRITE,command,
1322 I2C_SMBUS_BYTE_DATA,&data);
1324 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1326 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1328 union i2c_smbus_data data;
1329 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1330 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1335 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1337 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1339 union i2c_smbus_data data;
1341 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1342 I2C_SMBUS_WRITE,command,
1343 I2C_SMBUS_WORD_DATA,&data);
1345 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1348 * i2c_smbus_read_block_data - SMBus block read request
1349 * @client: Handle to slave device
1350 * @command: Command byte issued to let the slave know what data should
1352 * @values: Byte array into which data will be read; big enough to hold
1353 * the data returned by the slave. SMBus allows at most 32 bytes.
1355 * Returns the number of bytes read in the slave's response, else a
1356 * negative number to indicate some kind of error.
1358 * Note that using this function requires that the client's adapter support
1359 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1360 * support this; its emulation through I2C messaging relies on a specific
1361 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1363 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1366 union i2c_smbus_data data;
1368 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1369 I2C_SMBUS_READ, command,
1370 I2C_SMBUS_BLOCK_DATA, &data))
1373 memcpy(values, &data.block[1], data.block[0]);
1374 return data.block[0];
1376 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1378 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1379 u8 length, const u8 *values)
1381 union i2c_smbus_data data;
1383 if (length > I2C_SMBUS_BLOCK_MAX)
1384 length = I2C_SMBUS_BLOCK_MAX;
1385 data.block[0] = length;
1386 memcpy(&data.block[1], values, length);
1387 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1388 I2C_SMBUS_WRITE,command,
1389 I2C_SMBUS_BLOCK_DATA,&data);
1391 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1393 /* Returns the number of read bytes */
1394 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1395 u8 length, u8 *values)
1397 union i2c_smbus_data data;
1399 if (length > I2C_SMBUS_BLOCK_MAX)
1400 length = I2C_SMBUS_BLOCK_MAX;
1401 data.block[0] = length;
1402 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1403 I2C_SMBUS_READ,command,
1404 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1407 memcpy(values, &data.block[1], data.block[0]);
1408 return data.block[0];
1410 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1412 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1413 u8 length, const u8 *values)
1415 union i2c_smbus_data data;
1417 if (length > I2C_SMBUS_BLOCK_MAX)
1418 length = I2C_SMBUS_BLOCK_MAX;
1419 data.block[0] = length;
1420 memcpy(data.block + 1, values, length);
1421 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1422 I2C_SMBUS_WRITE, command,
1423 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1425 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1427 /* Simulate a SMBus command using the i2c protocol
1428 No checking of parameters is done! */
1429 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1430 unsigned short flags,
1431 char read_write, u8 command, int size,
1432 union i2c_smbus_data * data)
1434 /* So we need to generate a series of msgs. In the case of writing, we
1435 need to use only one message; when reading, we need two. We initialize
1436 most things with sane defaults, to keep the code below somewhat
1438 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1439 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1440 int num = read_write == I2C_SMBUS_READ?2:1;
1441 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1442 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1447 msgbuf0[0] = command;
1449 case I2C_SMBUS_QUICK:
1451 /* Special case: The read/write field is used as data */
1452 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1455 case I2C_SMBUS_BYTE:
1456 if (read_write == I2C_SMBUS_READ) {
1457 /* Special case: only a read! */
1458 msg[0].flags = I2C_M_RD | flags;
1462 case I2C_SMBUS_BYTE_DATA:
1463 if (read_write == I2C_SMBUS_READ)
1467 msgbuf0[1] = data->byte;
1470 case I2C_SMBUS_WORD_DATA:
1471 if (read_write == I2C_SMBUS_READ)
1475 msgbuf0[1] = data->word & 0xff;
1476 msgbuf0[2] = data->word >> 8;
1479 case I2C_SMBUS_PROC_CALL:
1480 num = 2; /* Special case */
1481 read_write = I2C_SMBUS_READ;
1484 msgbuf0[1] = data->word & 0xff;
1485 msgbuf0[2] = data->word >> 8;
1487 case I2C_SMBUS_BLOCK_DATA:
1488 if (read_write == I2C_SMBUS_READ) {
1489 msg[1].flags |= I2C_M_RECV_LEN;
1490 msg[1].len = 1; /* block length will be added by
1491 the underlying bus driver */
1493 msg[0].len = data->block[0] + 2;
1494 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1495 dev_err(&adapter->dev, "smbus_access called with "
1496 "invalid block write size (%d)\n",
1500 for (i = 1; i < msg[0].len; i++)
1501 msgbuf0[i] = data->block[i-1];
1504 case I2C_SMBUS_BLOCK_PROC_CALL:
1505 num = 2; /* Another special case */
1506 read_write = I2C_SMBUS_READ;
1507 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1508 dev_err(&adapter->dev, "%s called with invalid "
1509 "block proc call size (%d)\n", __FUNCTION__,
1513 msg[0].len = data->block[0] + 2;
1514 for (i = 1; i < msg[0].len; i++)
1515 msgbuf0[i] = data->block[i-1];
1516 msg[1].flags |= I2C_M_RECV_LEN;
1517 msg[1].len = 1; /* block length will be added by
1518 the underlying bus driver */
1520 case I2C_SMBUS_I2C_BLOCK_DATA:
1521 if (read_write == I2C_SMBUS_READ) {
1522 msg[1].len = data->block[0];
1524 msg[0].len = data->block[0] + 1;
1525 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1526 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1527 "invalid block write size (%d)\n",
1531 for (i = 1; i <= data->block[0]; i++)
1532 msgbuf0[i] = data->block[i];
1536 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1541 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1542 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1544 /* Compute PEC if first message is a write */
1545 if (!(msg[0].flags & I2C_M_RD)) {
1546 if (num == 1) /* Write only */
1547 i2c_smbus_add_pec(&msg[0]);
1548 else /* Write followed by read */
1549 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1551 /* Ask for PEC if last message is a read */
1552 if (msg[num-1].flags & I2C_M_RD)
1556 if (i2c_transfer(adapter, msg, num) < 0)
1559 /* Check PEC if last message is a read */
1560 if (i && (msg[num-1].flags & I2C_M_RD)) {
1561 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1565 if (read_write == I2C_SMBUS_READ)
1567 case I2C_SMBUS_BYTE:
1568 data->byte = msgbuf0[0];
1570 case I2C_SMBUS_BYTE_DATA:
1571 data->byte = msgbuf1[0];
1573 case I2C_SMBUS_WORD_DATA:
1574 case I2C_SMBUS_PROC_CALL:
1575 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1577 case I2C_SMBUS_I2C_BLOCK_DATA:
1578 for (i = 0; i < data->block[0]; i++)
1579 data->block[i+1] = msgbuf1[i];
1581 case I2C_SMBUS_BLOCK_DATA:
1582 case I2C_SMBUS_BLOCK_PROC_CALL:
1583 for (i = 0; i < msgbuf1[0] + 1; i++)
1584 data->block[i] = msgbuf1[i];
1591 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1592 char read_write, u8 command, int size,
1593 union i2c_smbus_data * data)
1597 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1599 if (adapter->algo->smbus_xfer) {
1600 mutex_lock(&adapter->bus_lock);
1601 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1603 mutex_unlock(&adapter->bus_lock);
1605 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1610 EXPORT_SYMBOL(i2c_smbus_xfer);
1612 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1613 MODULE_DESCRIPTION("I2C-Bus main module");
1614 MODULE_LICENSE("GPL");