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