]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* i2c-core.c - a device driver for the iic-bus interface */ |
2 | /* ------------------------------------------------------------------------- */ | |
3 | /* Copyright (C) 1995-99 Simon G. Vogl | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
5694f8a8 JD |
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
18 | MA 02110-1301 USA. */ | |
1da177e4 LT |
19 | /* ------------------------------------------------------------------------- */ |
20 | ||
96de0e25 | 21 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
1da177e4 | 22 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
421ef47b | 23 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and |
0826374b ML |
24 | Jean Delvare <khali@linux-fr.org> |
25 | Mux support by Rodolfo Giometti <giometti@enneenne.com> and | |
26 | Michael Lawnick <michael.lawnick.ext@nsn.com> */ | |
1da177e4 | 27 | |
1da177e4 LT |
28 | #include <linux/module.h> |
29 | #include <linux/kernel.h> | |
5f9296ba | 30 | #include <linux/delay.h> |
1da177e4 | 31 | #include <linux/errno.h> |
5f9296ba | 32 | #include <linux/gpio.h> |
1da177e4 LT |
33 | #include <linux/slab.h> |
34 | #include <linux/i2c.h> | |
35 | #include <linux/init.h> | |
36 | #include <linux/idr.h> | |
b3585e4f | 37 | #include <linux/mutex.h> |
959e85f7 | 38 | #include <linux/of_device.h> |
b8d6f45b | 39 | #include <linux/completion.h> |
cea443a8 MR |
40 | #include <linux/hardirq.h> |
41 | #include <linux/irqflags.h> | |
f18c41da | 42 | #include <linux/rwsem.h> |
6de468ae | 43 | #include <linux/pm_runtime.h> |
907ddf89 | 44 | #include <linux/acpi.h> |
1da177e4 LT |
45 | #include <asm/uaccess.h> |
46 | ||
9c1600ed DB |
47 | #include "i2c-core.h" |
48 | ||
1da177e4 | 49 | |
6629dcff | 50 | /* core_lock protects i2c_adapter_idr, and guarantees |
35fc37f8 | 51 | that device detection, deletion of detected devices, and attach_adapter |
19baba4c | 52 | calls are serialized */ |
caada32a | 53 | static DEFINE_MUTEX(core_lock); |
1da177e4 LT |
54 | static DEFINE_IDR(i2c_adapter_idr); |
55 | ||
4f8cf824 | 56 | static struct device_type i2c_client_type; |
4735c98f | 57 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); |
f37dd80a DB |
58 | |
59 | /* ------------------------------------------------------------------------- */ | |
60 | ||
d2653e92 JD |
61 | static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, |
62 | const struct i2c_client *client) | |
63 | { | |
64 | while (id->name[0]) { | |
65 | if (strcmp(client->name, id->name) == 0) | |
66 | return id; | |
67 | id++; | |
68 | } | |
69 | return NULL; | |
70 | } | |
71 | ||
1da177e4 LT |
72 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
73 | { | |
51298d12 JD |
74 | struct i2c_client *client = i2c_verify_client(dev); |
75 | struct i2c_driver *driver; | |
76 | ||
77 | if (!client) | |
78 | return 0; | |
7b4fbc50 | 79 | |
959e85f7 GL |
80 | /* Attempt an OF style match */ |
81 | if (of_driver_match_device(dev, drv)) | |
82 | return 1; | |
83 | ||
907ddf89 MW |
84 | /* Then ACPI style match */ |
85 | if (acpi_driver_match_device(dev, drv)) | |
86 | return 1; | |
87 | ||
51298d12 | 88 | driver = to_i2c_driver(drv); |
d2653e92 JD |
89 | /* match on an id table if there is one */ |
90 | if (driver->id_table) | |
91 | return i2c_match_id(driver->id_table, client) != NULL; | |
92 | ||
eb8a7908 | 93 | return 0; |
1da177e4 LT |
94 | } |
95 | ||
7b4fbc50 DB |
96 | #ifdef CONFIG_HOTPLUG |
97 | ||
98 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ | |
7eff2e7a | 99 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
7b4fbc50 DB |
100 | { |
101 | struct i2c_client *client = to_i2c_client(dev); | |
7b4fbc50 | 102 | |
eb8a7908 JD |
103 | if (add_uevent_var(env, "MODALIAS=%s%s", |
104 | I2C_MODULE_PREFIX, client->name)) | |
105 | return -ENOMEM; | |
7b4fbc50 DB |
106 | dev_dbg(dev, "uevent\n"); |
107 | return 0; | |
108 | } | |
109 | ||
110 | #else | |
111 | #define i2c_device_uevent NULL | |
112 | #endif /* CONFIG_HOTPLUG */ | |
113 | ||
5f9296ba VK |
114 | /* i2c bus recovery routines */ |
115 | static int get_scl_gpio_value(struct i2c_adapter *adap) | |
116 | { | |
117 | return gpio_get_value(adap->bus_recovery_info->scl_gpio); | |
118 | } | |
119 | ||
120 | static void set_scl_gpio_value(struct i2c_adapter *adap, int val) | |
121 | { | |
122 | gpio_set_value(adap->bus_recovery_info->scl_gpio, val); | |
123 | } | |
124 | ||
125 | static int get_sda_gpio_value(struct i2c_adapter *adap) | |
126 | { | |
127 | return gpio_get_value(adap->bus_recovery_info->sda_gpio); | |
128 | } | |
129 | ||
130 | static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap) | |
131 | { | |
132 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | |
133 | struct device *dev = &adap->dev; | |
134 | int ret = 0; | |
135 | ||
136 | ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN | | |
137 | GPIOF_OUT_INIT_HIGH, "i2c-scl"); | |
138 | if (ret) { | |
139 | dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio); | |
140 | return ret; | |
141 | } | |
142 | ||
143 | if (bri->get_sda) { | |
144 | if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) { | |
145 | /* work without SDA polling */ | |
146 | dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n", | |
147 | bri->sda_gpio); | |
148 | bri->get_sda = NULL; | |
149 | } | |
150 | } | |
151 | ||
152 | return ret; | |
153 | } | |
154 | ||
155 | static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap) | |
156 | { | |
157 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | |
158 | ||
159 | if (bri->get_sda) | |
160 | gpio_free(bri->sda_gpio); | |
161 | ||
162 | gpio_free(bri->scl_gpio); | |
163 | } | |
164 | ||
165 | /* | |
166 | * We are generating clock pulses. ndelay() determines durating of clk pulses. | |
167 | * We will generate clock with rate 100 KHz and so duration of both clock levels | |
168 | * is: delay in ns = (10^6 / 100) / 2 | |
169 | */ | |
170 | #define RECOVERY_NDELAY 5000 | |
171 | #define RECOVERY_CLK_CNT 9 | |
172 | ||
173 | static int i2c_generic_recovery(struct i2c_adapter *adap) | |
174 | { | |
175 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | |
176 | int i = 0, val = 1, ret = 0; | |
177 | ||
178 | if (bri->prepare_recovery) | |
179 | bri->prepare_recovery(bri); | |
180 | ||
181 | /* | |
182 | * By this time SCL is high, as we need to give 9 falling-rising edges | |
183 | */ | |
184 | while (i++ < RECOVERY_CLK_CNT * 2) { | |
185 | if (val) { | |
186 | /* Break if SDA is high */ | |
187 | if (bri->get_sda && bri->get_sda(adap)) | |
188 | break; | |
189 | /* SCL shouldn't be low here */ | |
190 | if (!bri->get_scl(adap)) { | |
191 | dev_err(&adap->dev, | |
192 | "SCL is stuck low, exit recovery\n"); | |
193 | ret = -EBUSY; | |
194 | break; | |
195 | } | |
196 | } | |
197 | ||
198 | val = !val; | |
199 | bri->set_scl(adap, val); | |
200 | ndelay(RECOVERY_NDELAY); | |
201 | } | |
202 | ||
203 | if (bri->unprepare_recovery) | |
204 | bri->unprepare_recovery(bri); | |
205 | ||
206 | return ret; | |
207 | } | |
208 | ||
209 | int i2c_generic_scl_recovery(struct i2c_adapter *adap) | |
210 | { | |
211 | adap->bus_recovery_info->set_scl(adap, 1); | |
212 | return i2c_generic_recovery(adap); | |
213 | } | |
214 | ||
215 | int i2c_generic_gpio_recovery(struct i2c_adapter *adap) | |
216 | { | |
217 | int ret; | |
218 | ||
219 | ret = i2c_get_gpios_for_recovery(adap); | |
220 | if (ret) | |
221 | return ret; | |
222 | ||
223 | ret = i2c_generic_recovery(adap); | |
224 | i2c_put_gpios_for_recovery(adap); | |
225 | ||
226 | return ret; | |
227 | } | |
228 | ||
229 | int i2c_recover_bus(struct i2c_adapter *adap) | |
230 | { | |
231 | if (!adap->bus_recovery_info) | |
232 | return -EOPNOTSUPP; | |
233 | ||
234 | dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); | |
235 | return adap->bus_recovery_info->recover_bus(adap); | |
236 | } | |
237 | ||
f37dd80a | 238 | static int i2c_device_probe(struct device *dev) |
1da177e4 | 239 | { |
51298d12 JD |
240 | struct i2c_client *client = i2c_verify_client(dev); |
241 | struct i2c_driver *driver; | |
50c3304a | 242 | int status; |
7b4fbc50 | 243 | |
51298d12 JD |
244 | if (!client) |
245 | return 0; | |
246 | ||
247 | driver = to_i2c_driver(dev->driver); | |
e0457442 | 248 | if (!driver->probe || !driver->id_table) |
7b4fbc50 DB |
249 | return -ENODEV; |
250 | client->driver = driver; | |
ee35425c MP |
251 | if (!device_can_wakeup(&client->dev)) |
252 | device_init_wakeup(&client->dev, | |
253 | client->flags & I2C_CLIENT_WAKE); | |
7b4fbc50 | 254 | dev_dbg(dev, "probe\n"); |
d2653e92 | 255 | |
e0457442 | 256 | status = driver->probe(client, i2c_match_id(driver->id_table, client)); |
e4a7b9b0 | 257 | if (status) { |
50c3304a | 258 | client->driver = NULL; |
e4a7b9b0 WS |
259 | i2c_set_clientdata(client, NULL); |
260 | } | |
50c3304a | 261 | return status; |
f37dd80a | 262 | } |
1da177e4 | 263 | |
f37dd80a DB |
264 | static int i2c_device_remove(struct device *dev) |
265 | { | |
51298d12 | 266 | struct i2c_client *client = i2c_verify_client(dev); |
a1d9e6e4 DB |
267 | struct i2c_driver *driver; |
268 | int status; | |
269 | ||
51298d12 | 270 | if (!client || !dev->driver) |
a1d9e6e4 DB |
271 | return 0; |
272 | ||
273 | driver = to_i2c_driver(dev->driver); | |
274 | if (driver->remove) { | |
275 | dev_dbg(dev, "remove\n"); | |
276 | status = driver->remove(client); | |
277 | } else { | |
278 | dev->driver = NULL; | |
279 | status = 0; | |
280 | } | |
e4a7b9b0 | 281 | if (status == 0) { |
a1d9e6e4 | 282 | client->driver = NULL; |
e4a7b9b0 WS |
283 | i2c_set_clientdata(client, NULL); |
284 | } | |
a1d9e6e4 | 285 | return status; |
1da177e4 LT |
286 | } |
287 | ||
f37dd80a | 288 | static void i2c_device_shutdown(struct device *dev) |
1da177e4 | 289 | { |
51298d12 | 290 | struct i2c_client *client = i2c_verify_client(dev); |
f37dd80a DB |
291 | struct i2c_driver *driver; |
292 | ||
51298d12 | 293 | if (!client || !dev->driver) |
f37dd80a DB |
294 | return; |
295 | driver = to_i2c_driver(dev->driver); | |
296 | if (driver->shutdown) | |
51298d12 | 297 | driver->shutdown(client); |
1da177e4 LT |
298 | } |
299 | ||
2f60ba70 RW |
300 | #ifdef CONFIG_PM_SLEEP |
301 | static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg) | |
54067ee2 | 302 | { |
2f60ba70 RW |
303 | struct i2c_client *client = i2c_verify_client(dev); |
304 | struct i2c_driver *driver; | |
54067ee2 | 305 | |
2f60ba70 | 306 | if (!client || !dev->driver) |
54067ee2 | 307 | return 0; |
2f60ba70 RW |
308 | driver = to_i2c_driver(dev->driver); |
309 | if (!driver->suspend) | |
54067ee2 | 310 | return 0; |
2f60ba70 | 311 | return driver->suspend(client, mesg); |
54067ee2 | 312 | } |
313 | ||
2f60ba70 | 314 | static int i2c_legacy_resume(struct device *dev) |
54067ee2 | 315 | { |
2f60ba70 RW |
316 | struct i2c_client *client = i2c_verify_client(dev); |
317 | struct i2c_driver *driver; | |
54067ee2 | 318 | |
2f60ba70 | 319 | if (!client || !dev->driver) |
54067ee2 | 320 | return 0; |
2f60ba70 RW |
321 | driver = to_i2c_driver(dev->driver); |
322 | if (!driver->resume) | |
54067ee2 | 323 | return 0; |
2f60ba70 | 324 | return driver->resume(client); |
54067ee2 | 325 | } |
54067ee2 | 326 | |
2f60ba70 | 327 | static int i2c_device_pm_suspend(struct device *dev) |
6de468ae | 328 | { |
2f60ba70 | 329 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
6de468ae | 330 | |
d529de29 MB |
331 | if (pm) |
332 | return pm_generic_suspend(dev); | |
333 | else | |
334 | return i2c_legacy_suspend(dev, PMSG_SUSPEND); | |
6de468ae MB |
335 | } |
336 | ||
2f60ba70 | 337 | static int i2c_device_pm_resume(struct device *dev) |
6de468ae | 338 | { |
2f60ba70 | 339 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
6de468ae | 340 | |
2f60ba70 | 341 | if (pm) |
d529de29 | 342 | return pm_generic_resume(dev); |
2f60ba70 | 343 | else |
d529de29 | 344 | return i2c_legacy_resume(dev); |
6de468ae | 345 | } |
6de468ae | 346 | |
2f60ba70 | 347 | static int i2c_device_pm_freeze(struct device *dev) |
1da177e4 | 348 | { |
2f60ba70 | 349 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
f37dd80a | 350 | |
d529de29 MB |
351 | if (pm) |
352 | return pm_generic_freeze(dev); | |
353 | else | |
354 | return i2c_legacy_suspend(dev, PMSG_FREEZE); | |
1da177e4 LT |
355 | } |
356 | ||
2f60ba70 | 357 | static int i2c_device_pm_thaw(struct device *dev) |
1da177e4 | 358 | { |
2f60ba70 | 359 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
f37dd80a | 360 | |
d529de29 MB |
361 | if (pm) |
362 | return pm_generic_thaw(dev); | |
363 | else | |
364 | return i2c_legacy_resume(dev); | |
2f60ba70 RW |
365 | } |
366 | ||
367 | static int i2c_device_pm_poweroff(struct device *dev) | |
368 | { | |
369 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | |
370 | ||
d529de29 MB |
371 | if (pm) |
372 | return pm_generic_poweroff(dev); | |
373 | else | |
374 | return i2c_legacy_suspend(dev, PMSG_HIBERNATE); | |
2f60ba70 RW |
375 | } |
376 | ||
377 | static int i2c_device_pm_restore(struct device *dev) | |
378 | { | |
379 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | |
2f60ba70 RW |
380 | |
381 | if (pm) | |
d529de29 | 382 | return pm_generic_restore(dev); |
2f60ba70 | 383 | else |
d529de29 | 384 | return i2c_legacy_resume(dev); |
1da177e4 | 385 | } |
2f60ba70 RW |
386 | #else /* !CONFIG_PM_SLEEP */ |
387 | #define i2c_device_pm_suspend NULL | |
388 | #define i2c_device_pm_resume NULL | |
389 | #define i2c_device_pm_freeze NULL | |
390 | #define i2c_device_pm_thaw NULL | |
391 | #define i2c_device_pm_poweroff NULL | |
392 | #define i2c_device_pm_restore NULL | |
393 | #endif /* !CONFIG_PM_SLEEP */ | |
1da177e4 | 394 | |
9c1600ed DB |
395 | static void i2c_client_dev_release(struct device *dev) |
396 | { | |
397 | kfree(to_i2c_client(dev)); | |
398 | } | |
399 | ||
09b8ce0a | 400 | static ssize_t |
4f8cf824 | 401 | show_name(struct device *dev, struct device_attribute *attr, char *buf) |
7b4fbc50 | 402 | { |
4f8cf824 JD |
403 | return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? |
404 | to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); | |
7b4fbc50 DB |
405 | } |
406 | ||
09b8ce0a ZX |
407 | static ssize_t |
408 | show_modalias(struct device *dev, struct device_attribute *attr, char *buf) | |
7b4fbc50 DB |
409 | { |
410 | struct i2c_client *client = to_i2c_client(dev); | |
eb8a7908 | 411 | return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); |
7b4fbc50 DB |
412 | } |
413 | ||
4f8cf824 | 414 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
51298d12 JD |
415 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
416 | ||
417 | static struct attribute *i2c_dev_attrs[] = { | |
418 | &dev_attr_name.attr, | |
7b4fbc50 | 419 | /* modalias helps coldplug: modprobe $(cat .../modalias) */ |
51298d12 JD |
420 | &dev_attr_modalias.attr, |
421 | NULL | |
422 | }; | |
423 | ||
424 | static struct attribute_group i2c_dev_attr_group = { | |
425 | .attrs = i2c_dev_attrs, | |
426 | }; | |
427 | ||
428 | static const struct attribute_group *i2c_dev_attr_groups[] = { | |
429 | &i2c_dev_attr_group, | |
430 | NULL | |
7b4fbc50 DB |
431 | }; |
432 | ||
0b2c3688 | 433 | static const struct dev_pm_ops i2c_device_pm_ops = { |
54067ee2 | 434 | .suspend = i2c_device_pm_suspend, |
435 | .resume = i2c_device_pm_resume, | |
2f60ba70 RW |
436 | .freeze = i2c_device_pm_freeze, |
437 | .thaw = i2c_device_pm_thaw, | |
438 | .poweroff = i2c_device_pm_poweroff, | |
439 | .restore = i2c_device_pm_restore, | |
440 | SET_RUNTIME_PM_OPS( | |
441 | pm_generic_runtime_suspend, | |
442 | pm_generic_runtime_resume, | |
443 | pm_generic_runtime_idle | |
444 | ) | |
54067ee2 | 445 | }; |
446 | ||
e9ca9eb9 | 447 | struct bus_type i2c_bus_type = { |
f37dd80a DB |
448 | .name = "i2c", |
449 | .match = i2c_device_match, | |
450 | .probe = i2c_device_probe, | |
451 | .remove = i2c_device_remove, | |
452 | .shutdown = i2c_device_shutdown, | |
54067ee2 | 453 | .pm = &i2c_device_pm_ops, |
b864c7d5 | 454 | }; |
e9ca9eb9 | 455 | EXPORT_SYMBOL_GPL(i2c_bus_type); |
b864c7d5 | 456 | |
51298d12 JD |
457 | static struct device_type i2c_client_type = { |
458 | .groups = i2c_dev_attr_groups, | |
459 | .uevent = i2c_device_uevent, | |
460 | .release = i2c_client_dev_release, | |
461 | }; | |
462 | ||
9b766b81 DB |
463 | |
464 | /** | |
465 | * i2c_verify_client - return parameter as i2c_client, or NULL | |
466 | * @dev: device, probably from some driver model iterator | |
467 | * | |
468 | * When traversing the driver model tree, perhaps using driver model | |
469 | * iterators like @device_for_each_child(), you can't assume very much | |
470 | * about the nodes you find. Use this function to avoid oopses caused | |
471 | * by wrongly treating some non-I2C device as an i2c_client. | |
472 | */ | |
473 | struct i2c_client *i2c_verify_client(struct device *dev) | |
474 | { | |
51298d12 | 475 | return (dev->type == &i2c_client_type) |
9b766b81 DB |
476 | ? to_i2c_client(dev) |
477 | : NULL; | |
478 | } | |
479 | EXPORT_SYMBOL(i2c_verify_client); | |
480 | ||
481 | ||
3a89db5f | 482 | /* This is a permissive address validity check, I2C address map constraints |
25985edc | 483 | * are purposely not enforced, except for the general call address. */ |
3a89db5f JD |
484 | static int i2c_check_client_addr_validity(const struct i2c_client *client) |
485 | { | |
486 | if (client->flags & I2C_CLIENT_TEN) { | |
487 | /* 10-bit address, all values are valid */ | |
488 | if (client->addr > 0x3ff) | |
489 | return -EINVAL; | |
490 | } else { | |
491 | /* 7-bit address, reject the general call address */ | |
492 | if (client->addr == 0x00 || client->addr > 0x7f) | |
493 | return -EINVAL; | |
494 | } | |
495 | return 0; | |
496 | } | |
497 | ||
656b8761 JD |
498 | /* And this is a strict address validity check, used when probing. If a |
499 | * device uses a reserved address, then it shouldn't be probed. 7-bit | |
500 | * addressing is assumed, 10-bit address devices are rare and should be | |
501 | * explicitly enumerated. */ | |
502 | static int i2c_check_addr_validity(unsigned short addr) | |
503 | { | |
504 | /* | |
505 | * Reserved addresses per I2C specification: | |
506 | * 0x00 General call address / START byte | |
507 | * 0x01 CBUS address | |
508 | * 0x02 Reserved for different bus format | |
509 | * 0x03 Reserved for future purposes | |
510 | * 0x04-0x07 Hs-mode master code | |
511 | * 0x78-0x7b 10-bit slave addressing | |
512 | * 0x7c-0x7f Reserved for future purposes | |
513 | */ | |
514 | if (addr < 0x08 || addr > 0x77) | |
515 | return -EINVAL; | |
516 | return 0; | |
517 | } | |
518 | ||
3b5f794b JD |
519 | static int __i2c_check_addr_busy(struct device *dev, void *addrp) |
520 | { | |
521 | struct i2c_client *client = i2c_verify_client(dev); | |
522 | int addr = *(int *)addrp; | |
523 | ||
524 | if (client && client->addr == addr) | |
525 | return -EBUSY; | |
526 | return 0; | |
527 | } | |
528 | ||
0826374b ML |
529 | /* walk up mux tree */ |
530 | static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr) | |
531 | { | |
97cc4d49 | 532 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
0826374b ML |
533 | int result; |
534 | ||
535 | result = device_for_each_child(&adapter->dev, &addr, | |
536 | __i2c_check_addr_busy); | |
537 | ||
97cc4d49 JD |
538 | if (!result && parent) |
539 | result = i2c_check_mux_parents(parent, addr); | |
0826374b ML |
540 | |
541 | return result; | |
542 | } | |
543 | ||
544 | /* recurse down mux tree */ | |
545 | static int i2c_check_mux_children(struct device *dev, void *addrp) | |
546 | { | |
547 | int result; | |
548 | ||
549 | if (dev->type == &i2c_adapter_type) | |
550 | result = device_for_each_child(dev, addrp, | |
551 | i2c_check_mux_children); | |
552 | else | |
553 | result = __i2c_check_addr_busy(dev, addrp); | |
554 | ||
555 | return result; | |
556 | } | |
557 | ||
3b5f794b JD |
558 | static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) |
559 | { | |
97cc4d49 | 560 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
0826374b ML |
561 | int result = 0; |
562 | ||
97cc4d49 JD |
563 | if (parent) |
564 | result = i2c_check_mux_parents(parent, addr); | |
0826374b ML |
565 | |
566 | if (!result) | |
567 | result = device_for_each_child(&adapter->dev, &addr, | |
568 | i2c_check_mux_children); | |
569 | ||
570 | return result; | |
3b5f794b JD |
571 | } |
572 | ||
fe61e07e JD |
573 | /** |
574 | * i2c_lock_adapter - Get exclusive access to an I2C bus segment | |
575 | * @adapter: Target I2C bus segment | |
576 | */ | |
577 | void i2c_lock_adapter(struct i2c_adapter *adapter) | |
578 | { | |
97cc4d49 JD |
579 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
580 | ||
581 | if (parent) | |
582 | i2c_lock_adapter(parent); | |
0826374b ML |
583 | else |
584 | rt_mutex_lock(&adapter->bus_lock); | |
fe61e07e JD |
585 | } |
586 | EXPORT_SYMBOL_GPL(i2c_lock_adapter); | |
587 | ||
588 | /** | |
589 | * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment | |
590 | * @adapter: Target I2C bus segment | |
591 | */ | |
592 | static int i2c_trylock_adapter(struct i2c_adapter *adapter) | |
593 | { | |
97cc4d49 JD |
594 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
595 | ||
596 | if (parent) | |
597 | return i2c_trylock_adapter(parent); | |
0826374b ML |
598 | else |
599 | return rt_mutex_trylock(&adapter->bus_lock); | |
fe61e07e JD |
600 | } |
601 | ||
602 | /** | |
603 | * i2c_unlock_adapter - Release exclusive access to an I2C bus segment | |
604 | * @adapter: Target I2C bus segment | |
605 | */ | |
606 | void i2c_unlock_adapter(struct i2c_adapter *adapter) | |
607 | { | |
97cc4d49 JD |
608 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
609 | ||
610 | if (parent) | |
611 | i2c_unlock_adapter(parent); | |
0826374b ML |
612 | else |
613 | rt_mutex_unlock(&adapter->bus_lock); | |
fe61e07e JD |
614 | } |
615 | EXPORT_SYMBOL_GPL(i2c_unlock_adapter); | |
616 | ||
9c1600ed | 617 | /** |
f8a227e8 | 618 | * i2c_new_device - instantiate an i2c device |
9c1600ed DB |
619 | * @adap: the adapter managing the device |
620 | * @info: describes one I2C device; bus_num is ignored | |
d64f73be | 621 | * Context: can sleep |
9c1600ed | 622 | * |
f8a227e8 JD |
623 | * Create an i2c device. Binding is handled through driver model |
624 | * probe()/remove() methods. A driver may be bound to this device when we | |
625 | * return from this function, or any later moment (e.g. maybe hotplugging will | |
626 | * load the driver module). This call is not appropriate for use by mainboard | |
627 | * initialization logic, which usually runs during an arch_initcall() long | |
628 | * before any i2c_adapter could exist. | |
9c1600ed DB |
629 | * |
630 | * This returns the new i2c client, which may be saved for later use with | |
631 | * i2c_unregister_device(); or NULL to indicate an error. | |
632 | */ | |
633 | struct i2c_client * | |
634 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) | |
635 | { | |
636 | struct i2c_client *client; | |
637 | int status; | |
638 | ||
639 | client = kzalloc(sizeof *client, GFP_KERNEL); | |
640 | if (!client) | |
641 | return NULL; | |
642 | ||
643 | client->adapter = adap; | |
644 | ||
645 | client->dev.platform_data = info->platform_data; | |
3bbb835d | 646 | |
11f1f2af AV |
647 | if (info->archdata) |
648 | client->dev.archdata = *info->archdata; | |
649 | ||
ee35425c | 650 | client->flags = info->flags; |
9c1600ed DB |
651 | client->addr = info->addr; |
652 | client->irq = info->irq; | |
653 | ||
9c1600ed DB |
654 | strlcpy(client->name, info->type, sizeof(client->name)); |
655 | ||
3a89db5f JD |
656 | /* Check for address validity */ |
657 | status = i2c_check_client_addr_validity(client); | |
658 | if (status) { | |
659 | dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", | |
660 | client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); | |
661 | goto out_err_silent; | |
662 | } | |
663 | ||
f8a227e8 | 664 | /* Check for address business */ |
3b5f794b | 665 | status = i2c_check_addr_busy(adap, client->addr); |
f8a227e8 JD |
666 | if (status) |
667 | goto out_err; | |
668 | ||
669 | client->dev.parent = &client->adapter->dev; | |
670 | client->dev.bus = &i2c_bus_type; | |
51298d12 | 671 | client->dev.type = &i2c_client_type; |
d12d42f7 | 672 | client->dev.of_node = info->of_node; |
907ddf89 | 673 | ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle); |
f8a227e8 | 674 | |
cbb44514 | 675 | /* For 10-bit clients, add an arbitrary offset to avoid collisions */ |
f8a227e8 | 676 | dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), |
cbb44514 JD |
677 | client->addr | ((client->flags & I2C_CLIENT_TEN) |
678 | ? 0xa000 : 0)); | |
f8a227e8 JD |
679 | status = device_register(&client->dev); |
680 | if (status) | |
681 | goto out_err; | |
682 | ||
f8a227e8 JD |
683 | dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", |
684 | client->name, dev_name(&client->dev)); | |
685 | ||
9c1600ed | 686 | return client; |
f8a227e8 JD |
687 | |
688 | out_err: | |
689 | dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x " | |
690 | "(%d)\n", client->name, client->addr, status); | |
3a89db5f | 691 | out_err_silent: |
f8a227e8 JD |
692 | kfree(client); |
693 | return NULL; | |
9c1600ed DB |
694 | } |
695 | EXPORT_SYMBOL_GPL(i2c_new_device); | |
696 | ||
697 | ||
698 | /** | |
699 | * i2c_unregister_device - reverse effect of i2c_new_device() | |
700 | * @client: value returned from i2c_new_device() | |
d64f73be | 701 | * Context: can sleep |
9c1600ed DB |
702 | */ |
703 | void i2c_unregister_device(struct i2c_client *client) | |
a1d9e6e4 | 704 | { |
a1d9e6e4 DB |
705 | device_unregister(&client->dev); |
706 | } | |
9c1600ed | 707 | EXPORT_SYMBOL_GPL(i2c_unregister_device); |
a1d9e6e4 DB |
708 | |
709 | ||
60b129d7 JD |
710 | static const struct i2c_device_id dummy_id[] = { |
711 | { "dummy", 0 }, | |
712 | { }, | |
713 | }; | |
714 | ||
d2653e92 JD |
715 | static int dummy_probe(struct i2c_client *client, |
716 | const struct i2c_device_id *id) | |
717 | { | |
718 | return 0; | |
719 | } | |
720 | ||
721 | static int dummy_remove(struct i2c_client *client) | |
e9f1373b DB |
722 | { |
723 | return 0; | |
724 | } | |
725 | ||
726 | static struct i2c_driver dummy_driver = { | |
727 | .driver.name = "dummy", | |
d2653e92 JD |
728 | .probe = dummy_probe, |
729 | .remove = dummy_remove, | |
60b129d7 | 730 | .id_table = dummy_id, |
e9f1373b DB |
731 | }; |
732 | ||
733 | /** | |
734 | * i2c_new_dummy - return a new i2c device bound to a dummy driver | |
735 | * @adapter: the adapter managing the device | |
736 | * @address: seven bit address to be used | |
e9f1373b DB |
737 | * Context: can sleep |
738 | * | |
739 | * This returns an I2C client bound to the "dummy" driver, intended for use | |
740 | * with devices that consume multiple addresses. Examples of such chips | |
741 | * include various EEPROMS (like 24c04 and 24c08 models). | |
742 | * | |
743 | * These dummy devices have two main uses. First, most I2C and SMBus calls | |
744 | * except i2c_transfer() need a client handle; the dummy will be that handle. | |
745 | * And second, this prevents the specified address from being bound to a | |
746 | * different driver. | |
747 | * | |
748 | * This returns the new i2c client, which should be saved for later use with | |
749 | * i2c_unregister_device(); or NULL to indicate an error. | |
750 | */ | |
09b8ce0a | 751 | struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) |
e9f1373b DB |
752 | { |
753 | struct i2c_board_info info = { | |
60b129d7 | 754 | I2C_BOARD_INFO("dummy", address), |
e9f1373b DB |
755 | }; |
756 | ||
e9f1373b DB |
757 | return i2c_new_device(adapter, &info); |
758 | } | |
759 | EXPORT_SYMBOL_GPL(i2c_new_dummy); | |
760 | ||
f37dd80a DB |
761 | /* ------------------------------------------------------------------------- */ |
762 | ||
16ffadfc DB |
763 | /* I2C bus adapters -- one roots each I2C or SMBUS segment */ |
764 | ||
83eaaed0 | 765 | static void i2c_adapter_dev_release(struct device *dev) |
1da177e4 | 766 | { |
ef2c8321 | 767 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
1da177e4 LT |
768 | complete(&adap->dev_released); |
769 | } | |
770 | ||
390946b1 JD |
771 | /* |
772 | * This function is only needed for mutex_lock_nested, so it is never | |
773 | * called unless locking correctness checking is enabled. Thus we | |
774 | * make it inline to avoid a compiler warning. That's what gcc ends up | |
775 | * doing anyway. | |
776 | */ | |
777 | static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter) | |
778 | { | |
779 | unsigned int depth = 0; | |
780 | ||
781 | while ((adapter = i2c_parent_is_i2c_adapter(adapter))) | |
782 | depth++; | |
783 | ||
784 | return depth; | |
785 | } | |
786 | ||
99cd8e25 JD |
787 | /* |
788 | * Let users instantiate I2C devices through sysfs. This can be used when | |
789 | * platform initialization code doesn't contain the proper data for | |
790 | * whatever reason. Also useful for drivers that do device detection and | |
791 | * detection fails, either because the device uses an unexpected address, | |
792 | * or this is a compatible device with different ID register values. | |
793 | * | |
794 | * Parameter checking may look overzealous, but we really don't want | |
795 | * the user to provide incorrect parameters. | |
796 | */ | |
797 | static ssize_t | |
798 | i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, | |
799 | const char *buf, size_t count) | |
800 | { | |
801 | struct i2c_adapter *adap = to_i2c_adapter(dev); | |
802 | struct i2c_board_info info; | |
803 | struct i2c_client *client; | |
804 | char *blank, end; | |
805 | int res; | |
806 | ||
99cd8e25 JD |
807 | memset(&info, 0, sizeof(struct i2c_board_info)); |
808 | ||
809 | blank = strchr(buf, ' '); | |
810 | if (!blank) { | |
811 | dev_err(dev, "%s: Missing parameters\n", "new_device"); | |
812 | return -EINVAL; | |
813 | } | |
814 | if (blank - buf > I2C_NAME_SIZE - 1) { | |
815 | dev_err(dev, "%s: Invalid device name\n", "new_device"); | |
816 | return -EINVAL; | |
817 | } | |
818 | memcpy(info.type, buf, blank - buf); | |
819 | ||
820 | /* Parse remaining parameters, reject extra parameters */ | |
821 | res = sscanf(++blank, "%hi%c", &info.addr, &end); | |
822 | if (res < 1) { | |
823 | dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); | |
824 | return -EINVAL; | |
825 | } | |
826 | if (res > 1 && end != '\n') { | |
827 | dev_err(dev, "%s: Extra parameters\n", "new_device"); | |
828 | return -EINVAL; | |
829 | } | |
830 | ||
99cd8e25 JD |
831 | client = i2c_new_device(adap, &info); |
832 | if (!client) | |
3a89db5f | 833 | return -EINVAL; |
99cd8e25 JD |
834 | |
835 | /* Keep track of the added device */ | |
dafc50d1 | 836 | mutex_lock(&adap->userspace_clients_lock); |
6629dcff | 837 | list_add_tail(&client->detected, &adap->userspace_clients); |
dafc50d1 | 838 | mutex_unlock(&adap->userspace_clients_lock); |
99cd8e25 JD |
839 | dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", |
840 | info.type, info.addr); | |
841 | ||
842 | return count; | |
843 | } | |
844 | ||
845 | /* | |
846 | * And of course let the users delete the devices they instantiated, if | |
847 | * they got it wrong. This interface can only be used to delete devices | |
848 | * instantiated by i2c_sysfs_new_device above. This guarantees that we | |
849 | * don't delete devices to which some kernel code still has references. | |
850 | * | |
851 | * Parameter checking may look overzealous, but we really don't want | |
852 | * the user to delete the wrong device. | |
853 | */ | |
854 | static ssize_t | |
855 | i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, | |
856 | const char *buf, size_t count) | |
857 | { | |
858 | struct i2c_adapter *adap = to_i2c_adapter(dev); | |
859 | struct i2c_client *client, *next; | |
860 | unsigned short addr; | |
861 | char end; | |
862 | int res; | |
863 | ||
864 | /* Parse parameters, reject extra parameters */ | |
865 | res = sscanf(buf, "%hi%c", &addr, &end); | |
866 | if (res < 1) { | |
867 | dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); | |
868 | return -EINVAL; | |
869 | } | |
870 | if (res > 1 && end != '\n') { | |
871 | dev_err(dev, "%s: Extra parameters\n", "delete_device"); | |
872 | return -EINVAL; | |
873 | } | |
874 | ||
875 | /* Make sure the device was added through sysfs */ | |
876 | res = -ENOENT; | |
390946b1 JD |
877 | mutex_lock_nested(&adap->userspace_clients_lock, |
878 | i2c_adapter_depth(adap)); | |
6629dcff JD |
879 | list_for_each_entry_safe(client, next, &adap->userspace_clients, |
880 | detected) { | |
881 | if (client->addr == addr) { | |
99cd8e25 JD |
882 | dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", |
883 | "delete_device", client->name, client->addr); | |
884 | ||
885 | list_del(&client->detected); | |
886 | i2c_unregister_device(client); | |
887 | res = count; | |
888 | break; | |
889 | } | |
890 | } | |
dafc50d1 | 891 | mutex_unlock(&adap->userspace_clients_lock); |
99cd8e25 JD |
892 | |
893 | if (res < 0) | |
894 | dev_err(dev, "%s: Can't find device in list\n", | |
895 | "delete_device"); | |
896 | return res; | |
897 | } | |
898 | ||
4f8cf824 JD |
899 | static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device); |
900 | static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device); | |
901 | ||
902 | static struct attribute *i2c_adapter_attrs[] = { | |
903 | &dev_attr_name.attr, | |
904 | &dev_attr_new_device.attr, | |
905 | &dev_attr_delete_device.attr, | |
906 | NULL | |
907 | }; | |
908 | ||
909 | static struct attribute_group i2c_adapter_attr_group = { | |
910 | .attrs = i2c_adapter_attrs, | |
911 | }; | |
912 | ||
913 | static const struct attribute_group *i2c_adapter_attr_groups[] = { | |
914 | &i2c_adapter_attr_group, | |
915 | NULL | |
16ffadfc | 916 | }; |
b119dc3f | 917 | |
0826374b | 918 | struct device_type i2c_adapter_type = { |
4f8cf824 JD |
919 | .groups = i2c_adapter_attr_groups, |
920 | .release = i2c_adapter_dev_release, | |
1da177e4 | 921 | }; |
0826374b | 922 | EXPORT_SYMBOL_GPL(i2c_adapter_type); |
1da177e4 | 923 | |
643dd09e SW |
924 | /** |
925 | * i2c_verify_adapter - return parameter as i2c_adapter or NULL | |
926 | * @dev: device, probably from some driver model iterator | |
927 | * | |
928 | * When traversing the driver model tree, perhaps using driver model | |
929 | * iterators like @device_for_each_child(), you can't assume very much | |
930 | * about the nodes you find. Use this function to avoid oopses caused | |
931 | * by wrongly treating some non-I2C device as an i2c_adapter. | |
932 | */ | |
933 | struct i2c_adapter *i2c_verify_adapter(struct device *dev) | |
934 | { | |
935 | return (dev->type == &i2c_adapter_type) | |
936 | ? to_i2c_adapter(dev) | |
937 | : NULL; | |
938 | } | |
939 | EXPORT_SYMBOL(i2c_verify_adapter); | |
940 | ||
2bb5095a JD |
941 | #ifdef CONFIG_I2C_COMPAT |
942 | static struct class_compat *i2c_adapter_compat_class; | |
943 | #endif | |
944 | ||
9c1600ed DB |
945 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) |
946 | { | |
947 | struct i2c_devinfo *devinfo; | |
948 | ||
f18c41da | 949 | down_read(&__i2c_board_lock); |
9c1600ed DB |
950 | list_for_each_entry(devinfo, &__i2c_board_list, list) { |
951 | if (devinfo->busnum == adapter->nr | |
952 | && !i2c_new_device(adapter, | |
953 | &devinfo->board_info)) | |
09b8ce0a ZX |
954 | dev_err(&adapter->dev, |
955 | "Can't create device at 0x%02x\n", | |
9c1600ed DB |
956 | devinfo->board_info.addr); |
957 | } | |
f18c41da | 958 | up_read(&__i2c_board_lock); |
9c1600ed DB |
959 | } |
960 | ||
69b0089a JD |
961 | static int i2c_do_add_adapter(struct i2c_driver *driver, |
962 | struct i2c_adapter *adap) | |
026526f5 | 963 | { |
4735c98f JD |
964 | /* Detect supported devices on that bus, and instantiate them */ |
965 | i2c_detect(adap, driver); | |
966 | ||
967 | /* Let legacy drivers scan this bus for matching devices */ | |
026526f5 | 968 | if (driver->attach_adapter) { |
a920ff41 JD |
969 | dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n", |
970 | driver->driver.name); | |
fe6fc258 JD |
971 | dev_warn(&adap->dev, "Please use another way to instantiate " |
972 | "your i2c_client\n"); | |
026526f5 JD |
973 | /* We ignore the return code; if it fails, too bad */ |
974 | driver->attach_adapter(adap); | |
975 | } | |
976 | return 0; | |
977 | } | |
978 | ||
69b0089a JD |
979 | static int __process_new_adapter(struct device_driver *d, void *data) |
980 | { | |
981 | return i2c_do_add_adapter(to_i2c_driver(d), data); | |
982 | } | |
983 | ||
6e13e641 | 984 | static int i2c_register_adapter(struct i2c_adapter *adap) |
1da177e4 | 985 | { |
d6703281 | 986 | int res = 0; |
1da177e4 | 987 | |
1d0b19c9 | 988 | /* Can't register until after driver model init */ |
35fc37f8 JD |
989 | if (unlikely(WARN_ON(!i2c_bus_type.p))) { |
990 | res = -EAGAIN; | |
991 | goto out_list; | |
992 | } | |
1d0b19c9 | 993 | |
2236baa7 JD |
994 | /* Sanity checks */ |
995 | if (unlikely(adap->name[0] == '\0')) { | |
996 | pr_err("i2c-core: Attempt to register an adapter with " | |
997 | "no name!\n"); | |
998 | return -EINVAL; | |
999 | } | |
1000 | if (unlikely(!adap->algo)) { | |
1001 | pr_err("i2c-core: Attempt to register adapter '%s' with " | |
1002 | "no algo!\n", adap->name); | |
1003 | return -EINVAL; | |
1004 | } | |
1005 | ||
194684e5 | 1006 | rt_mutex_init(&adap->bus_lock); |
dafc50d1 | 1007 | mutex_init(&adap->userspace_clients_lock); |
6629dcff | 1008 | INIT_LIST_HEAD(&adap->userspace_clients); |
1da177e4 | 1009 | |
8fcfef6e JD |
1010 | /* Set default timeout to 1 second if not already set */ |
1011 | if (adap->timeout == 0) | |
1012 | adap->timeout = HZ; | |
1013 | ||
27d9c183 | 1014 | dev_set_name(&adap->dev, "i2c-%d", adap->nr); |
4f8cf824 JD |
1015 | adap->dev.bus = &i2c_bus_type; |
1016 | adap->dev.type = &i2c_adapter_type; | |
b119c6c9 JD |
1017 | res = device_register(&adap->dev); |
1018 | if (res) | |
1019 | goto out_list; | |
1da177e4 | 1020 | |
b6d7b3d1 JD |
1021 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
1022 | ||
2bb5095a JD |
1023 | #ifdef CONFIG_I2C_COMPAT |
1024 | res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, | |
1025 | adap->dev.parent); | |
1026 | if (res) | |
1027 | dev_warn(&adap->dev, | |
1028 | "Failed to create compatibility class link\n"); | |
1029 | #endif | |
1030 | ||
5f9296ba VK |
1031 | /* bus recovery specific initialization */ |
1032 | if (adap->bus_recovery_info) { | |
1033 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; | |
1034 | ||
1035 | if (!bri->recover_bus) { | |
1036 | dev_err(&adap->dev, "No recover_bus() found, not using recovery\n"); | |
1037 | adap->bus_recovery_info = NULL; | |
1038 | goto exit_recovery; | |
1039 | } | |
1040 | ||
1041 | /* Generic GPIO recovery */ | |
1042 | if (bri->recover_bus == i2c_generic_gpio_recovery) { | |
1043 | if (!gpio_is_valid(bri->scl_gpio)) { | |
1044 | dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n"); | |
1045 | adap->bus_recovery_info = NULL; | |
1046 | goto exit_recovery; | |
1047 | } | |
1048 | ||
1049 | if (gpio_is_valid(bri->sda_gpio)) | |
1050 | bri->get_sda = get_sda_gpio_value; | |
1051 | else | |
1052 | bri->get_sda = NULL; | |
1053 | ||
1054 | bri->get_scl = get_scl_gpio_value; | |
1055 | bri->set_scl = set_scl_gpio_value; | |
1056 | } else if (!bri->set_scl || !bri->get_scl) { | |
1057 | /* Generic SCL recovery */ | |
1058 | dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n"); | |
1059 | adap->bus_recovery_info = NULL; | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | exit_recovery: | |
729d6dd5 | 1064 | /* create pre-declared device nodes */ |
6e13e641 DB |
1065 | if (adap->nr < __i2c_first_dynamic_bus_num) |
1066 | i2c_scan_static_board_info(adap); | |
1067 | ||
4735c98f | 1068 | /* Notify drivers */ |
35fc37f8 | 1069 | mutex_lock(&core_lock); |
d6703281 | 1070 | bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); |
caada32a | 1071 | mutex_unlock(&core_lock); |
35fc37f8 JD |
1072 | |
1073 | return 0; | |
b119c6c9 | 1074 | |
b119c6c9 | 1075 | out_list: |
35fc37f8 | 1076 | mutex_lock(&core_lock); |
b119c6c9 | 1077 | idr_remove(&i2c_adapter_idr, adap->nr); |
35fc37f8 JD |
1078 | mutex_unlock(&core_lock); |
1079 | return res; | |
1da177e4 LT |
1080 | } |
1081 | ||
ee5c2744 DA |
1082 | /** |
1083 | * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1 | |
1084 | * @adap: the adapter to register (with adap->nr initialized) | |
1085 | * Context: can sleep | |
1086 | * | |
1087 | * See i2c_add_numbered_adapter() for details. | |
1088 | */ | |
1089 | static int __i2c_add_numbered_adapter(struct i2c_adapter *adap) | |
1090 | { | |
1091 | int id; | |
1092 | ||
1093 | mutex_lock(&core_lock); | |
1094 | id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, | |
1095 | GFP_KERNEL); | |
1096 | mutex_unlock(&core_lock); | |
1097 | if (id < 0) | |
1098 | return id == -ENOSPC ? -EBUSY : id; | |
1099 | ||
1100 | return i2c_register_adapter(adap); | |
1101 | } | |
1102 | ||
6e13e641 DB |
1103 | /** |
1104 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number | |
1105 | * @adapter: the adapter to add | |
d64f73be | 1106 | * Context: can sleep |
6e13e641 DB |
1107 | * |
1108 | * This routine is used to declare an I2C adapter when its bus number | |
ee5c2744 DA |
1109 | * doesn't matter or when its bus number is specified by an dt alias. |
1110 | * Examples of bases when the bus number doesn't matter: I2C adapters | |
1111 | * dynamically added by USB links or PCI plugin cards. | |
6e13e641 DB |
1112 | * |
1113 | * When this returns zero, a new bus number was allocated and stored | |
1114 | * in adap->nr, and the specified adapter became available for clients. | |
1115 | * Otherwise, a negative errno value is returned. | |
1116 | */ | |
1117 | int i2c_add_adapter(struct i2c_adapter *adapter) | |
1118 | { | |
ee5c2744 | 1119 | struct device *dev = &adapter->dev; |
4ae42b0f | 1120 | int id; |
6e13e641 | 1121 | |
ee5c2744 DA |
1122 | if (dev->of_node) { |
1123 | id = of_alias_get_id(dev->of_node, "i2c"); | |
1124 | if (id >= 0) { | |
1125 | adapter->nr = id; | |
1126 | return __i2c_add_numbered_adapter(adapter); | |
1127 | } | |
1128 | } | |
1129 | ||
caada32a | 1130 | mutex_lock(&core_lock); |
4ae42b0f TH |
1131 | id = idr_alloc(&i2c_adapter_idr, adapter, |
1132 | __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); | |
caada32a | 1133 | mutex_unlock(&core_lock); |
4ae42b0f TH |
1134 | if (id < 0) |
1135 | return id; | |
6e13e641 DB |
1136 | |
1137 | adapter->nr = id; | |
4ae42b0f | 1138 | |
6e13e641 DB |
1139 | return i2c_register_adapter(adapter); |
1140 | } | |
1141 | EXPORT_SYMBOL(i2c_add_adapter); | |
1142 | ||
1143 | /** | |
1144 | * i2c_add_numbered_adapter - declare i2c adapter, use static bus number | |
1145 | * @adap: the adapter to register (with adap->nr initialized) | |
d64f73be | 1146 | * Context: can sleep |
6e13e641 DB |
1147 | * |
1148 | * This routine is used to declare an I2C adapter when its bus number | |
8c07e46f RD |
1149 | * matters. For example, use it for I2C adapters from system-on-chip CPUs, |
1150 | * or otherwise built in to the system's mainboard, and where i2c_board_info | |
6e13e641 DB |
1151 | * is used to properly configure I2C devices. |
1152 | * | |
488bf314 GL |
1153 | * If the requested bus number is set to -1, then this function will behave |
1154 | * identically to i2c_add_adapter, and will dynamically assign a bus number. | |
1155 | * | |
6e13e641 DB |
1156 | * If no devices have pre-been declared for this bus, then be sure to |
1157 | * register the adapter before any dynamically allocated ones. Otherwise | |
1158 | * the required bus ID may not be available. | |
1159 | * | |
1160 | * When this returns zero, the specified adapter became available for | |
1161 | * clients using the bus number provided in adap->nr. Also, the table | |
1162 | * of I2C devices pre-declared using i2c_register_board_info() is scanned, | |
1163 | * and the appropriate driver model device nodes are created. Otherwise, a | |
1164 | * negative errno value is returned. | |
1165 | */ | |
1166 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) | |
1167 | { | |
488bf314 GL |
1168 | if (adap->nr == -1) /* -1 means dynamically assign bus id */ |
1169 | return i2c_add_adapter(adap); | |
6e13e641 | 1170 | |
ee5c2744 | 1171 | return __i2c_add_numbered_adapter(adap); |
6e13e641 DB |
1172 | } |
1173 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); | |
1174 | ||
19baba4c | 1175 | static void i2c_do_del_adapter(struct i2c_driver *driver, |
69b0089a | 1176 | struct i2c_adapter *adapter) |
026526f5 | 1177 | { |
4735c98f | 1178 | struct i2c_client *client, *_n; |
026526f5 | 1179 | |
acec211c JD |
1180 | /* Remove the devices we created ourselves as the result of hardware |
1181 | * probing (using a driver's detect method) */ | |
4735c98f JD |
1182 | list_for_each_entry_safe(client, _n, &driver->clients, detected) { |
1183 | if (client->adapter == adapter) { | |
1184 | dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", | |
1185 | client->name, client->addr); | |
1186 | list_del(&client->detected); | |
1187 | i2c_unregister_device(client); | |
1188 | } | |
1189 | } | |
026526f5 JD |
1190 | } |
1191 | ||
e549c2b5 | 1192 | static int __unregister_client(struct device *dev, void *dummy) |
5219bf88 JD |
1193 | { |
1194 | struct i2c_client *client = i2c_verify_client(dev); | |
1195 | if (client && strcmp(client->name, "dummy")) | |
1196 | i2c_unregister_device(client); | |
1197 | return 0; | |
1198 | } | |
1199 | ||
1200 | static int __unregister_dummy(struct device *dev, void *dummy) | |
e549c2b5 JD |
1201 | { |
1202 | struct i2c_client *client = i2c_verify_client(dev); | |
1203 | if (client) | |
1204 | i2c_unregister_device(client); | |
1205 | return 0; | |
1206 | } | |
1207 | ||
69b0089a JD |
1208 | static int __process_removed_adapter(struct device_driver *d, void *data) |
1209 | { | |
19baba4c LPC |
1210 | i2c_do_del_adapter(to_i2c_driver(d), data); |
1211 | return 0; | |
69b0089a JD |
1212 | } |
1213 | ||
d64f73be DB |
1214 | /** |
1215 | * i2c_del_adapter - unregister I2C adapter | |
1216 | * @adap: the adapter being unregistered | |
1217 | * Context: can sleep | |
1218 | * | |
1219 | * This unregisters an I2C adapter which was previously registered | |
1220 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. | |
1221 | */ | |
1da177e4 LT |
1222 | int i2c_del_adapter(struct i2c_adapter *adap) |
1223 | { | |
35fc37f8 | 1224 | struct i2c_adapter *found; |
bbd2d9c9 | 1225 | struct i2c_client *client, *next; |
1da177e4 LT |
1226 | |
1227 | /* First make sure that this adapter was ever added */ | |
35fc37f8 JD |
1228 | mutex_lock(&core_lock); |
1229 | found = idr_find(&i2c_adapter_idr, adap->nr); | |
1230 | mutex_unlock(&core_lock); | |
1231 | if (found != adap) { | |
b6d7b3d1 JD |
1232 | pr_debug("i2c-core: attempting to delete unregistered " |
1233 | "adapter [%s]\n", adap->name); | |
f5fb0822 | 1234 | return 0; |
1da177e4 LT |
1235 | } |
1236 | ||
026526f5 | 1237 | /* Tell drivers about this removal */ |
35fc37f8 | 1238 | mutex_lock(&core_lock); |
19baba4c | 1239 | bus_for_each_drv(&i2c_bus_type, NULL, adap, |
69b0089a | 1240 | __process_removed_adapter); |
35fc37f8 | 1241 | mutex_unlock(&core_lock); |
1da177e4 | 1242 | |
bbd2d9c9 | 1243 | /* Remove devices instantiated from sysfs */ |
390946b1 JD |
1244 | mutex_lock_nested(&adap->userspace_clients_lock, |
1245 | i2c_adapter_depth(adap)); | |
6629dcff JD |
1246 | list_for_each_entry_safe(client, next, &adap->userspace_clients, |
1247 | detected) { | |
1248 | dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, | |
1249 | client->addr); | |
1250 | list_del(&client->detected); | |
1251 | i2c_unregister_device(client); | |
bbd2d9c9 | 1252 | } |
dafc50d1 | 1253 | mutex_unlock(&adap->userspace_clients_lock); |
bbd2d9c9 | 1254 | |
e549c2b5 | 1255 | /* Detach any active clients. This can't fail, thus we do not |
5219bf88 JD |
1256 | * check the returned value. This is a two-pass process, because |
1257 | * we can't remove the dummy devices during the first pass: they | |
1258 | * could have been instantiated by real devices wishing to clean | |
1259 | * them up properly, so we give them a chance to do that first. */ | |
19baba4c LPC |
1260 | device_for_each_child(&adap->dev, NULL, __unregister_client); |
1261 | device_for_each_child(&adap->dev, NULL, __unregister_dummy); | |
1da177e4 | 1262 | |
2bb5095a JD |
1263 | #ifdef CONFIG_I2C_COMPAT |
1264 | class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, | |
1265 | adap->dev.parent); | |
1266 | #endif | |
1267 | ||
c5567521 TLSC |
1268 | /* device name is gone after device_unregister */ |
1269 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); | |
1270 | ||
1da177e4 LT |
1271 | /* clean up the sysfs representation */ |
1272 | init_completion(&adap->dev_released); | |
1da177e4 | 1273 | device_unregister(&adap->dev); |
1da177e4 LT |
1274 | |
1275 | /* wait for sysfs to drop all references */ | |
1276 | wait_for_completion(&adap->dev_released); | |
1da177e4 | 1277 | |
6e13e641 | 1278 | /* free bus id */ |
35fc37f8 | 1279 | mutex_lock(&core_lock); |
1da177e4 | 1280 | idr_remove(&i2c_adapter_idr, adap->nr); |
35fc37f8 | 1281 | mutex_unlock(&core_lock); |
1da177e4 | 1282 | |
bd4bc3db JD |
1283 | /* Clear the device structure in case this adapter is ever going to be |
1284 | added again */ | |
1285 | memset(&adap->dev, 0, sizeof(adap->dev)); | |
1286 | ||
35fc37f8 | 1287 | return 0; |
1da177e4 | 1288 | } |
c0564606 | 1289 | EXPORT_SYMBOL(i2c_del_adapter); |
1da177e4 LT |
1290 | |
1291 | ||
7b4fbc50 DB |
1292 | /* ------------------------------------------------------------------------- */ |
1293 | ||
7ae31482 JD |
1294 | int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *)) |
1295 | { | |
1296 | int res; | |
1297 | ||
1298 | mutex_lock(&core_lock); | |
1299 | res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn); | |
1300 | mutex_unlock(&core_lock); | |
1301 | ||
1302 | return res; | |
1303 | } | |
1304 | EXPORT_SYMBOL_GPL(i2c_for_each_dev); | |
1305 | ||
69b0089a | 1306 | static int __process_new_driver(struct device *dev, void *data) |
7f101a97 | 1307 | { |
4f8cf824 JD |
1308 | if (dev->type != &i2c_adapter_type) |
1309 | return 0; | |
69b0089a | 1310 | return i2c_do_add_adapter(data, to_i2c_adapter(dev)); |
7f101a97 DY |
1311 | } |
1312 | ||
7b4fbc50 DB |
1313 | /* |
1314 | * An i2c_driver is used with one or more i2c_client (device) nodes to access | |
729d6dd5 | 1315 | * i2c slave chips, on a bus instance associated with some i2c_adapter. |
1da177e4 LT |
1316 | */ |
1317 | ||
de59cf9e | 1318 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
1da177e4 | 1319 | { |
7eebcb7c | 1320 | int res; |
1da177e4 | 1321 | |
1d0b19c9 DB |
1322 | /* Can't register until after driver model init */ |
1323 | if (unlikely(WARN_ON(!i2c_bus_type.p))) | |
1324 | return -EAGAIN; | |
1325 | ||
1da177e4 | 1326 | /* add the driver to the list of i2c drivers in the driver core */ |
de59cf9e | 1327 | driver->driver.owner = owner; |
1da177e4 | 1328 | driver->driver.bus = &i2c_bus_type; |
1da177e4 | 1329 | |
729d6dd5 | 1330 | /* When registration returns, the driver core |
6e13e641 DB |
1331 | * will have called probe() for all matching-but-unbound devices. |
1332 | */ | |
1da177e4 LT |
1333 | res = driver_register(&driver->driver); |
1334 | if (res) | |
7eebcb7c | 1335 | return res; |
438d6c2c | 1336 | |
f4e8db31 MB |
1337 | /* Drivers should switch to dev_pm_ops instead. */ |
1338 | if (driver->suspend) | |
1339 | pr_warn("i2c-core: driver [%s] using legacy suspend method\n", | |
1340 | driver->driver.name); | |
1341 | if (driver->resume) | |
1342 | pr_warn("i2c-core: driver [%s] using legacy resume method\n", | |
1343 | driver->driver.name); | |
1344 | ||
35d8b2e6 | 1345 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
1da177e4 | 1346 | |
4735c98f JD |
1347 | INIT_LIST_HEAD(&driver->clients); |
1348 | /* Walk the adapters that are already present */ | |
7ae31482 | 1349 | i2c_for_each_dev(driver, __process_new_driver); |
35fc37f8 | 1350 | |
7f101a97 DY |
1351 | return 0; |
1352 | } | |
1353 | EXPORT_SYMBOL(i2c_register_driver); | |
1354 | ||
69b0089a | 1355 | static int __process_removed_driver(struct device *dev, void *data) |
7f101a97 | 1356 | { |
19baba4c LPC |
1357 | if (dev->type == &i2c_adapter_type) |
1358 | i2c_do_del_adapter(data, to_i2c_adapter(dev)); | |
1359 | return 0; | |
1da177e4 LT |
1360 | } |
1361 | ||
a1d9e6e4 DB |
1362 | /** |
1363 | * i2c_del_driver - unregister I2C driver | |
1364 | * @driver: the driver being unregistered | |
d64f73be | 1365 | * Context: can sleep |
a1d9e6e4 | 1366 | */ |
b3e82096 | 1367 | void i2c_del_driver(struct i2c_driver *driver) |
1da177e4 | 1368 | { |
7ae31482 | 1369 | i2c_for_each_dev(driver, __process_removed_driver); |
1da177e4 LT |
1370 | |
1371 | driver_unregister(&driver->driver); | |
35d8b2e6 | 1372 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
1da177e4 | 1373 | } |
c0564606 | 1374 | EXPORT_SYMBOL(i2c_del_driver); |
1da177e4 | 1375 | |
7b4fbc50 DB |
1376 | /* ------------------------------------------------------------------------- */ |
1377 | ||
e48d3319 JD |
1378 | /** |
1379 | * i2c_use_client - increments the reference count of the i2c client structure | |
1380 | * @client: the client being referenced | |
1381 | * | |
1382 | * Each live reference to a client should be refcounted. The driver model does | |
1383 | * that automatically as part of driver binding, so that most drivers don't | |
1384 | * need to do this explicitly: they hold a reference until they're unbound | |
1385 | * from the device. | |
1386 | * | |
1387 | * A pointer to the client with the incremented reference counter is returned. | |
1388 | */ | |
1389 | struct i2c_client *i2c_use_client(struct i2c_client *client) | |
1da177e4 | 1390 | { |
6ea438ec DB |
1391 | if (client && get_device(&client->dev)) |
1392 | return client; | |
1393 | return NULL; | |
1da177e4 | 1394 | } |
c0564606 | 1395 | EXPORT_SYMBOL(i2c_use_client); |
1da177e4 | 1396 | |
e48d3319 JD |
1397 | /** |
1398 | * i2c_release_client - release a use of the i2c client structure | |
1399 | * @client: the client being no longer referenced | |
1400 | * | |
1401 | * Must be called when a user of a client is finished with it. | |
1402 | */ | |
1403 | void i2c_release_client(struct i2c_client *client) | |
1da177e4 | 1404 | { |
6ea438ec DB |
1405 | if (client) |
1406 | put_device(&client->dev); | |
1da177e4 | 1407 | } |
c0564606 | 1408 | EXPORT_SYMBOL(i2c_release_client); |
1da177e4 | 1409 | |
9b766b81 DB |
1410 | struct i2c_cmd_arg { |
1411 | unsigned cmd; | |
1412 | void *arg; | |
1413 | }; | |
1414 | ||
1415 | static int i2c_cmd(struct device *dev, void *_arg) | |
1416 | { | |
1417 | struct i2c_client *client = i2c_verify_client(dev); | |
1418 | struct i2c_cmd_arg *arg = _arg; | |
1419 | ||
1420 | if (client && client->driver && client->driver->command) | |
1421 | client->driver->command(client, arg->cmd, arg->arg); | |
1422 | return 0; | |
1423 | } | |
1424 | ||
1da177e4 LT |
1425 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
1426 | { | |
9b766b81 | 1427 | struct i2c_cmd_arg cmd_arg; |
1da177e4 | 1428 | |
9b766b81 DB |
1429 | cmd_arg.cmd = cmd; |
1430 | cmd_arg.arg = arg; | |
1431 | device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); | |
1da177e4 | 1432 | } |
c0564606 | 1433 | EXPORT_SYMBOL(i2c_clients_command); |
1da177e4 LT |
1434 | |
1435 | static int __init i2c_init(void) | |
1436 | { | |
1437 | int retval; | |
1438 | ||
1439 | retval = bus_register(&i2c_bus_type); | |
1da177e4 LT |
1440 | if (retval) |
1441 | return retval; | |
2bb5095a JD |
1442 | #ifdef CONFIG_I2C_COMPAT |
1443 | i2c_adapter_compat_class = class_compat_register("i2c-adapter"); | |
1444 | if (!i2c_adapter_compat_class) { | |
1445 | retval = -ENOMEM; | |
1446 | goto bus_err; | |
1447 | } | |
1448 | #endif | |
e9f1373b DB |
1449 | retval = i2c_add_driver(&dummy_driver); |
1450 | if (retval) | |
2bb5095a | 1451 | goto class_err; |
e9f1373b DB |
1452 | return 0; |
1453 | ||
2bb5095a JD |
1454 | class_err: |
1455 | #ifdef CONFIG_I2C_COMPAT | |
1456 | class_compat_unregister(i2c_adapter_compat_class); | |
e9f1373b | 1457 | bus_err: |
2bb5095a | 1458 | #endif |
e9f1373b DB |
1459 | bus_unregister(&i2c_bus_type); |
1460 | return retval; | |
1da177e4 LT |
1461 | } |
1462 | ||
1463 | static void __exit i2c_exit(void) | |
1464 | { | |
e9f1373b | 1465 | i2c_del_driver(&dummy_driver); |
2bb5095a JD |
1466 | #ifdef CONFIG_I2C_COMPAT |
1467 | class_compat_unregister(i2c_adapter_compat_class); | |
1468 | #endif | |
1da177e4 LT |
1469 | bus_unregister(&i2c_bus_type); |
1470 | } | |
1471 | ||
a10f9e7c DB |
1472 | /* We must initialize early, because some subsystems register i2c drivers |
1473 | * in subsys_initcall() code, but are linked (and initialized) before i2c. | |
1474 | */ | |
1475 | postcore_initcall(i2c_init); | |
1da177e4 LT |
1476 | module_exit(i2c_exit); |
1477 | ||
1478 | /* ---------------------------------------------------- | |
1479 | * the functional interface to the i2c busses. | |
1480 | * ---------------------------------------------------- | |
1481 | */ | |
1482 | ||
b37d2a3a JD |
1483 | /** |
1484 | * __i2c_transfer - unlocked flavor of i2c_transfer | |
1485 | * @adap: Handle to I2C bus | |
1486 | * @msgs: One or more messages to execute before STOP is issued to | |
1487 | * terminate the operation; each message begins with a START. | |
1488 | * @num: Number of messages to be executed. | |
1489 | * | |
1490 | * Returns negative errno, else the number of messages executed. | |
1491 | * | |
1492 | * Adapter lock must be held when calling this function. No debug logging | |
1493 | * takes place. adap->algo->master_xfer existence isn't checked. | |
1494 | */ | |
1495 | int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) | |
1496 | { | |
1497 | unsigned long orig_jiffies; | |
1498 | int ret, try; | |
1499 | ||
1500 | /* Retry automatically on arbitration loss */ | |
1501 | orig_jiffies = jiffies; | |
1502 | for (ret = 0, try = 0; try <= adap->retries; try++) { | |
1503 | ret = adap->algo->master_xfer(adap, msgs, num); | |
1504 | if (ret != -EAGAIN) | |
1505 | break; | |
1506 | if (time_after(jiffies, orig_jiffies + adap->timeout)) | |
1507 | break; | |
1508 | } | |
1509 | ||
1510 | return ret; | |
1511 | } | |
1512 | EXPORT_SYMBOL(__i2c_transfer); | |
1513 | ||
a1cdedac DB |
1514 | /** |
1515 | * i2c_transfer - execute a single or combined I2C message | |
1516 | * @adap: Handle to I2C bus | |
1517 | * @msgs: One or more messages to execute before STOP is issued to | |
1518 | * terminate the operation; each message begins with a START. | |
1519 | * @num: Number of messages to be executed. | |
1520 | * | |
1521 | * Returns negative errno, else the number of messages executed. | |
1522 | * | |
1523 | * Note that there is no requirement that each message be sent to | |
1524 | * the same slave address, although that is the most common model. | |
1525 | */ | |
09b8ce0a | 1526 | int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
1da177e4 | 1527 | { |
b37d2a3a | 1528 | int ret; |
1da177e4 | 1529 | |
a1cdedac DB |
1530 | /* REVISIT the fault reporting model here is weak: |
1531 | * | |
1532 | * - When we get an error after receiving N bytes from a slave, | |
1533 | * there is no way to report "N". | |
1534 | * | |
1535 | * - When we get a NAK after transmitting N bytes to a slave, | |
1536 | * there is no way to report "N" ... or to let the master | |
1537 | * continue executing the rest of this combined message, if | |
1538 | * that's the appropriate response. | |
1539 | * | |
1540 | * - When for example "num" is two and we successfully complete | |
1541 | * the first message but get an error part way through the | |
1542 | * second, it's unclear whether that should be reported as | |
1543 | * one (discarding status on the second message) or errno | |
1544 | * (discarding status on the first one). | |
1545 | */ | |
1546 | ||
1da177e4 LT |
1547 | if (adap->algo->master_xfer) { |
1548 | #ifdef DEBUG | |
1549 | for (ret = 0; ret < num; ret++) { | |
1550 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " | |
209d27c3 JD |
1551 | "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) |
1552 | ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, | |
1553 | (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); | |
1da177e4 LT |
1554 | } |
1555 | #endif | |
1556 | ||
cea443a8 | 1557 | if (in_atomic() || irqs_disabled()) { |
fe61e07e | 1558 | ret = i2c_trylock_adapter(adap); |
cea443a8 MR |
1559 | if (!ret) |
1560 | /* I2C activity is ongoing. */ | |
1561 | return -EAGAIN; | |
1562 | } else { | |
fe61e07e | 1563 | i2c_lock_adapter(adap); |
cea443a8 MR |
1564 | } |
1565 | ||
b37d2a3a | 1566 | ret = __i2c_transfer(adap, msgs, num); |
fe61e07e | 1567 | i2c_unlock_adapter(adap); |
1da177e4 LT |
1568 | |
1569 | return ret; | |
1570 | } else { | |
1571 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); | |
24a5bb7b | 1572 | return -EOPNOTSUPP; |
1da177e4 LT |
1573 | } |
1574 | } | |
c0564606 | 1575 | EXPORT_SYMBOL(i2c_transfer); |
1da177e4 | 1576 | |
a1cdedac DB |
1577 | /** |
1578 | * i2c_master_send - issue a single I2C message in master transmit mode | |
1579 | * @client: Handle to slave device | |
1580 | * @buf: Data that will be written to the slave | |
0c43ea54 | 1581 | * @count: How many bytes to write, must be less than 64k since msg.len is u16 |
a1cdedac DB |
1582 | * |
1583 | * Returns negative errno, or else the number of bytes written. | |
1584 | */ | |
0cc43a18 | 1585 | int i2c_master_send(const struct i2c_client *client, const char *buf, int count) |
1da177e4 LT |
1586 | { |
1587 | int ret; | |
7225acf4 | 1588 | struct i2c_adapter *adap = client->adapter; |
1da177e4 LT |
1589 | struct i2c_msg msg; |
1590 | ||
815f55f2 JD |
1591 | msg.addr = client->addr; |
1592 | msg.flags = client->flags & I2C_M_TEN; | |
1593 | msg.len = count; | |
1594 | msg.buf = (char *)buf; | |
438d6c2c | 1595 | |
815f55f2 | 1596 | ret = i2c_transfer(adap, &msg, 1); |
1da177e4 | 1597 | |
834aa6f3 WS |
1598 | /* |
1599 | * If everything went ok (i.e. 1 msg transmitted), return #bytes | |
1600 | * transmitted, else error code. | |
1601 | */ | |
815f55f2 | 1602 | return (ret == 1) ? count : ret; |
1da177e4 | 1603 | } |
c0564606 | 1604 | EXPORT_SYMBOL(i2c_master_send); |
1da177e4 | 1605 | |
a1cdedac DB |
1606 | /** |
1607 | * i2c_master_recv - issue a single I2C message in master receive mode | |
1608 | * @client: Handle to slave device | |
1609 | * @buf: Where to store data read from slave | |
0c43ea54 | 1610 | * @count: How many bytes to read, must be less than 64k since msg.len is u16 |
a1cdedac DB |
1611 | * |
1612 | * Returns negative errno, or else the number of bytes read. | |
1613 | */ | |
0cc43a18 | 1614 | int i2c_master_recv(const struct i2c_client *client, char *buf, int count) |
1da177e4 | 1615 | { |
7225acf4 | 1616 | struct i2c_adapter *adap = client->adapter; |
1da177e4 LT |
1617 | struct i2c_msg msg; |
1618 | int ret; | |
815f55f2 JD |
1619 | |
1620 | msg.addr = client->addr; | |
1621 | msg.flags = client->flags & I2C_M_TEN; | |
1622 | msg.flags |= I2C_M_RD; | |
1623 | msg.len = count; | |
1624 | msg.buf = buf; | |
1625 | ||
1626 | ret = i2c_transfer(adap, &msg, 1); | |
1627 | ||
834aa6f3 WS |
1628 | /* |
1629 | * If everything went ok (i.e. 1 msg received), return #bytes received, | |
1630 | * else error code. | |
1631 | */ | |
815f55f2 | 1632 | return (ret == 1) ? count : ret; |
1da177e4 | 1633 | } |
c0564606 | 1634 | EXPORT_SYMBOL(i2c_master_recv); |
1da177e4 | 1635 | |
1da177e4 LT |
1636 | /* ---------------------------------------------------- |
1637 | * the i2c address scanning function | |
1638 | * Will not work for 10-bit addresses! | |
1639 | * ---------------------------------------------------- | |
1640 | */ | |
1da177e4 | 1641 | |
63e4e802 JD |
1642 | /* |
1643 | * Legacy default probe function, mostly relevant for SMBus. The default | |
1644 | * probe method is a quick write, but it is known to corrupt the 24RF08 | |
1645 | * EEPROMs due to a state machine bug, and could also irreversibly | |
1646 | * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f, | |
1647 | * we use a short byte read instead. Also, some bus drivers don't implement | |
1648 | * quick write, so we fallback to a byte read in that case too. | |
1649 | * On x86, there is another special case for FSC hardware monitoring chips, | |
1650 | * which want regular byte reads (address 0x73.) Fortunately, these are the | |
1651 | * only known chips using this I2C address on PC hardware. | |
1652 | * Returns 1 if probe succeeded, 0 if not. | |
1653 | */ | |
1654 | static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr) | |
1655 | { | |
1656 | int err; | |
1657 | union i2c_smbus_data dummy; | |
1658 | ||
1659 | #ifdef CONFIG_X86 | |
1660 | if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON) | |
1661 | && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA)) | |
1662 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | |
1663 | I2C_SMBUS_BYTE_DATA, &dummy); | |
1664 | else | |
1665 | #endif | |
8031d79b JD |
1666 | if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50) |
1667 | && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) | |
63e4e802 JD |
1668 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, |
1669 | I2C_SMBUS_QUICK, NULL); | |
8031d79b JD |
1670 | else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) |
1671 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | |
1672 | I2C_SMBUS_BYTE, &dummy); | |
1673 | else { | |
1674 | dev_warn(&adap->dev, "No suitable probing method supported\n"); | |
1675 | err = -EOPNOTSUPP; | |
1676 | } | |
63e4e802 JD |
1677 | |
1678 | return err >= 0; | |
1679 | } | |
1680 | ||
ccfbbd08 | 1681 | static int i2c_detect_address(struct i2c_client *temp_client, |
4735c98f JD |
1682 | struct i2c_driver *driver) |
1683 | { | |
1684 | struct i2c_board_info info; | |
1685 | struct i2c_adapter *adapter = temp_client->adapter; | |
1686 | int addr = temp_client->addr; | |
1687 | int err; | |
1688 | ||
1689 | /* Make sure the address is valid */ | |
656b8761 JD |
1690 | err = i2c_check_addr_validity(addr); |
1691 | if (err) { | |
4735c98f JD |
1692 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", |
1693 | addr); | |
656b8761 | 1694 | return err; |
4735c98f JD |
1695 | } |
1696 | ||
1697 | /* Skip if already in use */ | |
3b5f794b | 1698 | if (i2c_check_addr_busy(adapter, addr)) |
4735c98f JD |
1699 | return 0; |
1700 | ||
ccfbbd08 | 1701 | /* Make sure there is something at this address */ |
63e4e802 JD |
1702 | if (!i2c_default_probe(adapter, addr)) |
1703 | return 0; | |
4735c98f JD |
1704 | |
1705 | /* Finally call the custom detection function */ | |
1706 | memset(&info, 0, sizeof(struct i2c_board_info)); | |
1707 | info.addr = addr; | |
310ec792 | 1708 | err = driver->detect(temp_client, &info); |
4735c98f JD |
1709 | if (err) { |
1710 | /* -ENODEV is returned if the detection fails. We catch it | |
1711 | here as this isn't an error. */ | |
1712 | return err == -ENODEV ? 0 : err; | |
1713 | } | |
1714 | ||
1715 | /* Consistency check */ | |
1716 | if (info.type[0] == '\0') { | |
1717 | dev_err(&adapter->dev, "%s detection function provided " | |
1718 | "no name for 0x%x\n", driver->driver.name, | |
1719 | addr); | |
1720 | } else { | |
1721 | struct i2c_client *client; | |
1722 | ||
1723 | /* Detection succeeded, instantiate the device */ | |
1724 | dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", | |
1725 | info.type, info.addr); | |
1726 | client = i2c_new_device(adapter, &info); | |
1727 | if (client) | |
1728 | list_add_tail(&client->detected, &driver->clients); | |
1729 | else | |
1730 | dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", | |
1731 | info.type, info.addr); | |
1732 | } | |
1733 | return 0; | |
1734 | } | |
1735 | ||
1736 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) | |
1737 | { | |
c3813d6a | 1738 | const unsigned short *address_list; |
4735c98f JD |
1739 | struct i2c_client *temp_client; |
1740 | int i, err = 0; | |
1741 | int adap_id = i2c_adapter_id(adapter); | |
1742 | ||
c3813d6a JD |
1743 | address_list = driver->address_list; |
1744 | if (!driver->detect || !address_list) | |
4735c98f JD |
1745 | return 0; |
1746 | ||
51b54ba9 JD |
1747 | /* Stop here if the classes do not match */ |
1748 | if (!(adapter->class & driver->class)) | |
1749 | return 0; | |
1750 | ||
4735c98f JD |
1751 | /* Set up a temporary client to help detect callback */ |
1752 | temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); | |
1753 | if (!temp_client) | |
1754 | return -ENOMEM; | |
1755 | temp_client->adapter = adapter; | |
1756 | ||
c3813d6a | 1757 | for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { |
4735c98f | 1758 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
c3813d6a JD |
1759 | "addr 0x%02x\n", adap_id, address_list[i]); |
1760 | temp_client->addr = address_list[i]; | |
ccfbbd08 | 1761 | err = i2c_detect_address(temp_client, driver); |
51b54ba9 JD |
1762 | if (unlikely(err)) |
1763 | break; | |
4735c98f JD |
1764 | } |
1765 | ||
4735c98f JD |
1766 | kfree(temp_client); |
1767 | return err; | |
1768 | } | |
1769 | ||
d44f19d5 JD |
1770 | int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr) |
1771 | { | |
1772 | return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | |
1773 | I2C_SMBUS_QUICK, NULL) >= 0; | |
1774 | } | |
1775 | EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read); | |
1776 | ||
12b5053a JD |
1777 | struct i2c_client * |
1778 | i2c_new_probed_device(struct i2c_adapter *adap, | |
1779 | struct i2c_board_info *info, | |
9a94241a JD |
1780 | unsigned short const *addr_list, |
1781 | int (*probe)(struct i2c_adapter *, unsigned short addr)) | |
12b5053a JD |
1782 | { |
1783 | int i; | |
1784 | ||
8031d79b | 1785 | if (!probe) |
9a94241a | 1786 | probe = i2c_default_probe; |
12b5053a | 1787 | |
12b5053a JD |
1788 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { |
1789 | /* Check address validity */ | |
656b8761 | 1790 | if (i2c_check_addr_validity(addr_list[i]) < 0) { |
12b5053a JD |
1791 | dev_warn(&adap->dev, "Invalid 7-bit address " |
1792 | "0x%02x\n", addr_list[i]); | |
1793 | continue; | |
1794 | } | |
1795 | ||
1796 | /* Check address availability */ | |
3b5f794b | 1797 | if (i2c_check_addr_busy(adap, addr_list[i])) { |
12b5053a JD |
1798 | dev_dbg(&adap->dev, "Address 0x%02x already in " |
1799 | "use, not probing\n", addr_list[i]); | |
1800 | continue; | |
1801 | } | |
1802 | ||
63e4e802 | 1803 | /* Test address responsiveness */ |
9a94241a | 1804 | if (probe(adap, addr_list[i])) |
63e4e802 | 1805 | break; |
12b5053a | 1806 | } |
12b5053a JD |
1807 | |
1808 | if (addr_list[i] == I2C_CLIENT_END) { | |
1809 | dev_dbg(&adap->dev, "Probing failed, no device found\n"); | |
1810 | return NULL; | |
1811 | } | |
1812 | ||
1813 | info->addr = addr_list[i]; | |
1814 | return i2c_new_device(adap, info); | |
1815 | } | |
1816 | EXPORT_SYMBOL_GPL(i2c_new_probed_device); | |
1817 | ||
d735b34d | 1818 | struct i2c_adapter *i2c_get_adapter(int nr) |
1da177e4 | 1819 | { |
1da177e4 | 1820 | struct i2c_adapter *adapter; |
438d6c2c | 1821 | |
caada32a | 1822 | mutex_lock(&core_lock); |
d735b34d | 1823 | adapter = idr_find(&i2c_adapter_idr, nr); |
a0920e10 MH |
1824 | if (adapter && !try_module_get(adapter->owner)) |
1825 | adapter = NULL; | |
1826 | ||
caada32a | 1827 | mutex_unlock(&core_lock); |
a0920e10 | 1828 | return adapter; |
1da177e4 | 1829 | } |
c0564606 | 1830 | EXPORT_SYMBOL(i2c_get_adapter); |
1da177e4 LT |
1831 | |
1832 | void i2c_put_adapter(struct i2c_adapter *adap) | |
1833 | { | |
1834 | module_put(adap->owner); | |
1835 | } | |
c0564606 | 1836 | EXPORT_SYMBOL(i2c_put_adapter); |
1da177e4 LT |
1837 | |
1838 | /* The SMBus parts */ | |
1839 | ||
438d6c2c | 1840 | #define POLY (0x1070U << 3) |
09b8ce0a | 1841 | static u8 crc8(u16 data) |
1da177e4 LT |
1842 | { |
1843 | int i; | |
438d6c2c | 1844 | |
7225acf4 | 1845 | for (i = 0; i < 8; i++) { |
438d6c2c | 1846 | if (data & 0x8000) |
1da177e4 LT |
1847 | data = data ^ POLY; |
1848 | data = data << 1; | |
1849 | } | |
1850 | return (u8)(data >> 8); | |
1851 | } | |
1852 | ||
421ef47b JD |
1853 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
1854 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) | |
1da177e4 LT |
1855 | { |
1856 | int i; | |
1857 | ||
7225acf4 | 1858 | for (i = 0; i < count; i++) |
421ef47b | 1859 | crc = crc8((crc ^ p[i]) << 8); |
1da177e4 LT |
1860 | return crc; |
1861 | } | |
1862 | ||
421ef47b JD |
1863 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
1864 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) | |
1da177e4 | 1865 | { |
421ef47b JD |
1866 | /* The address will be sent first */ |
1867 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); | |
1868 | pec = i2c_smbus_pec(pec, &addr, 1); | |
1869 | ||
1870 | /* The data buffer follows */ | |
1871 | return i2c_smbus_pec(pec, msg->buf, msg->len); | |
1da177e4 LT |
1872 | } |
1873 | ||
421ef47b JD |
1874 | /* Used for write only transactions */ |
1875 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) | |
1da177e4 | 1876 | { |
421ef47b JD |
1877 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
1878 | msg->len++; | |
1da177e4 LT |
1879 | } |
1880 | ||
421ef47b JD |
1881 | /* Return <0 on CRC error |
1882 | If there was a write before this read (most cases) we need to take the | |
1883 | partial CRC from the write part into account. | |
1884 | Note that this function does modify the message (we need to decrease the | |
1885 | message length to hide the CRC byte from the caller). */ | |
1886 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) | |
1da177e4 | 1887 | { |
421ef47b JD |
1888 | u8 rpec = msg->buf[--msg->len]; |
1889 | cpec = i2c_smbus_msg_pec(cpec, msg); | |
1da177e4 | 1890 | |
1da177e4 LT |
1891 | if (rpec != cpec) { |
1892 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", | |
1893 | rpec, cpec); | |
24a5bb7b | 1894 | return -EBADMSG; |
1da177e4 | 1895 | } |
438d6c2c | 1896 | return 0; |
1da177e4 LT |
1897 | } |
1898 | ||
a1cdedac DB |
1899 | /** |
1900 | * i2c_smbus_read_byte - SMBus "receive byte" protocol | |
1901 | * @client: Handle to slave device | |
1902 | * | |
1903 | * This executes the SMBus "receive byte" protocol, returning negative errno | |
1904 | * else the byte received from the device. | |
1905 | */ | |
0cc43a18 | 1906 | s32 i2c_smbus_read_byte(const struct i2c_client *client) |
1da177e4 LT |
1907 | { |
1908 | union i2c_smbus_data data; | |
24a5bb7b DB |
1909 | int status; |
1910 | ||
1911 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1912 | I2C_SMBUS_READ, 0, | |
1913 | I2C_SMBUS_BYTE, &data); | |
1914 | return (status < 0) ? status : data.byte; | |
1da177e4 | 1915 | } |
c0564606 | 1916 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
1da177e4 | 1917 | |
a1cdedac DB |
1918 | /** |
1919 | * i2c_smbus_write_byte - SMBus "send byte" protocol | |
1920 | * @client: Handle to slave device | |
1921 | * @value: Byte to be sent | |
1922 | * | |
1923 | * This executes the SMBus "send byte" protocol, returning negative errno | |
1924 | * else zero on success. | |
1925 | */ | |
0cc43a18 | 1926 | s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value) |
1da177e4 | 1927 | { |
7225acf4 | 1928 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
421ef47b | 1929 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
1da177e4 | 1930 | } |
c0564606 | 1931 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
1da177e4 | 1932 | |
a1cdedac DB |
1933 | /** |
1934 | * i2c_smbus_read_byte_data - SMBus "read byte" protocol | |
1935 | * @client: Handle to slave device | |
1936 | * @command: Byte interpreted by slave | |
1937 | * | |
1938 | * This executes the SMBus "read byte" protocol, returning negative errno | |
1939 | * else a data byte received from the device. | |
1940 | */ | |
0cc43a18 | 1941 | s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command) |
1da177e4 LT |
1942 | { |
1943 | union i2c_smbus_data data; | |
24a5bb7b DB |
1944 | int status; |
1945 | ||
1946 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1947 | I2C_SMBUS_READ, command, | |
1948 | I2C_SMBUS_BYTE_DATA, &data); | |
1949 | return (status < 0) ? status : data.byte; | |
1da177e4 | 1950 | } |
c0564606 | 1951 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
1da177e4 | 1952 | |
a1cdedac DB |
1953 | /** |
1954 | * i2c_smbus_write_byte_data - SMBus "write byte" protocol | |
1955 | * @client: Handle to slave device | |
1956 | * @command: Byte interpreted by slave | |
1957 | * @value: Byte being written | |
1958 | * | |
1959 | * This executes the SMBus "write byte" protocol, returning negative errno | |
1960 | * else zero on success. | |
1961 | */ | |
0cc43a18 JD |
1962 | s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command, |
1963 | u8 value) | |
1da177e4 LT |
1964 | { |
1965 | union i2c_smbus_data data; | |
1966 | data.byte = value; | |
7225acf4 FH |
1967 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
1968 | I2C_SMBUS_WRITE, command, | |
1969 | I2C_SMBUS_BYTE_DATA, &data); | |
1da177e4 | 1970 | } |
c0564606 | 1971 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
1da177e4 | 1972 | |
a1cdedac DB |
1973 | /** |
1974 | * i2c_smbus_read_word_data - SMBus "read word" protocol | |
1975 | * @client: Handle to slave device | |
1976 | * @command: Byte interpreted by slave | |
1977 | * | |
1978 | * This executes the SMBus "read word" protocol, returning negative errno | |
1979 | * else a 16-bit unsigned "word" received from the device. | |
1980 | */ | |
0cc43a18 | 1981 | s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command) |
1da177e4 LT |
1982 | { |
1983 | union i2c_smbus_data data; | |
24a5bb7b DB |
1984 | int status; |
1985 | ||
1986 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1987 | I2C_SMBUS_READ, command, | |
1988 | I2C_SMBUS_WORD_DATA, &data); | |
1989 | return (status < 0) ? status : data.word; | |
1da177e4 | 1990 | } |
c0564606 | 1991 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
1da177e4 | 1992 | |
a1cdedac DB |
1993 | /** |
1994 | * i2c_smbus_write_word_data - SMBus "write word" protocol | |
1995 | * @client: Handle to slave device | |
1996 | * @command: Byte interpreted by slave | |
1997 | * @value: 16-bit "word" being written | |
1998 | * | |
1999 | * This executes the SMBus "write word" protocol, returning negative errno | |
2000 | * else zero on success. | |
2001 | */ | |
0cc43a18 JD |
2002 | s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command, |
2003 | u16 value) | |
1da177e4 LT |
2004 | { |
2005 | union i2c_smbus_data data; | |
2006 | data.word = value; | |
7225acf4 FH |
2007 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
2008 | I2C_SMBUS_WRITE, command, | |
2009 | I2C_SMBUS_WORD_DATA, &data); | |
1da177e4 | 2010 | } |
c0564606 | 2011 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
1da177e4 | 2012 | |
a64ec07d | 2013 | /** |
a1cdedac | 2014 | * i2c_smbus_read_block_data - SMBus "block read" protocol |
a64ec07d | 2015 | * @client: Handle to slave device |
a1cdedac | 2016 | * @command: Byte interpreted by slave |
a64ec07d DB |
2017 | * @values: Byte array into which data will be read; big enough to hold |
2018 | * the data returned by the slave. SMBus allows at most 32 bytes. | |
2019 | * | |
a1cdedac DB |
2020 | * This executes the SMBus "block read" protocol, returning negative errno |
2021 | * else the number of data bytes in the slave's response. | |
a64ec07d DB |
2022 | * |
2023 | * Note that using this function requires that the client's adapter support | |
2024 | * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers | |
2025 | * support this; its emulation through I2C messaging relies on a specific | |
2026 | * mechanism (I2C_M_RECV_LEN) which may not be implemented. | |
2027 | */ | |
0cc43a18 | 2028 | s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command, |
b86a1bc8 JD |
2029 | u8 *values) |
2030 | { | |
2031 | union i2c_smbus_data data; | |
24a5bb7b | 2032 | int status; |
b86a1bc8 | 2033 | |
24a5bb7b DB |
2034 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
2035 | I2C_SMBUS_READ, command, | |
2036 | I2C_SMBUS_BLOCK_DATA, &data); | |
2037 | if (status) | |
2038 | return status; | |
b86a1bc8 JD |
2039 | |
2040 | memcpy(values, &data.block[1], data.block[0]); | |
2041 | return data.block[0]; | |
2042 | } | |
2043 | EXPORT_SYMBOL(i2c_smbus_read_block_data); | |
2044 | ||
a1cdedac DB |
2045 | /** |
2046 | * i2c_smbus_write_block_data - SMBus "block write" protocol | |
2047 | * @client: Handle to slave device | |
2048 | * @command: Byte interpreted by slave | |
2049 | * @length: Size of data block; SMBus allows at most 32 bytes | |
2050 | * @values: Byte array which will be written. | |
2051 | * | |
2052 | * This executes the SMBus "block write" protocol, returning negative errno | |
2053 | * else zero on success. | |
2054 | */ | |
0cc43a18 | 2055 | s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, |
46f5ed75 | 2056 | u8 length, const u8 *values) |
1da177e4 LT |
2057 | { |
2058 | union i2c_smbus_data data; | |
7656032b | 2059 | |
1da177e4 LT |
2060 | if (length > I2C_SMBUS_BLOCK_MAX) |
2061 | length = I2C_SMBUS_BLOCK_MAX; | |
1da177e4 | 2062 | data.block[0] = length; |
7656032b | 2063 | memcpy(&data.block[1], values, length); |
7225acf4 FH |
2064 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
2065 | I2C_SMBUS_WRITE, command, | |
2066 | I2C_SMBUS_BLOCK_DATA, &data); | |
1da177e4 | 2067 | } |
c0564606 | 2068 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
1da177e4 LT |
2069 | |
2070 | /* Returns the number of read bytes */ | |
0cc43a18 | 2071 | s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command, |
4b2643d7 | 2072 | u8 length, u8 *values) |
1da177e4 LT |
2073 | { |
2074 | union i2c_smbus_data data; | |
24a5bb7b | 2075 | int status; |
7656032b | 2076 | |
4b2643d7 JD |
2077 | if (length > I2C_SMBUS_BLOCK_MAX) |
2078 | length = I2C_SMBUS_BLOCK_MAX; | |
2079 | data.block[0] = length; | |
24a5bb7b DB |
2080 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
2081 | I2C_SMBUS_READ, command, | |
2082 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
2083 | if (status < 0) | |
2084 | return status; | |
7656032b JD |
2085 | |
2086 | memcpy(values, &data.block[1], data.block[0]); | |
2087 | return data.block[0]; | |
1da177e4 | 2088 | } |
c0564606 | 2089 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
1da177e4 | 2090 | |
0cc43a18 | 2091 | s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command, |
46f5ed75 | 2092 | u8 length, const u8 *values) |
21bbd691 JD |
2093 | { |
2094 | union i2c_smbus_data data; | |
2095 | ||
2096 | if (length > I2C_SMBUS_BLOCK_MAX) | |
2097 | length = I2C_SMBUS_BLOCK_MAX; | |
2098 | data.block[0] = length; | |
2099 | memcpy(data.block + 1, values, length); | |
2100 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
2101 | I2C_SMBUS_WRITE, command, | |
2102 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
2103 | } | |
c0564606 | 2104 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
21bbd691 | 2105 | |
438d6c2c | 2106 | /* Simulate a SMBus command using the i2c protocol |
1da177e4 | 2107 | No checking of parameters is done! */ |
7225acf4 FH |
2108 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, |
2109 | unsigned short flags, | |
2110 | char read_write, u8 command, int size, | |
2111 | union i2c_smbus_data *data) | |
1da177e4 LT |
2112 | { |
2113 | /* So we need to generate a series of msgs. In the case of writing, we | |
2114 | need to use only one message; when reading, we need two. We initialize | |
2115 | most things with sane defaults, to keep the code below somewhat | |
2116 | simpler. */ | |
5c50d188 HI |
2117 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
2118 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; | |
7225acf4 | 2119 | int num = read_write == I2C_SMBUS_READ ? 2 : 1; |
1da177e4 | 2120 | int i; |
421ef47b | 2121 | u8 partial_pec = 0; |
24a5bb7b | 2122 | int status; |
230da094 S |
2123 | struct i2c_msg msg[2] = { |
2124 | { | |
2125 | .addr = addr, | |
2126 | .flags = flags, | |
2127 | .len = 1, | |
2128 | .buf = msgbuf0, | |
2129 | }, { | |
2130 | .addr = addr, | |
2131 | .flags = flags | I2C_M_RD, | |
2132 | .len = 0, | |
2133 | .buf = msgbuf1, | |
2134 | }, | |
2135 | }; | |
1da177e4 LT |
2136 | |
2137 | msgbuf0[0] = command; | |
7225acf4 | 2138 | switch (size) { |
1da177e4 LT |
2139 | case I2C_SMBUS_QUICK: |
2140 | msg[0].len = 0; | |
2141 | /* Special case: The read/write field is used as data */ | |
f29d2e02 RK |
2142 | msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? |
2143 | I2C_M_RD : 0); | |
1da177e4 LT |
2144 | num = 1; |
2145 | break; | |
2146 | case I2C_SMBUS_BYTE: | |
2147 | if (read_write == I2C_SMBUS_READ) { | |
2148 | /* Special case: only a read! */ | |
2149 | msg[0].flags = I2C_M_RD | flags; | |
2150 | num = 1; | |
2151 | } | |
2152 | break; | |
2153 | case I2C_SMBUS_BYTE_DATA: | |
2154 | if (read_write == I2C_SMBUS_READ) | |
2155 | msg[1].len = 1; | |
2156 | else { | |
2157 | msg[0].len = 2; | |
2158 | msgbuf0[1] = data->byte; | |
2159 | } | |
2160 | break; | |
2161 | case I2C_SMBUS_WORD_DATA: | |
2162 | if (read_write == I2C_SMBUS_READ) | |
2163 | msg[1].len = 2; | |
2164 | else { | |
7225acf4 | 2165 | msg[0].len = 3; |
1da177e4 | 2166 | msgbuf0[1] = data->word & 0xff; |
7eff82c8 | 2167 | msgbuf0[2] = data->word >> 8; |
1da177e4 LT |
2168 | } |
2169 | break; | |
2170 | case I2C_SMBUS_PROC_CALL: | |
2171 | num = 2; /* Special case */ | |
2172 | read_write = I2C_SMBUS_READ; | |
2173 | msg[0].len = 3; | |
2174 | msg[1].len = 2; | |
2175 | msgbuf0[1] = data->word & 0xff; | |
7eff82c8 | 2176 | msgbuf0[2] = data->word >> 8; |
1da177e4 LT |
2177 | break; |
2178 | case I2C_SMBUS_BLOCK_DATA: | |
1da177e4 | 2179 | if (read_write == I2C_SMBUS_READ) { |
209d27c3 JD |
2180 | msg[1].flags |= I2C_M_RECV_LEN; |
2181 | msg[1].len = 1; /* block length will be added by | |
2182 | the underlying bus driver */ | |
1da177e4 LT |
2183 | } else { |
2184 | msg[0].len = data->block[0] + 2; | |
2185 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { | |
24a5bb7b DB |
2186 | dev_err(&adapter->dev, |
2187 | "Invalid block write size %d\n", | |
2188 | data->block[0]); | |
2189 | return -EINVAL; | |
1da177e4 | 2190 | } |
5c50d188 | 2191 | for (i = 1; i < msg[0].len; i++) |
1da177e4 LT |
2192 | msgbuf0[i] = data->block[i-1]; |
2193 | } | |
2194 | break; | |
2195 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
209d27c3 JD |
2196 | num = 2; /* Another special case */ |
2197 | read_write = I2C_SMBUS_READ; | |
2198 | if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { | |
24a5bb7b DB |
2199 | dev_err(&adapter->dev, |
2200 | "Invalid block write size %d\n", | |
209d27c3 | 2201 | data->block[0]); |
24a5bb7b | 2202 | return -EINVAL; |
209d27c3 JD |
2203 | } |
2204 | msg[0].len = data->block[0] + 2; | |
2205 | for (i = 1; i < msg[0].len; i++) | |
2206 | msgbuf0[i] = data->block[i-1]; | |
2207 | msg[1].flags |= I2C_M_RECV_LEN; | |
2208 | msg[1].len = 1; /* block length will be added by | |
2209 | the underlying bus driver */ | |
2210 | break; | |
1da177e4 LT |
2211 | case I2C_SMBUS_I2C_BLOCK_DATA: |
2212 | if (read_write == I2C_SMBUS_READ) { | |
4b2643d7 | 2213 | msg[1].len = data->block[0]; |
1da177e4 LT |
2214 | } else { |
2215 | msg[0].len = data->block[0] + 1; | |
30dac746 | 2216 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
24a5bb7b DB |
2217 | dev_err(&adapter->dev, |
2218 | "Invalid block write size %d\n", | |
2219 | data->block[0]); | |
2220 | return -EINVAL; | |
1da177e4 LT |
2221 | } |
2222 | for (i = 1; i <= data->block[0]; i++) | |
2223 | msgbuf0[i] = data->block[i]; | |
2224 | } | |
2225 | break; | |
2226 | default: | |
24a5bb7b DB |
2227 | dev_err(&adapter->dev, "Unsupported transaction %d\n", size); |
2228 | return -EOPNOTSUPP; | |
1da177e4 LT |
2229 | } |
2230 | ||
421ef47b JD |
2231 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
2232 | && size != I2C_SMBUS_I2C_BLOCK_DATA); | |
2233 | if (i) { | |
2234 | /* Compute PEC if first message is a write */ | |
2235 | if (!(msg[0].flags & I2C_M_RD)) { | |
438d6c2c | 2236 | if (num == 1) /* Write only */ |
421ef47b JD |
2237 | i2c_smbus_add_pec(&msg[0]); |
2238 | else /* Write followed by read */ | |
2239 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); | |
2240 | } | |
2241 | /* Ask for PEC if last message is a read */ | |
2242 | if (msg[num-1].flags & I2C_M_RD) | |
438d6c2c | 2243 | msg[num-1].len++; |
421ef47b JD |
2244 | } |
2245 | ||
24a5bb7b DB |
2246 | status = i2c_transfer(adapter, msg, num); |
2247 | if (status < 0) | |
2248 | return status; | |
1da177e4 | 2249 | |
421ef47b JD |
2250 | /* Check PEC if last message is a read */ |
2251 | if (i && (msg[num-1].flags & I2C_M_RD)) { | |
24a5bb7b DB |
2252 | status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); |
2253 | if (status < 0) | |
2254 | return status; | |
421ef47b JD |
2255 | } |
2256 | ||
1da177e4 | 2257 | if (read_write == I2C_SMBUS_READ) |
7225acf4 FH |
2258 | switch (size) { |
2259 | case I2C_SMBUS_BYTE: | |
2260 | data->byte = msgbuf0[0]; | |
2261 | break; | |
2262 | case I2C_SMBUS_BYTE_DATA: | |
2263 | data->byte = msgbuf1[0]; | |
2264 | break; | |
2265 | case I2C_SMBUS_WORD_DATA: | |
2266 | case I2C_SMBUS_PROC_CALL: | |
2267 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); | |
2268 | break; | |
2269 | case I2C_SMBUS_I2C_BLOCK_DATA: | |
2270 | for (i = 0; i < data->block[0]; i++) | |
2271 | data->block[i+1] = msgbuf1[i]; | |
2272 | break; | |
2273 | case I2C_SMBUS_BLOCK_DATA: | |
2274 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
2275 | for (i = 0; i < msgbuf1[0] + 1; i++) | |
2276 | data->block[i] = msgbuf1[i]; | |
2277 | break; | |
1da177e4 LT |
2278 | } |
2279 | return 0; | |
2280 | } | |
2281 | ||
a1cdedac DB |
2282 | /** |
2283 | * i2c_smbus_xfer - execute SMBus protocol operations | |
2284 | * @adapter: Handle to I2C bus | |
2285 | * @addr: Address of SMBus slave on that bus | |
2286 | * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) | |
2287 | * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE | |
2288 | * @command: Byte interpreted by slave, for protocols which use such bytes | |
2289 | * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL | |
2290 | * @data: Data to be read or written | |
2291 | * | |
2292 | * This executes an SMBus protocol operation, and returns a negative | |
2293 | * errno code else zero on success. | |
2294 | */ | |
09b8ce0a | 2295 | s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, |
a1cdedac | 2296 | char read_write, u8 command, int protocol, |
09b8ce0a | 2297 | union i2c_smbus_data *data) |
1da177e4 | 2298 | { |
66b650f0 CW |
2299 | unsigned long orig_jiffies; |
2300 | int try; | |
1da177e4 | 2301 | s32 res; |
1da177e4 | 2302 | |
d47726c5 | 2303 | flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB; |
1da177e4 LT |
2304 | |
2305 | if (adapter->algo->smbus_xfer) { | |
fe61e07e | 2306 | i2c_lock_adapter(adapter); |
66b650f0 CW |
2307 | |
2308 | /* Retry automatically on arbitration loss */ | |
2309 | orig_jiffies = jiffies; | |
2310 | for (res = 0, try = 0; try <= adapter->retries; try++) { | |
2311 | res = adapter->algo->smbus_xfer(adapter, addr, flags, | |
2312 | read_write, command, | |
2313 | protocol, data); | |
2314 | if (res != -EAGAIN) | |
2315 | break; | |
2316 | if (time_after(jiffies, | |
2317 | orig_jiffies + adapter->timeout)) | |
2318 | break; | |
2319 | } | |
fe61e07e | 2320 | i2c_unlock_adapter(adapter); |
1da177e4 | 2321 | |
72fc2c7f LP |
2322 | if (res != -EOPNOTSUPP || !adapter->algo->master_xfer) |
2323 | return res; | |
2324 | /* | |
2325 | * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't | |
2326 | * implement native support for the SMBus operation. | |
2327 | */ | |
2328 | } | |
2329 | ||
2330 | return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, | |
2331 | command, protocol, data); | |
1da177e4 | 2332 | } |
1da177e4 | 2333 | EXPORT_SYMBOL(i2c_smbus_xfer); |
1da177e4 LT |
2334 | |
2335 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); | |
2336 | MODULE_DESCRIPTION("I2C-Bus main module"); | |
2337 | MODULE_LICENSE("GPL"); |