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.c180
1 files changed, 49 insertions, 131 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 8d80fceca6a4..4f34823e86b1 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
@@ -762,6 +769,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
762{ 769{
763 int res = 0; 770 int res = 0;
764 struct i2c_adapter *found; 771 struct i2c_adapter *found;
772 struct i2c_client *client, *next;
765 773
766 /* First make sure that this adapter was ever added */ 774 /* First make sure that this adapter was ever added */
767 mutex_lock(&core_lock); 775 mutex_lock(&core_lock);
@@ -776,11 +784,21 @@ int i2c_del_adapter(struct i2c_adapter *adap)
776 /* Tell drivers about this removal */ 784 /* Tell drivers about this removal */
777 mutex_lock(&core_lock); 785 mutex_lock(&core_lock);
778 res = bus_for_each_drv(&i2c_bus_type, NULL, adap, 786 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
779 i2c_do_del_adapter); 787 __process_removed_adapter);
780 mutex_unlock(&core_lock); 788 mutex_unlock(&core_lock);
781 if (res) 789 if (res)
782 return res; 790 return res;
783 791
792 /* Remove devices instantiated from sysfs */
793 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
794 if (client->adapter == adap) {
795 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
796 client->name, client->addr);
797 list_del(&client->detected);
798 i2c_unregister_device(client);
799 }
800 }
801
784 /* Detach any active clients. This can't fail, thus we do not 802 /* Detach any active clients. This can't fail, thus we do not
785 checking the returned value. */ 803 checking the returned value. */
786 res = device_for_each_child(&adap->dev, NULL, __unregister_client); 804 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
@@ -815,22 +833,11 @@ EXPORT_SYMBOL(i2c_del_adapter);
815 833
816/* ------------------------------------------------------------------------- */ 834/* ------------------------------------------------------------------------- */
817 835
818static int __attach_adapter(struct device *dev, void *data) 836static int __process_new_driver(struct device *dev, void *data)
819{ 837{
820 struct i2c_adapter *adapter;
821 struct i2c_driver *driver = data;
822
823 if (dev->type != &i2c_adapter_type) 838 if (dev->type != &i2c_adapter_type)
824 return 0; 839 return 0;
825 adapter = to_i2c_adapter(dev); 840 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
826
827 i2c_detect(adapter, driver);
828
829 /* Legacy drivers scan i2c busses directly */
830 if (driver->attach_adapter)
831 driver->attach_adapter(adapter);
832
833 return 0;
834} 841}
835 842
836/* 843/*
@@ -862,40 +869,18 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
862 INIT_LIST_HEAD(&driver->clients); 869 INIT_LIST_HEAD(&driver->clients);
863 /* Walk the adapters that are already present */ 870 /* Walk the adapters that are already present */
864 mutex_lock(&core_lock); 871 mutex_lock(&core_lock);
865 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter); 872 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
866 mutex_unlock(&core_lock); 873 mutex_unlock(&core_lock);
867 874
868 return 0; 875 return 0;
869} 876}
870EXPORT_SYMBOL(i2c_register_driver); 877EXPORT_SYMBOL(i2c_register_driver);
871 878
872static int __detach_adapter(struct device *dev, void *data) 879static int __process_removed_driver(struct device *dev, void *data)
873{ 880{
874 struct i2c_adapter *adapter;
875 struct i2c_driver *driver = data;
876 struct i2c_client *client, *_n;
877
878 if (dev->type != &i2c_adapter_type) 881 if (dev->type != &i2c_adapter_type)
879 return 0; 882 return 0;
880 adapter = to_i2c_adapter(dev); 883 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
881
882 /* Remove the devices we created ourselves as the result of hardware
883 * probing (using a driver's detect method) */
884 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
885 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
886 client->name, client->addr);
887 list_del(&client->detected);
888 i2c_unregister_device(client);
889 }
890
891 if (driver->detach_adapter) {
892 if (driver->detach_adapter(adapter))
893 dev_err(&adapter->dev,
894 "detach_adapter failed for driver [%s]\n",
895 driver->driver.name);
896 }
897
898 return 0;
899} 884}
900 885
901/** 886/**
@@ -906,7 +891,7 @@ static int __detach_adapter(struct device *dev, void *data)
906void i2c_del_driver(struct i2c_driver *driver) 891void i2c_del_driver(struct i2c_driver *driver)
907{ 892{
908 mutex_lock(&core_lock); 893 mutex_lock(&core_lock);
909 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter); 894 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
910 mutex_unlock(&core_lock); 895 mutex_unlock(&core_lock);
911 896
912 driver_unregister(&driver->driver); 897 driver_unregister(&driver->driver);
@@ -1081,12 +1066,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1081#endif 1066#endif
1082 1067
1083 if (in_atomic() || irqs_disabled()) { 1068 if (in_atomic() || irqs_disabled()) {
1084 ret = mutex_trylock(&adap->bus_lock); 1069 ret = rt_mutex_trylock(&adap->bus_lock);
1085 if (!ret) 1070 if (!ret)
1086 /* I2C activity is ongoing. */ 1071 /* I2C activity is ongoing. */
1087 return -EAGAIN; 1072 return -EAGAIN;
1088 } else { 1073 } else {
1089 mutex_lock_nested(&adap->bus_lock, adap->level); 1074 rt_mutex_lock(&adap->bus_lock);
1090 } 1075 }
1091 1076
1092 /* Retry automatically on arbitration loss */ 1077 /* Retry automatically on arbitration loss */
@@ -1098,7 +1083,7 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1098 if (time_after(jiffies, orig_jiffies + adap->timeout)) 1083 if (time_after(jiffies, orig_jiffies + adap->timeout))
1099 break; 1084 break;
1100 } 1085 }
1101 mutex_unlock(&adap->bus_lock); 1086 rt_mutex_unlock(&adap->bus_lock);
1102 1087
1103 return ret; 1088 return ret;
1104 } else { 1089 } else {
@@ -1169,7 +1154,7 @@ EXPORT_SYMBOL(i2c_master_recv);
1169 * ---------------------------------------------------- 1154 * ----------------------------------------------------
1170 */ 1155 */
1171 1156
1172static int i2c_detect_address(struct i2c_client *temp_client, int kind, 1157static int i2c_detect_address(struct i2c_client *temp_client,
1173 struct i2c_driver *driver) 1158 struct i2c_driver *driver)
1174{ 1159{
1175 struct i2c_board_info info; 1160 struct i2c_board_info info;
@@ -1188,22 +1173,18 @@ static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1188 if (i2c_check_addr(adapter, addr)) 1173 if (i2c_check_addr(adapter, addr))
1189 return 0; 1174 return 0;
1190 1175
1191 /* Make sure there is something at this address, unless forced */ 1176 /* Make sure there is something at this address */
1192 if (kind < 0) { 1177 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
1193 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, 1178 return 0;
1194 I2C_SMBUS_QUICK, NULL) < 0)
1195 return 0;
1196 1179
1197 /* prevent 24RF08 corruption */ 1180 /* Prevent 24RF08 corruption */
1198 if ((addr & ~0x0f) == 0x50) 1181 if ((addr & ~0x0f) == 0x50)
1199 i2c_smbus_xfer(adapter, addr, 0, 0, 0, 1182 i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
1200 I2C_SMBUS_QUICK, NULL);
1201 }
1202 1183
1203 /* Finally call the custom detection function */ 1184 /* Finally call the custom detection function */
1204 memset(&info, 0, sizeof(struct i2c_board_info)); 1185 memset(&info, 0, sizeof(struct i2c_board_info));
1205 info.addr = addr; 1186 info.addr = addr;
1206 err = driver->detect(temp_client, kind, &info); 1187 err = driver->detect(temp_client, -1, &info);
1207 if (err) { 1188 if (err) {
1208 /* -ENODEV is returned if the detection fails. We catch it 1189 /* -ENODEV is returned if the detection fails. We catch it
1209 here as this isn't an error. */ 1190 here as this isn't an error. */
@@ -1248,40 +1229,13 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1248 return -ENOMEM; 1229 return -ENOMEM;
1249 temp_client->adapter = adapter; 1230 temp_client->adapter = adapter;
1250 1231
1251 /* Force entries are done first, and are not affected by ignore
1252 entries */
1253 if (address_data->forces) {
1254 const unsigned short * const *forces = address_data->forces;
1255 int kind;
1256
1257 for (kind = 0; forces[kind]; kind++) {
1258 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1259 i += 2) {
1260 if (forces[kind][i] == adap_id
1261 || forces[kind][i] == ANY_I2C_BUS) {
1262 dev_dbg(&adapter->dev, "found force "
1263 "parameter for adapter %d, "
1264 "addr 0x%02x, kind %d\n",
1265 adap_id, forces[kind][i + 1],
1266 kind);
1267 temp_client->addr = forces[kind][i + 1];
1268 err = i2c_detect_address(temp_client,
1269 kind, driver);
1270 if (err)
1271 goto exit_free;
1272 }
1273 }
1274 }
1275 }
1276
1277 /* Stop here if the classes do not match */ 1232 /* Stop here if the classes do not match */
1278 if (!(adapter->class & driver->class)) 1233 if (!(adapter->class & driver->class))
1279 goto exit_free; 1234 goto exit_free;
1280 1235
1281 /* Stop here if we can't use SMBUS_QUICK */ 1236 /* Stop here if we can't use SMBUS_QUICK */
1282 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { 1237 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1283 if (address_data->probe[0] == I2C_CLIENT_END 1238 if (address_data->normal_i2c[0] == I2C_CLIENT_END)
1284 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1285 goto exit_free; 1239 goto exit_free;
1286 1240
1287 dev_warn(&adapter->dev, "SMBus Quick command not supported, " 1241 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
@@ -1290,48 +1244,12 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1290 goto exit_free; 1244 goto exit_free;
1291 } 1245 }
1292 1246
1293 /* Probe entries are done second, and are not affected by ignore
1294 entries either */
1295 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1296 if (address_data->probe[i] == adap_id
1297 || address_data->probe[i] == ANY_I2C_BUS) {
1298 dev_dbg(&adapter->dev, "found probe parameter for "
1299 "adapter %d, addr 0x%02x\n", adap_id,
1300 address_data->probe[i + 1]);
1301 temp_client->addr = address_data->probe[i + 1];
1302 err = i2c_detect_address(temp_client, -1, driver);
1303 if (err)
1304 goto exit_free;
1305 }
1306 }
1307
1308 /* Normal entries are done last, unless shadowed by an ignore entry */
1309 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) {
1310 int j, ignore;
1311
1312 ignore = 0;
1313 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1314 j += 2) {
1315 if ((address_data->ignore[j] == adap_id ||
1316 address_data->ignore[j] == ANY_I2C_BUS)
1317 && address_data->ignore[j + 1]
1318 == address_data->normal_i2c[i]) {
1319 dev_dbg(&adapter->dev, "found ignore "
1320 "parameter for adapter %d, "
1321 "addr 0x%02x\n", adap_id,
1322 address_data->ignore[j + 1]);
1323 ignore = 1;
1324 break;
1325 }
1326 }
1327 if (ignore)
1328 continue;
1329
1330 dev_dbg(&adapter->dev, "found normal entry for adapter %d, " 1248 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1331 "addr 0x%02x\n", adap_id, 1249 "addr 0x%02x\n", adap_id,
1332 address_data->normal_i2c[i]); 1250 address_data->normal_i2c[i]);
1333 temp_client->addr = address_data->normal_i2c[i]; 1251 temp_client->addr = address_data->normal_i2c[i];
1334 err = i2c_detect_address(temp_client, -1, driver); 1252 err = i2c_detect_address(temp_client, driver);
1335 if (err) 1253 if (err)
1336 goto exit_free; 1254 goto exit_free;
1337 } 1255 }
@@ -1902,7 +1820,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1902 flags &= I2C_M_TEN | I2C_CLIENT_PEC; 1820 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1903 1821
1904 if (adapter->algo->smbus_xfer) { 1822 if (adapter->algo->smbus_xfer) {
1905 mutex_lock(&adapter->bus_lock); 1823 rt_mutex_lock(&adapter->bus_lock);
1906 1824
1907 /* Retry automatically on arbitration loss */ 1825 /* Retry automatically on arbitration loss */
1908 orig_jiffies = jiffies; 1826 orig_jiffies = jiffies;
@@ -1916,7 +1834,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1916 orig_jiffies + adapter->timeout)) 1834 orig_jiffies + adapter->timeout))
1917 break; 1835 break;
1918 } 1836 }
1919 mutex_unlock(&adapter->bus_lock); 1837 rt_mutex_unlock(&adapter->bus_lock);
1920 } else 1838 } else
1921 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, 1839 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1922 command, protocol, data); 1840 command, protocol, data);