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>
41 static LIST_HEAD(adapters);
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, char **envp, int num_envp,
71 char *buffer, int buffer_size)
73 struct i2c_client *client = to_i2c_client(dev);
74 int i = 0, length = 0;
76 /* by definition, legacy drivers can't hotplug */
77 if (dev->driver || !client->driver_name)
80 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
81 "MODALIAS=%s", client->driver_name))
84 dev_dbg(dev, "uevent\n");
89 #define i2c_device_uevent NULL
90 #endif /* CONFIG_HOTPLUG */
92 static int i2c_device_probe(struct device *dev)
94 struct i2c_client *client = to_i2c_client(dev);
95 struct i2c_driver *driver = to_i2c_driver(dev->driver);
99 client->driver = driver;
100 dev_dbg(dev, "probe\n");
101 return driver->probe(client);
104 static int i2c_device_remove(struct device *dev)
106 struct i2c_client *client = to_i2c_client(dev);
107 struct i2c_driver *driver;
113 driver = to_i2c_driver(dev->driver);
114 if (driver->remove) {
115 dev_dbg(dev, "remove\n");
116 status = driver->remove(client);
122 client->driver = NULL;
126 static void i2c_device_shutdown(struct device *dev)
128 struct i2c_driver *driver;
132 driver = to_i2c_driver(dev->driver);
133 if (driver->shutdown)
134 driver->shutdown(to_i2c_client(dev));
137 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
139 struct i2c_driver *driver;
143 driver = to_i2c_driver(dev->driver);
144 if (!driver->suspend)
146 return driver->suspend(to_i2c_client(dev), mesg);
149 static int i2c_device_resume(struct device * dev)
151 struct i2c_driver *driver;
155 driver = to_i2c_driver(dev->driver);
158 return driver->resume(to_i2c_client(dev));
161 static void i2c_client_release(struct device *dev)
163 struct i2c_client *client = to_i2c_client(dev);
164 complete(&client->released);
167 static void i2c_client_dev_release(struct device *dev)
169 kfree(to_i2c_client(dev));
172 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
174 struct i2c_client *client = to_i2c_client(dev);
175 return sprintf(buf, "%s\n", client->name);
178 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
180 struct i2c_client *client = to_i2c_client(dev);
181 return client->driver_name
182 ? sprintf(buf, "%s\n", client->driver_name)
186 static struct device_attribute i2c_dev_attrs[] = {
187 __ATTR(name, S_IRUGO, show_client_name, NULL),
188 /* modalias helps coldplug: modprobe $(cat .../modalias) */
189 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
193 struct bus_type i2c_bus_type = {
195 .dev_attrs = i2c_dev_attrs,
196 .match = i2c_device_match,
197 .uevent = i2c_device_uevent,
198 .probe = i2c_device_probe,
199 .remove = i2c_device_remove,
200 .shutdown = i2c_device_shutdown,
201 .suspend = i2c_device_suspend,
202 .resume = i2c_device_resume,
206 * i2c_new_device - instantiate an i2c device for use with a new style driver
207 * @adap: the adapter managing the device
208 * @info: describes one I2C device; bus_num is ignored
210 * Create a device to work with a new style i2c driver, where binding is
211 * handled through driver model probe()/remove() methods. This call is not
212 * appropriate for use by mainboad initialization logic, which usually runs
213 * during an arch_initcall() long before any i2c_adapter could exist.
215 * This returns the new i2c client, which may be saved for later use with
216 * i2c_unregister_device(); or NULL to indicate an error.
219 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
221 struct i2c_client *client;
224 client = kzalloc(sizeof *client, GFP_KERNEL);
228 client->adapter = adap;
230 client->dev.platform_data = info->platform_data;
231 client->flags = info->flags;
232 client->addr = info->addr;
233 client->irq = info->irq;
235 strlcpy(client->driver_name, info->driver_name,
236 sizeof(client->driver_name));
237 strlcpy(client->name, info->type, sizeof(client->name));
239 /* a new style driver may be bound to this device when we
240 * return from this function, or any later moment (e.g. maybe
241 * hotplugging will load the driver module). and the device
242 * refcount model is the standard driver model one.
244 status = i2c_attach_client(client);
251 EXPORT_SYMBOL_GPL(i2c_new_device);
255 * i2c_unregister_device - reverse effect of i2c_new_device()
256 * @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 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 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);
334 list_add_tail(&adap->list, &adapters);
336 /* Add the adapter to the driver core.
337 * If the parent pointer is not set up,
338 * we add this adapter to the host bus.
340 if (adap->dev.parent == NULL) {
341 adap->dev.parent = &platform_bus;
342 pr_debug("I2C adapter driver [%s] forgot to specify "
343 "physical device\n", adap->name);
345 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
346 adap->dev.release = &i2c_adapter_dev_release;
347 adap->dev.class = &i2c_adapter_class;
348 res = device_register(&adap->dev);
352 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
354 /* create pre-declared device nodes for new-style drivers */
355 if (adap->nr < __i2c_first_dynamic_bus_num)
356 i2c_scan_static_board_info(adap);
358 /* let legacy drivers scan this bus for matching devices */
359 list_for_each(item,&drivers) {
360 driver = list_entry(item, struct i2c_driver, list);
361 if (driver->attach_adapter)
362 /* We ignore the return code; if it fails, too bad */
363 driver->attach_adapter(adap);
367 mutex_unlock(&core_lists);
371 list_del(&adap->list);
372 idr_remove(&i2c_adapter_idr, adap->nr);
377 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
378 * @adapter: the adapter to add
380 * This routine is used to declare an I2C adapter when its bus number
381 * doesn't matter. Examples: for I2C adapters dynamically added by
382 * USB links or PCI plugin cards.
384 * When this returns zero, a new bus number was allocated and stored
385 * in adap->nr, and the specified adapter became available for clients.
386 * Otherwise, a negative errno value is returned.
388 int i2c_add_adapter(struct i2c_adapter *adapter)
393 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
396 mutex_lock(&core_lists);
397 /* "above" here means "above or equal to", sigh */
398 res = idr_get_new_above(&i2c_adapter_idr, adapter,
399 __i2c_first_dynamic_bus_num, &id);
400 mutex_unlock(&core_lists);
409 return i2c_register_adapter(adapter);
411 EXPORT_SYMBOL(i2c_add_adapter);
414 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
415 * @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);
463 int i2c_del_adapter(struct i2c_adapter *adap)
465 struct list_head *item, *_n;
466 struct i2c_adapter *adap_from_list;
467 struct i2c_driver *driver;
468 struct i2c_client *client;
471 mutex_lock(&core_lists);
473 /* First make sure that this adapter was ever added */
474 list_for_each_entry(adap_from_list, &adapters, list) {
475 if (adap_from_list == adap)
478 if (adap_from_list != adap) {
479 pr_debug("i2c-core: attempting to delete unregistered "
480 "adapter [%s]\n", adap->name);
485 list_for_each(item,&drivers) {
486 driver = list_entry(item, struct i2c_driver, list);
487 if (driver->detach_adapter)
488 if ((res = driver->detach_adapter(adap))) {
489 dev_err(&adap->dev, "detach_adapter failed "
491 driver->driver.name);
496 /* detach any active clients. This must be done first, because
497 * it can fail; in which case we give up. */
498 list_for_each_safe(item, _n, &adap->clients) {
499 struct i2c_driver *driver;
501 client = list_entry(item, struct i2c_client, list);
502 driver = client->driver;
504 /* new style, follow standard driver model */
505 if (!driver || is_newstyle_driver(driver)) {
506 i2c_unregister_device(client);
510 /* legacy drivers create and remove clients themselves */
511 if ((res = driver->detach_client(client))) {
512 dev_err(&adap->dev, "detach_client failed for client "
513 "[%s] at address 0x%02x\n", client->name,
519 /* clean up the sysfs representation */
520 init_completion(&adap->dev_released);
521 device_unregister(&adap->dev);
522 list_del(&adap->list);
524 /* wait for sysfs to drop all references */
525 wait_for_completion(&adap->dev_released);
528 idr_remove(&i2c_adapter_idr, adap->nr);
530 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
533 mutex_unlock(&core_lists);
538 /* ------------------------------------------------------------------------- */
541 * An i2c_driver is used with one or more i2c_client (device) nodes to access
542 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
543 * are two models for binding the driver to its device: "new style" drivers
544 * follow the standard Linux driver model and just respond to probe() calls
545 * issued if the driver core sees they match(); "legacy" drivers create device
549 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
553 /* new style driver methods can't mix with legacy ones */
554 if (is_newstyle_driver(driver)) {
555 if (driver->attach_adapter || driver->detach_adapter
556 || driver->detach_client) {
558 "i2c-core: driver [%s] is confused\n",
559 driver->driver.name);
564 /* add the driver to the list of i2c drivers in the driver core */
565 driver->driver.owner = owner;
566 driver->driver.bus = &i2c_bus_type;
568 /* for new style drivers, when registration returns the driver core
569 * will have called probe() for all matching-but-unbound devices.
571 res = driver_register(&driver->driver);
575 mutex_lock(&core_lists);
577 list_add_tail(&driver->list,&drivers);
578 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
580 /* legacy drivers scan i2c busses directly */
581 if (driver->attach_adapter) {
582 struct i2c_adapter *adapter;
584 list_for_each_entry(adapter, &adapters, list) {
585 driver->attach_adapter(adapter);
589 mutex_unlock(&core_lists);
592 EXPORT_SYMBOL(i2c_register_driver);
595 * i2c_del_driver - unregister I2C driver
596 * @driver: the driver being unregistered
598 int i2c_del_driver(struct i2c_driver *driver)
600 struct list_head *item1, *item2, *_n;
601 struct i2c_client *client;
602 struct i2c_adapter *adap;
606 mutex_lock(&core_lists);
608 /* new-style driver? */
609 if (is_newstyle_driver(driver))
612 /* Have a look at each adapter, if clients of this driver are still
613 * attached. If so, detach them to be able to kill the driver
616 list_for_each(item1,&adapters) {
617 adap = list_entry(item1, struct i2c_adapter, list);
618 if (driver->detach_adapter) {
619 if ((res = driver->detach_adapter(adap))) {
620 dev_err(&adap->dev, "detach_adapter failed "
622 driver->driver.name);
626 list_for_each_safe(item2, _n, &adap->clients) {
627 client = list_entry(item2, struct i2c_client, list);
628 if (client->driver != driver)
630 dev_dbg(&adap->dev, "detaching client [%s] "
631 "at 0x%02x\n", client->name,
633 if ((res = driver->detach_client(client))) {
634 dev_err(&adap->dev, "detach_client "
635 "failed for client [%s] at "
636 "0x%02x\n", client->name,
645 driver_unregister(&driver->driver);
646 list_del(&driver->list);
647 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
650 mutex_unlock(&core_lists);
654 /* ------------------------------------------------------------------------- */
656 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
658 struct list_head *item;
659 struct i2c_client *client;
661 list_for_each(item,&adapter->clients) {
662 client = list_entry(item, struct i2c_client, list);
663 if (client->addr == addr)
669 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
673 mutex_lock(&adapter->clist_lock);
674 rval = __i2c_check_addr(adapter, addr);
675 mutex_unlock(&adapter->clist_lock);
680 int i2c_attach_client(struct i2c_client *client)
682 struct i2c_adapter *adapter = client->adapter;
685 mutex_lock(&adapter->clist_lock);
686 if (__i2c_check_addr(client->adapter, client->addr)) {
690 list_add_tail(&client->list,&adapter->clients);
692 client->usage_count = 0;
694 client->dev.parent = &client->adapter->dev;
695 client->dev.bus = &i2c_bus_type;
698 client->dev.driver = &client->driver->driver;
700 if (client->driver && !is_newstyle_driver(client->driver))
701 client->dev.release = i2c_client_release;
703 client->dev.release = i2c_client_dev_release;
705 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
706 "%d-%04x", i2c_adapter_id(adapter), client->addr);
707 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
708 client->name, client->dev.bus_id);
709 res = device_register(&client->dev);
712 mutex_unlock(&adapter->clist_lock);
714 if (adapter->client_register) {
715 if (adapter->client_register(client)) {
716 dev_dbg(&adapter->dev, "client_register "
717 "failed for client [%s] at 0x%02x\n",
718 client->name, client->addr);
725 list_del(&client->list);
726 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
727 "(%d)\n", client->name, client->addr, res);
729 mutex_unlock(&adapter->clist_lock);
734 int i2c_detach_client(struct i2c_client *client)
736 struct i2c_adapter *adapter = client->adapter;
739 if (client->usage_count > 0) {
740 dev_warn(&client->dev, "Client [%s] still busy, "
741 "can't detach\n", client->name);
745 if (adapter->client_unregister) {
746 res = adapter->client_unregister(client);
748 dev_err(&client->dev,
749 "client_unregister [%s] failed, "
750 "client not detached\n", client->name);
755 mutex_lock(&adapter->clist_lock);
756 list_del(&client->list);
757 init_completion(&client->released);
758 device_unregister(&client->dev);
759 mutex_unlock(&adapter->clist_lock);
760 wait_for_completion(&client->released);
766 static int i2c_inc_use_client(struct i2c_client *client)
769 if (!try_module_get(client->driver->driver.owner))
771 if (!try_module_get(client->adapter->owner)) {
772 module_put(client->driver->driver.owner);
779 static void i2c_dec_use_client(struct i2c_client *client)
781 module_put(client->driver->driver.owner);
782 module_put(client->adapter->owner);
785 int i2c_use_client(struct i2c_client *client)
789 ret = i2c_inc_use_client(client);
793 client->usage_count++;
798 int i2c_release_client(struct i2c_client *client)
800 if (!client->usage_count) {
801 pr_debug("i2c-core: %s used one too many times\n",
806 client->usage_count--;
807 i2c_dec_use_client(client);
812 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
814 struct list_head *item;
815 struct i2c_client *client;
817 mutex_lock(&adap->clist_lock);
818 list_for_each(item,&adap->clients) {
819 client = list_entry(item, struct i2c_client, list);
820 if (!try_module_get(client->driver->driver.owner))
822 if (NULL != client->driver->command) {
823 mutex_unlock(&adap->clist_lock);
824 client->driver->command(client,cmd,arg);
825 mutex_lock(&adap->clist_lock);
827 module_put(client->driver->driver.owner);
829 mutex_unlock(&adap->clist_lock);
832 static int __init i2c_init(void)
836 retval = bus_register(&i2c_bus_type);
839 return class_register(&i2c_adapter_class);
842 static void __exit i2c_exit(void)
844 class_unregister(&i2c_adapter_class);
845 bus_unregister(&i2c_bus_type);
848 subsys_initcall(i2c_init);
849 module_exit(i2c_exit);
851 /* ----------------------------------------------------
852 * the functional interface to the i2c busses.
853 * ----------------------------------------------------
856 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
860 if (adap->algo->master_xfer) {
862 for (ret = 0; ret < num; ret++) {
863 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
864 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
865 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
866 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
870 mutex_lock_nested(&adap->bus_lock, adap->level);
871 ret = adap->algo->master_xfer(adap,msgs,num);
872 mutex_unlock(&adap->bus_lock);
876 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
881 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
884 struct i2c_adapter *adap=client->adapter;
887 msg.addr = client->addr;
888 msg.flags = client->flags & I2C_M_TEN;
890 msg.buf = (char *)buf;
892 ret = i2c_transfer(adap, &msg, 1);
894 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
895 transmitted, else error code. */
896 return (ret == 1) ? count : ret;
899 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
901 struct i2c_adapter *adap=client->adapter;
905 msg.addr = client->addr;
906 msg.flags = client->flags & I2C_M_TEN;
907 msg.flags |= I2C_M_RD;
911 ret = i2c_transfer(adap, &msg, 1);
913 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
914 transmitted, else error code. */
915 return (ret == 1) ? count : ret;
919 int i2c_control(struct i2c_client *client,
920 unsigned int cmd, unsigned long arg)
923 struct i2c_adapter *adap = client->adapter;
925 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
934 if (adap->algo->algo_control!=NULL)
935 ret = adap->algo->algo_control(adap,cmd,arg);
940 /* ----------------------------------------------------
941 * the i2c address scanning function
942 * Will not work for 10-bit addresses!
943 * ----------------------------------------------------
945 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
946 int (*found_proc) (struct i2c_adapter *, int, int))
950 /* Make sure the address is valid */
951 if (addr < 0x03 || addr > 0x77) {
952 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
957 /* Skip if already in use */
958 if (i2c_check_addr(adapter, addr))
961 /* Make sure there is something at this address, unless forced */
963 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
964 I2C_SMBUS_QUICK, NULL) < 0)
967 /* prevent 24RF08 corruption */
968 if ((addr & ~0x0f) == 0x50)
969 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
970 I2C_SMBUS_QUICK, NULL);
973 /* Finally call the custom detection function */
974 err = found_proc(adapter, addr, kind);
975 /* -ENODEV can be returned if there is a chip at the given address
976 but it isn't supported by this chip driver. We catch it here as
977 this isn't an error. */
982 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
987 int i2c_probe(struct i2c_adapter *adapter,
988 struct i2c_client_address_data *address_data,
989 int (*found_proc) (struct i2c_adapter *, int, int))
992 int adap_id = i2c_adapter_id(adapter);
994 /* Force entries are done first, and are not affected by ignore
996 if (address_data->forces) {
997 unsigned short **forces = address_data->forces;
1000 for (kind = 0; forces[kind]; kind++) {
1001 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1003 if (forces[kind][i] == adap_id
1004 || forces[kind][i] == ANY_I2C_BUS) {
1005 dev_dbg(&adapter->dev, "found force "
1006 "parameter for adapter %d, "
1007 "addr 0x%02x, kind %d\n",
1008 adap_id, forces[kind][i + 1],
1010 err = i2c_probe_address(adapter,
1011 forces[kind][i + 1],
1020 /* Stop here if we can't use SMBUS_QUICK */
1021 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1022 if (address_data->probe[0] == I2C_CLIENT_END
1023 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1026 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1027 "can't probe for chips\n");
1031 /* Probe entries are done second, and are not affected by ignore
1033 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1034 if (address_data->probe[i] == adap_id
1035 || address_data->probe[i] == ANY_I2C_BUS) {
1036 dev_dbg(&adapter->dev, "found probe parameter for "
1037 "adapter %d, addr 0x%02x\n", adap_id,
1038 address_data->probe[i + 1]);
1039 err = i2c_probe_address(adapter,
1040 address_data->probe[i + 1],
1047 /* Normal entries are done last, unless shadowed by an ignore entry */
1048 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1052 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1054 if ((address_data->ignore[j] == adap_id ||
1055 address_data->ignore[j] == ANY_I2C_BUS)
1056 && address_data->ignore[j + 1]
1057 == address_data->normal_i2c[i]) {
1058 dev_dbg(&adapter->dev, "found ignore "
1059 "parameter for adapter %d, "
1060 "addr 0x%02x\n", adap_id,
1061 address_data->ignore[j + 1]);
1069 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1070 "addr 0x%02x\n", adap_id,
1071 address_data->normal_i2c[i]);
1072 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1081 struct i2c_adapter* i2c_get_adapter(int id)
1083 struct i2c_adapter *adapter;
1085 mutex_lock(&core_lists);
1086 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1087 if (adapter && !try_module_get(adapter->owner))
1090 mutex_unlock(&core_lists);
1094 void i2c_put_adapter(struct i2c_adapter *adap)
1096 module_put(adap->owner);
1099 /* The SMBus parts */
1101 #define POLY (0x1070U << 3)
1107 for(i = 0; i < 8; i++) {
1112 return (u8)(data >> 8);
1115 /* Incremental CRC8 over count bytes in the array pointed to by p */
1116 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1120 for(i = 0; i < count; i++)
1121 crc = crc8((crc ^ p[i]) << 8);
1125 /* Assume a 7-bit address, which is reasonable for SMBus */
1126 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1128 /* The address will be sent first */
1129 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1130 pec = i2c_smbus_pec(pec, &addr, 1);
1132 /* The data buffer follows */
1133 return i2c_smbus_pec(pec, msg->buf, msg->len);
1136 /* Used for write only transactions */
1137 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1139 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1143 /* Return <0 on CRC error
1144 If there was a write before this read (most cases) we need to take the
1145 partial CRC from the write part into account.
1146 Note that this function does modify the message (we need to decrease the
1147 message length to hide the CRC byte from the caller). */
1148 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1150 u8 rpec = msg->buf[--msg->len];
1151 cpec = i2c_smbus_msg_pec(cpec, msg);
1154 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1161 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1163 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1164 value,0,I2C_SMBUS_QUICK,NULL);
1167 s32 i2c_smbus_read_byte(struct i2c_client *client)
1169 union i2c_smbus_data data;
1170 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1171 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1177 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1179 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1180 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1183 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1185 union i2c_smbus_data data;
1186 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1187 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1193 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1195 union i2c_smbus_data data;
1197 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1198 I2C_SMBUS_WRITE,command,
1199 I2C_SMBUS_BYTE_DATA,&data);
1202 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1204 union i2c_smbus_data data;
1205 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1206 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1212 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1214 union i2c_smbus_data data;
1216 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1217 I2C_SMBUS_WRITE,command,
1218 I2C_SMBUS_WORD_DATA,&data);
1221 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1222 u8 length, const u8 *values)
1224 union i2c_smbus_data data;
1226 if (length > I2C_SMBUS_BLOCK_MAX)
1227 length = I2C_SMBUS_BLOCK_MAX;
1228 data.block[0] = length;
1229 memcpy(&data.block[1], values, length);
1230 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1231 I2C_SMBUS_WRITE,command,
1232 I2C_SMBUS_BLOCK_DATA,&data);
1235 /* Returns the number of read bytes */
1236 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1238 union i2c_smbus_data data;
1240 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1241 I2C_SMBUS_READ,command,
1242 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1245 memcpy(values, &data.block[1], data.block[0]);
1246 return data.block[0];
1249 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1250 u8 length, const u8 *values)
1252 union i2c_smbus_data data;
1254 if (length > I2C_SMBUS_BLOCK_MAX)
1255 length = I2C_SMBUS_BLOCK_MAX;
1256 data.block[0] = length;
1257 memcpy(data.block + 1, values, length);
1258 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1259 I2C_SMBUS_WRITE, command,
1260 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1263 /* Simulate a SMBus command using the i2c protocol
1264 No checking of parameters is done! */
1265 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1266 unsigned short flags,
1267 char read_write, u8 command, int size,
1268 union i2c_smbus_data * data)
1270 /* So we need to generate a series of msgs. In the case of writing, we
1271 need to use only one message; when reading, we need two. We initialize
1272 most things with sane defaults, to keep the code below somewhat
1274 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1275 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1276 int num = read_write == I2C_SMBUS_READ?2:1;
1277 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1278 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1283 msgbuf0[0] = command;
1285 case I2C_SMBUS_QUICK:
1287 /* Special case: The read/write field is used as data */
1288 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1291 case I2C_SMBUS_BYTE:
1292 if (read_write == I2C_SMBUS_READ) {
1293 /* Special case: only a read! */
1294 msg[0].flags = I2C_M_RD | flags;
1298 case I2C_SMBUS_BYTE_DATA:
1299 if (read_write == I2C_SMBUS_READ)
1303 msgbuf0[1] = data->byte;
1306 case I2C_SMBUS_WORD_DATA:
1307 if (read_write == I2C_SMBUS_READ)
1311 msgbuf0[1] = data->word & 0xff;
1312 msgbuf0[2] = data->word >> 8;
1315 case I2C_SMBUS_PROC_CALL:
1316 num = 2; /* Special case */
1317 read_write = I2C_SMBUS_READ;
1320 msgbuf0[1] = data->word & 0xff;
1321 msgbuf0[2] = data->word >> 8;
1323 case I2C_SMBUS_BLOCK_DATA:
1324 if (read_write == I2C_SMBUS_READ) {
1325 msg[1].flags |= I2C_M_RECV_LEN;
1326 msg[1].len = 1; /* block length will be added by
1327 the underlying bus driver */
1329 msg[0].len = data->block[0] + 2;
1330 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1331 dev_err(&adapter->dev, "smbus_access called with "
1332 "invalid block write size (%d)\n",
1336 for (i = 1; i < msg[0].len; i++)
1337 msgbuf0[i] = data->block[i-1];
1340 case I2C_SMBUS_BLOCK_PROC_CALL:
1341 num = 2; /* Another special case */
1342 read_write = I2C_SMBUS_READ;
1343 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1344 dev_err(&adapter->dev, "%s called with invalid "
1345 "block proc call size (%d)\n", __FUNCTION__,
1349 msg[0].len = data->block[0] + 2;
1350 for (i = 1; i < msg[0].len; i++)
1351 msgbuf0[i] = data->block[i-1];
1352 msg[1].flags |= I2C_M_RECV_LEN;
1353 msg[1].len = 1; /* block length will be added by
1354 the underlying bus driver */
1356 case I2C_SMBUS_I2C_BLOCK_DATA:
1357 if (read_write == I2C_SMBUS_READ) {
1358 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1360 msg[0].len = data->block[0] + 1;
1361 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1362 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1363 "invalid block write size (%d)\n",
1367 for (i = 1; i <= data->block[0]; i++)
1368 msgbuf0[i] = data->block[i];
1372 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1377 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1378 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1380 /* Compute PEC if first message is a write */
1381 if (!(msg[0].flags & I2C_M_RD)) {
1382 if (num == 1) /* Write only */
1383 i2c_smbus_add_pec(&msg[0]);
1384 else /* Write followed by read */
1385 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1387 /* Ask for PEC if last message is a read */
1388 if (msg[num-1].flags & I2C_M_RD)
1392 if (i2c_transfer(adapter, msg, num) < 0)
1395 /* Check PEC if last message is a read */
1396 if (i && (msg[num-1].flags & I2C_M_RD)) {
1397 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1401 if (read_write == I2C_SMBUS_READ)
1403 case I2C_SMBUS_BYTE:
1404 data->byte = msgbuf0[0];
1406 case I2C_SMBUS_BYTE_DATA:
1407 data->byte = msgbuf1[0];
1409 case I2C_SMBUS_WORD_DATA:
1410 case I2C_SMBUS_PROC_CALL:
1411 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1413 case I2C_SMBUS_I2C_BLOCK_DATA:
1414 /* fixed at 32 for now */
1415 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1416 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1417 data->block[i+1] = msgbuf1[i];
1419 case I2C_SMBUS_BLOCK_DATA:
1420 case I2C_SMBUS_BLOCK_PROC_CALL:
1421 for (i = 0; i < msgbuf1[0] + 1; i++)
1422 data->block[i] = msgbuf1[i];
1429 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1430 char read_write, u8 command, int size,
1431 union i2c_smbus_data * data)
1435 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1437 if (adapter->algo->smbus_xfer) {
1438 mutex_lock(&adapter->bus_lock);
1439 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1441 mutex_unlock(&adapter->bus_lock);
1443 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1450 /* Next three are needed by i2c-isa */
1451 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1452 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1453 EXPORT_SYMBOL_GPL(i2c_bus_type);
1455 EXPORT_SYMBOL(i2c_del_adapter);
1456 EXPORT_SYMBOL(i2c_del_driver);
1457 EXPORT_SYMBOL(i2c_attach_client);
1458 EXPORT_SYMBOL(i2c_detach_client);
1459 EXPORT_SYMBOL(i2c_use_client);
1460 EXPORT_SYMBOL(i2c_release_client);
1461 EXPORT_SYMBOL(i2c_clients_command);
1462 EXPORT_SYMBOL(i2c_check_addr);
1464 EXPORT_SYMBOL(i2c_master_send);
1465 EXPORT_SYMBOL(i2c_master_recv);
1466 EXPORT_SYMBOL(i2c_control);
1467 EXPORT_SYMBOL(i2c_transfer);
1468 EXPORT_SYMBOL(i2c_get_adapter);
1469 EXPORT_SYMBOL(i2c_put_adapter);
1470 EXPORT_SYMBOL(i2c_probe);
1472 EXPORT_SYMBOL(i2c_smbus_xfer);
1473 EXPORT_SYMBOL(i2c_smbus_write_quick);
1474 EXPORT_SYMBOL(i2c_smbus_read_byte);
1475 EXPORT_SYMBOL(i2c_smbus_write_byte);
1476 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1477 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1478 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1479 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1480 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1481 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1482 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1484 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1485 MODULE_DESCRIPTION("I2C-Bus main module");
1486 MODULE_LICENSE("GPL");