blob: 1a2c9ab5d9e326a21a6be05b09cef994c2ed7286 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
20/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
32#include <linux/seq_file.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/uaccess.h>
35
36
37static LIST_HEAD(adapters);
38static LIST_HEAD(drivers);
39static DECLARE_MUTEX(core_lists);
40static DEFINE_IDR(i2c_adapter_idr);
41
42/* match always succeeds, as we want the probe() to tell if we really accept this match */
43static int i2c_device_match(struct device *dev, struct device_driver *drv)
44{
45 return 1;
46}
47
48static int i2c_bus_suspend(struct device * dev, pm_message_t state)
49{
50 int rc = 0;
51
52 if (dev->driver && dev->driver->suspend)
Russell King9480e302005-10-28 09:52:56 -070053 rc = dev->driver->suspend(dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return rc;
55}
56
57static int i2c_bus_resume(struct device * dev)
58{
59 int rc = 0;
60
61 if (dev->driver && dev->driver->resume)
Russell King9480e302005-10-28 09:52:56 -070062 rc = dev->driver->resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return rc;
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static int i2c_device_probe(struct device *dev)
67{
68 return -ENODEV;
69}
70
71static int i2c_device_remove(struct device *dev)
72{
73 return 0;
74}
75
Russell Kingb864c7d2006-01-05 14:37:50 +000076struct bus_type i2c_bus_type = {
77 .name = "i2c",
78 .match = i2c_device_match,
79 .probe = i2c_device_probe,
80 .remove = i2c_device_remove,
81 .suspend = i2c_bus_suspend,
82 .resume = i2c_bus_resume,
83};
84
Jean Delvareefde7232005-07-20 23:03:50 +020085void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
88 complete(&adap->dev_released);
89}
90
Jean Delvareefde7232005-07-20 23:03:50 +020091struct device_driver i2c_adapter_driver = {
Laurent Riffard6586bcd2005-10-17 22:54:45 +020092 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .name = "i2c_adapter",
94 .bus = &i2c_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97static void i2c_adapter_class_dev_release(struct class_device *dev)
98{
99 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
100 complete(&adap->class_dev_released);
101}
102
Jean Delvareefde7232005-07-20 23:03:50 +0200103struct class i2c_adapter_class = {
Laurent Riffard6586bcd2005-10-17 22:54:45 +0200104 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .name = "i2c-adapter",
106 .release = &i2c_adapter_class_dev_release,
107};
108
Yani Ioannoue404e272005-05-17 06:42:58 -0400109static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
112 return sprintf(buf, "%s\n", adap->name);
113}
114static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
115
116
117static void i2c_client_release(struct device *dev)
118{
119 struct i2c_client *client = to_i2c_client(dev);
120 complete(&client->released);
121}
122
Yani Ioannoue404e272005-05-17 06:42:58 -0400123static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct i2c_client *client = to_i2c_client(dev);
126 return sprintf(buf, "%s\n", client->name);
127}
128
129/*
130 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
131 * different type of a device. So beware if the DEVICE_ATTR() macro ever
132 * changes, this definition will also have to change.
133 */
134static struct device_attribute dev_attr_client_name = {
135 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
136 .show = &show_client_name,
137};
138
139
140/* ---------------------------------------------------
141 * registering functions
142 * ---------------------------------------------------
143 */
144
145/* -----
146 * i2c_add_adapter is called from within the algorithm layer,
147 * when a new hw adapter registers. A new device is register to be
148 * available for clients.
149 */
150int i2c_add_adapter(struct i2c_adapter *adap)
151{
152 int id, res = 0;
153 struct list_head *item;
154 struct i2c_driver *driver;
155
156 down(&core_lists);
157
158 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
159 res = -ENOMEM;
160 goto out_unlock;
161 }
162
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400163 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (res < 0) {
165 if (res == -EAGAIN)
166 res = -ENOMEM;
167 goto out_unlock;
168 }
169
170 adap->nr = id & MAX_ID_MASK;
171 init_MUTEX(&adap->bus_lock);
172 init_MUTEX(&adap->clist_lock);
173 list_add_tail(&adap->list,&adapters);
174 INIT_LIST_HEAD(&adap->clients);
175
176 /* Add the adapter to the driver core.
177 * If the parent pointer is not set up,
178 * we add this adapter to the host bus.
179 */
180 if (adap->dev.parent == NULL)
181 adap->dev.parent = &platform_bus;
182 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
183 adap->dev.driver = &i2c_adapter_driver;
184 adap->dev.release = &i2c_adapter_dev_release;
185 device_register(&adap->dev);
186 device_create_file(&adap->dev, &dev_attr_name);
187
188 /* Add this adapter to the i2c_adapter class */
189 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
190 adap->class_dev.dev = &adap->dev;
191 adap->class_dev.class = &i2c_adapter_class;
192 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
193 class_device_register(&adap->class_dev);
194
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200195 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* inform drivers of new adapters */
198 list_for_each(item,&drivers) {
199 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100200 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* We ignore the return code; if it fails, too bad */
202 driver->attach_adapter(adap);
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205out_unlock:
206 up(&core_lists);
207 return res;
208}
209
210
211int i2c_del_adapter(struct i2c_adapter *adap)
212{
213 struct list_head *item, *_n;
214 struct i2c_adapter *adap_from_list;
215 struct i2c_driver *driver;
216 struct i2c_client *client;
217 int res = 0;
218
219 down(&core_lists);
220
221 /* First make sure that this adapter was ever added */
222 list_for_each_entry(adap_from_list, &adapters, list) {
223 if (adap_from_list == adap)
224 break;
225 }
226 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200227 pr_debug("i2c-core: attempting to delete unregistered "
228 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 res = -EINVAL;
230 goto out_unlock;
231 }
232
233 list_for_each(item,&drivers) {
234 driver = list_entry(item, struct i2c_driver, list);
235 if (driver->detach_adapter)
236 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200237 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100238 "for driver [%s]\n",
239 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto out_unlock;
241 }
242 }
243
244 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200245 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 list_for_each_safe(item, _n, &adap->clients) {
247 client = list_entry(item, struct i2c_client, list);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200250 dev_err(&adap->dev, "detach_client failed for client "
251 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 client->addr);
253 goto out_unlock;
254 }
255 }
256
257 /* clean up the sysfs representation */
258 init_completion(&adap->dev_released);
259 init_completion(&adap->class_dev_released);
260 class_device_unregister(&adap->class_dev);
261 device_remove_file(&adap->dev, &dev_attr_name);
262 device_unregister(&adap->dev);
263 list_del(&adap->list);
264
265 /* wait for sysfs to drop all references */
266 wait_for_completion(&adap->dev_released);
267 wait_for_completion(&adap->class_dev_released);
268
269 /* free dynamically allocated bus id */
270 idr_remove(&i2c_adapter_idr, adap->nr);
271
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200272 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 out_unlock:
275 up(&core_lists);
276 return res;
277}
278
279
280/* -----
281 * What follows is the "upwards" interface: commands for talking to clients,
282 * which implement the functions to access the physical information of the
283 * chips.
284 */
285
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800286int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct list_head *item;
289 struct i2c_adapter *adapter;
290 int res = 0;
291
292 down(&core_lists);
293
294 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800295 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 res = driver_register(&driver->driver);
299 if (res)
300 goto out_unlock;
301
302 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100303 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* now look for instances of driver on our adapters */
Jean Delvare8a994752005-11-26 20:28:06 +0100306 if (driver->attach_adapter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 list_for_each(item,&adapters) {
308 adapter = list_entry(item, struct i2c_adapter, list);
309 driver->attach_adapter(adapter);
310 }
311 }
312
313 out_unlock:
314 up(&core_lists);
315 return res;
316}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800317EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319int i2c_del_driver(struct i2c_driver *driver)
320{
321 struct list_head *item1, *item2, *_n;
322 struct i2c_client *client;
323 struct i2c_adapter *adap;
324
325 int res = 0;
326
327 down(&core_lists);
328
329 /* Have a look at each adapter, if clients of this driver are still
330 * attached. If so, detach them to be able to kill the driver
331 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
333 list_for_each(item1,&adapters) {
334 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (driver->detach_adapter) {
336 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200337 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100338 "for driver [%s]\n",
339 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto out_unlock;
341 }
342 } else {
343 list_for_each_safe(item2, _n, &adap->clients) {
344 client = list_entry(item2, struct i2c_client, list);
345 if (client->driver != driver)
346 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200347 dev_dbg(&adap->dev, "detaching client [%s] "
348 "at 0x%02x\n", client->name,
349 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200351 dev_err(&adap->dev, "detach_client "
352 "failed for client [%s] at "
353 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 client->addr);
355 goto out_unlock;
356 }
357 }
358 }
359 }
360
361 driver_unregister(&driver->driver);
362 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100363 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 out_unlock:
366 up(&core_lists);
367 return 0;
368}
369
370static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
371{
372 struct list_head *item;
373 struct i2c_client *client;
374
375 list_for_each(item,&adapter->clients) {
376 client = list_entry(item, struct i2c_client, list);
377 if (client->addr == addr)
378 return -EBUSY;
379 }
380 return 0;
381}
382
383int i2c_check_addr(struct i2c_adapter *adapter, int addr)
384{
385 int rval;
386
387 down(&adapter->clist_lock);
388 rval = __i2c_check_addr(adapter, addr);
389 up(&adapter->clist_lock);
390
391 return rval;
392}
393
394int i2c_attach_client(struct i2c_client *client)
395{
396 struct i2c_adapter *adapter = client->adapter;
397
398 down(&adapter->clist_lock);
399 if (__i2c_check_addr(client->adapter, client->addr)) {
400 up(&adapter->clist_lock);
401 return -EBUSY;
402 }
403 list_add_tail(&client->list,&adapter->clients);
404 up(&adapter->clist_lock);
405
406 if (adapter->client_register) {
407 if (adapter->client_register(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200408 dev_dbg(&adapter->dev, "client_register "
409 "failed for client [%s] at 0x%02x\n",
410 client->name, client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
413
Jean Delvarecde78592005-11-26 21:00:54 +0100414 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 client->dev.parent = &client->adapter->dev;
417 client->dev.driver = &client->driver->driver;
418 client->dev.bus = &i2c_bus_type;
419 client->dev.release = &i2c_client_release;
420
421 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
422 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200423 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
424 client->name, client->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 device_register(&client->dev);
426 device_create_file(&client->dev, &dev_attr_client_name);
427
428 return 0;
429}
430
431
432int i2c_detach_client(struct i2c_client *client)
433{
434 struct i2c_adapter *adapter = client->adapter;
435 int res = 0;
436
Jean Delvarecde78592005-11-26 21:00:54 +0100437 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200438 dev_warn(&client->dev, "Client [%s] still busy, "
439 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (adapter->client_unregister) {
444 res = adapter->client_unregister(client);
445 if (res) {
446 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700447 "client_unregister [%s] failed, "
448 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto out;
450 }
451 }
452
453 down(&adapter->clist_lock);
454 list_del(&client->list);
455 init_completion(&client->released);
456 device_remove_file(&client->dev, &dev_attr_client_name);
457 device_unregister(&client->dev);
458 up(&adapter->clist_lock);
459 wait_for_completion(&client->released);
460
461 out:
462 return res;
463}
464
465static int i2c_inc_use_client(struct i2c_client *client)
466{
467
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100468 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -ENODEV;
470 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100471 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -ENODEV;
473 }
474
475 return 0;
476}
477
478static void i2c_dec_use_client(struct i2c_client *client)
479{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100480 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 module_put(client->adapter->owner);
482}
483
484int i2c_use_client(struct i2c_client *client)
485{
486 int ret;
487
488 ret = i2c_inc_use_client(client);
489 if (ret)
490 return ret;
491
Jean Delvarecde78592005-11-26 21:00:54 +0100492 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497int i2c_release_client(struct i2c_client *client)
498{
Jean Delvarecde78592005-11-26 21:00:54 +0100499 if (!client->usage_count) {
500 pr_debug("i2c-core: %s used one too many times\n",
501 __FUNCTION__);
502 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
Jean Delvarecde78592005-11-26 21:00:54 +0100505 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 i2c_dec_use_client(client);
507
508 return 0;
509}
510
511void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
512{
513 struct list_head *item;
514 struct i2c_client *client;
515
516 down(&adap->clist_lock);
517 list_for_each(item,&adap->clients) {
518 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100519 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 continue;
521 if (NULL != client->driver->command) {
522 up(&adap->clist_lock);
523 client->driver->command(client,cmd,arg);
524 down(&adap->clist_lock);
525 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100526 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528 up(&adap->clist_lock);
529}
530
531static int __init i2c_init(void)
532{
533 int retval;
534
535 retval = bus_register(&i2c_bus_type);
536 if (retval)
537 return retval;
538 retval = driver_register(&i2c_adapter_driver);
539 if (retval)
540 return retval;
541 return class_register(&i2c_adapter_class);
542}
543
544static void __exit i2c_exit(void)
545{
546 class_unregister(&i2c_adapter_class);
547 driver_unregister(&i2c_adapter_driver);
548 bus_unregister(&i2c_bus_type);
549}
550
551subsys_initcall(i2c_init);
552module_exit(i2c_exit);
553
554/* ----------------------------------------------------
555 * the functional interface to the i2c busses.
556 * ----------------------------------------------------
557 */
558
559int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
560{
561 int ret;
562
563 if (adap->algo->master_xfer) {
564#ifdef DEBUG
565 for (ret = 0; ret < num; ret++) {
566 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
567 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
568 'R' : 'W', msgs[ret].addr, msgs[ret].len);
569 }
570#endif
571
572 down(&adap->bus_lock);
573 ret = adap->algo->master_xfer(adap,msgs,num);
574 up(&adap->bus_lock);
575
576 return ret;
577 } else {
578 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
579 return -ENOSYS;
580 }
581}
582
583int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
584{
585 int ret;
586 struct i2c_adapter *adap=client->adapter;
587 struct i2c_msg msg;
588
Jean Delvare815f55f2005-05-07 22:58:46 +0200589 msg.addr = client->addr;
590 msg.flags = client->flags & I2C_M_TEN;
591 msg.len = count;
592 msg.buf = (char *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Jean Delvare815f55f2005-05-07 22:58:46 +0200594 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Jean Delvare815f55f2005-05-07 22:58:46 +0200596 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
597 transmitted, else error code. */
598 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
602{
603 struct i2c_adapter *adap=client->adapter;
604 struct i2c_msg msg;
605 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Jean Delvare815f55f2005-05-07 22:58:46 +0200607 msg.addr = client->addr;
608 msg.flags = client->flags & I2C_M_TEN;
609 msg.flags |= I2C_M_RD;
610 msg.len = count;
611 msg.buf = buf;
612
613 ret = i2c_transfer(adap, &msg, 1);
614
615 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
616 transmitted, else error code. */
617 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620
621int i2c_control(struct i2c_client *client,
622 unsigned int cmd, unsigned long arg)
623{
624 int ret = 0;
625 struct i2c_adapter *adap = client->adapter;
626
627 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
628 switch (cmd) {
629 case I2C_RETRIES:
630 adap->retries = arg;
631 break;
632 case I2C_TIMEOUT:
633 adap->timeout = arg;
634 break;
635 default:
636 if (adap->algo->algo_control!=NULL)
637 ret = adap->algo->algo_control(adap,cmd,arg);
638 }
639 return ret;
640}
641
642/* ----------------------------------------------------
643 * the i2c address scanning function
644 * Will not work for 10-bit addresses!
645 * ----------------------------------------------------
646 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200647static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
648 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200649{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200650 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200651
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200652 /* Make sure the address is valid */
653 if (addr < 0x03 || addr > 0x77) {
654 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
655 addr);
656 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200657 }
658
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200659 /* Skip if already in use */
660 if (i2c_check_addr(adapter, addr))
661 return 0;
662
663 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200664 if (kind < 0) {
665 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
666 I2C_SMBUS_QUICK, NULL) < 0)
667 return 0;
668
669 /* prevent 24RF08 corruption */
670 if ((addr & ~0x0f) == 0x50)
671 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
672 I2C_SMBUS_QUICK, NULL);
673 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200674
675 /* Finally call the custom detection function */
676 err = found_proc(adapter, addr, kind);
677
678 /* -ENODEV can be returned if there is a chip at the given address
679 but it isn't supported by this chip driver. We catch it here as
680 this isn't an error. */
681 return (err == -ENODEV) ? 0 : err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684int i2c_probe(struct i2c_adapter *adapter,
685 struct i2c_client_address_data *address_data,
686 int (*found_proc) (struct i2c_adapter *, int, int))
687{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200688 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int adap_id = i2c_adapter_id(adapter);
690
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200691 /* Force entries are done first, and are not affected by ignore
692 entries */
693 if (address_data->forces) {
694 unsigned short **forces = address_data->forces;
695 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200697 for (kind = 0; forces[kind]; kind++) {
698 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
699 i += 2) {
700 if (forces[kind][i] == adap_id
701 || forces[kind][i] == ANY_I2C_BUS) {
702 dev_dbg(&adapter->dev, "found force "
703 "parameter for adapter %d, "
704 "addr 0x%02x, kind %d\n",
705 adap_id, forces[kind][i + 1],
706 kind);
707 err = i2c_probe_address(adapter,
708 forces[kind][i + 1],
709 kind, found_proc);
710 if (err)
711 return err;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200716
Jean Delvare4366dc92005-09-25 16:50:06 +0200717 /* Stop here if we can't use SMBUS_QUICK */
718 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
719 if (address_data->probe[0] == I2C_CLIENT_END
720 && address_data->normal_i2c[0] == I2C_CLIENT_END)
721 return 0;
722
723 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
724 "can't probe for chips\n");
725 return -1;
726 }
727
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200728 /* Probe entries are done second, and are not affected by ignore
729 entries either */
730 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
731 if (address_data->probe[i] == adap_id
732 || address_data->probe[i] == ANY_I2C_BUS) {
733 dev_dbg(&adapter->dev, "found probe parameter for "
734 "adapter %d, addr 0x%02x\n", adap_id,
735 address_data->probe[i + 1]);
736 err = i2c_probe_address(adapter,
737 address_data->probe[i + 1],
738 -1, found_proc);
739 if (err)
740 return err;
741 }
742 }
743
744 /* Normal entries are done last, unless shadowed by an ignore entry */
745 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
746 int j, ignore;
747
748 ignore = 0;
749 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
750 j += 2) {
751 if ((address_data->ignore[j] == adap_id ||
752 address_data->ignore[j] == ANY_I2C_BUS)
753 && address_data->ignore[j + 1]
754 == address_data->normal_i2c[i]) {
755 dev_dbg(&adapter->dev, "found ignore "
756 "parameter for adapter %d, "
757 "addr 0x%02x\n", adap_id,
758 address_data->ignore[j + 1]);
759 }
760 ignore = 1;
761 break;
762 }
763 if (ignore)
764 continue;
765
766 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
767 "addr 0x%02x\n", adap_id,
768 address_data->normal_i2c[i]);
769 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
770 -1, found_proc);
771 if (err)
772 return err;
773 }
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return 0;
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778struct i2c_adapter* i2c_get_adapter(int id)
779{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct i2c_adapter *adapter;
781
782 down(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400783 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
784 if (adapter && !try_module_get(adapter->owner))
785 adapter = NULL;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 up(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400788 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791void i2c_put_adapter(struct i2c_adapter *adap)
792{
793 module_put(adap->owner);
794}
795
796/* The SMBus parts */
797
798#define POLY (0x1070U << 3)
799static u8
800crc8(u16 data)
801{
802 int i;
803
804 for(i = 0; i < 8; i++) {
805 if (data & 0x8000)
806 data = data ^ POLY;
807 data = data << 1;
808 }
809 return (u8)(data >> 8);
810}
811
Jean Delvare421ef472005-10-26 21:28:55 +0200812/* Incremental CRC8 over count bytes in the array pointed to by p */
813static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 int i;
816
817 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200818 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return crc;
820}
821
Jean Delvare421ef472005-10-26 21:28:55 +0200822/* Assume a 7-bit address, which is reasonable for SMBus */
823static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Jean Delvare421ef472005-10-26 21:28:55 +0200825 /* The address will be sent first */
826 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
827 pec = i2c_smbus_pec(pec, &addr, 1);
828
829 /* The data buffer follows */
830 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Jean Delvare421ef472005-10-26 21:28:55 +0200833/* Used for write only transactions */
834static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Jean Delvare421ef472005-10-26 21:28:55 +0200836 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
837 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Jean Delvare421ef472005-10-26 21:28:55 +0200840/* Return <0 on CRC error
841 If there was a write before this read (most cases) we need to take the
842 partial CRC from the write part into account.
843 Note that this function does modify the message (we need to decrease the
844 message length to hide the CRC byte from the caller). */
845static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Jean Delvare421ef472005-10-26 21:28:55 +0200847 u8 rpec = msg->buf[--msg->len];
848 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (rpec != cpec) {
851 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
852 rpec, cpec);
853 return -1;
854 }
855 return 0;
856}
857
858s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
859{
860 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
861 value,0,I2C_SMBUS_QUICK,NULL);
862}
863
864s32 i2c_smbus_read_byte(struct i2c_client *client)
865{
866 union i2c_smbus_data data;
867 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
868 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
869 return -1;
870 else
871 return 0x0FF & data.byte;
872}
873
874s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200877 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
881{
882 union i2c_smbus_data data;
883 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
884 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
885 return -1;
886 else
887 return 0x0FF & data.byte;
888}
889
890s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
891{
892 union i2c_smbus_data data;
893 data.byte = value;
894 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
895 I2C_SMBUS_WRITE,command,
896 I2C_SMBUS_BYTE_DATA,&data);
897}
898
899s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
900{
901 union i2c_smbus_data data;
902 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
903 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
904 return -1;
905 else
906 return 0x0FFFF & data.word;
907}
908
909s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
910{
911 union i2c_smbus_data data;
912 data.word = value;
913 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
914 I2C_SMBUS_WRITE,command,
915 I2C_SMBUS_WORD_DATA,&data);
916}
917
918s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
919 u8 length, u8 *values)
920{
921 union i2c_smbus_data data;
922 int i;
923 if (length > I2C_SMBUS_BLOCK_MAX)
924 length = I2C_SMBUS_BLOCK_MAX;
925 for (i = 1; i <= length; i++)
926 data.block[i] = values[i-1];
927 data.block[0] = length;
928 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
929 I2C_SMBUS_WRITE,command,
930 I2C_SMBUS_BLOCK_DATA,&data);
931}
932
933/* Returns the number of read bytes */
934s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
935{
936 union i2c_smbus_data data;
937 int i;
938 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
939 I2C_SMBUS_READ,command,
940 I2C_SMBUS_I2C_BLOCK_DATA,&data))
941 return -1;
942 else {
943 for (i = 1; i <= data.block[0]; i++)
944 values[i-1] = data.block[i];
945 return data.block[0];
946 }
947}
948
Jean Delvare21bbd692006-01-09 15:19:18 +1100949s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
950 u8 length, u8 *values)
951{
952 union i2c_smbus_data data;
953
954 if (length > I2C_SMBUS_BLOCK_MAX)
955 length = I2C_SMBUS_BLOCK_MAX;
956 data.block[0] = length;
957 memcpy(data.block + 1, values, length);
958 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
959 I2C_SMBUS_WRITE, command,
960 I2C_SMBUS_I2C_BLOCK_DATA, &data);
961}
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963/* Simulate a SMBus command using the i2c protocol
964 No checking of parameters is done! */
965static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
966 unsigned short flags,
967 char read_write, u8 command, int size,
968 union i2c_smbus_data * data)
969{
970 /* So we need to generate a series of msgs. In the case of writing, we
971 need to use only one message; when reading, we need two. We initialize
972 most things with sane defaults, to keep the code below somewhat
973 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +0200974 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
975 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int num = read_write == I2C_SMBUS_READ?2:1;
977 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
978 { addr, flags | I2C_M_RD, 0, msgbuf1 }
979 };
980 int i;
Jean Delvare421ef472005-10-26 21:28:55 +0200981 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 msgbuf0[0] = command;
984 switch(size) {
985 case I2C_SMBUS_QUICK:
986 msg[0].len = 0;
987 /* Special case: The read/write field is used as data */
988 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
989 num = 1;
990 break;
991 case I2C_SMBUS_BYTE:
992 if (read_write == I2C_SMBUS_READ) {
993 /* Special case: only a read! */
994 msg[0].flags = I2C_M_RD | flags;
995 num = 1;
996 }
997 break;
998 case I2C_SMBUS_BYTE_DATA:
999 if (read_write == I2C_SMBUS_READ)
1000 msg[1].len = 1;
1001 else {
1002 msg[0].len = 2;
1003 msgbuf0[1] = data->byte;
1004 }
1005 break;
1006 case I2C_SMBUS_WORD_DATA:
1007 if (read_write == I2C_SMBUS_READ)
1008 msg[1].len = 2;
1009 else {
1010 msg[0].len=3;
1011 msgbuf0[1] = data->word & 0xff;
1012 msgbuf0[2] = (data->word >> 8) & 0xff;
1013 }
1014 break;
1015 case I2C_SMBUS_PROC_CALL:
1016 num = 2; /* Special case */
1017 read_write = I2C_SMBUS_READ;
1018 msg[0].len = 3;
1019 msg[1].len = 2;
1020 msgbuf0[1] = data->word & 0xff;
1021 msgbuf0[2] = (data->word >> 8) & 0xff;
1022 break;
1023 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (read_write == I2C_SMBUS_READ) {
1025 dev_err(&adapter->dev, "Block read not supported "
1026 "under I2C emulation!\n");
1027 return -1;
1028 } else {
1029 msg[0].len = data->block[0] + 2;
1030 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1031 dev_err(&adapter->dev, "smbus_access called with "
1032 "invalid block write size (%d)\n",
1033 data->block[0]);
1034 return -1;
1035 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001036 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 msgbuf0[i] = data->block[i-1];
1038 }
1039 break;
1040 case I2C_SMBUS_BLOCK_PROC_CALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 dev_dbg(&adapter->dev, "Block process call not supported "
1042 "under I2C emulation!\n");
1043 return -1;
1044 case I2C_SMBUS_I2C_BLOCK_DATA:
1045 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001046 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 } else {
1048 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001049 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1051 "invalid block write size (%d)\n",
1052 data->block[0]);
1053 return -1;
1054 }
1055 for (i = 1; i <= data->block[0]; i++)
1056 msgbuf0[i] = data->block[i];
1057 }
1058 break;
1059 default:
1060 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1061 size);
1062 return -1;
1063 }
1064
Jean Delvare421ef472005-10-26 21:28:55 +02001065 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1066 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1067 if (i) {
1068 /* Compute PEC if first message is a write */
1069 if (!(msg[0].flags & I2C_M_RD)) {
1070 if (num == 1) /* Write only */
1071 i2c_smbus_add_pec(&msg[0]);
1072 else /* Write followed by read */
1073 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1074 }
1075 /* Ask for PEC if last message is a read */
1076 if (msg[num-1].flags & I2C_M_RD)
1077 msg[num-1].len++;
1078 }
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (i2c_transfer(adapter, msg, num) < 0)
1081 return -1;
1082
Jean Delvare421ef472005-10-26 21:28:55 +02001083 /* Check PEC if last message is a read */
1084 if (i && (msg[num-1].flags & I2C_M_RD)) {
1085 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1086 return -1;
1087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (read_write == I2C_SMBUS_READ)
1090 switch(size) {
1091 case I2C_SMBUS_BYTE:
1092 data->byte = msgbuf0[0];
1093 break;
1094 case I2C_SMBUS_BYTE_DATA:
1095 data->byte = msgbuf1[0];
1096 break;
1097 case I2C_SMBUS_WORD_DATA:
1098 case I2C_SMBUS_PROC_CALL:
1099 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1100 break;
1101 case I2C_SMBUS_I2C_BLOCK_DATA:
1102 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001103 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1104 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 data->block[i+1] = msgbuf1[i];
1106 break;
1107 }
1108 return 0;
1109}
1110
1111
1112s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1113 char read_write, u8 command, int size,
1114 union i2c_smbus_data * data)
1115{
1116 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (adapter->algo->smbus_xfer) {
1121 down(&adapter->bus_lock);
1122 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1123 command,size,data);
1124 up(&adapter->bus_lock);
1125 } else
1126 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1127 command,size,data);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return res;
1130}
1131
1132
Jean Delvareefde7232005-07-20 23:03:50 +02001133/* Next four are needed by i2c-isa */
1134EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1135EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1136EXPORT_SYMBOL_GPL(i2c_adapter_class);
1137EXPORT_SYMBOL_GPL(i2c_bus_type);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139EXPORT_SYMBOL(i2c_add_adapter);
1140EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141EXPORT_SYMBOL(i2c_del_driver);
1142EXPORT_SYMBOL(i2c_attach_client);
1143EXPORT_SYMBOL(i2c_detach_client);
1144EXPORT_SYMBOL(i2c_use_client);
1145EXPORT_SYMBOL(i2c_release_client);
1146EXPORT_SYMBOL(i2c_clients_command);
1147EXPORT_SYMBOL(i2c_check_addr);
1148
1149EXPORT_SYMBOL(i2c_master_send);
1150EXPORT_SYMBOL(i2c_master_recv);
1151EXPORT_SYMBOL(i2c_control);
1152EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153EXPORT_SYMBOL(i2c_get_adapter);
1154EXPORT_SYMBOL(i2c_put_adapter);
1155EXPORT_SYMBOL(i2c_probe);
1156
1157EXPORT_SYMBOL(i2c_smbus_xfer);
1158EXPORT_SYMBOL(i2c_smbus_write_quick);
1159EXPORT_SYMBOL(i2c_smbus_read_byte);
1160EXPORT_SYMBOL(i2c_smbus_write_byte);
1161EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1162EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1163EXPORT_SYMBOL(i2c_smbus_read_word_data);
1164EXPORT_SYMBOL(i2c_smbus_write_word_data);
1165EXPORT_SYMBOL(i2c_smbus_write_block_data);
1166EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001167EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1170MODULE_DESCRIPTION("I2C-Bus main module");
1171MODULE_LICENSE("GPL");