aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/i2c-core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/i2c-core.c')
-rw-r--r--drivers/i2c/i2c-core.c575
1 files changed, 448 insertions, 127 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index d0175f4f8fc6..0a79f7661017 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -29,13 +29,11 @@
29#include <linux/i2c.h> 29#include <linux/i2c.h>
30#include <linux/init.h> 30#include <linux/init.h>
31#include <linux/idr.h> 31#include <linux/idr.h>
32#include <linux/seq_file.h>
33#include <linux/platform_device.h> 32#include <linux/platform_device.h>
34#include <linux/mutex.h> 33#include <linux/mutex.h>
35#include <linux/completion.h> 34#include <linux/completion.h>
36#include <linux/hardirq.h> 35#include <linux/hardirq.h>
37#include <linux/irqflags.h> 36#include <linux/irqflags.h>
38#include <linux/semaphore.h>
39#include <asm/uaccess.h> 37#include <asm/uaccess.h>
40 38
41#include "i2c-core.h" 39#include "i2c-core.h"
@@ -44,7 +42,9 @@
44static DEFINE_MUTEX(core_lock); 42static DEFINE_MUTEX(core_lock);
45static DEFINE_IDR(i2c_adapter_idr); 43static DEFINE_IDR(i2c_adapter_idr);
46 44
47#define is_newstyle_driver(d) ((d)->probe || (d)->remove) 45#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
46
47static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
48 48
49/* ------------------------------------------------------------------------- */ 49/* ------------------------------------------------------------------------- */
50 50
@@ -103,19 +103,14 @@ static int i2c_device_probe(struct device *dev)
103{ 103{
104 struct i2c_client *client = to_i2c_client(dev); 104 struct i2c_client *client = to_i2c_client(dev);
105 struct i2c_driver *driver = to_i2c_driver(dev->driver); 105 struct i2c_driver *driver = to_i2c_driver(dev->driver);
106 const struct i2c_device_id *id;
107 int status; 106 int status;
108 107
109 if (!driver->probe) 108 if (!driver->probe || !driver->id_table)
110 return -ENODEV; 109 return -ENODEV;
111 client->driver = driver; 110 client->driver = driver;
112 dev_dbg(dev, "probe\n"); 111 dev_dbg(dev, "probe\n");
113 112
114 if (driver->id_table) 113 status = driver->probe(client, i2c_match_id(driver->id_table, client));
115 id = i2c_match_id(driver->id_table, client);
116 else
117 id = NULL;
118 status = driver->probe(client, id);
119 if (status) 114 if (status)
120 client->driver = NULL; 115 client->driver = NULL;
121 return status; 116 return status;
@@ -208,7 +203,7 @@ static struct device_attribute i2c_dev_attrs[] = {
208 { }, 203 { },
209}; 204};
210 205
211static struct bus_type i2c_bus_type = { 206struct bus_type i2c_bus_type = {
212 .name = "i2c", 207 .name = "i2c",
213 .dev_attrs = i2c_dev_attrs, 208 .dev_attrs = i2c_dev_attrs,
214 .match = i2c_device_match, 209 .match = i2c_device_match,
@@ -219,6 +214,7 @@ static struct bus_type i2c_bus_type = {
219 .suspend = i2c_device_suspend, 214 .suspend = i2c_device_suspend,
220 .resume = i2c_device_resume, 215 .resume = i2c_device_resume,
221}; 216};
217EXPORT_SYMBOL_GPL(i2c_bus_type);
222 218
223 219
224/** 220/**
@@ -306,6 +302,14 @@ void i2c_unregister_device(struct i2c_client *client)
306 return; 302 return;
307 } 303 }
308 304
305 if (adapter->client_unregister) {
306 if (adapter->client_unregister(client)) {
307 dev_warn(&client->dev,
308 "client_unregister [%s] failed\n",
309 client->name);
310 }
311 }
312
309 mutex_lock(&adapter->clist_lock); 313 mutex_lock(&adapter->clist_lock);
310 list_del(&client->list); 314 list_del(&client->list);
311 mutex_unlock(&adapter->clist_lock); 315 mutex_unlock(&adapter->clist_lock);
@@ -416,6 +420,10 @@ static int i2c_do_add_adapter(struct device_driver *d, void *data)
416 struct i2c_driver *driver = to_i2c_driver(d); 420 struct i2c_driver *driver = to_i2c_driver(d);
417 struct i2c_adapter *adap = data; 421 struct i2c_adapter *adap = data;
418 422
423 /* Detect supported devices on that bus, and instantiate them */
424 i2c_detect(adap, driver);
425
426 /* Let legacy drivers scan this bus for matching devices */
419 if (driver->attach_adapter) { 427 if (driver->attach_adapter) {
420 /* We ignore the return code; if it fails, too bad */ 428 /* We ignore the return code; if it fails, too bad */
421 driver->attach_adapter(adap); 429 driver->attach_adapter(adap);
@@ -455,7 +463,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
455 if (adap->nr < __i2c_first_dynamic_bus_num) 463 if (adap->nr < __i2c_first_dynamic_bus_num)
456 i2c_scan_static_board_info(adap); 464 i2c_scan_static_board_info(adap);
457 465
458 /* let legacy drivers scan this bus for matching devices */ 466 /* Notify drivers */
459 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, 467 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
460 i2c_do_add_adapter); 468 i2c_do_add_adapter);
461 469
@@ -561,8 +569,19 @@ static int i2c_do_del_adapter(struct device_driver *d, void *data)
561{ 569{
562 struct i2c_driver *driver = to_i2c_driver(d); 570 struct i2c_driver *driver = to_i2c_driver(d);
563 struct i2c_adapter *adapter = data; 571 struct i2c_adapter *adapter = data;
572 struct i2c_client *client, *_n;
564 int res; 573 int res;
565 574
575 /* Remove the devices we created ourselves */
576 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
577 if (client->adapter == adapter) {
578 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
579 client->name, client->addr);
580 list_del(&client->detected);
581 i2c_unregister_device(client);
582 }
583 }
584
566 if (!driver->detach_adapter) 585 if (!driver->detach_adapter)
567 return 0; 586 return 0;
568 res = driver->detach_adapter(adapter); 587 res = driver->detach_adapter(adapter);
@@ -582,8 +601,7 @@ static int i2c_do_del_adapter(struct device_driver *d, void *data)
582 */ 601 */
583int i2c_del_adapter(struct i2c_adapter *adap) 602int i2c_del_adapter(struct i2c_adapter *adap)
584{ 603{
585 struct list_head *item, *_n; 604 struct i2c_client *client, *_n;
586 struct i2c_client *client;
587 int res = 0; 605 int res = 0;
588 606
589 mutex_lock(&core_lock); 607 mutex_lock(&core_lock);
@@ -604,10 +622,9 @@ int i2c_del_adapter(struct i2c_adapter *adap)
604 622
605 /* detach any active clients. This must be done first, because 623 /* detach any active clients. This must be done first, because
606 * it can fail; in which case we give up. */ 624 * it can fail; in which case we give up. */
607 list_for_each_safe(item, _n, &adap->clients) { 625 list_for_each_entry_safe(client, _n, &adap->clients, list) {
608 struct i2c_driver *driver; 626 struct i2c_driver *driver;
609 627
610 client = list_entry(item, struct i2c_client, list);
611 driver = client->driver; 628 driver = client->driver;
612 629
613 /* new style, follow standard driver model */ 630 /* new style, follow standard driver model */
@@ -646,6 +663,20 @@ EXPORT_SYMBOL(i2c_del_adapter);
646 663
647/* ------------------------------------------------------------------------- */ 664/* ------------------------------------------------------------------------- */
648 665
666static int __attach_adapter(struct device *dev, void *data)
667{
668 struct i2c_adapter *adapter = to_i2c_adapter(dev);
669 struct i2c_driver *driver = data;
670
671 i2c_detect(adapter, driver);
672
673 /* Legacy drivers scan i2c busses directly */
674 if (driver->attach_adapter)
675 driver->attach_adapter(adapter);
676
677 return 0;
678}
679
649/* 680/*
650 * An i2c_driver is used with one or more i2c_client (device) nodes to access 681 * An i2c_driver is used with one or more i2c_client (device) nodes to access
651 * i2c slave chips, on a bus instance associated with some i2c_adapter. There 682 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
@@ -685,72 +716,70 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
685 716
686 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); 717 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
687 718
688 /* legacy drivers scan i2c busses directly */ 719 INIT_LIST_HEAD(&driver->clients);
689 if (driver->attach_adapter) { 720 /* Walk the adapters that are already present */
690 struct i2c_adapter *adapter; 721 class_for_each_device(&i2c_adapter_class, driver, __attach_adapter);
691
692 down(&i2c_adapter_class.sem);
693 list_for_each_entry(adapter, &i2c_adapter_class.devices,
694 dev.node) {
695 driver->attach_adapter(adapter);
696 }
697 up(&i2c_adapter_class.sem);
698 }
699 722
700 mutex_unlock(&core_lock); 723 mutex_unlock(&core_lock);
701 return 0; 724 return 0;
702} 725}
703EXPORT_SYMBOL(i2c_register_driver); 726EXPORT_SYMBOL(i2c_register_driver);
704 727
705/** 728static int __detach_adapter(struct device *dev, void *data)
706 * i2c_del_driver - unregister I2C driver
707 * @driver: the driver being unregistered
708 * Context: can sleep
709 */
710void i2c_del_driver(struct i2c_driver *driver)
711{ 729{
712 struct list_head *item2, *_n; 730 struct i2c_adapter *adapter = to_i2c_adapter(dev);
713 struct i2c_client *client; 731 struct i2c_driver *driver = data;
714 struct i2c_adapter *adap; 732 struct i2c_client *client, *_n;
715 733
716 mutex_lock(&core_lock); 734 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
735 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
736 client->name, client->addr);
737 list_del(&client->detected);
738 i2c_unregister_device(client);
739 }
717 740
718 /* new-style driver? */
719 if (is_newstyle_driver(driver)) 741 if (is_newstyle_driver(driver))
720 goto unregister; 742 return 0;
721 743
722 /* Have a look at each adapter, if clients of this driver are still 744 /* Have a look at each adapter, if clients of this driver are still
723 * attached. If so, detach them to be able to kill the driver 745 * attached. If so, detach them to be able to kill the driver
724 * afterwards. 746 * afterwards.
725 */ 747 */
726 down(&i2c_adapter_class.sem); 748 if (driver->detach_adapter) {
727 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) { 749 if (driver->detach_adapter(adapter))
728 if (driver->detach_adapter) { 750 dev_err(&adapter->dev,
729 if (driver->detach_adapter(adap)) { 751 "detach_adapter failed for driver [%s]\n",
730 dev_err(&adap->dev, "detach_adapter failed " 752 driver->driver.name);
731 "for driver [%s]\n", 753 } else {
732 driver->driver.name); 754 struct i2c_client *client, *_n;
733 } 755
734 } else { 756 list_for_each_entry_safe(client, _n, &adapter->clients, list) {
735 list_for_each_safe(item2, _n, &adap->clients) { 757 if (client->driver != driver)
736 client = list_entry(item2, struct i2c_client, list); 758 continue;
737 if (client->driver != driver) 759 dev_dbg(&adapter->dev,
738 continue; 760 "detaching client [%s] at 0x%02x\n",
739 dev_dbg(&adap->dev, "detaching client [%s] " 761 client->name, client->addr);
740 "at 0x%02x\n", client->name, 762 if (driver->detach_client(client))
741 client->addr); 763 dev_err(&adapter->dev, "detach_client "
742 if (driver->detach_client(client)) { 764 "failed for client [%s] at 0x%02x\n",
743 dev_err(&adap->dev, "detach_client " 765 client->name, client->addr);
744 "failed for client [%s] at "
745 "0x%02x\n", client->name,
746 client->addr);
747 }
748 }
749 } 766 }
750 } 767 }
751 up(&i2c_adapter_class.sem);
752 768
753 unregister: 769 return 0;
770}
771
772/**
773 * i2c_del_driver - unregister I2C driver
774 * @driver: the driver being unregistered
775 * Context: can sleep
776 */
777void i2c_del_driver(struct i2c_driver *driver)
778{
779 mutex_lock(&core_lock);
780
781 class_for_each_device(&i2c_adapter_class, driver, __detach_adapter);
782
754 driver_unregister(&driver->driver); 783 driver_unregister(&driver->driver);
755 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); 784 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
756 785
@@ -863,8 +892,9 @@ EXPORT_SYMBOL(i2c_detach_client);
863 */ 892 */
864struct i2c_client *i2c_use_client(struct i2c_client *client) 893struct i2c_client *i2c_use_client(struct i2c_client *client)
865{ 894{
866 get_device(&client->dev); 895 if (client && get_device(&client->dev))
867 return client; 896 return client;
897 return NULL;
868} 898}
869EXPORT_SYMBOL(i2c_use_client); 899EXPORT_SYMBOL(i2c_use_client);
870 900
@@ -876,7 +906,8 @@ EXPORT_SYMBOL(i2c_use_client);
876 */ 906 */
877void i2c_release_client(struct i2c_client *client) 907void i2c_release_client(struct i2c_client *client)
878{ 908{
879 put_device(&client->dev); 909 if (client)
910 put_device(&client->dev);
880} 911}
881EXPORT_SYMBOL(i2c_release_client); 912EXPORT_SYMBOL(i2c_release_client);
882 913
@@ -942,10 +973,39 @@ module_exit(i2c_exit);
942 * ---------------------------------------------------- 973 * ----------------------------------------------------
943 */ 974 */
944 975
976/**
977 * i2c_transfer - execute a single or combined I2C message
978 * @adap: Handle to I2C bus
979 * @msgs: One or more messages to execute before STOP is issued to
980 * terminate the operation; each message begins with a START.
981 * @num: Number of messages to be executed.
982 *
983 * Returns negative errno, else the number of messages executed.
984 *
985 * Note that there is no requirement that each message be sent to
986 * the same slave address, although that is the most common model.
987 */
945int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num) 988int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
946{ 989{
947 int ret; 990 int ret;
948 991
992 /* REVISIT the fault reporting model here is weak:
993 *
994 * - When we get an error after receiving N bytes from a slave,
995 * there is no way to report "N".
996 *
997 * - When we get a NAK after transmitting N bytes to a slave,
998 * there is no way to report "N" ... or to let the master
999 * continue executing the rest of this combined message, if
1000 * that's the appropriate response.
1001 *
1002 * - When for example "num" is two and we successfully complete
1003 * the first message but get an error part way through the
1004 * second, it's unclear whether that should be reported as
1005 * one (discarding status on the second message) or errno
1006 * (discarding status on the first one).
1007 */
1008
949 if (adap->algo->master_xfer) { 1009 if (adap->algo->master_xfer) {
950#ifdef DEBUG 1010#ifdef DEBUG
951 for (ret = 0; ret < num; ret++) { 1011 for (ret = 0; ret < num; ret++) {
@@ -971,11 +1031,19 @@ int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
971 return ret; 1031 return ret;
972 } else { 1032 } else {
973 dev_dbg(&adap->dev, "I2C level transfers not supported\n"); 1033 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
974 return -ENOSYS; 1034 return -EOPNOTSUPP;
975 } 1035 }
976} 1036}
977EXPORT_SYMBOL(i2c_transfer); 1037EXPORT_SYMBOL(i2c_transfer);
978 1038
1039/**
1040 * i2c_master_send - issue a single I2C message in master transmit mode
1041 * @client: Handle to slave device
1042 * @buf: Data that will be written to the slave
1043 * @count: How many bytes to write
1044 *
1045 * Returns negative errno, or else the number of bytes written.
1046 */
979int i2c_master_send(struct i2c_client *client,const char *buf ,int count) 1047int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
980{ 1048{
981 int ret; 1049 int ret;
@@ -995,6 +1063,14 @@ int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
995} 1063}
996EXPORT_SYMBOL(i2c_master_send); 1064EXPORT_SYMBOL(i2c_master_send);
997 1065
1066/**
1067 * i2c_master_recv - issue a single I2C message in master receive mode
1068 * @client: Handle to slave device
1069 * @buf: Where to store data read from slave
1070 * @count: How many bytes to read
1071 *
1072 * Returns negative errno, or else the number of bytes read.
1073 */
998int i2c_master_recv(struct i2c_client *client, char *buf ,int count) 1074int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
999{ 1075{
1000 struct i2c_adapter *adap=client->adapter; 1076 struct i2c_adapter *adap=client->adapter;
@@ -1103,7 +1179,7 @@ int i2c_probe(struct i2c_adapter *adapter,
1103 1179
1104 dev_warn(&adapter->dev, "SMBus Quick command not supported, " 1180 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1105 "can't probe for chips\n"); 1181 "can't probe for chips\n");
1106 return -1; 1182 return -EOPNOTSUPP;
1107 } 1183 }
1108 1184
1109 /* Probe entries are done second, and are not affected by ignore 1185 /* Probe entries are done second, and are not affected by ignore
@@ -1157,6 +1233,179 @@ int i2c_probe(struct i2c_adapter *adapter,
1157} 1233}
1158EXPORT_SYMBOL(i2c_probe); 1234EXPORT_SYMBOL(i2c_probe);
1159 1235
1236/* Separate detection function for new-style drivers */
1237static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1238 struct i2c_driver *driver)
1239{
1240 struct i2c_board_info info;
1241 struct i2c_adapter *adapter = temp_client->adapter;
1242 int addr = temp_client->addr;
1243 int err;
1244
1245 /* Make sure the address is valid */
1246 if (addr < 0x03 || addr > 0x77) {
1247 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1248 addr);
1249 return -EINVAL;
1250 }
1251
1252 /* Skip if already in use */
1253 if (i2c_check_addr(adapter, addr))
1254 return 0;
1255
1256 /* Make sure there is something at this address, unless forced */
1257 if (kind < 0) {
1258 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1259 I2C_SMBUS_QUICK, NULL) < 0)
1260 return 0;
1261
1262 /* prevent 24RF08 corruption */
1263 if ((addr & ~0x0f) == 0x50)
1264 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1265 I2C_SMBUS_QUICK, NULL);
1266 }
1267
1268 /* Finally call the custom detection function */
1269 memset(&info, 0, sizeof(struct i2c_board_info));
1270 info.addr = addr;
1271 err = driver->detect(temp_client, kind, &info);
1272 if (err) {
1273 /* -ENODEV is returned if the detection fails. We catch it
1274 here as this isn't an error. */
1275 return err == -ENODEV ? 0 : err;
1276 }
1277
1278 /* Consistency check */
1279 if (info.type[0] == '\0') {
1280 dev_err(&adapter->dev, "%s detection function provided "
1281 "no name for 0x%x\n", driver->driver.name,
1282 addr);
1283 } else {
1284 struct i2c_client *client;
1285
1286 /* Detection succeeded, instantiate the device */
1287 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1288 info.type, info.addr);
1289 client = i2c_new_device(adapter, &info);
1290 if (client)
1291 list_add_tail(&client->detected, &driver->clients);
1292 else
1293 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1294 info.type, info.addr);
1295 }
1296 return 0;
1297}
1298
1299static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1300{
1301 const struct i2c_client_address_data *address_data;
1302 struct i2c_client *temp_client;
1303 int i, err = 0;
1304 int adap_id = i2c_adapter_id(adapter);
1305
1306 address_data = driver->address_data;
1307 if (!driver->detect || !address_data)
1308 return 0;
1309
1310 /* Set up a temporary client to help detect callback */
1311 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1312 if (!temp_client)
1313 return -ENOMEM;
1314 temp_client->adapter = adapter;
1315
1316 /* Force entries are done first, and are not affected by ignore
1317 entries */
1318 if (address_data->forces) {
1319 const unsigned short * const *forces = address_data->forces;
1320 int kind;
1321
1322 for (kind = 0; forces[kind]; kind++) {
1323 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1324 i += 2) {
1325 if (forces[kind][i] == adap_id
1326 || forces[kind][i] == ANY_I2C_BUS) {
1327 dev_dbg(&adapter->dev, "found force "
1328 "parameter for adapter %d, "
1329 "addr 0x%02x, kind %d\n",
1330 adap_id, forces[kind][i + 1],
1331 kind);
1332 temp_client->addr = forces[kind][i + 1];
1333 err = i2c_detect_address(temp_client,
1334 kind, driver);
1335 if (err)
1336 goto exit_free;
1337 }
1338 }
1339 }
1340 }
1341
1342 /* Stop here if we can't use SMBUS_QUICK */
1343 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1344 if (address_data->probe[0] == I2C_CLIENT_END
1345 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1346 goto exit_free;
1347
1348 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1349 "can't probe for chips\n");
1350 err = -EOPNOTSUPP;
1351 goto exit_free;
1352 }
1353
1354 /* Stop here if the classes do not match */
1355 if (!(adapter->class & driver->class))
1356 goto exit_free;
1357
1358 /* Probe entries are done second, and are not affected by ignore
1359 entries either */
1360 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1361 if (address_data->probe[i] == adap_id
1362 || address_data->probe[i] == ANY_I2C_BUS) {
1363 dev_dbg(&adapter->dev, "found probe parameter for "
1364 "adapter %d, addr 0x%02x\n", adap_id,
1365 address_data->probe[i + 1]);
1366 temp_client->addr = address_data->probe[i + 1];
1367 err = i2c_detect_address(temp_client, -1, driver);
1368 if (err)
1369 goto exit_free;
1370 }
1371 }
1372
1373 /* Normal entries are done last, unless shadowed by an ignore entry */
1374 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1375 int j, ignore;
1376
1377 ignore = 0;
1378 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1379 j += 2) {
1380 if ((address_data->ignore[j] == adap_id ||
1381 address_data->ignore[j] == ANY_I2C_BUS)
1382 && address_data->ignore[j + 1]
1383 == address_data->normal_i2c[i]) {
1384 dev_dbg(&adapter->dev, "found ignore "
1385 "parameter for adapter %d, "
1386 "addr 0x%02x\n", adap_id,
1387 address_data->ignore[j + 1]);
1388 ignore = 1;
1389 break;
1390 }
1391 }
1392 if (ignore)
1393 continue;
1394
1395 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1396 "addr 0x%02x\n", adap_id,
1397 address_data->normal_i2c[i]);
1398 temp_client->addr = address_data->normal_i2c[i];
1399 err = i2c_detect_address(temp_client, -1, driver);
1400 if (err)
1401 goto exit_free;
1402 }
1403
1404 exit_free:
1405 kfree(temp_client);
1406 return err;
1407}
1408
1160struct i2c_client * 1409struct i2c_client *
1161i2c_new_probed_device(struct i2c_adapter *adap, 1410i2c_new_probed_device(struct i2c_adapter *adap,
1162 struct i2c_board_info *info, 1411 struct i2c_board_info *info,
@@ -1295,29 +1544,38 @@ static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1295 if (rpec != cpec) { 1544 if (rpec != cpec) {
1296 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", 1545 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1297 rpec, cpec); 1546 rpec, cpec);
1298 return -1; 1547 return -EBADMSG;
1299 } 1548 }
1300 return 0; 1549 return 0;
1301} 1550}
1302 1551
1303s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value) 1552/**
1304{ 1553 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1305 return i2c_smbus_xfer(client->adapter,client->addr,client->flags, 1554 * @client: Handle to slave device
1306 value,0,I2C_SMBUS_QUICK,NULL); 1555 *
1307} 1556 * This executes the SMBus "receive byte" protocol, returning negative errno
1308EXPORT_SYMBOL(i2c_smbus_write_quick); 1557 * else the byte received from the device.
1309 1558 */
1310s32 i2c_smbus_read_byte(struct i2c_client *client) 1559s32 i2c_smbus_read_byte(struct i2c_client *client)
1311{ 1560{
1312 union i2c_smbus_data data; 1561 union i2c_smbus_data data;
1313 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, 1562 int status;
1314 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data)) 1563
1315 return -1; 1564 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1316 else 1565 I2C_SMBUS_READ, 0,
1317 return data.byte; 1566 I2C_SMBUS_BYTE, &data);
1567 return (status < 0) ? status : data.byte;
1318} 1568}
1319EXPORT_SYMBOL(i2c_smbus_read_byte); 1569EXPORT_SYMBOL(i2c_smbus_read_byte);
1320 1570
1571/**
1572 * i2c_smbus_write_byte - SMBus "send byte" protocol
1573 * @client: Handle to slave device
1574 * @value: Byte to be sent
1575 *
1576 * This executes the SMBus "send byte" protocol, returning negative errno
1577 * else zero on success.
1578 */
1321s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) 1579s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1322{ 1580{
1323 return i2c_smbus_xfer(client->adapter,client->addr,client->flags, 1581 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
@@ -1325,17 +1583,35 @@ s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1325} 1583}
1326EXPORT_SYMBOL(i2c_smbus_write_byte); 1584EXPORT_SYMBOL(i2c_smbus_write_byte);
1327 1585
1586/**
1587 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1588 * @client: Handle to slave device
1589 * @command: Byte interpreted by slave
1590 *
1591 * This executes the SMBus "read byte" protocol, returning negative errno
1592 * else a data byte received from the device.
1593 */
1328s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) 1594s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1329{ 1595{
1330 union i2c_smbus_data data; 1596 union i2c_smbus_data data;
1331 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, 1597 int status;
1332 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data)) 1598
1333 return -1; 1599 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1334 else 1600 I2C_SMBUS_READ, command,
1335 return data.byte; 1601 I2C_SMBUS_BYTE_DATA, &data);
1602 return (status < 0) ? status : data.byte;
1336} 1603}
1337EXPORT_SYMBOL(i2c_smbus_read_byte_data); 1604EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1338 1605
1606/**
1607 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1608 * @client: Handle to slave device
1609 * @command: Byte interpreted by slave
1610 * @value: Byte being written
1611 *
1612 * This executes the SMBus "write byte" protocol, returning negative errno
1613 * else zero on success.
1614 */
1339s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) 1615s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1340{ 1616{
1341 union i2c_smbus_data data; 1617 union i2c_smbus_data data;
@@ -1346,17 +1622,35 @@ s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1346} 1622}
1347EXPORT_SYMBOL(i2c_smbus_write_byte_data); 1623EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1348 1624
1625/**
1626 * i2c_smbus_read_word_data - SMBus "read word" protocol
1627 * @client: Handle to slave device
1628 * @command: Byte interpreted by slave
1629 *
1630 * This executes the SMBus "read word" protocol, returning negative errno
1631 * else a 16-bit unsigned "word" received from the device.
1632 */
1349s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) 1633s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1350{ 1634{
1351 union i2c_smbus_data data; 1635 union i2c_smbus_data data;
1352 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, 1636 int status;
1353 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data)) 1637
1354 return -1; 1638 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1355 else 1639 I2C_SMBUS_READ, command,
1356 return data.word; 1640 I2C_SMBUS_WORD_DATA, &data);
1641 return (status < 0) ? status : data.word;
1357} 1642}
1358EXPORT_SYMBOL(i2c_smbus_read_word_data); 1643EXPORT_SYMBOL(i2c_smbus_read_word_data);
1359 1644
1645/**
1646 * i2c_smbus_write_word_data - SMBus "write word" protocol
1647 * @client: Handle to slave device
1648 * @command: Byte interpreted by slave
1649 * @value: 16-bit "word" being written
1650 *
1651 * This executes the SMBus "write word" protocol, returning negative errno
1652 * else zero on success.
1653 */
1360s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) 1654s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1361{ 1655{
1362 union i2c_smbus_data data; 1656 union i2c_smbus_data data;
@@ -1368,15 +1662,14 @@ s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1368EXPORT_SYMBOL(i2c_smbus_write_word_data); 1662EXPORT_SYMBOL(i2c_smbus_write_word_data);
1369 1663
1370/** 1664/**
1371 * i2c_smbus_read_block_data - SMBus block read request 1665 * i2c_smbus_read_block_data - SMBus "block read" protocol
1372 * @client: Handle to slave device 1666 * @client: Handle to slave device
1373 * @command: Command byte issued to let the slave know what data should 1667 * @command: Byte interpreted by slave
1374 * be returned
1375 * @values: Byte array into which data will be read; big enough to hold 1668 * @values: Byte array into which data will be read; big enough to hold
1376 * the data returned by the slave. SMBus allows at most 32 bytes. 1669 * the data returned by the slave. SMBus allows at most 32 bytes.
1377 * 1670 *
1378 * Returns the number of bytes read in the slave's response, else a 1671 * This executes the SMBus "block read" protocol, returning negative errno
1379 * negative number to indicate some kind of error. 1672 * else the number of data bytes in the slave's response.
1380 * 1673 *
1381 * Note that using this function requires that the client's adapter support 1674 * Note that using this function requires that the client's adapter support
1382 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers 1675 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
@@ -1387,17 +1680,29 @@ s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1387 u8 *values) 1680 u8 *values)
1388{ 1681{
1389 union i2c_smbus_data data; 1682 union i2c_smbus_data data;
1683 int status;
1390 1684
1391 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags, 1685 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1392 I2C_SMBUS_READ, command, 1686 I2C_SMBUS_READ, command,
1393 I2C_SMBUS_BLOCK_DATA, &data)) 1687 I2C_SMBUS_BLOCK_DATA, &data);
1394 return -1; 1688 if (status)
1689 return status;
1395 1690
1396 memcpy(values, &data.block[1], data.block[0]); 1691 memcpy(values, &data.block[1], data.block[0]);
1397 return data.block[0]; 1692 return data.block[0];
1398} 1693}
1399EXPORT_SYMBOL(i2c_smbus_read_block_data); 1694EXPORT_SYMBOL(i2c_smbus_read_block_data);
1400 1695
1696/**
1697 * i2c_smbus_write_block_data - SMBus "block write" protocol
1698 * @client: Handle to slave device
1699 * @command: Byte interpreted by slave
1700 * @length: Size of data block; SMBus allows at most 32 bytes
1701 * @values: Byte array which will be written.
1702 *
1703 * This executes the SMBus "block write" protocol, returning negative errno
1704 * else zero on success.
1705 */
1401s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, 1706s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1402 u8 length, const u8 *values) 1707 u8 length, const u8 *values)
1403{ 1708{
@@ -1418,14 +1723,16 @@ s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1418 u8 length, u8 *values) 1723 u8 length, u8 *values)
1419{ 1724{
1420 union i2c_smbus_data data; 1725 union i2c_smbus_data data;
1726 int status;
1421 1727
1422 if (length > I2C_SMBUS_BLOCK_MAX) 1728 if (length > I2C_SMBUS_BLOCK_MAX)
1423 length = I2C_SMBUS_BLOCK_MAX; 1729 length = I2C_SMBUS_BLOCK_MAX;
1424 data.block[0] = length; 1730 data.block[0] = length;
1425 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, 1731 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1426 I2C_SMBUS_READ,command, 1732 I2C_SMBUS_READ, command,
1427 I2C_SMBUS_I2C_BLOCK_DATA,&data)) 1733 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1428 return -1; 1734 if (status < 0)
1735 return status;
1429 1736
1430 memcpy(values, &data.block[1], data.block[0]); 1737 memcpy(values, &data.block[1], data.block[0]);
1431 return data.block[0]; 1738 return data.block[0];
@@ -1466,6 +1773,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1466 }; 1773 };
1467 int i; 1774 int i;
1468 u8 partial_pec = 0; 1775 u8 partial_pec = 0;
1776 int status;
1469 1777
1470 msgbuf0[0] = command; 1778 msgbuf0[0] = command;
1471 switch(size) { 1779 switch(size) {
@@ -1515,10 +1823,10 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1515 } else { 1823 } else {
1516 msg[0].len = data->block[0] + 2; 1824 msg[0].len = data->block[0] + 2;
1517 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { 1825 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1518 dev_err(&adapter->dev, "smbus_access called with " 1826 dev_err(&adapter->dev,
1519 "invalid block write size (%d)\n", 1827 "Invalid block write size %d\n",
1520 data->block[0]); 1828 data->block[0]);
1521 return -1; 1829 return -EINVAL;
1522 } 1830 }
1523 for (i = 1; i < msg[0].len; i++) 1831 for (i = 1; i < msg[0].len; i++)
1524 msgbuf0[i] = data->block[i-1]; 1832 msgbuf0[i] = data->block[i-1];
@@ -1528,10 +1836,10 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1528 num = 2; /* Another special case */ 1836 num = 2; /* Another special case */
1529 read_write = I2C_SMBUS_READ; 1837 read_write = I2C_SMBUS_READ;
1530 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { 1838 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1531 dev_err(&adapter->dev, "%s called with invalid " 1839 dev_err(&adapter->dev,
1532 "block proc call size (%d)\n", __func__, 1840 "Invalid block write size %d\n",
1533 data->block[0]); 1841 data->block[0]);
1534 return -1; 1842 return -EINVAL;
1535 } 1843 }
1536 msg[0].len = data->block[0] + 2; 1844 msg[0].len = data->block[0] + 2;
1537 for (i = 1; i < msg[0].len; i++) 1845 for (i = 1; i < msg[0].len; i++)
@@ -1546,19 +1854,18 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1546 } else { 1854 } else {
1547 msg[0].len = data->block[0] + 1; 1855 msg[0].len = data->block[0] + 1;
1548 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { 1856 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1549 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with " 1857 dev_err(&adapter->dev,
1550 "invalid block write size (%d)\n", 1858 "Invalid block write size %d\n",
1551 data->block[0]); 1859 data->block[0]);
1552 return -1; 1860 return -EINVAL;
1553 } 1861 }
1554 for (i = 1; i <= data->block[0]; i++) 1862 for (i = 1; i <= data->block[0]; i++)
1555 msgbuf0[i] = data->block[i]; 1863 msgbuf0[i] = data->block[i];
1556 } 1864 }
1557 break; 1865 break;
1558 default: 1866 default:
1559 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n", 1867 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1560 size); 1868 return -EOPNOTSUPP;
1561 return -1;
1562 } 1869 }
1563 1870
1564 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK 1871 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
@@ -1576,13 +1883,15 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1576 msg[num-1].len++; 1883 msg[num-1].len++;
1577 } 1884 }
1578 1885
1579 if (i2c_transfer(adapter, msg, num) < 0) 1886 status = i2c_transfer(adapter, msg, num);
1580 return -1; 1887 if (status < 0)
1888 return status;
1581 1889
1582 /* Check PEC if last message is a read */ 1890 /* Check PEC if last message is a read */
1583 if (i && (msg[num-1].flags & I2C_M_RD)) { 1891 if (i && (msg[num-1].flags & I2C_M_RD)) {
1584 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0) 1892 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1585 return -1; 1893 if (status < 0)
1894 return status;
1586 } 1895 }
1587 1896
1588 if (read_write == I2C_SMBUS_READ) 1897 if (read_write == I2C_SMBUS_READ)
@@ -1610,9 +1919,21 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1610 return 0; 1919 return 0;
1611} 1920}
1612 1921
1613 1922/**
1923 * i2c_smbus_xfer - execute SMBus protocol operations
1924 * @adapter: Handle to I2C bus
1925 * @addr: Address of SMBus slave on that bus
1926 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1927 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1928 * @command: Byte interpreted by slave, for protocols which use such bytes
1929 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1930 * @data: Data to be read or written
1931 *
1932 * This executes an SMBus protocol operation, and returns a negative
1933 * errno code else zero on success.
1934 */
1614s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags, 1935s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1615 char read_write, u8 command, int size, 1936 char read_write, u8 command, int protocol,
1616 union i2c_smbus_data * data) 1937 union i2c_smbus_data * data)
1617{ 1938{
1618 s32 res; 1939 s32 res;
@@ -1622,11 +1943,11 @@ s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1622 if (adapter->algo->smbus_xfer) { 1943 if (adapter->algo->smbus_xfer) {
1623 mutex_lock(&adapter->bus_lock); 1944 mutex_lock(&adapter->bus_lock);
1624 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write, 1945 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1625 command,size,data); 1946 command, protocol, data);
1626 mutex_unlock(&adapter->bus_lock); 1947 mutex_unlock(&adapter->bus_lock);
1627 } else 1948 } else
1628 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, 1949 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1629 command,size,data); 1950 command, protocol, data);
1630 1951
1631 return res; 1952 return res;
1632} 1953}