blob: d48438908e1e51331b77e0ef51c18b3dc4b1bd3a [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
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 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>
Arjan van de Venb3585e42006-01-11 10:50:26 +010032#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010033#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010034#include <linux/hardirq.h>
35#include <linux/irqflags.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
Jean Delvarecaada322008-01-27 18:14:49 +010041static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_IDR(i2c_adapter_idr);
43
Jean Delvare4735c982008-07-14 22:38:36 +020044#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
45
Jean Delvare729d6dd2009-06-19 16:58:18 +020046static int i2c_attach_client(struct i2c_client *client);
Jean Delvare4735c982008-07-14 22:38:36 +020047static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010048
49/* ------------------------------------------------------------------------- */
50
Jean Delvared2653e92008-04-29 23:11:39 +020051static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
52 const struct i2c_client *client)
53{
54 while (id->name[0]) {
55 if (strcmp(client->name, id->name) == 0)
56 return id;
57 id++;
58 }
59 return NULL;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int i2c_device_match(struct device *dev, struct device_driver *drv)
63{
David Brownell7b4fbc52007-05-01 23:26:30 +020064 struct i2c_client *client = to_i2c_client(dev);
65 struct i2c_driver *driver = to_i2c_driver(drv);
66
67 /* make legacy i2c drivers bypass driver model probing entirely;
68 * such drivers scan each i2c adapter/bus themselves.
69 */
David Brownella1d9e6e2007-05-01 23:26:30 +020070 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020071 return 0;
72
Jean Delvared2653e92008-04-29 23:11:39 +020073 /* match on an id table if there is one */
74 if (driver->id_table)
75 return i2c_match_id(driver->id_table, client) != NULL;
76
Jean Delvareeb8a7902008-05-18 20:49:41 +020077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
David Brownell7b4fbc52007-05-01 23:26:30 +020080#ifdef CONFIG_HOTPLUG
81
82/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020083static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020084{
85 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020086
Jean Delvareeb8a7902008-05-18 20:49:41 +020087 if (add_uevent_var(env, "MODALIAS=%s%s",
88 I2C_MODULE_PREFIX, client->name))
89 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020090 dev_dbg(dev, "uevent\n");
91 return 0;
92}
93
94#else
95#define i2c_device_uevent NULL
96#endif /* CONFIG_HOTPLUG */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int i2c_device_probe(struct device *dev)
99{
David Brownell7b4fbc52007-05-01 23:26:30 +0200100 struct i2c_client *client = to_i2c_client(dev);
101 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +0100102 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200103
Jean Delvaree0457442008-07-14 22:38:30 +0200104 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200105 return -ENODEV;
106 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200107 if (!device_can_wakeup(&client->dev))
108 device_init_wakeup(&client->dev,
109 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200110 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200111
Jean Delvaree0457442008-07-14 22:38:30 +0200112 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100113 if (status)
114 client->driver = NULL;
115 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static int i2c_device_remove(struct device *dev)
119{
David Brownella1d9e6e2007-05-01 23:26:30 +0200120 struct i2c_client *client = to_i2c_client(dev);
121 struct i2c_driver *driver;
122 int status;
123
124 if (!dev->driver)
125 return 0;
126
127 driver = to_i2c_driver(dev->driver);
128 if (driver->remove) {
129 dev_dbg(dev, "remove\n");
130 status = driver->remove(client);
131 } else {
132 dev->driver = NULL;
133 status = 0;
134 }
135 if (status == 0)
136 client->driver = NULL;
137 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
David Brownellf37dd802007-02-13 22:09:00 +0100140static void i2c_device_shutdown(struct device *dev)
141{
142 struct i2c_driver *driver;
143
144 if (!dev->driver)
145 return;
146 driver = to_i2c_driver(dev->driver);
147 if (driver->shutdown)
148 driver->shutdown(to_i2c_client(dev));
149}
150
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100151static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100152{
153 struct i2c_driver *driver;
154
155 if (!dev->driver)
156 return 0;
157 driver = to_i2c_driver(dev->driver);
158 if (!driver->suspend)
159 return 0;
160 return driver->suspend(to_i2c_client(dev), mesg);
161}
162
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100163static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100164{
165 struct i2c_driver *driver;
166
167 if (!dev->driver)
168 return 0;
169 driver = to_i2c_driver(dev->driver);
170 if (!driver->resume)
171 return 0;
172 return driver->resume(to_i2c_client(dev));
173}
174
David Brownell7b4fbc52007-05-01 23:26:30 +0200175static void i2c_client_release(struct device *dev)
176{
177 struct i2c_client *client = to_i2c_client(dev);
178 complete(&client->released);
179}
180
David Brownell9c1600e2007-05-01 23:26:31 +0200181static void i2c_client_dev_release(struct device *dev)
182{
183 kfree(to_i2c_client(dev));
184}
185
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100186static ssize_t
187show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200188{
189 struct i2c_client *client = to_i2c_client(dev);
190 return sprintf(buf, "%s\n", client->name);
191}
192
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100193static ssize_t
194show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200195{
196 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200197 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200198}
199
200static struct device_attribute i2c_dev_attrs[] = {
201 __ATTR(name, S_IRUGO, show_client_name, NULL),
202 /* modalias helps coldplug: modprobe $(cat .../modalias) */
203 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
204 { },
205};
206
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200207struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100208 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200209 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100210 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200211 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100212 .probe = i2c_device_probe,
213 .remove = i2c_device_remove,
214 .shutdown = i2c_device_shutdown,
215 .suspend = i2c_device_suspend,
216 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000217};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200218EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000219
David Brownell9b766b82008-01-27 18:14:51 +0100220
221/**
222 * i2c_verify_client - return parameter as i2c_client, or NULL
223 * @dev: device, probably from some driver model iterator
224 *
225 * When traversing the driver model tree, perhaps using driver model
226 * iterators like @device_for_each_child(), you can't assume very much
227 * about the nodes you find. Use this function to avoid oopses caused
228 * by wrongly treating some non-I2C device as an i2c_client.
229 */
230struct i2c_client *i2c_verify_client(struct device *dev)
231{
232 return (dev->bus == &i2c_bus_type)
233 ? to_i2c_client(dev)
234 : NULL;
235}
236EXPORT_SYMBOL(i2c_verify_client);
237
238
David Brownell9c1600e2007-05-01 23:26:31 +0200239/**
240 * i2c_new_device - instantiate an i2c device for use with a new style driver
241 * @adap: the adapter managing the device
242 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200243 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200244 *
245 * Create a device to work with a new style i2c driver, where binding is
246 * handled through driver model probe()/remove() methods. This call is not
247 * appropriate for use by mainboad initialization logic, which usually runs
248 * during an arch_initcall() long before any i2c_adapter could exist.
249 *
250 * This returns the new i2c client, which may be saved for later use with
251 * i2c_unregister_device(); or NULL to indicate an error.
252 */
253struct i2c_client *
254i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
255{
256 struct i2c_client *client;
257 int status;
258
259 client = kzalloc(sizeof *client, GFP_KERNEL);
260 if (!client)
261 return NULL;
262
263 client->adapter = adap;
264
265 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200266
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200267 if (info->archdata)
268 client->dev.archdata = *info->archdata;
269
Marc Pignatee354252008-08-28 08:33:22 +0200270 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200271 client->addr = info->addr;
272 client->irq = info->irq;
273
David Brownell9c1600e2007-05-01 23:26:31 +0200274 strlcpy(client->name, info->type, sizeof(client->name));
275
276 /* a new style driver may be bound to this device when we
277 * return from this function, or any later moment (e.g. maybe
278 * hotplugging will load the driver module). and the device
279 * refcount model is the standard driver model one.
280 */
281 status = i2c_attach_client(client);
282 if (status < 0) {
283 kfree(client);
284 client = NULL;
285 }
286 return client;
287}
288EXPORT_SYMBOL_GPL(i2c_new_device);
289
290
291/**
292 * i2c_unregister_device - reverse effect of i2c_new_device()
293 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200294 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200295 */
296void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200297{
298 struct i2c_adapter *adapter = client->adapter;
299 struct i2c_driver *driver = client->driver;
300
301 if (driver && !is_newstyle_driver(driver)) {
302 dev_err(&client->dev, "can't unregister devices "
303 "with legacy drivers\n");
304 WARN_ON(1);
305 return;
306 }
307
308 mutex_lock(&adapter->clist_lock);
309 list_del(&client->list);
310 mutex_unlock(&adapter->clist_lock);
311
312 device_unregister(&client->dev);
313}
David Brownell9c1600e2007-05-01 23:26:31 +0200314EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200315
316
Jean Delvare60b129d2008-05-11 20:37:06 +0200317static const struct i2c_device_id dummy_id[] = {
318 { "dummy", 0 },
319 { },
320};
321
Jean Delvared2653e92008-04-29 23:11:39 +0200322static int dummy_probe(struct i2c_client *client,
323 const struct i2c_device_id *id)
324{
325 return 0;
326}
327
328static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100329{
330 return 0;
331}
332
333static struct i2c_driver dummy_driver = {
334 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200335 .probe = dummy_probe,
336 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200337 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100338};
339
340/**
341 * i2c_new_dummy - return a new i2c device bound to a dummy driver
342 * @adapter: the adapter managing the device
343 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100344 * Context: can sleep
345 *
346 * This returns an I2C client bound to the "dummy" driver, intended for use
347 * with devices that consume multiple addresses. Examples of such chips
348 * include various EEPROMS (like 24c04 and 24c08 models).
349 *
350 * These dummy devices have two main uses. First, most I2C and SMBus calls
351 * except i2c_transfer() need a client handle; the dummy will be that handle.
352 * And second, this prevents the specified address from being bound to a
353 * different driver.
354 *
355 * This returns the new i2c client, which should be saved for later use with
356 * i2c_unregister_device(); or NULL to indicate an error.
357 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100358struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100359{
360 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200361 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100362 };
363
David Brownelle9f13732008-01-27 18:14:52 +0100364 return i2c_new_device(adapter, &info);
365}
366EXPORT_SYMBOL_GPL(i2c_new_dummy);
367
David Brownellf37dd802007-02-13 22:09:00 +0100368/* ------------------------------------------------------------------------- */
369
David Brownell16ffadf2007-05-01 23:26:28 +0200370/* I2C bus adapters -- one roots each I2C or SMBUS segment */
371
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200372static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
David Brownellef2c83212007-05-01 23:26:28 +0200374 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 complete(&adap->dev_released);
376}
377
David Brownell16ffadf2007-05-01 23:26:28 +0200378static ssize_t
379show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
David Brownellef2c83212007-05-01 23:26:28 +0200381 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return sprintf(buf, "%s\n", adap->name);
383}
David Brownell16ffadf2007-05-01 23:26:28 +0200384
385static struct device_attribute i2c_adapter_attrs[] = {
386 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
387 { },
388};
389
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200390static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200391 .owner = THIS_MODULE,
392 .name = "i2c-adapter",
393 .dev_attrs = i2c_adapter_attrs,
394};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
David Brownell9c1600e2007-05-01 23:26:31 +0200396static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
397{
398 struct i2c_devinfo *devinfo;
399
400 mutex_lock(&__i2c_board_lock);
401 list_for_each_entry(devinfo, &__i2c_board_list, list) {
402 if (devinfo->busnum == adapter->nr
403 && !i2c_new_device(adapter,
404 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100405 dev_err(&adapter->dev,
406 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200407 devinfo->board_info.addr);
408 }
409 mutex_unlock(&__i2c_board_lock);
410}
411
Jean Delvare026526f2008-01-27 18:14:49 +0100412static int i2c_do_add_adapter(struct device_driver *d, void *data)
413{
414 struct i2c_driver *driver = to_i2c_driver(d);
415 struct i2c_adapter *adap = data;
416
Jean Delvare4735c982008-07-14 22:38:36 +0200417 /* Detect supported devices on that bus, and instantiate them */
418 i2c_detect(adap, driver);
419
420 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100421 if (driver->attach_adapter) {
422 /* We ignore the return code; if it fails, too bad */
423 driver->attach_adapter(adap);
424 }
425 return 0;
426}
427
David Brownell6e13e642007-05-01 23:26:31 +0200428static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Jean Delvare026526f2008-01-27 18:14:49 +0100430 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
David Brownell1d0b19c2008-10-14 17:30:05 +0200432 /* Can't register until after driver model init */
433 if (unlikely(WARN_ON(!i2c_bus_type.p)))
434 return -EAGAIN;
435
Ingo Molnar5c085d32006-01-18 23:16:04 +0100436 mutex_init(&adap->bus_lock);
437 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 INIT_LIST_HEAD(&adap->clients);
439
Jean Delvarecaada322008-01-27 18:14:49 +0100440 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200441
Jean Delvare8fcfef62009-03-28 21:34:43 +0100442 /* Set default timeout to 1 second if not already set */
443 if (adap->timeout == 0)
444 adap->timeout = HZ;
445
Kay Sievers27d9c182009-01-07 14:29:16 +0100446 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200448 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200449 res = device_register(&adap->dev);
450 if (res)
451 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200453 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
454
Jean Delvare729d6dd2009-06-19 16:58:18 +0200455 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200456 if (adap->nr < __i2c_first_dynamic_bus_num)
457 i2c_scan_static_board_info(adap);
458
Jean Delvare4735c982008-07-14 22:38:36 +0200459 /* Notify drivers */
Jean Delvare026526f2008-01-27 18:14:49 +0100460 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
461 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100464 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200466
Jean Delvareb119c6c2006-08-15 18:26:30 +0200467out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200468 idr_remove(&i2c_adapter_idr, adap->nr);
469 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
David Brownell6e13e642007-05-01 23:26:31 +0200472/**
473 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
474 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200475 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200476 *
477 * This routine is used to declare an I2C adapter when its bus number
478 * doesn't matter. Examples: for I2C adapters dynamically added by
479 * USB links or PCI plugin cards.
480 *
481 * When this returns zero, a new bus number was allocated and stored
482 * in adap->nr, and the specified adapter became available for clients.
483 * Otherwise, a negative errno value is returned.
484 */
485int i2c_add_adapter(struct i2c_adapter *adapter)
486{
487 int id, res = 0;
488
489retry:
490 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
491 return -ENOMEM;
492
Jean Delvarecaada322008-01-27 18:14:49 +0100493 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200494 /* "above" here means "above or equal to", sigh */
495 res = idr_get_new_above(&i2c_adapter_idr, adapter,
496 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100497 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200498
499 if (res < 0) {
500 if (res == -EAGAIN)
501 goto retry;
502 return res;
503 }
504
505 adapter->nr = id;
506 return i2c_register_adapter(adapter);
507}
508EXPORT_SYMBOL(i2c_add_adapter);
509
510/**
511 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
512 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200513 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200514 *
515 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100516 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
517 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200518 * is used to properly configure I2C devices.
519 *
520 * If no devices have pre-been declared for this bus, then be sure to
521 * register the adapter before any dynamically allocated ones. Otherwise
522 * the required bus ID may not be available.
523 *
524 * When this returns zero, the specified adapter became available for
525 * clients using the bus number provided in adap->nr. Also, the table
526 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
527 * and the appropriate driver model device nodes are created. Otherwise, a
528 * negative errno value is returned.
529 */
530int i2c_add_numbered_adapter(struct i2c_adapter *adap)
531{
532 int id;
533 int status;
534
535 if (adap->nr & ~MAX_ID_MASK)
536 return -EINVAL;
537
538retry:
539 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
540 return -ENOMEM;
541
Jean Delvarecaada322008-01-27 18:14:49 +0100542 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200543 /* "above" here means "above or equal to", sigh;
544 * we need the "equal to" result to force the result
545 */
546 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
547 if (status == 0 && id != adap->nr) {
548 status = -EBUSY;
549 idr_remove(&i2c_adapter_idr, id);
550 }
Jean Delvarecaada322008-01-27 18:14:49 +0100551 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200552 if (status == -EAGAIN)
553 goto retry;
554
555 if (status == 0)
556 status = i2c_register_adapter(adap);
557 return status;
558}
559EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
560
Jean Delvare026526f2008-01-27 18:14:49 +0100561static int i2c_do_del_adapter(struct device_driver *d, void *data)
562{
563 struct i2c_driver *driver = to_i2c_driver(d);
564 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200565 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100566 int res;
567
Jean Delvareacec2112009-03-28 21:34:40 +0100568 /* Remove the devices we created ourselves as the result of hardware
569 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200570 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
571 if (client->adapter == adapter) {
572 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
573 client->name, client->addr);
574 list_del(&client->detected);
575 i2c_unregister_device(client);
576 }
577 }
578
Jean Delvare026526f2008-01-27 18:14:49 +0100579 if (!driver->detach_adapter)
580 return 0;
581 res = driver->detach_adapter(adapter);
582 if (res)
583 dev_err(&adapter->dev, "detach_adapter failed (%d) "
584 "for driver [%s]\n", res, driver->driver.name);
585 return res;
586}
587
David Brownelld64f73b2007-07-12 14:12:28 +0200588/**
589 * i2c_del_adapter - unregister I2C adapter
590 * @adap: the adapter being unregistered
591 * Context: can sleep
592 *
593 * This unregisters an I2C adapter which was previously registered
594 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596int i2c_del_adapter(struct i2c_adapter *adap)
597{
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200598 struct i2c_client *client, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int res = 0;
600
Jean Delvarecaada322008-01-27 18:14:49 +0100601 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100604 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200605 pr_debug("i2c-core: attempting to delete unregistered "
606 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 res = -EINVAL;
608 goto out_unlock;
609 }
610
Jean Delvare026526f2008-01-27 18:14:49 +0100611 /* Tell drivers about this removal */
612 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
613 i2c_do_del_adapter);
614 if (res)
615 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Jean Delvare729d6dd2009-06-19 16:58:18 +0200617 /* Detach any active clients */
Jean Delvare79b93e12008-11-28 15:24:38 +0100618 list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
Jean Delvare729d6dd2009-06-19 16:58:18 +0200619 i2c_unregister_device(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
622 /* clean up the sysfs representation */
623 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* wait for sysfs to drop all references */
627 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
David Brownell6e13e642007-05-01 23:26:31 +0200629 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 idr_remove(&i2c_adapter_idr, adap->nr);
631
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200632 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200634 /* Clear the device structure in case this adapter is ever going to be
635 added again */
636 memset(&adap->dev, 0, sizeof(adap->dev));
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100639 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return res;
641}
David Brownellc0564602007-05-01 23:26:31 +0200642EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644
David Brownell7b4fbc52007-05-01 23:26:30 +0200645/* ------------------------------------------------------------------------- */
646
Dave Young7f101a92008-07-14 22:38:19 +0200647static int __attach_adapter(struct device *dev, void *data)
648{
649 struct i2c_adapter *adapter = to_i2c_adapter(dev);
650 struct i2c_driver *driver = data;
651
Jean Delvare4735c982008-07-14 22:38:36 +0200652 i2c_detect(adapter, driver);
653
654 /* Legacy drivers scan i2c busses directly */
655 if (driver->attach_adapter)
656 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200657
658 return 0;
659}
660
David Brownell7b4fbc52007-05-01 23:26:30 +0200661/*
662 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200663 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
665
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800666int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100668 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
David Brownell1d0b19c2008-10-14 17:30:05 +0200670 /* Can't register until after driver model init */
671 if (unlikely(WARN_ON(!i2c_bus_type.p)))
672 return -EAGAIN;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800675 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Jean Delvare729d6dd2009-06-19 16:58:18 +0200678 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200679 * will have called probe() for all matching-but-unbound devices.
680 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 res = driver_register(&driver->driver);
682 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100683 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100684
Jean Delvarecaada322008-01-27 18:14:49 +0100685 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100686
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100687 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jean Delvare4735c982008-07-14 22:38:36 +0200689 INIT_LIST_HEAD(&driver->clients);
690 /* Walk the adapters that are already present */
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400691 class_for_each_device(&i2c_adapter_class, NULL, driver,
692 __attach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Jean Delvarecaada322008-01-27 18:14:49 +0100694 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800697EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Dave Young7f101a92008-07-14 22:38:19 +0200699static int __detach_adapter(struct device *dev, void *data)
700{
701 struct i2c_adapter *adapter = to_i2c_adapter(dev);
702 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200703 struct i2c_client *client, *_n;
704
Jean Delvareacec2112009-03-28 21:34:40 +0100705 /* Remove the devices we created ourselves as the result of hardware
706 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200707 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
708 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
709 client->name, client->addr);
710 list_del(&client->detected);
711 i2c_unregister_device(client);
712 }
713
714 if (is_newstyle_driver(driver))
715 return 0;
Dave Young7f101a92008-07-14 22:38:19 +0200716
Dave Young7f101a92008-07-14 22:38:19 +0200717 if (driver->detach_adapter) {
718 if (driver->detach_adapter(adapter))
719 dev_err(&adapter->dev,
720 "detach_adapter failed for driver [%s]\n",
721 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200722 }
723
724 return 0;
725}
726
David Brownella1d9e6e2007-05-01 23:26:30 +0200727/**
728 * i2c_del_driver - unregister I2C driver
729 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200730 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200731 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200732void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Jean Delvarecaada322008-01-27 18:14:49 +0100734 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400736 class_for_each_device(&i2c_adapter_class, NULL, driver,
737 __detach_adapter);
David Brownella1d9e6e2007-05-01 23:26:30 +0200738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100740 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jean Delvarecaada322008-01-27 18:14:49 +0100742 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
David Brownellc0564602007-05-01 23:26:31 +0200744EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
David Brownell7b4fbc52007-05-01 23:26:30 +0200746/* ------------------------------------------------------------------------- */
747
David Brownell9b766b82008-01-27 18:14:51 +0100748static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
David Brownell9b766b82008-01-27 18:14:51 +0100750 struct i2c_client *client = i2c_verify_client(dev);
751 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
David Brownell9b766b82008-01-27 18:14:51 +0100753 if (client && client->addr == addr)
754 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756}
757
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100758static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
David Brownell9b766b82008-01-27 18:14:51 +0100760 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Jean Delvare729d6dd2009-06-19 16:58:18 +0200763static int i2c_attach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct i2c_adapter *adapter = client->adapter;
Jean Delvarec1159f92008-08-10 22:56:16 +0200766 int res;
767
768 /* Check for address business */
769 res = i2c_check_addr(adapter, client->addr);
770 if (res)
771 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200775
776 if (client->driver)
777 client->dev.driver = &client->driver->driver;
778
David Brownellde81d2a2007-05-22 19:49:16 +0200779 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200780 client->dev.release = i2c_client_release;
Ming Leif67f1292009-03-01 21:10:49 +0800781 dev_set_uevent_suppress(&client->dev, 1);
David Brownellde81d2a2007-05-22 19:49:16 +0200782 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200783 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100784
Kay Sievers27d9c182009-01-07 14:29:16 +0100785 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
786 client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200787 res = device_register(&client->dev);
788 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100789 goto out_err;
790
791 mutex_lock(&adapter->clist_lock);
792 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200793 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200794
David Brownell86ec5ec2008-01-27 18:14:51 +0100795 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
Kay Sievers27d9c182009-01-07 14:29:16 +0100796 client->name, dev_name(&client->dev));
David Brownell86ec5ec2008-01-27 18:14:51 +0100797
Jean Delvare77ed74d2006-09-30 17:18:59 +0200798 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200799
David Brownell86ec5ec2008-01-27 18:14:51 +0100800out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200801 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
802 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200803 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Jean Delvaree48d3312008-01-27 18:14:48 +0100806/**
807 * i2c_use_client - increments the reference count of the i2c client structure
808 * @client: the client being referenced
809 *
810 * Each live reference to a client should be refcounted. The driver model does
811 * that automatically as part of driver binding, so that most drivers don't
812 * need to do this explicitly: they hold a reference until they're unbound
813 * from the device.
814 *
815 * A pointer to the client with the incremented reference counter is returned.
816 */
817struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
David Brownell6ea438e2008-07-14 22:38:24 +0200819 if (client && get_device(&client->dev))
820 return client;
821 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
David Brownellc0564602007-05-01 23:26:31 +0200823EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Jean Delvaree48d3312008-01-27 18:14:48 +0100825/**
826 * i2c_release_client - release a use of the i2c client structure
827 * @client: the client being no longer referenced
828 *
829 * Must be called when a user of a client is finished with it.
830 */
831void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
David Brownell6ea438e2008-07-14 22:38:24 +0200833 if (client)
834 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
David Brownellc0564602007-05-01 23:26:31 +0200836EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
David Brownell9b766b82008-01-27 18:14:51 +0100838struct i2c_cmd_arg {
839 unsigned cmd;
840 void *arg;
841};
842
843static int i2c_cmd(struct device *dev, void *_arg)
844{
845 struct i2c_client *client = i2c_verify_client(dev);
846 struct i2c_cmd_arg *arg = _arg;
847
848 if (client && client->driver && client->driver->command)
849 client->driver->command(client, arg->cmd, arg->arg);
850 return 0;
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
854{
David Brownell9b766b82008-01-27 18:14:51 +0100855 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
David Brownell9b766b82008-01-27 18:14:51 +0100857 cmd_arg.cmd = cmd;
858 cmd_arg.arg = arg;
859 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
David Brownellc0564602007-05-01 23:26:31 +0200861EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863static int __init i2c_init(void)
864{
865 int retval;
866
867 retval = bus_register(&i2c_bus_type);
868 if (retval)
869 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100870 retval = class_register(&i2c_adapter_class);
871 if (retval)
872 goto bus_err;
873 retval = i2c_add_driver(&dummy_driver);
874 if (retval)
875 goto class_err;
876 return 0;
877
878class_err:
879 class_unregister(&i2c_adapter_class);
880bus_err:
881 bus_unregister(&i2c_bus_type);
882 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885static void __exit i2c_exit(void)
886{
David Brownelle9f13732008-01-27 18:14:52 +0100887 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 bus_unregister(&i2c_bus_type);
890}
891
David Brownella10f9e72008-10-14 17:30:06 +0200892/* We must initialize early, because some subsystems register i2c drivers
893 * in subsys_initcall() code, but are linked (and initialized) before i2c.
894 */
895postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896module_exit(i2c_exit);
897
898/* ----------------------------------------------------
899 * the functional interface to the i2c busses.
900 * ----------------------------------------------------
901 */
902
David Brownella1cdeda2008-07-14 22:38:24 +0200903/**
904 * i2c_transfer - execute a single or combined I2C message
905 * @adap: Handle to I2C bus
906 * @msgs: One or more messages to execute before STOP is issued to
907 * terminate the operation; each message begins with a START.
908 * @num: Number of messages to be executed.
909 *
910 * Returns negative errno, else the number of messages executed.
911 *
912 * Note that there is no requirement that each message be sent to
913 * the same slave address, although that is the most common model.
914 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100915int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Clifford Wolf66b650f2009-06-15 18:01:46 +0200917 unsigned long orig_jiffies;
918 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
David Brownella1cdeda2008-07-14 22:38:24 +0200920 /* REVISIT the fault reporting model here is weak:
921 *
922 * - When we get an error after receiving N bytes from a slave,
923 * there is no way to report "N".
924 *
925 * - When we get a NAK after transmitting N bytes to a slave,
926 * there is no way to report "N" ... or to let the master
927 * continue executing the rest of this combined message, if
928 * that's the appropriate response.
929 *
930 * - When for example "num" is two and we successfully complete
931 * the first message but get an error part way through the
932 * second, it's unclear whether that should be reported as
933 * one (discarding status on the second message) or errno
934 * (discarding status on the first one).
935 */
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (adap->algo->master_xfer) {
938#ifdef DEBUG
939 for (ret = 0; ret < num; ret++) {
940 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200941 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
942 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
943 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945#endif
946
Mike Rapoportcea443a82008-01-27 18:14:50 +0100947 if (in_atomic() || irqs_disabled()) {
948 ret = mutex_trylock(&adap->bus_lock);
949 if (!ret)
950 /* I2C activity is ongoing. */
951 return -EAGAIN;
952 } else {
953 mutex_lock_nested(&adap->bus_lock, adap->level);
954 }
955
Clifford Wolf66b650f2009-06-15 18:01:46 +0200956 /* Retry automatically on arbitration loss */
957 orig_jiffies = jiffies;
958 for (ret = 0, try = 0; try <= adap->retries; try++) {
959 ret = adap->algo->master_xfer(adap, msgs, num);
960 if (ret != -EAGAIN)
961 break;
962 if (time_after(jiffies, orig_jiffies + adap->timeout))
963 break;
964 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100965 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 return ret;
968 } else {
969 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +0200970 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972}
David Brownellc0564602007-05-01 23:26:31 +0200973EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
David Brownella1cdeda2008-07-14 22:38:24 +0200975/**
976 * i2c_master_send - issue a single I2C message in master transmit mode
977 * @client: Handle to slave device
978 * @buf: Data that will be written to the slave
979 * @count: How many bytes to write
980 *
981 * Returns negative errno, or else the number of bytes written.
982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
984{
985 int ret;
986 struct i2c_adapter *adap=client->adapter;
987 struct i2c_msg msg;
988
Jean Delvare815f55f2005-05-07 22:58:46 +0200989 msg.addr = client->addr;
990 msg.flags = client->flags & I2C_M_TEN;
991 msg.len = count;
992 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100993
Jean Delvare815f55f2005-05-07 22:58:46 +0200994 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Jean Delvare815f55f2005-05-07 22:58:46 +0200996 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
997 transmitted, else error code. */
998 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
David Brownellc0564602007-05-01 23:26:31 +02001000EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
David Brownella1cdeda2008-07-14 22:38:24 +02001002/**
1003 * i2c_master_recv - issue a single I2C message in master receive mode
1004 * @client: Handle to slave device
1005 * @buf: Where to store data read from slave
1006 * @count: How many bytes to read
1007 *
1008 * Returns negative errno, or else the number of bytes read.
1009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1011{
1012 struct i2c_adapter *adap=client->adapter;
1013 struct i2c_msg msg;
1014 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Jean Delvare815f55f2005-05-07 22:58:46 +02001016 msg.addr = client->addr;
1017 msg.flags = client->flags & I2C_M_TEN;
1018 msg.flags |= I2C_M_RD;
1019 msg.len = count;
1020 msg.buf = buf;
1021
1022 ret = i2c_transfer(adap, &msg, 1);
1023
1024 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1025 transmitted, else error code. */
1026 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
David Brownellc0564602007-05-01 23:26:31 +02001028EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/* ----------------------------------------------------
1031 * the i2c address scanning function
1032 * Will not work for 10-bit addresses!
1033 * ----------------------------------------------------
1034 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001035
Jean Delvare4735c982008-07-14 22:38:36 +02001036static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1037 struct i2c_driver *driver)
1038{
1039 struct i2c_board_info info;
1040 struct i2c_adapter *adapter = temp_client->adapter;
1041 int addr = temp_client->addr;
1042 int err;
1043
1044 /* Make sure the address is valid */
1045 if (addr < 0x03 || addr > 0x77) {
1046 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1047 addr);
1048 return -EINVAL;
1049 }
1050
1051 /* Skip if already in use */
1052 if (i2c_check_addr(adapter, addr))
1053 return 0;
1054
1055 /* Make sure there is something at this address, unless forced */
1056 if (kind < 0) {
1057 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1058 I2C_SMBUS_QUICK, NULL) < 0)
1059 return 0;
1060
1061 /* prevent 24RF08 corruption */
1062 if ((addr & ~0x0f) == 0x50)
1063 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1064 I2C_SMBUS_QUICK, NULL);
1065 }
1066
1067 /* Finally call the custom detection function */
1068 memset(&info, 0, sizeof(struct i2c_board_info));
1069 info.addr = addr;
1070 err = driver->detect(temp_client, kind, &info);
1071 if (err) {
1072 /* -ENODEV is returned if the detection fails. We catch it
1073 here as this isn't an error. */
1074 return err == -ENODEV ? 0 : err;
1075 }
1076
1077 /* Consistency check */
1078 if (info.type[0] == '\0') {
1079 dev_err(&adapter->dev, "%s detection function provided "
1080 "no name for 0x%x\n", driver->driver.name,
1081 addr);
1082 } else {
1083 struct i2c_client *client;
1084
1085 /* Detection succeeded, instantiate the device */
1086 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1087 info.type, info.addr);
1088 client = i2c_new_device(adapter, &info);
1089 if (client)
1090 list_add_tail(&client->detected, &driver->clients);
1091 else
1092 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1093 info.type, info.addr);
1094 }
1095 return 0;
1096}
1097
1098static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1099{
1100 const struct i2c_client_address_data *address_data;
1101 struct i2c_client *temp_client;
1102 int i, err = 0;
1103 int adap_id = i2c_adapter_id(adapter);
1104
1105 address_data = driver->address_data;
1106 if (!driver->detect || !address_data)
1107 return 0;
1108
1109 /* Set up a temporary client to help detect callback */
1110 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1111 if (!temp_client)
1112 return -ENOMEM;
1113 temp_client->adapter = adapter;
1114
1115 /* Force entries are done first, and are not affected by ignore
1116 entries */
1117 if (address_data->forces) {
1118 const unsigned short * const *forces = address_data->forces;
1119 int kind;
1120
1121 for (kind = 0; forces[kind]; kind++) {
1122 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1123 i += 2) {
1124 if (forces[kind][i] == adap_id
1125 || forces[kind][i] == ANY_I2C_BUS) {
1126 dev_dbg(&adapter->dev, "found force "
1127 "parameter for adapter %d, "
1128 "addr 0x%02x, kind %d\n",
1129 adap_id, forces[kind][i + 1],
1130 kind);
1131 temp_client->addr = forces[kind][i + 1];
1132 err = i2c_detect_address(temp_client,
1133 kind, driver);
1134 if (err)
1135 goto exit_free;
1136 }
1137 }
1138 }
1139 }
1140
Jean Delvare4329cf82008-08-28 08:33:23 +02001141 /* Stop here if the classes do not match */
1142 if (!(adapter->class & driver->class))
1143 goto exit_free;
1144
Jean Delvare4735c982008-07-14 22:38:36 +02001145 /* Stop here if we can't use SMBUS_QUICK */
1146 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1147 if (address_data->probe[0] == I2C_CLIENT_END
1148 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1149 goto exit_free;
1150
1151 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1152 "can't probe for chips\n");
1153 err = -EOPNOTSUPP;
1154 goto exit_free;
1155 }
1156
Jean Delvare4735c982008-07-14 22:38:36 +02001157 /* Probe entries are done second, and are not affected by ignore
1158 entries either */
1159 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1160 if (address_data->probe[i] == adap_id
1161 || address_data->probe[i] == ANY_I2C_BUS) {
1162 dev_dbg(&adapter->dev, "found probe parameter for "
1163 "adapter %d, addr 0x%02x\n", adap_id,
1164 address_data->probe[i + 1]);
1165 temp_client->addr = address_data->probe[i + 1];
1166 err = i2c_detect_address(temp_client, -1, driver);
1167 if (err)
1168 goto exit_free;
1169 }
1170 }
1171
1172 /* Normal entries are done last, unless shadowed by an ignore entry */
1173 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1174 int j, ignore;
1175
1176 ignore = 0;
1177 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1178 j += 2) {
1179 if ((address_data->ignore[j] == adap_id ||
1180 address_data->ignore[j] == ANY_I2C_BUS)
1181 && address_data->ignore[j + 1]
1182 == address_data->normal_i2c[i]) {
1183 dev_dbg(&adapter->dev, "found ignore "
1184 "parameter for adapter %d, "
1185 "addr 0x%02x\n", adap_id,
1186 address_data->ignore[j + 1]);
1187 ignore = 1;
1188 break;
1189 }
1190 }
1191 if (ignore)
1192 continue;
1193
1194 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1195 "addr 0x%02x\n", adap_id,
1196 address_data->normal_i2c[i]);
1197 temp_client->addr = address_data->normal_i2c[i];
1198 err = i2c_detect_address(temp_client, -1, driver);
1199 if (err)
1200 goto exit_free;
1201 }
1202
1203 exit_free:
1204 kfree(temp_client);
1205 return err;
1206}
1207
Jean Delvare12b5053a2007-05-01 23:26:31 +02001208struct i2c_client *
1209i2c_new_probed_device(struct i2c_adapter *adap,
1210 struct i2c_board_info *info,
1211 unsigned short const *addr_list)
1212{
1213 int i;
1214
1215 /* Stop here if the bus doesn't support probing */
1216 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1217 dev_err(&adap->dev, "Probing not supported\n");
1218 return NULL;
1219 }
1220
Jean Delvare12b5053a2007-05-01 23:26:31 +02001221 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1222 /* Check address validity */
1223 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1224 dev_warn(&adap->dev, "Invalid 7-bit address "
1225 "0x%02x\n", addr_list[i]);
1226 continue;
1227 }
1228
1229 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001230 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001231 dev_dbg(&adap->dev, "Address 0x%02x already in "
1232 "use, not probing\n", addr_list[i]);
1233 continue;
1234 }
1235
1236 /* Test address responsiveness
1237 The default probe method is a quick write, but it is known
1238 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1239 and could also irreversibly write-protect some EEPROMs, so
1240 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1241 read instead. Also, some bus drivers don't implement
1242 quick write, so we fallback to a byte read it that case
1243 too. */
1244 if ((addr_list[i] & ~0x07) == 0x30
1245 || (addr_list[i] & ~0x0f) == 0x50
1246 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001247 union i2c_smbus_data data;
1248
Jean Delvare12b5053a2007-05-01 23:26:31 +02001249 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1250 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001251 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001252 break;
1253 } else {
1254 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1255 I2C_SMBUS_WRITE, 0,
1256 I2C_SMBUS_QUICK, NULL) >= 0)
1257 break;
1258 }
1259 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001260
1261 if (addr_list[i] == I2C_CLIENT_END) {
1262 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1263 return NULL;
1264 }
1265
1266 info->addr = addr_list[i];
1267 return i2c_new_device(adap, info);
1268}
1269EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271struct i2c_adapter* i2c_get_adapter(int id)
1272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001274
Jean Delvarecaada322008-01-27 18:14:49 +01001275 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001276 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001277 if (adapter && !try_module_get(adapter->owner))
1278 adapter = NULL;
1279
Jean Delvarecaada322008-01-27 18:14:49 +01001280 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001281 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
David Brownellc0564602007-05-01 23:26:31 +02001283EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285void i2c_put_adapter(struct i2c_adapter *adap)
1286{
1287 module_put(adap->owner);
1288}
David Brownellc0564602007-05-01 23:26:31 +02001289EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291/* The SMBus parts */
1292
David Brownell438d6c22006-12-10 21:21:31 +01001293#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001294static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001299 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 data = data ^ POLY;
1301 data = data << 1;
1302 }
1303 return (u8)(data >> 8);
1304}
1305
Jean Delvare421ef472005-10-26 21:28:55 +02001306/* Incremental CRC8 over count bytes in the array pointed to by p */
1307static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 int i;
1310
1311 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001312 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return crc;
1314}
1315
Jean Delvare421ef472005-10-26 21:28:55 +02001316/* Assume a 7-bit address, which is reasonable for SMBus */
1317static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Jean Delvare421ef472005-10-26 21:28:55 +02001319 /* The address will be sent first */
1320 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1321 pec = i2c_smbus_pec(pec, &addr, 1);
1322
1323 /* The data buffer follows */
1324 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Jean Delvare421ef472005-10-26 21:28:55 +02001327/* Used for write only transactions */
1328static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Jean Delvare421ef472005-10-26 21:28:55 +02001330 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1331 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Jean Delvare421ef472005-10-26 21:28:55 +02001334/* Return <0 on CRC error
1335 If there was a write before this read (most cases) we need to take the
1336 partial CRC from the write part into account.
1337 Note that this function does modify the message (we need to decrease the
1338 message length to hide the CRC byte from the caller). */
1339static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Jean Delvare421ef472005-10-26 21:28:55 +02001341 u8 rpec = msg->buf[--msg->len];
1342 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (rpec != cpec) {
1345 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1346 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001347 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
David Brownell438d6c22006-12-10 21:21:31 +01001349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
David Brownella1cdeda2008-07-14 22:38:24 +02001352/**
1353 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1354 * @client: Handle to slave device
1355 *
1356 * This executes the SMBus "receive byte" protocol, returning negative errno
1357 * else the byte received from the device.
1358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359s32 i2c_smbus_read_byte(struct i2c_client *client)
1360{
1361 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001362 int status;
1363
1364 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1365 I2C_SMBUS_READ, 0,
1366 I2C_SMBUS_BYTE, &data);
1367 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
David Brownellc0564602007-05-01 23:26:31 +02001369EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
David Brownella1cdeda2008-07-14 22:38:24 +02001371/**
1372 * i2c_smbus_write_byte - SMBus "send byte" protocol
1373 * @client: Handle to slave device
1374 * @value: Byte to be sent
1375 *
1376 * This executes the SMBus "send byte" protocol, returning negative errno
1377 * else zero on success.
1378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001382 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
David Brownellc0564602007-05-01 23:26:31 +02001384EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
David Brownella1cdeda2008-07-14 22:38:24 +02001386/**
1387 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1388 * @client: Handle to slave device
1389 * @command: Byte interpreted by slave
1390 *
1391 * This executes the SMBus "read byte" protocol, returning negative errno
1392 * else a data byte received from the device.
1393 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1395{
1396 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001397 int status;
1398
1399 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1400 I2C_SMBUS_READ, command,
1401 I2C_SMBUS_BYTE_DATA, &data);
1402 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
David Brownellc0564602007-05-01 23:26:31 +02001404EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
David Brownella1cdeda2008-07-14 22:38:24 +02001406/**
1407 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1408 * @client: Handle to slave device
1409 * @command: Byte interpreted by slave
1410 * @value: Byte being written
1411 *
1412 * This executes the SMBus "write byte" protocol, returning negative errno
1413 * else zero on success.
1414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1416{
1417 union i2c_smbus_data data;
1418 data.byte = value;
1419 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1420 I2C_SMBUS_WRITE,command,
1421 I2C_SMBUS_BYTE_DATA,&data);
1422}
David Brownellc0564602007-05-01 23:26:31 +02001423EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
David Brownella1cdeda2008-07-14 22:38:24 +02001425/**
1426 * i2c_smbus_read_word_data - SMBus "read word" protocol
1427 * @client: Handle to slave device
1428 * @command: Byte interpreted by slave
1429 *
1430 * This executes the SMBus "read word" protocol, returning negative errno
1431 * else a 16-bit unsigned "word" received from the device.
1432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1434{
1435 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001436 int status;
1437
1438 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1439 I2C_SMBUS_READ, command,
1440 I2C_SMBUS_WORD_DATA, &data);
1441 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
David Brownellc0564602007-05-01 23:26:31 +02001443EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
David Brownella1cdeda2008-07-14 22:38:24 +02001445/**
1446 * i2c_smbus_write_word_data - SMBus "write word" protocol
1447 * @client: Handle to slave device
1448 * @command: Byte interpreted by slave
1449 * @value: 16-bit "word" being written
1450 *
1451 * This executes the SMBus "write word" protocol, returning negative errno
1452 * else zero on success.
1453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1455{
1456 union i2c_smbus_data data;
1457 data.word = value;
1458 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1459 I2C_SMBUS_WRITE,command,
1460 I2C_SMBUS_WORD_DATA,&data);
1461}
David Brownellc0564602007-05-01 23:26:31 +02001462EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
David Brownella64ec072007-10-13 23:56:31 +02001464/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001465 * i2c_smbus_process_call - SMBus "process call" protocol
1466 * @client: Handle to slave device
1467 * @command: Byte interpreted by slave
1468 * @value: 16-bit "word" being written
1469 *
1470 * This executes the SMBus "process call" protocol, returning negative errno
1471 * else a 16-bit unsigned "word" received from the device.
1472 */
1473s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1474{
1475 union i2c_smbus_data data;
1476 int status;
1477 data.word = value;
1478
1479 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1480 I2C_SMBUS_WRITE, command,
1481 I2C_SMBUS_PROC_CALL, &data);
1482 return (status < 0) ? status : data.word;
1483}
1484EXPORT_SYMBOL(i2c_smbus_process_call);
1485
1486/**
David Brownella1cdeda2008-07-14 22:38:24 +02001487 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001488 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001489 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001490 * @values: Byte array into which data will be read; big enough to hold
1491 * the data returned by the slave. SMBus allows at most 32 bytes.
1492 *
David Brownella1cdeda2008-07-14 22:38:24 +02001493 * This executes the SMBus "block read" protocol, returning negative errno
1494 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001495 *
1496 * Note that using this function requires that the client's adapter support
1497 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1498 * support this; its emulation through I2C messaging relies on a specific
1499 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1500 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001501s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1502 u8 *values)
1503{
1504 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001505 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001506
David Brownell24a5bb72008-07-14 22:38:23 +02001507 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1508 I2C_SMBUS_READ, command,
1509 I2C_SMBUS_BLOCK_DATA, &data);
1510 if (status)
1511 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001512
1513 memcpy(values, &data.block[1], data.block[0]);
1514 return data.block[0];
1515}
1516EXPORT_SYMBOL(i2c_smbus_read_block_data);
1517
David Brownella1cdeda2008-07-14 22:38:24 +02001518/**
1519 * i2c_smbus_write_block_data - SMBus "block write" protocol
1520 * @client: Handle to slave device
1521 * @command: Byte interpreted by slave
1522 * @length: Size of data block; SMBus allows at most 32 bytes
1523 * @values: Byte array which will be written.
1524 *
1525 * This executes the SMBus "block write" protocol, returning negative errno
1526 * else zero on success.
1527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001529 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (length > I2C_SMBUS_BLOCK_MAX)
1534 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001536 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1538 I2C_SMBUS_WRITE,command,
1539 I2C_SMBUS_BLOCK_DATA,&data);
1540}
David Brownellc0564602007-05-01 23:26:31 +02001541EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001544s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1545 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001548 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001549
Jean Delvare4b2643d2007-07-12 14:12:29 +02001550 if (length > I2C_SMBUS_BLOCK_MAX)
1551 length = I2C_SMBUS_BLOCK_MAX;
1552 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001553 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1554 I2C_SMBUS_READ, command,
1555 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1556 if (status < 0)
1557 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001558
1559 memcpy(values, &data.block[1], data.block[0]);
1560 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
David Brownellc0564602007-05-01 23:26:31 +02001562EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Jean Delvare21bbd692006-01-09 15:19:18 +11001564s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001565 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001566{
1567 union i2c_smbus_data data;
1568
1569 if (length > I2C_SMBUS_BLOCK_MAX)
1570 length = I2C_SMBUS_BLOCK_MAX;
1571 data.block[0] = length;
1572 memcpy(data.block + 1, values, length);
1573 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1574 I2C_SMBUS_WRITE, command,
1575 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1576}
David Brownellc0564602007-05-01 23:26:31 +02001577EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001578
David Brownell438d6c22006-12-10 21:21:31 +01001579/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001581static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001583 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 union i2c_smbus_data * data)
1585{
1586 /* So we need to generate a series of msgs. In the case of writing, we
1587 need to use only one message; when reading, we need two. We initialize
1588 most things with sane defaults, to keep the code below somewhat
1589 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001590 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1591 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001593 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1595 };
1596 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001597 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001598 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 msgbuf0[0] = command;
1601 switch(size) {
1602 case I2C_SMBUS_QUICK:
1603 msg[0].len = 0;
1604 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001605 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1606 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 num = 1;
1608 break;
1609 case I2C_SMBUS_BYTE:
1610 if (read_write == I2C_SMBUS_READ) {
1611 /* Special case: only a read! */
1612 msg[0].flags = I2C_M_RD | flags;
1613 num = 1;
1614 }
1615 break;
1616 case I2C_SMBUS_BYTE_DATA:
1617 if (read_write == I2C_SMBUS_READ)
1618 msg[1].len = 1;
1619 else {
1620 msg[0].len = 2;
1621 msgbuf0[1] = data->byte;
1622 }
1623 break;
1624 case I2C_SMBUS_WORD_DATA:
1625 if (read_write == I2C_SMBUS_READ)
1626 msg[1].len = 2;
1627 else {
1628 msg[0].len=3;
1629 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001630 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 break;
1633 case I2C_SMBUS_PROC_CALL:
1634 num = 2; /* Special case */
1635 read_write = I2C_SMBUS_READ;
1636 msg[0].len = 3;
1637 msg[1].len = 2;
1638 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001639 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001643 msg[1].flags |= I2C_M_RECV_LEN;
1644 msg[1].len = 1; /* block length will be added by
1645 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 } else {
1647 msg[0].len = data->block[0] + 2;
1648 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001649 dev_err(&adapter->dev,
1650 "Invalid block write size %d\n",
1651 data->block[0]);
1652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001654 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 msgbuf0[i] = data->block[i-1];
1656 }
1657 break;
1658 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001659 num = 2; /* Another special case */
1660 read_write = I2C_SMBUS_READ;
1661 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001662 dev_err(&adapter->dev,
1663 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001664 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001665 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001666 }
1667 msg[0].len = data->block[0] + 2;
1668 for (i = 1; i < msg[0].len; i++)
1669 msgbuf0[i] = data->block[i-1];
1670 msg[1].flags |= I2C_M_RECV_LEN;
1671 msg[1].len = 1; /* block length will be added by
1672 the underlying bus driver */
1673 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 case I2C_SMBUS_I2C_BLOCK_DATA:
1675 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001676 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 } else {
1678 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001679 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001680 dev_err(&adapter->dev,
1681 "Invalid block write size %d\n",
1682 data->block[0]);
1683 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685 for (i = 1; i <= data->block[0]; i++)
1686 msgbuf0[i] = data->block[i];
1687 }
1688 break;
1689 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001690 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1691 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693
Jean Delvare421ef472005-10-26 21:28:55 +02001694 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1695 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1696 if (i) {
1697 /* Compute PEC if first message is a write */
1698 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001699 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001700 i2c_smbus_add_pec(&msg[0]);
1701 else /* Write followed by read */
1702 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1703 }
1704 /* Ask for PEC if last message is a read */
1705 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001706 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001707 }
1708
David Brownell24a5bb72008-07-14 22:38:23 +02001709 status = i2c_transfer(adapter, msg, num);
1710 if (status < 0)
1711 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Jean Delvare421ef472005-10-26 21:28:55 +02001713 /* Check PEC if last message is a read */
1714 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001715 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1716 if (status < 0)
1717 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001718 }
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (read_write == I2C_SMBUS_READ)
1721 switch(size) {
1722 case I2C_SMBUS_BYTE:
1723 data->byte = msgbuf0[0];
1724 break;
1725 case I2C_SMBUS_BYTE_DATA:
1726 data->byte = msgbuf1[0];
1727 break;
David Brownell438d6c22006-12-10 21:21:31 +01001728 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 case I2C_SMBUS_PROC_CALL:
1730 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1731 break;
1732 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001733 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 data->block[i+1] = msgbuf1[i];
1735 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001736 case I2C_SMBUS_BLOCK_DATA:
1737 case I2C_SMBUS_BLOCK_PROC_CALL:
1738 for (i = 0; i < msgbuf1[0] + 1; i++)
1739 data->block[i] = msgbuf1[i];
1740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742 return 0;
1743}
1744
David Brownella1cdeda2008-07-14 22:38:24 +02001745/**
1746 * i2c_smbus_xfer - execute SMBus protocol operations
1747 * @adapter: Handle to I2C bus
1748 * @addr: Address of SMBus slave on that bus
1749 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1750 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1751 * @command: Byte interpreted by slave, for protocols which use such bytes
1752 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1753 * @data: Data to be read or written
1754 *
1755 * This executes an SMBus protocol operation, and returns a negative
1756 * errno code else zero on success.
1757 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001758s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001759 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001760 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001762 unsigned long orig_jiffies;
1763 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001769 mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001770
1771 /* Retry automatically on arbitration loss */
1772 orig_jiffies = jiffies;
1773 for (res = 0, try = 0; try <= adapter->retries; try++) {
1774 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1775 read_write, command,
1776 protocol, data);
1777 if (res != -EAGAIN)
1778 break;
1779 if (time_after(jiffies,
1780 orig_jiffies + adapter->timeout))
1781 break;
1782 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001783 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 } else
1785 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001786 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return res;
1789}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1793MODULE_DESCRIPTION("I2C-Bus main module");
1794MODULE_LICENSE("GPL");