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.c169
1 files changed, 38 insertions, 131 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 29650435514..4f34823e86b 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -558,11 +558,9 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
558 up_read(&__i2c_board_lock); 558 up_read(&__i2c_board_lock);
559} 559}
560 560
561static int i2c_do_add_adapter(struct device_driver *d, void *data) 561static int i2c_do_add_adapter(struct i2c_driver *driver,
562 struct i2c_adapter *adap)
562{ 563{
563 struct i2c_driver *driver = to_i2c_driver(d);
564 struct i2c_adapter *adap = data;
565
566 /* Detect supported devices on that bus, and instantiate them */ 564 /* Detect supported devices on that bus, and instantiate them */
567 i2c_detect(adap, driver); 565 i2c_detect(adap, driver);
568 566
@@ -574,6 +572,11 @@ static int i2c_do_add_adapter(struct device_driver *d, void *data)
574 return 0; 572 return 0;
575} 573}
576 574
575static int __process_new_adapter(struct device_driver *d, void *data)
576{
577 return i2c_do_add_adapter(to_i2c_driver(d), data);
578}
579
577static int i2c_register_adapter(struct i2c_adapter *adap) 580static int i2c_register_adapter(struct i2c_adapter *adap)
578{ 581{
579 int res = 0, dummy; 582 int res = 0, dummy;
@@ -584,7 +587,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
584 goto out_list; 587 goto out_list;
585 } 588 }
586 589
587 mutex_init(&adap->bus_lock); 590 rt_mutex_init(&adap->bus_lock);
588 591
589 /* Set default timeout to 1 second if not already set */ 592 /* Set default timeout to 1 second if not already set */
590 if (adap->timeout == 0) 593 if (adap->timeout == 0)
@@ -614,7 +617,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
614 /* Notify drivers */ 617 /* Notify drivers */
615 mutex_lock(&core_lock); 618 mutex_lock(&core_lock);
616 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, 619 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
617 i2c_do_add_adapter); 620 __process_new_adapter);
618 mutex_unlock(&core_lock); 621 mutex_unlock(&core_lock);
619 622
620 return 0; 623 return 0;
@@ -715,10 +718,9 @@ retry:
715} 718}
716EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); 719EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
717 720
718static int i2c_do_del_adapter(struct device_driver *d, void *data) 721static int i2c_do_del_adapter(struct i2c_driver *driver,
722 struct i2c_adapter *adapter)
719{ 723{
720 struct i2c_driver *driver = to_i2c_driver(d);
721 struct i2c_adapter *adapter = data;
722 struct i2c_client *client, *_n; 724 struct i2c_client *client, *_n;
723 int res; 725 int res;
724 726
@@ -750,6 +752,11 @@ static int __unregister_client(struct device *dev, void *dummy)
750 return 0; 752 return 0;
751} 753}
752 754
755static int __process_removed_adapter(struct device_driver *d, void *data)
756{
757 return i2c_do_del_adapter(to_i2c_driver(d), data);
758}
759
753/** 760/**
754 * i2c_del_adapter - unregister I2C adapter 761 * i2c_del_adapter - unregister I2C adapter
755 * @adap: the adapter being unregistered 762 * @adap: the adapter being unregistered
@@ -777,7 +784,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
777 /* Tell drivers about this removal */ 784 /* Tell drivers about this removal */
778 mutex_lock(&core_lock); 785 mutex_lock(&core_lock);
779 res = bus_for_each_drv(&i2c_bus_type, NULL, adap, 786 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
780 i2c_do_del_adapter); 787 __process_removed_adapter);
781 mutex_unlock(&core_lock); 788 mutex_unlock(&core_lock);
782 if (res) 789 if (res)
783 return res; 790 return res;
@@ -826,22 +833,11 @@ EXPORT_SYMBOL(i2c_del_adapter);
826 833
827/* ------------------------------------------------------------------------- */ 834/* ------------------------------------------------------------------------- */
828 835
829static int __attach_adapter(struct device *dev, void *data) 836static int __process_new_driver(struct device *dev, void *data)
830{ 837{
831 struct i2c_adapter *adapter;
832 struct i2c_driver *driver = data;
833
834 if (dev->type != &i2c_adapter_type) 838 if (dev->type != &i2c_adapter_type)
835 return 0; 839 return 0;
836 adapter = to_i2c_adapter(dev); 840 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
837
838 i2c_detect(adapter, driver);
839
840 /* Legacy drivers scan i2c busses directly */
841 if (driver->attach_adapter)
842 driver->attach_adapter(adapter);
843
844 return 0;
845} 841}
846 842
847/* 843/*
@@ -873,40 +869,18 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
873 INIT_LIST_HEAD(&driver->clients); 869 INIT_LIST_HEAD(&driver->clients);
874 /* Walk the adapters that are already present */ 870 /* Walk the adapters that are already present */
875 mutex_lock(&core_lock); 871 mutex_lock(&core_lock);
876 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter); 872 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
877 mutex_unlock(&core_lock); 873 mutex_unlock(&core_lock);
878 874
879 return 0; 875 return 0;
880} 876}
881EXPORT_SYMBOL(i2c_register_driver); 877EXPORT_SYMBOL(i2c_register_driver);
882 878
883static int __detach_adapter(struct device *dev, void *data) 879static int __process_removed_driver(struct device *dev, void *data)
884{ 880{
885 struct i2c_adapter *adapter;
886 struct i2c_driver *driver = data;
887 struct i2c_client *client, *_n;
888
889 if (dev->type != &i2c_adapter_type) 881 if (dev->type != &i2c_adapter_type)
890 return 0; 882 return 0;
891 adapter = to_i2c_adapter(dev); 883 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
892
893 /* Remove the devices we created ourselves as the result of hardware
894 * probing (using a driver's detect method) */
895 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
896 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
897 client->name, client->addr);
898 list_del(&client->detected);
899 i2c_unregister_device(client);
900 }
901
902 if (driver->detach_adapter) {
903 if (driver->detach_adapter(adapter))
904 dev_err(&adapter->dev,
905 "detach_adapter failed for driver [%s]\n",
906 driver->driver.name);
907 }
908
909 return 0;
910} 884}
911 885
912/** 886/**
@@ -917,7 +891,7 @@ static int __detach_adapter(struct device *dev, void *data)
917void i2c_del_driver(struct i2c_driver *driver) 891void i2c_del_driver(struct i2c_driver *driver)
918{ 892{
919 mutex_lock(&core_lock); 893 mutex_lock(&core_lock);
920 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter); 894 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
921 mutex_unlock(&core_lock); 895 mutex_unlock(&core_lock);
922 896
923 driver_unregister(&driver->driver); 897 driver_unregister(&driver->driver);
@@ -1092,12 +1066,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1092#endif 1066#endif
1093 1067
1094 if (in_atomic() || irqs_disabled()) { 1068 if (in_atomic() || irqs_disabled()) {
1095 ret = mutex_trylock(&adap->bus_lock); 1069 ret = rt_mutex_trylock(&adap->bus_lock);
1096 if (!ret) 1070 if (!ret)
1097 /* I2C activity is ongoing. */ 1071 /* I2C activity is ongoing. */
1098 return -EAGAIN; 1072 return -EAGAIN;
1099 } else { 1073 } else {
1100 mutex_lock_nested(&adap->bus_lock, adap->level); 1074 rt_mutex_lock(&adap->bus_lock);
1101 } 1075 }
1102 1076
1103 /* Retry automatically on arbitration loss */ 1077 /* Retry automatically on arbitration loss */
@@ -1109,7 +1083,7 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1109 if (time_after(jiffies, orig_jiffies + adap->timeout)) 1083 if (time_after(jiffies, orig_jiffies + adap->timeout))
1110 break; 1084 break;
1111 } 1085 }
1112 mutex_unlock(&adap->bus_lock); 1086 rt_mutex_unlock(&adap->bus_lock);
1113 1087
1114 return ret; 1088 return ret;
1115 } else { 1089 } else {
@@ -1180,7 +1154,7 @@ EXPORT_SYMBOL(i2c_master_recv);
1180 * ---------------------------------------------------- 1154 * ----------------------------------------------------
1181 */ 1155 */
1182 1156
1183static int i2c_detect_address(struct i2c_client *temp_client, int kind, 1157static int i2c_detect_address(struct i2c_client *temp_client,
1184 struct i2c_driver *driver) 1158 struct i2c_driver *driver)
1185{ 1159{
1186 struct i2c_board_info info; 1160 struct i2c_board_info info;
@@ -1199,22 +1173,18 @@ static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1199 if (i2c_check_addr(adapter, addr)) 1173 if (i2c_check_addr(adapter, addr))
1200 return 0; 1174 return 0;
1201 1175
1202 /* Make sure there is something at this address, unless forced */ 1176 /* Make sure there is something at this address */
1203 if (kind < 0) { 1177 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
1204 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, 1178 return 0;
1205 I2C_SMBUS_QUICK, NULL) < 0)
1206 return 0;
1207 1179
1208 /* prevent 24RF08 corruption */ 1180 /* Prevent 24RF08 corruption */
1209 if ((addr & ~0x0f) == 0x50) 1181 if ((addr & ~0x0f) == 0x50)
1210 i2c_smbus_xfer(adapter, addr, 0, 0, 0, 1182 i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
1211 I2C_SMBUS_QUICK, NULL);
1212 }
1213 1183
1214 /* Finally call the custom detection function */ 1184 /* Finally call the custom detection function */
1215 memset(&info, 0, sizeof(struct i2c_board_info)); 1185 memset(&info, 0, sizeof(struct i2c_board_info));
1216 info.addr = addr; 1186 info.addr = addr;
1217 err = driver->detect(temp_client, kind, &info); 1187 err = driver->detect(temp_client, -1, &info);
1218 if (err) { 1188 if (err) {
1219 /* -ENODEV is returned if the detection fails. We catch it 1189 /* -ENODEV is returned if the detection fails. We catch it
1220 here as this isn't an error. */ 1190 here as this isn't an error. */
@@ -1259,40 +1229,13 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1259 return -ENOMEM; 1229 return -ENOMEM;
1260 temp_client->adapter = adapter; 1230 temp_client->adapter = adapter;
1261 1231
1262 /* Force entries are done first, and are not affected by ignore
1263 entries */
1264 if (address_data->forces) {
1265 const unsigned short * const *forces = address_data->forces;
1266 int kind;
1267
1268 for (kind = 0; forces[kind]; kind++) {
1269 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1270 i += 2) {
1271 if (forces[kind][i] == adap_id
1272 || forces[kind][i] == ANY_I2C_BUS) {
1273 dev_dbg(&adapter->dev, "found force "
1274 "parameter for adapter %d, "
1275 "addr 0x%02x, kind %d\n",
1276 adap_id, forces[kind][i + 1],
1277 kind);
1278 temp_client->addr = forces[kind][i + 1];
1279 err = i2c_detect_address(temp_client,
1280 kind, driver);
1281 if (err)
1282 goto exit_free;
1283 }
1284 }
1285 }
1286 }
1287
1288 /* Stop here if the classes do not match */ 1232 /* Stop here if the classes do not match */
1289 if (!(adapter->class & driver->class)) 1233 if (!(adapter->class & driver->class))
1290 goto exit_free; 1234 goto exit_free;
1291 1235
1292 /* Stop here if we can't use SMBUS_QUICK */ 1236 /* Stop here if we can't use SMBUS_QUICK */
1293 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { 1237 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1294 if (address_data->probe[0] == I2C_CLIENT_END 1238 if (address_data->normal_i2c[0] == I2C_CLIENT_END)
1295 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1296 goto exit_free; 1239 goto exit_free;
1297 1240
1298 dev_warn(&adapter->dev, "SMBus Quick command not supported, " 1241 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
@@ -1301,48 +1244,12 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1301 goto exit_free; 1244 goto exit_free;
1302 } 1245 }
1303 1246
1304 /* Probe entries are done second, and are not affected by ignore
1305 entries either */
1306 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1307 if (address_data->probe[i] == adap_id
1308 || address_data->probe[i] == ANY_I2C_BUS) {
1309 dev_dbg(&adapter->dev, "found probe parameter for "
1310 "adapter %d, addr 0x%02x\n", adap_id,
1311 address_data->probe[i + 1]);
1312 temp_client->addr = address_data->probe[i + 1];
1313 err = i2c_detect_address(temp_client, -1, driver);
1314 if (err)
1315 goto exit_free;
1316 }
1317 }
1318
1319 /* Normal entries are done last, unless shadowed by an ignore entry */
1320 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { 1247 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1321 int j, ignore;
1322
1323 ignore = 0;
1324 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1325 j += 2) {
1326 if ((address_data->ignore[j] == adap_id ||
1327 address_data->ignore[j] == ANY_I2C_BUS)
1328 && address_data->ignore[j + 1]
1329 == address_data->normal_i2c[i]) {
1330 dev_dbg(&adapter->dev, "found ignore "
1331 "parameter for adapter %d, "
1332 "addr 0x%02x\n", adap_id,
1333 address_data->ignore[j + 1]);
1334 ignore = 1;
1335 break;
1336 }
1337 }
1338 if (ignore)
1339 continue;
1340
1341 dev_dbg(&adapter->dev, "found normal entry for adapter %d, " 1248 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1342 "addr 0x%02x\n", adap_id, 1249 "addr 0x%02x\n", adap_id,
1343 address_data->normal_i2c[i]); 1250 address_data->normal_i2c[i]);
1344 temp_client->addr = address_data->normal_i2c[i]; 1251 temp_client->addr = address_data->normal_i2c[i];
1345 err = i2c_detect_address(temp_client, -1, driver); 1252 err = i2c_detect_address(temp_client, driver);
1346 if (err) 1253 if (err)
1347 goto exit_free; 1254 goto exit_free;
1348 } 1255 }
@@ -1913,7 +1820,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1913 flags &= I2C_M_TEN | I2C_CLIENT_PEC; 1820 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1914 1821
1915 if (adapter->algo->smbus_xfer) { 1822 if (adapter->algo->smbus_xfer) {
1916 mutex_lock(&adapter->bus_lock); 1823 rt_mutex_lock(&adapter->bus_lock);
1917 1824
1918 /* Retry automatically on arbitration loss */ 1825 /* Retry automatically on arbitration loss */
1919 orig_jiffies = jiffies; 1826 orig_jiffies = jiffies;
@@ -1927,7 +1834,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1927 orig_jiffies + adapter->timeout)) 1834 orig_jiffies + adapter->timeout))
1928 break; 1835 break;
1929 } 1836 }
1930 mutex_unlock(&adapter->bus_lock); 1837 rt_mutex_unlock(&adapter->bus_lock);
1931 } else 1838 } else
1932 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, 1839 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1933 command, protocol, data); 1840 command, protocol, data);