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 <asm/uaccess.h>
37 #include <asm/semaphore.h>
42 static LIST_HEAD(drivers);
43 static DEFINE_MUTEX(core_lists);
44 static DEFINE_IDR(i2c_adapter_idr);
46 #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
48 /* ------------------------------------------------------------------------- */
50 static int i2c_device_match(struct device *dev, struct device_driver *drv)
52 struct i2c_client *client = to_i2c_client(dev);
53 struct i2c_driver *driver = to_i2c_driver(drv);
55 /* make legacy i2c drivers bypass driver model probing entirely;
56 * such drivers scan each i2c adapter/bus themselves.
58 if (!is_newstyle_driver(driver))
61 /* new style drivers use the same kind of driver matching policy
62 * as platform devices or SPI: compare device and driver IDs.
64 return strcmp(client->driver_name, drv->name) == 0;
69 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
70 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
72 struct i2c_client *client = to_i2c_client(dev);
74 /* by definition, legacy drivers can't hotplug */
75 if (dev->driver || !client->driver_name)
78 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
80 dev_dbg(dev, "uevent\n");
85 #define i2c_device_uevent NULL
86 #endif /* CONFIG_HOTPLUG */
88 static int i2c_device_probe(struct device *dev)
90 struct i2c_client *client = to_i2c_client(dev);
91 struct i2c_driver *driver = to_i2c_driver(dev->driver);
95 client->driver = driver;
96 dev_dbg(dev, "probe\n");
97 return driver->probe(client);
100 static int i2c_device_remove(struct device *dev)
102 struct i2c_client *client = to_i2c_client(dev);
103 struct i2c_driver *driver;
109 driver = to_i2c_driver(dev->driver);
110 if (driver->remove) {
111 dev_dbg(dev, "remove\n");
112 status = driver->remove(client);
118 client->driver = NULL;
122 static void i2c_device_shutdown(struct device *dev)
124 struct i2c_driver *driver;
128 driver = to_i2c_driver(dev->driver);
129 if (driver->shutdown)
130 driver->shutdown(to_i2c_client(dev));
133 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
135 struct i2c_driver *driver;
139 driver = to_i2c_driver(dev->driver);
140 if (!driver->suspend)
142 return driver->suspend(to_i2c_client(dev), mesg);
145 static int i2c_device_resume(struct device * dev)
147 struct i2c_driver *driver;
151 driver = to_i2c_driver(dev->driver);
154 return driver->resume(to_i2c_client(dev));
157 static void i2c_client_release(struct device *dev)
159 struct i2c_client *client = to_i2c_client(dev);
160 complete(&client->released);
163 static void i2c_client_dev_release(struct device *dev)
165 kfree(to_i2c_client(dev));
168 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
170 struct i2c_client *client = to_i2c_client(dev);
171 return sprintf(buf, "%s\n", client->name);
174 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
176 struct i2c_client *client = to_i2c_client(dev);
177 return client->driver_name
178 ? sprintf(buf, "%s\n", client->driver_name)
182 static struct device_attribute i2c_dev_attrs[] = {
183 __ATTR(name, S_IRUGO, show_client_name, NULL),
184 /* modalias helps coldplug: modprobe $(cat .../modalias) */
185 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
189 static struct bus_type i2c_bus_type = {
191 .dev_attrs = i2c_dev_attrs,
192 .match = i2c_device_match,
193 .uevent = i2c_device_uevent,
194 .probe = i2c_device_probe,
195 .remove = i2c_device_remove,
196 .shutdown = i2c_device_shutdown,
197 .suspend = i2c_device_suspend,
198 .resume = i2c_device_resume,
202 * i2c_new_device - instantiate an i2c device for use with a new style driver
203 * @adap: the adapter managing the device
204 * @info: describes one I2C device; bus_num is ignored
207 * Create a device to work with a new style i2c driver, where binding is
208 * handled through driver model probe()/remove() methods. This call is not
209 * appropriate for use by mainboad initialization logic, which usually runs
210 * during an arch_initcall() long before any i2c_adapter could exist.
212 * This returns the new i2c client, which may be saved for later use with
213 * i2c_unregister_device(); or NULL to indicate an error.
216 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
218 struct i2c_client *client;
221 client = kzalloc(sizeof *client, GFP_KERNEL);
225 client->adapter = adap;
227 client->dev.platform_data = info->platform_data;
228 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
230 client->flags = info->flags & ~I2C_CLIENT_WAKE;
231 client->addr = info->addr;
232 client->irq = info->irq;
234 strlcpy(client->driver_name, info->driver_name,
235 sizeof(client->driver_name));
236 strlcpy(client->name, info->type, sizeof(client->name));
238 /* a new style driver may be bound to this device when we
239 * return from this function, or any later moment (e.g. maybe
240 * hotplugging will load the driver module). and the device
241 * refcount model is the standard driver model one.
243 status = i2c_attach_client(client);
250 EXPORT_SYMBOL_GPL(i2c_new_device);
254 * i2c_unregister_device - reverse effect of i2c_new_device()
255 * @client: value returned from i2c_new_device()
258 void i2c_unregister_device(struct i2c_client *client)
260 struct i2c_adapter *adapter = client->adapter;
261 struct i2c_driver *driver = client->driver;
263 if (driver && !is_newstyle_driver(driver)) {
264 dev_err(&client->dev, "can't unregister devices "
265 "with legacy drivers\n");
270 mutex_lock(&adapter->clist_lock);
271 list_del(&client->list);
272 mutex_unlock(&adapter->clist_lock);
274 device_unregister(&client->dev);
276 EXPORT_SYMBOL_GPL(i2c_unregister_device);
279 /* ------------------------------------------------------------------------- */
281 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
283 static void i2c_adapter_dev_release(struct device *dev)
285 struct i2c_adapter *adap = to_i2c_adapter(dev);
286 complete(&adap->dev_released);
290 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
292 struct i2c_adapter *adap = to_i2c_adapter(dev);
293 return sprintf(buf, "%s\n", adap->name);
296 static struct device_attribute i2c_adapter_attrs[] = {
297 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
301 static struct class i2c_adapter_class = {
302 .owner = THIS_MODULE,
303 .name = "i2c-adapter",
304 .dev_attrs = i2c_adapter_attrs,
307 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
309 struct i2c_devinfo *devinfo;
311 mutex_lock(&__i2c_board_lock);
312 list_for_each_entry(devinfo, &__i2c_board_list, list) {
313 if (devinfo->busnum == adapter->nr
314 && !i2c_new_device(adapter,
315 &devinfo->board_info))
316 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
317 i2c_adapter_id(adapter),
318 devinfo->board_info.addr);
320 mutex_unlock(&__i2c_board_lock);
323 static int i2c_register_adapter(struct i2c_adapter *adap)
326 struct list_head *item;
327 struct i2c_driver *driver;
329 mutex_init(&adap->bus_lock);
330 mutex_init(&adap->clist_lock);
331 INIT_LIST_HEAD(&adap->clients);
333 mutex_lock(&core_lists);
335 /* Add the adapter to the driver core.
336 * If the parent pointer is not set up,
337 * we add this adapter to the host bus.
339 if (adap->dev.parent == NULL) {
340 adap->dev.parent = &platform_bus;
341 pr_debug("I2C adapter driver [%s] forgot to specify "
342 "physical device\n", adap->name);
344 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
345 adap->dev.release = &i2c_adapter_dev_release;
346 adap->dev.class = &i2c_adapter_class;
347 res = device_register(&adap->dev);
351 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
353 /* create pre-declared device nodes for new-style drivers */
354 if (adap->nr < __i2c_first_dynamic_bus_num)
355 i2c_scan_static_board_info(adap);
357 /* let legacy drivers scan this bus for matching devices */
358 list_for_each(item,&drivers) {
359 driver = list_entry(item, struct i2c_driver, list);
360 if (driver->attach_adapter)
361 /* We ignore the return code; if it fails, too bad */
362 driver->attach_adapter(adap);
366 mutex_unlock(&core_lists);
370 idr_remove(&i2c_adapter_idr, adap->nr);
375 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
376 * @adapter: the adapter to add
379 * This routine is used to declare an I2C adapter when its bus number
380 * doesn't matter. Examples: for I2C adapters dynamically added by
381 * USB links or PCI plugin cards.
383 * When this returns zero, a new bus number was allocated and stored
384 * in adap->nr, and the specified adapter became available for clients.
385 * Otherwise, a negative errno value is returned.
387 int i2c_add_adapter(struct i2c_adapter *adapter)
392 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
395 mutex_lock(&core_lists);
396 /* "above" here means "above or equal to", sigh */
397 res = idr_get_new_above(&i2c_adapter_idr, adapter,
398 __i2c_first_dynamic_bus_num, &id);
399 mutex_unlock(&core_lists);
408 return i2c_register_adapter(adapter);
410 EXPORT_SYMBOL(i2c_add_adapter);
413 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
414 * @adap: the adapter to register (with adap->nr initialized)
417 * This routine is used to declare an I2C adapter when its bus number
418 * matters. Example: for I2C adapters from system-on-chip CPUs, or
419 * otherwise built in to the system's mainboard, and where i2c_board_info
420 * is used to properly configure I2C devices.
422 * If no devices have pre-been declared for this bus, then be sure to
423 * register the adapter before any dynamically allocated ones. Otherwise
424 * the required bus ID may not be available.
426 * When this returns zero, the specified adapter became available for
427 * clients using the bus number provided in adap->nr. Also, the table
428 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
429 * and the appropriate driver model device nodes are created. Otherwise, a
430 * negative errno value is returned.
432 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
437 if (adap->nr & ~MAX_ID_MASK)
441 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
444 mutex_lock(&core_lists);
445 /* "above" here means "above or equal to", sigh;
446 * we need the "equal to" result to force the result
448 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
449 if (status == 0 && id != adap->nr) {
451 idr_remove(&i2c_adapter_idr, id);
453 mutex_unlock(&core_lists);
454 if (status == -EAGAIN)
458 status = i2c_register_adapter(adap);
461 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
464 * i2c_del_adapter - unregister I2C adapter
465 * @adap: the adapter being unregistered
468 * This unregisters an I2C adapter which was previously registered
469 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
471 int i2c_del_adapter(struct i2c_adapter *adap)
473 struct list_head *item, *_n;
474 struct i2c_driver *driver;
475 struct i2c_client *client;
478 mutex_lock(&core_lists);
480 /* First make sure that this adapter was ever added */
481 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
482 pr_debug("i2c-core: attempting to delete unregistered "
483 "adapter [%s]\n", adap->name);
488 list_for_each(item,&drivers) {
489 driver = list_entry(item, struct i2c_driver, list);
490 if (driver->detach_adapter)
491 if ((res = driver->detach_adapter(adap))) {
492 dev_err(&adap->dev, "detach_adapter failed "
494 driver->driver.name);
499 /* detach any active clients. This must be done first, because
500 * it can fail; in which case we give up. */
501 list_for_each_safe(item, _n, &adap->clients) {
502 struct i2c_driver *driver;
504 client = list_entry(item, struct i2c_client, list);
505 driver = client->driver;
507 /* new style, follow standard driver model */
508 if (!driver || is_newstyle_driver(driver)) {
509 i2c_unregister_device(client);
513 /* legacy drivers create and remove clients themselves */
514 if ((res = driver->detach_client(client))) {
515 dev_err(&adap->dev, "detach_client failed for client "
516 "[%s] at address 0x%02x\n", client->name,
522 /* clean up the sysfs representation */
523 init_completion(&adap->dev_released);
524 device_unregister(&adap->dev);
526 /* wait for sysfs to drop all references */
527 wait_for_completion(&adap->dev_released);
530 idr_remove(&i2c_adapter_idr, adap->nr);
532 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
535 mutex_unlock(&core_lists);
538 EXPORT_SYMBOL(i2c_del_adapter);
541 /* ------------------------------------------------------------------------- */
544 * An i2c_driver is used with one or more i2c_client (device) nodes to access
545 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
546 * are two models for binding the driver to its device: "new style" drivers
547 * follow the standard Linux driver model and just respond to probe() calls
548 * issued if the driver core sees they match(); "legacy" drivers create device
552 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
556 /* new style driver methods can't mix with legacy ones */
557 if (is_newstyle_driver(driver)) {
558 if (driver->attach_adapter || driver->detach_adapter
559 || driver->detach_client) {
561 "i2c-core: driver [%s] is confused\n",
562 driver->driver.name);
567 /* add the driver to the list of i2c drivers in the driver core */
568 driver->driver.owner = owner;
569 driver->driver.bus = &i2c_bus_type;
571 /* for new style drivers, when registration returns the driver core
572 * will have called probe() for all matching-but-unbound devices.
574 res = driver_register(&driver->driver);
578 mutex_lock(&core_lists);
580 list_add_tail(&driver->list,&drivers);
581 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
583 /* legacy drivers scan i2c busses directly */
584 if (driver->attach_adapter) {
585 struct i2c_adapter *adapter;
587 down(&i2c_adapter_class.sem);
588 list_for_each_entry(adapter, &i2c_adapter_class.devices,
590 driver->attach_adapter(adapter);
592 up(&i2c_adapter_class.sem);
595 mutex_unlock(&core_lists);
598 EXPORT_SYMBOL(i2c_register_driver);
601 * i2c_del_driver - unregister I2C driver
602 * @driver: the driver being unregistered
605 void i2c_del_driver(struct i2c_driver *driver)
607 struct list_head *item2, *_n;
608 struct i2c_client *client;
609 struct i2c_adapter *adap;
611 mutex_lock(&core_lists);
613 /* new-style driver? */
614 if (is_newstyle_driver(driver))
617 /* Have a look at each adapter, if clients of this driver are still
618 * attached. If so, detach them to be able to kill the driver
621 down(&i2c_adapter_class.sem);
622 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
623 if (driver->detach_adapter) {
624 if (driver->detach_adapter(adap)) {
625 dev_err(&adap->dev, "detach_adapter failed "
627 driver->driver.name);
630 list_for_each_safe(item2, _n, &adap->clients) {
631 client = list_entry(item2, struct i2c_client, list);
632 if (client->driver != driver)
634 dev_dbg(&adap->dev, "detaching client [%s] "
635 "at 0x%02x\n", client->name,
637 if (driver->detach_client(client)) {
638 dev_err(&adap->dev, "detach_client "
639 "failed for client [%s] at "
640 "0x%02x\n", client->name,
646 up(&i2c_adapter_class.sem);
649 driver_unregister(&driver->driver);
650 list_del(&driver->list);
651 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
653 mutex_unlock(&core_lists);
655 EXPORT_SYMBOL(i2c_del_driver);
657 /* ------------------------------------------------------------------------- */
659 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
661 struct list_head *item;
662 struct i2c_client *client;
664 list_for_each(item,&adapter->clients) {
665 client = list_entry(item, struct i2c_client, list);
666 if (client->addr == addr)
672 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
676 mutex_lock(&adapter->clist_lock);
677 rval = __i2c_check_addr(adapter, addr);
678 mutex_unlock(&adapter->clist_lock);
683 int i2c_attach_client(struct i2c_client *client)
685 struct i2c_adapter *adapter = client->adapter;
688 mutex_lock(&adapter->clist_lock);
689 if (__i2c_check_addr(client->adapter, client->addr)) {
693 list_add_tail(&client->list,&adapter->clients);
695 client->dev.parent = &client->adapter->dev;
696 client->dev.bus = &i2c_bus_type;
699 client->dev.driver = &client->driver->driver;
701 if (client->driver && !is_newstyle_driver(client->driver)) {
702 client->dev.release = i2c_client_release;
703 client->dev.uevent_suppress = 1;
705 client->dev.release = i2c_client_dev_release;
707 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
708 "%d-%04x", i2c_adapter_id(adapter), client->addr);
709 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
710 client->name, client->dev.bus_id);
711 res = device_register(&client->dev);
714 mutex_unlock(&adapter->clist_lock);
716 if (adapter->client_register) {
717 if (adapter->client_register(client)) {
718 dev_dbg(&adapter->dev, "client_register "
719 "failed for client [%s] at 0x%02x\n",
720 client->name, client->addr);
727 list_del(&client->list);
728 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
729 "(%d)\n", client->name, client->addr, res);
731 mutex_unlock(&adapter->clist_lock);
734 EXPORT_SYMBOL(i2c_attach_client);
736 int i2c_detach_client(struct i2c_client *client)
738 struct i2c_adapter *adapter = client->adapter;
741 if (adapter->client_unregister) {
742 res = adapter->client_unregister(client);
744 dev_err(&client->dev,
745 "client_unregister [%s] failed, "
746 "client not detached\n", client->name);
751 mutex_lock(&adapter->clist_lock);
752 list_del(&client->list);
753 init_completion(&client->released);
754 device_unregister(&client->dev);
755 mutex_unlock(&adapter->clist_lock);
756 wait_for_completion(&client->released);
761 EXPORT_SYMBOL(i2c_detach_client);
764 * i2c_use_client - increments the reference count of the i2c client structure
765 * @client: the client being referenced
767 * Each live reference to a client should be refcounted. The driver model does
768 * that automatically as part of driver binding, so that most drivers don't
769 * need to do this explicitly: they hold a reference until they're unbound
772 * A pointer to the client with the incremented reference counter is returned.
774 struct i2c_client *i2c_use_client(struct i2c_client *client)
776 get_device(&client->dev);
779 EXPORT_SYMBOL(i2c_use_client);
782 * i2c_release_client - release a use of the i2c client structure
783 * @client: the client being no longer referenced
785 * Must be called when a user of a client is finished with it.
787 void i2c_release_client(struct i2c_client *client)
789 put_device(&client->dev);
791 EXPORT_SYMBOL(i2c_release_client);
793 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
795 struct list_head *item;
796 struct i2c_client *client;
798 mutex_lock(&adap->clist_lock);
799 list_for_each(item,&adap->clients) {
800 client = list_entry(item, struct i2c_client, list);
801 if (!try_module_get(client->driver->driver.owner))
803 if (NULL != client->driver->command) {
804 mutex_unlock(&adap->clist_lock);
805 client->driver->command(client,cmd,arg);
806 mutex_lock(&adap->clist_lock);
808 module_put(client->driver->driver.owner);
810 mutex_unlock(&adap->clist_lock);
812 EXPORT_SYMBOL(i2c_clients_command);
814 static int __init i2c_init(void)
818 retval = bus_register(&i2c_bus_type);
821 return class_register(&i2c_adapter_class);
824 static void __exit i2c_exit(void)
826 class_unregister(&i2c_adapter_class);
827 bus_unregister(&i2c_bus_type);
830 subsys_initcall(i2c_init);
831 module_exit(i2c_exit);
833 /* ----------------------------------------------------
834 * the functional interface to the i2c busses.
835 * ----------------------------------------------------
838 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
842 if (adap->algo->master_xfer) {
844 for (ret = 0; ret < num; ret++) {
845 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
846 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
847 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
848 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
852 mutex_lock_nested(&adap->bus_lock, adap->level);
853 ret = adap->algo->master_xfer(adap,msgs,num);
854 mutex_unlock(&adap->bus_lock);
858 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
862 EXPORT_SYMBOL(i2c_transfer);
864 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
867 struct i2c_adapter *adap=client->adapter;
870 msg.addr = client->addr;
871 msg.flags = client->flags & I2C_M_TEN;
873 msg.buf = (char *)buf;
875 ret = i2c_transfer(adap, &msg, 1);
877 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
878 transmitted, else error code. */
879 return (ret == 1) ? count : ret;
881 EXPORT_SYMBOL(i2c_master_send);
883 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
885 struct i2c_adapter *adap=client->adapter;
889 msg.addr = client->addr;
890 msg.flags = client->flags & I2C_M_TEN;
891 msg.flags |= I2C_M_RD;
895 ret = i2c_transfer(adap, &msg, 1);
897 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
898 transmitted, else error code. */
899 return (ret == 1) ? count : ret;
901 EXPORT_SYMBOL(i2c_master_recv);
903 /* ----------------------------------------------------
904 * the i2c address scanning function
905 * Will not work for 10-bit addresses!
906 * ----------------------------------------------------
908 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
909 int (*found_proc) (struct i2c_adapter *, int, int))
913 /* Make sure the address is valid */
914 if (addr < 0x03 || addr > 0x77) {
915 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
920 /* Skip if already in use */
921 if (i2c_check_addr(adapter, addr))
924 /* Make sure there is something at this address, unless forced */
926 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
927 I2C_SMBUS_QUICK, NULL) < 0)
930 /* prevent 24RF08 corruption */
931 if ((addr & ~0x0f) == 0x50)
932 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
933 I2C_SMBUS_QUICK, NULL);
936 /* Finally call the custom detection function */
937 err = found_proc(adapter, addr, kind);
938 /* -ENODEV can be returned if there is a chip at the given address
939 but it isn't supported by this chip driver. We catch it here as
940 this isn't an error. */
945 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
950 int i2c_probe(struct i2c_adapter *adapter,
951 const struct i2c_client_address_data *address_data,
952 int (*found_proc) (struct i2c_adapter *, int, int))
955 int adap_id = i2c_adapter_id(adapter);
957 /* Force entries are done first, and are not affected by ignore
959 if (address_data->forces) {
960 const unsigned short * const *forces = address_data->forces;
963 for (kind = 0; forces[kind]; kind++) {
964 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
966 if (forces[kind][i] == adap_id
967 || forces[kind][i] == ANY_I2C_BUS) {
968 dev_dbg(&adapter->dev, "found force "
969 "parameter for adapter %d, "
970 "addr 0x%02x, kind %d\n",
971 adap_id, forces[kind][i + 1],
973 err = i2c_probe_address(adapter,
983 /* Stop here if we can't use SMBUS_QUICK */
984 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
985 if (address_data->probe[0] == I2C_CLIENT_END
986 && address_data->normal_i2c[0] == I2C_CLIENT_END)
989 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
990 "can't probe for chips\n");
994 /* Probe entries are done second, and are not affected by ignore
996 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
997 if (address_data->probe[i] == adap_id
998 || address_data->probe[i] == ANY_I2C_BUS) {
999 dev_dbg(&adapter->dev, "found probe parameter for "
1000 "adapter %d, addr 0x%02x\n", adap_id,
1001 address_data->probe[i + 1]);
1002 err = i2c_probe_address(adapter,
1003 address_data->probe[i + 1],
1010 /* Normal entries are done last, unless shadowed by an ignore entry */
1011 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1015 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1017 if ((address_data->ignore[j] == adap_id ||
1018 address_data->ignore[j] == ANY_I2C_BUS)
1019 && address_data->ignore[j + 1]
1020 == address_data->normal_i2c[i]) {
1021 dev_dbg(&adapter->dev, "found ignore "
1022 "parameter for adapter %d, "
1023 "addr 0x%02x\n", adap_id,
1024 address_data->ignore[j + 1]);
1032 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1033 "addr 0x%02x\n", adap_id,
1034 address_data->normal_i2c[i]);
1035 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1043 EXPORT_SYMBOL(i2c_probe);
1046 i2c_new_probed_device(struct i2c_adapter *adap,
1047 struct i2c_board_info *info,
1048 unsigned short const *addr_list)
1052 /* Stop here if the bus doesn't support probing */
1053 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1054 dev_err(&adap->dev, "Probing not supported\n");
1058 mutex_lock(&adap->clist_lock);
1059 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1060 /* Check address validity */
1061 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1062 dev_warn(&adap->dev, "Invalid 7-bit address "
1063 "0x%02x\n", addr_list[i]);
1067 /* Check address availability */
1068 if (__i2c_check_addr(adap, addr_list[i])) {
1069 dev_dbg(&adap->dev, "Address 0x%02x already in "
1070 "use, not probing\n", addr_list[i]);
1074 /* Test address responsiveness
1075 The default probe method is a quick write, but it is known
1076 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1077 and could also irreversibly write-protect some EEPROMs, so
1078 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1079 read instead. Also, some bus drivers don't implement
1080 quick write, so we fallback to a byte read it that case
1082 if ((addr_list[i] & ~0x07) == 0x30
1083 || (addr_list[i] & ~0x0f) == 0x50
1084 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1085 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1087 I2C_SMBUS_BYTE, NULL) >= 0)
1090 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1092 I2C_SMBUS_QUICK, NULL) >= 0)
1096 mutex_unlock(&adap->clist_lock);
1098 if (addr_list[i] == I2C_CLIENT_END) {
1099 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1103 info->addr = addr_list[i];
1104 return i2c_new_device(adap, info);
1106 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1108 struct i2c_adapter* i2c_get_adapter(int id)
1110 struct i2c_adapter *adapter;
1112 mutex_lock(&core_lists);
1113 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1114 if (adapter && !try_module_get(adapter->owner))
1117 mutex_unlock(&core_lists);
1120 EXPORT_SYMBOL(i2c_get_adapter);
1122 void i2c_put_adapter(struct i2c_adapter *adap)
1124 module_put(adap->owner);
1126 EXPORT_SYMBOL(i2c_put_adapter);
1128 /* The SMBus parts */
1130 #define POLY (0x1070U << 3)
1136 for(i = 0; i < 8; i++) {
1141 return (u8)(data >> 8);
1144 /* Incremental CRC8 over count bytes in the array pointed to by p */
1145 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1149 for(i = 0; i < count; i++)
1150 crc = crc8((crc ^ p[i]) << 8);
1154 /* Assume a 7-bit address, which is reasonable for SMBus */
1155 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1157 /* The address will be sent first */
1158 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1159 pec = i2c_smbus_pec(pec, &addr, 1);
1161 /* The data buffer follows */
1162 return i2c_smbus_pec(pec, msg->buf, msg->len);
1165 /* Used for write only transactions */
1166 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1168 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1172 /* Return <0 on CRC error
1173 If there was a write before this read (most cases) we need to take the
1174 partial CRC from the write part into account.
1175 Note that this function does modify the message (we need to decrease the
1176 message length to hide the CRC byte from the caller). */
1177 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1179 u8 rpec = msg->buf[--msg->len];
1180 cpec = i2c_smbus_msg_pec(cpec, msg);
1183 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1190 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1192 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1193 value,0,I2C_SMBUS_QUICK,NULL);
1195 EXPORT_SYMBOL(i2c_smbus_write_quick);
1197 s32 i2c_smbus_read_byte(struct i2c_client *client)
1199 union i2c_smbus_data data;
1200 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1201 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1206 EXPORT_SYMBOL(i2c_smbus_read_byte);
1208 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1210 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1211 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1213 EXPORT_SYMBOL(i2c_smbus_write_byte);
1215 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1217 union i2c_smbus_data data;
1218 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1219 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1224 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1226 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1228 union i2c_smbus_data data;
1230 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1231 I2C_SMBUS_WRITE,command,
1232 I2C_SMBUS_BYTE_DATA,&data);
1234 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1236 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1238 union i2c_smbus_data data;
1239 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1240 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1245 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1247 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1249 union i2c_smbus_data data;
1251 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1252 I2C_SMBUS_WRITE,command,
1253 I2C_SMBUS_WORD_DATA,&data);
1255 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1258 * i2c_smbus_read_block_data - SMBus block read request
1259 * @client: Handle to slave device
1260 * @command: Command byte issued to let the slave know what data should
1262 * @values: Byte array into which data will be read; big enough to hold
1263 * the data returned by the slave. SMBus allows at most 32 bytes.
1265 * Returns the number of bytes read in the slave's response, else a
1266 * negative number to indicate some kind of error.
1268 * Note that using this function requires that the client's adapter support
1269 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1270 * support this; its emulation through I2C messaging relies on a specific
1271 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1273 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1276 union i2c_smbus_data data;
1278 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1279 I2C_SMBUS_READ, command,
1280 I2C_SMBUS_BLOCK_DATA, &data))
1283 memcpy(values, &data.block[1], data.block[0]);
1284 return data.block[0];
1286 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1288 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1289 u8 length, const u8 *values)
1291 union i2c_smbus_data data;
1293 if (length > I2C_SMBUS_BLOCK_MAX)
1294 length = I2C_SMBUS_BLOCK_MAX;
1295 data.block[0] = length;
1296 memcpy(&data.block[1], values, length);
1297 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1298 I2C_SMBUS_WRITE,command,
1299 I2C_SMBUS_BLOCK_DATA,&data);
1301 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1303 /* Returns the number of read bytes */
1304 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1305 u8 length, u8 *values)
1307 union i2c_smbus_data data;
1309 if (length > I2C_SMBUS_BLOCK_MAX)
1310 length = I2C_SMBUS_BLOCK_MAX;
1311 data.block[0] = length;
1312 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1313 I2C_SMBUS_READ,command,
1314 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1317 memcpy(values, &data.block[1], data.block[0]);
1318 return data.block[0];
1320 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1322 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1323 u8 length, const u8 *values)
1325 union i2c_smbus_data data;
1327 if (length > I2C_SMBUS_BLOCK_MAX)
1328 length = I2C_SMBUS_BLOCK_MAX;
1329 data.block[0] = length;
1330 memcpy(data.block + 1, values, length);
1331 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1332 I2C_SMBUS_WRITE, command,
1333 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1335 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1337 /* Simulate a SMBus command using the i2c protocol
1338 No checking of parameters is done! */
1339 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1340 unsigned short flags,
1341 char read_write, u8 command, int size,
1342 union i2c_smbus_data * data)
1344 /* So we need to generate a series of msgs. In the case of writing, we
1345 need to use only one message; when reading, we need two. We initialize
1346 most things with sane defaults, to keep the code below somewhat
1348 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1349 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1350 int num = read_write == I2C_SMBUS_READ?2:1;
1351 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1352 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1357 msgbuf0[0] = command;
1359 case I2C_SMBUS_QUICK:
1361 /* Special case: The read/write field is used as data */
1362 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1365 case I2C_SMBUS_BYTE:
1366 if (read_write == I2C_SMBUS_READ) {
1367 /* Special case: only a read! */
1368 msg[0].flags = I2C_M_RD | flags;
1372 case I2C_SMBUS_BYTE_DATA:
1373 if (read_write == I2C_SMBUS_READ)
1377 msgbuf0[1] = data->byte;
1380 case I2C_SMBUS_WORD_DATA:
1381 if (read_write == I2C_SMBUS_READ)
1385 msgbuf0[1] = data->word & 0xff;
1386 msgbuf0[2] = data->word >> 8;
1389 case I2C_SMBUS_PROC_CALL:
1390 num = 2; /* Special case */
1391 read_write = I2C_SMBUS_READ;
1394 msgbuf0[1] = data->word & 0xff;
1395 msgbuf0[2] = data->word >> 8;
1397 case I2C_SMBUS_BLOCK_DATA:
1398 if (read_write == I2C_SMBUS_READ) {
1399 msg[1].flags |= I2C_M_RECV_LEN;
1400 msg[1].len = 1; /* block length will be added by
1401 the underlying bus driver */
1403 msg[0].len = data->block[0] + 2;
1404 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1405 dev_err(&adapter->dev, "smbus_access called with "
1406 "invalid block write size (%d)\n",
1410 for (i = 1; i < msg[0].len; i++)
1411 msgbuf0[i] = data->block[i-1];
1414 case I2C_SMBUS_BLOCK_PROC_CALL:
1415 num = 2; /* Another special case */
1416 read_write = I2C_SMBUS_READ;
1417 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1418 dev_err(&adapter->dev, "%s called with invalid "
1419 "block proc call size (%d)\n", __FUNCTION__,
1423 msg[0].len = data->block[0] + 2;
1424 for (i = 1; i < msg[0].len; i++)
1425 msgbuf0[i] = data->block[i-1];
1426 msg[1].flags |= I2C_M_RECV_LEN;
1427 msg[1].len = 1; /* block length will be added by
1428 the underlying bus driver */
1430 case I2C_SMBUS_I2C_BLOCK_DATA:
1431 if (read_write == I2C_SMBUS_READ) {
1432 msg[1].len = data->block[0];
1434 msg[0].len = data->block[0] + 1;
1435 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1436 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1437 "invalid block write size (%d)\n",
1441 for (i = 1; i <= data->block[0]; i++)
1442 msgbuf0[i] = data->block[i];
1446 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1451 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1452 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1454 /* Compute PEC if first message is a write */
1455 if (!(msg[0].flags & I2C_M_RD)) {
1456 if (num == 1) /* Write only */
1457 i2c_smbus_add_pec(&msg[0]);
1458 else /* Write followed by read */
1459 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1461 /* Ask for PEC if last message is a read */
1462 if (msg[num-1].flags & I2C_M_RD)
1466 if (i2c_transfer(adapter, msg, num) < 0)
1469 /* Check PEC if last message is a read */
1470 if (i && (msg[num-1].flags & I2C_M_RD)) {
1471 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1475 if (read_write == I2C_SMBUS_READ)
1477 case I2C_SMBUS_BYTE:
1478 data->byte = msgbuf0[0];
1480 case I2C_SMBUS_BYTE_DATA:
1481 data->byte = msgbuf1[0];
1483 case I2C_SMBUS_WORD_DATA:
1484 case I2C_SMBUS_PROC_CALL:
1485 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1487 case I2C_SMBUS_I2C_BLOCK_DATA:
1488 for (i = 0; i < data->block[0]; i++)
1489 data->block[i+1] = msgbuf1[i];
1491 case I2C_SMBUS_BLOCK_DATA:
1492 case I2C_SMBUS_BLOCK_PROC_CALL:
1493 for (i = 0; i < msgbuf1[0] + 1; i++)
1494 data->block[i] = msgbuf1[i];
1501 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1502 char read_write, u8 command, int size,
1503 union i2c_smbus_data * data)
1507 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1509 if (adapter->algo->smbus_xfer) {
1510 mutex_lock(&adapter->bus_lock);
1511 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1513 mutex_unlock(&adapter->bus_lock);
1515 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1520 EXPORT_SYMBOL(i2c_smbus_xfer);
1522 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1523 MODULE_DESCRIPTION("I2C-Bus main module");
1524 MODULE_LICENSE("GPL");