blob: 0e45c296d3d22b3230f044a9a75bf360515102b0 [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>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020036#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
David Brownell9c1600e2007-05-01 23:26:31 +020039#include "i2c-core.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jean Delvare99cd8e22009-06-19 16:58:20 +020042/* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020043 that device detection, deletion of detected devices, and attach_adapter
44 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010045static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_IDR(i2c_adapter_idr);
Jean Delvare99cd8e22009-06-19 16:58:20 +020047static LIST_HEAD(userspace_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jean Delvaref8a227e2009-06-19 16:58:18 +020049static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020050static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010051
52/* ------------------------------------------------------------------------- */
53
Jean Delvared2653e92008-04-29 23:11:39 +020054static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
55 const struct i2c_client *client)
56{
57 while (id->name[0]) {
58 if (strcmp(client->name, id->name) == 0)
59 return id;
60 id++;
61 }
62 return NULL;
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int i2c_device_match(struct device *dev, struct device_driver *drv)
66{
David Brownell7b4fbc52007-05-01 23:26:30 +020067 struct i2c_client *client = to_i2c_client(dev);
68 struct i2c_driver *driver = to_i2c_driver(drv);
69
Jean Delvared2653e92008-04-29 23:11:39 +020070 /* match on an id table if there is one */
71 if (driver->id_table)
72 return i2c_match_id(driver->id_table, client) != NULL;
73
Jean Delvareeb8a7902008-05-18 20:49:41 +020074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
David Brownell7b4fbc52007-05-01 23:26:30 +020077#ifdef CONFIG_HOTPLUG
78
79/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020080static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020081{
82 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020083
Jean Delvareeb8a7902008-05-18 20:49:41 +020084 if (add_uevent_var(env, "MODALIAS=%s%s",
85 I2C_MODULE_PREFIX, client->name))
86 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020087 dev_dbg(dev, "uevent\n");
88 return 0;
89}
90
91#else
92#define i2c_device_uevent NULL
93#endif /* CONFIG_HOTPLUG */
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int i2c_device_probe(struct device *dev)
96{
David Brownell7b4fbc52007-05-01 23:26:30 +020097 struct i2c_client *client = to_i2c_client(dev);
98 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +010099 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200100
Jean Delvaree0457442008-07-14 22:38:30 +0200101 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200102 return -ENODEV;
103 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200104 if (!device_can_wakeup(&client->dev))
105 device_init_wakeup(&client->dev,
106 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200107 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200108
Jean Delvaree0457442008-07-14 22:38:30 +0200109 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100110 if (status)
111 client->driver = NULL;
112 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115static int i2c_device_remove(struct device *dev)
116{
David Brownella1d9e6e2007-05-01 23:26:30 +0200117 struct i2c_client *client = to_i2c_client(dev);
118 struct i2c_driver *driver;
119 int status;
120
121 if (!dev->driver)
122 return 0;
123
124 driver = to_i2c_driver(dev->driver);
125 if (driver->remove) {
126 dev_dbg(dev, "remove\n");
127 status = driver->remove(client);
128 } else {
129 dev->driver = NULL;
130 status = 0;
131 }
132 if (status == 0)
133 client->driver = NULL;
134 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
David Brownellf37dd802007-02-13 22:09:00 +0100137static void i2c_device_shutdown(struct device *dev)
138{
139 struct i2c_driver *driver;
140
141 if (!dev->driver)
142 return;
143 driver = to_i2c_driver(dev->driver);
144 if (driver->shutdown)
145 driver->shutdown(to_i2c_client(dev));
146}
147
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100148static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100149{
150 struct i2c_driver *driver;
151
152 if (!dev->driver)
153 return 0;
154 driver = to_i2c_driver(dev->driver);
155 if (!driver->suspend)
156 return 0;
157 return driver->suspend(to_i2c_client(dev), mesg);
158}
159
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100160static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100161{
162 struct i2c_driver *driver;
163
164 if (!dev->driver)
165 return 0;
166 driver = to_i2c_driver(dev->driver);
167 if (!driver->resume)
168 return 0;
169 return driver->resume(to_i2c_client(dev));
170}
171
David Brownell9c1600e2007-05-01 23:26:31 +0200172static void i2c_client_dev_release(struct device *dev)
173{
174 kfree(to_i2c_client(dev));
175}
176
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100177static ssize_t
178show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200179{
180 struct i2c_client *client = to_i2c_client(dev);
181 return sprintf(buf, "%s\n", client->name);
182}
183
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100184static ssize_t
185show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200186{
187 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200188 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200189}
190
191static struct device_attribute i2c_dev_attrs[] = {
192 __ATTR(name, S_IRUGO, show_client_name, NULL),
193 /* modalias helps coldplug: modprobe $(cat .../modalias) */
194 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
195 { },
196};
197
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200198struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100199 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200200 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100201 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200202 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100203 .probe = i2c_device_probe,
204 .remove = i2c_device_remove,
205 .shutdown = i2c_device_shutdown,
206 .suspend = i2c_device_suspend,
207 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000208};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200209EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000210
David Brownell9b766b82008-01-27 18:14:51 +0100211
212/**
213 * i2c_verify_client - return parameter as i2c_client, or NULL
214 * @dev: device, probably from some driver model iterator
215 *
216 * When traversing the driver model tree, perhaps using driver model
217 * iterators like @device_for_each_child(), you can't assume very much
218 * about the nodes you find. Use this function to avoid oopses caused
219 * by wrongly treating some non-I2C device as an i2c_client.
220 */
221struct i2c_client *i2c_verify_client(struct device *dev)
222{
223 return (dev->bus == &i2c_bus_type)
224 ? to_i2c_client(dev)
225 : NULL;
226}
227EXPORT_SYMBOL(i2c_verify_client);
228
229
David Brownell9c1600e2007-05-01 23:26:31 +0200230/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200231 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200232 * @adap: the adapter managing the device
233 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200234 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200235 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200236 * Create an i2c device. Binding is handled through driver model
237 * probe()/remove() methods. A driver may be bound to this device when we
238 * return from this function, or any later moment (e.g. maybe hotplugging will
239 * load the driver module). This call is not appropriate for use by mainboard
240 * initialization logic, which usually runs during an arch_initcall() long
241 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200242 *
243 * This returns the new i2c client, which may be saved for later use with
244 * i2c_unregister_device(); or NULL to indicate an error.
245 */
246struct i2c_client *
247i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
248{
249 struct i2c_client *client;
250 int status;
251
252 client = kzalloc(sizeof *client, GFP_KERNEL);
253 if (!client)
254 return NULL;
255
256 client->adapter = adap;
257
258 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200259
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200260 if (info->archdata)
261 client->dev.archdata = *info->archdata;
262
Marc Pignatee354252008-08-28 08:33:22 +0200263 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200264 client->addr = info->addr;
265 client->irq = info->irq;
266
David Brownell9c1600e2007-05-01 23:26:31 +0200267 strlcpy(client->name, info->type, sizeof(client->name));
268
Jean Delvaref8a227e2009-06-19 16:58:18 +0200269 /* Check for address business */
270 status = i2c_check_addr(adap, client->addr);
271 if (status)
272 goto out_err;
273
274 client->dev.parent = &client->adapter->dev;
275 client->dev.bus = &i2c_bus_type;
Jean Delvare1e40ac12009-06-19 16:58:19 +0200276 client->dev.release = i2c_client_dev_release;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200277
278 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
279 client->addr);
280 status = device_register(&client->dev);
281 if (status)
282 goto out_err;
283
Jean Delvaref8a227e2009-06-19 16:58:18 +0200284 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
285 client->name, dev_name(&client->dev));
286
David Brownell9c1600e2007-05-01 23:26:31 +0200287 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200288
289out_err:
290 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
291 "(%d)\n", client->name, client->addr, status);
292 kfree(client);
293 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200294}
295EXPORT_SYMBOL_GPL(i2c_new_device);
296
297
298/**
299 * i2c_unregister_device - reverse effect of i2c_new_device()
300 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200301 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200302 */
303void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200304{
David Brownella1d9e6e2007-05-01 23:26:30 +0200305 device_unregister(&client->dev);
306}
David Brownell9c1600e2007-05-01 23:26:31 +0200307EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200308
309
Jean Delvare60b129d2008-05-11 20:37:06 +0200310static const struct i2c_device_id dummy_id[] = {
311 { "dummy", 0 },
312 { },
313};
314
Jean Delvared2653e92008-04-29 23:11:39 +0200315static int dummy_probe(struct i2c_client *client,
316 const struct i2c_device_id *id)
317{
318 return 0;
319}
320
321static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100322{
323 return 0;
324}
325
326static struct i2c_driver dummy_driver = {
327 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200328 .probe = dummy_probe,
329 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200330 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100331};
332
333/**
334 * i2c_new_dummy - return a new i2c device bound to a dummy driver
335 * @adapter: the adapter managing the device
336 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100337 * Context: can sleep
338 *
339 * This returns an I2C client bound to the "dummy" driver, intended for use
340 * with devices that consume multiple addresses. Examples of such chips
341 * include various EEPROMS (like 24c04 and 24c08 models).
342 *
343 * These dummy devices have two main uses. First, most I2C and SMBus calls
344 * except i2c_transfer() need a client handle; the dummy will be that handle.
345 * And second, this prevents the specified address from being bound to a
346 * different driver.
347 *
348 * This returns the new i2c client, which should be saved for later use with
349 * i2c_unregister_device(); or NULL to indicate an error.
350 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100351struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100352{
353 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200354 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100355 };
356
David Brownelle9f13732008-01-27 18:14:52 +0100357 return i2c_new_device(adapter, &info);
358}
359EXPORT_SYMBOL_GPL(i2c_new_dummy);
360
David Brownellf37dd802007-02-13 22:09:00 +0100361/* ------------------------------------------------------------------------- */
362
David Brownell16ffadf2007-05-01 23:26:28 +0200363/* I2C bus adapters -- one roots each I2C or SMBUS segment */
364
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200365static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
David Brownellef2c83212007-05-01 23:26:28 +0200367 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 complete(&adap->dev_released);
369}
370
David Brownell16ffadf2007-05-01 23:26:28 +0200371static ssize_t
372show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
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 return sprintf(buf, "%s\n", adap->name);
376}
David Brownell16ffadf2007-05-01 23:26:28 +0200377
Jean Delvare99cd8e22009-06-19 16:58:20 +0200378/*
379 * Let users instantiate I2C devices through sysfs. This can be used when
380 * platform initialization code doesn't contain the proper data for
381 * whatever reason. Also useful for drivers that do device detection and
382 * detection fails, either because the device uses an unexpected address,
383 * or this is a compatible device with different ID register values.
384 *
385 * Parameter checking may look overzealous, but we really don't want
386 * the user to provide incorrect parameters.
387 */
388static ssize_t
389i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
391{
392 struct i2c_adapter *adap = to_i2c_adapter(dev);
393 struct i2c_board_info info;
394 struct i2c_client *client;
395 char *blank, end;
396 int res;
397
398 dev_warn(dev, "The new_device interface is still experimental "
399 "and may change in a near future\n");
400 memset(&info, 0, sizeof(struct i2c_board_info));
401
402 blank = strchr(buf, ' ');
403 if (!blank) {
404 dev_err(dev, "%s: Missing parameters\n", "new_device");
405 return -EINVAL;
406 }
407 if (blank - buf > I2C_NAME_SIZE - 1) {
408 dev_err(dev, "%s: Invalid device name\n", "new_device");
409 return -EINVAL;
410 }
411 memcpy(info.type, buf, blank - buf);
412
413 /* Parse remaining parameters, reject extra parameters */
414 res = sscanf(++blank, "%hi%c", &info.addr, &end);
415 if (res < 1) {
416 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
417 return -EINVAL;
418 }
419 if (res > 1 && end != '\n') {
420 dev_err(dev, "%s: Extra parameters\n", "new_device");
421 return -EINVAL;
422 }
423
424 if (info.addr < 0x03 || info.addr > 0x77) {
425 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
426 info.addr);
427 return -EINVAL;
428 }
429
430 client = i2c_new_device(adap, &info);
431 if (!client)
432 return -EEXIST;
433
434 /* Keep track of the added device */
435 mutex_lock(&core_lock);
436 list_add_tail(&client->detected, &userspace_devices);
437 mutex_unlock(&core_lock);
438 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
439 info.type, info.addr);
440
441 return count;
442}
443
444/*
445 * And of course let the users delete the devices they instantiated, if
446 * they got it wrong. This interface can only be used to delete devices
447 * instantiated by i2c_sysfs_new_device above. This guarantees that we
448 * don't delete devices to which some kernel code still has references.
449 *
450 * Parameter checking may look overzealous, but we really don't want
451 * the user to delete the wrong device.
452 */
453static ssize_t
454i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
455 const char *buf, size_t count)
456{
457 struct i2c_adapter *adap = to_i2c_adapter(dev);
458 struct i2c_client *client, *next;
459 unsigned short addr;
460 char end;
461 int res;
462
463 /* Parse parameters, reject extra parameters */
464 res = sscanf(buf, "%hi%c", &addr, &end);
465 if (res < 1) {
466 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
467 return -EINVAL;
468 }
469 if (res > 1 && end != '\n') {
470 dev_err(dev, "%s: Extra parameters\n", "delete_device");
471 return -EINVAL;
472 }
473
474 /* Make sure the device was added through sysfs */
475 res = -ENOENT;
476 mutex_lock(&core_lock);
477 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
478 if (client->addr == addr && client->adapter == adap) {
479 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
480 "delete_device", client->name, client->addr);
481
482 list_del(&client->detected);
483 i2c_unregister_device(client);
484 res = count;
485 break;
486 }
487 }
488 mutex_unlock(&core_lock);
489
490 if (res < 0)
491 dev_err(dev, "%s: Can't find device in list\n",
492 "delete_device");
493 return res;
494}
495
David Brownell16ffadf2007-05-01 23:26:28 +0200496static struct device_attribute i2c_adapter_attrs[] = {
497 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
Jean Delvare99cd8e22009-06-19 16:58:20 +0200498 __ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device),
499 __ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device),
David Brownell16ffadf2007-05-01 23:26:28 +0200500 { },
501};
502
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200503static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200504 .owner = THIS_MODULE,
505 .name = "i2c-adapter",
506 .dev_attrs = i2c_adapter_attrs,
507};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
David Brownell9c1600e2007-05-01 23:26:31 +0200509static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
510{
511 struct i2c_devinfo *devinfo;
512
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200513 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200514 list_for_each_entry(devinfo, &__i2c_board_list, list) {
515 if (devinfo->busnum == adapter->nr
516 && !i2c_new_device(adapter,
517 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100518 dev_err(&adapter->dev,
519 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200520 devinfo->board_info.addr);
521 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200522 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200523}
524
Jean Delvare026526f2008-01-27 18:14:49 +0100525static int i2c_do_add_adapter(struct device_driver *d, void *data)
526{
527 struct i2c_driver *driver = to_i2c_driver(d);
528 struct i2c_adapter *adap = data;
529
Jean Delvare4735c982008-07-14 22:38:36 +0200530 /* Detect supported devices on that bus, and instantiate them */
531 i2c_detect(adap, driver);
532
533 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100534 if (driver->attach_adapter) {
535 /* We ignore the return code; if it fails, too bad */
536 driver->attach_adapter(adap);
537 }
538 return 0;
539}
540
David Brownell6e13e642007-05-01 23:26:31 +0200541static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Jean Delvare026526f2008-01-27 18:14:49 +0100543 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
David Brownell1d0b19c2008-10-14 17:30:05 +0200545 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200546 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
547 res = -EAGAIN;
548 goto out_list;
549 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200550
Ingo Molnar5c085d32006-01-18 23:16:04 +0100551 mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jean Delvare8fcfef62009-03-28 21:34:43 +0100553 /* Set default timeout to 1 second if not already set */
554 if (adap->timeout == 0)
555 adap->timeout = HZ;
556
Kay Sievers27d9c182009-01-07 14:29:16 +0100557 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200559 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200560 res = device_register(&adap->dev);
561 if (res)
562 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200564 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
565
Jean Delvare729d6dd2009-06-19 16:58:18 +0200566 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200567 if (adap->nr < __i2c_first_dynamic_bus_num)
568 i2c_scan_static_board_info(adap);
569
Jean Delvare4735c982008-07-14 22:38:36 +0200570 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200571 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100572 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
573 i2c_do_add_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100574 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200575
576 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200577
Jean Delvareb119c6c2006-08-15 18:26:30 +0200578out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200579 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200580 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200581 mutex_unlock(&core_lock);
582 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
David Brownell6e13e642007-05-01 23:26:31 +0200585/**
586 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
587 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200588 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200589 *
590 * This routine is used to declare an I2C adapter when its bus number
591 * doesn't matter. Examples: for I2C adapters dynamically added by
592 * USB links or PCI plugin cards.
593 *
594 * When this returns zero, a new bus number was allocated and stored
595 * in adap->nr, and the specified adapter became available for clients.
596 * Otherwise, a negative errno value is returned.
597 */
598int i2c_add_adapter(struct i2c_adapter *adapter)
599{
600 int id, res = 0;
601
602retry:
603 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
604 return -ENOMEM;
605
Jean Delvarecaada322008-01-27 18:14:49 +0100606 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200607 /* "above" here means "above or equal to", sigh */
608 res = idr_get_new_above(&i2c_adapter_idr, adapter,
609 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100610 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200611
612 if (res < 0) {
613 if (res == -EAGAIN)
614 goto retry;
615 return res;
616 }
617
618 adapter->nr = id;
619 return i2c_register_adapter(adapter);
620}
621EXPORT_SYMBOL(i2c_add_adapter);
622
623/**
624 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
625 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200626 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200627 *
628 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100629 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
630 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200631 * is used to properly configure I2C devices.
632 *
633 * If no devices have pre-been declared for this bus, then be sure to
634 * register the adapter before any dynamically allocated ones. Otherwise
635 * the required bus ID may not be available.
636 *
637 * When this returns zero, the specified adapter became available for
638 * clients using the bus number provided in adap->nr. Also, the table
639 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
640 * and the appropriate driver model device nodes are created. Otherwise, a
641 * negative errno value is returned.
642 */
643int i2c_add_numbered_adapter(struct i2c_adapter *adap)
644{
645 int id;
646 int status;
647
648 if (adap->nr & ~MAX_ID_MASK)
649 return -EINVAL;
650
651retry:
652 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
653 return -ENOMEM;
654
Jean Delvarecaada322008-01-27 18:14:49 +0100655 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200656 /* "above" here means "above or equal to", sigh;
657 * we need the "equal to" result to force the result
658 */
659 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
660 if (status == 0 && id != adap->nr) {
661 status = -EBUSY;
662 idr_remove(&i2c_adapter_idr, id);
663 }
Jean Delvarecaada322008-01-27 18:14:49 +0100664 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200665 if (status == -EAGAIN)
666 goto retry;
667
668 if (status == 0)
669 status = i2c_register_adapter(adap);
670 return status;
671}
672EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
673
Jean Delvare026526f2008-01-27 18:14:49 +0100674static int i2c_do_del_adapter(struct device_driver *d, void *data)
675{
676 struct i2c_driver *driver = to_i2c_driver(d);
677 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200678 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100679 int res;
680
Jean Delvareacec2112009-03-28 21:34:40 +0100681 /* Remove the devices we created ourselves as the result of hardware
682 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200683 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
684 if (client->adapter == adapter) {
685 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
686 client->name, client->addr);
687 list_del(&client->detected);
688 i2c_unregister_device(client);
689 }
690 }
691
Jean Delvare026526f2008-01-27 18:14:49 +0100692 if (!driver->detach_adapter)
693 return 0;
694 res = driver->detach_adapter(adapter);
695 if (res)
696 dev_err(&adapter->dev, "detach_adapter failed (%d) "
697 "for driver [%s]\n", res, driver->driver.name);
698 return res;
699}
700
Jean Delvaree549c2b2009-06-19 16:58:19 +0200701static int __unregister_client(struct device *dev, void *dummy)
702{
703 struct i2c_client *client = i2c_verify_client(dev);
704 if (client)
705 i2c_unregister_device(client);
706 return 0;
707}
708
David Brownelld64f73b2007-07-12 14:12:28 +0200709/**
710 * i2c_del_adapter - unregister I2C adapter
711 * @adap: the adapter being unregistered
712 * Context: can sleep
713 *
714 * This unregisters an I2C adapter which was previously registered
715 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717int i2c_del_adapter(struct i2c_adapter *adap)
718{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200720 struct i2c_adapter *found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200723 mutex_lock(&core_lock);
724 found = idr_find(&i2c_adapter_idr, adap->nr);
725 mutex_unlock(&core_lock);
726 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200727 pr_debug("i2c-core: attempting to delete unregistered "
728 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200729 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
Jean Delvare026526f2008-01-27 18:14:49 +0100732 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200733 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100734 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
735 i2c_do_del_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200736 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100737 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200738 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Jean Delvaree549c2b2009-06-19 16:58:19 +0200740 /* Detach any active clients. This can't fail, thus we do not
741 checking the returned value. */
742 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /* clean up the sysfs representation */
745 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /* wait for sysfs to drop all references */
749 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
David Brownell6e13e642007-05-01 23:26:31 +0200751 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200752 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200754 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200756 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200758 /* Clear the device structure in case this adapter is ever going to be
759 added again */
760 memset(&adap->dev, 0, sizeof(adap->dev));
761
Jean Delvare35fc37f2009-06-19 16:58:19 +0200762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
David Brownellc0564602007-05-01 23:26:31 +0200764EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766
David Brownell7b4fbc52007-05-01 23:26:30 +0200767/* ------------------------------------------------------------------------- */
768
Dave Young7f101a92008-07-14 22:38:19 +0200769static int __attach_adapter(struct device *dev, void *data)
770{
771 struct i2c_adapter *adapter = to_i2c_adapter(dev);
772 struct i2c_driver *driver = data;
773
Jean Delvare4735c982008-07-14 22:38:36 +0200774 i2c_detect(adapter, driver);
775
776 /* Legacy drivers scan i2c busses directly */
777 if (driver->attach_adapter)
778 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200779
780 return 0;
781}
782
David Brownell7b4fbc52007-05-01 23:26:30 +0200783/*
784 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200785 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
787
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800788int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100790 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
David Brownell1d0b19c2008-10-14 17:30:05 +0200792 /* Can't register until after driver model init */
793 if (unlikely(WARN_ON(!i2c_bus_type.p)))
794 return -EAGAIN;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800797 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jean Delvare729d6dd2009-06-19 16:58:18 +0200800 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200801 * will have called probe() for all matching-but-unbound devices.
802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 res = driver_register(&driver->driver);
804 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100805 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100806
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100807 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Jean Delvare4735c982008-07-14 22:38:36 +0200809 INIT_LIST_HEAD(&driver->clients);
810 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200811 mutex_lock(&core_lock);
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400812 class_for_each_device(&i2c_adapter_class, NULL, driver,
813 __attach_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100814 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200815
Jean Delvare7eebcb72006-02-05 23:28:21 +0100816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800818EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Dave Young7f101a92008-07-14 22:38:19 +0200820static int __detach_adapter(struct device *dev, void *data)
821{
822 struct i2c_adapter *adapter = to_i2c_adapter(dev);
823 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200824 struct i2c_client *client, *_n;
825
Jean Delvareacec2112009-03-28 21:34:40 +0100826 /* Remove the devices we created ourselves as the result of hardware
827 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200828 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
829 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
830 client->name, client->addr);
831 list_del(&client->detected);
832 i2c_unregister_device(client);
833 }
834
Dave Young7f101a92008-07-14 22:38:19 +0200835 if (driver->detach_adapter) {
836 if (driver->detach_adapter(adapter))
837 dev_err(&adapter->dev,
838 "detach_adapter failed for driver [%s]\n",
839 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200840 }
841
842 return 0;
843}
844
David Brownella1d9e6e2007-05-01 23:26:30 +0200845/**
846 * i2c_del_driver - unregister I2C driver
847 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200848 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200849 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200850void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jean Delvarecaada322008-01-27 18:14:49 +0100852 mutex_lock(&core_lock);
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400853 class_for_each_device(&i2c_adapter_class, NULL, driver,
854 __detach_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200855 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100858 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
David Brownellc0564602007-05-01 23:26:31 +0200860EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
David Brownell7b4fbc52007-05-01 23:26:30 +0200862/* ------------------------------------------------------------------------- */
863
David Brownell9b766b82008-01-27 18:14:51 +0100864static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
David Brownell9b766b82008-01-27 18:14:51 +0100866 struct i2c_client *client = i2c_verify_client(dev);
867 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
David Brownell9b766b82008-01-27 18:14:51 +0100869 if (client && client->addr == addr)
870 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
872}
873
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100874static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
David Brownell9b766b82008-01-27 18:14:51 +0100876 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Jean Delvaree48d3312008-01-27 18:14:48 +0100879/**
880 * i2c_use_client - increments the reference count of the i2c client structure
881 * @client: the client being referenced
882 *
883 * Each live reference to a client should be refcounted. The driver model does
884 * that automatically as part of driver binding, so that most drivers don't
885 * need to do this explicitly: they hold a reference until they're unbound
886 * from the device.
887 *
888 * A pointer to the client with the incremented reference counter is returned.
889 */
890struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
David Brownell6ea438e2008-07-14 22:38:24 +0200892 if (client && get_device(&client->dev))
893 return client;
894 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
David Brownellc0564602007-05-01 23:26:31 +0200896EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Jean Delvaree48d3312008-01-27 18:14:48 +0100898/**
899 * i2c_release_client - release a use of the i2c client structure
900 * @client: the client being no longer referenced
901 *
902 * Must be called when a user of a client is finished with it.
903 */
904void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
David Brownell6ea438e2008-07-14 22:38:24 +0200906 if (client)
907 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
David Brownellc0564602007-05-01 23:26:31 +0200909EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
David Brownell9b766b82008-01-27 18:14:51 +0100911struct i2c_cmd_arg {
912 unsigned cmd;
913 void *arg;
914};
915
916static int i2c_cmd(struct device *dev, void *_arg)
917{
918 struct i2c_client *client = i2c_verify_client(dev);
919 struct i2c_cmd_arg *arg = _arg;
920
921 if (client && client->driver && client->driver->command)
922 client->driver->command(client, arg->cmd, arg->arg);
923 return 0;
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
927{
David Brownell9b766b82008-01-27 18:14:51 +0100928 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
David Brownell9b766b82008-01-27 18:14:51 +0100930 cmd_arg.cmd = cmd;
931 cmd_arg.arg = arg;
932 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
David Brownellc0564602007-05-01 23:26:31 +0200934EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936static int __init i2c_init(void)
937{
938 int retval;
939
940 retval = bus_register(&i2c_bus_type);
941 if (retval)
942 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100943 retval = class_register(&i2c_adapter_class);
944 if (retval)
945 goto bus_err;
946 retval = i2c_add_driver(&dummy_driver);
947 if (retval)
948 goto class_err;
949 return 0;
950
951class_err:
952 class_unregister(&i2c_adapter_class);
953bus_err:
954 bus_unregister(&i2c_bus_type);
955 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958static void __exit i2c_exit(void)
959{
David Brownelle9f13732008-01-27 18:14:52 +0100960 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 bus_unregister(&i2c_bus_type);
963}
964
David Brownella10f9e72008-10-14 17:30:06 +0200965/* We must initialize early, because some subsystems register i2c drivers
966 * in subsys_initcall() code, but are linked (and initialized) before i2c.
967 */
968postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969module_exit(i2c_exit);
970
971/* ----------------------------------------------------
972 * the functional interface to the i2c busses.
973 * ----------------------------------------------------
974 */
975
David Brownella1cdeda2008-07-14 22:38:24 +0200976/**
977 * i2c_transfer - execute a single or combined I2C message
978 * @adap: Handle to I2C bus
979 * @msgs: One or more messages to execute before STOP is issued to
980 * terminate the operation; each message begins with a START.
981 * @num: Number of messages to be executed.
982 *
983 * Returns negative errno, else the number of messages executed.
984 *
985 * Note that there is no requirement that each message be sent to
986 * the same slave address, although that is the most common model.
987 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100988int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Clifford Wolf66b650f2009-06-15 18:01:46 +0200990 unsigned long orig_jiffies;
991 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
David Brownella1cdeda2008-07-14 22:38:24 +0200993 /* REVISIT the fault reporting model here is weak:
994 *
995 * - When we get an error after receiving N bytes from a slave,
996 * there is no way to report "N".
997 *
998 * - When we get a NAK after transmitting N bytes to a slave,
999 * there is no way to report "N" ... or to let the master
1000 * continue executing the rest of this combined message, if
1001 * that's the appropriate response.
1002 *
1003 * - When for example "num" is two and we successfully complete
1004 * the first message but get an error part way through the
1005 * second, it's unclear whether that should be reported as
1006 * one (discarding status on the second message) or errno
1007 * (discarding status on the first one).
1008 */
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (adap->algo->master_xfer) {
1011#ifdef DEBUG
1012 for (ret = 0; ret < num; ret++) {
1013 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001014 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1015 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1016 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018#endif
1019
Mike Rapoportcea443a82008-01-27 18:14:50 +01001020 if (in_atomic() || irqs_disabled()) {
1021 ret = mutex_trylock(&adap->bus_lock);
1022 if (!ret)
1023 /* I2C activity is ongoing. */
1024 return -EAGAIN;
1025 } else {
1026 mutex_lock_nested(&adap->bus_lock, adap->level);
1027 }
1028
Clifford Wolf66b650f2009-06-15 18:01:46 +02001029 /* Retry automatically on arbitration loss */
1030 orig_jiffies = jiffies;
1031 for (ret = 0, try = 0; try <= adap->retries; try++) {
1032 ret = adap->algo->master_xfer(adap, msgs, num);
1033 if (ret != -EAGAIN)
1034 break;
1035 if (time_after(jiffies, orig_jiffies + adap->timeout))
1036 break;
1037 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001038 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 return ret;
1041 } else {
1042 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001043 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045}
David Brownellc0564602007-05-01 23:26:31 +02001046EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
David Brownella1cdeda2008-07-14 22:38:24 +02001048/**
1049 * i2c_master_send - issue a single I2C message in master transmit mode
1050 * @client: Handle to slave device
1051 * @buf: Data that will be written to the slave
1052 * @count: How many bytes to write
1053 *
1054 * Returns negative errno, or else the number of bytes written.
1055 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1057{
1058 int ret;
1059 struct i2c_adapter *adap=client->adapter;
1060 struct i2c_msg msg;
1061
Jean Delvare815f55f2005-05-07 22:58:46 +02001062 msg.addr = client->addr;
1063 msg.flags = client->flags & I2C_M_TEN;
1064 msg.len = count;
1065 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001066
Jean Delvare815f55f2005-05-07 22:58:46 +02001067 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Jean Delvare815f55f2005-05-07 22:58:46 +02001069 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1070 transmitted, else error code. */
1071 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
David Brownellc0564602007-05-01 23:26:31 +02001073EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
David Brownella1cdeda2008-07-14 22:38:24 +02001075/**
1076 * i2c_master_recv - issue a single I2C message in master receive mode
1077 * @client: Handle to slave device
1078 * @buf: Where to store data read from slave
1079 * @count: How many bytes to read
1080 *
1081 * Returns negative errno, or else the number of bytes read.
1082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1084{
1085 struct i2c_adapter *adap=client->adapter;
1086 struct i2c_msg msg;
1087 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Jean Delvare815f55f2005-05-07 22:58:46 +02001089 msg.addr = client->addr;
1090 msg.flags = client->flags & I2C_M_TEN;
1091 msg.flags |= I2C_M_RD;
1092 msg.len = count;
1093 msg.buf = buf;
1094
1095 ret = i2c_transfer(adap, &msg, 1);
1096
1097 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1098 transmitted, else error code. */
1099 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
David Brownellc0564602007-05-01 23:26:31 +02001101EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/* ----------------------------------------------------
1104 * the i2c address scanning function
1105 * Will not work for 10-bit addresses!
1106 * ----------------------------------------------------
1107 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001108
Jean Delvare4735c982008-07-14 22:38:36 +02001109static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1110 struct i2c_driver *driver)
1111{
1112 struct i2c_board_info info;
1113 struct i2c_adapter *adapter = temp_client->adapter;
1114 int addr = temp_client->addr;
1115 int err;
1116
1117 /* Make sure the address is valid */
1118 if (addr < 0x03 || addr > 0x77) {
1119 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1120 addr);
1121 return -EINVAL;
1122 }
1123
1124 /* Skip if already in use */
1125 if (i2c_check_addr(adapter, addr))
1126 return 0;
1127
1128 /* Make sure there is something at this address, unless forced */
1129 if (kind < 0) {
1130 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1131 I2C_SMBUS_QUICK, NULL) < 0)
1132 return 0;
1133
1134 /* prevent 24RF08 corruption */
1135 if ((addr & ~0x0f) == 0x50)
1136 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1137 I2C_SMBUS_QUICK, NULL);
1138 }
1139
1140 /* Finally call the custom detection function */
1141 memset(&info, 0, sizeof(struct i2c_board_info));
1142 info.addr = addr;
1143 err = driver->detect(temp_client, kind, &info);
1144 if (err) {
1145 /* -ENODEV is returned if the detection fails. We catch it
1146 here as this isn't an error. */
1147 return err == -ENODEV ? 0 : err;
1148 }
1149
1150 /* Consistency check */
1151 if (info.type[0] == '\0') {
1152 dev_err(&adapter->dev, "%s detection function provided "
1153 "no name for 0x%x\n", driver->driver.name,
1154 addr);
1155 } else {
1156 struct i2c_client *client;
1157
1158 /* Detection succeeded, instantiate the device */
1159 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1160 info.type, info.addr);
1161 client = i2c_new_device(adapter, &info);
1162 if (client)
1163 list_add_tail(&client->detected, &driver->clients);
1164 else
1165 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1166 info.type, info.addr);
1167 }
1168 return 0;
1169}
1170
1171static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1172{
1173 const struct i2c_client_address_data *address_data;
1174 struct i2c_client *temp_client;
1175 int i, err = 0;
1176 int adap_id = i2c_adapter_id(adapter);
1177
1178 address_data = driver->address_data;
1179 if (!driver->detect || !address_data)
1180 return 0;
1181
1182 /* Set up a temporary client to help detect callback */
1183 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1184 if (!temp_client)
1185 return -ENOMEM;
1186 temp_client->adapter = adapter;
1187
1188 /* Force entries are done first, and are not affected by ignore
1189 entries */
1190 if (address_data->forces) {
1191 const unsigned short * const *forces = address_data->forces;
1192 int kind;
1193
1194 for (kind = 0; forces[kind]; kind++) {
1195 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1196 i += 2) {
1197 if (forces[kind][i] == adap_id
1198 || forces[kind][i] == ANY_I2C_BUS) {
1199 dev_dbg(&adapter->dev, "found force "
1200 "parameter for adapter %d, "
1201 "addr 0x%02x, kind %d\n",
1202 adap_id, forces[kind][i + 1],
1203 kind);
1204 temp_client->addr = forces[kind][i + 1];
1205 err = i2c_detect_address(temp_client,
1206 kind, driver);
1207 if (err)
1208 goto exit_free;
1209 }
1210 }
1211 }
1212 }
1213
Jean Delvare4329cf82008-08-28 08:33:23 +02001214 /* Stop here if the classes do not match */
1215 if (!(adapter->class & driver->class))
1216 goto exit_free;
1217
Jean Delvare4735c982008-07-14 22:38:36 +02001218 /* Stop here if we can't use SMBUS_QUICK */
1219 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1220 if (address_data->probe[0] == I2C_CLIENT_END
1221 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1222 goto exit_free;
1223
1224 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1225 "can't probe for chips\n");
1226 err = -EOPNOTSUPP;
1227 goto exit_free;
1228 }
1229
Jean Delvare4735c982008-07-14 22:38:36 +02001230 /* Probe entries are done second, and are not affected by ignore
1231 entries either */
1232 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1233 if (address_data->probe[i] == adap_id
1234 || address_data->probe[i] == ANY_I2C_BUS) {
1235 dev_dbg(&adapter->dev, "found probe parameter for "
1236 "adapter %d, addr 0x%02x\n", adap_id,
1237 address_data->probe[i + 1]);
1238 temp_client->addr = address_data->probe[i + 1];
1239 err = i2c_detect_address(temp_client, -1, driver);
1240 if (err)
1241 goto exit_free;
1242 }
1243 }
1244
1245 /* Normal entries are done last, unless shadowed by an ignore entry */
1246 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1247 int j, ignore;
1248
1249 ignore = 0;
1250 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1251 j += 2) {
1252 if ((address_data->ignore[j] == adap_id ||
1253 address_data->ignore[j] == ANY_I2C_BUS)
1254 && address_data->ignore[j + 1]
1255 == address_data->normal_i2c[i]) {
1256 dev_dbg(&adapter->dev, "found ignore "
1257 "parameter for adapter %d, "
1258 "addr 0x%02x\n", adap_id,
1259 address_data->ignore[j + 1]);
1260 ignore = 1;
1261 break;
1262 }
1263 }
1264 if (ignore)
1265 continue;
1266
1267 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1268 "addr 0x%02x\n", adap_id,
1269 address_data->normal_i2c[i]);
1270 temp_client->addr = address_data->normal_i2c[i];
1271 err = i2c_detect_address(temp_client, -1, driver);
1272 if (err)
1273 goto exit_free;
1274 }
1275
1276 exit_free:
1277 kfree(temp_client);
1278 return err;
1279}
1280
Jean Delvare12b5053a2007-05-01 23:26:31 +02001281struct i2c_client *
1282i2c_new_probed_device(struct i2c_adapter *adap,
1283 struct i2c_board_info *info,
1284 unsigned short const *addr_list)
1285{
1286 int i;
1287
1288 /* Stop here if the bus doesn't support probing */
1289 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1290 dev_err(&adap->dev, "Probing not supported\n");
1291 return NULL;
1292 }
1293
Jean Delvare12b5053a2007-05-01 23:26:31 +02001294 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1295 /* Check address validity */
1296 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1297 dev_warn(&adap->dev, "Invalid 7-bit address "
1298 "0x%02x\n", addr_list[i]);
1299 continue;
1300 }
1301
1302 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001303 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001304 dev_dbg(&adap->dev, "Address 0x%02x already in "
1305 "use, not probing\n", addr_list[i]);
1306 continue;
1307 }
1308
1309 /* Test address responsiveness
1310 The default probe method is a quick write, but it is known
1311 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1312 and could also irreversibly write-protect some EEPROMs, so
1313 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1314 read instead. Also, some bus drivers don't implement
1315 quick write, so we fallback to a byte read it that case
1316 too. */
1317 if ((addr_list[i] & ~0x07) == 0x30
1318 || (addr_list[i] & ~0x0f) == 0x50
1319 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001320 union i2c_smbus_data data;
1321
Jean Delvare12b5053a2007-05-01 23:26:31 +02001322 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1323 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001324 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001325 break;
1326 } else {
1327 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1328 I2C_SMBUS_WRITE, 0,
1329 I2C_SMBUS_QUICK, NULL) >= 0)
1330 break;
1331 }
1332 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001333
1334 if (addr_list[i] == I2C_CLIENT_END) {
1335 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1336 return NULL;
1337 }
1338
1339 info->addr = addr_list[i];
1340 return i2c_new_device(adap, info);
1341}
1342EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344struct i2c_adapter* i2c_get_adapter(int id)
1345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001347
Jean Delvarecaada322008-01-27 18:14:49 +01001348 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001349 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001350 if (adapter && !try_module_get(adapter->owner))
1351 adapter = NULL;
1352
Jean Delvarecaada322008-01-27 18:14:49 +01001353 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001354 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
David Brownellc0564602007-05-01 23:26:31 +02001356EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358void i2c_put_adapter(struct i2c_adapter *adap)
1359{
1360 module_put(adap->owner);
1361}
David Brownellc0564602007-05-01 23:26:31 +02001362EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364/* The SMBus parts */
1365
David Brownell438d6c22006-12-10 21:21:31 +01001366#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001367static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001372 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 data = data ^ POLY;
1374 data = data << 1;
1375 }
1376 return (u8)(data >> 8);
1377}
1378
Jean Delvare421ef472005-10-26 21:28:55 +02001379/* Incremental CRC8 over count bytes in the array pointed to by p */
1380static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 int i;
1383
1384 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001385 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return crc;
1387}
1388
Jean Delvare421ef472005-10-26 21:28:55 +02001389/* Assume a 7-bit address, which is reasonable for SMBus */
1390static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Jean Delvare421ef472005-10-26 21:28:55 +02001392 /* The address will be sent first */
1393 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1394 pec = i2c_smbus_pec(pec, &addr, 1);
1395
1396 /* The data buffer follows */
1397 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Jean Delvare421ef472005-10-26 21:28:55 +02001400/* Used for write only transactions */
1401static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Jean Delvare421ef472005-10-26 21:28:55 +02001403 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1404 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Jean Delvare421ef472005-10-26 21:28:55 +02001407/* Return <0 on CRC error
1408 If there was a write before this read (most cases) we need to take the
1409 partial CRC from the write part into account.
1410 Note that this function does modify the message (we need to decrease the
1411 message length to hide the CRC byte from the caller). */
1412static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Jean Delvare421ef472005-10-26 21:28:55 +02001414 u8 rpec = msg->buf[--msg->len];
1415 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (rpec != cpec) {
1418 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1419 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001420 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
David Brownell438d6c22006-12-10 21:21:31 +01001422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
David Brownella1cdeda2008-07-14 22:38:24 +02001425/**
1426 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1427 * @client: Handle to slave device
1428 *
1429 * This executes the SMBus "receive byte" protocol, returning negative errno
1430 * else the byte received from the device.
1431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432s32 i2c_smbus_read_byte(struct i2c_client *client)
1433{
1434 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001435 int status;
1436
1437 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1438 I2C_SMBUS_READ, 0,
1439 I2C_SMBUS_BYTE, &data);
1440 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
David Brownellc0564602007-05-01 23:26:31 +02001442EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
David Brownella1cdeda2008-07-14 22:38:24 +02001444/**
1445 * i2c_smbus_write_byte - SMBus "send byte" protocol
1446 * @client: Handle to slave device
1447 * @value: Byte to be sent
1448 *
1449 * This executes the SMBus "send byte" protocol, returning negative errno
1450 * else zero on success.
1451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001455 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
David Brownellc0564602007-05-01 23:26:31 +02001457EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
David Brownella1cdeda2008-07-14 22:38:24 +02001459/**
1460 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1461 * @client: Handle to slave device
1462 * @command: Byte interpreted by slave
1463 *
1464 * This executes the SMBus "read byte" protocol, returning negative errno
1465 * else a data byte received from the device.
1466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1468{
1469 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001470 int status;
1471
1472 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1473 I2C_SMBUS_READ, command,
1474 I2C_SMBUS_BYTE_DATA, &data);
1475 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
David Brownellc0564602007-05-01 23:26:31 +02001477EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
David Brownella1cdeda2008-07-14 22:38:24 +02001479/**
1480 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1481 * @client: Handle to slave device
1482 * @command: Byte interpreted by slave
1483 * @value: Byte being written
1484 *
1485 * This executes the SMBus "write byte" protocol, returning negative errno
1486 * else zero on success.
1487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1489{
1490 union i2c_smbus_data data;
1491 data.byte = value;
1492 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1493 I2C_SMBUS_WRITE,command,
1494 I2C_SMBUS_BYTE_DATA,&data);
1495}
David Brownellc0564602007-05-01 23:26:31 +02001496EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
David Brownella1cdeda2008-07-14 22:38:24 +02001498/**
1499 * i2c_smbus_read_word_data - SMBus "read word" protocol
1500 * @client: Handle to slave device
1501 * @command: Byte interpreted by slave
1502 *
1503 * This executes the SMBus "read word" protocol, returning negative errno
1504 * else a 16-bit unsigned "word" received from the device.
1505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1507{
1508 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001509 int status;
1510
1511 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1512 I2C_SMBUS_READ, command,
1513 I2C_SMBUS_WORD_DATA, &data);
1514 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
David Brownellc0564602007-05-01 23:26:31 +02001516EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
David Brownella1cdeda2008-07-14 22:38:24 +02001518/**
1519 * i2c_smbus_write_word_data - SMBus "write word" protocol
1520 * @client: Handle to slave device
1521 * @command: Byte interpreted by slave
1522 * @value: 16-bit "word" being written
1523 *
1524 * This executes the SMBus "write word" protocol, returning negative errno
1525 * else zero on success.
1526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1528{
1529 union i2c_smbus_data data;
1530 data.word = value;
1531 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1532 I2C_SMBUS_WRITE,command,
1533 I2C_SMBUS_WORD_DATA,&data);
1534}
David Brownellc0564602007-05-01 23:26:31 +02001535EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
David Brownella64ec072007-10-13 23:56:31 +02001537/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001538 * i2c_smbus_process_call - SMBus "process call" protocol
1539 * @client: Handle to slave device
1540 * @command: Byte interpreted by slave
1541 * @value: 16-bit "word" being written
1542 *
1543 * This executes the SMBus "process call" protocol, returning negative errno
1544 * else a 16-bit unsigned "word" received from the device.
1545 */
1546s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1547{
1548 union i2c_smbus_data data;
1549 int status;
1550 data.word = value;
1551
1552 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1553 I2C_SMBUS_WRITE, command,
1554 I2C_SMBUS_PROC_CALL, &data);
1555 return (status < 0) ? status : data.word;
1556}
1557EXPORT_SYMBOL(i2c_smbus_process_call);
1558
1559/**
David Brownella1cdeda2008-07-14 22:38:24 +02001560 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001561 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001562 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001563 * @values: Byte array into which data will be read; big enough to hold
1564 * the data returned by the slave. SMBus allows at most 32 bytes.
1565 *
David Brownella1cdeda2008-07-14 22:38:24 +02001566 * This executes the SMBus "block read" protocol, returning negative errno
1567 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001568 *
1569 * Note that using this function requires that the client's adapter support
1570 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1571 * support this; its emulation through I2C messaging relies on a specific
1572 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1573 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001574s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1575 u8 *values)
1576{
1577 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001578 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001579
David Brownell24a5bb72008-07-14 22:38:23 +02001580 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1581 I2C_SMBUS_READ, command,
1582 I2C_SMBUS_BLOCK_DATA, &data);
1583 if (status)
1584 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001585
1586 memcpy(values, &data.block[1], data.block[0]);
1587 return data.block[0];
1588}
1589EXPORT_SYMBOL(i2c_smbus_read_block_data);
1590
David Brownella1cdeda2008-07-14 22:38:24 +02001591/**
1592 * i2c_smbus_write_block_data - SMBus "block write" protocol
1593 * @client: Handle to slave device
1594 * @command: Byte interpreted by slave
1595 * @length: Size of data block; SMBus allows at most 32 bytes
1596 * @values: Byte array which will be written.
1597 *
1598 * This executes the SMBus "block write" protocol, returning negative errno
1599 * else zero on success.
1600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001602 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (length > I2C_SMBUS_BLOCK_MAX)
1607 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001609 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1611 I2C_SMBUS_WRITE,command,
1612 I2C_SMBUS_BLOCK_DATA,&data);
1613}
David Brownellc0564602007-05-01 23:26:31 +02001614EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001617s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1618 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001621 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001622
Jean Delvare4b2643d2007-07-12 14:12:29 +02001623 if (length > I2C_SMBUS_BLOCK_MAX)
1624 length = I2C_SMBUS_BLOCK_MAX;
1625 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001626 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1627 I2C_SMBUS_READ, command,
1628 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1629 if (status < 0)
1630 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001631
1632 memcpy(values, &data.block[1], data.block[0]);
1633 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
David Brownellc0564602007-05-01 23:26:31 +02001635EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Jean Delvare21bbd692006-01-09 15:19:18 +11001637s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001638 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001639{
1640 union i2c_smbus_data data;
1641
1642 if (length > I2C_SMBUS_BLOCK_MAX)
1643 length = I2C_SMBUS_BLOCK_MAX;
1644 data.block[0] = length;
1645 memcpy(data.block + 1, values, length);
1646 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1647 I2C_SMBUS_WRITE, command,
1648 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1649}
David Brownellc0564602007-05-01 23:26:31 +02001650EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001651
David Brownell438d6c22006-12-10 21:21:31 +01001652/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001654static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001656 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 union i2c_smbus_data * data)
1658{
1659 /* So we need to generate a series of msgs. In the case of writing, we
1660 need to use only one message; when reading, we need two. We initialize
1661 most things with sane defaults, to keep the code below somewhat
1662 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001663 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1664 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001666 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1668 };
1669 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001670 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001671 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 msgbuf0[0] = command;
1674 switch(size) {
1675 case I2C_SMBUS_QUICK:
1676 msg[0].len = 0;
1677 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001678 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1679 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 num = 1;
1681 break;
1682 case I2C_SMBUS_BYTE:
1683 if (read_write == I2C_SMBUS_READ) {
1684 /* Special case: only a read! */
1685 msg[0].flags = I2C_M_RD | flags;
1686 num = 1;
1687 }
1688 break;
1689 case I2C_SMBUS_BYTE_DATA:
1690 if (read_write == I2C_SMBUS_READ)
1691 msg[1].len = 1;
1692 else {
1693 msg[0].len = 2;
1694 msgbuf0[1] = data->byte;
1695 }
1696 break;
1697 case I2C_SMBUS_WORD_DATA:
1698 if (read_write == I2C_SMBUS_READ)
1699 msg[1].len = 2;
1700 else {
1701 msg[0].len=3;
1702 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001703 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 break;
1706 case I2C_SMBUS_PROC_CALL:
1707 num = 2; /* Special case */
1708 read_write = I2C_SMBUS_READ;
1709 msg[0].len = 3;
1710 msg[1].len = 2;
1711 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001712 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001716 msg[1].flags |= I2C_M_RECV_LEN;
1717 msg[1].len = 1; /* block length will be added by
1718 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 } else {
1720 msg[0].len = data->block[0] + 2;
1721 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001722 dev_err(&adapter->dev,
1723 "Invalid block write size %d\n",
1724 data->block[0]);
1725 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001727 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 msgbuf0[i] = data->block[i-1];
1729 }
1730 break;
1731 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001732 num = 2; /* Another special case */
1733 read_write = I2C_SMBUS_READ;
1734 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001735 dev_err(&adapter->dev,
1736 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001737 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001738 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001739 }
1740 msg[0].len = data->block[0] + 2;
1741 for (i = 1; i < msg[0].len; i++)
1742 msgbuf0[i] = data->block[i-1];
1743 msg[1].flags |= I2C_M_RECV_LEN;
1744 msg[1].len = 1; /* block length will be added by
1745 the underlying bus driver */
1746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 case I2C_SMBUS_I2C_BLOCK_DATA:
1748 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001749 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 } else {
1751 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001752 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001753 dev_err(&adapter->dev,
1754 "Invalid block write size %d\n",
1755 data->block[0]);
1756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758 for (i = 1; i <= data->block[0]; i++)
1759 msgbuf0[i] = data->block[i];
1760 }
1761 break;
1762 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001763 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1764 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
Jean Delvare421ef472005-10-26 21:28:55 +02001767 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1768 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1769 if (i) {
1770 /* Compute PEC if first message is a write */
1771 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001772 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001773 i2c_smbus_add_pec(&msg[0]);
1774 else /* Write followed by read */
1775 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1776 }
1777 /* Ask for PEC if last message is a read */
1778 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001779 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001780 }
1781
David Brownell24a5bb72008-07-14 22:38:23 +02001782 status = i2c_transfer(adapter, msg, num);
1783 if (status < 0)
1784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Jean Delvare421ef472005-10-26 21:28:55 +02001786 /* Check PEC if last message is a read */
1787 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001788 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1789 if (status < 0)
1790 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001791 }
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (read_write == I2C_SMBUS_READ)
1794 switch(size) {
1795 case I2C_SMBUS_BYTE:
1796 data->byte = msgbuf0[0];
1797 break;
1798 case I2C_SMBUS_BYTE_DATA:
1799 data->byte = msgbuf1[0];
1800 break;
David Brownell438d6c22006-12-10 21:21:31 +01001801 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 case I2C_SMBUS_PROC_CALL:
1803 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1804 break;
1805 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001806 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 data->block[i+1] = msgbuf1[i];
1808 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001809 case I2C_SMBUS_BLOCK_DATA:
1810 case I2C_SMBUS_BLOCK_PROC_CALL:
1811 for (i = 0; i < msgbuf1[0] + 1; i++)
1812 data->block[i] = msgbuf1[i];
1813 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815 return 0;
1816}
1817
David Brownella1cdeda2008-07-14 22:38:24 +02001818/**
1819 * i2c_smbus_xfer - execute SMBus protocol operations
1820 * @adapter: Handle to I2C bus
1821 * @addr: Address of SMBus slave on that bus
1822 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1823 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1824 * @command: Byte interpreted by slave, for protocols which use such bytes
1825 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1826 * @data: Data to be read or written
1827 *
1828 * This executes an SMBus protocol operation, and returns a negative
1829 * errno code else zero on success.
1830 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001831s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001832 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001833 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001835 unsigned long orig_jiffies;
1836 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001842 mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001843
1844 /* Retry automatically on arbitration loss */
1845 orig_jiffies = jiffies;
1846 for (res = 0, try = 0; try <= adapter->retries; try++) {
1847 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1848 read_write, command,
1849 protocol, data);
1850 if (res != -EAGAIN)
1851 break;
1852 if (time_after(jiffies,
1853 orig_jiffies + adapter->timeout))
1854 break;
1855 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001856 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 } else
1858 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001859 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return res;
1862}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1866MODULE_DESCRIPTION("I2C-Bus main module");
1867MODULE_LICENSE("GPL");