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;
77 /* new style drivers use the same kind of driver matching policy
78 * as platform devices or SPI: compare device and driver IDs.
80 return strcmp(client->driver_name, drv->name) == 0;
85 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
86 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
88 struct i2c_client *client = to_i2c_client(dev);
90 /* by definition, legacy drivers can't hotplug */
94 if (client->driver_name[0]) {
95 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
98 if (add_uevent_var(env, "MODALIAS=%s%s",
99 I2C_MODULE_PREFIX, client->name))
102 dev_dbg(dev, "uevent\n");
107 #define i2c_device_uevent NULL
108 #endif /* CONFIG_HOTPLUG */
110 static int i2c_device_probe(struct device *dev)
112 struct i2c_client *client = to_i2c_client(dev);
113 struct i2c_driver *driver = to_i2c_driver(dev->driver);
114 const struct i2c_device_id *id;
119 client->driver = driver;
120 dev_dbg(dev, "probe\n");
122 if (driver->id_table)
123 id = i2c_match_id(driver->id_table, client);
126 status = driver->probe(client, id);
128 client->driver = NULL;
132 static int i2c_device_remove(struct device *dev)
134 struct i2c_client *client = to_i2c_client(dev);
135 struct i2c_driver *driver;
141 driver = to_i2c_driver(dev->driver);
142 if (driver->remove) {
143 dev_dbg(dev, "remove\n");
144 status = driver->remove(client);
150 client->driver = NULL;
154 static void i2c_device_shutdown(struct device *dev)
156 struct i2c_driver *driver;
160 driver = to_i2c_driver(dev->driver);
161 if (driver->shutdown)
162 driver->shutdown(to_i2c_client(dev));
165 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
167 struct i2c_driver *driver;
171 driver = to_i2c_driver(dev->driver);
172 if (!driver->suspend)
174 return driver->suspend(to_i2c_client(dev), mesg);
177 static int i2c_device_resume(struct device * dev)
179 struct i2c_driver *driver;
183 driver = to_i2c_driver(dev->driver);
186 return driver->resume(to_i2c_client(dev));
189 static void i2c_client_release(struct device *dev)
191 struct i2c_client *client = to_i2c_client(dev);
192 complete(&client->released);
195 static void i2c_client_dev_release(struct device *dev)
197 kfree(to_i2c_client(dev));
200 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
202 struct i2c_client *client = to_i2c_client(dev);
203 return sprintf(buf, "%s\n", client->name);
206 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
208 struct i2c_client *client = to_i2c_client(dev);
209 return client->driver_name[0]
210 ? sprintf(buf, "%s\n", client->driver_name)
211 : sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
214 static struct device_attribute i2c_dev_attrs[] = {
215 __ATTR(name, S_IRUGO, show_client_name, NULL),
216 /* modalias helps coldplug: modprobe $(cat .../modalias) */
217 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
221 static struct bus_type i2c_bus_type = {
223 .dev_attrs = i2c_dev_attrs,
224 .match = i2c_device_match,
225 .uevent = i2c_device_uevent,
226 .probe = i2c_device_probe,
227 .remove = i2c_device_remove,
228 .shutdown = i2c_device_shutdown,
229 .suspend = i2c_device_suspend,
230 .resume = i2c_device_resume,
235 * i2c_verify_client - return parameter as i2c_client, or NULL
236 * @dev: device, probably from some driver model iterator
238 * When traversing the driver model tree, perhaps using driver model
239 * iterators like @device_for_each_child(), you can't assume very much
240 * about the nodes you find. Use this function to avoid oopses caused
241 * by wrongly treating some non-I2C device as an i2c_client.
243 struct i2c_client *i2c_verify_client(struct device *dev)
245 return (dev->bus == &i2c_bus_type)
249 EXPORT_SYMBOL(i2c_verify_client);
253 * i2c_new_device - instantiate an i2c device for use with a new style driver
254 * @adap: the adapter managing the device
255 * @info: describes one I2C device; bus_num is ignored
258 * Create a device to work with a new style i2c driver, where binding is
259 * handled through driver model probe()/remove() methods. This call is not
260 * appropriate for use by mainboad initialization logic, which usually runs
261 * during an arch_initcall() long before any i2c_adapter could exist.
263 * This returns the new i2c client, which may be saved for later use with
264 * i2c_unregister_device(); or NULL to indicate an error.
267 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
269 struct i2c_client *client;
272 client = kzalloc(sizeof *client, GFP_KERNEL);
276 client->adapter = adap;
278 client->dev.platform_data = info->platform_data;
279 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
281 client->flags = info->flags & ~I2C_CLIENT_WAKE;
282 client->addr = info->addr;
283 client->irq = info->irq;
285 strlcpy(client->driver_name, info->driver_name,
286 sizeof(client->driver_name));
287 strlcpy(client->name, info->type, sizeof(client->name));
289 /* a new style driver may be bound to this device when we
290 * return from this function, or any later moment (e.g. maybe
291 * hotplugging will load the driver module). and the device
292 * refcount model is the standard driver model one.
294 status = i2c_attach_client(client);
301 EXPORT_SYMBOL_GPL(i2c_new_device);
305 * i2c_unregister_device - reverse effect of i2c_new_device()
306 * @client: value returned from i2c_new_device()
309 void i2c_unregister_device(struct i2c_client *client)
311 struct i2c_adapter *adapter = client->adapter;
312 struct i2c_driver *driver = client->driver;
314 if (driver && !is_newstyle_driver(driver)) {
315 dev_err(&client->dev, "can't unregister devices "
316 "with legacy drivers\n");
321 mutex_lock(&adapter->clist_lock);
322 list_del(&client->list);
323 mutex_unlock(&adapter->clist_lock);
325 device_unregister(&client->dev);
327 EXPORT_SYMBOL_GPL(i2c_unregister_device);
330 static int dummy_probe(struct i2c_client *client,
331 const struct i2c_device_id *id)
336 static int dummy_remove(struct i2c_client *client)
341 static struct i2c_driver dummy_driver = {
342 .driver.name = "dummy",
343 .probe = dummy_probe,
344 .remove = dummy_remove,
348 * i2c_new_dummy - return a new i2c device bound to a dummy driver
349 * @adapter: the adapter managing the device
350 * @address: seven bit address to be used
351 * @type: optional label used for i2c_client.name
354 * This returns an I2C client bound to the "dummy" driver, intended for use
355 * with devices that consume multiple addresses. Examples of such chips
356 * include various EEPROMS (like 24c04 and 24c08 models).
358 * These dummy devices have two main uses. First, most I2C and SMBus calls
359 * except i2c_transfer() need a client handle; the dummy will be that handle.
360 * And second, this prevents the specified address from being bound to a
363 * This returns the new i2c client, which should be saved for later use with
364 * i2c_unregister_device(); or NULL to indicate an error.
367 i2c_new_dummy(struct i2c_adapter *adapter, u16 address, const char *type)
369 struct i2c_board_info info = {
370 .driver_name = "dummy",
375 strlcpy(info.type, type, sizeof info.type);
376 return i2c_new_device(adapter, &info);
378 EXPORT_SYMBOL_GPL(i2c_new_dummy);
380 /* ------------------------------------------------------------------------- */
382 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
384 static void i2c_adapter_dev_release(struct device *dev)
386 struct i2c_adapter *adap = to_i2c_adapter(dev);
387 complete(&adap->dev_released);
391 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
393 struct i2c_adapter *adap = to_i2c_adapter(dev);
394 return sprintf(buf, "%s\n", adap->name);
397 static struct device_attribute i2c_adapter_attrs[] = {
398 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
402 static struct class i2c_adapter_class = {
403 .owner = THIS_MODULE,
404 .name = "i2c-adapter",
405 .dev_attrs = i2c_adapter_attrs,
408 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
410 struct i2c_devinfo *devinfo;
412 mutex_lock(&__i2c_board_lock);
413 list_for_each_entry(devinfo, &__i2c_board_list, list) {
414 if (devinfo->busnum == adapter->nr
415 && !i2c_new_device(adapter,
416 &devinfo->board_info))
417 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
418 i2c_adapter_id(adapter),
419 devinfo->board_info.addr);
421 mutex_unlock(&__i2c_board_lock);
424 static int i2c_do_add_adapter(struct device_driver *d, void *data)
426 struct i2c_driver *driver = to_i2c_driver(d);
427 struct i2c_adapter *adap = data;
429 if (driver->attach_adapter) {
430 /* We ignore the return code; if it fails, too bad */
431 driver->attach_adapter(adap);
436 static int i2c_register_adapter(struct i2c_adapter *adap)
440 mutex_init(&adap->bus_lock);
441 mutex_init(&adap->clist_lock);
442 INIT_LIST_HEAD(&adap->clients);
444 mutex_lock(&core_lock);
446 /* Add the adapter to the driver core.
447 * If the parent pointer is not set up,
448 * we add this adapter to the host bus.
450 if (adap->dev.parent == NULL) {
451 adap->dev.parent = &platform_bus;
452 pr_debug("I2C adapter driver [%s] forgot to specify "
453 "physical device\n", adap->name);
455 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
456 adap->dev.release = &i2c_adapter_dev_release;
457 adap->dev.class = &i2c_adapter_class;
458 res = device_register(&adap->dev);
462 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
464 /* create pre-declared device nodes for new-style drivers */
465 if (adap->nr < __i2c_first_dynamic_bus_num)
466 i2c_scan_static_board_info(adap);
468 /* let legacy drivers scan this bus for matching devices */
469 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
473 mutex_unlock(&core_lock);
477 idr_remove(&i2c_adapter_idr, adap->nr);
482 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
483 * @adapter: the adapter to add
486 * This routine is used to declare an I2C adapter when its bus number
487 * doesn't matter. Examples: for I2C adapters dynamically added by
488 * USB links or PCI plugin cards.
490 * When this returns zero, a new bus number was allocated and stored
491 * in adap->nr, and the specified adapter became available for clients.
492 * Otherwise, a negative errno value is returned.
494 int i2c_add_adapter(struct i2c_adapter *adapter)
499 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
502 mutex_lock(&core_lock);
503 /* "above" here means "above or equal to", sigh */
504 res = idr_get_new_above(&i2c_adapter_idr, adapter,
505 __i2c_first_dynamic_bus_num, &id);
506 mutex_unlock(&core_lock);
515 return i2c_register_adapter(adapter);
517 EXPORT_SYMBOL(i2c_add_adapter);
520 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
521 * @adap: the adapter to register (with adap->nr initialized)
524 * This routine is used to declare an I2C adapter when its bus number
525 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
526 * or otherwise built in to the system's mainboard, and where i2c_board_info
527 * is used to properly configure I2C devices.
529 * If no devices have pre-been declared for this bus, then be sure to
530 * register the adapter before any dynamically allocated ones. Otherwise
531 * the required bus ID may not be available.
533 * When this returns zero, the specified adapter became available for
534 * clients using the bus number provided in adap->nr. Also, the table
535 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
536 * and the appropriate driver model device nodes are created. Otherwise, a
537 * negative errno value is returned.
539 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
544 if (adap->nr & ~MAX_ID_MASK)
548 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
551 mutex_lock(&core_lock);
552 /* "above" here means "above or equal to", sigh;
553 * we need the "equal to" result to force the result
555 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
556 if (status == 0 && id != adap->nr) {
558 idr_remove(&i2c_adapter_idr, id);
560 mutex_unlock(&core_lock);
561 if (status == -EAGAIN)
565 status = i2c_register_adapter(adap);
568 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
570 static int i2c_do_del_adapter(struct device_driver *d, void *data)
572 struct i2c_driver *driver = to_i2c_driver(d);
573 struct i2c_adapter *adapter = data;
576 if (!driver->detach_adapter)
578 res = driver->detach_adapter(adapter);
580 dev_err(&adapter->dev, "detach_adapter failed (%d) "
581 "for driver [%s]\n", res, driver->driver.name);
586 * i2c_del_adapter - unregister I2C adapter
587 * @adap: the adapter being unregistered
590 * This unregisters an I2C adapter which was previously registered
591 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
593 int i2c_del_adapter(struct i2c_adapter *adap)
595 struct list_head *item, *_n;
596 struct i2c_client *client;
599 mutex_lock(&core_lock);
601 /* First make sure that this adapter was ever added */
602 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
603 pr_debug("i2c-core: attempting to delete unregistered "
604 "adapter [%s]\n", adap->name);
609 /* Tell drivers about this removal */
610 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
615 /* detach any active clients. This must be done first, because
616 * it can fail; in which case we give up. */
617 list_for_each_safe(item, _n, &adap->clients) {
618 struct i2c_driver *driver;
620 client = list_entry(item, struct i2c_client, list);
621 driver = client->driver;
623 /* new style, follow standard driver model */
624 if (!driver || is_newstyle_driver(driver)) {
625 i2c_unregister_device(client);
629 /* legacy drivers create and remove clients themselves */
630 if ((res = driver->detach_client(client))) {
631 dev_err(&adap->dev, "detach_client failed for client "
632 "[%s] at address 0x%02x\n", client->name,
638 /* clean up the sysfs representation */
639 init_completion(&adap->dev_released);
640 device_unregister(&adap->dev);
642 /* wait for sysfs to drop all references */
643 wait_for_completion(&adap->dev_released);
646 idr_remove(&i2c_adapter_idr, adap->nr);
648 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
651 mutex_unlock(&core_lock);
654 EXPORT_SYMBOL(i2c_del_adapter);
657 /* ------------------------------------------------------------------------- */
660 * An i2c_driver is used with one or more i2c_client (device) nodes to access
661 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
662 * are two models for binding the driver to its device: "new style" drivers
663 * follow the standard Linux driver model and just respond to probe() calls
664 * issued if the driver core sees they match(); "legacy" drivers create device
668 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
672 /* new style driver methods can't mix with legacy ones */
673 if (is_newstyle_driver(driver)) {
674 if (driver->attach_adapter || driver->detach_adapter
675 || driver->detach_client) {
677 "i2c-core: driver [%s] is confused\n",
678 driver->driver.name);
683 /* add the driver to the list of i2c drivers in the driver core */
684 driver->driver.owner = owner;
685 driver->driver.bus = &i2c_bus_type;
687 /* for new style drivers, when registration returns the driver core
688 * will have called probe() for all matching-but-unbound devices.
690 res = driver_register(&driver->driver);
694 mutex_lock(&core_lock);
696 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
698 /* legacy drivers scan i2c busses directly */
699 if (driver->attach_adapter) {
700 struct i2c_adapter *adapter;
702 down(&i2c_adapter_class.sem);
703 list_for_each_entry(adapter, &i2c_adapter_class.devices,
705 driver->attach_adapter(adapter);
707 up(&i2c_adapter_class.sem);
710 mutex_unlock(&core_lock);
713 EXPORT_SYMBOL(i2c_register_driver);
716 * i2c_del_driver - unregister I2C driver
717 * @driver: the driver being unregistered
720 void i2c_del_driver(struct i2c_driver *driver)
722 struct list_head *item2, *_n;
723 struct i2c_client *client;
724 struct i2c_adapter *adap;
726 mutex_lock(&core_lock);
728 /* new-style driver? */
729 if (is_newstyle_driver(driver))
732 /* Have a look at each adapter, if clients of this driver are still
733 * attached. If so, detach them to be able to kill the driver
736 down(&i2c_adapter_class.sem);
737 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
738 if (driver->detach_adapter) {
739 if (driver->detach_adapter(adap)) {
740 dev_err(&adap->dev, "detach_adapter failed "
742 driver->driver.name);
745 list_for_each_safe(item2, _n, &adap->clients) {
746 client = list_entry(item2, struct i2c_client, list);
747 if (client->driver != driver)
749 dev_dbg(&adap->dev, "detaching client [%s] "
750 "at 0x%02x\n", client->name,
752 if (driver->detach_client(client)) {
753 dev_err(&adap->dev, "detach_client "
754 "failed for client [%s] at "
755 "0x%02x\n", client->name,
761 up(&i2c_adapter_class.sem);
764 driver_unregister(&driver->driver);
765 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
767 mutex_unlock(&core_lock);
769 EXPORT_SYMBOL(i2c_del_driver);
771 /* ------------------------------------------------------------------------- */
773 static int __i2c_check_addr(struct device *dev, void *addrp)
775 struct i2c_client *client = i2c_verify_client(dev);
776 int addr = *(int *)addrp;
778 if (client && client->addr == addr)
783 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
785 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
788 int i2c_attach_client(struct i2c_client *client)
790 struct i2c_adapter *adapter = client->adapter;
793 client->dev.parent = &client->adapter->dev;
794 client->dev.bus = &i2c_bus_type;
797 client->dev.driver = &client->driver->driver;
799 if (client->driver && !is_newstyle_driver(client->driver)) {
800 client->dev.release = i2c_client_release;
801 client->dev.uevent_suppress = 1;
803 client->dev.release = i2c_client_dev_release;
805 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
806 "%d-%04x", i2c_adapter_id(adapter), client->addr);
807 res = device_register(&client->dev);
811 mutex_lock(&adapter->clist_lock);
812 list_add_tail(&client->list, &adapter->clients);
813 mutex_unlock(&adapter->clist_lock);
815 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
816 client->name, client->dev.bus_id);
818 if (adapter->client_register) {
819 if (adapter->client_register(client)) {
820 dev_dbg(&adapter->dev, "client_register "
821 "failed for client [%s] at 0x%02x\n",
822 client->name, client->addr);
829 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
830 "(%d)\n", client->name, client->addr, res);
833 EXPORT_SYMBOL(i2c_attach_client);
835 int i2c_detach_client(struct i2c_client *client)
837 struct i2c_adapter *adapter = client->adapter;
840 if (adapter->client_unregister) {
841 res = adapter->client_unregister(client);
843 dev_err(&client->dev,
844 "client_unregister [%s] failed, "
845 "client not detached\n", client->name);
850 mutex_lock(&adapter->clist_lock);
851 list_del(&client->list);
852 mutex_unlock(&adapter->clist_lock);
854 init_completion(&client->released);
855 device_unregister(&client->dev);
856 wait_for_completion(&client->released);
861 EXPORT_SYMBOL(i2c_detach_client);
864 * i2c_use_client - increments the reference count of the i2c client structure
865 * @client: the client being referenced
867 * Each live reference to a client should be refcounted. The driver model does
868 * that automatically as part of driver binding, so that most drivers don't
869 * need to do this explicitly: they hold a reference until they're unbound
872 * A pointer to the client with the incremented reference counter is returned.
874 struct i2c_client *i2c_use_client(struct i2c_client *client)
876 get_device(&client->dev);
879 EXPORT_SYMBOL(i2c_use_client);
882 * i2c_release_client - release a use of the i2c client structure
883 * @client: the client being no longer referenced
885 * Must be called when a user of a client is finished with it.
887 void i2c_release_client(struct i2c_client *client)
889 put_device(&client->dev);
891 EXPORT_SYMBOL(i2c_release_client);
898 static int i2c_cmd(struct device *dev, void *_arg)
900 struct i2c_client *client = i2c_verify_client(dev);
901 struct i2c_cmd_arg *arg = _arg;
903 if (client && client->driver && client->driver->command)
904 client->driver->command(client, arg->cmd, arg->arg);
908 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
910 struct i2c_cmd_arg cmd_arg;
914 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
916 EXPORT_SYMBOL(i2c_clients_command);
918 static int __init i2c_init(void)
922 retval = bus_register(&i2c_bus_type);
925 retval = class_register(&i2c_adapter_class);
928 retval = i2c_add_driver(&dummy_driver);
934 class_unregister(&i2c_adapter_class);
936 bus_unregister(&i2c_bus_type);
940 static void __exit i2c_exit(void)
942 i2c_del_driver(&dummy_driver);
943 class_unregister(&i2c_adapter_class);
944 bus_unregister(&i2c_bus_type);
947 subsys_initcall(i2c_init);
948 module_exit(i2c_exit);
950 /* ----------------------------------------------------
951 * the functional interface to the i2c busses.
952 * ----------------------------------------------------
955 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
959 if (adap->algo->master_xfer) {
961 for (ret = 0; ret < num; ret++) {
962 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
963 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
964 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
965 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
969 if (in_atomic() || irqs_disabled()) {
970 ret = mutex_trylock(&adap->bus_lock);
972 /* I2C activity is ongoing. */
975 mutex_lock_nested(&adap->bus_lock, adap->level);
978 ret = adap->algo->master_xfer(adap,msgs,num);
979 mutex_unlock(&adap->bus_lock);
983 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
987 EXPORT_SYMBOL(i2c_transfer);
989 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
992 struct i2c_adapter *adap=client->adapter;
995 msg.addr = client->addr;
996 msg.flags = client->flags & I2C_M_TEN;
998 msg.buf = (char *)buf;
1000 ret = i2c_transfer(adap, &msg, 1);
1002 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1003 transmitted, else error code. */
1004 return (ret == 1) ? count : ret;
1006 EXPORT_SYMBOL(i2c_master_send);
1008 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1010 struct i2c_adapter *adap=client->adapter;
1014 msg.addr = client->addr;
1015 msg.flags = client->flags & I2C_M_TEN;
1016 msg.flags |= I2C_M_RD;
1020 ret = i2c_transfer(adap, &msg, 1);
1022 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1023 transmitted, else error code. */
1024 return (ret == 1) ? count : ret;
1026 EXPORT_SYMBOL(i2c_master_recv);
1028 /* ----------------------------------------------------
1029 * the i2c address scanning function
1030 * Will not work for 10-bit addresses!
1031 * ----------------------------------------------------
1033 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1034 int (*found_proc) (struct i2c_adapter *, int, int))
1038 /* Make sure the address is valid */
1039 if (addr < 0x03 || addr > 0x77) {
1040 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1045 /* Skip if already in use */
1046 if (i2c_check_addr(adapter, addr))
1049 /* Make sure there is something at this address, unless forced */
1051 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1052 I2C_SMBUS_QUICK, NULL) < 0)
1055 /* prevent 24RF08 corruption */
1056 if ((addr & ~0x0f) == 0x50)
1057 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1058 I2C_SMBUS_QUICK, NULL);
1061 /* Finally call the custom detection function */
1062 err = found_proc(adapter, addr, kind);
1063 /* -ENODEV can be returned if there is a chip at the given address
1064 but it isn't supported by this chip driver. We catch it here as
1065 this isn't an error. */
1070 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1075 int i2c_probe(struct i2c_adapter *adapter,
1076 const struct i2c_client_address_data *address_data,
1077 int (*found_proc) (struct i2c_adapter *, int, int))
1080 int adap_id = i2c_adapter_id(adapter);
1082 /* Force entries are done first, and are not affected by ignore
1084 if (address_data->forces) {
1085 const unsigned short * const *forces = address_data->forces;
1088 for (kind = 0; forces[kind]; kind++) {
1089 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1091 if (forces[kind][i] == adap_id
1092 || forces[kind][i] == ANY_I2C_BUS) {
1093 dev_dbg(&adapter->dev, "found force "
1094 "parameter for adapter %d, "
1095 "addr 0x%02x, kind %d\n",
1096 adap_id, forces[kind][i + 1],
1098 err = i2c_probe_address(adapter,
1099 forces[kind][i + 1],
1108 /* Stop here if we can't use SMBUS_QUICK */
1109 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1110 if (address_data->probe[0] == I2C_CLIENT_END
1111 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1114 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1115 "can't probe for chips\n");
1119 /* Probe entries are done second, and are not affected by ignore
1121 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1122 if (address_data->probe[i] == adap_id
1123 || address_data->probe[i] == ANY_I2C_BUS) {
1124 dev_dbg(&adapter->dev, "found probe parameter for "
1125 "adapter %d, addr 0x%02x\n", adap_id,
1126 address_data->probe[i + 1]);
1127 err = i2c_probe_address(adapter,
1128 address_data->probe[i + 1],
1135 /* Normal entries are done last, unless shadowed by an ignore entry */
1136 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1140 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1142 if ((address_data->ignore[j] == adap_id ||
1143 address_data->ignore[j] == ANY_I2C_BUS)
1144 && address_data->ignore[j + 1]
1145 == address_data->normal_i2c[i]) {
1146 dev_dbg(&adapter->dev, "found ignore "
1147 "parameter for adapter %d, "
1148 "addr 0x%02x\n", adap_id,
1149 address_data->ignore[j + 1]);
1157 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1158 "addr 0x%02x\n", adap_id,
1159 address_data->normal_i2c[i]);
1160 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1168 EXPORT_SYMBOL(i2c_probe);
1171 i2c_new_probed_device(struct i2c_adapter *adap,
1172 struct i2c_board_info *info,
1173 unsigned short const *addr_list)
1177 /* Stop here if the bus doesn't support probing */
1178 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1179 dev_err(&adap->dev, "Probing not supported\n");
1183 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1184 /* Check address validity */
1185 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1186 dev_warn(&adap->dev, "Invalid 7-bit address "
1187 "0x%02x\n", addr_list[i]);
1191 /* Check address availability */
1192 if (i2c_check_addr(adap, addr_list[i])) {
1193 dev_dbg(&adap->dev, "Address 0x%02x already in "
1194 "use, not probing\n", addr_list[i]);
1198 /* Test address responsiveness
1199 The default probe method is a quick write, but it is known
1200 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1201 and could also irreversibly write-protect some EEPROMs, so
1202 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1203 read instead. Also, some bus drivers don't implement
1204 quick write, so we fallback to a byte read it that case
1206 if ((addr_list[i] & ~0x07) == 0x30
1207 || (addr_list[i] & ~0x0f) == 0x50
1208 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1209 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1211 I2C_SMBUS_BYTE, NULL) >= 0)
1214 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1216 I2C_SMBUS_QUICK, NULL) >= 0)
1221 if (addr_list[i] == I2C_CLIENT_END) {
1222 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1226 info->addr = addr_list[i];
1227 return i2c_new_device(adap, info);
1229 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1231 struct i2c_adapter* i2c_get_adapter(int id)
1233 struct i2c_adapter *adapter;
1235 mutex_lock(&core_lock);
1236 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1237 if (adapter && !try_module_get(adapter->owner))
1240 mutex_unlock(&core_lock);
1243 EXPORT_SYMBOL(i2c_get_adapter);
1245 void i2c_put_adapter(struct i2c_adapter *adap)
1247 module_put(adap->owner);
1249 EXPORT_SYMBOL(i2c_put_adapter);
1251 /* The SMBus parts */
1253 #define POLY (0x1070U << 3)
1259 for(i = 0; i < 8; i++) {
1264 return (u8)(data >> 8);
1267 /* Incremental CRC8 over count bytes in the array pointed to by p */
1268 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1272 for(i = 0; i < count; i++)
1273 crc = crc8((crc ^ p[i]) << 8);
1277 /* Assume a 7-bit address, which is reasonable for SMBus */
1278 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1280 /* The address will be sent first */
1281 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1282 pec = i2c_smbus_pec(pec, &addr, 1);
1284 /* The data buffer follows */
1285 return i2c_smbus_pec(pec, msg->buf, msg->len);
1288 /* Used for write only transactions */
1289 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1291 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1295 /* Return <0 on CRC error
1296 If there was a write before this read (most cases) we need to take the
1297 partial CRC from the write part into account.
1298 Note that this function does modify the message (we need to decrease the
1299 message length to hide the CRC byte from the caller). */
1300 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1302 u8 rpec = msg->buf[--msg->len];
1303 cpec = i2c_smbus_msg_pec(cpec, msg);
1306 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1313 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1315 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1316 value,0,I2C_SMBUS_QUICK,NULL);
1318 EXPORT_SYMBOL(i2c_smbus_write_quick);
1320 s32 i2c_smbus_read_byte(struct i2c_client *client)
1322 union i2c_smbus_data data;
1323 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1324 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1329 EXPORT_SYMBOL(i2c_smbus_read_byte);
1331 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1333 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1334 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1336 EXPORT_SYMBOL(i2c_smbus_write_byte);
1338 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1340 union i2c_smbus_data data;
1341 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1342 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1347 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1349 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1351 union i2c_smbus_data data;
1353 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1354 I2C_SMBUS_WRITE,command,
1355 I2C_SMBUS_BYTE_DATA,&data);
1357 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1359 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1361 union i2c_smbus_data data;
1362 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1363 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1368 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1370 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1372 union i2c_smbus_data data;
1374 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1375 I2C_SMBUS_WRITE,command,
1376 I2C_SMBUS_WORD_DATA,&data);
1378 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1381 * i2c_smbus_read_block_data - SMBus block read request
1382 * @client: Handle to slave device
1383 * @command: Command byte issued to let the slave know what data should
1385 * @values: Byte array into which data will be read; big enough to hold
1386 * the data returned by the slave. SMBus allows at most 32 bytes.
1388 * Returns the number of bytes read in the slave's response, else a
1389 * negative number to indicate some kind of error.
1391 * Note that using this function requires that the client's adapter support
1392 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1393 * support this; its emulation through I2C messaging relies on a specific
1394 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1396 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1399 union i2c_smbus_data data;
1401 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1402 I2C_SMBUS_READ, command,
1403 I2C_SMBUS_BLOCK_DATA, &data))
1406 memcpy(values, &data.block[1], data.block[0]);
1407 return data.block[0];
1409 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1411 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1412 u8 length, const u8 *values)
1414 union i2c_smbus_data data;
1416 if (length > I2C_SMBUS_BLOCK_MAX)
1417 length = I2C_SMBUS_BLOCK_MAX;
1418 data.block[0] = length;
1419 memcpy(&data.block[1], values, length);
1420 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1421 I2C_SMBUS_WRITE,command,
1422 I2C_SMBUS_BLOCK_DATA,&data);
1424 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1426 /* Returns the number of read bytes */
1427 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1428 u8 length, u8 *values)
1430 union i2c_smbus_data data;
1432 if (length > I2C_SMBUS_BLOCK_MAX)
1433 length = I2C_SMBUS_BLOCK_MAX;
1434 data.block[0] = length;
1435 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1436 I2C_SMBUS_READ,command,
1437 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1440 memcpy(values, &data.block[1], data.block[0]);
1441 return data.block[0];
1443 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1445 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1446 u8 length, const u8 *values)
1448 union i2c_smbus_data data;
1450 if (length > I2C_SMBUS_BLOCK_MAX)
1451 length = I2C_SMBUS_BLOCK_MAX;
1452 data.block[0] = length;
1453 memcpy(data.block + 1, values, length);
1454 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1455 I2C_SMBUS_WRITE, command,
1456 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1458 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1460 /* Simulate a SMBus command using the i2c protocol
1461 No checking of parameters is done! */
1462 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1463 unsigned short flags,
1464 char read_write, u8 command, int size,
1465 union i2c_smbus_data * data)
1467 /* So we need to generate a series of msgs. In the case of writing, we
1468 need to use only one message; when reading, we need two. We initialize
1469 most things with sane defaults, to keep the code below somewhat
1471 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1472 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1473 int num = read_write == I2C_SMBUS_READ?2:1;
1474 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1475 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1480 msgbuf0[0] = command;
1482 case I2C_SMBUS_QUICK:
1484 /* Special case: The read/write field is used as data */
1485 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1488 case I2C_SMBUS_BYTE:
1489 if (read_write == I2C_SMBUS_READ) {
1490 /* Special case: only a read! */
1491 msg[0].flags = I2C_M_RD | flags;
1495 case I2C_SMBUS_BYTE_DATA:
1496 if (read_write == I2C_SMBUS_READ)
1500 msgbuf0[1] = data->byte;
1503 case I2C_SMBUS_WORD_DATA:
1504 if (read_write == I2C_SMBUS_READ)
1508 msgbuf0[1] = data->word & 0xff;
1509 msgbuf0[2] = data->word >> 8;
1512 case I2C_SMBUS_PROC_CALL:
1513 num = 2; /* Special case */
1514 read_write = I2C_SMBUS_READ;
1517 msgbuf0[1] = data->word & 0xff;
1518 msgbuf0[2] = data->word >> 8;
1520 case I2C_SMBUS_BLOCK_DATA:
1521 if (read_write == I2C_SMBUS_READ) {
1522 msg[1].flags |= I2C_M_RECV_LEN;
1523 msg[1].len = 1; /* block length will be added by
1524 the underlying bus driver */
1526 msg[0].len = data->block[0] + 2;
1527 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1528 dev_err(&adapter->dev, "smbus_access called with "
1529 "invalid block write size (%d)\n",
1533 for (i = 1; i < msg[0].len; i++)
1534 msgbuf0[i] = data->block[i-1];
1537 case I2C_SMBUS_BLOCK_PROC_CALL:
1538 num = 2; /* Another special case */
1539 read_write = I2C_SMBUS_READ;
1540 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1541 dev_err(&adapter->dev, "%s called with invalid "
1542 "block proc call size (%d)\n", __func__,
1546 msg[0].len = data->block[0] + 2;
1547 for (i = 1; i < msg[0].len; i++)
1548 msgbuf0[i] = data->block[i-1];
1549 msg[1].flags |= I2C_M_RECV_LEN;
1550 msg[1].len = 1; /* block length will be added by
1551 the underlying bus driver */
1553 case I2C_SMBUS_I2C_BLOCK_DATA:
1554 if (read_write == I2C_SMBUS_READ) {
1555 msg[1].len = data->block[0];
1557 msg[0].len = data->block[0] + 1;
1558 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1559 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1560 "invalid block write size (%d)\n",
1564 for (i = 1; i <= data->block[0]; i++)
1565 msgbuf0[i] = data->block[i];
1569 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1574 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1575 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1577 /* Compute PEC if first message is a write */
1578 if (!(msg[0].flags & I2C_M_RD)) {
1579 if (num == 1) /* Write only */
1580 i2c_smbus_add_pec(&msg[0]);
1581 else /* Write followed by read */
1582 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1584 /* Ask for PEC if last message is a read */
1585 if (msg[num-1].flags & I2C_M_RD)
1589 if (i2c_transfer(adapter, msg, num) < 0)
1592 /* Check PEC if last message is a read */
1593 if (i && (msg[num-1].flags & I2C_M_RD)) {
1594 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1598 if (read_write == I2C_SMBUS_READ)
1600 case I2C_SMBUS_BYTE:
1601 data->byte = msgbuf0[0];
1603 case I2C_SMBUS_BYTE_DATA:
1604 data->byte = msgbuf1[0];
1606 case I2C_SMBUS_WORD_DATA:
1607 case I2C_SMBUS_PROC_CALL:
1608 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1610 case I2C_SMBUS_I2C_BLOCK_DATA:
1611 for (i = 0; i < data->block[0]; i++)
1612 data->block[i+1] = msgbuf1[i];
1614 case I2C_SMBUS_BLOCK_DATA:
1615 case I2C_SMBUS_BLOCK_PROC_CALL:
1616 for (i = 0; i < msgbuf1[0] + 1; i++)
1617 data->block[i] = msgbuf1[i];
1624 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1625 char read_write, u8 command, int size,
1626 union i2c_smbus_data * data)
1630 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1632 if (adapter->algo->smbus_xfer) {
1633 mutex_lock(&adapter->bus_lock);
1634 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1636 mutex_unlock(&adapter->bus_lock);
1638 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1643 EXPORT_SYMBOL(i2c_smbus_xfer);
1645 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1646 MODULE_DESCRIPTION("I2C-Bus main module");
1647 MODULE_LICENSE("GPL");