blob: 95fb997b41e01a4c3a7639abbdbe57b90ed520fb [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 Rapoportcea443a2008-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 Delvaref8a227e2009-06-19 16:58:18 +020044static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020045static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010046
47/* ------------------------------------------------------------------------- */
48
Jean Delvared2653e92008-04-29 23:11:39 +020049static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
50 const struct i2c_client *client)
51{
52 while (id->name[0]) {
53 if (strcmp(client->name, id->name) == 0)
54 return id;
55 id++;
56 }
57 return NULL;
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int i2c_device_match(struct device *dev, struct device_driver *drv)
61{
David Brownell7b4fbc52007-05-01 23:26:30 +020062 struct i2c_client *client = to_i2c_client(dev);
63 struct i2c_driver *driver = to_i2c_driver(drv);
64
Jean Delvared2653e92008-04-29 23:11:39 +020065 /* match on an id table if there is one */
66 if (driver->id_table)
67 return i2c_match_id(driver->id_table, client) != NULL;
68
Jean Delvareeb8a7902008-05-18 20:49:41 +020069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
David Brownell7b4fbc52007-05-01 23:26:30 +020072#ifdef CONFIG_HOTPLUG
73
74/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020075static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020076{
77 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020078
Jean Delvareeb8a7902008-05-18 20:49:41 +020079 if (add_uevent_var(env, "MODALIAS=%s%s",
80 I2C_MODULE_PREFIX, client->name))
81 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020082 dev_dbg(dev, "uevent\n");
83 return 0;
84}
85
86#else
87#define i2c_device_uevent NULL
88#endif /* CONFIG_HOTPLUG */
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int i2c_device_probe(struct device *dev)
91{
David Brownell7b4fbc52007-05-01 23:26:30 +020092 struct i2c_client *client = to_i2c_client(dev);
93 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +010094 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +020095
Jean Delvaree0457442008-07-14 22:38:30 +020096 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +020097 return -ENODEV;
98 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +020099 if (!device_can_wakeup(&client->dev))
100 device_init_wakeup(&client->dev,
101 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200102 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200103
Jean Delvaree0457442008-07-14 22:38:30 +0200104 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100105 if (status)
106 client->driver = NULL;
107 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110static int i2c_device_remove(struct device *dev)
111{
David Brownella1d9e6e2007-05-01 23:26:30 +0200112 struct i2c_client *client = to_i2c_client(dev);
113 struct i2c_driver *driver;
114 int status;
115
116 if (!dev->driver)
117 return 0;
118
119 driver = to_i2c_driver(dev->driver);
120 if (driver->remove) {
121 dev_dbg(dev, "remove\n");
122 status = driver->remove(client);
123 } else {
124 dev->driver = NULL;
125 status = 0;
126 }
127 if (status == 0)
128 client->driver = NULL;
129 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
David Brownellf37dd802007-02-13 22:09:00 +0100132static void i2c_device_shutdown(struct device *dev)
133{
134 struct i2c_driver *driver;
135
136 if (!dev->driver)
137 return;
138 driver = to_i2c_driver(dev->driver);
139 if (driver->shutdown)
140 driver->shutdown(to_i2c_client(dev));
141}
142
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100143static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100144{
145 struct i2c_driver *driver;
146
147 if (!dev->driver)
148 return 0;
149 driver = to_i2c_driver(dev->driver);
150 if (!driver->suspend)
151 return 0;
152 return driver->suspend(to_i2c_client(dev), mesg);
153}
154
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100155static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100156{
157 struct i2c_driver *driver;
158
159 if (!dev->driver)
160 return 0;
161 driver = to_i2c_driver(dev->driver);
162 if (!driver->resume)
163 return 0;
164 return driver->resume(to_i2c_client(dev));
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
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100172static ssize_t
173show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200174{
175 struct i2c_client *client = to_i2c_client(dev);
176 return sprintf(buf, "%s\n", client->name);
177}
178
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100179static ssize_t
180show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200181{
182 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200183 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200184}
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
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200193struct 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};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200204EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000205
David Brownell9b766b82008-01-27 18:14:51 +0100206
207/**
208 * i2c_verify_client - return parameter as i2c_client, or NULL
209 * @dev: device, probably from some driver model iterator
210 *
211 * When traversing the driver model tree, perhaps using driver model
212 * iterators like @device_for_each_child(), you can't assume very much
213 * about the nodes you find. Use this function to avoid oopses caused
214 * by wrongly treating some non-I2C device as an i2c_client.
215 */
216struct i2c_client *i2c_verify_client(struct device *dev)
217{
218 return (dev->bus == &i2c_bus_type)
219 ? to_i2c_client(dev)
220 : NULL;
221}
222EXPORT_SYMBOL(i2c_verify_client);
223
224
David Brownell9c1600e2007-05-01 23:26:31 +0200225/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200226 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200227 * @adap: the adapter managing the device
228 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200229 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200230 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200231 * Create an i2c device. Binding is handled through driver model
232 * probe()/remove() methods. A driver may be bound to this device when we
233 * return from this function, or any later moment (e.g. maybe hotplugging will
234 * load the driver module). This call is not appropriate for use by mainboard
235 * initialization logic, which usually runs during an arch_initcall() long
236 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200237 *
238 * This returns the new i2c client, which may be saved for later use with
239 * i2c_unregister_device(); or NULL to indicate an error.
240 */
241struct i2c_client *
242i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
243{
244 struct i2c_client *client;
245 int status;
246
247 client = kzalloc(sizeof *client, GFP_KERNEL);
248 if (!client)
249 return NULL;
250
251 client->adapter = adap;
252
253 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200254
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200255 if (info->archdata)
256 client->dev.archdata = *info->archdata;
257
Marc Pignatee354252008-08-28 08:33:22 +0200258 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200259 client->addr = info->addr;
260 client->irq = info->irq;
261
David Brownell9c1600e2007-05-01 23:26:31 +0200262 strlcpy(client->name, info->type, sizeof(client->name));
263
Jean Delvaref8a227e2009-06-19 16:58:18 +0200264 /* Check for address business */
265 status = i2c_check_addr(adap, client->addr);
266 if (status)
267 goto out_err;
268
269 client->dev.parent = &client->adapter->dev;
270 client->dev.bus = &i2c_bus_type;
Jean Delvare1e40ac12009-06-19 16:58:19 +0200271 client->dev.release = i2c_client_dev_release;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200272
273 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
274 client->addr);
275 status = device_register(&client->dev);
276 if (status)
277 goto out_err;
278
279 mutex_lock(&adap->clist_lock);
280 list_add_tail(&client->list, &adap->clients);
281 mutex_unlock(&adap->clist_lock);
282
283 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
284 client->name, dev_name(&client->dev));
285
David Brownell9c1600e2007-05-01 23:26:31 +0200286 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200287
288out_err:
289 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
290 "(%d)\n", client->name, client->addr, status);
291 kfree(client);
292 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200293}
294EXPORT_SYMBOL_GPL(i2c_new_device);
295
296
297/**
298 * i2c_unregister_device - reverse effect of i2c_new_device()
299 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200300 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200301 */
302void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200303{
304 struct i2c_adapter *adapter = client->adapter;
David Brownella1d9e6e2007-05-01 23:26:30 +0200305
306 mutex_lock(&adapter->clist_lock);
307 list_del(&client->list);
308 mutex_unlock(&adapter->clist_lock);
309
310 device_unregister(&client->dev);
311}
David Brownell9c1600e2007-05-01 23:26:31 +0200312EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200313
314
Jean Delvare60b129d2008-05-11 20:37:06 +0200315static const struct i2c_device_id dummy_id[] = {
316 { "dummy", 0 },
317 { },
318};
319
Jean Delvared2653e92008-04-29 23:11:39 +0200320static int dummy_probe(struct i2c_client *client,
321 const struct i2c_device_id *id)
322{
323 return 0;
324}
325
326static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100327{
328 return 0;
329}
330
331static struct i2c_driver dummy_driver = {
332 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200333 .probe = dummy_probe,
334 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200335 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100336};
337
338/**
339 * i2c_new_dummy - return a new i2c device bound to a dummy driver
340 * @adapter: the adapter managing the device
341 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100342 * Context: can sleep
343 *
344 * This returns an I2C client bound to the "dummy" driver, intended for use
345 * with devices that consume multiple addresses. Examples of such chips
346 * include various EEPROMS (like 24c04 and 24c08 models).
347 *
348 * These dummy devices have two main uses. First, most I2C and SMBus calls
349 * except i2c_transfer() need a client handle; the dummy will be that handle.
350 * And second, this prevents the specified address from being bound to a
351 * different driver.
352 *
353 * This returns the new i2c client, which should be saved for later use with
354 * i2c_unregister_device(); or NULL to indicate an error.
355 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100356struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100357{
358 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200359 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100360 };
361
David Brownelle9f13732008-01-27 18:14:52 +0100362 return i2c_new_device(adapter, &info);
363}
364EXPORT_SYMBOL_GPL(i2c_new_dummy);
365
David Brownellf37dd802007-02-13 22:09:00 +0100366/* ------------------------------------------------------------------------- */
367
David Brownell16ffadf2007-05-01 23:26:28 +0200368/* I2C bus adapters -- one roots each I2C or SMBUS segment */
369
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200370static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
David Brownellef2c83212007-05-01 23:26:28 +0200372 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 complete(&adap->dev_released);
374}
375
David Brownell16ffadf2007-05-01 23:26:28 +0200376static ssize_t
377show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
David Brownellef2c83212007-05-01 23:26:28 +0200379 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return sprintf(buf, "%s\n", adap->name);
381}
David Brownell16ffadf2007-05-01 23:26:28 +0200382
383static struct device_attribute i2c_adapter_attrs[] = {
384 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
385 { },
386};
387
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200388static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200389 .owner = THIS_MODULE,
390 .name = "i2c-adapter",
391 .dev_attrs = i2c_adapter_attrs,
392};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David Brownell9c1600e2007-05-01 23:26:31 +0200394static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
395{
396 struct i2c_devinfo *devinfo;
397
398 mutex_lock(&__i2c_board_lock);
399 list_for_each_entry(devinfo, &__i2c_board_list, list) {
400 if (devinfo->busnum == adapter->nr
401 && !i2c_new_device(adapter,
402 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100403 dev_err(&adapter->dev,
404 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200405 devinfo->board_info.addr);
406 }
407 mutex_unlock(&__i2c_board_lock);
408}
409
Jean Delvare026526f2008-01-27 18:14:49 +0100410static int i2c_do_add_adapter(struct device_driver *d, void *data)
411{
412 struct i2c_driver *driver = to_i2c_driver(d);
413 struct i2c_adapter *adap = data;
414
Jean Delvare4735c982008-07-14 22:38:36 +0200415 /* Detect supported devices on that bus, and instantiate them */
416 i2c_detect(adap, driver);
417
418 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100419 if (driver->attach_adapter) {
420 /* We ignore the return code; if it fails, too bad */
421 driver->attach_adapter(adap);
422 }
423 return 0;
424}
425
David Brownell6e13e642007-05-01 23:26:31 +0200426static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Jean Delvare026526f2008-01-27 18:14:49 +0100428 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
David Brownell1d0b19c2008-10-14 17:30:05 +0200430 /* Can't register until after driver model init */
431 if (unlikely(WARN_ON(!i2c_bus_type.p)))
432 return -EAGAIN;
433
Ingo Molnar5c085d32006-01-18 23:16:04 +0100434 mutex_init(&adap->bus_lock);
435 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 INIT_LIST_HEAD(&adap->clients);
437
Jean Delvarecaada322008-01-27 18:14:49 +0100438 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200439
Jean Delvare8fcfef62009-03-28 21:34:43 +0100440 /* Set default timeout to 1 second if not already set */
441 if (adap->timeout == 0)
442 adap->timeout = HZ;
443
Kay Sievers27d9c182009-01-07 14:29:16 +0100444 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200446 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200447 res = device_register(&adap->dev);
448 if (res)
449 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200451 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
452
Jean Delvare729d6dd2009-06-19 16:58:18 +0200453 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200454 if (adap->nr < __i2c_first_dynamic_bus_num)
455 i2c_scan_static_board_info(adap);
456
Jean Delvare4735c982008-07-14 22:38:36 +0200457 /* Notify drivers */
Jean Delvare026526f2008-01-27 18:14:49 +0100458 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
459 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100462 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200464
Jean Delvareb119c6c2006-08-15 18:26:30 +0200465out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200466 idr_remove(&i2c_adapter_idr, adap->nr);
467 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
David Brownell6e13e642007-05-01 23:26:31 +0200470/**
471 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
472 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200473 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200474 *
475 * This routine is used to declare an I2C adapter when its bus number
476 * doesn't matter. Examples: for I2C adapters dynamically added by
477 * USB links or PCI plugin cards.
478 *
479 * When this returns zero, a new bus number was allocated and stored
480 * in adap->nr, and the specified adapter became available for clients.
481 * Otherwise, a negative errno value is returned.
482 */
483int i2c_add_adapter(struct i2c_adapter *adapter)
484{
485 int id, res = 0;
486
487retry:
488 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
489 return -ENOMEM;
490
Jean Delvarecaada322008-01-27 18:14:49 +0100491 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200492 /* "above" here means "above or equal to", sigh */
493 res = idr_get_new_above(&i2c_adapter_idr, adapter,
494 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100495 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200496
497 if (res < 0) {
498 if (res == -EAGAIN)
499 goto retry;
500 return res;
501 }
502
503 adapter->nr = id;
504 return i2c_register_adapter(adapter);
505}
506EXPORT_SYMBOL(i2c_add_adapter);
507
508/**
509 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
510 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200511 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200512 *
513 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100514 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
515 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200516 * is used to properly configure I2C devices.
517 *
518 * If no devices have pre-been declared for this bus, then be sure to
519 * register the adapter before any dynamically allocated ones. Otherwise
520 * the required bus ID may not be available.
521 *
522 * When this returns zero, the specified adapter became available for
523 * clients using the bus number provided in adap->nr. Also, the table
524 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
525 * and the appropriate driver model device nodes are created. Otherwise, a
526 * negative errno value is returned.
527 */
528int i2c_add_numbered_adapter(struct i2c_adapter *adap)
529{
530 int id;
531 int status;
532
533 if (adap->nr & ~MAX_ID_MASK)
534 return -EINVAL;
535
536retry:
537 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
538 return -ENOMEM;
539
Jean Delvarecaada322008-01-27 18:14:49 +0100540 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200541 /* "above" here means "above or equal to", sigh;
542 * we need the "equal to" result to force the result
543 */
544 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
545 if (status == 0 && id != adap->nr) {
546 status = -EBUSY;
547 idr_remove(&i2c_adapter_idr, id);
548 }
Jean Delvarecaada322008-01-27 18:14:49 +0100549 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200550 if (status == -EAGAIN)
551 goto retry;
552
553 if (status == 0)
554 status = i2c_register_adapter(adap);
555 return status;
556}
557EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
558
Jean Delvare026526f2008-01-27 18:14:49 +0100559static int i2c_do_del_adapter(struct device_driver *d, void *data)
560{
561 struct i2c_driver *driver = to_i2c_driver(d);
562 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200563 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100564 int res;
565
Jean Delvareacec2112009-03-28 21:34:40 +0100566 /* Remove the devices we created ourselves as the result of hardware
567 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200568 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
569 if (client->adapter == adapter) {
570 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
571 client->name, client->addr);
572 list_del(&client->detected);
573 i2c_unregister_device(client);
574 }
575 }
576
Jean Delvare026526f2008-01-27 18:14:49 +0100577 if (!driver->detach_adapter)
578 return 0;
579 res = driver->detach_adapter(adapter);
580 if (res)
581 dev_err(&adapter->dev, "detach_adapter failed (%d) "
582 "for driver [%s]\n", res, driver->driver.name);
583 return res;
584}
585
David Brownelld64f73b2007-07-12 14:12:28 +0200586/**
587 * i2c_del_adapter - unregister I2C adapter
588 * @adap: the adapter being unregistered
589 * Context: can sleep
590 *
591 * This unregisters an I2C adapter which was previously registered
592 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594int i2c_del_adapter(struct i2c_adapter *adap)
595{
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200596 struct i2c_client *client, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int res = 0;
598
Jean Delvarecaada322008-01-27 18:14:49 +0100599 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100602 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200603 pr_debug("i2c-core: attempting to delete unregistered "
604 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 res = -EINVAL;
606 goto out_unlock;
607 }
608
Jean Delvare026526f2008-01-27 18:14:49 +0100609 /* Tell drivers about this removal */
610 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
611 i2c_do_del_adapter);
612 if (res)
613 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Jean Delvare729d6dd2009-06-19 16:58:18 +0200615 /* Detach any active clients */
Jean Delvare79b93e12008-11-28 15:24:38 +0100616 list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
Jean Delvare729d6dd2009-06-19 16:58:18 +0200617 i2c_unregister_device(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 /* clean up the sysfs representation */
621 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* wait for sysfs to drop all references */
625 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
David Brownell6e13e642007-05-01 23:26:31 +0200627 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 idr_remove(&i2c_adapter_idr, adap->nr);
629
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200630 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200632 /* Clear the device structure in case this adapter is ever going to be
633 added again */
634 memset(&adap->dev, 0, sizeof(adap->dev));
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100637 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return res;
639}
David Brownellc0564602007-05-01 23:26:31 +0200640EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642
David Brownell7b4fbc52007-05-01 23:26:30 +0200643/* ------------------------------------------------------------------------- */
644
Dave Young7f101a92008-07-14 22:38:19 +0200645static int __attach_adapter(struct device *dev, void *data)
646{
647 struct i2c_adapter *adapter = to_i2c_adapter(dev);
648 struct i2c_driver *driver = data;
649
Jean Delvare4735c982008-07-14 22:38:36 +0200650 i2c_detect(adapter, driver);
651
652 /* Legacy drivers scan i2c busses directly */
653 if (driver->attach_adapter)
654 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200655
656 return 0;
657}
658
David Brownell7b4fbc52007-05-01 23:26:30 +0200659/*
660 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200661 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
663
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800664int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100666 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
David Brownell1d0b19c2008-10-14 17:30:05 +0200668 /* Can't register until after driver model init */
669 if (unlikely(WARN_ON(!i2c_bus_type.p)))
670 return -EAGAIN;
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800673 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Jean Delvare729d6dd2009-06-19 16:58:18 +0200676 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200677 * will have called probe() for all matching-but-unbound devices.
678 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 res = driver_register(&driver->driver);
680 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100681 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100682
Jean Delvarecaada322008-01-27 18:14:49 +0100683 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100684
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100685 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jean Delvare4735c982008-07-14 22:38:36 +0200687 INIT_LIST_HEAD(&driver->clients);
688 /* Walk the adapters that are already present */
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400689 class_for_each_device(&i2c_adapter_class, NULL, driver,
690 __attach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Jean Delvarecaada322008-01-27 18:14:49 +0100692 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800695EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dave Young7f101a92008-07-14 22:38:19 +0200697static int __detach_adapter(struct device *dev, void *data)
698{
699 struct i2c_adapter *adapter = to_i2c_adapter(dev);
700 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200701 struct i2c_client *client, *_n;
702
Jean Delvareacec2112009-03-28 21:34:40 +0100703 /* Remove the devices we created ourselves as the result of hardware
704 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200705 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
706 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
707 client->name, client->addr);
708 list_del(&client->detected);
709 i2c_unregister_device(client);
710 }
711
Dave Young7f101a92008-07-14 22:38:19 +0200712 if (driver->detach_adapter) {
713 if (driver->detach_adapter(adapter))
714 dev_err(&adapter->dev,
715 "detach_adapter failed for driver [%s]\n",
716 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200717 }
718
719 return 0;
720}
721
David Brownella1d9e6e2007-05-01 23:26:30 +0200722/**
723 * i2c_del_driver - unregister I2C driver
724 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200725 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200726 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200727void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Jean Delvarecaada322008-01-27 18:14:49 +0100729 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400731 class_for_each_device(&i2c_adapter_class, NULL, driver,
732 __detach_adapter);
David Brownella1d9e6e2007-05-01 23:26:30 +0200733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100735 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jean Delvarecaada322008-01-27 18:14:49 +0100737 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
David Brownellc0564602007-05-01 23:26:31 +0200739EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
David Brownell7b4fbc52007-05-01 23:26:30 +0200741/* ------------------------------------------------------------------------- */
742
David Brownell9b766b82008-01-27 18:14:51 +0100743static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
David Brownell9b766b82008-01-27 18:14:51 +0100745 struct i2c_client *client = i2c_verify_client(dev);
746 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
David Brownell9b766b82008-01-27 18:14:51 +0100748 if (client && client->addr == addr)
749 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751}
752
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100753static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
David Brownell9b766b82008-01-27 18:14:51 +0100755 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Jean Delvaree48d3312008-01-27 18:14:48 +0100758/**
759 * i2c_use_client - increments the reference count of the i2c client structure
760 * @client: the client being referenced
761 *
762 * Each live reference to a client should be refcounted. The driver model does
763 * that automatically as part of driver binding, so that most drivers don't
764 * need to do this explicitly: they hold a reference until they're unbound
765 * from the device.
766 *
767 * A pointer to the client with the incremented reference counter is returned.
768 */
769struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
David Brownell6ea438e2008-07-14 22:38:24 +0200771 if (client && get_device(&client->dev))
772 return client;
773 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
David Brownellc0564602007-05-01 23:26:31 +0200775EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jean Delvaree48d3312008-01-27 18:14:48 +0100777/**
778 * i2c_release_client - release a use of the i2c client structure
779 * @client: the client being no longer referenced
780 *
781 * Must be called when a user of a client is finished with it.
782 */
783void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David Brownell6ea438e2008-07-14 22:38:24 +0200785 if (client)
786 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
David Brownellc0564602007-05-01 23:26:31 +0200788EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
David Brownell9b766b82008-01-27 18:14:51 +0100790struct i2c_cmd_arg {
791 unsigned cmd;
792 void *arg;
793};
794
795static int i2c_cmd(struct device *dev, void *_arg)
796{
797 struct i2c_client *client = i2c_verify_client(dev);
798 struct i2c_cmd_arg *arg = _arg;
799
800 if (client && client->driver && client->driver->command)
801 client->driver->command(client, arg->cmd, arg->arg);
802 return 0;
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
806{
David Brownell9b766b82008-01-27 18:14:51 +0100807 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
David Brownell9b766b82008-01-27 18:14:51 +0100809 cmd_arg.cmd = cmd;
810 cmd_arg.arg = arg;
811 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
David Brownellc0564602007-05-01 23:26:31 +0200813EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815static int __init i2c_init(void)
816{
817 int retval;
818
819 retval = bus_register(&i2c_bus_type);
820 if (retval)
821 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100822 retval = class_register(&i2c_adapter_class);
823 if (retval)
824 goto bus_err;
825 retval = i2c_add_driver(&dummy_driver);
826 if (retval)
827 goto class_err;
828 return 0;
829
830class_err:
831 class_unregister(&i2c_adapter_class);
832bus_err:
833 bus_unregister(&i2c_bus_type);
834 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837static void __exit i2c_exit(void)
838{
David Brownelle9f13732008-01-27 18:14:52 +0100839 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 bus_unregister(&i2c_bus_type);
842}
843
David Brownella10f9e72008-10-14 17:30:06 +0200844/* We must initialize early, because some subsystems register i2c drivers
845 * in subsys_initcall() code, but are linked (and initialized) before i2c.
846 */
847postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848module_exit(i2c_exit);
849
850/* ----------------------------------------------------
851 * the functional interface to the i2c busses.
852 * ----------------------------------------------------
853 */
854
David Brownella1cdeda2008-07-14 22:38:24 +0200855/**
856 * i2c_transfer - execute a single or combined I2C message
857 * @adap: Handle to I2C bus
858 * @msgs: One or more messages to execute before STOP is issued to
859 * terminate the operation; each message begins with a START.
860 * @num: Number of messages to be executed.
861 *
862 * Returns negative errno, else the number of messages executed.
863 *
864 * Note that there is no requirement that each message be sent to
865 * the same slave address, although that is the most common model.
866 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100867int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Clifford Wolf66b650f2009-06-15 18:01:46 +0200869 unsigned long orig_jiffies;
870 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Brownella1cdeda2008-07-14 22:38:24 +0200872 /* REVISIT the fault reporting model here is weak:
873 *
874 * - When we get an error after receiving N bytes from a slave,
875 * there is no way to report "N".
876 *
877 * - When we get a NAK after transmitting N bytes to a slave,
878 * there is no way to report "N" ... or to let the master
879 * continue executing the rest of this combined message, if
880 * that's the appropriate response.
881 *
882 * - When for example "num" is two and we successfully complete
883 * the first message but get an error part way through the
884 * second, it's unclear whether that should be reported as
885 * one (discarding status on the second message) or errno
886 * (discarding status on the first one).
887 */
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (adap->algo->master_xfer) {
890#ifdef DEBUG
891 for (ret = 0; ret < num; ret++) {
892 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200893 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
894 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
895 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897#endif
898
Mike Rapoportcea443a2008-01-27 18:14:50 +0100899 if (in_atomic() || irqs_disabled()) {
900 ret = mutex_trylock(&adap->bus_lock);
901 if (!ret)
902 /* I2C activity is ongoing. */
903 return -EAGAIN;
904 } else {
905 mutex_lock_nested(&adap->bus_lock, adap->level);
906 }
907
Clifford Wolf66b650f2009-06-15 18:01:46 +0200908 /* Retry automatically on arbitration loss */
909 orig_jiffies = jiffies;
910 for (ret = 0, try = 0; try <= adap->retries; try++) {
911 ret = adap->algo->master_xfer(adap, msgs, num);
912 if (ret != -EAGAIN)
913 break;
914 if (time_after(jiffies, orig_jiffies + adap->timeout))
915 break;
916 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100917 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 return ret;
920 } else {
921 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +0200922 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924}
David Brownellc0564602007-05-01 23:26:31 +0200925EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
David Brownella1cdeda2008-07-14 22:38:24 +0200927/**
928 * i2c_master_send - issue a single I2C message in master transmit mode
929 * @client: Handle to slave device
930 * @buf: Data that will be written to the slave
931 * @count: How many bytes to write
932 *
933 * Returns negative errno, or else the number of bytes written.
934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
936{
937 int ret;
938 struct i2c_adapter *adap=client->adapter;
939 struct i2c_msg msg;
940
Jean Delvare815f55f2005-05-07 22:58:46 +0200941 msg.addr = client->addr;
942 msg.flags = client->flags & I2C_M_TEN;
943 msg.len = count;
944 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100945
Jean Delvare815f55f2005-05-07 22:58:46 +0200946 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Jean Delvare815f55f2005-05-07 22:58:46 +0200948 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
949 transmitted, else error code. */
950 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
David Brownellc0564602007-05-01 23:26:31 +0200952EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
David Brownella1cdeda2008-07-14 22:38:24 +0200954/**
955 * i2c_master_recv - issue a single I2C message in master receive mode
956 * @client: Handle to slave device
957 * @buf: Where to store data read from slave
958 * @count: How many bytes to read
959 *
960 * Returns negative errno, or else the number of bytes read.
961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
963{
964 struct i2c_adapter *adap=client->adapter;
965 struct i2c_msg msg;
966 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Jean Delvare815f55f2005-05-07 22:58:46 +0200968 msg.addr = client->addr;
969 msg.flags = client->flags & I2C_M_TEN;
970 msg.flags |= I2C_M_RD;
971 msg.len = count;
972 msg.buf = buf;
973
974 ret = i2c_transfer(adap, &msg, 1);
975
976 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
977 transmitted, else error code. */
978 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
David Brownellc0564602007-05-01 23:26:31 +0200980EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/* ----------------------------------------------------
983 * the i2c address scanning function
984 * Will not work for 10-bit addresses!
985 * ----------------------------------------------------
986 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200987
Jean Delvare4735c982008-07-14 22:38:36 +0200988static int i2c_detect_address(struct i2c_client *temp_client, int kind,
989 struct i2c_driver *driver)
990{
991 struct i2c_board_info info;
992 struct i2c_adapter *adapter = temp_client->adapter;
993 int addr = temp_client->addr;
994 int err;
995
996 /* Make sure the address is valid */
997 if (addr < 0x03 || addr > 0x77) {
998 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
999 addr);
1000 return -EINVAL;
1001 }
1002
1003 /* Skip if already in use */
1004 if (i2c_check_addr(adapter, addr))
1005 return 0;
1006
1007 /* Make sure there is something at this address, unless forced */
1008 if (kind < 0) {
1009 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1010 I2C_SMBUS_QUICK, NULL) < 0)
1011 return 0;
1012
1013 /* prevent 24RF08 corruption */
1014 if ((addr & ~0x0f) == 0x50)
1015 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1016 I2C_SMBUS_QUICK, NULL);
1017 }
1018
1019 /* Finally call the custom detection function */
1020 memset(&info, 0, sizeof(struct i2c_board_info));
1021 info.addr = addr;
1022 err = driver->detect(temp_client, kind, &info);
1023 if (err) {
1024 /* -ENODEV is returned if the detection fails. We catch it
1025 here as this isn't an error. */
1026 return err == -ENODEV ? 0 : err;
1027 }
1028
1029 /* Consistency check */
1030 if (info.type[0] == '\0') {
1031 dev_err(&adapter->dev, "%s detection function provided "
1032 "no name for 0x%x\n", driver->driver.name,
1033 addr);
1034 } else {
1035 struct i2c_client *client;
1036
1037 /* Detection succeeded, instantiate the device */
1038 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1039 info.type, info.addr);
1040 client = i2c_new_device(adapter, &info);
1041 if (client)
1042 list_add_tail(&client->detected, &driver->clients);
1043 else
1044 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1045 info.type, info.addr);
1046 }
1047 return 0;
1048}
1049
1050static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1051{
1052 const struct i2c_client_address_data *address_data;
1053 struct i2c_client *temp_client;
1054 int i, err = 0;
1055 int adap_id = i2c_adapter_id(adapter);
1056
1057 address_data = driver->address_data;
1058 if (!driver->detect || !address_data)
1059 return 0;
1060
1061 /* Set up a temporary client to help detect callback */
1062 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1063 if (!temp_client)
1064 return -ENOMEM;
1065 temp_client->adapter = adapter;
1066
1067 /* Force entries are done first, and are not affected by ignore
1068 entries */
1069 if (address_data->forces) {
1070 const unsigned short * const *forces = address_data->forces;
1071 int kind;
1072
1073 for (kind = 0; forces[kind]; kind++) {
1074 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1075 i += 2) {
1076 if (forces[kind][i] == adap_id
1077 || forces[kind][i] == ANY_I2C_BUS) {
1078 dev_dbg(&adapter->dev, "found force "
1079 "parameter for adapter %d, "
1080 "addr 0x%02x, kind %d\n",
1081 adap_id, forces[kind][i + 1],
1082 kind);
1083 temp_client->addr = forces[kind][i + 1];
1084 err = i2c_detect_address(temp_client,
1085 kind, driver);
1086 if (err)
1087 goto exit_free;
1088 }
1089 }
1090 }
1091 }
1092
Jean Delvare4329cf82008-08-28 08:33:23 +02001093 /* Stop here if the classes do not match */
1094 if (!(adapter->class & driver->class))
1095 goto exit_free;
1096
Jean Delvare4735c982008-07-14 22:38:36 +02001097 /* Stop here if we can't use SMBUS_QUICK */
1098 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1099 if (address_data->probe[0] == I2C_CLIENT_END
1100 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1101 goto exit_free;
1102
1103 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1104 "can't probe for chips\n");
1105 err = -EOPNOTSUPP;
1106 goto exit_free;
1107 }
1108
Jean Delvare4735c982008-07-14 22:38:36 +02001109 /* Probe entries are done second, and are not affected by ignore
1110 entries either */
1111 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1112 if (address_data->probe[i] == adap_id
1113 || address_data->probe[i] == ANY_I2C_BUS) {
1114 dev_dbg(&adapter->dev, "found probe parameter for "
1115 "adapter %d, addr 0x%02x\n", adap_id,
1116 address_data->probe[i + 1]);
1117 temp_client->addr = address_data->probe[i + 1];
1118 err = i2c_detect_address(temp_client, -1, driver);
1119 if (err)
1120 goto exit_free;
1121 }
1122 }
1123
1124 /* Normal entries are done last, unless shadowed by an ignore entry */
1125 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1126 int j, ignore;
1127
1128 ignore = 0;
1129 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1130 j += 2) {
1131 if ((address_data->ignore[j] == adap_id ||
1132 address_data->ignore[j] == ANY_I2C_BUS)
1133 && address_data->ignore[j + 1]
1134 == address_data->normal_i2c[i]) {
1135 dev_dbg(&adapter->dev, "found ignore "
1136 "parameter for adapter %d, "
1137 "addr 0x%02x\n", adap_id,
1138 address_data->ignore[j + 1]);
1139 ignore = 1;
1140 break;
1141 }
1142 }
1143 if (ignore)
1144 continue;
1145
1146 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1147 "addr 0x%02x\n", adap_id,
1148 address_data->normal_i2c[i]);
1149 temp_client->addr = address_data->normal_i2c[i];
1150 err = i2c_detect_address(temp_client, -1, driver);
1151 if (err)
1152 goto exit_free;
1153 }
1154
1155 exit_free:
1156 kfree(temp_client);
1157 return err;
1158}
1159
Jean Delvare12b5053a2007-05-01 23:26:31 +02001160struct i2c_client *
1161i2c_new_probed_device(struct i2c_adapter *adap,
1162 struct i2c_board_info *info,
1163 unsigned short const *addr_list)
1164{
1165 int i;
1166
1167 /* Stop here if the bus doesn't support probing */
1168 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1169 dev_err(&adap->dev, "Probing not supported\n");
1170 return NULL;
1171 }
1172
Jean Delvare12b5053a2007-05-01 23:26:31 +02001173 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1174 /* Check address validity */
1175 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1176 dev_warn(&adap->dev, "Invalid 7-bit address "
1177 "0x%02x\n", addr_list[i]);
1178 continue;
1179 }
1180
1181 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001182 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001183 dev_dbg(&adap->dev, "Address 0x%02x already in "
1184 "use, not probing\n", addr_list[i]);
1185 continue;
1186 }
1187
1188 /* Test address responsiveness
1189 The default probe method is a quick write, but it is known
1190 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1191 and could also irreversibly write-protect some EEPROMs, so
1192 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1193 read instead. Also, some bus drivers don't implement
1194 quick write, so we fallback to a byte read it that case
1195 too. */
1196 if ((addr_list[i] & ~0x07) == 0x30
1197 || (addr_list[i] & ~0x0f) == 0x50
1198 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001199 union i2c_smbus_data data;
1200
Jean Delvare12b5053a2007-05-01 23:26:31 +02001201 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1202 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001203 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001204 break;
1205 } else {
1206 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1207 I2C_SMBUS_WRITE, 0,
1208 I2C_SMBUS_QUICK, NULL) >= 0)
1209 break;
1210 }
1211 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001212
1213 if (addr_list[i] == I2C_CLIENT_END) {
1214 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1215 return NULL;
1216 }
1217
1218 info->addr = addr_list[i];
1219 return i2c_new_device(adap, info);
1220}
1221EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223struct i2c_adapter* i2c_get_adapter(int id)
1224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001226
Jean Delvarecaada322008-01-27 18:14:49 +01001227 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001228 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001229 if (adapter && !try_module_get(adapter->owner))
1230 adapter = NULL;
1231
Jean Delvarecaada322008-01-27 18:14:49 +01001232 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001233 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
David Brownellc0564602007-05-01 23:26:31 +02001235EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237void i2c_put_adapter(struct i2c_adapter *adap)
1238{
1239 module_put(adap->owner);
1240}
David Brownellc0564602007-05-01 23:26:31 +02001241EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243/* The SMBus parts */
1244
David Brownell438d6c22006-12-10 21:21:31 +01001245#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001246static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001251 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 data = data ^ POLY;
1253 data = data << 1;
1254 }
1255 return (u8)(data >> 8);
1256}
1257
Jean Delvare421ef472005-10-26 21:28:55 +02001258/* Incremental CRC8 over count bytes in the array pointed to by p */
1259static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 int i;
1262
1263 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001264 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return crc;
1266}
1267
Jean Delvare421ef472005-10-26 21:28:55 +02001268/* Assume a 7-bit address, which is reasonable for SMBus */
1269static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Jean Delvare421ef472005-10-26 21:28:55 +02001271 /* The address will be sent first */
1272 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1273 pec = i2c_smbus_pec(pec, &addr, 1);
1274
1275 /* The data buffer follows */
1276 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Jean Delvare421ef472005-10-26 21:28:55 +02001279/* Used for write only transactions */
1280static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Jean Delvare421ef472005-10-26 21:28:55 +02001282 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1283 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Jean Delvare421ef472005-10-26 21:28:55 +02001286/* Return <0 on CRC error
1287 If there was a write before this read (most cases) we need to take the
1288 partial CRC from the write part into account.
1289 Note that this function does modify the message (we need to decrease the
1290 message length to hide the CRC byte from the caller). */
1291static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Jean Delvare421ef472005-10-26 21:28:55 +02001293 u8 rpec = msg->buf[--msg->len];
1294 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (rpec != cpec) {
1297 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1298 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001299 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
David Brownell438d6c22006-12-10 21:21:31 +01001301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
David Brownella1cdeda2008-07-14 22:38:24 +02001304/**
1305 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1306 * @client: Handle to slave device
1307 *
1308 * This executes the SMBus "receive byte" protocol, returning negative errno
1309 * else the byte received from the device.
1310 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311s32 i2c_smbus_read_byte(struct i2c_client *client)
1312{
1313 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001314 int status;
1315
1316 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1317 I2C_SMBUS_READ, 0,
1318 I2C_SMBUS_BYTE, &data);
1319 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
David Brownellc0564602007-05-01 23:26:31 +02001321EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
David Brownella1cdeda2008-07-14 22:38:24 +02001323/**
1324 * i2c_smbus_write_byte - SMBus "send byte" protocol
1325 * @client: Handle to slave device
1326 * @value: Byte to be sent
1327 *
1328 * This executes the SMBus "send byte" protocol, returning negative errno
1329 * else zero on success.
1330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001334 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
David Brownellc0564602007-05-01 23:26:31 +02001336EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
David Brownella1cdeda2008-07-14 22:38:24 +02001338/**
1339 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1340 * @client: Handle to slave device
1341 * @command: Byte interpreted by slave
1342 *
1343 * This executes the SMBus "read byte" protocol, returning negative errno
1344 * else a data byte received from the device.
1345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1347{
1348 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001349 int status;
1350
1351 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1352 I2C_SMBUS_READ, command,
1353 I2C_SMBUS_BYTE_DATA, &data);
1354 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
David Brownellc0564602007-05-01 23:26:31 +02001356EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
David Brownella1cdeda2008-07-14 22:38:24 +02001358/**
1359 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1360 * @client: Handle to slave device
1361 * @command: Byte interpreted by slave
1362 * @value: Byte being written
1363 *
1364 * This executes the SMBus "write byte" protocol, returning negative errno
1365 * else zero on success.
1366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1368{
1369 union i2c_smbus_data data;
1370 data.byte = value;
1371 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1372 I2C_SMBUS_WRITE,command,
1373 I2C_SMBUS_BYTE_DATA,&data);
1374}
David Brownellc0564602007-05-01 23:26:31 +02001375EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
David Brownella1cdeda2008-07-14 22:38:24 +02001377/**
1378 * i2c_smbus_read_word_data - SMBus "read word" protocol
1379 * @client: Handle to slave device
1380 * @command: Byte interpreted by slave
1381 *
1382 * This executes the SMBus "read word" protocol, returning negative errno
1383 * else a 16-bit unsigned "word" received from the device.
1384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1386{
1387 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001388 int status;
1389
1390 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1391 I2C_SMBUS_READ, command,
1392 I2C_SMBUS_WORD_DATA, &data);
1393 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
David Brownellc0564602007-05-01 23:26:31 +02001395EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
David Brownella1cdeda2008-07-14 22:38:24 +02001397/**
1398 * i2c_smbus_write_word_data - SMBus "write word" protocol
1399 * @client: Handle to slave device
1400 * @command: Byte interpreted by slave
1401 * @value: 16-bit "word" being written
1402 *
1403 * This executes the SMBus "write word" protocol, returning negative errno
1404 * else zero on success.
1405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1407{
1408 union i2c_smbus_data data;
1409 data.word = value;
1410 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1411 I2C_SMBUS_WRITE,command,
1412 I2C_SMBUS_WORD_DATA,&data);
1413}
David Brownellc0564602007-05-01 23:26:31 +02001414EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
David Brownella64ec072007-10-13 23:56:31 +02001416/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001417 * i2c_smbus_process_call - SMBus "process call" protocol
1418 * @client: Handle to slave device
1419 * @command: Byte interpreted by slave
1420 * @value: 16-bit "word" being written
1421 *
1422 * This executes the SMBus "process call" protocol, returning negative errno
1423 * else a 16-bit unsigned "word" received from the device.
1424 */
1425s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1426{
1427 union i2c_smbus_data data;
1428 int status;
1429 data.word = value;
1430
1431 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1432 I2C_SMBUS_WRITE, command,
1433 I2C_SMBUS_PROC_CALL, &data);
1434 return (status < 0) ? status : data.word;
1435}
1436EXPORT_SYMBOL(i2c_smbus_process_call);
1437
1438/**
David Brownella1cdeda2008-07-14 22:38:24 +02001439 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001440 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001441 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001442 * @values: Byte array into which data will be read; big enough to hold
1443 * the data returned by the slave. SMBus allows at most 32 bytes.
1444 *
David Brownella1cdeda2008-07-14 22:38:24 +02001445 * This executes the SMBus "block read" protocol, returning negative errno
1446 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001447 *
1448 * Note that using this function requires that the client's adapter support
1449 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1450 * support this; its emulation through I2C messaging relies on a specific
1451 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1452 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001453s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1454 u8 *values)
1455{
1456 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001457 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001458
David Brownell24a5bb72008-07-14 22:38:23 +02001459 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1460 I2C_SMBUS_READ, command,
1461 I2C_SMBUS_BLOCK_DATA, &data);
1462 if (status)
1463 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001464
1465 memcpy(values, &data.block[1], data.block[0]);
1466 return data.block[0];
1467}
1468EXPORT_SYMBOL(i2c_smbus_read_block_data);
1469
David Brownella1cdeda2008-07-14 22:38:24 +02001470/**
1471 * i2c_smbus_write_block_data - SMBus "block write" protocol
1472 * @client: Handle to slave device
1473 * @command: Byte interpreted by slave
1474 * @length: Size of data block; SMBus allows at most 32 bytes
1475 * @values: Byte array which will be written.
1476 *
1477 * This executes the SMBus "block write" protocol, returning negative errno
1478 * else zero on success.
1479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001481 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (length > I2C_SMBUS_BLOCK_MAX)
1486 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001488 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1490 I2C_SMBUS_WRITE,command,
1491 I2C_SMBUS_BLOCK_DATA,&data);
1492}
David Brownellc0564602007-05-01 23:26:31 +02001493EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001496s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1497 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001500 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001501
Jean Delvare4b2643d2007-07-12 14:12:29 +02001502 if (length > I2C_SMBUS_BLOCK_MAX)
1503 length = I2C_SMBUS_BLOCK_MAX;
1504 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001505 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1506 I2C_SMBUS_READ, command,
1507 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1508 if (status < 0)
1509 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001510
1511 memcpy(values, &data.block[1], data.block[0]);
1512 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
David Brownellc0564602007-05-01 23:26:31 +02001514EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Jean Delvare21bbd692006-01-09 15:19:18 +11001516s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001517 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001518{
1519 union i2c_smbus_data data;
1520
1521 if (length > I2C_SMBUS_BLOCK_MAX)
1522 length = I2C_SMBUS_BLOCK_MAX;
1523 data.block[0] = length;
1524 memcpy(data.block + 1, values, length);
1525 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1526 I2C_SMBUS_WRITE, command,
1527 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1528}
David Brownellc0564602007-05-01 23:26:31 +02001529EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001530
David Brownell438d6c22006-12-10 21:21:31 +01001531/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001533static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001535 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 union i2c_smbus_data * data)
1537{
1538 /* So we need to generate a series of msgs. In the case of writing, we
1539 need to use only one message; when reading, we need two. We initialize
1540 most things with sane defaults, to keep the code below somewhat
1541 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001542 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1543 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001545 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1547 };
1548 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001549 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001550 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 msgbuf0[0] = command;
1553 switch(size) {
1554 case I2C_SMBUS_QUICK:
1555 msg[0].len = 0;
1556 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001557 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1558 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 num = 1;
1560 break;
1561 case I2C_SMBUS_BYTE:
1562 if (read_write == I2C_SMBUS_READ) {
1563 /* Special case: only a read! */
1564 msg[0].flags = I2C_M_RD | flags;
1565 num = 1;
1566 }
1567 break;
1568 case I2C_SMBUS_BYTE_DATA:
1569 if (read_write == I2C_SMBUS_READ)
1570 msg[1].len = 1;
1571 else {
1572 msg[0].len = 2;
1573 msgbuf0[1] = data->byte;
1574 }
1575 break;
1576 case I2C_SMBUS_WORD_DATA:
1577 if (read_write == I2C_SMBUS_READ)
1578 msg[1].len = 2;
1579 else {
1580 msg[0].len=3;
1581 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001582 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584 break;
1585 case I2C_SMBUS_PROC_CALL:
1586 num = 2; /* Special case */
1587 read_write = I2C_SMBUS_READ;
1588 msg[0].len = 3;
1589 msg[1].len = 2;
1590 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001591 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 break;
1593 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001595 msg[1].flags |= I2C_M_RECV_LEN;
1596 msg[1].len = 1; /* block length will be added by
1597 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 } else {
1599 msg[0].len = data->block[0] + 2;
1600 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001601 dev_err(&adapter->dev,
1602 "Invalid block write size %d\n",
1603 data->block[0]);
1604 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001606 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 msgbuf0[i] = data->block[i-1];
1608 }
1609 break;
1610 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001611 num = 2; /* Another special case */
1612 read_write = I2C_SMBUS_READ;
1613 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001614 dev_err(&adapter->dev,
1615 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001616 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001617 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001618 }
1619 msg[0].len = data->block[0] + 2;
1620 for (i = 1; i < msg[0].len; i++)
1621 msgbuf0[i] = data->block[i-1];
1622 msg[1].flags |= I2C_M_RECV_LEN;
1623 msg[1].len = 1; /* block length will be added by
1624 the underlying bus driver */
1625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 case I2C_SMBUS_I2C_BLOCK_DATA:
1627 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001628 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 } else {
1630 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001631 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001632 dev_err(&adapter->dev,
1633 "Invalid block write size %d\n",
1634 data->block[0]);
1635 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 for (i = 1; i <= data->block[0]; i++)
1638 msgbuf0[i] = data->block[i];
1639 }
1640 break;
1641 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001642 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1643 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645
Jean Delvare421ef472005-10-26 21:28:55 +02001646 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1647 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1648 if (i) {
1649 /* Compute PEC if first message is a write */
1650 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001651 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001652 i2c_smbus_add_pec(&msg[0]);
1653 else /* Write followed by read */
1654 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1655 }
1656 /* Ask for PEC if last message is a read */
1657 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001658 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001659 }
1660
David Brownell24a5bb72008-07-14 22:38:23 +02001661 status = i2c_transfer(adapter, msg, num);
1662 if (status < 0)
1663 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Jean Delvare421ef472005-10-26 21:28:55 +02001665 /* Check PEC if last message is a read */
1666 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001667 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1668 if (status < 0)
1669 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001670 }
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (read_write == I2C_SMBUS_READ)
1673 switch(size) {
1674 case I2C_SMBUS_BYTE:
1675 data->byte = msgbuf0[0];
1676 break;
1677 case I2C_SMBUS_BYTE_DATA:
1678 data->byte = msgbuf1[0];
1679 break;
David Brownell438d6c22006-12-10 21:21:31 +01001680 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 case I2C_SMBUS_PROC_CALL:
1682 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1683 break;
1684 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001685 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 data->block[i+1] = msgbuf1[i];
1687 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001688 case I2C_SMBUS_BLOCK_DATA:
1689 case I2C_SMBUS_BLOCK_PROC_CALL:
1690 for (i = 0; i < msgbuf1[0] + 1; i++)
1691 data->block[i] = msgbuf1[i];
1692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694 return 0;
1695}
1696
David Brownella1cdeda2008-07-14 22:38:24 +02001697/**
1698 * i2c_smbus_xfer - execute SMBus protocol operations
1699 * @adapter: Handle to I2C bus
1700 * @addr: Address of SMBus slave on that bus
1701 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1702 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1703 * @command: Byte interpreted by slave, for protocols which use such bytes
1704 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1705 * @data: Data to be read or written
1706 *
1707 * This executes an SMBus protocol operation, and returns a negative
1708 * errno code else zero on success.
1709 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001710s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001711 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001712 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001714 unsigned long orig_jiffies;
1715 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001721 mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001722
1723 /* Retry automatically on arbitration loss */
1724 orig_jiffies = jiffies;
1725 for (res = 0, try = 0; try <= adapter->retries; try++) {
1726 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1727 read_write, command,
1728 protocol, data);
1729 if (res != -EAGAIN)
1730 break;
1731 if (time_after(jiffies,
1732 orig_jiffies + adapter->timeout))
1733 break;
1734 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001735 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 } else
1737 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001738 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return res;
1741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1745MODULE_DESCRIPTION("I2C-Bus main module");
1746MODULE_LICENSE("GPL");