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飉ti M鄟kki <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,
204 EXPORT_SYMBOL_GPL(i2c_bus_type);
207 * i2c_new_device - instantiate an i2c device for use with a new style driver
208 * @adap: the adapter managing the device
209 * @info: describes one I2C device; bus_num is ignored
211 * Create a device to work with a new style i2c driver, where binding is
212 * handled through driver model probe()/remove() methods. This call is not
213 * appropriate for use by mainboad initialization logic, which usually runs
214 * during an arch_initcall() long before any i2c_adapter could exist.
216 * This returns the new i2c client, which may be saved for later use with
217 * i2c_unregister_device(); or NULL to indicate an error.
220 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
222 struct i2c_client *client;
225 client = kzalloc(sizeof *client, GFP_KERNEL);
229 client->adapter = adap;
231 client->dev.platform_data = info->platform_data;
232 client->flags = info->flags;
233 client->addr = info->addr;
234 client->irq = info->irq;
236 strlcpy(client->driver_name, info->driver_name,
237 sizeof(client->driver_name));
238 strlcpy(client->name, info->type, sizeof(client->name));
240 /* a new style driver may be bound to this device when we
241 * return from this function, or any later moment (e.g. maybe
242 * hotplugging will load the driver module). and the device
243 * refcount model is the standard driver model one.
245 status = i2c_attach_client(client);
252 EXPORT_SYMBOL_GPL(i2c_new_device);
256 * i2c_unregister_device - reverse effect of i2c_new_device()
257 * @client: value returned from i2c_new_device()
259 void i2c_unregister_device(struct i2c_client *client)
261 struct i2c_adapter *adapter = client->adapter;
262 struct i2c_driver *driver = client->driver;
264 if (driver && !is_newstyle_driver(driver)) {
265 dev_err(&client->dev, "can't unregister devices "
266 "with legacy drivers\n");
271 mutex_lock(&adapter->clist_lock);
272 list_del(&client->list);
273 mutex_unlock(&adapter->clist_lock);
275 device_unregister(&client->dev);
277 EXPORT_SYMBOL_GPL(i2c_unregister_device);
280 /* ------------------------------------------------------------------------- */
282 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
284 void i2c_adapter_dev_release(struct device *dev)
286 struct i2c_adapter *adap = to_i2c_adapter(dev);
287 complete(&adap->dev_released);
291 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
293 struct i2c_adapter *adap = to_i2c_adapter(dev);
294 return sprintf(buf, "%s\n", adap->name);
297 static struct device_attribute i2c_adapter_attrs[] = {
298 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
302 struct class i2c_adapter_class = {
303 .owner = THIS_MODULE,
304 .name = "i2c-adapter",
305 .dev_attrs = i2c_adapter_attrs,
308 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
310 struct i2c_devinfo *devinfo;
312 mutex_lock(&__i2c_board_lock);
313 list_for_each_entry(devinfo, &__i2c_board_list, list) {
314 if (devinfo->busnum == adapter->nr
315 && !i2c_new_device(adapter,
316 &devinfo->board_info))
317 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
318 i2c_adapter_id(adapter),
319 devinfo->board_info.addr);
321 mutex_unlock(&__i2c_board_lock);
324 static int i2c_register_adapter(struct i2c_adapter *adap)
327 struct list_head *item;
328 struct i2c_driver *driver;
330 mutex_init(&adap->bus_lock);
331 mutex_init(&adap->clist_lock);
332 INIT_LIST_HEAD(&adap->clients);
334 mutex_lock(&core_lists);
335 list_add_tail(&adap->list, &adapters);
337 /* Add the adapter to the driver core.
338 * If the parent pointer is not set up,
339 * we add this adapter to the host bus.
341 if (adap->dev.parent == NULL) {
342 adap->dev.parent = &platform_bus;
343 pr_debug("I2C adapter driver [%s] forgot to specify "
344 "physical device\n", adap->name);
346 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
347 adap->dev.release = &i2c_adapter_dev_release;
348 adap->dev.class = &i2c_adapter_class;
349 res = device_register(&adap->dev);
353 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
355 /* create pre-declared device nodes for new-style drivers */
356 if (adap->nr < __i2c_first_dynamic_bus_num)
357 i2c_scan_static_board_info(adap);
359 /* let legacy drivers scan this bus for matching devices */
360 list_for_each(item,&drivers) {
361 driver = list_entry(item, struct i2c_driver, list);
362 if (driver->attach_adapter)
363 /* We ignore the return code; if it fails, too bad */
364 driver->attach_adapter(adap);
368 mutex_unlock(&core_lists);
372 list_del(&adap->list);
373 idr_remove(&i2c_adapter_idr, adap->nr);
378 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
379 * @adapter: the adapter to add
381 * This routine is used to declare an I2C adapter when its bus number
382 * doesn't matter. Examples: for I2C adapters dynamically added by
383 * USB links or PCI plugin cards.
385 * When this returns zero, a new bus number was allocated and stored
386 * in adap->nr, and the specified adapter became available for clients.
387 * Otherwise, a negative errno value is returned.
389 int i2c_add_adapter(struct i2c_adapter *adapter)
394 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
397 mutex_lock(&core_lists);
398 /* "above" here means "above or equal to", sigh */
399 res = idr_get_new_above(&i2c_adapter_idr, adapter,
400 __i2c_first_dynamic_bus_num, &id);
401 mutex_unlock(&core_lists);
410 return i2c_register_adapter(adapter);
412 EXPORT_SYMBOL(i2c_add_adapter);
415 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
416 * @adap: the adapter to register (with adap->nr initialized)
418 * This routine is used to declare an I2C adapter when its bus number
419 * matters. Example: for I2C adapters from system-on-chip CPUs, or
420 * otherwise built in to the system's mainboard, and where i2c_board_info
421 * is used to properly configure I2C devices.
423 * If no devices have pre-been declared for this bus, then be sure to
424 * register the adapter before any dynamically allocated ones. Otherwise
425 * the required bus ID may not be available.
427 * When this returns zero, the specified adapter became available for
428 * clients using the bus number provided in adap->nr. Also, the table
429 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
430 * and the appropriate driver model device nodes are created. Otherwise, a
431 * negative errno value is returned.
433 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
438 if (adap->nr & ~MAX_ID_MASK)
442 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
445 mutex_lock(&core_lists);
446 /* "above" here means "above or equal to", sigh;
447 * we need the "equal to" result to force the result
449 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
450 if (status == 0 && id != adap->nr) {
452 idr_remove(&i2c_adapter_idr, id);
454 mutex_unlock(&core_lists);
455 if (status == -EAGAIN)
459 status = i2c_register_adapter(adap);
462 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
464 int i2c_del_adapter(struct i2c_adapter *adap)
466 struct list_head *item, *_n;
467 struct i2c_adapter *adap_from_list;
468 struct i2c_driver *driver;
469 struct i2c_client *client;
472 mutex_lock(&core_lists);
474 /* First make sure that this adapter was ever added */
475 list_for_each_entry(adap_from_list, &adapters, list) {
476 if (adap_from_list == adap)
479 if (adap_from_list != adap) {
480 pr_debug("i2c-core: attempting to delete unregistered "
481 "adapter [%s]\n", adap->name);
486 list_for_each(item,&drivers) {
487 driver = list_entry(item, struct i2c_driver, list);
488 if (driver->detach_adapter)
489 if ((res = driver->detach_adapter(adap))) {
490 dev_err(&adap->dev, "detach_adapter failed "
492 driver->driver.name);
497 /* detach any active clients. This must be done first, because
498 * it can fail; in which case we give up. */
499 list_for_each_safe(item, _n, &adap->clients) {
500 struct i2c_driver *driver;
502 client = list_entry(item, struct i2c_client, list);
503 driver = client->driver;
505 /* new style, follow standard driver model */
506 if (!driver || is_newstyle_driver(driver)) {
507 i2c_unregister_device(client);
511 /* legacy drivers create and remove clients themselves */
512 if ((res = driver->detach_client(client))) {
513 dev_err(&adap->dev, "detach_client failed for client "
514 "[%s] at address 0x%02x\n", client->name,
520 /* clean up the sysfs representation */
521 init_completion(&adap->dev_released);
522 device_unregister(&adap->dev);
523 list_del(&adap->list);
525 /* wait for sysfs to drop all references */
526 wait_for_completion(&adap->dev_released);
529 idr_remove(&i2c_adapter_idr, adap->nr);
531 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
534 mutex_unlock(&core_lists);
537 EXPORT_SYMBOL(i2c_del_adapter);
540 /* ------------------------------------------------------------------------- */
543 * An i2c_driver is used with one or more i2c_client (device) nodes to access
544 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
545 * are two models for binding the driver to its device: "new style" drivers
546 * follow the standard Linux driver model and just respond to probe() calls
547 * issued if the driver core sees they match(); "legacy" drivers create device
551 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
555 /* new style driver methods can't mix with legacy ones */
556 if (is_newstyle_driver(driver)) {
557 if (driver->attach_adapter || driver->detach_adapter
558 || driver->detach_client) {
560 "i2c-core: driver [%s] is confused\n",
561 driver->driver.name);
566 /* add the driver to the list of i2c drivers in the driver core */
567 driver->driver.owner = owner;
568 driver->driver.bus = &i2c_bus_type;
570 /* for new style drivers, when registration returns the driver core
571 * will have called probe() for all matching-but-unbound devices.
573 res = driver_register(&driver->driver);
577 mutex_lock(&core_lists);
579 list_add_tail(&driver->list,&drivers);
580 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
582 /* legacy drivers scan i2c busses directly */
583 if (driver->attach_adapter) {
584 struct i2c_adapter *adapter;
586 list_for_each_entry(adapter, &adapters, list) {
587 driver->attach_adapter(adapter);
591 mutex_unlock(&core_lists);
594 EXPORT_SYMBOL(i2c_register_driver);
597 * i2c_del_driver - unregister I2C driver
598 * @driver: the driver being unregistered
600 void i2c_del_driver(struct i2c_driver *driver)
602 struct list_head *item1, *item2, *_n;
603 struct i2c_client *client;
604 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 (driver->detach_adapter(adap)) {
620 dev_err(&adap->dev, "detach_adapter failed "
622 driver->driver.name);
625 list_for_each_safe(item2, _n, &adap->clients) {
626 client = list_entry(item2, struct i2c_client, list);
627 if (client->driver != driver)
629 dev_dbg(&adap->dev, "detaching client [%s] "
630 "at 0x%02x\n", client->name,
632 if (driver->detach_client(client)) {
633 dev_err(&adap->dev, "detach_client "
634 "failed for client [%s] at "
635 "0x%02x\n", client->name,
643 driver_unregister(&driver->driver);
644 list_del(&driver->list);
645 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
647 mutex_unlock(&core_lists);
649 EXPORT_SYMBOL(i2c_del_driver);
651 /* ------------------------------------------------------------------------- */
653 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
655 struct list_head *item;
656 struct i2c_client *client;
658 list_for_each(item,&adapter->clients) {
659 client = list_entry(item, struct i2c_client, list);
660 if (client->addr == addr)
666 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
670 mutex_lock(&adapter->clist_lock);
671 rval = __i2c_check_addr(adapter, addr);
672 mutex_unlock(&adapter->clist_lock);
676 EXPORT_SYMBOL(i2c_check_addr);
678 int i2c_attach_client(struct i2c_client *client)
680 struct i2c_adapter *adapter = client->adapter;
683 mutex_lock(&adapter->clist_lock);
684 if (__i2c_check_addr(client->adapter, client->addr)) {
688 list_add_tail(&client->list,&adapter->clients);
690 client->usage_count = 0;
692 client->dev.parent = &client->adapter->dev;
693 client->dev.bus = &i2c_bus_type;
696 client->dev.driver = &client->driver->driver;
698 if (client->driver && !is_newstyle_driver(client->driver)) {
699 client->dev.release = i2c_client_release;
700 client->dev.uevent_suppress = 1;
702 client->dev.release = i2c_client_dev_release;
704 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
705 "%d-%04x", i2c_adapter_id(adapter), client->addr);
706 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
707 client->name, client->dev.bus_id);
708 res = device_register(&client->dev);
711 mutex_unlock(&adapter->clist_lock);
713 if (adapter->client_register) {
714 if (adapter->client_register(client)) {
715 dev_dbg(&adapter->dev, "client_register "
716 "failed for client [%s] at 0x%02x\n",
717 client->name, client->addr);
724 list_del(&client->list);
725 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
726 "(%d)\n", client->name, client->addr, res);
728 mutex_unlock(&adapter->clist_lock);
731 EXPORT_SYMBOL(i2c_attach_client);
733 int i2c_detach_client(struct i2c_client *client)
735 struct i2c_adapter *adapter = client->adapter;
738 if (client->usage_count > 0) {
739 dev_warn(&client->dev, "Client [%s] still busy, "
740 "can't detach\n", client->name);
744 if (adapter->client_unregister) {
745 res = adapter->client_unregister(client);
747 dev_err(&client->dev,
748 "client_unregister [%s] failed, "
749 "client not detached\n", client->name);
754 mutex_lock(&adapter->clist_lock);
755 list_del(&client->list);
756 init_completion(&client->released);
757 device_unregister(&client->dev);
758 mutex_unlock(&adapter->clist_lock);
759 wait_for_completion(&client->released);
764 EXPORT_SYMBOL(i2c_detach_client);
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++;
797 EXPORT_SYMBOL(i2c_use_client);
799 int i2c_release_client(struct i2c_client *client)
801 if (!client->usage_count) {
802 pr_debug("i2c-core: %s used one too many times\n",
807 client->usage_count--;
808 i2c_dec_use_client(client);
812 EXPORT_SYMBOL(i2c_release_client);
814 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
816 struct list_head *item;
817 struct i2c_client *client;
819 mutex_lock(&adap->clist_lock);
820 list_for_each(item,&adap->clients) {
821 client = list_entry(item, struct i2c_client, list);
822 if (!try_module_get(client->driver->driver.owner))
824 if (NULL != client->driver->command) {
825 mutex_unlock(&adap->clist_lock);
826 client->driver->command(client,cmd,arg);
827 mutex_lock(&adap->clist_lock);
829 module_put(client->driver->driver.owner);
831 mutex_unlock(&adap->clist_lock);
833 EXPORT_SYMBOL(i2c_clients_command);
835 static int __init i2c_init(void)
839 retval = bus_register(&i2c_bus_type);
842 return class_register(&i2c_adapter_class);
845 static void __exit i2c_exit(void)
847 class_unregister(&i2c_adapter_class);
848 bus_unregister(&i2c_bus_type);
851 subsys_initcall(i2c_init);
852 module_exit(i2c_exit);
854 /* ----------------------------------------------------
855 * the functional interface to the i2c busses.
856 * ----------------------------------------------------
859 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
863 if (adap->algo->master_xfer) {
865 for (ret = 0; ret < num; ret++) {
866 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
867 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
868 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
869 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
873 mutex_lock_nested(&adap->bus_lock, adap->level);
874 ret = adap->algo->master_xfer(adap,msgs,num);
875 mutex_unlock(&adap->bus_lock);
879 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
883 EXPORT_SYMBOL(i2c_transfer);
885 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
888 struct i2c_adapter *adap=client->adapter;
891 msg.addr = client->addr;
892 msg.flags = client->flags & I2C_M_TEN;
894 msg.buf = (char *)buf;
896 ret = i2c_transfer(adap, &msg, 1);
898 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
899 transmitted, else error code. */
900 return (ret == 1) ? count : ret;
902 EXPORT_SYMBOL(i2c_master_send);
904 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
906 struct i2c_adapter *adap=client->adapter;
910 msg.addr = client->addr;
911 msg.flags = client->flags & I2C_M_TEN;
912 msg.flags |= I2C_M_RD;
916 ret = i2c_transfer(adap, &msg, 1);
918 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
919 transmitted, else error code. */
920 return (ret == 1) ? count : ret;
922 EXPORT_SYMBOL(i2c_master_recv);
924 int i2c_control(struct i2c_client *client,
925 unsigned int cmd, unsigned long arg)
928 struct i2c_adapter *adap = client->adapter;
930 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
939 if (adap->algo->algo_control!=NULL)
940 ret = adap->algo->algo_control(adap,cmd,arg);
944 EXPORT_SYMBOL(i2c_control);
946 /* ----------------------------------------------------
947 * the i2c address scanning function
948 * Will not work for 10-bit addresses!
949 * ----------------------------------------------------
951 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
952 int (*found_proc) (struct i2c_adapter *, int, int))
956 /* Make sure the address is valid */
957 if (addr < 0x03 || addr > 0x77) {
958 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
963 /* Skip if already in use */
964 if (i2c_check_addr(adapter, addr))
967 /* Make sure there is something at this address, unless forced */
969 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
970 I2C_SMBUS_QUICK, NULL) < 0)
973 /* prevent 24RF08 corruption */
974 if ((addr & ~0x0f) == 0x50)
975 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
976 I2C_SMBUS_QUICK, NULL);
979 /* Finally call the custom detection function */
980 err = found_proc(adapter, addr, kind);
981 /* -ENODEV can be returned if there is a chip at the given address
982 but it isn't supported by this chip driver. We catch it here as
983 this isn't an error. */
988 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
993 int i2c_probe(struct i2c_adapter *adapter,
994 struct i2c_client_address_data *address_data,
995 int (*found_proc) (struct i2c_adapter *, int, int))
998 int adap_id = i2c_adapter_id(adapter);
1000 /* Force entries are done first, and are not affected by ignore
1002 if (address_data->forces) {
1003 unsigned short **forces = address_data->forces;
1006 for (kind = 0; forces[kind]; kind++) {
1007 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1009 if (forces[kind][i] == adap_id
1010 || forces[kind][i] == ANY_I2C_BUS) {
1011 dev_dbg(&adapter->dev, "found force "
1012 "parameter for adapter %d, "
1013 "addr 0x%02x, kind %d\n",
1014 adap_id, forces[kind][i + 1],
1016 err = i2c_probe_address(adapter,
1017 forces[kind][i + 1],
1026 /* Stop here if we can't use SMBUS_QUICK */
1027 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1028 if (address_data->probe[0] == I2C_CLIENT_END
1029 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1032 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1033 "can't probe for chips\n");
1037 /* Probe entries are done second, and are not affected by ignore
1039 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1040 if (address_data->probe[i] == adap_id
1041 || address_data->probe[i] == ANY_I2C_BUS) {
1042 dev_dbg(&adapter->dev, "found probe parameter for "
1043 "adapter %d, addr 0x%02x\n", adap_id,
1044 address_data->probe[i + 1]);
1045 err = i2c_probe_address(adapter,
1046 address_data->probe[i + 1],
1053 /* Normal entries are done last, unless shadowed by an ignore entry */
1054 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1058 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1060 if ((address_data->ignore[j] == adap_id ||
1061 address_data->ignore[j] == ANY_I2C_BUS)
1062 && address_data->ignore[j + 1]
1063 == address_data->normal_i2c[i]) {
1064 dev_dbg(&adapter->dev, "found ignore "
1065 "parameter for adapter %d, "
1066 "addr 0x%02x\n", adap_id,
1067 address_data->ignore[j + 1]);
1075 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1076 "addr 0x%02x\n", adap_id,
1077 address_data->normal_i2c[i]);
1078 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1086 EXPORT_SYMBOL(i2c_probe);
1089 i2c_new_probed_device(struct i2c_adapter *adap,
1090 struct i2c_board_info *info,
1091 unsigned short const *addr_list)
1095 /* Stop here if the bus doesn't support probing */
1096 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1097 dev_err(&adap->dev, "Probing not supported\n");
1101 mutex_lock(&adap->clist_lock);
1102 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1103 /* Check address validity */
1104 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1105 dev_warn(&adap->dev, "Invalid 7-bit address "
1106 "0x%02x\n", addr_list[i]);
1110 /* Check address availability */
1111 if (__i2c_check_addr(adap, addr_list[i])) {
1112 dev_dbg(&adap->dev, "Address 0x%02x already in "
1113 "use, not probing\n", addr_list[i]);
1117 /* Test address responsiveness
1118 The default probe method is a quick write, but it is known
1119 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1120 and could also irreversibly write-protect some EEPROMs, so
1121 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1122 read instead. Also, some bus drivers don't implement
1123 quick write, so we fallback to a byte read it that case
1125 if ((addr_list[i] & ~0x07) == 0x30
1126 || (addr_list[i] & ~0x0f) == 0x50
1127 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1128 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1130 I2C_SMBUS_BYTE, NULL) >= 0)
1133 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1135 I2C_SMBUS_QUICK, NULL) >= 0)
1139 mutex_unlock(&adap->clist_lock);
1141 if (addr_list[i] == I2C_CLIENT_END) {
1142 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1146 info->addr = addr_list[i];
1147 return i2c_new_device(adap, info);
1149 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1151 struct i2c_adapter* i2c_get_adapter(int id)
1153 struct i2c_adapter *adapter;
1155 mutex_lock(&core_lists);
1156 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1157 if (adapter && !try_module_get(adapter->owner))
1160 mutex_unlock(&core_lists);
1163 EXPORT_SYMBOL(i2c_get_adapter);
1165 void i2c_put_adapter(struct i2c_adapter *adap)
1167 module_put(adap->owner);
1169 EXPORT_SYMBOL(i2c_put_adapter);
1171 /* The SMBus parts */
1173 #define POLY (0x1070U << 3)
1179 for(i = 0; i < 8; i++) {
1184 return (u8)(data >> 8);
1187 /* Incremental CRC8 over count bytes in the array pointed to by p */
1188 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1192 for(i = 0; i < count; i++)
1193 crc = crc8((crc ^ p[i]) << 8);
1197 /* Assume a 7-bit address, which is reasonable for SMBus */
1198 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1200 /* The address will be sent first */
1201 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1202 pec = i2c_smbus_pec(pec, &addr, 1);
1204 /* The data buffer follows */
1205 return i2c_smbus_pec(pec, msg->buf, msg->len);
1208 /* Used for write only transactions */
1209 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1211 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1215 /* Return <0 on CRC error
1216 If there was a write before this read (most cases) we need to take the
1217 partial CRC from the write part into account.
1218 Note that this function does modify the message (we need to decrease the
1219 message length to hide the CRC byte from the caller). */
1220 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1222 u8 rpec = msg->buf[--msg->len];
1223 cpec = i2c_smbus_msg_pec(cpec, msg);
1226 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1233 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1235 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1236 value,0,I2C_SMBUS_QUICK,NULL);
1238 EXPORT_SYMBOL(i2c_smbus_write_quick);
1240 s32 i2c_smbus_read_byte(struct i2c_client *client)
1242 union i2c_smbus_data data;
1243 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1244 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1249 EXPORT_SYMBOL(i2c_smbus_read_byte);
1251 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1253 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1254 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1256 EXPORT_SYMBOL(i2c_smbus_write_byte);
1258 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1260 union i2c_smbus_data data;
1261 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1262 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1267 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1269 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1271 union i2c_smbus_data data;
1273 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1274 I2C_SMBUS_WRITE,command,
1275 I2C_SMBUS_BYTE_DATA,&data);
1277 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1279 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1281 union i2c_smbus_data data;
1282 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1283 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1288 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1290 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1292 union i2c_smbus_data data;
1294 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1295 I2C_SMBUS_WRITE,command,
1296 I2C_SMBUS_WORD_DATA,&data);
1298 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1300 /* Returns the number of read bytes */
1301 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1304 union i2c_smbus_data data;
1306 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1307 I2C_SMBUS_READ, command,
1308 I2C_SMBUS_BLOCK_DATA, &data))
1311 memcpy(values, &data.block[1], data.block[0]);
1312 return data.block[0];
1314 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1316 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1317 u8 length, const u8 *values)
1319 union i2c_smbus_data data;
1321 if (length > I2C_SMBUS_BLOCK_MAX)
1322 length = I2C_SMBUS_BLOCK_MAX;
1323 data.block[0] = length;
1324 memcpy(&data.block[1], values, length);
1325 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1326 I2C_SMBUS_WRITE,command,
1327 I2C_SMBUS_BLOCK_DATA,&data);
1329 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1331 /* Returns the number of read bytes */
1332 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1334 union i2c_smbus_data data;
1336 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1337 I2C_SMBUS_READ,command,
1338 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1341 memcpy(values, &data.block[1], data.block[0]);
1342 return data.block[0];
1344 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1346 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1347 u8 length, const u8 *values)
1349 union i2c_smbus_data data;
1351 if (length > I2C_SMBUS_BLOCK_MAX)
1352 length = I2C_SMBUS_BLOCK_MAX;
1353 data.block[0] = length;
1354 memcpy(data.block + 1, values, length);
1355 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1356 I2C_SMBUS_WRITE, command,
1357 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1359 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1361 /* Simulate a SMBus command using the i2c protocol
1362 No checking of parameters is done! */
1363 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1364 unsigned short flags,
1365 char read_write, u8 command, int size,
1366 union i2c_smbus_data * data)
1368 /* So we need to generate a series of msgs. In the case of writing, we
1369 need to use only one message; when reading, we need two. We initialize
1370 most things with sane defaults, to keep the code below somewhat
1372 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1373 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1374 int num = read_write == I2C_SMBUS_READ?2:1;
1375 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1376 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1381 msgbuf0[0] = command;
1383 case I2C_SMBUS_QUICK:
1385 /* Special case: The read/write field is used as data */
1386 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1389 case I2C_SMBUS_BYTE:
1390 if (read_write == I2C_SMBUS_READ) {
1391 /* Special case: only a read! */
1392 msg[0].flags = I2C_M_RD | flags;
1396 case I2C_SMBUS_BYTE_DATA:
1397 if (read_write == I2C_SMBUS_READ)
1401 msgbuf0[1] = data->byte;
1404 case I2C_SMBUS_WORD_DATA:
1405 if (read_write == I2C_SMBUS_READ)
1409 msgbuf0[1] = data->word & 0xff;
1410 msgbuf0[2] = data->word >> 8;
1413 case I2C_SMBUS_PROC_CALL:
1414 num = 2; /* Special case */
1415 read_write = I2C_SMBUS_READ;
1418 msgbuf0[1] = data->word & 0xff;
1419 msgbuf0[2] = data->word >> 8;
1421 case I2C_SMBUS_BLOCK_DATA:
1422 if (read_write == I2C_SMBUS_READ) {
1423 msg[1].flags |= I2C_M_RECV_LEN;
1424 msg[1].len = 1; /* block length will be added by
1425 the underlying bus driver */
1427 msg[0].len = data->block[0] + 2;
1428 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1429 dev_err(&adapter->dev, "smbus_access called with "
1430 "invalid block write size (%d)\n",
1434 for (i = 1; i < msg[0].len; i++)
1435 msgbuf0[i] = data->block[i-1];
1438 case I2C_SMBUS_BLOCK_PROC_CALL:
1439 num = 2; /* Another special case */
1440 read_write = I2C_SMBUS_READ;
1441 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1442 dev_err(&adapter->dev, "%s called with invalid "
1443 "block proc call size (%d)\n", __FUNCTION__,
1447 msg[0].len = data->block[0] + 2;
1448 for (i = 1; i < msg[0].len; i++)
1449 msgbuf0[i] = data->block[i-1];
1450 msg[1].flags |= I2C_M_RECV_LEN;
1451 msg[1].len = 1; /* block length will be added by
1452 the underlying bus driver */
1454 case I2C_SMBUS_I2C_BLOCK_DATA:
1455 if (read_write == I2C_SMBUS_READ) {
1456 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1458 msg[0].len = data->block[0] + 1;
1459 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1460 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1461 "invalid block write size (%d)\n",
1465 for (i = 1; i <= data->block[0]; i++)
1466 msgbuf0[i] = data->block[i];
1470 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1475 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1476 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1478 /* Compute PEC if first message is a write */
1479 if (!(msg[0].flags & I2C_M_RD)) {
1480 if (num == 1) /* Write only */
1481 i2c_smbus_add_pec(&msg[0]);
1482 else /* Write followed by read */
1483 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1485 /* Ask for PEC if last message is a read */
1486 if (msg[num-1].flags & I2C_M_RD)
1490 if (i2c_transfer(adapter, msg, num) < 0)
1493 /* Check PEC if last message is a read */
1494 if (i && (msg[num-1].flags & I2C_M_RD)) {
1495 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1499 if (read_write == I2C_SMBUS_READ)
1501 case I2C_SMBUS_BYTE:
1502 data->byte = msgbuf0[0];
1504 case I2C_SMBUS_BYTE_DATA:
1505 data->byte = msgbuf1[0];
1507 case I2C_SMBUS_WORD_DATA:
1508 case I2C_SMBUS_PROC_CALL:
1509 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1511 case I2C_SMBUS_I2C_BLOCK_DATA:
1512 /* fixed at 32 for now */
1513 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1514 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1515 data->block[i+1] = msgbuf1[i];
1517 case I2C_SMBUS_BLOCK_DATA:
1518 case I2C_SMBUS_BLOCK_PROC_CALL:
1519 for (i = 0; i < msgbuf1[0] + 1; i++)
1520 data->block[i] = msgbuf1[i];
1527 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1528 char read_write, u8 command, int size,
1529 union i2c_smbus_data * data)
1533 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1535 if (adapter->algo->smbus_xfer) {
1536 mutex_lock(&adapter->bus_lock);
1537 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1539 mutex_unlock(&adapter->bus_lock);
1541 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1546 EXPORT_SYMBOL(i2c_smbus_xfer);
1548 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1549 MODULE_DESCRIPTION("I2C-Bus main module");
1550 MODULE_LICENSE("GPL");