blob: c50229bd9f85b95be01930721e4e08109352d478 [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>
Arjan van de Venb3585e42006-01-11 10:50:26 +010034#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010035#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
David Brownell9c1600e2007-05-01 23:26:31 +020038#include "i2c-core.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static LIST_HEAD(adapters);
42static LIST_HEAD(drivers);
Arjan van de Venb3585e42006-01-11 10:50:26 +010043static DEFINE_MUTEX(core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static DEFINE_IDR(i2c_adapter_idr);
45
David Brownella1d9e6e2007-05-01 23:26:30 +020046#define is_newstyle_driver(d) ((d)->probe || (d)->remove)
David Brownellf37dd802007-02-13 22:09:00 +010047
48/* ------------------------------------------------------------------------- */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int i2c_device_match(struct device *dev, struct device_driver *drv)
51{
David Brownell7b4fbc52007-05-01 23:26:30 +020052 struct i2c_client *client = to_i2c_client(dev);
53 struct i2c_driver *driver = to_i2c_driver(drv);
54
55 /* make legacy i2c drivers bypass driver model probing entirely;
56 * such drivers scan each i2c adapter/bus themselves.
57 */
David Brownella1d9e6e2007-05-01 23:26:30 +020058 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020059 return 0;
60
61 /* new style drivers use the same kind of driver matching policy
62 * as platform devices or SPI: compare device and driver IDs.
63 */
64 return strcmp(client->driver_name, drv->name) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
David Brownell7b4fbc52007-05-01 23:26:30 +020067#ifdef CONFIG_HOTPLUG
68
69/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
70static int i2c_device_uevent(struct device *dev, char **envp, int num_envp,
71 char *buffer, int buffer_size)
72{
73 struct i2c_client *client = to_i2c_client(dev);
74 int i = 0, length = 0;
75
76 /* by definition, legacy drivers can't hotplug */
77 if (dev->driver || !client->driver_name)
78 return 0;
79
80 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
81 "MODALIAS=%s", client->driver_name))
82 return -ENOMEM;
83 envp[i] = NULL;
84 dev_dbg(dev, "uevent\n");
85 return 0;
86}
87
88#else
89#define i2c_device_uevent NULL
90#endif /* CONFIG_HOTPLUG */
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int i2c_device_probe(struct device *dev)
93{
David Brownell7b4fbc52007-05-01 23:26:30 +020094 struct i2c_client *client = to_i2c_client(dev);
95 struct i2c_driver *driver = to_i2c_driver(dev->driver);
96
97 if (!driver->probe)
98 return -ENODEV;
99 client->driver = driver;
100 dev_dbg(dev, "probe\n");
101 return driver->probe(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static int i2c_device_remove(struct device *dev)
105{
David Brownella1d9e6e2007-05-01 23:26:30 +0200106 struct i2c_client *client = to_i2c_client(dev);
107 struct i2c_driver *driver;
108 int status;
109
110 if (!dev->driver)
111 return 0;
112
113 driver = to_i2c_driver(dev->driver);
114 if (driver->remove) {
115 dev_dbg(dev, "remove\n");
116 status = driver->remove(client);
117 } else {
118 dev->driver = NULL;
119 status = 0;
120 }
121 if (status == 0)
122 client->driver = NULL;
123 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
David Brownellf37dd802007-02-13 22:09:00 +0100126static void i2c_device_shutdown(struct device *dev)
127{
128 struct i2c_driver *driver;
129
130 if (!dev->driver)
131 return;
132 driver = to_i2c_driver(dev->driver);
133 if (driver->shutdown)
134 driver->shutdown(to_i2c_client(dev));
135}
136
137static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
138{
139 struct i2c_driver *driver;
140
141 if (!dev->driver)
142 return 0;
143 driver = to_i2c_driver(dev->driver);
144 if (!driver->suspend)
145 return 0;
146 return driver->suspend(to_i2c_client(dev), mesg);
147}
148
149static int i2c_device_resume(struct device * dev)
150{
151 struct i2c_driver *driver;
152
153 if (!dev->driver)
154 return 0;
155 driver = to_i2c_driver(dev->driver);
156 if (!driver->resume)
157 return 0;
158 return driver->resume(to_i2c_client(dev));
159}
160
David Brownell7b4fbc52007-05-01 23:26:30 +0200161static void i2c_client_release(struct device *dev)
162{
163 struct i2c_client *client = to_i2c_client(dev);
164 complete(&client->released);
165}
166
David Brownell9c1600e2007-05-01 23:26:31 +0200167static void i2c_client_dev_release(struct device *dev)
168{
169 kfree(to_i2c_client(dev));
170}
171
David Brownell7b4fbc52007-05-01 23:26:30 +0200172static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
173{
174 struct i2c_client *client = to_i2c_client(dev);
175 return sprintf(buf, "%s\n", client->name);
176}
177
178static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
179{
180 struct i2c_client *client = to_i2c_client(dev);
181 return client->driver_name
182 ? sprintf(buf, "%s\n", client->driver_name)
183 : 0;
184}
185
186static struct device_attribute i2c_dev_attrs[] = {
187 __ATTR(name, S_IRUGO, show_client_name, NULL),
188 /* modalias helps coldplug: modprobe $(cat .../modalias) */
189 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
190 { },
191};
192
Russell Kingb864c7d2006-01-05 14:37:50 +0000193struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100194 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200195 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100196 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200197 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100198 .probe = i2c_device_probe,
199 .remove = i2c_device_remove,
200 .shutdown = i2c_device_shutdown,
201 .suspend = i2c_device_suspend,
202 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000203};
204
David Brownell9c1600e2007-05-01 23:26:31 +0200205/**
206 * i2c_new_device - instantiate an i2c device for use with a new style driver
207 * @adap: the adapter managing the device
208 * @info: describes one I2C device; bus_num is ignored
209 *
210 * Create a device to work with a new style i2c driver, where binding is
211 * handled through driver model probe()/remove() methods. This call is not
212 * appropriate for use by mainboad initialization logic, which usually runs
213 * during an arch_initcall() long before any i2c_adapter could exist.
214 *
215 * This returns the new i2c client, which may be saved for later use with
216 * i2c_unregister_device(); or NULL to indicate an error.
217 */
218struct i2c_client *
219i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
220{
221 struct i2c_client *client;
222 int status;
223
224 client = kzalloc(sizeof *client, GFP_KERNEL);
225 if (!client)
226 return NULL;
227
228 client->adapter = adap;
229
230 client->dev.platform_data = info->platform_data;
231 client->flags = info->flags;
232 client->addr = info->addr;
233 client->irq = info->irq;
234
235 strlcpy(client->driver_name, info->driver_name,
236 sizeof(client->driver_name));
237 strlcpy(client->name, info->type, sizeof(client->name));
238
239 /* a new style driver may be bound to this device when we
240 * return from this function, or any later moment (e.g. maybe
241 * hotplugging will load the driver module). and the device
242 * refcount model is the standard driver model one.
243 */
244 status = i2c_attach_client(client);
245 if (status < 0) {
246 kfree(client);
247 client = NULL;
248 }
249 return client;
250}
251EXPORT_SYMBOL_GPL(i2c_new_device);
252
253
254/**
255 * i2c_unregister_device - reverse effect of i2c_new_device()
256 * @client: value returned from i2c_new_device()
257 */
258void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200259{
260 struct i2c_adapter *adapter = client->adapter;
261 struct i2c_driver *driver = client->driver;
262
263 if (driver && !is_newstyle_driver(driver)) {
264 dev_err(&client->dev, "can't unregister devices "
265 "with legacy drivers\n");
266 WARN_ON(1);
267 return;
268 }
269
270 mutex_lock(&adapter->clist_lock);
271 list_del(&client->list);
272 mutex_unlock(&adapter->clist_lock);
273
274 device_unregister(&client->dev);
275}
David Brownell9c1600e2007-05-01 23:26:31 +0200276EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200277
278
David Brownellf37dd802007-02-13 22:09:00 +0100279/* ------------------------------------------------------------------------- */
280
David Brownell16ffadf2007-05-01 23:26:28 +0200281/* I2C bus adapters -- one roots each I2C or SMBUS segment */
282
Jean Delvareefde7232005-07-20 23:03:50 +0200283void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
David Brownellef2c83212007-05-01 23:26:28 +0200285 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 complete(&adap->dev_released);
287}
288
David Brownell16ffadf2007-05-01 23:26:28 +0200289static ssize_t
290show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
David Brownellef2c83212007-05-01 23:26:28 +0200292 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return sprintf(buf, "%s\n", adap->name);
294}
David Brownell16ffadf2007-05-01 23:26:28 +0200295
296static struct device_attribute i2c_adapter_attrs[] = {
297 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
298 { },
299};
300
301struct class i2c_adapter_class = {
302 .owner = THIS_MODULE,
303 .name = "i2c-adapter",
304 .dev_attrs = i2c_adapter_attrs,
305};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Brownell9c1600e2007-05-01 23:26:31 +0200307static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
308{
309 struct i2c_devinfo *devinfo;
310
311 mutex_lock(&__i2c_board_lock);
312 list_for_each_entry(devinfo, &__i2c_board_list, list) {
313 if (devinfo->busnum == adapter->nr
314 && !i2c_new_device(adapter,
315 &devinfo->board_info))
316 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
317 i2c_adapter_id(adapter),
318 devinfo->board_info.addr);
319 }
320 mutex_unlock(&__i2c_board_lock);
321}
322
David Brownell6e13e642007-05-01 23:26:31 +0200323static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
David Brownell6e13e642007-05-01 23:26:31 +0200325 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct list_head *item;
327 struct i2c_driver *driver;
328
Ingo Molnar5c085d32006-01-18 23:16:04 +0100329 mutex_init(&adap->bus_lock);
330 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 INIT_LIST_HEAD(&adap->clients);
332
David Brownell6e13e642007-05-01 23:26:31 +0200333 mutex_lock(&core_lists);
334 list_add_tail(&adap->list, &adapters);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* Add the adapter to the driver core.
337 * If the parent pointer is not set up,
338 * we add this adapter to the host bus.
339 */
David Brownellb119dc32007-01-04 13:07:04 +0100340 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100342 pr_debug("I2C adapter driver [%s] forgot to specify "
343 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200347 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200348 res = device_register(&adap->dev);
349 if (res)
350 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200352 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
353
David Brownell6e13e642007-05-01 23:26:31 +0200354 /* create pre-declared device nodes for new-style drivers */
355 if (adap->nr < __i2c_first_dynamic_bus_num)
356 i2c_scan_static_board_info(adap);
357
David Brownell7b4fbc52007-05-01 23:26:30 +0200358 /* let legacy drivers scan this bus for matching devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 list_for_each(item,&drivers) {
360 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100361 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* We ignore the return code; if it fails, too bad */
363 driver->attach_adapter(adap);
364 }
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100367 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200369
Jean Delvareb119c6c2006-08-15 18:26:30 +0200370out_list:
371 list_del(&adap->list);
372 idr_remove(&i2c_adapter_idr, adap->nr);
373 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
David Brownell6e13e642007-05-01 23:26:31 +0200376/**
377 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
378 * @adapter: the adapter to add
379 *
380 * This routine is used to declare an I2C adapter when its bus number
381 * doesn't matter. Examples: for I2C adapters dynamically added by
382 * USB links or PCI plugin cards.
383 *
384 * When this returns zero, a new bus number was allocated and stored
385 * in adap->nr, and the specified adapter became available for clients.
386 * Otherwise, a negative errno value is returned.
387 */
388int i2c_add_adapter(struct i2c_adapter *adapter)
389{
390 int id, res = 0;
391
392retry:
393 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
394 return -ENOMEM;
395
396 mutex_lock(&core_lists);
397 /* "above" here means "above or equal to", sigh */
398 res = idr_get_new_above(&i2c_adapter_idr, adapter,
399 __i2c_first_dynamic_bus_num, &id);
400 mutex_unlock(&core_lists);
401
402 if (res < 0) {
403 if (res == -EAGAIN)
404 goto retry;
405 return res;
406 }
407
408 adapter->nr = id;
409 return i2c_register_adapter(adapter);
410}
411EXPORT_SYMBOL(i2c_add_adapter);
412
413/**
414 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
415 * @adap: the adapter to register (with adap->nr initialized)
416 *
417 * This routine is used to declare an I2C adapter when its bus number
418 * matters. Example: for I2C adapters from system-on-chip CPUs, or
419 * otherwise built in to the system's mainboard, and where i2c_board_info
420 * is used to properly configure I2C devices.
421 *
422 * If no devices have pre-been declared for this bus, then be sure to
423 * register the adapter before any dynamically allocated ones. Otherwise
424 * the required bus ID may not be available.
425 *
426 * When this returns zero, the specified adapter became available for
427 * clients using the bus number provided in adap->nr. Also, the table
428 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
429 * and the appropriate driver model device nodes are created. Otherwise, a
430 * negative errno value is returned.
431 */
432int i2c_add_numbered_adapter(struct i2c_adapter *adap)
433{
434 int id;
435 int status;
436
437 if (adap->nr & ~MAX_ID_MASK)
438 return -EINVAL;
439
440retry:
441 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
442 return -ENOMEM;
443
444 mutex_lock(&core_lists);
445 /* "above" here means "above or equal to", sigh;
446 * we need the "equal to" result to force the result
447 */
448 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
449 if (status == 0 && id != adap->nr) {
450 status = -EBUSY;
451 idr_remove(&i2c_adapter_idr, id);
452 }
453 mutex_unlock(&core_lists);
454 if (status == -EAGAIN)
455 goto retry;
456
457 if (status == 0)
458 status = i2c_register_adapter(adap);
459 return status;
460}
461EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463int i2c_del_adapter(struct i2c_adapter *adap)
464{
465 struct list_head *item, *_n;
466 struct i2c_adapter *adap_from_list;
467 struct i2c_driver *driver;
468 struct i2c_client *client;
469 int res = 0;
470
Arjan van de Venb3585e42006-01-11 10:50:26 +0100471 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* First make sure that this adapter was ever added */
474 list_for_each_entry(adap_from_list, &adapters, list) {
475 if (adap_from_list == adap)
476 break;
477 }
478 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200479 pr_debug("i2c-core: attempting to delete unregistered "
480 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 res = -EINVAL;
482 goto out_unlock;
483 }
484
485 list_for_each(item,&drivers) {
486 driver = list_entry(item, struct i2c_driver, list);
487 if (driver->detach_adapter)
488 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200489 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100490 "for driver [%s]\n",
491 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto out_unlock;
493 }
494 }
495
496 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200497 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200499 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
David Brownella1d9e6e2007-05-01 23:26:30 +0200501 client = list_entry(item, struct i2c_client, list);
502 driver = client->driver;
503
504 /* new style, follow standard driver model */
505 if (!driver || is_newstyle_driver(driver)) {
506 i2c_unregister_device(client);
507 continue;
508 }
509
510 /* legacy drivers create and remove clients themselves */
511 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200512 dev_err(&adap->dev, "detach_client failed for client "
513 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 client->addr);
515 goto out_unlock;
516 }
517 }
518
519 /* clean up the sysfs representation */
520 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 device_unregister(&adap->dev);
522 list_del(&adap->list);
523
524 /* wait for sysfs to drop all references */
525 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
David Brownell6e13e642007-05-01 23:26:31 +0200527 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 idr_remove(&i2c_adapter_idr, adap->nr);
529
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200530 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100533 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return res;
535}
536
537
David Brownell7b4fbc52007-05-01 23:26:30 +0200538/* ------------------------------------------------------------------------- */
539
540/*
541 * An i2c_driver is used with one or more i2c_client (device) nodes to access
542 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
543 * are two models for binding the driver to its device: "new style" drivers
544 * follow the standard Linux driver model and just respond to probe() calls
545 * issued if the driver core sees they match(); "legacy" drivers create device
546 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
548
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800549int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100551 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
David Brownell7b4fbc52007-05-01 23:26:30 +0200553 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200554 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200555 if (driver->attach_adapter || driver->detach_adapter
556 || driver->detach_client) {
557 printk(KERN_WARNING
558 "i2c-core: driver [%s] is confused\n",
559 driver->driver.name);
560 return -EINVAL;
561 }
562 }
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800565 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
David Brownell6e13e642007-05-01 23:26:31 +0200568 /* for new style drivers, when registration returns the driver core
569 * will have called probe() for all matching-but-unbound devices.
570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 res = driver_register(&driver->driver);
572 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100573 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100574
Jean Delvare7eebcb72006-02-05 23:28:21 +0100575 mutex_lock(&core_lists);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100578 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
David Brownell7b4fbc52007-05-01 23:26:30 +0200580 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100581 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200582 struct i2c_adapter *adapter;
583
584 list_for_each_entry(adapter, &adapters, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 driver->attach_adapter(adapter);
586 }
587 }
588
Arjan van de Venb3585e42006-01-11 10:50:26 +0100589 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800592EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
David Brownella1d9e6e2007-05-01 23:26:30 +0200594/**
595 * i2c_del_driver - unregister I2C driver
596 * @driver: the driver being unregistered
597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598int i2c_del_driver(struct i2c_driver *driver)
599{
600 struct list_head *item1, *item2, *_n;
601 struct i2c_client *client;
602 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int res = 0;
605
Arjan van de Venb3585e42006-01-11 10:50:26 +0100606 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
David Brownella1d9e6e2007-05-01 23:26:30 +0200608 /* new-style driver? */
609 if (is_newstyle_driver(driver))
610 goto unregister;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100613 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
616 list_for_each(item1,&adapters) {
617 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (driver->detach_adapter) {
619 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200620 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100621 "for driver [%s]\n",
622 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto out_unlock;
624 }
625 } else {
626 list_for_each_safe(item2, _n, &adap->clients) {
627 client = list_entry(item2, struct i2c_client, list);
628 if (client->driver != driver)
629 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200630 dev_dbg(&adap->dev, "detaching client [%s] "
631 "at 0x%02x\n", client->name,
632 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200634 dev_err(&adap->dev, "detach_client "
635 "failed for client [%s] at "
636 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 client->addr);
638 goto out_unlock;
639 }
640 }
641 }
642 }
643
David Brownella1d9e6e2007-05-01 23:26:30 +0200644 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 driver_unregister(&driver->driver);
646 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100647 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100650 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652}
653
David Brownell7b4fbc52007-05-01 23:26:30 +0200654/* ------------------------------------------------------------------------- */
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
657{
658 struct list_head *item;
659 struct i2c_client *client;
660
661 list_for_each(item,&adapter->clients) {
662 client = list_entry(item, struct i2c_client, list);
663 if (client->addr == addr)
664 return -EBUSY;
665 }
666 return 0;
667}
668
669int i2c_check_addr(struct i2c_adapter *adapter, int addr)
670{
671 int rval;
672
Ingo Molnar5c085d32006-01-18 23:16:04 +0100673 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100675 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 return rval;
678}
679
680int i2c_attach_client(struct i2c_client *client)
681{
682 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200683 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Ingo Molnar5c085d32006-01-18 23:16:04 +0100685 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200687 res = -EBUSY;
688 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100691
Jean Delvarecde78592005-11-26 21:00:54 +0100692 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200696
697 if (client->driver)
698 client->dev.driver = &client->driver->driver;
699
700 if (client->driver && !is_newstyle_driver(client->driver))
701 client->dev.release = i2c_client_release;
702 else
703 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
706 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200707 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
708 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200709 res = device_register(&client->dev);
710 if (res)
711 goto out_list;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200712 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200713
714 if (adapter->client_register) {
715 if (adapter->client_register(client)) {
716 dev_dbg(&adapter->dev, "client_register "
717 "failed for client [%s] at 0x%02x\n",
718 client->name, client->addr);
719 }
720 }
721
722 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200723
Jean Delvareb119c6c2006-08-15 18:26:30 +0200724out_list:
725 list_del(&client->list);
726 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
727 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200728out_unlock:
729 mutex_unlock(&adapter->clist_lock);
730 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733
734int i2c_detach_client(struct i2c_client *client)
735{
736 struct i2c_adapter *adapter = client->adapter;
737 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100738
Jean Delvarecde78592005-11-26 21:00:54 +0100739 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200740 dev_warn(&client->dev, "Client [%s] still busy, "
741 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (adapter->client_unregister) {
746 res = adapter->client_unregister(client);
747 if (res) {
748 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700749 "client_unregister [%s] failed, "
750 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto out;
752 }
753 }
754
Ingo Molnar5c085d32006-01-18 23:16:04 +0100755 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 list_del(&client->list);
757 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100759 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 wait_for_completion(&client->released);
761
762 out:
763 return res;
764}
765
766static int i2c_inc_use_client(struct i2c_client *client)
767{
768
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100769 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -ENODEV;
771 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100772 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -ENODEV;
774 }
775
776 return 0;
777}
778
779static void i2c_dec_use_client(struct i2c_client *client)
780{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100781 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 module_put(client->adapter->owner);
783}
784
785int i2c_use_client(struct i2c_client *client)
786{
787 int ret;
788
789 ret = i2c_inc_use_client(client);
790 if (ret)
791 return ret;
792
Jean Delvarecde78592005-11-26 21:00:54 +0100793 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798int i2c_release_client(struct i2c_client *client)
799{
Jean Delvarecde78592005-11-26 21:00:54 +0100800 if (!client->usage_count) {
801 pr_debug("i2c-core: %s used one too many times\n",
802 __FUNCTION__);
803 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
David Brownell438d6c22006-12-10 21:21:31 +0100805
Jean Delvarecde78592005-11-26 21:00:54 +0100806 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return 0;
810}
811
812void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
813{
814 struct list_head *item;
815 struct i2c_client *client;
816
Ingo Molnar5c085d32006-01-18 23:16:04 +0100817 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 list_for_each(item,&adap->clients) {
819 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100820 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 continue;
822 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100823 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100825 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100827 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100829 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832static int __init i2c_init(void)
833{
834 int retval;
835
836 retval = bus_register(&i2c_bus_type);
837 if (retval)
838 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return class_register(&i2c_adapter_class);
840}
841
842static void __exit i2c_exit(void)
843{
844 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 bus_unregister(&i2c_bus_type);
846}
847
848subsys_initcall(i2c_init);
849module_exit(i2c_exit);
850
851/* ----------------------------------------------------
852 * the functional interface to the i2c busses.
853 * ----------------------------------------------------
854 */
855
856int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
857{
858 int ret;
859
860 if (adap->algo->master_xfer) {
861#ifdef DEBUG
862 for (ret = 0; ret < num; ret++) {
863 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200864 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
865 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
866 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868#endif
869
Jiri Kosina6ea23032006-12-10 21:21:30 +0100870 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100872 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 return ret;
875 } else {
876 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
877 return -ENOSYS;
878 }
879}
880
881int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
882{
883 int ret;
884 struct i2c_adapter *adap=client->adapter;
885 struct i2c_msg msg;
886
Jean Delvare815f55f2005-05-07 22:58:46 +0200887 msg.addr = client->addr;
888 msg.flags = client->flags & I2C_M_TEN;
889 msg.len = count;
890 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100891
Jean Delvare815f55f2005-05-07 22:58:46 +0200892 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Jean Delvare815f55f2005-05-07 22:58:46 +0200894 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
895 transmitted, else error code. */
896 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
900{
901 struct i2c_adapter *adap=client->adapter;
902 struct i2c_msg msg;
903 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Jean Delvare815f55f2005-05-07 22:58:46 +0200905 msg.addr = client->addr;
906 msg.flags = client->flags & I2C_M_TEN;
907 msg.flags |= I2C_M_RD;
908 msg.len = count;
909 msg.buf = buf;
910
911 ret = i2c_transfer(adap, &msg, 1);
912
913 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
914 transmitted, else error code. */
915 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918
919int i2c_control(struct i2c_client *client,
920 unsigned int cmd, unsigned long arg)
921{
922 int ret = 0;
923 struct i2c_adapter *adap = client->adapter;
924
925 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
926 switch (cmd) {
927 case I2C_RETRIES:
928 adap->retries = arg;
929 break;
930 case I2C_TIMEOUT:
931 adap->timeout = arg;
932 break;
933 default:
934 if (adap->algo->algo_control!=NULL)
935 ret = adap->algo->algo_control(adap,cmd,arg);
936 }
937 return ret;
938}
939
940/* ----------------------------------------------------
941 * the i2c address scanning function
942 * Will not work for 10-bit addresses!
943 * ----------------------------------------------------
944 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200945static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
946 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200947{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200948 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200949
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200950 /* Make sure the address is valid */
951 if (addr < 0x03 || addr > 0x77) {
952 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
953 addr);
954 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200955 }
956
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200957 /* Skip if already in use */
958 if (i2c_check_addr(adapter, addr))
959 return 0;
960
961 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200962 if (kind < 0) {
963 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
964 I2C_SMBUS_QUICK, NULL) < 0)
965 return 0;
966
967 /* prevent 24RF08 corruption */
968 if ((addr & ~0x0f) == 0x50)
969 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
970 I2C_SMBUS_QUICK, NULL);
971 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200972
973 /* Finally call the custom detection function */
974 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200975 /* -ENODEV can be returned if there is a chip at the given address
976 but it isn't supported by this chip driver. We catch it here as
977 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200978 if (err == -ENODEV)
979 err = 0;
980
981 if (err)
982 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
983 addr, err);
984 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987int i2c_probe(struct i2c_adapter *adapter,
988 struct i2c_client_address_data *address_data,
989 int (*found_proc) (struct i2c_adapter *, int, int))
990{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200991 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 int adap_id = i2c_adapter_id(adapter);
993
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200994 /* Force entries are done first, and are not affected by ignore
995 entries */
996 if (address_data->forces) {
997 unsigned short **forces = address_data->forces;
998 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001000 for (kind = 0; forces[kind]; kind++) {
1001 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1002 i += 2) {
1003 if (forces[kind][i] == adap_id
1004 || forces[kind][i] == ANY_I2C_BUS) {
1005 dev_dbg(&adapter->dev, "found force "
1006 "parameter for adapter %d, "
1007 "addr 0x%02x, kind %d\n",
1008 adap_id, forces[kind][i + 1],
1009 kind);
1010 err = i2c_probe_address(adapter,
1011 forces[kind][i + 1],
1012 kind, found_proc);
1013 if (err)
1014 return err;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001019
Jean Delvare4366dc92005-09-25 16:50:06 +02001020 /* Stop here if we can't use SMBUS_QUICK */
1021 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1022 if (address_data->probe[0] == I2C_CLIENT_END
1023 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001024 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001025
1026 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1027 "can't probe for chips\n");
1028 return -1;
1029 }
1030
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001031 /* Probe entries are done second, and are not affected by ignore
1032 entries either */
1033 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1034 if (address_data->probe[i] == adap_id
1035 || address_data->probe[i] == ANY_I2C_BUS) {
1036 dev_dbg(&adapter->dev, "found probe parameter for "
1037 "adapter %d, addr 0x%02x\n", adap_id,
1038 address_data->probe[i + 1]);
1039 err = i2c_probe_address(adapter,
1040 address_data->probe[i + 1],
1041 -1, found_proc);
1042 if (err)
1043 return err;
1044 }
1045 }
1046
1047 /* Normal entries are done last, unless shadowed by an ignore entry */
1048 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1049 int j, ignore;
1050
1051 ignore = 0;
1052 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1053 j += 2) {
1054 if ((address_data->ignore[j] == adap_id ||
1055 address_data->ignore[j] == ANY_I2C_BUS)
1056 && address_data->ignore[j + 1]
1057 == address_data->normal_i2c[i]) {
1058 dev_dbg(&adapter->dev, "found ignore "
1059 "parameter for adapter %d, "
1060 "addr 0x%02x\n", adap_id,
1061 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001062 ignore = 1;
1063 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001064 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001065 }
1066 if (ignore)
1067 continue;
1068
1069 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1070 "addr 0x%02x\n", adap_id,
1071 address_data->normal_i2c[i]);
1072 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1073 -1, found_proc);
1074 if (err)
1075 return err;
1076 }
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return 0;
1079}
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081struct i2c_adapter* i2c_get_adapter(int id)
1082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001084
Arjan van de Venb3585e42006-01-11 10:50:26 +01001085 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001086 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1087 if (adapter && !try_module_get(adapter->owner))
1088 adapter = NULL;
1089
Arjan van de Venb3585e42006-01-11 10:50:26 +01001090 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001091 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
1094void i2c_put_adapter(struct i2c_adapter *adap)
1095{
1096 module_put(adap->owner);
1097}
1098
1099/* The SMBus parts */
1100
David Brownell438d6c22006-12-10 21:21:31 +01001101#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102static u8
1103crc8(u16 data)
1104{
1105 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001108 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 data = data ^ POLY;
1110 data = data << 1;
1111 }
1112 return (u8)(data >> 8);
1113}
1114
Jean Delvare421ef472005-10-26 21:28:55 +02001115/* Incremental CRC8 over count bytes in the array pointed to by p */
1116static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 int i;
1119
1120 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001121 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return crc;
1123}
1124
Jean Delvare421ef472005-10-26 21:28:55 +02001125/* Assume a 7-bit address, which is reasonable for SMBus */
1126static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Jean Delvare421ef472005-10-26 21:28:55 +02001128 /* The address will be sent first */
1129 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1130 pec = i2c_smbus_pec(pec, &addr, 1);
1131
1132 /* The data buffer follows */
1133 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Jean Delvare421ef472005-10-26 21:28:55 +02001136/* Used for write only transactions */
1137static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jean Delvare421ef472005-10-26 21:28:55 +02001139 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1140 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Jean Delvare421ef472005-10-26 21:28:55 +02001143/* Return <0 on CRC error
1144 If there was a write before this read (most cases) we need to take the
1145 partial CRC from the write part into account.
1146 Note that this function does modify the message (we need to decrease the
1147 message length to hide the CRC byte from the caller). */
1148static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Jean Delvare421ef472005-10-26 21:28:55 +02001150 u8 rpec = msg->buf[--msg->len];
1151 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (rpec != cpec) {
1154 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1155 rpec, cpec);
1156 return -1;
1157 }
David Brownell438d6c22006-12-10 21:21:31 +01001158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1162{
1163 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001164 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167s32 i2c_smbus_read_byte(struct i2c_client *client)
1168{
1169 union i2c_smbus_data data;
1170 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1171 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1172 return -1;
1173 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001174 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1178{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001180 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1184{
1185 union i2c_smbus_data data;
1186 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1187 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1188 return -1;
1189 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001190 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1194{
1195 union i2c_smbus_data data;
1196 data.byte = value;
1197 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1198 I2C_SMBUS_WRITE,command,
1199 I2C_SMBUS_BYTE_DATA,&data);
1200}
1201
1202s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1203{
1204 union i2c_smbus_data data;
1205 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1206 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1207 return -1;
1208 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001209 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1213{
1214 union i2c_smbus_data data;
1215 data.word = value;
1216 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1217 I2C_SMBUS_WRITE,command,
1218 I2C_SMBUS_WORD_DATA,&data);
1219}
1220
1221s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001222 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (length > I2C_SMBUS_BLOCK_MAX)
1227 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001229 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1231 I2C_SMBUS_WRITE,command,
1232 I2C_SMBUS_BLOCK_DATA,&data);
1233}
1234
1235/* Returns the number of read bytes */
1236s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1237{
1238 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1241 I2C_SMBUS_READ,command,
1242 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1243 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001244
1245 memcpy(values, &data.block[1], data.block[0]);
1246 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Jean Delvare21bbd692006-01-09 15:19:18 +11001249s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001250 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001251{
1252 union i2c_smbus_data data;
1253
1254 if (length > I2C_SMBUS_BLOCK_MAX)
1255 length = I2C_SMBUS_BLOCK_MAX;
1256 data.block[0] = length;
1257 memcpy(data.block + 1, values, length);
1258 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1259 I2C_SMBUS_WRITE, command,
1260 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1261}
1262
David Brownell438d6c22006-12-10 21:21:31 +01001263/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001265static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001267 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 union i2c_smbus_data * data)
1269{
1270 /* So we need to generate a series of msgs. In the case of writing, we
1271 need to use only one message; when reading, we need two. We initialize
1272 most things with sane defaults, to keep the code below somewhat
1273 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001274 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1275 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001277 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1279 };
1280 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001281 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 msgbuf0[0] = command;
1284 switch(size) {
1285 case I2C_SMBUS_QUICK:
1286 msg[0].len = 0;
1287 /* Special case: The read/write field is used as data */
1288 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1289 num = 1;
1290 break;
1291 case I2C_SMBUS_BYTE:
1292 if (read_write == I2C_SMBUS_READ) {
1293 /* Special case: only a read! */
1294 msg[0].flags = I2C_M_RD | flags;
1295 num = 1;
1296 }
1297 break;
1298 case I2C_SMBUS_BYTE_DATA:
1299 if (read_write == I2C_SMBUS_READ)
1300 msg[1].len = 1;
1301 else {
1302 msg[0].len = 2;
1303 msgbuf0[1] = data->byte;
1304 }
1305 break;
1306 case I2C_SMBUS_WORD_DATA:
1307 if (read_write == I2C_SMBUS_READ)
1308 msg[1].len = 2;
1309 else {
1310 msg[0].len=3;
1311 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001312 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314 break;
1315 case I2C_SMBUS_PROC_CALL:
1316 num = 2; /* Special case */
1317 read_write = I2C_SMBUS_READ;
1318 msg[0].len = 3;
1319 msg[1].len = 2;
1320 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001321 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 break;
1323 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001325 msg[1].flags |= I2C_M_RECV_LEN;
1326 msg[1].len = 1; /* block length will be added by
1327 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 } else {
1329 msg[0].len = data->block[0] + 2;
1330 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1331 dev_err(&adapter->dev, "smbus_access called with "
1332 "invalid block write size (%d)\n",
1333 data->block[0]);
1334 return -1;
1335 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001336 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 msgbuf0[i] = data->block[i-1];
1338 }
1339 break;
1340 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001341 num = 2; /* Another special case */
1342 read_write = I2C_SMBUS_READ;
1343 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1344 dev_err(&adapter->dev, "%s called with invalid "
1345 "block proc call size (%d)\n", __FUNCTION__,
1346 data->block[0]);
1347 return -1;
1348 }
1349 msg[0].len = data->block[0] + 2;
1350 for (i = 1; i < msg[0].len; i++)
1351 msgbuf0[i] = data->block[i-1];
1352 msg[1].flags |= I2C_M_RECV_LEN;
1353 msg[1].len = 1; /* block length will be added by
1354 the underlying bus driver */
1355 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 case I2C_SMBUS_I2C_BLOCK_DATA:
1357 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001358 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 } else {
1360 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001361 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1363 "invalid block write size (%d)\n",
1364 data->block[0]);
1365 return -1;
1366 }
1367 for (i = 1; i <= data->block[0]; i++)
1368 msgbuf0[i] = data->block[i];
1369 }
1370 break;
1371 default:
1372 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1373 size);
1374 return -1;
1375 }
1376
Jean Delvare421ef472005-10-26 21:28:55 +02001377 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1378 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1379 if (i) {
1380 /* Compute PEC if first message is a write */
1381 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001382 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001383 i2c_smbus_add_pec(&msg[0]);
1384 else /* Write followed by read */
1385 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1386 }
1387 /* Ask for PEC if last message is a read */
1388 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001389 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001390 }
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (i2c_transfer(adapter, msg, num) < 0)
1393 return -1;
1394
Jean Delvare421ef472005-10-26 21:28:55 +02001395 /* Check PEC if last message is a read */
1396 if (i && (msg[num-1].flags & I2C_M_RD)) {
1397 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1398 return -1;
1399 }
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (read_write == I2C_SMBUS_READ)
1402 switch(size) {
1403 case I2C_SMBUS_BYTE:
1404 data->byte = msgbuf0[0];
1405 break;
1406 case I2C_SMBUS_BYTE_DATA:
1407 data->byte = msgbuf1[0];
1408 break;
David Brownell438d6c22006-12-10 21:21:31 +01001409 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 case I2C_SMBUS_PROC_CALL:
1411 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1412 break;
1413 case I2C_SMBUS_I2C_BLOCK_DATA:
1414 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001415 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1416 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 data->block[i+1] = msgbuf1[i];
1418 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001419 case I2C_SMBUS_BLOCK_DATA:
1420 case I2C_SMBUS_BLOCK_PROC_CALL:
1421 for (i = 0; i < msgbuf1[0] + 1; i++)
1422 data->block[i] = msgbuf1[i];
1423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 return 0;
1426}
1427
1428
1429s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001430 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 union i2c_smbus_data * data)
1432{
1433 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001438 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1440 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001441 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 } else
1443 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1444 command,size,data);
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return res;
1447}
1448
1449
Jean Delvareb31366f2007-05-01 23:26:28 +02001450/* Next three are needed by i2c-isa */
Jean Delvareefde7232005-07-20 23:03:50 +02001451EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
Jean Delvareefde7232005-07-20 23:03:50 +02001452EXPORT_SYMBOL_GPL(i2c_adapter_class);
1453EXPORT_SYMBOL_GPL(i2c_bus_type);
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456EXPORT_SYMBOL(i2c_del_driver);
1457EXPORT_SYMBOL(i2c_attach_client);
1458EXPORT_SYMBOL(i2c_detach_client);
1459EXPORT_SYMBOL(i2c_use_client);
1460EXPORT_SYMBOL(i2c_release_client);
1461EXPORT_SYMBOL(i2c_clients_command);
1462EXPORT_SYMBOL(i2c_check_addr);
1463
1464EXPORT_SYMBOL(i2c_master_send);
1465EXPORT_SYMBOL(i2c_master_recv);
1466EXPORT_SYMBOL(i2c_control);
1467EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468EXPORT_SYMBOL(i2c_get_adapter);
1469EXPORT_SYMBOL(i2c_put_adapter);
1470EXPORT_SYMBOL(i2c_probe);
1471
1472EXPORT_SYMBOL(i2c_smbus_xfer);
1473EXPORT_SYMBOL(i2c_smbus_write_quick);
1474EXPORT_SYMBOL(i2c_smbus_read_byte);
1475EXPORT_SYMBOL(i2c_smbus_write_byte);
1476EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1477EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1478EXPORT_SYMBOL(i2c_smbus_read_word_data);
1479EXPORT_SYMBOL(i2c_smbus_write_word_data);
1480EXPORT_SYMBOL(i2c_smbus_write_block_data);
1481EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001482EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1485MODULE_DESCRIPTION("I2C-Bus main module");
1486MODULE_LICENSE("GPL");