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.c228
1 files changed, 90 insertions, 138 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 8d80fceca6a4..0ac2f90ab840 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -155,6 +155,35 @@ static void i2c_device_shutdown(struct device *dev)
155 driver->shutdown(client); 155 driver->shutdown(client);
156} 156}
157 157
158#ifdef CONFIG_SUSPEND
159static int i2c_device_pm_suspend(struct device *dev)
160{
161 const struct dev_pm_ops *pm;
162
163 if (!dev->driver)
164 return 0;
165 pm = dev->driver->pm;
166 if (!pm || !pm->suspend)
167 return 0;
168 return pm->suspend(dev);
169}
170
171static int i2c_device_pm_resume(struct device *dev)
172{
173 const struct dev_pm_ops *pm;
174
175 if (!dev->driver)
176 return 0;
177 pm = dev->driver->pm;
178 if (!pm || !pm->resume)
179 return 0;
180 return pm->resume(dev);
181}
182#else
183#define i2c_device_pm_suspend NULL
184#define i2c_device_pm_resume NULL
185#endif
186
158static int i2c_device_suspend(struct device *dev, pm_message_t mesg) 187static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
159{ 188{
160 struct i2c_client *client = i2c_verify_client(dev); 189 struct i2c_client *client = i2c_verify_client(dev);
@@ -219,6 +248,11 @@ static const struct attribute_group *i2c_dev_attr_groups[] = {
219 NULL 248 NULL
220}; 249};
221 250
251const static struct dev_pm_ops i2c_device_pm_ops = {
252 .suspend = i2c_device_pm_suspend,
253 .resume = i2c_device_pm_resume,
254};
255
222struct bus_type i2c_bus_type = { 256struct bus_type i2c_bus_type = {
223 .name = "i2c", 257 .name = "i2c",
224 .match = i2c_device_match, 258 .match = i2c_device_match,
@@ -227,6 +261,7 @@ struct bus_type i2c_bus_type = {
227 .shutdown = i2c_device_shutdown, 261 .shutdown = i2c_device_shutdown,
228 .suspend = i2c_device_suspend, 262 .suspend = i2c_device_suspend,
229 .resume = i2c_device_resume, 263 .resume = i2c_device_resume,
264 .pm = &i2c_device_pm_ops,
230}; 265};
231EXPORT_SYMBOL_GPL(i2c_bus_type); 266EXPORT_SYMBOL_GPL(i2c_bus_type);
232 267
@@ -558,11 +593,9 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
558 up_read(&__i2c_board_lock); 593 up_read(&__i2c_board_lock);
559} 594}
560 595
561static int i2c_do_add_adapter(struct device_driver *d, void *data) 596static int i2c_do_add_adapter(struct i2c_driver *driver,
597 struct i2c_adapter *adap)
562{ 598{
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 */ 599 /* Detect supported devices on that bus, and instantiate them */
567 i2c_detect(adap, driver); 600 i2c_detect(adap, driver);
568 601
@@ -574,6 +607,11 @@ static int i2c_do_add_adapter(struct device_driver *d, void *data)
574 return 0; 607 return 0;
575} 608}
576 609
610static int __process_new_adapter(struct device_driver *d, void *data)
611{
612 return i2c_do_add_adapter(to_i2c_driver(d), data);
613}
614
577static int i2c_register_adapter(struct i2c_adapter *adap) 615static int i2c_register_adapter(struct i2c_adapter *adap)
578{ 616{
579 int res = 0, dummy; 617 int res = 0, dummy;
@@ -584,7 +622,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
584 goto out_list; 622 goto out_list;
585 } 623 }
586 624
587 mutex_init(&adap->bus_lock); 625 rt_mutex_init(&adap->bus_lock);
588 626
589 /* Set default timeout to 1 second if not already set */ 627 /* Set default timeout to 1 second if not already set */
590 if (adap->timeout == 0) 628 if (adap->timeout == 0)
@@ -614,7 +652,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
614 /* Notify drivers */ 652 /* Notify drivers */
615 mutex_lock(&core_lock); 653 mutex_lock(&core_lock);
616 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, 654 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
617 i2c_do_add_adapter); 655 __process_new_adapter);
618 mutex_unlock(&core_lock); 656 mutex_unlock(&core_lock);
619 657
620 return 0; 658 return 0;
@@ -715,10 +753,9 @@ retry:
715} 753}
716EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); 754EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
717 755
718static int i2c_do_del_adapter(struct device_driver *d, void *data) 756static int i2c_do_del_adapter(struct i2c_driver *driver,
757 struct i2c_adapter *adapter)
719{ 758{
720 struct i2c_driver *driver = to_i2c_driver(d);
721 struct i2c_adapter *adapter = data;
722 struct i2c_client *client, *_n; 759 struct i2c_client *client, *_n;
723 int res; 760 int res;
724 761
@@ -750,6 +787,11 @@ static int __unregister_client(struct device *dev, void *dummy)
750 return 0; 787 return 0;
751} 788}
752 789
790static int __process_removed_adapter(struct device_driver *d, void *data)
791{
792 return i2c_do_del_adapter(to_i2c_driver(d), data);
793}
794
753/** 795/**
754 * i2c_del_adapter - unregister I2C adapter 796 * i2c_del_adapter - unregister I2C adapter
755 * @adap: the adapter being unregistered 797 * @adap: the adapter being unregistered
@@ -762,6 +804,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
762{ 804{
763 int res = 0; 805 int res = 0;
764 struct i2c_adapter *found; 806 struct i2c_adapter *found;
807 struct i2c_client *client, *next;
765 808
766 /* First make sure that this adapter was ever added */ 809 /* First make sure that this adapter was ever added */
767 mutex_lock(&core_lock); 810 mutex_lock(&core_lock);
@@ -776,11 +819,21 @@ int i2c_del_adapter(struct i2c_adapter *adap)
776 /* Tell drivers about this removal */ 819 /* Tell drivers about this removal */
777 mutex_lock(&core_lock); 820 mutex_lock(&core_lock);
778 res = bus_for_each_drv(&i2c_bus_type, NULL, adap, 821 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
779 i2c_do_del_adapter); 822 __process_removed_adapter);
780 mutex_unlock(&core_lock); 823 mutex_unlock(&core_lock);
781 if (res) 824 if (res)
782 return res; 825 return res;
783 826
827 /* Remove devices instantiated from sysfs */
828 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
829 if (client->adapter == adap) {
830 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
831 client->name, client->addr);
832 list_del(&client->detected);
833 i2c_unregister_device(client);
834 }
835 }
836
784 /* Detach any active clients. This can't fail, thus we do not 837 /* Detach any active clients. This can't fail, thus we do not
785 checking the returned value. */ 838 checking the returned value. */
786 res = device_for_each_child(&adap->dev, NULL, __unregister_client); 839 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
@@ -815,22 +868,11 @@ EXPORT_SYMBOL(i2c_del_adapter);
815 868
816/* ------------------------------------------------------------------------- */ 869/* ------------------------------------------------------------------------- */
817 870
818static int __attach_adapter(struct device *dev, void *data) 871static int __process_new_driver(struct device *dev, void *data)
819{ 872{
820 struct i2c_adapter *adapter;
821 struct i2c_driver *driver = data;
822
823 if (dev->type != &i2c_adapter_type) 873 if (dev->type != &i2c_adapter_type)
824 return 0; 874 return 0;
825 adapter = to_i2c_adapter(dev); 875 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} 876}
835 877
836/* 878/*
@@ -862,40 +904,18 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
862 INIT_LIST_HEAD(&driver->clients); 904 INIT_LIST_HEAD(&driver->clients);
863 /* Walk the adapters that are already present */ 905 /* Walk the adapters that are already present */
864 mutex_lock(&core_lock); 906 mutex_lock(&core_lock);
865 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter); 907 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
866 mutex_unlock(&core_lock); 908 mutex_unlock(&core_lock);
867 909
868 return 0; 910 return 0;
869} 911}
870EXPORT_SYMBOL(i2c_register_driver); 912EXPORT_SYMBOL(i2c_register_driver);
871 913
872static int __detach_adapter(struct device *dev, void *data) 914static int __process_removed_driver(struct device *dev, void *data)
873{ 915{
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) 916 if (dev->type != &i2c_adapter_type)
879 return 0; 917 return 0;
880 adapter = to_i2c_adapter(dev); 918 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} 919}
900 920
901/** 921/**
@@ -906,7 +926,7 @@ static int __detach_adapter(struct device *dev, void *data)
906void i2c_del_driver(struct i2c_driver *driver) 926void i2c_del_driver(struct i2c_driver *driver)
907{ 927{
908 mutex_lock(&core_lock); 928 mutex_lock(&core_lock);
909 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter); 929 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
910 mutex_unlock(&core_lock); 930 mutex_unlock(&core_lock);
911 931
912 driver_unregister(&driver->driver); 932 driver_unregister(&driver->driver);
@@ -1081,12 +1101,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1081#endif 1101#endif
1082 1102
1083 if (in_atomic() || irqs_disabled()) { 1103 if (in_atomic() || irqs_disabled()) {
1084 ret = mutex_trylock(&adap->bus_lock); 1104 ret = rt_mutex_trylock(&adap->bus_lock);
1085 if (!ret) 1105 if (!ret)
1086 /* I2C activity is ongoing. */ 1106 /* I2C activity is ongoing. */
1087 return -EAGAIN; 1107 return -EAGAIN;
1088 } else { 1108 } else {
1089 mutex_lock_nested(&adap->bus_lock, adap->level); 1109 rt_mutex_lock(&adap->bus_lock);
1090 } 1110 }
1091 1111
1092 /* Retry automatically on arbitration loss */ 1112 /* Retry automatically on arbitration loss */
@@ -1098,7 +1118,7 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1098 if (time_after(jiffies, orig_jiffies + adap->timeout)) 1118 if (time_after(jiffies, orig_jiffies + adap->timeout))
1099 break; 1119 break;
1100 } 1120 }
1101 mutex_unlock(&adap->bus_lock); 1121 rt_mutex_unlock(&adap->bus_lock);
1102 1122
1103 return ret; 1123 return ret;
1104 } else { 1124 } else {
@@ -1169,7 +1189,7 @@ EXPORT_SYMBOL(i2c_master_recv);
1169 * ---------------------------------------------------- 1189 * ----------------------------------------------------
1170 */ 1190 */
1171 1191
1172static int i2c_detect_address(struct i2c_client *temp_client, int kind, 1192static int i2c_detect_address(struct i2c_client *temp_client,
1173 struct i2c_driver *driver) 1193 struct i2c_driver *driver)
1174{ 1194{
1175 struct i2c_board_info info; 1195 struct i2c_board_info info;
@@ -1188,22 +1208,18 @@ static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1188 if (i2c_check_addr(adapter, addr)) 1208 if (i2c_check_addr(adapter, addr))
1189 return 0; 1209 return 0;
1190 1210
1191 /* Make sure there is something at this address, unless forced */ 1211 /* Make sure there is something at this address */
1192 if (kind < 0) { 1212 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
1193 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, 1213 return 0;
1194 I2C_SMBUS_QUICK, NULL) < 0)
1195 return 0;
1196 1214
1197 /* prevent 24RF08 corruption */ 1215 /* Prevent 24RF08 corruption */
1198 if ((addr & ~0x0f) == 0x50) 1216 if ((addr & ~0x0f) == 0x50)
1199 i2c_smbus_xfer(adapter, addr, 0, 0, 0, 1217 i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
1200 I2C_SMBUS_QUICK, NULL);
1201 }
1202 1218
1203 /* Finally call the custom detection function */ 1219 /* Finally call the custom detection function */
1204 memset(&info, 0, sizeof(struct i2c_board_info)); 1220 memset(&info, 0, sizeof(struct i2c_board_info));
1205 info.addr = addr; 1221 info.addr = addr;
1206 err = driver->detect(temp_client, kind, &info); 1222 err = driver->detect(temp_client, &info);
1207 if (err) { 1223 if (err) {
1208 /* -ENODEV is returned if the detection fails. We catch it 1224 /* -ENODEV is returned if the detection fails. We catch it
1209 here as this isn't an error. */ 1225 here as this isn't an error. */
@@ -1233,13 +1249,13 @@ static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1233 1249
1234static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) 1250static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1235{ 1251{
1236 const struct i2c_client_address_data *address_data; 1252 const unsigned short *address_list;
1237 struct i2c_client *temp_client; 1253 struct i2c_client *temp_client;
1238 int i, err = 0; 1254 int i, err = 0;
1239 int adap_id = i2c_adapter_id(adapter); 1255 int adap_id = i2c_adapter_id(adapter);
1240 1256
1241 address_data = driver->address_data; 1257 address_list = driver->address_list;
1242 if (!driver->detect || !address_data) 1258 if (!driver->detect || !address_list)
1243 return 0; 1259 return 0;
1244 1260
1245 /* Set up a temporary client to help detect callback */ 1261 /* Set up a temporary client to help detect callback */
@@ -1248,40 +1264,13 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1248 return -ENOMEM; 1264 return -ENOMEM;
1249 temp_client->adapter = adapter; 1265 temp_client->adapter = adapter;
1250 1266
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 */ 1267 /* Stop here if the classes do not match */
1278 if (!(adapter->class & driver->class)) 1268 if (!(adapter->class & driver->class))
1279 goto exit_free; 1269 goto exit_free;
1280 1270
1281 /* Stop here if we can't use SMBUS_QUICK */ 1271 /* Stop here if we can't use SMBUS_QUICK */
1282 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { 1272 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1283 if (address_data->probe[0] == I2C_CLIENT_END 1273 if (address_list[0] == I2C_CLIENT_END)
1284 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1285 goto exit_free; 1274 goto exit_free;
1286 1275
1287 dev_warn(&adapter->dev, "SMBus Quick command not supported, " 1276 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
@@ -1290,48 +1279,11 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1290 goto exit_free; 1279 goto exit_free;
1291 } 1280 }
1292 1281
1293 /* Probe entries are done second, and are not affected by ignore 1282 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
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) {
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, " 1283 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1331 "addr 0x%02x\n", adap_id, 1284 "addr 0x%02x\n", adap_id, address_list[i]);
1332 address_data->normal_i2c[i]); 1285 temp_client->addr = address_list[i];
1333 temp_client->addr = address_data->normal_i2c[i]; 1286 err = i2c_detect_address(temp_client, driver);
1334 err = i2c_detect_address(temp_client, -1, driver);
1335 if (err) 1287 if (err)
1336 goto exit_free; 1288 goto exit_free;
1337 } 1289 }
@@ -1902,7 +1854,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1902 flags &= I2C_M_TEN | I2C_CLIENT_PEC; 1854 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1903 1855
1904 if (adapter->algo->smbus_xfer) { 1856 if (adapter->algo->smbus_xfer) {
1905 mutex_lock(&adapter->bus_lock); 1857 rt_mutex_lock(&adapter->bus_lock);
1906 1858
1907 /* Retry automatically on arbitration loss */ 1859 /* Retry automatically on arbitration loss */
1908 orig_jiffies = jiffies; 1860 orig_jiffies = jiffies;
@@ -1916,7 +1868,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1916 orig_jiffies + adapter->timeout)) 1868 orig_jiffies + adapter->timeout))
1917 break; 1869 break;
1918 } 1870 }
1919 mutex_unlock(&adapter->bus_lock); 1871 rt_mutex_unlock(&adapter->bus_lock);
1920 } else 1872 } else
1921 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, 1873 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1922 command, protocol, data); 1874 command, protocol, data);