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