diff options
Diffstat (limited to 'drivers/i2c/i2c-core.c')
-rw-r--r-- | drivers/i2c/i2c-core.c | 246 |
1 files changed, 204 insertions, 42 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 991d38daa87d..6b63cc7eb71e 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c | |||
@@ -27,7 +27,9 @@ | |||
27 | 27 | ||
28 | #include <linux/module.h> | 28 | #include <linux/module.h> |
29 | #include <linux/kernel.h> | 29 | #include <linux/kernel.h> |
30 | #include <linux/delay.h> | ||
30 | #include <linux/errno.h> | 31 | #include <linux/errno.h> |
32 | #include <linux/gpio.h> | ||
31 | #include <linux/slab.h> | 33 | #include <linux/slab.h> |
32 | #include <linux/i2c.h> | 34 | #include <linux/i2c.h> |
33 | #include <linux/init.h> | 35 | #include <linux/init.h> |
@@ -47,7 +49,7 @@ | |||
47 | 49 | ||
48 | /* core_lock protects i2c_adapter_idr, and guarantees | 50 | /* core_lock protects i2c_adapter_idr, and guarantees |
49 | that device detection, deletion of detected devices, and attach_adapter | 51 | that device detection, deletion of detected devices, and attach_adapter |
50 | and detach_adapter calls are serialized */ | 52 | calls are serialized */ |
51 | static DEFINE_MUTEX(core_lock); | 53 | static DEFINE_MUTEX(core_lock); |
52 | static DEFINE_IDR(i2c_adapter_idr); | 54 | static DEFINE_IDR(i2c_adapter_idr); |
53 | 55 | ||
@@ -91,7 +93,6 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv) | |||
91 | return 0; | 93 | return 0; |
92 | } | 94 | } |
93 | 95 | ||
94 | #ifdef CONFIG_HOTPLUG | ||
95 | 96 | ||
96 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ | 97 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ |
97 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) | 98 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
@@ -105,9 +106,129 @@ static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) | |||
105 | return 0; | 106 | return 0; |
106 | } | 107 | } |
107 | 108 | ||
108 | #else | 109 | /* i2c bus recovery routines */ |
109 | #define i2c_device_uevent NULL | 110 | static int get_scl_gpio_value(struct i2c_adapter *adap) |
110 | #endif /* CONFIG_HOTPLUG */ | 111 | { |
112 | return gpio_get_value(adap->bus_recovery_info->scl_gpio); | ||
113 | } | ||
114 | |||
115 | static void set_scl_gpio_value(struct i2c_adapter *adap, int val) | ||
116 | { | ||
117 | gpio_set_value(adap->bus_recovery_info->scl_gpio, val); | ||
118 | } | ||
119 | |||
120 | static int get_sda_gpio_value(struct i2c_adapter *adap) | ||
121 | { | ||
122 | return gpio_get_value(adap->bus_recovery_info->sda_gpio); | ||
123 | } | ||
124 | |||
125 | static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap) | ||
126 | { | ||
127 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | ||
128 | struct device *dev = &adap->dev; | ||
129 | int ret = 0; | ||
130 | |||
131 | ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN | | ||
132 | GPIOF_OUT_INIT_HIGH, "i2c-scl"); | ||
133 | if (ret) { | ||
134 | dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio); | ||
135 | return ret; | ||
136 | } | ||
137 | |||
138 | if (bri->get_sda) { | ||
139 | if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) { | ||
140 | /* work without SDA polling */ | ||
141 | dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n", | ||
142 | bri->sda_gpio); | ||
143 | bri->get_sda = NULL; | ||
144 | } | ||
145 | } | ||
146 | |||
147 | return ret; | ||
148 | } | ||
149 | |||
150 | static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap) | ||
151 | { | ||
152 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | ||
153 | |||
154 | if (bri->get_sda) | ||
155 | gpio_free(bri->sda_gpio); | ||
156 | |||
157 | gpio_free(bri->scl_gpio); | ||
158 | } | ||
159 | |||
160 | /* | ||
161 | * We are generating clock pulses. ndelay() determines durating of clk pulses. | ||
162 | * We will generate clock with rate 100 KHz and so duration of both clock levels | ||
163 | * is: delay in ns = (10^6 / 100) / 2 | ||
164 | */ | ||
165 | #define RECOVERY_NDELAY 5000 | ||
166 | #define RECOVERY_CLK_CNT 9 | ||
167 | |||
168 | static int i2c_generic_recovery(struct i2c_adapter *adap) | ||
169 | { | ||
170 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | ||
171 | int i = 0, val = 1, ret = 0; | ||
172 | |||
173 | if (bri->prepare_recovery) | ||
174 | bri->prepare_recovery(bri); | ||
175 | |||
176 | /* | ||
177 | * By this time SCL is high, as we need to give 9 falling-rising edges | ||
178 | */ | ||
179 | while (i++ < RECOVERY_CLK_CNT * 2) { | ||
180 | if (val) { | ||
181 | /* Break if SDA is high */ | ||
182 | if (bri->get_sda && bri->get_sda(adap)) | ||
183 | break; | ||
184 | /* SCL shouldn't be low here */ | ||
185 | if (!bri->get_scl(adap)) { | ||
186 | dev_err(&adap->dev, | ||
187 | "SCL is stuck low, exit recovery\n"); | ||
188 | ret = -EBUSY; | ||
189 | break; | ||
190 | } | ||
191 | } | ||
192 | |||
193 | val = !val; | ||
194 | bri->set_scl(adap, val); | ||
195 | ndelay(RECOVERY_NDELAY); | ||
196 | } | ||
197 | |||
198 | if (bri->unprepare_recovery) | ||
199 | bri->unprepare_recovery(bri); | ||
200 | |||
201 | return ret; | ||
202 | } | ||
203 | |||
204 | int i2c_generic_scl_recovery(struct i2c_adapter *adap) | ||
205 | { | ||
206 | adap->bus_recovery_info->set_scl(adap, 1); | ||
207 | return i2c_generic_recovery(adap); | ||
208 | } | ||
209 | |||
210 | int i2c_generic_gpio_recovery(struct i2c_adapter *adap) | ||
211 | { | ||
212 | int ret; | ||
213 | |||
214 | ret = i2c_get_gpios_for_recovery(adap); | ||
215 | if (ret) | ||
216 | return ret; | ||
217 | |||
218 | ret = i2c_generic_recovery(adap); | ||
219 | i2c_put_gpios_for_recovery(adap); | ||
220 | |||
221 | return ret; | ||
222 | } | ||
223 | |||
224 | int i2c_recover_bus(struct i2c_adapter *adap) | ||
225 | { | ||
226 | if (!adap->bus_recovery_info) | ||
227 | return -EOPNOTSUPP; | ||
228 | |||
229 | dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); | ||
230 | return adap->bus_recovery_info->recover_bus(adap); | ||
231 | } | ||
111 | 232 | ||
112 | static int i2c_device_probe(struct device *dev) | 233 | static int i2c_device_probe(struct device *dev) |
113 | { | 234 | { |
@@ -902,6 +1023,39 @@ static int i2c_register_adapter(struct i2c_adapter *adap) | |||
902 | "Failed to create compatibility class link\n"); | 1023 | "Failed to create compatibility class link\n"); |
903 | #endif | 1024 | #endif |
904 | 1025 | ||
1026 | /* bus recovery specific initialization */ | ||
1027 | if (adap->bus_recovery_info) { | ||
1028 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | ||
1029 | |||
1030 | if (!bri->recover_bus) { | ||
1031 | dev_err(&adap->dev, "No recover_bus() found, not using recovery\n"); | ||
1032 | adap->bus_recovery_info = NULL; | ||
1033 | goto exit_recovery; | ||
1034 | } | ||
1035 | |||
1036 | /* Generic GPIO recovery */ | ||
1037 | if (bri->recover_bus == i2c_generic_gpio_recovery) { | ||
1038 | if (!gpio_is_valid(bri->scl_gpio)) { | ||
1039 | dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n"); | ||
1040 | adap->bus_recovery_info = NULL; | ||
1041 | goto exit_recovery; | ||
1042 | } | ||
1043 | |||
1044 | if (gpio_is_valid(bri->sda_gpio)) | ||
1045 | bri->get_sda = get_sda_gpio_value; | ||
1046 | else | ||
1047 | bri->get_sda = NULL; | ||
1048 | |||
1049 | bri->get_scl = get_scl_gpio_value; | ||
1050 | bri->set_scl = set_scl_gpio_value; | ||
1051 | } else if (!bri->set_scl || !bri->get_scl) { | ||
1052 | /* Generic SCL recovery */ | ||
1053 | dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n"); | ||
1054 | adap->bus_recovery_info = NULL; | ||
1055 | } | ||
1056 | } | ||
1057 | |||
1058 | exit_recovery: | ||
905 | /* create pre-declared device nodes */ | 1059 | /* create pre-declared device nodes */ |
906 | if (adap->nr < __i2c_first_dynamic_bus_num) | 1060 | if (adap->nr < __i2c_first_dynamic_bus_num) |
907 | i2c_scan_static_board_info(adap); | 1061 | i2c_scan_static_board_info(adap); |
@@ -921,13 +1075,35 @@ out_list: | |||
921 | } | 1075 | } |
922 | 1076 | ||
923 | /** | 1077 | /** |
1078 | * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1 | ||
1079 | * @adap: the adapter to register (with adap->nr initialized) | ||
1080 | * Context: can sleep | ||
1081 | * | ||
1082 | * See i2c_add_numbered_adapter() for details. | ||
1083 | */ | ||
1084 | static int __i2c_add_numbered_adapter(struct i2c_adapter *adap) | ||
1085 | { | ||
1086 | int id; | ||
1087 | |||
1088 | mutex_lock(&core_lock); | ||
1089 | id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, | ||
1090 | GFP_KERNEL); | ||
1091 | mutex_unlock(&core_lock); | ||
1092 | if (id < 0) | ||
1093 | return id == -ENOSPC ? -EBUSY : id; | ||
1094 | |||
1095 | return i2c_register_adapter(adap); | ||
1096 | } | ||
1097 | |||
1098 | /** | ||
924 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number | 1099 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number |
925 | * @adapter: the adapter to add | 1100 | * @adapter: the adapter to add |
926 | * Context: can sleep | 1101 | * Context: can sleep |
927 | * | 1102 | * |
928 | * This routine is used to declare an I2C adapter when its bus number | 1103 | * This routine is used to declare an I2C adapter when its bus number |
929 | * doesn't matter. Examples: for I2C adapters dynamically added by | 1104 | * doesn't matter or when its bus number is specified by an dt alias. |
930 | * USB links or PCI plugin cards. | 1105 | * Examples of bases when the bus number doesn't matter: I2C adapters |
1106 | * dynamically added by USB links or PCI plugin cards. | ||
931 | * | 1107 | * |
932 | * When this returns zero, a new bus number was allocated and stored | 1108 | * When this returns zero, a new bus number was allocated and stored |
933 | * in adap->nr, and the specified adapter became available for clients. | 1109 | * in adap->nr, and the specified adapter became available for clients. |
@@ -935,8 +1111,17 @@ out_list: | |||
935 | */ | 1111 | */ |
936 | int i2c_add_adapter(struct i2c_adapter *adapter) | 1112 | int i2c_add_adapter(struct i2c_adapter *adapter) |
937 | { | 1113 | { |
1114 | struct device *dev = &adapter->dev; | ||
938 | int id; | 1115 | int id; |
939 | 1116 | ||
1117 | if (dev->of_node) { | ||
1118 | id = of_alias_get_id(dev->of_node, "i2c"); | ||
1119 | if (id >= 0) { | ||
1120 | adapter->nr = id; | ||
1121 | return __i2c_add_numbered_adapter(adapter); | ||
1122 | } | ||
1123 | } | ||
1124 | |||
940 | mutex_lock(&core_lock); | 1125 | mutex_lock(&core_lock); |
941 | id = idr_alloc(&i2c_adapter_idr, adapter, | 1126 | id = idr_alloc(&i2c_adapter_idr, adapter, |
942 | __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); | 1127 | __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); |
@@ -975,26 +1160,17 @@ EXPORT_SYMBOL(i2c_add_adapter); | |||
975 | */ | 1160 | */ |
976 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) | 1161 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) |
977 | { | 1162 | { |
978 | int id; | ||
979 | |||
980 | if (adap->nr == -1) /* -1 means dynamically assign bus id */ | 1163 | if (adap->nr == -1) /* -1 means dynamically assign bus id */ |
981 | return i2c_add_adapter(adap); | 1164 | return i2c_add_adapter(adap); |
982 | 1165 | ||
983 | mutex_lock(&core_lock); | 1166 | return __i2c_add_numbered_adapter(adap); |
984 | id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, | ||
985 | GFP_KERNEL); | ||
986 | mutex_unlock(&core_lock); | ||
987 | if (id < 0) | ||
988 | return id == -ENOSPC ? -EBUSY : id; | ||
989 | return i2c_register_adapter(adap); | ||
990 | } | 1167 | } |
991 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); | 1168 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); |
992 | 1169 | ||
993 | static int i2c_do_del_adapter(struct i2c_driver *driver, | 1170 | static void i2c_do_del_adapter(struct i2c_driver *driver, |
994 | struct i2c_adapter *adapter) | 1171 | struct i2c_adapter *adapter) |
995 | { | 1172 | { |
996 | struct i2c_client *client, *_n; | 1173 | struct i2c_client *client, *_n; |
997 | int res; | ||
998 | 1174 | ||
999 | /* Remove the devices we created ourselves as the result of hardware | 1175 | /* Remove the devices we created ourselves as the result of hardware |
1000 | * probing (using a driver's detect method) */ | 1176 | * probing (using a driver's detect method) */ |
@@ -1006,16 +1182,6 @@ static int i2c_do_del_adapter(struct i2c_driver *driver, | |||
1006 | i2c_unregister_device(client); | 1182 | i2c_unregister_device(client); |
1007 | } | 1183 | } |
1008 | } | 1184 | } |
1009 | |||
1010 | if (!driver->detach_adapter) | ||
1011 | return 0; | ||
1012 | dev_warn(&adapter->dev, "%s: detach_adapter method is deprecated\n", | ||
1013 | driver->driver.name); | ||
1014 | res = driver->detach_adapter(adapter); | ||
1015 | if (res) | ||
1016 | dev_err(&adapter->dev, "detach_adapter failed (%d) " | ||
1017 | "for driver [%s]\n", res, driver->driver.name); | ||
1018 | return res; | ||
1019 | } | 1185 | } |
1020 | 1186 | ||
1021 | static int __unregister_client(struct device *dev, void *dummy) | 1187 | static int __unregister_client(struct device *dev, void *dummy) |
@@ -1036,7 +1202,8 @@ static int __unregister_dummy(struct device *dev, void *dummy) | |||
1036 | 1202 | ||
1037 | static int __process_removed_adapter(struct device_driver *d, void *data) | 1203 | static int __process_removed_adapter(struct device_driver *d, void *data) |
1038 | { | 1204 | { |
1039 | return i2c_do_del_adapter(to_i2c_driver(d), data); | 1205 | i2c_do_del_adapter(to_i2c_driver(d), data); |
1206 | return 0; | ||
1040 | } | 1207 | } |
1041 | 1208 | ||
1042 | /** | 1209 | /** |
@@ -1047,9 +1214,8 @@ static int __process_removed_adapter(struct device_driver *d, void *data) | |||
1047 | * This unregisters an I2C adapter which was previously registered | 1214 | * This unregisters an I2C adapter which was previously registered |
1048 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. | 1215 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. |
1049 | */ | 1216 | */ |
1050 | int i2c_del_adapter(struct i2c_adapter *adap) | 1217 | void i2c_del_adapter(struct i2c_adapter *adap) |
1051 | { | 1218 | { |
1052 | int res = 0; | ||
1053 | struct i2c_adapter *found; | 1219 | struct i2c_adapter *found; |
1054 | struct i2c_client *client, *next; | 1220 | struct i2c_client *client, *next; |
1055 | 1221 | ||
@@ -1060,16 +1226,14 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
1060 | if (found != adap) { | 1226 | if (found != adap) { |
1061 | pr_debug("i2c-core: attempting to delete unregistered " | 1227 | pr_debug("i2c-core: attempting to delete unregistered " |
1062 | "adapter [%s]\n", adap->name); | 1228 | "adapter [%s]\n", adap->name); |
1063 | return -EINVAL; | 1229 | return; |
1064 | } | 1230 | } |
1065 | 1231 | ||
1066 | /* Tell drivers about this removal */ | 1232 | /* Tell drivers about this removal */ |
1067 | mutex_lock(&core_lock); | 1233 | mutex_lock(&core_lock); |
1068 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, | 1234 | bus_for_each_drv(&i2c_bus_type, NULL, adap, |
1069 | __process_removed_adapter); | 1235 | __process_removed_adapter); |
1070 | mutex_unlock(&core_lock); | 1236 | mutex_unlock(&core_lock); |
1071 | if (res) | ||
1072 | return res; | ||
1073 | 1237 | ||
1074 | /* Remove devices instantiated from sysfs */ | 1238 | /* Remove devices instantiated from sysfs */ |
1075 | mutex_lock_nested(&adap->userspace_clients_lock, | 1239 | mutex_lock_nested(&adap->userspace_clients_lock, |
@@ -1088,8 +1252,8 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
1088 | * we can't remove the dummy devices during the first pass: they | 1252 | * we can't remove the dummy devices during the first pass: they |
1089 | * could have been instantiated by real devices wishing to clean | 1253 | * could have been instantiated by real devices wishing to clean |
1090 | * them up properly, so we give them a chance to do that first. */ | 1254 | * them up properly, so we give them a chance to do that first. */ |
1091 | res = device_for_each_child(&adap->dev, NULL, __unregister_client); | 1255 | device_for_each_child(&adap->dev, NULL, __unregister_client); |
1092 | res = device_for_each_child(&adap->dev, NULL, __unregister_dummy); | 1256 | device_for_each_child(&adap->dev, NULL, __unregister_dummy); |
1093 | 1257 | ||
1094 | #ifdef CONFIG_I2C_COMPAT | 1258 | #ifdef CONFIG_I2C_COMPAT |
1095 | class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, | 1259 | class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, |
@@ -1114,8 +1278,6 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
1114 | /* Clear the device structure in case this adapter is ever going to be | 1278 | /* Clear the device structure in case this adapter is ever going to be |
1115 | added again */ | 1279 | added again */ |
1116 | memset(&adap->dev, 0, sizeof(adap->dev)); | 1280 | memset(&adap->dev, 0, sizeof(adap->dev)); |
1117 | |||
1118 | return 0; | ||
1119 | } | 1281 | } |
1120 | EXPORT_SYMBOL(i2c_del_adapter); | 1282 | EXPORT_SYMBOL(i2c_del_adapter); |
1121 | 1283 | ||
@@ -1185,9 +1347,9 @@ EXPORT_SYMBOL(i2c_register_driver); | |||
1185 | 1347 | ||
1186 | static int __process_removed_driver(struct device *dev, void *data) | 1348 | static int __process_removed_driver(struct device *dev, void *data) |
1187 | { | 1349 | { |
1188 | if (dev->type != &i2c_adapter_type) | 1350 | if (dev->type == &i2c_adapter_type) |
1189 | return 0; | 1351 | i2c_do_del_adapter(data, to_i2c_adapter(dev)); |
1190 | return i2c_do_del_adapter(data, to_i2c_adapter(dev)); | 1352 | return 0; |
1191 | } | 1353 | } |
1192 | 1354 | ||
1193 | /** | 1355 | /** |