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