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 | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
18 | /* ------------------------------------------------------------------------- */ | |
19 | ||
96de0e25 | 20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
1da177e4 | 21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
421ef47b JD |
22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and |
23 | Jean Delvare <khali@linux-fr.org> */ | |
1da177e4 | 24 | |
1da177e4 LT |
25 | #include <linux/module.h> |
26 | #include <linux/kernel.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/i2c.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/idr.h> | |
b3585e4f | 32 | #include <linux/mutex.h> |
b8d6f45b | 33 | #include <linux/completion.h> |
cea443a8 MR |
34 | #include <linux/hardirq.h> |
35 | #include <linux/irqflags.h> | |
1da177e4 LT |
36 | #include <asm/uaccess.h> |
37 | ||
9c1600ed DB |
38 | #include "i2c-core.h" |
39 | ||
1da177e4 | 40 | |
99cd8e25 | 41 | /* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees |
35fc37f8 JD |
42 | that device detection, deletion of detected devices, and attach_adapter |
43 | and detach_adapter calls are serialized */ | |
caada32a | 44 | static DEFINE_MUTEX(core_lock); |
1da177e4 | 45 | static DEFINE_IDR(i2c_adapter_idr); |
99cd8e25 | 46 | static LIST_HEAD(userspace_devices); |
1da177e4 | 47 | |
f8a227e8 | 48 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr); |
4735c98f | 49 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); |
f37dd80a DB |
50 | |
51 | /* ------------------------------------------------------------------------- */ | |
52 | ||
d2653e92 JD |
53 | static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, |
54 | const struct i2c_client *client) | |
55 | { | |
56 | while (id->name[0]) { | |
57 | if (strcmp(client->name, id->name) == 0) | |
58 | return id; | |
59 | id++; | |
60 | } | |
61 | return NULL; | |
62 | } | |
63 | ||
1da177e4 LT |
64 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
65 | { | |
7b4fbc50 DB |
66 | struct i2c_client *client = to_i2c_client(dev); |
67 | struct i2c_driver *driver = to_i2c_driver(drv); | |
68 | ||
d2653e92 JD |
69 | /* match on an id table if there is one */ |
70 | if (driver->id_table) | |
71 | return i2c_match_id(driver->id_table, client) != NULL; | |
72 | ||
eb8a7908 | 73 | return 0; |
1da177e4 LT |
74 | } |
75 | ||
7b4fbc50 DB |
76 | #ifdef CONFIG_HOTPLUG |
77 | ||
78 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ | |
7eff2e7a | 79 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
7b4fbc50 DB |
80 | { |
81 | struct i2c_client *client = to_i2c_client(dev); | |
7b4fbc50 | 82 | |
eb8a7908 JD |
83 | if (add_uevent_var(env, "MODALIAS=%s%s", |
84 | I2C_MODULE_PREFIX, client->name)) | |
85 | return -ENOMEM; | |
7b4fbc50 DB |
86 | dev_dbg(dev, "uevent\n"); |
87 | return 0; | |
88 | } | |
89 | ||
90 | #else | |
91 | #define i2c_device_uevent NULL | |
92 | #endif /* CONFIG_HOTPLUG */ | |
93 | ||
f37dd80a | 94 | static int i2c_device_probe(struct device *dev) |
1da177e4 | 95 | { |
7b4fbc50 DB |
96 | struct i2c_client *client = to_i2c_client(dev); |
97 | struct i2c_driver *driver = to_i2c_driver(dev->driver); | |
50c3304a | 98 | int status; |
7b4fbc50 | 99 | |
e0457442 | 100 | if (!driver->probe || !driver->id_table) |
7b4fbc50 DB |
101 | return -ENODEV; |
102 | client->driver = driver; | |
ee35425c MP |
103 | if (!device_can_wakeup(&client->dev)) |
104 | device_init_wakeup(&client->dev, | |
105 | client->flags & I2C_CLIENT_WAKE); | |
7b4fbc50 | 106 | dev_dbg(dev, "probe\n"); |
d2653e92 | 107 | |
e0457442 | 108 | status = driver->probe(client, i2c_match_id(driver->id_table, client)); |
50c3304a HV |
109 | if (status) |
110 | client->driver = NULL; | |
111 | return status; | |
f37dd80a | 112 | } |
1da177e4 | 113 | |
f37dd80a DB |
114 | static int i2c_device_remove(struct device *dev) |
115 | { | |
a1d9e6e4 DB |
116 | struct i2c_client *client = to_i2c_client(dev); |
117 | struct i2c_driver *driver; | |
118 | int status; | |
119 | ||
120 | if (!dev->driver) | |
121 | return 0; | |
122 | ||
123 | driver = to_i2c_driver(dev->driver); | |
124 | if (driver->remove) { | |
125 | dev_dbg(dev, "remove\n"); | |
126 | status = driver->remove(client); | |
127 | } else { | |
128 | dev->driver = NULL; | |
129 | status = 0; | |
130 | } | |
131 | if (status == 0) | |
132 | client->driver = NULL; | |
133 | return status; | |
1da177e4 LT |
134 | } |
135 | ||
f37dd80a | 136 | static void i2c_device_shutdown(struct device *dev) |
1da177e4 | 137 | { |
f37dd80a DB |
138 | struct i2c_driver *driver; |
139 | ||
140 | if (!dev->driver) | |
141 | return; | |
142 | driver = to_i2c_driver(dev->driver); | |
143 | if (driver->shutdown) | |
144 | driver->shutdown(to_i2c_client(dev)); | |
1da177e4 LT |
145 | } |
146 | ||
09b8ce0a | 147 | static int i2c_device_suspend(struct device *dev, pm_message_t mesg) |
1da177e4 | 148 | { |
f37dd80a DB |
149 | struct i2c_driver *driver; |
150 | ||
151 | if (!dev->driver) | |
152 | return 0; | |
153 | driver = to_i2c_driver(dev->driver); | |
154 | if (!driver->suspend) | |
155 | return 0; | |
156 | return driver->suspend(to_i2c_client(dev), mesg); | |
1da177e4 LT |
157 | } |
158 | ||
09b8ce0a | 159 | static int i2c_device_resume(struct device *dev) |
1da177e4 | 160 | { |
f37dd80a DB |
161 | struct i2c_driver *driver; |
162 | ||
163 | if (!dev->driver) | |
164 | return 0; | |
165 | driver = to_i2c_driver(dev->driver); | |
166 | if (!driver->resume) | |
167 | return 0; | |
168 | return driver->resume(to_i2c_client(dev)); | |
1da177e4 LT |
169 | } |
170 | ||
9c1600ed DB |
171 | static void i2c_client_dev_release(struct device *dev) |
172 | { | |
173 | kfree(to_i2c_client(dev)); | |
174 | } | |
175 | ||
09b8ce0a ZX |
176 | static ssize_t |
177 | show_client_name(struct device *dev, struct device_attribute *attr, char *buf) | |
7b4fbc50 DB |
178 | { |
179 | struct i2c_client *client = to_i2c_client(dev); | |
180 | return sprintf(buf, "%s\n", client->name); | |
181 | } | |
182 | ||
09b8ce0a ZX |
183 | static ssize_t |
184 | show_modalias(struct device *dev, struct device_attribute *attr, char *buf) | |
7b4fbc50 DB |
185 | { |
186 | struct i2c_client *client = to_i2c_client(dev); | |
eb8a7908 | 187 | return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); |
7b4fbc50 DB |
188 | } |
189 | ||
190 | static struct device_attribute i2c_dev_attrs[] = { | |
191 | __ATTR(name, S_IRUGO, show_client_name, NULL), | |
192 | /* modalias helps coldplug: modprobe $(cat .../modalias) */ | |
193 | __ATTR(modalias, S_IRUGO, show_modalias, NULL), | |
194 | { }, | |
195 | }; | |
196 | ||
e9ca9eb9 | 197 | struct bus_type i2c_bus_type = { |
f37dd80a | 198 | .name = "i2c", |
7b4fbc50 | 199 | .dev_attrs = i2c_dev_attrs, |
f37dd80a | 200 | .match = i2c_device_match, |
7b4fbc50 | 201 | .uevent = i2c_device_uevent, |
f37dd80a DB |
202 | .probe = i2c_device_probe, |
203 | .remove = i2c_device_remove, | |
204 | .shutdown = i2c_device_shutdown, | |
205 | .suspend = i2c_device_suspend, | |
206 | .resume = i2c_device_resume, | |
b864c7d5 | 207 | }; |
e9ca9eb9 | 208 | EXPORT_SYMBOL_GPL(i2c_bus_type); |
b864c7d5 | 209 | |
9b766b81 DB |
210 | |
211 | /** | |
212 | * i2c_verify_client - return parameter as i2c_client, or NULL | |
213 | * @dev: device, probably from some driver model iterator | |
214 | * | |
215 | * When traversing the driver model tree, perhaps using driver model | |
216 | * iterators like @device_for_each_child(), you can't assume very much | |
217 | * about the nodes you find. Use this function to avoid oopses caused | |
218 | * by wrongly treating some non-I2C device as an i2c_client. | |
219 | */ | |
220 | struct i2c_client *i2c_verify_client(struct device *dev) | |
221 | { | |
222 | return (dev->bus == &i2c_bus_type) | |
223 | ? to_i2c_client(dev) | |
224 | : NULL; | |
225 | } | |
226 | EXPORT_SYMBOL(i2c_verify_client); | |
227 | ||
228 | ||
9c1600ed | 229 | /** |
f8a227e8 | 230 | * i2c_new_device - instantiate an i2c device |
9c1600ed DB |
231 | * @adap: the adapter managing the device |
232 | * @info: describes one I2C device; bus_num is ignored | |
d64f73be | 233 | * Context: can sleep |
9c1600ed | 234 | * |
f8a227e8 JD |
235 | * Create an i2c device. Binding is handled through driver model |
236 | * probe()/remove() methods. A driver may be bound to this device when we | |
237 | * return from this function, or any later moment (e.g. maybe hotplugging will | |
238 | * load the driver module). This call is not appropriate for use by mainboard | |
239 | * initialization logic, which usually runs during an arch_initcall() long | |
240 | * before any i2c_adapter could exist. | |
9c1600ed DB |
241 | * |
242 | * This returns the new i2c client, which may be saved for later use with | |
243 | * i2c_unregister_device(); or NULL to indicate an error. | |
244 | */ | |
245 | struct i2c_client * | |
246 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) | |
247 | { | |
248 | struct i2c_client *client; | |
249 | int status; | |
250 | ||
251 | client = kzalloc(sizeof *client, GFP_KERNEL); | |
252 | if (!client) | |
253 | return NULL; | |
254 | ||
255 | client->adapter = adap; | |
256 | ||
257 | client->dev.platform_data = info->platform_data; | |
3bbb835d | 258 | |
11f1f2af AV |
259 | if (info->archdata) |
260 | client->dev.archdata = *info->archdata; | |
261 | ||
ee35425c | 262 | client->flags = info->flags; |
9c1600ed DB |
263 | client->addr = info->addr; |
264 | client->irq = info->irq; | |
265 | ||
9c1600ed DB |
266 | strlcpy(client->name, info->type, sizeof(client->name)); |
267 | ||
f8a227e8 JD |
268 | /* Check for address business */ |
269 | status = i2c_check_addr(adap, client->addr); | |
270 | if (status) | |
271 | goto out_err; | |
272 | ||
273 | client->dev.parent = &client->adapter->dev; | |
274 | client->dev.bus = &i2c_bus_type; | |
1e40ac12 | 275 | client->dev.release = i2c_client_dev_release; |
f8a227e8 JD |
276 | |
277 | dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), | |
278 | client->addr); | |
279 | status = device_register(&client->dev); | |
280 | if (status) | |
281 | goto out_err; | |
282 | ||
f8a227e8 JD |
283 | dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", |
284 | client->name, dev_name(&client->dev)); | |
285 | ||
9c1600ed | 286 | return client; |
f8a227e8 JD |
287 | |
288 | out_err: | |
289 | dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x " | |
290 | "(%d)\n", client->name, client->addr, status); | |
291 | kfree(client); | |
292 | return NULL; | |
9c1600ed DB |
293 | } |
294 | EXPORT_SYMBOL_GPL(i2c_new_device); | |
295 | ||
296 | ||
297 | /** | |
298 | * i2c_unregister_device - reverse effect of i2c_new_device() | |
299 | * @client: value returned from i2c_new_device() | |
d64f73be | 300 | * Context: can sleep |
9c1600ed DB |
301 | */ |
302 | void i2c_unregister_device(struct i2c_client *client) | |
a1d9e6e4 | 303 | { |
a1d9e6e4 DB |
304 | device_unregister(&client->dev); |
305 | } | |
9c1600ed | 306 | EXPORT_SYMBOL_GPL(i2c_unregister_device); |
a1d9e6e4 DB |
307 | |
308 | ||
60b129d7 JD |
309 | static const struct i2c_device_id dummy_id[] = { |
310 | { "dummy", 0 }, | |
311 | { }, | |
312 | }; | |
313 | ||
d2653e92 JD |
314 | static int dummy_probe(struct i2c_client *client, |
315 | const struct i2c_device_id *id) | |
316 | { | |
317 | return 0; | |
318 | } | |
319 | ||
320 | static int dummy_remove(struct i2c_client *client) | |
e9f1373b DB |
321 | { |
322 | return 0; | |
323 | } | |
324 | ||
325 | static struct i2c_driver dummy_driver = { | |
326 | .driver.name = "dummy", | |
d2653e92 JD |
327 | .probe = dummy_probe, |
328 | .remove = dummy_remove, | |
60b129d7 | 329 | .id_table = dummy_id, |
e9f1373b DB |
330 | }; |
331 | ||
332 | /** | |
333 | * i2c_new_dummy - return a new i2c device bound to a dummy driver | |
334 | * @adapter: the adapter managing the device | |
335 | * @address: seven bit address to be used | |
e9f1373b DB |
336 | * Context: can sleep |
337 | * | |
338 | * This returns an I2C client bound to the "dummy" driver, intended for use | |
339 | * with devices that consume multiple addresses. Examples of such chips | |
340 | * include various EEPROMS (like 24c04 and 24c08 models). | |
341 | * | |
342 | * These dummy devices have two main uses. First, most I2C and SMBus calls | |
343 | * except i2c_transfer() need a client handle; the dummy will be that handle. | |
344 | * And second, this prevents the specified address from being bound to a | |
345 | * different driver. | |
346 | * | |
347 | * This returns the new i2c client, which should be saved for later use with | |
348 | * i2c_unregister_device(); or NULL to indicate an error. | |
349 | */ | |
09b8ce0a | 350 | struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) |
e9f1373b DB |
351 | { |
352 | struct i2c_board_info info = { | |
60b129d7 | 353 | I2C_BOARD_INFO("dummy", address), |
e9f1373b DB |
354 | }; |
355 | ||
e9f1373b DB |
356 | return i2c_new_device(adapter, &info); |
357 | } | |
358 | EXPORT_SYMBOL_GPL(i2c_new_dummy); | |
359 | ||
f37dd80a DB |
360 | /* ------------------------------------------------------------------------- */ |
361 | ||
16ffadfc DB |
362 | /* I2C bus adapters -- one roots each I2C or SMBUS segment */ |
363 | ||
83eaaed0 | 364 | static void i2c_adapter_dev_release(struct device *dev) |
1da177e4 | 365 | { |
ef2c8321 | 366 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
1da177e4 LT |
367 | complete(&adap->dev_released); |
368 | } | |
369 | ||
16ffadfc DB |
370 | static ssize_t |
371 | show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf) | |
372 | { | |
ef2c8321 | 373 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
16ffadfc DB |
374 | return sprintf(buf, "%s\n", adap->name); |
375 | } | |
b119dc3f | 376 | |
99cd8e25 JD |
377 | /* |
378 | * Let users instantiate I2C devices through sysfs. This can be used when | |
379 | * platform initialization code doesn't contain the proper data for | |
380 | * whatever reason. Also useful for drivers that do device detection and | |
381 | * detection fails, either because the device uses an unexpected address, | |
382 | * or this is a compatible device with different ID register values. | |
383 | * | |
384 | * Parameter checking may look overzealous, but we really don't want | |
385 | * the user to provide incorrect parameters. | |
386 | */ | |
387 | static ssize_t | |
388 | i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, | |
389 | const char *buf, size_t count) | |
390 | { | |
391 | struct i2c_adapter *adap = to_i2c_adapter(dev); | |
392 | struct i2c_board_info info; | |
393 | struct i2c_client *client; | |
394 | char *blank, end; | |
395 | int res; | |
396 | ||
397 | dev_warn(dev, "The new_device interface is still experimental " | |
398 | "and may change in a near future\n"); | |
399 | memset(&info, 0, sizeof(struct i2c_board_info)); | |
400 | ||
401 | blank = strchr(buf, ' '); | |
402 | if (!blank) { | |
403 | dev_err(dev, "%s: Missing parameters\n", "new_device"); | |
404 | return -EINVAL; | |
405 | } | |
406 | if (blank - buf > I2C_NAME_SIZE - 1) { | |
407 | dev_err(dev, "%s: Invalid device name\n", "new_device"); | |
408 | return -EINVAL; | |
409 | } | |
410 | memcpy(info.type, buf, blank - buf); | |
411 | ||
412 | /* Parse remaining parameters, reject extra parameters */ | |
413 | res = sscanf(++blank, "%hi%c", &info.addr, &end); | |
414 | if (res < 1) { | |
415 | dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); | |
416 | return -EINVAL; | |
417 | } | |
418 | if (res > 1 && end != '\n') { | |
419 | dev_err(dev, "%s: Extra parameters\n", "new_device"); | |
420 | return -EINVAL; | |
421 | } | |
422 | ||
423 | if (info.addr < 0x03 || info.addr > 0x77) { | |
424 | dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device", | |
425 | info.addr); | |
426 | return -EINVAL; | |
427 | } | |
428 | ||
429 | client = i2c_new_device(adap, &info); | |
430 | if (!client) | |
431 | return -EEXIST; | |
432 | ||
433 | /* Keep track of the added device */ | |
434 | mutex_lock(&core_lock); | |
435 | list_add_tail(&client->detected, &userspace_devices); | |
436 | mutex_unlock(&core_lock); | |
437 | dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", | |
438 | info.type, info.addr); | |
439 | ||
440 | return count; | |
441 | } | |
442 | ||
443 | /* | |
444 | * And of course let the users delete the devices they instantiated, if | |
445 | * they got it wrong. This interface can only be used to delete devices | |
446 | * instantiated by i2c_sysfs_new_device above. This guarantees that we | |
447 | * don't delete devices to which some kernel code still has references. | |
448 | * | |
449 | * Parameter checking may look overzealous, but we really don't want | |
450 | * the user to delete the wrong device. | |
451 | */ | |
452 | static ssize_t | |
453 | i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, | |
454 | const char *buf, size_t count) | |
455 | { | |
456 | struct i2c_adapter *adap = to_i2c_adapter(dev); | |
457 | struct i2c_client *client, *next; | |
458 | unsigned short addr; | |
459 | char end; | |
460 | int res; | |
461 | ||
462 | /* Parse parameters, reject extra parameters */ | |
463 | res = sscanf(buf, "%hi%c", &addr, &end); | |
464 | if (res < 1) { | |
465 | dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); | |
466 | return -EINVAL; | |
467 | } | |
468 | if (res > 1 && end != '\n') { | |
469 | dev_err(dev, "%s: Extra parameters\n", "delete_device"); | |
470 | return -EINVAL; | |
471 | } | |
472 | ||
473 | /* Make sure the device was added through sysfs */ | |
474 | res = -ENOENT; | |
475 | mutex_lock(&core_lock); | |
476 | list_for_each_entry_safe(client, next, &userspace_devices, detected) { | |
477 | if (client->addr == addr && client->adapter == adap) { | |
478 | dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", | |
479 | "delete_device", client->name, client->addr); | |
480 | ||
481 | list_del(&client->detected); | |
482 | i2c_unregister_device(client); | |
483 | res = count; | |
484 | break; | |
485 | } | |
486 | } | |
487 | mutex_unlock(&core_lock); | |
488 | ||
489 | if (res < 0) | |
490 | dev_err(dev, "%s: Can't find device in list\n", | |
491 | "delete_device"); | |
492 | return res; | |
493 | } | |
494 | ||
16ffadfc DB |
495 | static struct device_attribute i2c_adapter_attrs[] = { |
496 | __ATTR(name, S_IRUGO, show_adapter_name, NULL), | |
99cd8e25 JD |
497 | __ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device), |
498 | __ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device), | |
16ffadfc DB |
499 | { }, |
500 | }; | |
b119dc3f | 501 | |
83eaaed0 | 502 | static struct class i2c_adapter_class = { |
b119dc3f DB |
503 | .owner = THIS_MODULE, |
504 | .name = "i2c-adapter", | |
16ffadfc | 505 | .dev_attrs = i2c_adapter_attrs, |
1da177e4 LT |
506 | }; |
507 | ||
9c1600ed DB |
508 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) |
509 | { | |
510 | struct i2c_devinfo *devinfo; | |
511 | ||
512 | mutex_lock(&__i2c_board_lock); | |
513 | list_for_each_entry(devinfo, &__i2c_board_list, list) { | |
514 | if (devinfo->busnum == adapter->nr | |
515 | && !i2c_new_device(adapter, | |
516 | &devinfo->board_info)) | |
09b8ce0a ZX |
517 | dev_err(&adapter->dev, |
518 | "Can't create device at 0x%02x\n", | |
9c1600ed DB |
519 | devinfo->board_info.addr); |
520 | } | |
521 | mutex_unlock(&__i2c_board_lock); | |
522 | } | |
523 | ||
026526f5 JD |
524 | static int i2c_do_add_adapter(struct device_driver *d, void *data) |
525 | { | |
526 | struct i2c_driver *driver = to_i2c_driver(d); | |
527 | struct i2c_adapter *adap = data; | |
528 | ||
4735c98f JD |
529 | /* Detect supported devices on that bus, and instantiate them */ |
530 | i2c_detect(adap, driver); | |
531 | ||
532 | /* Let legacy drivers scan this bus for matching devices */ | |
026526f5 JD |
533 | if (driver->attach_adapter) { |
534 | /* We ignore the return code; if it fails, too bad */ | |
535 | driver->attach_adapter(adap); | |
536 | } | |
537 | return 0; | |
538 | } | |
539 | ||
6e13e641 | 540 | static int i2c_register_adapter(struct i2c_adapter *adap) |
1da177e4 | 541 | { |
026526f5 | 542 | int res = 0, dummy; |
1da177e4 | 543 | |
1d0b19c9 | 544 | /* Can't register until after driver model init */ |
35fc37f8 JD |
545 | if (unlikely(WARN_ON(!i2c_bus_type.p))) { |
546 | res = -EAGAIN; | |
547 | goto out_list; | |
548 | } | |
1d0b19c9 | 549 | |
5c085d36 | 550 | mutex_init(&adap->bus_lock); |
1da177e4 | 551 | |
8fcfef6e JD |
552 | /* Set default timeout to 1 second if not already set */ |
553 | if (adap->timeout == 0) | |
554 | adap->timeout = HZ; | |
555 | ||
27d9c183 | 556 | dev_set_name(&adap->dev, "i2c-%d", adap->nr); |
1da177e4 | 557 | adap->dev.release = &i2c_adapter_dev_release; |
fccb56e4 | 558 | adap->dev.class = &i2c_adapter_class; |
b119c6c9 JD |
559 | res = device_register(&adap->dev); |
560 | if (res) | |
561 | goto out_list; | |
1da177e4 | 562 | |
b6d7b3d1 JD |
563 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
564 | ||
729d6dd5 | 565 | /* create pre-declared device nodes */ |
6e13e641 DB |
566 | if (adap->nr < __i2c_first_dynamic_bus_num) |
567 | i2c_scan_static_board_info(adap); | |
568 | ||
4735c98f | 569 | /* Notify drivers */ |
35fc37f8 | 570 | mutex_lock(&core_lock); |
026526f5 JD |
571 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
572 | i2c_do_add_adapter); | |
caada32a | 573 | mutex_unlock(&core_lock); |
35fc37f8 JD |
574 | |
575 | return 0; | |
b119c6c9 | 576 | |
b119c6c9 | 577 | out_list: |
35fc37f8 | 578 | mutex_lock(&core_lock); |
b119c6c9 | 579 | idr_remove(&i2c_adapter_idr, adap->nr); |
35fc37f8 JD |
580 | mutex_unlock(&core_lock); |
581 | return res; | |
1da177e4 LT |
582 | } |
583 | ||
6e13e641 DB |
584 | /** |
585 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number | |
586 | * @adapter: the adapter to add | |
d64f73be | 587 | * Context: can sleep |
6e13e641 DB |
588 | * |
589 | * This routine is used to declare an I2C adapter when its bus number | |
590 | * doesn't matter. Examples: for I2C adapters dynamically added by | |
591 | * USB links or PCI plugin cards. | |
592 | * | |
593 | * When this returns zero, a new bus number was allocated and stored | |
594 | * in adap->nr, and the specified adapter became available for clients. | |
595 | * Otherwise, a negative errno value is returned. | |
596 | */ | |
597 | int i2c_add_adapter(struct i2c_adapter *adapter) | |
598 | { | |
599 | int id, res = 0; | |
600 | ||
601 | retry: | |
602 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) | |
603 | return -ENOMEM; | |
604 | ||
caada32a | 605 | mutex_lock(&core_lock); |
6e13e641 DB |
606 | /* "above" here means "above or equal to", sigh */ |
607 | res = idr_get_new_above(&i2c_adapter_idr, adapter, | |
608 | __i2c_first_dynamic_bus_num, &id); | |
caada32a | 609 | mutex_unlock(&core_lock); |
6e13e641 DB |
610 | |
611 | if (res < 0) { | |
612 | if (res == -EAGAIN) | |
613 | goto retry; | |
614 | return res; | |
615 | } | |
616 | ||
617 | adapter->nr = id; | |
618 | return i2c_register_adapter(adapter); | |
619 | } | |
620 | EXPORT_SYMBOL(i2c_add_adapter); | |
621 | ||
622 | /** | |
623 | * i2c_add_numbered_adapter - declare i2c adapter, use static bus number | |
624 | * @adap: the adapter to register (with adap->nr initialized) | |
d64f73be | 625 | * Context: can sleep |
6e13e641 DB |
626 | * |
627 | * This routine is used to declare an I2C adapter when its bus number | |
8c07e46f RD |
628 | * matters. For example, use it for I2C adapters from system-on-chip CPUs, |
629 | * or otherwise built in to the system's mainboard, and where i2c_board_info | |
6e13e641 DB |
630 | * is used to properly configure I2C devices. |
631 | * | |
632 | * If no devices have pre-been declared for this bus, then be sure to | |
633 | * register the adapter before any dynamically allocated ones. Otherwise | |
634 | * the required bus ID may not be available. | |
635 | * | |
636 | * When this returns zero, the specified adapter became available for | |
637 | * clients using the bus number provided in adap->nr. Also, the table | |
638 | * of I2C devices pre-declared using i2c_register_board_info() is scanned, | |
639 | * and the appropriate driver model device nodes are created. Otherwise, a | |
640 | * negative errno value is returned. | |
641 | */ | |
642 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) | |
643 | { | |
644 | int id; | |
645 | int status; | |
646 | ||
647 | if (adap->nr & ~MAX_ID_MASK) | |
648 | return -EINVAL; | |
649 | ||
650 | retry: | |
651 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) | |
652 | return -ENOMEM; | |
653 | ||
caada32a | 654 | mutex_lock(&core_lock); |
6e13e641 DB |
655 | /* "above" here means "above or equal to", sigh; |
656 | * we need the "equal to" result to force the result | |
657 | */ | |
658 | status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id); | |
659 | if (status == 0 && id != adap->nr) { | |
660 | status = -EBUSY; | |
661 | idr_remove(&i2c_adapter_idr, id); | |
662 | } | |
caada32a | 663 | mutex_unlock(&core_lock); |
6e13e641 DB |
664 | if (status == -EAGAIN) |
665 | goto retry; | |
666 | ||
667 | if (status == 0) | |
668 | status = i2c_register_adapter(adap); | |
669 | return status; | |
670 | } | |
671 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); | |
672 | ||
026526f5 JD |
673 | static int i2c_do_del_adapter(struct device_driver *d, void *data) |
674 | { | |
675 | struct i2c_driver *driver = to_i2c_driver(d); | |
676 | struct i2c_adapter *adapter = data; | |
4735c98f | 677 | struct i2c_client *client, *_n; |
026526f5 JD |
678 | int res; |
679 | ||
acec211c JD |
680 | /* Remove the devices we created ourselves as the result of hardware |
681 | * probing (using a driver's detect method) */ | |
4735c98f JD |
682 | list_for_each_entry_safe(client, _n, &driver->clients, detected) { |
683 | if (client->adapter == adapter) { | |
684 | dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", | |
685 | client->name, client->addr); | |
686 | list_del(&client->detected); | |
687 | i2c_unregister_device(client); | |
688 | } | |
689 | } | |
690 | ||
026526f5 JD |
691 | if (!driver->detach_adapter) |
692 | return 0; | |
693 | res = driver->detach_adapter(adapter); | |
694 | if (res) | |
695 | dev_err(&adapter->dev, "detach_adapter failed (%d) " | |
696 | "for driver [%s]\n", res, driver->driver.name); | |
697 | return res; | |
698 | } | |
699 | ||
e549c2b5 JD |
700 | static int __unregister_client(struct device *dev, void *dummy) |
701 | { | |
702 | struct i2c_client *client = i2c_verify_client(dev); | |
703 | if (client) | |
704 | i2c_unregister_device(client); | |
705 | return 0; | |
706 | } | |
707 | ||
d64f73be DB |
708 | /** |
709 | * i2c_del_adapter - unregister I2C adapter | |
710 | * @adap: the adapter being unregistered | |
711 | * Context: can sleep | |
712 | * | |
713 | * This unregisters an I2C adapter which was previously registered | |
714 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. | |
715 | */ | |
1da177e4 LT |
716 | int i2c_del_adapter(struct i2c_adapter *adap) |
717 | { | |
1da177e4 | 718 | int res = 0; |
35fc37f8 | 719 | struct i2c_adapter *found; |
1da177e4 LT |
720 | |
721 | /* First make sure that this adapter was ever added */ | |
35fc37f8 JD |
722 | mutex_lock(&core_lock); |
723 | found = idr_find(&i2c_adapter_idr, adap->nr); | |
724 | mutex_unlock(&core_lock); | |
725 | if (found != adap) { | |
b6d7b3d1 JD |
726 | pr_debug("i2c-core: attempting to delete unregistered " |
727 | "adapter [%s]\n", adap->name); | |
35fc37f8 | 728 | return -EINVAL; |
1da177e4 LT |
729 | } |
730 | ||
026526f5 | 731 | /* Tell drivers about this removal */ |
35fc37f8 | 732 | mutex_lock(&core_lock); |
026526f5 JD |
733 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
734 | i2c_do_del_adapter); | |
35fc37f8 | 735 | mutex_unlock(&core_lock); |
026526f5 | 736 | if (res) |
35fc37f8 | 737 | return res; |
1da177e4 | 738 | |
e549c2b5 JD |
739 | /* Detach any active clients. This can't fail, thus we do not |
740 | checking the returned value. */ | |
741 | res = device_for_each_child(&adap->dev, NULL, __unregister_client); | |
1da177e4 LT |
742 | |
743 | /* clean up the sysfs representation */ | |
744 | init_completion(&adap->dev_released); | |
1da177e4 | 745 | device_unregister(&adap->dev); |
1da177e4 LT |
746 | |
747 | /* wait for sysfs to drop all references */ | |
748 | wait_for_completion(&adap->dev_released); | |
1da177e4 | 749 | |
6e13e641 | 750 | /* free bus id */ |
35fc37f8 | 751 | mutex_lock(&core_lock); |
1da177e4 | 752 | idr_remove(&i2c_adapter_idr, adap->nr); |
35fc37f8 | 753 | mutex_unlock(&core_lock); |
1da177e4 | 754 | |
b6d7b3d1 | 755 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); |
1da177e4 | 756 | |
bd4bc3db JD |
757 | /* Clear the device structure in case this adapter is ever going to be |
758 | added again */ | |
759 | memset(&adap->dev, 0, sizeof(adap->dev)); | |
760 | ||
35fc37f8 | 761 | return 0; |
1da177e4 | 762 | } |
c0564606 | 763 | EXPORT_SYMBOL(i2c_del_adapter); |
1da177e4 LT |
764 | |
765 | ||
7b4fbc50 DB |
766 | /* ------------------------------------------------------------------------- */ |
767 | ||
7f101a97 DY |
768 | static int __attach_adapter(struct device *dev, void *data) |
769 | { | |
770 | struct i2c_adapter *adapter = to_i2c_adapter(dev); | |
771 | struct i2c_driver *driver = data; | |
772 | ||
4735c98f JD |
773 | i2c_detect(adapter, driver); |
774 | ||
775 | /* Legacy drivers scan i2c busses directly */ | |
776 | if (driver->attach_adapter) | |
777 | driver->attach_adapter(adapter); | |
7f101a97 DY |
778 | |
779 | return 0; | |
780 | } | |
781 | ||
7b4fbc50 DB |
782 | /* |
783 | * An i2c_driver is used with one or more i2c_client (device) nodes to access | |
729d6dd5 | 784 | * i2c slave chips, on a bus instance associated with some i2c_adapter. |
1da177e4 LT |
785 | */ |
786 | ||
de59cf9e | 787 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
1da177e4 | 788 | { |
7eebcb7c | 789 | int res; |
1da177e4 | 790 | |
1d0b19c9 DB |
791 | /* Can't register until after driver model init */ |
792 | if (unlikely(WARN_ON(!i2c_bus_type.p))) | |
793 | return -EAGAIN; | |
794 | ||
1da177e4 | 795 | /* add the driver to the list of i2c drivers in the driver core */ |
de59cf9e | 796 | driver->driver.owner = owner; |
1da177e4 | 797 | driver->driver.bus = &i2c_bus_type; |
1da177e4 | 798 | |
729d6dd5 | 799 | /* When registration returns, the driver core |
6e13e641 DB |
800 | * will have called probe() for all matching-but-unbound devices. |
801 | */ | |
1da177e4 LT |
802 | res = driver_register(&driver->driver); |
803 | if (res) | |
7eebcb7c | 804 | return res; |
438d6c2c | 805 | |
35d8b2e6 | 806 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
1da177e4 | 807 | |
4735c98f JD |
808 | INIT_LIST_HEAD(&driver->clients); |
809 | /* Walk the adapters that are already present */ | |
35fc37f8 | 810 | mutex_lock(&core_lock); |
93562b53 GKH |
811 | class_for_each_device(&i2c_adapter_class, NULL, driver, |
812 | __attach_adapter); | |
7f101a97 | 813 | mutex_unlock(&core_lock); |
35fc37f8 | 814 | |
7f101a97 DY |
815 | return 0; |
816 | } | |
817 | EXPORT_SYMBOL(i2c_register_driver); | |
818 | ||
819 | static int __detach_adapter(struct device *dev, void *data) | |
820 | { | |
821 | struct i2c_adapter *adapter = to_i2c_adapter(dev); | |
822 | struct i2c_driver *driver = data; | |
4735c98f JD |
823 | struct i2c_client *client, *_n; |
824 | ||
acec211c JD |
825 | /* Remove the devices we created ourselves as the result of hardware |
826 | * probing (using a driver's detect method) */ | |
4735c98f JD |
827 | list_for_each_entry_safe(client, _n, &driver->clients, detected) { |
828 | dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", | |
829 | client->name, client->addr); | |
830 | list_del(&client->detected); | |
831 | i2c_unregister_device(client); | |
832 | } | |
833 | ||
7f101a97 DY |
834 | if (driver->detach_adapter) { |
835 | if (driver->detach_adapter(adapter)) | |
836 | dev_err(&adapter->dev, | |
837 | "detach_adapter failed for driver [%s]\n", | |
838 | driver->driver.name); | |
1da177e4 LT |
839 | } |
840 | ||
7eebcb7c | 841 | return 0; |
1da177e4 LT |
842 | } |
843 | ||
a1d9e6e4 DB |
844 | /** |
845 | * i2c_del_driver - unregister I2C driver | |
846 | * @driver: the driver being unregistered | |
d64f73be | 847 | * Context: can sleep |
a1d9e6e4 | 848 | */ |
b3e82096 | 849 | void i2c_del_driver(struct i2c_driver *driver) |
1da177e4 | 850 | { |
caada32a | 851 | mutex_lock(&core_lock); |
93562b53 GKH |
852 | class_for_each_device(&i2c_adapter_class, NULL, driver, |
853 | __detach_adapter); | |
35fc37f8 | 854 | mutex_unlock(&core_lock); |
1da177e4 LT |
855 | |
856 | driver_unregister(&driver->driver); | |
35d8b2e6 | 857 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
1da177e4 | 858 | } |
c0564606 | 859 | EXPORT_SYMBOL(i2c_del_driver); |
1da177e4 | 860 | |
7b4fbc50 DB |
861 | /* ------------------------------------------------------------------------- */ |
862 | ||
9b766b81 | 863 | static int __i2c_check_addr(struct device *dev, void *addrp) |
1da177e4 | 864 | { |
9b766b81 DB |
865 | struct i2c_client *client = i2c_verify_client(dev); |
866 | int addr = *(int *)addrp; | |
1da177e4 | 867 | |
9b766b81 DB |
868 | if (client && client->addr == addr) |
869 | return -EBUSY; | |
1da177e4 LT |
870 | return 0; |
871 | } | |
872 | ||
5e31c2bd | 873 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr) |
1da177e4 | 874 | { |
9b766b81 | 875 | return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr); |
1da177e4 LT |
876 | } |
877 | ||
e48d3319 JD |
878 | /** |
879 | * i2c_use_client - increments the reference count of the i2c client structure | |
880 | * @client: the client being referenced | |
881 | * | |
882 | * Each live reference to a client should be refcounted. The driver model does | |
883 | * that automatically as part of driver binding, so that most drivers don't | |
884 | * need to do this explicitly: they hold a reference until they're unbound | |
885 | * from the device. | |
886 | * | |
887 | * A pointer to the client with the incremented reference counter is returned. | |
888 | */ | |
889 | struct i2c_client *i2c_use_client(struct i2c_client *client) | |
1da177e4 | 890 | { |
6ea438ec DB |
891 | if (client && get_device(&client->dev)) |
892 | return client; | |
893 | return NULL; | |
1da177e4 | 894 | } |
c0564606 | 895 | EXPORT_SYMBOL(i2c_use_client); |
1da177e4 | 896 | |
e48d3319 JD |
897 | /** |
898 | * i2c_release_client - release a use of the i2c client structure | |
899 | * @client: the client being no longer referenced | |
900 | * | |
901 | * Must be called when a user of a client is finished with it. | |
902 | */ | |
903 | void i2c_release_client(struct i2c_client *client) | |
1da177e4 | 904 | { |
6ea438ec DB |
905 | if (client) |
906 | put_device(&client->dev); | |
1da177e4 | 907 | } |
c0564606 | 908 | EXPORT_SYMBOL(i2c_release_client); |
1da177e4 | 909 | |
9b766b81 DB |
910 | struct i2c_cmd_arg { |
911 | unsigned cmd; | |
912 | void *arg; | |
913 | }; | |
914 | ||
915 | static int i2c_cmd(struct device *dev, void *_arg) | |
916 | { | |
917 | struct i2c_client *client = i2c_verify_client(dev); | |
918 | struct i2c_cmd_arg *arg = _arg; | |
919 | ||
920 | if (client && client->driver && client->driver->command) | |
921 | client->driver->command(client, arg->cmd, arg->arg); | |
922 | return 0; | |
923 | } | |
924 | ||
1da177e4 LT |
925 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
926 | { | |
9b766b81 | 927 | struct i2c_cmd_arg cmd_arg; |
1da177e4 | 928 | |
9b766b81 DB |
929 | cmd_arg.cmd = cmd; |
930 | cmd_arg.arg = arg; | |
931 | device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); | |
1da177e4 | 932 | } |
c0564606 | 933 | EXPORT_SYMBOL(i2c_clients_command); |
1da177e4 LT |
934 | |
935 | static int __init i2c_init(void) | |
936 | { | |
937 | int retval; | |
938 | ||
939 | retval = bus_register(&i2c_bus_type); | |
1da177e4 LT |
940 | if (retval) |
941 | return retval; | |
e9f1373b DB |
942 | retval = class_register(&i2c_adapter_class); |
943 | if (retval) | |
944 | goto bus_err; | |
945 | retval = i2c_add_driver(&dummy_driver); | |
946 | if (retval) | |
947 | goto class_err; | |
948 | return 0; | |
949 | ||
950 | class_err: | |
951 | class_unregister(&i2c_adapter_class); | |
952 | bus_err: | |
953 | bus_unregister(&i2c_bus_type); | |
954 | return retval; | |
1da177e4 LT |
955 | } |
956 | ||
957 | static void __exit i2c_exit(void) | |
958 | { | |
e9f1373b | 959 | i2c_del_driver(&dummy_driver); |
1da177e4 | 960 | class_unregister(&i2c_adapter_class); |
1da177e4 LT |
961 | bus_unregister(&i2c_bus_type); |
962 | } | |
963 | ||
a10f9e7c DB |
964 | /* We must initialize early, because some subsystems register i2c drivers |
965 | * in subsys_initcall() code, but are linked (and initialized) before i2c. | |
966 | */ | |
967 | postcore_initcall(i2c_init); | |
1da177e4 LT |
968 | module_exit(i2c_exit); |
969 | ||
970 | /* ---------------------------------------------------- | |
971 | * the functional interface to the i2c busses. | |
972 | * ---------------------------------------------------- | |
973 | */ | |
974 | ||
a1cdedac DB |
975 | /** |
976 | * i2c_transfer - execute a single or combined I2C message | |
977 | * @adap: Handle to I2C bus | |
978 | * @msgs: One or more messages to execute before STOP is issued to | |
979 | * terminate the operation; each message begins with a START. | |
980 | * @num: Number of messages to be executed. | |
981 | * | |
982 | * Returns negative errno, else the number of messages executed. | |
983 | * | |
984 | * Note that there is no requirement that each message be sent to | |
985 | * the same slave address, although that is the most common model. | |
986 | */ | |
09b8ce0a | 987 | int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
1da177e4 | 988 | { |
66b650f0 CW |
989 | unsigned long orig_jiffies; |
990 | int ret, try; | |
1da177e4 | 991 | |
a1cdedac DB |
992 | /* REVISIT the fault reporting model here is weak: |
993 | * | |
994 | * - When we get an error after receiving N bytes from a slave, | |
995 | * there is no way to report "N". | |
996 | * | |
997 | * - When we get a NAK after transmitting N bytes to a slave, | |
998 | * there is no way to report "N" ... or to let the master | |
999 | * continue executing the rest of this combined message, if | |
1000 | * that's the appropriate response. | |
1001 | * | |
1002 | * - When for example "num" is two and we successfully complete | |
1003 | * the first message but get an error part way through the | |
1004 | * second, it's unclear whether that should be reported as | |
1005 | * one (discarding status on the second message) or errno | |
1006 | * (discarding status on the first one). | |
1007 | */ | |
1008 | ||
1da177e4 LT |
1009 | if (adap->algo->master_xfer) { |
1010 | #ifdef DEBUG | |
1011 | for (ret = 0; ret < num; ret++) { | |
1012 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " | |
209d27c3 JD |
1013 | "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) |
1014 | ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, | |
1015 | (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); | |
1da177e4 LT |
1016 | } |
1017 | #endif | |
1018 | ||
cea443a8 MR |
1019 | if (in_atomic() || irqs_disabled()) { |
1020 | ret = mutex_trylock(&adap->bus_lock); | |
1021 | if (!ret) | |
1022 | /* I2C activity is ongoing. */ | |
1023 | return -EAGAIN; | |
1024 | } else { | |
1025 | mutex_lock_nested(&adap->bus_lock, adap->level); | |
1026 | } | |
1027 | ||
66b650f0 CW |
1028 | /* Retry automatically on arbitration loss */ |
1029 | orig_jiffies = jiffies; | |
1030 | for (ret = 0, try = 0; try <= adap->retries; try++) { | |
1031 | ret = adap->algo->master_xfer(adap, msgs, num); | |
1032 | if (ret != -EAGAIN) | |
1033 | break; | |
1034 | if (time_after(jiffies, orig_jiffies + adap->timeout)) | |
1035 | break; | |
1036 | } | |
5c085d36 | 1037 | mutex_unlock(&adap->bus_lock); |
1da177e4 LT |
1038 | |
1039 | return ret; | |
1040 | } else { | |
1041 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); | |
24a5bb7b | 1042 | return -EOPNOTSUPP; |
1da177e4 LT |
1043 | } |
1044 | } | |
c0564606 | 1045 | EXPORT_SYMBOL(i2c_transfer); |
1da177e4 | 1046 | |
a1cdedac DB |
1047 | /** |
1048 | * i2c_master_send - issue a single I2C message in master transmit mode | |
1049 | * @client: Handle to slave device | |
1050 | * @buf: Data that will be written to the slave | |
1051 | * @count: How many bytes to write | |
1052 | * | |
1053 | * Returns negative errno, or else the number of bytes written. | |
1054 | */ | |
1da177e4 LT |
1055 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
1056 | { | |
1057 | int ret; | |
1058 | struct i2c_adapter *adap=client->adapter; | |
1059 | struct i2c_msg msg; | |
1060 | ||
815f55f2 JD |
1061 | msg.addr = client->addr; |
1062 | msg.flags = client->flags & I2C_M_TEN; | |
1063 | msg.len = count; | |
1064 | msg.buf = (char *)buf; | |
438d6c2c | 1065 | |
815f55f2 | 1066 | ret = i2c_transfer(adap, &msg, 1); |
1da177e4 | 1067 | |
815f55f2 JD |
1068 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
1069 | transmitted, else error code. */ | |
1070 | return (ret == 1) ? count : ret; | |
1da177e4 | 1071 | } |
c0564606 | 1072 | EXPORT_SYMBOL(i2c_master_send); |
1da177e4 | 1073 | |
a1cdedac DB |
1074 | /** |
1075 | * i2c_master_recv - issue a single I2C message in master receive mode | |
1076 | * @client: Handle to slave device | |
1077 | * @buf: Where to store data read from slave | |
1078 | * @count: How many bytes to read | |
1079 | * | |
1080 | * Returns negative errno, or else the number of bytes read. | |
1081 | */ | |
1da177e4 LT |
1082 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
1083 | { | |
1084 | struct i2c_adapter *adap=client->adapter; | |
1085 | struct i2c_msg msg; | |
1086 | int ret; | |
815f55f2 JD |
1087 | |
1088 | msg.addr = client->addr; | |
1089 | msg.flags = client->flags & I2C_M_TEN; | |
1090 | msg.flags |= I2C_M_RD; | |
1091 | msg.len = count; | |
1092 | msg.buf = buf; | |
1093 | ||
1094 | ret = i2c_transfer(adap, &msg, 1); | |
1095 | ||
1096 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes | |
1097 | transmitted, else error code. */ | |
1098 | return (ret == 1) ? count : ret; | |
1da177e4 | 1099 | } |
c0564606 | 1100 | EXPORT_SYMBOL(i2c_master_recv); |
1da177e4 | 1101 | |
1da177e4 LT |
1102 | /* ---------------------------------------------------- |
1103 | * the i2c address scanning function | |
1104 | * Will not work for 10-bit addresses! | |
1105 | * ---------------------------------------------------- | |
1106 | */ | |
1da177e4 | 1107 | |
4735c98f JD |
1108 | static int i2c_detect_address(struct i2c_client *temp_client, int kind, |
1109 | struct i2c_driver *driver) | |
1110 | { | |
1111 | struct i2c_board_info info; | |
1112 | struct i2c_adapter *adapter = temp_client->adapter; | |
1113 | int addr = temp_client->addr; | |
1114 | int err; | |
1115 | ||
1116 | /* Make sure the address is valid */ | |
1117 | if (addr < 0x03 || addr > 0x77) { | |
1118 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", | |
1119 | addr); | |
1120 | return -EINVAL; | |
1121 | } | |
1122 | ||
1123 | /* Skip if already in use */ | |
1124 | if (i2c_check_addr(adapter, addr)) | |
1125 | return 0; | |
1126 | ||
1127 | /* Make sure there is something at this address, unless forced */ | |
1128 | if (kind < 0) { | |
1129 | if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, | |
1130 | I2C_SMBUS_QUICK, NULL) < 0) | |
1131 | return 0; | |
1132 | ||
1133 | /* prevent 24RF08 corruption */ | |
1134 | if ((addr & ~0x0f) == 0x50) | |
1135 | i2c_smbus_xfer(adapter, addr, 0, 0, 0, | |
1136 | I2C_SMBUS_QUICK, NULL); | |
1137 | } | |
1138 | ||
1139 | /* Finally call the custom detection function */ | |
1140 | memset(&info, 0, sizeof(struct i2c_board_info)); | |
1141 | info.addr = addr; | |
1142 | err = driver->detect(temp_client, kind, &info); | |
1143 | if (err) { | |
1144 | /* -ENODEV is returned if the detection fails. We catch it | |
1145 | here as this isn't an error. */ | |
1146 | return err == -ENODEV ? 0 : err; | |
1147 | } | |
1148 | ||
1149 | /* Consistency check */ | |
1150 | if (info.type[0] == '\0') { | |
1151 | dev_err(&adapter->dev, "%s detection function provided " | |
1152 | "no name for 0x%x\n", driver->driver.name, | |
1153 | addr); | |
1154 | } else { | |
1155 | struct i2c_client *client; | |
1156 | ||
1157 | /* Detection succeeded, instantiate the device */ | |
1158 | dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", | |
1159 | info.type, info.addr); | |
1160 | client = i2c_new_device(adapter, &info); | |
1161 | if (client) | |
1162 | list_add_tail(&client->detected, &driver->clients); | |
1163 | else | |
1164 | dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", | |
1165 | info.type, info.addr); | |
1166 | } | |
1167 | return 0; | |
1168 | } | |
1169 | ||
1170 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) | |
1171 | { | |
1172 | const struct i2c_client_address_data *address_data; | |
1173 | struct i2c_client *temp_client; | |
1174 | int i, err = 0; | |
1175 | int adap_id = i2c_adapter_id(adapter); | |
1176 | ||
1177 | address_data = driver->address_data; | |
1178 | if (!driver->detect || !address_data) | |
1179 | return 0; | |
1180 | ||
1181 | /* Set up a temporary client to help detect callback */ | |
1182 | temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); | |
1183 | if (!temp_client) | |
1184 | return -ENOMEM; | |
1185 | temp_client->adapter = adapter; | |
1186 | ||
1187 | /* Force entries are done first, and are not affected by ignore | |
1188 | entries */ | |
1189 | if (address_data->forces) { | |
1190 | const unsigned short * const *forces = address_data->forces; | |
1191 | int kind; | |
1192 | ||
1193 | for (kind = 0; forces[kind]; kind++) { | |
1194 | for (i = 0; forces[kind][i] != I2C_CLIENT_END; | |
1195 | i += 2) { | |
1196 | if (forces[kind][i] == adap_id | |
1197 | || forces[kind][i] == ANY_I2C_BUS) { | |
1198 | dev_dbg(&adapter->dev, "found force " | |
1199 | "parameter for adapter %d, " | |
1200 | "addr 0x%02x, kind %d\n", | |
1201 | adap_id, forces[kind][i + 1], | |
1202 | kind); | |
1203 | temp_client->addr = forces[kind][i + 1]; | |
1204 | err = i2c_detect_address(temp_client, | |
1205 | kind, driver); | |
1206 | if (err) | |
1207 | goto exit_free; | |
1208 | } | |
1209 | } | |
1210 | } | |
1211 | } | |
1212 | ||
4329cf86 JD |
1213 | /* Stop here if the classes do not match */ |
1214 | if (!(adapter->class & driver->class)) | |
1215 | goto exit_free; | |
1216 | ||
4735c98f JD |
1217 | /* Stop here if we can't use SMBUS_QUICK */ |
1218 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { | |
1219 | if (address_data->probe[0] == I2C_CLIENT_END | |
1220 | && address_data->normal_i2c[0] == I2C_CLIENT_END) | |
1221 | goto exit_free; | |
1222 | ||
1223 | dev_warn(&adapter->dev, "SMBus Quick command not supported, " | |
1224 | "can't probe for chips\n"); | |
1225 | err = -EOPNOTSUPP; | |
1226 | goto exit_free; | |
1227 | } | |
1228 | ||
4735c98f JD |
1229 | /* Probe entries are done second, and are not affected by ignore |
1230 | entries either */ | |
1231 | for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { | |
1232 | if (address_data->probe[i] == adap_id | |
1233 | || address_data->probe[i] == ANY_I2C_BUS) { | |
1234 | dev_dbg(&adapter->dev, "found probe parameter for " | |
1235 | "adapter %d, addr 0x%02x\n", adap_id, | |
1236 | address_data->probe[i + 1]); | |
1237 | temp_client->addr = address_data->probe[i + 1]; | |
1238 | err = i2c_detect_address(temp_client, -1, driver); | |
1239 | if (err) | |
1240 | goto exit_free; | |
1241 | } | |
1242 | } | |
1243 | ||
1244 | /* Normal entries are done last, unless shadowed by an ignore entry */ | |
1245 | for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { | |
1246 | int j, ignore; | |
1247 | ||
1248 | ignore = 0; | |
1249 | for (j = 0; address_data->ignore[j] != I2C_CLIENT_END; | |
1250 | j += 2) { | |
1251 | if ((address_data->ignore[j] == adap_id || | |
1252 | address_data->ignore[j] == ANY_I2C_BUS) | |
1253 | && address_data->ignore[j + 1] | |
1254 | == address_data->normal_i2c[i]) { | |
1255 | dev_dbg(&adapter->dev, "found ignore " | |
1256 | "parameter for adapter %d, " | |
1257 | "addr 0x%02x\n", adap_id, | |
1258 | address_data->ignore[j + 1]); | |
1259 | ignore = 1; | |
1260 | break; | |
1261 | } | |
1262 | } | |
1263 | if (ignore) | |
1264 | continue; | |
1265 | ||
1266 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " | |
1267 | "addr 0x%02x\n", adap_id, | |
1268 | address_data->normal_i2c[i]); | |
1269 | temp_client->addr = address_data->normal_i2c[i]; | |
1270 | err = i2c_detect_address(temp_client, -1, driver); | |
1271 | if (err) | |
1272 | goto exit_free; | |
1273 | } | |
1274 | ||
1275 | exit_free: | |
1276 | kfree(temp_client); | |
1277 | return err; | |
1278 | } | |
1279 | ||
12b5053a JD |
1280 | struct i2c_client * |
1281 | i2c_new_probed_device(struct i2c_adapter *adap, | |
1282 | struct i2c_board_info *info, | |
1283 | unsigned short const *addr_list) | |
1284 | { | |
1285 | int i; | |
1286 | ||
1287 | /* Stop here if the bus doesn't support probing */ | |
1288 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) { | |
1289 | dev_err(&adap->dev, "Probing not supported\n"); | |
1290 | return NULL; | |
1291 | } | |
1292 | ||
12b5053a JD |
1293 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { |
1294 | /* Check address validity */ | |
1295 | if (addr_list[i] < 0x03 || addr_list[i] > 0x77) { | |
1296 | dev_warn(&adap->dev, "Invalid 7-bit address " | |
1297 | "0x%02x\n", addr_list[i]); | |
1298 | continue; | |
1299 | } | |
1300 | ||
1301 | /* Check address availability */ | |
9b766b81 | 1302 | if (i2c_check_addr(adap, addr_list[i])) { |
12b5053a JD |
1303 | dev_dbg(&adap->dev, "Address 0x%02x already in " |
1304 | "use, not probing\n", addr_list[i]); | |
1305 | continue; | |
1306 | } | |
1307 | ||
1308 | /* Test address responsiveness | |
1309 | The default probe method is a quick write, but it is known | |
1310 | to corrupt the 24RF08 EEPROMs due to a state machine bug, | |
1311 | and could also irreversibly write-protect some EEPROMs, so | |
1312 | for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte | |
1313 | read instead. Also, some bus drivers don't implement | |
1314 | quick write, so we fallback to a byte read it that case | |
1315 | too. */ | |
1316 | if ((addr_list[i] & ~0x07) == 0x30 | |
1317 | || (addr_list[i] & ~0x0f) == 0x50 | |
1318 | || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) { | |
b25b791b HV |
1319 | union i2c_smbus_data data; |
1320 | ||
12b5053a JD |
1321 | if (i2c_smbus_xfer(adap, addr_list[i], 0, |
1322 | I2C_SMBUS_READ, 0, | |
b25b791b | 1323 | I2C_SMBUS_BYTE, &data) >= 0) |
12b5053a JD |
1324 | break; |
1325 | } else { | |
1326 | if (i2c_smbus_xfer(adap, addr_list[i], 0, | |
1327 | I2C_SMBUS_WRITE, 0, | |
1328 | I2C_SMBUS_QUICK, NULL) >= 0) | |
1329 | break; | |
1330 | } | |
1331 | } | |
12b5053a JD |
1332 | |
1333 | if (addr_list[i] == I2C_CLIENT_END) { | |
1334 | dev_dbg(&adap->dev, "Probing failed, no device found\n"); | |
1335 | return NULL; | |
1336 | } | |
1337 | ||
1338 | info->addr = addr_list[i]; | |
1339 | return i2c_new_device(adap, info); | |
1340 | } | |
1341 | EXPORT_SYMBOL_GPL(i2c_new_probed_device); | |
1342 | ||
1da177e4 LT |
1343 | struct i2c_adapter* i2c_get_adapter(int id) |
1344 | { | |
1da177e4 | 1345 | struct i2c_adapter *adapter; |
438d6c2c | 1346 | |
caada32a | 1347 | mutex_lock(&core_lock); |
1cf92b45 | 1348 | adapter = idr_find(&i2c_adapter_idr, id); |
a0920e10 MH |
1349 | if (adapter && !try_module_get(adapter->owner)) |
1350 | adapter = NULL; | |
1351 | ||
caada32a | 1352 | mutex_unlock(&core_lock); |
a0920e10 | 1353 | return adapter; |
1da177e4 | 1354 | } |
c0564606 | 1355 | EXPORT_SYMBOL(i2c_get_adapter); |
1da177e4 LT |
1356 | |
1357 | void i2c_put_adapter(struct i2c_adapter *adap) | |
1358 | { | |
1359 | module_put(adap->owner); | |
1360 | } | |
c0564606 | 1361 | EXPORT_SYMBOL(i2c_put_adapter); |
1da177e4 LT |
1362 | |
1363 | /* The SMBus parts */ | |
1364 | ||
438d6c2c | 1365 | #define POLY (0x1070U << 3) |
09b8ce0a | 1366 | static u8 crc8(u16 data) |
1da177e4 LT |
1367 | { |
1368 | int i; | |
438d6c2c | 1369 | |
1da177e4 | 1370 | for(i = 0; i < 8; i++) { |
438d6c2c | 1371 | if (data & 0x8000) |
1da177e4 LT |
1372 | data = data ^ POLY; |
1373 | data = data << 1; | |
1374 | } | |
1375 | return (u8)(data >> 8); | |
1376 | } | |
1377 | ||
421ef47b JD |
1378 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
1379 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) | |
1da177e4 LT |
1380 | { |
1381 | int i; | |
1382 | ||
1383 | for(i = 0; i < count; i++) | |
421ef47b | 1384 | crc = crc8((crc ^ p[i]) << 8); |
1da177e4 LT |
1385 | return crc; |
1386 | } | |
1387 | ||
421ef47b JD |
1388 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
1389 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) | |
1da177e4 | 1390 | { |
421ef47b JD |
1391 | /* The address will be sent first */ |
1392 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); | |
1393 | pec = i2c_smbus_pec(pec, &addr, 1); | |
1394 | ||
1395 | /* The data buffer follows */ | |
1396 | return i2c_smbus_pec(pec, msg->buf, msg->len); | |
1da177e4 LT |
1397 | } |
1398 | ||
421ef47b JD |
1399 | /* Used for write only transactions */ |
1400 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) | |
1da177e4 | 1401 | { |
421ef47b JD |
1402 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
1403 | msg->len++; | |
1da177e4 LT |
1404 | } |
1405 | ||
421ef47b JD |
1406 | /* Return <0 on CRC error |
1407 | If there was a write before this read (most cases) we need to take the | |
1408 | partial CRC from the write part into account. | |
1409 | Note that this function does modify the message (we need to decrease the | |
1410 | message length to hide the CRC byte from the caller). */ | |
1411 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) | |
1da177e4 | 1412 | { |
421ef47b JD |
1413 | u8 rpec = msg->buf[--msg->len]; |
1414 | cpec = i2c_smbus_msg_pec(cpec, msg); | |
1da177e4 | 1415 | |
1da177e4 LT |
1416 | if (rpec != cpec) { |
1417 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", | |
1418 | rpec, cpec); | |
24a5bb7b | 1419 | return -EBADMSG; |
1da177e4 | 1420 | } |
438d6c2c | 1421 | return 0; |
1da177e4 LT |
1422 | } |
1423 | ||
a1cdedac DB |
1424 | /** |
1425 | * i2c_smbus_read_byte - SMBus "receive byte" protocol | |
1426 | * @client: Handle to slave device | |
1427 | * | |
1428 | * This executes the SMBus "receive byte" protocol, returning negative errno | |
1429 | * else the byte received from the device. | |
1430 | */ | |
1da177e4 LT |
1431 | s32 i2c_smbus_read_byte(struct i2c_client *client) |
1432 | { | |
1433 | union i2c_smbus_data data; | |
24a5bb7b DB |
1434 | int status; |
1435 | ||
1436 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1437 | I2C_SMBUS_READ, 0, | |
1438 | I2C_SMBUS_BYTE, &data); | |
1439 | return (status < 0) ? status : data.byte; | |
1da177e4 | 1440 | } |
c0564606 | 1441 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
1da177e4 | 1442 | |
a1cdedac DB |
1443 | /** |
1444 | * i2c_smbus_write_byte - SMBus "send byte" protocol | |
1445 | * @client: Handle to slave device | |
1446 | * @value: Byte to be sent | |
1447 | * | |
1448 | * This executes the SMBus "send byte" protocol, returning negative errno | |
1449 | * else zero on success. | |
1450 | */ | |
1da177e4 LT |
1451 | s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) |
1452 | { | |
1da177e4 | 1453 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
421ef47b | 1454 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
1da177e4 | 1455 | } |
c0564606 | 1456 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
1da177e4 | 1457 | |
a1cdedac DB |
1458 | /** |
1459 | * i2c_smbus_read_byte_data - SMBus "read byte" protocol | |
1460 | * @client: Handle to slave device | |
1461 | * @command: Byte interpreted by slave | |
1462 | * | |
1463 | * This executes the SMBus "read byte" protocol, returning negative errno | |
1464 | * else a data byte received from the device. | |
1465 | */ | |
1da177e4 LT |
1466 | s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) |
1467 | { | |
1468 | union i2c_smbus_data data; | |
24a5bb7b DB |
1469 | int status; |
1470 | ||
1471 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1472 | I2C_SMBUS_READ, command, | |
1473 | I2C_SMBUS_BYTE_DATA, &data); | |
1474 | return (status < 0) ? status : data.byte; | |
1da177e4 | 1475 | } |
c0564606 | 1476 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
1da177e4 | 1477 | |
a1cdedac DB |
1478 | /** |
1479 | * i2c_smbus_write_byte_data - SMBus "write byte" protocol | |
1480 | * @client: Handle to slave device | |
1481 | * @command: Byte interpreted by slave | |
1482 | * @value: Byte being written | |
1483 | * | |
1484 | * This executes the SMBus "write byte" protocol, returning negative errno | |
1485 | * else zero on success. | |
1486 | */ | |
1da177e4 LT |
1487 | s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) |
1488 | { | |
1489 | union i2c_smbus_data data; | |
1490 | data.byte = value; | |
1491 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1492 | I2C_SMBUS_WRITE,command, | |
1493 | I2C_SMBUS_BYTE_DATA,&data); | |
1494 | } | |
c0564606 | 1495 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
1da177e4 | 1496 | |
a1cdedac DB |
1497 | /** |
1498 | * i2c_smbus_read_word_data - SMBus "read word" protocol | |
1499 | * @client: Handle to slave device | |
1500 | * @command: Byte interpreted by slave | |
1501 | * | |
1502 | * This executes the SMBus "read word" protocol, returning negative errno | |
1503 | * else a 16-bit unsigned "word" received from the device. | |
1504 | */ | |
1da177e4 LT |
1505 | s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) |
1506 | { | |
1507 | union i2c_smbus_data data; | |
24a5bb7b DB |
1508 | int status; |
1509 | ||
1510 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1511 | I2C_SMBUS_READ, command, | |
1512 | I2C_SMBUS_WORD_DATA, &data); | |
1513 | return (status < 0) ? status : data.word; | |
1da177e4 | 1514 | } |
c0564606 | 1515 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
1da177e4 | 1516 | |
a1cdedac DB |
1517 | /** |
1518 | * i2c_smbus_write_word_data - SMBus "write word" protocol | |
1519 | * @client: Handle to slave device | |
1520 | * @command: Byte interpreted by slave | |
1521 | * @value: 16-bit "word" being written | |
1522 | * | |
1523 | * This executes the SMBus "write word" protocol, returning negative errno | |
1524 | * else zero on success. | |
1525 | */ | |
1da177e4 LT |
1526 | s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) |
1527 | { | |
1528 | union i2c_smbus_data data; | |
1529 | data.word = value; | |
1530 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1531 | I2C_SMBUS_WRITE,command, | |
1532 | I2C_SMBUS_WORD_DATA,&data); | |
1533 | } | |
c0564606 | 1534 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
1da177e4 | 1535 | |
596c88f4 PM |
1536 | /** |
1537 | * i2c_smbus_process_call - SMBus "process call" protocol | |
1538 | * @client: Handle to slave device | |
1539 | * @command: Byte interpreted by slave | |
1540 | * @value: 16-bit "word" being written | |
1541 | * | |
1542 | * This executes the SMBus "process call" protocol, returning negative errno | |
1543 | * else a 16-bit unsigned "word" received from the device. | |
1544 | */ | |
1545 | s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value) | |
1546 | { | |
1547 | union i2c_smbus_data data; | |
1548 | int status; | |
1549 | data.word = value; | |
1550 | ||
1551 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1552 | I2C_SMBUS_WRITE, command, | |
1553 | I2C_SMBUS_PROC_CALL, &data); | |
1554 | return (status < 0) ? status : data.word; | |
1555 | } | |
1556 | EXPORT_SYMBOL(i2c_smbus_process_call); | |
1557 | ||
a64ec07d | 1558 | /** |
a1cdedac | 1559 | * i2c_smbus_read_block_data - SMBus "block read" protocol |
a64ec07d | 1560 | * @client: Handle to slave device |
a1cdedac | 1561 | * @command: Byte interpreted by slave |
a64ec07d DB |
1562 | * @values: Byte array into which data will be read; big enough to hold |
1563 | * the data returned by the slave. SMBus allows at most 32 bytes. | |
1564 | * | |
a1cdedac DB |
1565 | * This executes the SMBus "block read" protocol, returning negative errno |
1566 | * else the number of data bytes in the slave's response. | |
a64ec07d DB |
1567 | * |
1568 | * Note that using this function requires that the client's adapter support | |
1569 | * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers | |
1570 | * support this; its emulation through I2C messaging relies on a specific | |
1571 | * mechanism (I2C_M_RECV_LEN) which may not be implemented. | |
1572 | */ | |
b86a1bc8 JD |
1573 | s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command, |
1574 | u8 *values) | |
1575 | { | |
1576 | union i2c_smbus_data data; | |
24a5bb7b | 1577 | int status; |
b86a1bc8 | 1578 | |
24a5bb7b DB |
1579 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
1580 | I2C_SMBUS_READ, command, | |
1581 | I2C_SMBUS_BLOCK_DATA, &data); | |
1582 | if (status) | |
1583 | return status; | |
b86a1bc8 JD |
1584 | |
1585 | memcpy(values, &data.block[1], data.block[0]); | |
1586 | return data.block[0]; | |
1587 | } | |
1588 | EXPORT_SYMBOL(i2c_smbus_read_block_data); | |
1589 | ||
a1cdedac DB |
1590 | /** |
1591 | * i2c_smbus_write_block_data - SMBus "block write" protocol | |
1592 | * @client: Handle to slave device | |
1593 | * @command: Byte interpreted by slave | |
1594 | * @length: Size of data block; SMBus allows at most 32 bytes | |
1595 | * @values: Byte array which will be written. | |
1596 | * | |
1597 | * This executes the SMBus "block write" protocol, returning negative errno | |
1598 | * else zero on success. | |
1599 | */ | |
1da177e4 | 1600 | s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, |
46f5ed75 | 1601 | u8 length, const u8 *values) |
1da177e4 LT |
1602 | { |
1603 | union i2c_smbus_data data; | |
7656032b | 1604 | |
1da177e4 LT |
1605 | if (length > I2C_SMBUS_BLOCK_MAX) |
1606 | length = I2C_SMBUS_BLOCK_MAX; | |
1da177e4 | 1607 | data.block[0] = length; |
7656032b | 1608 | memcpy(&data.block[1], values, length); |
1da177e4 LT |
1609 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
1610 | I2C_SMBUS_WRITE,command, | |
1611 | I2C_SMBUS_BLOCK_DATA,&data); | |
1612 | } | |
c0564606 | 1613 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
1da177e4 LT |
1614 | |
1615 | /* Returns the number of read bytes */ | |
4b2643d7 JD |
1616 | s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, |
1617 | u8 length, u8 *values) | |
1da177e4 LT |
1618 | { |
1619 | union i2c_smbus_data data; | |
24a5bb7b | 1620 | int status; |
7656032b | 1621 | |
4b2643d7 JD |
1622 | if (length > I2C_SMBUS_BLOCK_MAX) |
1623 | length = I2C_SMBUS_BLOCK_MAX; | |
1624 | data.block[0] = length; | |
24a5bb7b DB |
1625 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
1626 | I2C_SMBUS_READ, command, | |
1627 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
1628 | if (status < 0) | |
1629 | return status; | |
7656032b JD |
1630 | |
1631 | memcpy(values, &data.block[1], data.block[0]); | |
1632 | return data.block[0]; | |
1da177e4 | 1633 | } |
c0564606 | 1634 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
1da177e4 | 1635 | |
21bbd691 | 1636 | s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command, |
46f5ed75 | 1637 | u8 length, const u8 *values) |
21bbd691 JD |
1638 | { |
1639 | union i2c_smbus_data data; | |
1640 | ||
1641 | if (length > I2C_SMBUS_BLOCK_MAX) | |
1642 | length = I2C_SMBUS_BLOCK_MAX; | |
1643 | data.block[0] = length; | |
1644 | memcpy(data.block + 1, values, length); | |
1645 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1646 | I2C_SMBUS_WRITE, command, | |
1647 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
1648 | } | |
c0564606 | 1649 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
21bbd691 | 1650 | |
438d6c2c | 1651 | /* Simulate a SMBus command using the i2c protocol |
1da177e4 | 1652 | No checking of parameters is done! */ |
438d6c2c | 1653 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
1da177e4 | 1654 | unsigned short flags, |
438d6c2c | 1655 | char read_write, u8 command, int size, |
1da177e4 LT |
1656 | union i2c_smbus_data * data) |
1657 | { | |
1658 | /* So we need to generate a series of msgs. In the case of writing, we | |
1659 | need to use only one message; when reading, we need two. We initialize | |
1660 | most things with sane defaults, to keep the code below somewhat | |
1661 | simpler. */ | |
5c50d188 HI |
1662 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
1663 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; | |
1da177e4 | 1664 | int num = read_write == I2C_SMBUS_READ?2:1; |
438d6c2c | 1665 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
1da177e4 LT |
1666 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
1667 | }; | |
1668 | int i; | |
421ef47b | 1669 | u8 partial_pec = 0; |
24a5bb7b | 1670 | int status; |
1da177e4 LT |
1671 | |
1672 | msgbuf0[0] = command; | |
1673 | switch(size) { | |
1674 | case I2C_SMBUS_QUICK: | |
1675 | msg[0].len = 0; | |
1676 | /* Special case: The read/write field is used as data */ | |
f29d2e02 RK |
1677 | msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? |
1678 | I2C_M_RD : 0); | |
1da177e4 LT |
1679 | num = 1; |
1680 | break; | |
1681 | case I2C_SMBUS_BYTE: | |
1682 | if (read_write == I2C_SMBUS_READ) { | |
1683 | /* Special case: only a read! */ | |
1684 | msg[0].flags = I2C_M_RD | flags; | |
1685 | num = 1; | |
1686 | } | |
1687 | break; | |
1688 | case I2C_SMBUS_BYTE_DATA: | |
1689 | if (read_write == I2C_SMBUS_READ) | |
1690 | msg[1].len = 1; | |
1691 | else { | |
1692 | msg[0].len = 2; | |
1693 | msgbuf0[1] = data->byte; | |
1694 | } | |
1695 | break; | |
1696 | case I2C_SMBUS_WORD_DATA: | |
1697 | if (read_write == I2C_SMBUS_READ) | |
1698 | msg[1].len = 2; | |
1699 | else { | |
1700 | msg[0].len=3; | |
1701 | msgbuf0[1] = data->word & 0xff; | |
7eff82c8 | 1702 | msgbuf0[2] = data->word >> 8; |
1da177e4 LT |
1703 | } |
1704 | break; | |
1705 | case I2C_SMBUS_PROC_CALL: | |
1706 | num = 2; /* Special case */ | |
1707 | read_write = I2C_SMBUS_READ; | |
1708 | msg[0].len = 3; | |
1709 | msg[1].len = 2; | |
1710 | msgbuf0[1] = data->word & 0xff; | |
7eff82c8 | 1711 | msgbuf0[2] = data->word >> 8; |
1da177e4 LT |
1712 | break; |
1713 | case I2C_SMBUS_BLOCK_DATA: | |
1da177e4 | 1714 | if (read_write == I2C_SMBUS_READ) { |
209d27c3 JD |
1715 | msg[1].flags |= I2C_M_RECV_LEN; |
1716 | msg[1].len = 1; /* block length will be added by | |
1717 | the underlying bus driver */ | |
1da177e4 LT |
1718 | } else { |
1719 | msg[0].len = data->block[0] + 2; | |
1720 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { | |
24a5bb7b DB |
1721 | dev_err(&adapter->dev, |
1722 | "Invalid block write size %d\n", | |
1723 | data->block[0]); | |
1724 | return -EINVAL; | |
1da177e4 | 1725 | } |
5c50d188 | 1726 | for (i = 1; i < msg[0].len; i++) |
1da177e4 LT |
1727 | msgbuf0[i] = data->block[i-1]; |
1728 | } | |
1729 | break; | |
1730 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
209d27c3 JD |
1731 | num = 2; /* Another special case */ |
1732 | read_write = I2C_SMBUS_READ; | |
1733 | if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { | |
24a5bb7b DB |
1734 | dev_err(&adapter->dev, |
1735 | "Invalid block write size %d\n", | |
209d27c3 | 1736 | data->block[0]); |
24a5bb7b | 1737 | return -EINVAL; |
209d27c3 JD |
1738 | } |
1739 | msg[0].len = data->block[0] + 2; | |
1740 | for (i = 1; i < msg[0].len; i++) | |
1741 | msgbuf0[i] = data->block[i-1]; | |
1742 | msg[1].flags |= I2C_M_RECV_LEN; | |
1743 | msg[1].len = 1; /* block length will be added by | |
1744 | the underlying bus driver */ | |
1745 | break; | |
1da177e4 LT |
1746 | case I2C_SMBUS_I2C_BLOCK_DATA: |
1747 | if (read_write == I2C_SMBUS_READ) { | |
4b2643d7 | 1748 | msg[1].len = data->block[0]; |
1da177e4 LT |
1749 | } else { |
1750 | msg[0].len = data->block[0] + 1; | |
30dac746 | 1751 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
24a5bb7b DB |
1752 | dev_err(&adapter->dev, |
1753 | "Invalid block write size %d\n", | |
1754 | data->block[0]); | |
1755 | return -EINVAL; | |
1da177e4 LT |
1756 | } |
1757 | for (i = 1; i <= data->block[0]; i++) | |
1758 | msgbuf0[i] = data->block[i]; | |
1759 | } | |
1760 | break; | |
1761 | default: | |
24a5bb7b DB |
1762 | dev_err(&adapter->dev, "Unsupported transaction %d\n", size); |
1763 | return -EOPNOTSUPP; | |
1da177e4 LT |
1764 | } |
1765 | ||
421ef47b JD |
1766 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
1767 | && size != I2C_SMBUS_I2C_BLOCK_DATA); | |
1768 | if (i) { | |
1769 | /* Compute PEC if first message is a write */ | |
1770 | if (!(msg[0].flags & I2C_M_RD)) { | |
438d6c2c | 1771 | if (num == 1) /* Write only */ |
421ef47b JD |
1772 | i2c_smbus_add_pec(&msg[0]); |
1773 | else /* Write followed by read */ | |
1774 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); | |
1775 | } | |
1776 | /* Ask for PEC if last message is a read */ | |
1777 | if (msg[num-1].flags & I2C_M_RD) | |
438d6c2c | 1778 | msg[num-1].len++; |
421ef47b JD |
1779 | } |
1780 | ||
24a5bb7b DB |
1781 | status = i2c_transfer(adapter, msg, num); |
1782 | if (status < 0) | |
1783 | return status; | |
1da177e4 | 1784 | |
421ef47b JD |
1785 | /* Check PEC if last message is a read */ |
1786 | if (i && (msg[num-1].flags & I2C_M_RD)) { | |
24a5bb7b DB |
1787 | status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); |
1788 | if (status < 0) | |
1789 | return status; | |
421ef47b JD |
1790 | } |
1791 | ||
1da177e4 LT |
1792 | if (read_write == I2C_SMBUS_READ) |
1793 | switch(size) { | |
1794 | case I2C_SMBUS_BYTE: | |
1795 | data->byte = msgbuf0[0]; | |
1796 | break; | |
1797 | case I2C_SMBUS_BYTE_DATA: | |
1798 | data->byte = msgbuf1[0]; | |
1799 | break; | |
438d6c2c | 1800 | case I2C_SMBUS_WORD_DATA: |
1da177e4 LT |
1801 | case I2C_SMBUS_PROC_CALL: |
1802 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); | |
1803 | break; | |
1804 | case I2C_SMBUS_I2C_BLOCK_DATA: | |
4b2643d7 | 1805 | for (i = 0; i < data->block[0]; i++) |
1da177e4 LT |
1806 | data->block[i+1] = msgbuf1[i]; |
1807 | break; | |
209d27c3 JD |
1808 | case I2C_SMBUS_BLOCK_DATA: |
1809 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
1810 | for (i = 0; i < msgbuf1[0] + 1; i++) | |
1811 | data->block[i] = msgbuf1[i]; | |
1812 | break; | |
1da177e4 LT |
1813 | } |
1814 | return 0; | |
1815 | } | |
1816 | ||
a1cdedac DB |
1817 | /** |
1818 | * i2c_smbus_xfer - execute SMBus protocol operations | |
1819 | * @adapter: Handle to I2C bus | |
1820 | * @addr: Address of SMBus slave on that bus | |
1821 | * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) | |
1822 | * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE | |
1823 | * @command: Byte interpreted by slave, for protocols which use such bytes | |
1824 | * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL | |
1825 | * @data: Data to be read or written | |
1826 | * | |
1827 | * This executes an SMBus protocol operation, and returns a negative | |
1828 | * errno code else zero on success. | |
1829 | */ | |
09b8ce0a | 1830 | s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, |
a1cdedac | 1831 | char read_write, u8 command, int protocol, |
09b8ce0a | 1832 | union i2c_smbus_data *data) |
1da177e4 | 1833 | { |
66b650f0 CW |
1834 | unsigned long orig_jiffies; |
1835 | int try; | |
1da177e4 | 1836 | s32 res; |
1da177e4 LT |
1837 | |
1838 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; | |
1da177e4 LT |
1839 | |
1840 | if (adapter->algo->smbus_xfer) { | |
5c085d36 | 1841 | mutex_lock(&adapter->bus_lock); |
66b650f0 CW |
1842 | |
1843 | /* Retry automatically on arbitration loss */ | |
1844 | orig_jiffies = jiffies; | |
1845 | for (res = 0, try = 0; try <= adapter->retries; try++) { | |
1846 | res = adapter->algo->smbus_xfer(adapter, addr, flags, | |
1847 | read_write, command, | |
1848 | protocol, data); | |
1849 | if (res != -EAGAIN) | |
1850 | break; | |
1851 | if (time_after(jiffies, | |
1852 | orig_jiffies + adapter->timeout)) | |
1853 | break; | |
1854 | } | |
5c085d36 | 1855 | mutex_unlock(&adapter->bus_lock); |
1da177e4 LT |
1856 | } else |
1857 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, | |
a1cdedac | 1858 | command, protocol, data); |
1da177e4 | 1859 | |
1da177e4 LT |
1860 | return res; |
1861 | } | |
1da177e4 | 1862 | EXPORT_SYMBOL(i2c_smbus_xfer); |
1da177e4 LT |
1863 | |
1864 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); | |
1865 | MODULE_DESCRIPTION("I2C-Bus main module"); | |
1866 | MODULE_LICENSE("GPL"); |