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