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