blob: eb084fa0df832836d4429412ef8809ce43a76930 [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 Delvare99cd8e22009-06-19 16:58:20 +020041/* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020042 that device detection, deletion of detected devices, and attach_adapter
43 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010044static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static DEFINE_IDR(i2c_adapter_idr);
Jean Delvare99cd8e22009-06-19 16:58:20 +020046static LIST_HEAD(userspace_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jean Delvaref8a227e2009-06-19 16:58:18 +020048static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020049static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010050
51/* ------------------------------------------------------------------------- */
52
Jean Delvared2653e92008-04-29 23:11:39 +020053static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
54 const struct i2c_client *client)
55{
56 while (id->name[0]) {
57 if (strcmp(client->name, id->name) == 0)
58 return id;
59 id++;
60 }
61 return NULL;
62}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int i2c_device_match(struct device *dev, struct device_driver *drv)
65{
David Brownell7b4fbc52007-05-01 23:26:30 +020066 struct i2c_client *client = to_i2c_client(dev);
67 struct i2c_driver *driver = to_i2c_driver(drv);
68
Jean Delvared2653e92008-04-29 23:11:39 +020069 /* match on an id table if there is one */
70 if (driver->id_table)
71 return i2c_match_id(driver->id_table, client) != NULL;
72
Jean Delvareeb8a7902008-05-18 20:49:41 +020073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
David Brownell7b4fbc52007-05-01 23:26:30 +020076#ifdef CONFIG_HOTPLUG
77
78/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020079static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020080{
81 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020082
Jean Delvareeb8a7902008-05-18 20:49:41 +020083 if (add_uevent_var(env, "MODALIAS=%s%s",
84 I2C_MODULE_PREFIX, client->name))
85 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020086 dev_dbg(dev, "uevent\n");
87 return 0;
88}
89
90#else
91#define i2c_device_uevent NULL
92#endif /* CONFIG_HOTPLUG */
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int i2c_device_probe(struct device *dev)
95{
David Brownell7b4fbc52007-05-01 23:26:30 +020096 struct i2c_client *client = to_i2c_client(dev);
97 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +010098 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +020099
Jean Delvaree0457442008-07-14 22:38:30 +0200100 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200101 return -ENODEV;
102 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200103 if (!device_can_wakeup(&client->dev))
104 device_init_wakeup(&client->dev,
105 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200106 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200107
Jean Delvaree0457442008-07-14 22:38:30 +0200108 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100109 if (status)
110 client->driver = NULL;
111 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114static int i2c_device_remove(struct device *dev)
115{
David Brownella1d9e6e2007-05-01 23:26:30 +0200116 struct i2c_client *client = to_i2c_client(dev);
117 struct i2c_driver *driver;
118 int status;
119
120 if (!dev->driver)
121 return 0;
122
123 driver = to_i2c_driver(dev->driver);
124 if (driver->remove) {
125 dev_dbg(dev, "remove\n");
126 status = driver->remove(client);
127 } else {
128 dev->driver = NULL;
129 status = 0;
130 }
131 if (status == 0)
132 client->driver = NULL;
133 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
David Brownellf37dd802007-02-13 22:09:00 +0100136static void i2c_device_shutdown(struct device *dev)
137{
138 struct i2c_driver *driver;
139
140 if (!dev->driver)
141 return;
142 driver = to_i2c_driver(dev->driver);
143 if (driver->shutdown)
144 driver->shutdown(to_i2c_client(dev));
145}
146
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100147static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100148{
149 struct i2c_driver *driver;
150
151 if (!dev->driver)
152 return 0;
153 driver = to_i2c_driver(dev->driver);
154 if (!driver->suspend)
155 return 0;
156 return driver->suspend(to_i2c_client(dev), mesg);
157}
158
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100159static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100160{
161 struct i2c_driver *driver;
162
163 if (!dev->driver)
164 return 0;
165 driver = to_i2c_driver(dev->driver);
166 if (!driver->resume)
167 return 0;
168 return driver->resume(to_i2c_client(dev));
169}
170
David Brownell9c1600e2007-05-01 23:26:31 +0200171static void i2c_client_dev_release(struct device *dev)
172{
173 kfree(to_i2c_client(dev));
174}
175
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100176static ssize_t
177show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200178{
179 struct i2c_client *client = to_i2c_client(dev);
180 return sprintf(buf, "%s\n", client->name);
181}
182
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100183static ssize_t
184show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200185{
186 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200187 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200188}
189
190static struct device_attribute i2c_dev_attrs[] = {
191 __ATTR(name, S_IRUGO, show_client_name, NULL),
192 /* modalias helps coldplug: modprobe $(cat .../modalias) */
193 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
194 { },
195};
196
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200197struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100198 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200199 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100200 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200201 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100202 .probe = i2c_device_probe,
203 .remove = i2c_device_remove,
204 .shutdown = i2c_device_shutdown,
205 .suspend = i2c_device_suspend,
206 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000207};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200208EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000209
David Brownell9b766b82008-01-27 18:14:51 +0100210
211/**
212 * i2c_verify_client - return parameter as i2c_client, or NULL
213 * @dev: device, probably from some driver model iterator
214 *
215 * When traversing the driver model tree, perhaps using driver model
216 * iterators like @device_for_each_child(), you can't assume very much
217 * about the nodes you find. Use this function to avoid oopses caused
218 * by wrongly treating some non-I2C device as an i2c_client.
219 */
220struct i2c_client *i2c_verify_client(struct device *dev)
221{
222 return (dev->bus == &i2c_bus_type)
223 ? to_i2c_client(dev)
224 : NULL;
225}
226EXPORT_SYMBOL(i2c_verify_client);
227
228
David Brownell9c1600e2007-05-01 23:26:31 +0200229/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200230 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200231 * @adap: the adapter managing the device
232 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200233 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200234 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200235 * Create an i2c device. Binding is handled through driver model
236 * probe()/remove() methods. A driver may be bound to this device when we
237 * return from this function, or any later moment (e.g. maybe hotplugging will
238 * load the driver module). This call is not appropriate for use by mainboard
239 * initialization logic, which usually runs during an arch_initcall() long
240 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200241 *
242 * This returns the new i2c client, which may be saved for later use with
243 * i2c_unregister_device(); or NULL to indicate an error.
244 */
245struct i2c_client *
246i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
247{
248 struct i2c_client *client;
249 int status;
250
251 client = kzalloc(sizeof *client, GFP_KERNEL);
252 if (!client)
253 return NULL;
254
255 client->adapter = adap;
256
257 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200258
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200259 if (info->archdata)
260 client->dev.archdata = *info->archdata;
261
Marc Pignatee354252008-08-28 08:33:22 +0200262 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200263 client->addr = info->addr;
264 client->irq = info->irq;
265
David Brownell9c1600e2007-05-01 23:26:31 +0200266 strlcpy(client->name, info->type, sizeof(client->name));
267
Jean Delvaref8a227e2009-06-19 16:58:18 +0200268 /* Check for address business */
269 status = i2c_check_addr(adap, client->addr);
270 if (status)
271 goto out_err;
272
273 client->dev.parent = &client->adapter->dev;
274 client->dev.bus = &i2c_bus_type;
Jean Delvare1e40ac12009-06-19 16:58:19 +0200275 client->dev.release = i2c_client_dev_release;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200276
277 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
278 client->addr);
279 status = device_register(&client->dev);
280 if (status)
281 goto out_err;
282
Jean Delvaref8a227e2009-06-19 16:58:18 +0200283 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{
David Brownella1d9e6e2007-05-01 23:26:30 +0200304 device_unregister(&client->dev);
305}
David Brownell9c1600e2007-05-01 23:26:31 +0200306EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200307
308
Jean Delvare60b129d2008-05-11 20:37:06 +0200309static const struct i2c_device_id dummy_id[] = {
310 { "dummy", 0 },
311 { },
312};
313
Jean Delvared2653e92008-04-29 23:11:39 +0200314static int dummy_probe(struct i2c_client *client,
315 const struct i2c_device_id *id)
316{
317 return 0;
318}
319
320static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100321{
322 return 0;
323}
324
325static struct i2c_driver dummy_driver = {
326 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200327 .probe = dummy_probe,
328 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200329 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100330};
331
332/**
333 * i2c_new_dummy - return a new i2c device bound to a dummy driver
334 * @adapter: the adapter managing the device
335 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100336 * Context: can sleep
337 *
338 * This returns an I2C client bound to the "dummy" driver, intended for use
339 * with devices that consume multiple addresses. Examples of such chips
340 * include various EEPROMS (like 24c04 and 24c08 models).
341 *
342 * These dummy devices have two main uses. First, most I2C and SMBus calls
343 * except i2c_transfer() need a client handle; the dummy will be that handle.
344 * And second, this prevents the specified address from being bound to a
345 * different driver.
346 *
347 * This returns the new i2c client, which should be saved for later use with
348 * i2c_unregister_device(); or NULL to indicate an error.
349 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100350struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100351{
352 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200353 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100354 };
355
David Brownelle9f13732008-01-27 18:14:52 +0100356 return i2c_new_device(adapter, &info);
357}
358EXPORT_SYMBOL_GPL(i2c_new_dummy);
359
David Brownellf37dd802007-02-13 22:09:00 +0100360/* ------------------------------------------------------------------------- */
361
David Brownell16ffadf2007-05-01 23:26:28 +0200362/* I2C bus adapters -- one roots each I2C or SMBUS segment */
363
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200364static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
David Brownellef2c83212007-05-01 23:26:28 +0200366 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 complete(&adap->dev_released);
368}
369
David Brownell16ffadf2007-05-01 23:26:28 +0200370static ssize_t
371show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
David Brownellef2c83212007-05-01 23:26:28 +0200373 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return sprintf(buf, "%s\n", adap->name);
375}
David Brownell16ffadf2007-05-01 23:26:28 +0200376
Jean Delvare99cd8e22009-06-19 16:58:20 +0200377/*
378 * Let users instantiate I2C devices through sysfs. This can be used when
379 * platform initialization code doesn't contain the proper data for
380 * whatever reason. Also useful for drivers that do device detection and
381 * detection fails, either because the device uses an unexpected address,
382 * or this is a compatible device with different ID register values.
383 *
384 * Parameter checking may look overzealous, but we really don't want
385 * the user to provide incorrect parameters.
386 */
387static ssize_t
388i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
389 const char *buf, size_t count)
390{
391 struct i2c_adapter *adap = to_i2c_adapter(dev);
392 struct i2c_board_info info;
393 struct i2c_client *client;
394 char *blank, end;
395 int res;
396
397 dev_warn(dev, "The new_device interface is still experimental "
398 "and may change in a near future\n");
399 memset(&info, 0, sizeof(struct i2c_board_info));
400
401 blank = strchr(buf, ' ');
402 if (!blank) {
403 dev_err(dev, "%s: Missing parameters\n", "new_device");
404 return -EINVAL;
405 }
406 if (blank - buf > I2C_NAME_SIZE - 1) {
407 dev_err(dev, "%s: Invalid device name\n", "new_device");
408 return -EINVAL;
409 }
410 memcpy(info.type, buf, blank - buf);
411
412 /* Parse remaining parameters, reject extra parameters */
413 res = sscanf(++blank, "%hi%c", &info.addr, &end);
414 if (res < 1) {
415 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
416 return -EINVAL;
417 }
418 if (res > 1 && end != '\n') {
419 dev_err(dev, "%s: Extra parameters\n", "new_device");
420 return -EINVAL;
421 }
422
423 if (info.addr < 0x03 || info.addr > 0x77) {
424 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
425 info.addr);
426 return -EINVAL;
427 }
428
429 client = i2c_new_device(adap, &info);
430 if (!client)
431 return -EEXIST;
432
433 /* Keep track of the added device */
434 mutex_lock(&core_lock);
435 list_add_tail(&client->detected, &userspace_devices);
436 mutex_unlock(&core_lock);
437 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
438 info.type, info.addr);
439
440 return count;
441}
442
443/*
444 * And of course let the users delete the devices they instantiated, if
445 * they got it wrong. This interface can only be used to delete devices
446 * instantiated by i2c_sysfs_new_device above. This guarantees that we
447 * don't delete devices to which some kernel code still has references.
448 *
449 * Parameter checking may look overzealous, but we really don't want
450 * the user to delete the wrong device.
451 */
452static ssize_t
453i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
454 const char *buf, size_t count)
455{
456 struct i2c_adapter *adap = to_i2c_adapter(dev);
457 struct i2c_client *client, *next;
458 unsigned short addr;
459 char end;
460 int res;
461
462 /* Parse parameters, reject extra parameters */
463 res = sscanf(buf, "%hi%c", &addr, &end);
464 if (res < 1) {
465 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
466 return -EINVAL;
467 }
468 if (res > 1 && end != '\n') {
469 dev_err(dev, "%s: Extra parameters\n", "delete_device");
470 return -EINVAL;
471 }
472
473 /* Make sure the device was added through sysfs */
474 res = -ENOENT;
475 mutex_lock(&core_lock);
476 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
477 if (client->addr == addr && client->adapter == adap) {
478 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
479 "delete_device", client->name, client->addr);
480
481 list_del(&client->detected);
482 i2c_unregister_device(client);
483 res = count;
484 break;
485 }
486 }
487 mutex_unlock(&core_lock);
488
489 if (res < 0)
490 dev_err(dev, "%s: Can't find device in list\n",
491 "delete_device");
492 return res;
493}
494
David Brownell16ffadf2007-05-01 23:26:28 +0200495static struct device_attribute i2c_adapter_attrs[] = {
496 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
Jean Delvare99cd8e22009-06-19 16:58:20 +0200497 __ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device),
498 __ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device),
David Brownell16ffadf2007-05-01 23:26:28 +0200499 { },
500};
501
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200502static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200503 .owner = THIS_MODULE,
504 .name = "i2c-adapter",
505 .dev_attrs = i2c_adapter_attrs,
506};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
David Brownell9c1600e2007-05-01 23:26:31 +0200508static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
509{
510 struct i2c_devinfo *devinfo;
511
512 mutex_lock(&__i2c_board_lock);
513 list_for_each_entry(devinfo, &__i2c_board_list, list) {
514 if (devinfo->busnum == adapter->nr
515 && !i2c_new_device(adapter,
516 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100517 dev_err(&adapter->dev,
518 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200519 devinfo->board_info.addr);
520 }
521 mutex_unlock(&__i2c_board_lock);
522}
523
Jean Delvare026526f2008-01-27 18:14:49 +0100524static int i2c_do_add_adapter(struct device_driver *d, void *data)
525{
526 struct i2c_driver *driver = to_i2c_driver(d);
527 struct i2c_adapter *adap = data;
528
Jean Delvare4735c982008-07-14 22:38:36 +0200529 /* Detect supported devices on that bus, and instantiate them */
530 i2c_detect(adap, driver);
531
532 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100533 if (driver->attach_adapter) {
534 /* We ignore the return code; if it fails, too bad */
535 driver->attach_adapter(adap);
536 }
537 return 0;
538}
539
David Brownell6e13e642007-05-01 23:26:31 +0200540static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Jean Delvare026526f2008-01-27 18:14:49 +0100542 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
David Brownell1d0b19c2008-10-14 17:30:05 +0200544 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200545 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
546 res = -EAGAIN;
547 goto out_list;
548 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200549
Ingo Molnar5c085d32006-01-18 23:16:04 +0100550 mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Jean Delvare8fcfef62009-03-28 21:34:43 +0100552 /* Set default timeout to 1 second if not already set */
553 if (adap->timeout == 0)
554 adap->timeout = HZ;
555
Kay Sievers27d9c182009-01-07 14:29:16 +0100556 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200558 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200559 res = device_register(&adap->dev);
560 if (res)
561 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200563 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
564
Jean Delvare729d6dd2009-06-19 16:58:18 +0200565 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200566 if (adap->nr < __i2c_first_dynamic_bus_num)
567 i2c_scan_static_board_info(adap);
568
Jean Delvare4735c982008-07-14 22:38:36 +0200569 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200570 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100571 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
572 i2c_do_add_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100573 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200574
575 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200576
Jean Delvareb119c6c2006-08-15 18:26:30 +0200577out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200578 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200579 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200580 mutex_unlock(&core_lock);
581 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
David Brownell6e13e642007-05-01 23:26:31 +0200584/**
585 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
586 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200587 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200588 *
589 * This routine is used to declare an I2C adapter when its bus number
590 * doesn't matter. Examples: for I2C adapters dynamically added by
591 * USB links or PCI plugin cards.
592 *
593 * When this returns zero, a new bus number was allocated and stored
594 * in adap->nr, and the specified adapter became available for clients.
595 * Otherwise, a negative errno value is returned.
596 */
597int i2c_add_adapter(struct i2c_adapter *adapter)
598{
599 int id, res = 0;
600
601retry:
602 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
603 return -ENOMEM;
604
Jean Delvarecaada322008-01-27 18:14:49 +0100605 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200606 /* "above" here means "above or equal to", sigh */
607 res = idr_get_new_above(&i2c_adapter_idr, adapter,
608 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100609 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200610
611 if (res < 0) {
612 if (res == -EAGAIN)
613 goto retry;
614 return res;
615 }
616
617 adapter->nr = id;
618 return i2c_register_adapter(adapter);
619}
620EXPORT_SYMBOL(i2c_add_adapter);
621
622/**
623 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
624 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200625 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200626 *
627 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100628 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
629 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200630 * is used to properly configure I2C devices.
631 *
632 * If no devices have pre-been declared for this bus, then be sure to
633 * register the adapter before any dynamically allocated ones. Otherwise
634 * the required bus ID may not be available.
635 *
636 * When this returns zero, the specified adapter became available for
637 * clients using the bus number provided in adap->nr. Also, the table
638 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
639 * and the appropriate driver model device nodes are created. Otherwise, a
640 * negative errno value is returned.
641 */
642int i2c_add_numbered_adapter(struct i2c_adapter *adap)
643{
644 int id;
645 int status;
646
647 if (adap->nr & ~MAX_ID_MASK)
648 return -EINVAL;
649
650retry:
651 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
652 return -ENOMEM;
653
Jean Delvarecaada322008-01-27 18:14:49 +0100654 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200655 /* "above" here means "above or equal to", sigh;
656 * we need the "equal to" result to force the result
657 */
658 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
659 if (status == 0 && id != adap->nr) {
660 status = -EBUSY;
661 idr_remove(&i2c_adapter_idr, id);
662 }
Jean Delvarecaada322008-01-27 18:14:49 +0100663 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200664 if (status == -EAGAIN)
665 goto retry;
666
667 if (status == 0)
668 status = i2c_register_adapter(adap);
669 return status;
670}
671EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
672
Jean Delvare026526f2008-01-27 18:14:49 +0100673static int i2c_do_del_adapter(struct device_driver *d, void *data)
674{
675 struct i2c_driver *driver = to_i2c_driver(d);
676 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200677 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100678 int res;
679
Jean Delvareacec2112009-03-28 21:34:40 +0100680 /* Remove the devices we created ourselves as the result of hardware
681 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200682 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
683 if (client->adapter == adapter) {
684 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
685 client->name, client->addr);
686 list_del(&client->detected);
687 i2c_unregister_device(client);
688 }
689 }
690
Jean Delvare026526f2008-01-27 18:14:49 +0100691 if (!driver->detach_adapter)
692 return 0;
693 res = driver->detach_adapter(adapter);
694 if (res)
695 dev_err(&adapter->dev, "detach_adapter failed (%d) "
696 "for driver [%s]\n", res, driver->driver.name);
697 return res;
698}
699
Jean Delvaree549c2b2009-06-19 16:58:19 +0200700static int __unregister_client(struct device *dev, void *dummy)
701{
702 struct i2c_client *client = i2c_verify_client(dev);
703 if (client)
704 i2c_unregister_device(client);
705 return 0;
706}
707
David Brownelld64f73b2007-07-12 14:12:28 +0200708/**
709 * i2c_del_adapter - unregister I2C adapter
710 * @adap: the adapter being unregistered
711 * Context: can sleep
712 *
713 * This unregisters an I2C adapter which was previously registered
714 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716int i2c_del_adapter(struct i2c_adapter *adap)
717{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200719 struct i2c_adapter *found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200722 mutex_lock(&core_lock);
723 found = idr_find(&i2c_adapter_idr, adap->nr);
724 mutex_unlock(&core_lock);
725 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200726 pr_debug("i2c-core: attempting to delete unregistered "
727 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200728 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Jean Delvare026526f2008-01-27 18:14:49 +0100731 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200732 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100733 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
734 i2c_do_del_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200735 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100736 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200737 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Jean Delvaree549c2b2009-06-19 16:58:19 +0200739 /* Detach any active clients. This can't fail, thus we do not
740 checking the returned value. */
741 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /* clean up the sysfs representation */
744 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* wait for sysfs to drop all references */
748 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
David Brownell6e13e642007-05-01 23:26:31 +0200750 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200751 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200753 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200755 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200757 /* Clear the device structure in case this adapter is ever going to be
758 added again */
759 memset(&adap->dev, 0, sizeof(adap->dev));
760
Jean Delvare35fc37f2009-06-19 16:58:19 +0200761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
David Brownellc0564602007-05-01 23:26:31 +0200763EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765
David Brownell7b4fbc52007-05-01 23:26:30 +0200766/* ------------------------------------------------------------------------- */
767
Dave Young7f101a92008-07-14 22:38:19 +0200768static int __attach_adapter(struct device *dev, void *data)
769{
770 struct i2c_adapter *adapter = to_i2c_adapter(dev);
771 struct i2c_driver *driver = data;
772
Jean Delvare4735c982008-07-14 22:38:36 +0200773 i2c_detect(adapter, driver);
774
775 /* Legacy drivers scan i2c busses directly */
776 if (driver->attach_adapter)
777 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200778
779 return 0;
780}
781
David Brownell7b4fbc52007-05-01 23:26:30 +0200782/*
783 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200784 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
786
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800787int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100789 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
David Brownell1d0b19c2008-10-14 17:30:05 +0200791 /* Can't register until after driver model init */
792 if (unlikely(WARN_ON(!i2c_bus_type.p)))
793 return -EAGAIN;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800796 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jean Delvare729d6dd2009-06-19 16:58:18 +0200799 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200800 * will have called probe() for all matching-but-unbound devices.
801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 res = driver_register(&driver->driver);
803 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100804 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100805
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100806 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Jean Delvare4735c982008-07-14 22:38:36 +0200808 INIT_LIST_HEAD(&driver->clients);
809 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200810 mutex_lock(&core_lock);
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400811 class_for_each_device(&i2c_adapter_class, NULL, driver,
812 __attach_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100813 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200814
Jean Delvare7eebcb72006-02-05 23:28:21 +0100815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800817EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Dave Young7f101a92008-07-14 22:38:19 +0200819static int __detach_adapter(struct device *dev, void *data)
820{
821 struct i2c_adapter *adapter = to_i2c_adapter(dev);
822 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200823 struct i2c_client *client, *_n;
824
Jean Delvareacec2112009-03-28 21:34:40 +0100825 /* Remove the devices we created ourselves as the result of hardware
826 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200827 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
828 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
829 client->name, client->addr);
830 list_del(&client->detected);
831 i2c_unregister_device(client);
832 }
833
Dave Young7f101a92008-07-14 22:38:19 +0200834 if (driver->detach_adapter) {
835 if (driver->detach_adapter(adapter))
836 dev_err(&adapter->dev,
837 "detach_adapter failed for driver [%s]\n",
838 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200839 }
840
841 return 0;
842}
843
David Brownella1d9e6e2007-05-01 23:26:30 +0200844/**
845 * i2c_del_driver - unregister I2C driver
846 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200847 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200848 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200849void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Jean Delvarecaada322008-01-27 18:14:49 +0100851 mutex_lock(&core_lock);
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400852 class_for_each_device(&i2c_adapter_class, NULL, driver,
853 __detach_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200854 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100857 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
David Brownellc0564602007-05-01 23:26:31 +0200859EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
David Brownell7b4fbc52007-05-01 23:26:30 +0200861/* ------------------------------------------------------------------------- */
862
David Brownell9b766b82008-01-27 18:14:51 +0100863static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
David Brownell9b766b82008-01-27 18:14:51 +0100865 struct i2c_client *client = i2c_verify_client(dev);
866 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
David Brownell9b766b82008-01-27 18:14:51 +0100868 if (client && client->addr == addr)
869 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 0;
871}
872
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100873static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
David Brownell9b766b82008-01-27 18:14:51 +0100875 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Jean Delvaree48d3312008-01-27 18:14:48 +0100878/**
879 * i2c_use_client - increments the reference count of the i2c client structure
880 * @client: the client being referenced
881 *
882 * Each live reference to a client should be refcounted. The driver model does
883 * that automatically as part of driver binding, so that most drivers don't
884 * need to do this explicitly: they hold a reference until they're unbound
885 * from the device.
886 *
887 * A pointer to the client with the incremented reference counter is returned.
888 */
889struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
David Brownell6ea438e2008-07-14 22:38:24 +0200891 if (client && get_device(&client->dev))
892 return client;
893 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
David Brownellc0564602007-05-01 23:26:31 +0200895EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Jean Delvaree48d3312008-01-27 18:14:48 +0100897/**
898 * i2c_release_client - release a use of the i2c client structure
899 * @client: the client being no longer referenced
900 *
901 * Must be called when a user of a client is finished with it.
902 */
903void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
David Brownell6ea438e2008-07-14 22:38:24 +0200905 if (client)
906 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
David Brownellc0564602007-05-01 23:26:31 +0200908EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
David Brownell9b766b82008-01-27 18:14:51 +0100910struct i2c_cmd_arg {
911 unsigned cmd;
912 void *arg;
913};
914
915static int i2c_cmd(struct device *dev, void *_arg)
916{
917 struct i2c_client *client = i2c_verify_client(dev);
918 struct i2c_cmd_arg *arg = _arg;
919
920 if (client && client->driver && client->driver->command)
921 client->driver->command(client, arg->cmd, arg->arg);
922 return 0;
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
926{
David Brownell9b766b82008-01-27 18:14:51 +0100927 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
David Brownell9b766b82008-01-27 18:14:51 +0100929 cmd_arg.cmd = cmd;
930 cmd_arg.arg = arg;
931 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
David Brownellc0564602007-05-01 23:26:31 +0200933EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935static int __init i2c_init(void)
936{
937 int retval;
938
939 retval = bus_register(&i2c_bus_type);
940 if (retval)
941 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100942 retval = class_register(&i2c_adapter_class);
943 if (retval)
944 goto bus_err;
945 retval = i2c_add_driver(&dummy_driver);
946 if (retval)
947 goto class_err;
948 return 0;
949
950class_err:
951 class_unregister(&i2c_adapter_class);
952bus_err:
953 bus_unregister(&i2c_bus_type);
954 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957static void __exit i2c_exit(void)
958{
David Brownelle9f13732008-01-27 18:14:52 +0100959 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 bus_unregister(&i2c_bus_type);
962}
963
David Brownella10f9e72008-10-14 17:30:06 +0200964/* We must initialize early, because some subsystems register i2c drivers
965 * in subsys_initcall() code, but are linked (and initialized) before i2c.
966 */
967postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968module_exit(i2c_exit);
969
970/* ----------------------------------------------------
971 * the functional interface to the i2c busses.
972 * ----------------------------------------------------
973 */
974
David Brownella1cdeda2008-07-14 22:38:24 +0200975/**
976 * i2c_transfer - execute a single or combined I2C message
977 * @adap: Handle to I2C bus
978 * @msgs: One or more messages to execute before STOP is issued to
979 * terminate the operation; each message begins with a START.
980 * @num: Number of messages to be executed.
981 *
982 * Returns negative errno, else the number of messages executed.
983 *
984 * Note that there is no requirement that each message be sent to
985 * the same slave address, although that is the most common model.
986 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100987int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Clifford Wolf66b650f2009-06-15 18:01:46 +0200989 unsigned long orig_jiffies;
990 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
David Brownella1cdeda2008-07-14 22:38:24 +0200992 /* REVISIT the fault reporting model here is weak:
993 *
994 * - When we get an error after receiving N bytes from a slave,
995 * there is no way to report "N".
996 *
997 * - When we get a NAK after transmitting N bytes to a slave,
998 * there is no way to report "N" ... or to let the master
999 * continue executing the rest of this combined message, if
1000 * that's the appropriate response.
1001 *
1002 * - When for example "num" is two and we successfully complete
1003 * the first message but get an error part way through the
1004 * second, it's unclear whether that should be reported as
1005 * one (discarding status on the second message) or errno
1006 * (discarding status on the first one).
1007 */
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (adap->algo->master_xfer) {
1010#ifdef DEBUG
1011 for (ret = 0; ret < num; ret++) {
1012 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001013 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1014 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1015 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017#endif
1018
Mike Rapoportcea443a2008-01-27 18:14:50 +01001019 if (in_atomic() || irqs_disabled()) {
1020 ret = mutex_trylock(&adap->bus_lock);
1021 if (!ret)
1022 /* I2C activity is ongoing. */
1023 return -EAGAIN;
1024 } else {
1025 mutex_lock_nested(&adap->bus_lock, adap->level);
1026 }
1027
Clifford Wolf66b650f2009-06-15 18:01:46 +02001028 /* Retry automatically on arbitration loss */
1029 orig_jiffies = jiffies;
1030 for (ret = 0, try = 0; try <= adap->retries; try++) {
1031 ret = adap->algo->master_xfer(adap, msgs, num);
1032 if (ret != -EAGAIN)
1033 break;
1034 if (time_after(jiffies, orig_jiffies + adap->timeout))
1035 break;
1036 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001037 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 return ret;
1040 } else {
1041 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001042 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044}
David Brownellc0564602007-05-01 23:26:31 +02001045EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
David Brownella1cdeda2008-07-14 22:38:24 +02001047/**
1048 * i2c_master_send - issue a single I2C message in master transmit mode
1049 * @client: Handle to slave device
1050 * @buf: Data that will be written to the slave
1051 * @count: How many bytes to write
1052 *
1053 * Returns negative errno, or else the number of bytes written.
1054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1056{
1057 int ret;
1058 struct i2c_adapter *adap=client->adapter;
1059 struct i2c_msg msg;
1060
Jean Delvare815f55f2005-05-07 22:58:46 +02001061 msg.addr = client->addr;
1062 msg.flags = client->flags & I2C_M_TEN;
1063 msg.len = count;
1064 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001065
Jean Delvare815f55f2005-05-07 22:58:46 +02001066 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Jean Delvare815f55f2005-05-07 22:58:46 +02001068 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1069 transmitted, else error code. */
1070 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
David Brownellc0564602007-05-01 23:26:31 +02001072EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
David Brownella1cdeda2008-07-14 22:38:24 +02001074/**
1075 * i2c_master_recv - issue a single I2C message in master receive mode
1076 * @client: Handle to slave device
1077 * @buf: Where to store data read from slave
1078 * @count: How many bytes to read
1079 *
1080 * Returns negative errno, or else the number of bytes read.
1081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1083{
1084 struct i2c_adapter *adap=client->adapter;
1085 struct i2c_msg msg;
1086 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Jean Delvare815f55f2005-05-07 22:58:46 +02001088 msg.addr = client->addr;
1089 msg.flags = client->flags & I2C_M_TEN;
1090 msg.flags |= I2C_M_RD;
1091 msg.len = count;
1092 msg.buf = buf;
1093
1094 ret = i2c_transfer(adap, &msg, 1);
1095
1096 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1097 transmitted, else error code. */
1098 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
David Brownellc0564602007-05-01 23:26:31 +02001100EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/* ----------------------------------------------------
1103 * the i2c address scanning function
1104 * Will not work for 10-bit addresses!
1105 * ----------------------------------------------------
1106 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001107
Jean Delvare4735c982008-07-14 22:38:36 +02001108static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1109 struct i2c_driver *driver)
1110{
1111 struct i2c_board_info info;
1112 struct i2c_adapter *adapter = temp_client->adapter;
1113 int addr = temp_client->addr;
1114 int err;
1115
1116 /* Make sure the address is valid */
1117 if (addr < 0x03 || addr > 0x77) {
1118 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1119 addr);
1120 return -EINVAL;
1121 }
1122
1123 /* Skip if already in use */
1124 if (i2c_check_addr(adapter, addr))
1125 return 0;
1126
1127 /* Make sure there is something at this address, unless forced */
1128 if (kind < 0) {
1129 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1130 I2C_SMBUS_QUICK, NULL) < 0)
1131 return 0;
1132
1133 /* prevent 24RF08 corruption */
1134 if ((addr & ~0x0f) == 0x50)
1135 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1136 I2C_SMBUS_QUICK, NULL);
1137 }
1138
1139 /* Finally call the custom detection function */
1140 memset(&info, 0, sizeof(struct i2c_board_info));
1141 info.addr = addr;
1142 err = driver->detect(temp_client, kind, &info);
1143 if (err) {
1144 /* -ENODEV is returned if the detection fails. We catch it
1145 here as this isn't an error. */
1146 return err == -ENODEV ? 0 : err;
1147 }
1148
1149 /* Consistency check */
1150 if (info.type[0] == '\0') {
1151 dev_err(&adapter->dev, "%s detection function provided "
1152 "no name for 0x%x\n", driver->driver.name,
1153 addr);
1154 } else {
1155 struct i2c_client *client;
1156
1157 /* Detection succeeded, instantiate the device */
1158 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1159 info.type, info.addr);
1160 client = i2c_new_device(adapter, &info);
1161 if (client)
1162 list_add_tail(&client->detected, &driver->clients);
1163 else
1164 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1165 info.type, info.addr);
1166 }
1167 return 0;
1168}
1169
1170static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1171{
1172 const struct i2c_client_address_data *address_data;
1173 struct i2c_client *temp_client;
1174 int i, err = 0;
1175 int adap_id = i2c_adapter_id(adapter);
1176
1177 address_data = driver->address_data;
1178 if (!driver->detect || !address_data)
1179 return 0;
1180
1181 /* Set up a temporary client to help detect callback */
1182 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1183 if (!temp_client)
1184 return -ENOMEM;
1185 temp_client->adapter = adapter;
1186
1187 /* Force entries are done first, and are not affected by ignore
1188 entries */
1189 if (address_data->forces) {
1190 const unsigned short * const *forces = address_data->forces;
1191 int kind;
1192
1193 for (kind = 0; forces[kind]; kind++) {
1194 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1195 i += 2) {
1196 if (forces[kind][i] == adap_id
1197 || forces[kind][i] == ANY_I2C_BUS) {
1198 dev_dbg(&adapter->dev, "found force "
1199 "parameter for adapter %d, "
1200 "addr 0x%02x, kind %d\n",
1201 adap_id, forces[kind][i + 1],
1202 kind);
1203 temp_client->addr = forces[kind][i + 1];
1204 err = i2c_detect_address(temp_client,
1205 kind, driver);
1206 if (err)
1207 goto exit_free;
1208 }
1209 }
1210 }
1211 }
1212
Jean Delvare4329cf82008-08-28 08:33:23 +02001213 /* Stop here if the classes do not match */
1214 if (!(adapter->class & driver->class))
1215 goto exit_free;
1216
Jean Delvare4735c982008-07-14 22:38:36 +02001217 /* Stop here if we can't use SMBUS_QUICK */
1218 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1219 if (address_data->probe[0] == I2C_CLIENT_END
1220 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1221 goto exit_free;
1222
1223 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1224 "can't probe for chips\n");
1225 err = -EOPNOTSUPP;
1226 goto exit_free;
1227 }
1228
Jean Delvare4735c982008-07-14 22:38:36 +02001229 /* Probe entries are done second, and are not affected by ignore
1230 entries either */
1231 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1232 if (address_data->probe[i] == adap_id
1233 || address_data->probe[i] == ANY_I2C_BUS) {
1234 dev_dbg(&adapter->dev, "found probe parameter for "
1235 "adapter %d, addr 0x%02x\n", adap_id,
1236 address_data->probe[i + 1]);
1237 temp_client->addr = address_data->probe[i + 1];
1238 err = i2c_detect_address(temp_client, -1, driver);
1239 if (err)
1240 goto exit_free;
1241 }
1242 }
1243
1244 /* Normal entries are done last, unless shadowed by an ignore entry */
1245 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1246 int j, ignore;
1247
1248 ignore = 0;
1249 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1250 j += 2) {
1251 if ((address_data->ignore[j] == adap_id ||
1252 address_data->ignore[j] == ANY_I2C_BUS)
1253 && address_data->ignore[j + 1]
1254 == address_data->normal_i2c[i]) {
1255 dev_dbg(&adapter->dev, "found ignore "
1256 "parameter for adapter %d, "
1257 "addr 0x%02x\n", adap_id,
1258 address_data->ignore[j + 1]);
1259 ignore = 1;
1260 break;
1261 }
1262 }
1263 if (ignore)
1264 continue;
1265
1266 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1267 "addr 0x%02x\n", adap_id,
1268 address_data->normal_i2c[i]);
1269 temp_client->addr = address_data->normal_i2c[i];
1270 err = i2c_detect_address(temp_client, -1, driver);
1271 if (err)
1272 goto exit_free;
1273 }
1274
1275 exit_free:
1276 kfree(temp_client);
1277 return err;
1278}
1279
Jean Delvare12b5053a2007-05-01 23:26:31 +02001280struct i2c_client *
1281i2c_new_probed_device(struct i2c_adapter *adap,
1282 struct i2c_board_info *info,
1283 unsigned short const *addr_list)
1284{
1285 int i;
1286
1287 /* Stop here if the bus doesn't support probing */
1288 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1289 dev_err(&adap->dev, "Probing not supported\n");
1290 return NULL;
1291 }
1292
Jean Delvare12b5053a2007-05-01 23:26:31 +02001293 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1294 /* Check address validity */
1295 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1296 dev_warn(&adap->dev, "Invalid 7-bit address "
1297 "0x%02x\n", addr_list[i]);
1298 continue;
1299 }
1300
1301 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001302 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001303 dev_dbg(&adap->dev, "Address 0x%02x already in "
1304 "use, not probing\n", addr_list[i]);
1305 continue;
1306 }
1307
1308 /* Test address responsiveness
1309 The default probe method is a quick write, but it is known
1310 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1311 and could also irreversibly write-protect some EEPROMs, so
1312 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1313 read instead. Also, some bus drivers don't implement
1314 quick write, so we fallback to a byte read it that case
1315 too. */
1316 if ((addr_list[i] & ~0x07) == 0x30
1317 || (addr_list[i] & ~0x0f) == 0x50
1318 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001319 union i2c_smbus_data data;
1320
Jean Delvare12b5053a2007-05-01 23:26:31 +02001321 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1322 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001323 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001324 break;
1325 } else {
1326 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1327 I2C_SMBUS_WRITE, 0,
1328 I2C_SMBUS_QUICK, NULL) >= 0)
1329 break;
1330 }
1331 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001332
1333 if (addr_list[i] == I2C_CLIENT_END) {
1334 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1335 return NULL;
1336 }
1337
1338 info->addr = addr_list[i];
1339 return i2c_new_device(adap, info);
1340}
1341EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343struct i2c_adapter* i2c_get_adapter(int id)
1344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001346
Jean Delvarecaada322008-01-27 18:14:49 +01001347 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001348 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001349 if (adapter && !try_module_get(adapter->owner))
1350 adapter = NULL;
1351
Jean Delvarecaada322008-01-27 18:14:49 +01001352 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001353 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
David Brownellc0564602007-05-01 23:26:31 +02001355EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357void i2c_put_adapter(struct i2c_adapter *adap)
1358{
1359 module_put(adap->owner);
1360}
David Brownellc0564602007-05-01 23:26:31 +02001361EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363/* The SMBus parts */
1364
David Brownell438d6c22006-12-10 21:21:31 +01001365#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001366static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001371 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 data = data ^ POLY;
1373 data = data << 1;
1374 }
1375 return (u8)(data >> 8);
1376}
1377
Jean Delvare421ef472005-10-26 21:28:55 +02001378/* Incremental CRC8 over count bytes in the array pointed to by p */
1379static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 int i;
1382
1383 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001384 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return crc;
1386}
1387
Jean Delvare421ef472005-10-26 21:28:55 +02001388/* Assume a 7-bit address, which is reasonable for SMBus */
1389static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Jean Delvare421ef472005-10-26 21:28:55 +02001391 /* The address will be sent first */
1392 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1393 pec = i2c_smbus_pec(pec, &addr, 1);
1394
1395 /* The data buffer follows */
1396 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Jean Delvare421ef472005-10-26 21:28:55 +02001399/* Used for write only transactions */
1400static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Jean Delvare421ef472005-10-26 21:28:55 +02001402 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1403 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Jean Delvare421ef472005-10-26 21:28:55 +02001406/* Return <0 on CRC error
1407 If there was a write before this read (most cases) we need to take the
1408 partial CRC from the write part into account.
1409 Note that this function does modify the message (we need to decrease the
1410 message length to hide the CRC byte from the caller). */
1411static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Jean Delvare421ef472005-10-26 21:28:55 +02001413 u8 rpec = msg->buf[--msg->len];
1414 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (rpec != cpec) {
1417 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1418 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001419 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
David Brownell438d6c22006-12-10 21:21:31 +01001421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
David Brownella1cdeda2008-07-14 22:38:24 +02001424/**
1425 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1426 * @client: Handle to slave device
1427 *
1428 * This executes the SMBus "receive byte" protocol, returning negative errno
1429 * else the byte received from the device.
1430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431s32 i2c_smbus_read_byte(struct i2c_client *client)
1432{
1433 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001434 int status;
1435
1436 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1437 I2C_SMBUS_READ, 0,
1438 I2C_SMBUS_BYTE, &data);
1439 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
David Brownellc0564602007-05-01 23:26:31 +02001441EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
David Brownella1cdeda2008-07-14 22:38:24 +02001443/**
1444 * i2c_smbus_write_byte - SMBus "send byte" protocol
1445 * @client: Handle to slave device
1446 * @value: Byte to be sent
1447 *
1448 * This executes the SMBus "send byte" protocol, returning negative errno
1449 * else zero on success.
1450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1452{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001454 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
David Brownellc0564602007-05-01 23:26:31 +02001456EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
David Brownella1cdeda2008-07-14 22:38:24 +02001458/**
1459 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1460 * @client: Handle to slave device
1461 * @command: Byte interpreted by slave
1462 *
1463 * This executes the SMBus "read byte" protocol, returning negative errno
1464 * else a data byte received from the device.
1465 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1467{
1468 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001469 int status;
1470
1471 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1472 I2C_SMBUS_READ, command,
1473 I2C_SMBUS_BYTE_DATA, &data);
1474 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
David Brownellc0564602007-05-01 23:26:31 +02001476EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
David Brownella1cdeda2008-07-14 22:38:24 +02001478/**
1479 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1480 * @client: Handle to slave device
1481 * @command: Byte interpreted by slave
1482 * @value: Byte being written
1483 *
1484 * This executes the SMBus "write byte" protocol, returning negative errno
1485 * else zero on success.
1486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1488{
1489 union i2c_smbus_data data;
1490 data.byte = value;
1491 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1492 I2C_SMBUS_WRITE,command,
1493 I2C_SMBUS_BYTE_DATA,&data);
1494}
David Brownellc0564602007-05-01 23:26:31 +02001495EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
David Brownella1cdeda2008-07-14 22:38:24 +02001497/**
1498 * i2c_smbus_read_word_data - SMBus "read word" protocol
1499 * @client: Handle to slave device
1500 * @command: Byte interpreted by slave
1501 *
1502 * This executes the SMBus "read word" protocol, returning negative errno
1503 * else a 16-bit unsigned "word" received from the device.
1504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1506{
1507 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001508 int status;
1509
1510 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1511 I2C_SMBUS_READ, command,
1512 I2C_SMBUS_WORD_DATA, &data);
1513 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
David Brownellc0564602007-05-01 23:26:31 +02001515EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
David Brownella1cdeda2008-07-14 22:38:24 +02001517/**
1518 * i2c_smbus_write_word_data - SMBus "write word" protocol
1519 * @client: Handle to slave device
1520 * @command: Byte interpreted by slave
1521 * @value: 16-bit "word" being written
1522 *
1523 * This executes the SMBus "write word" protocol, returning negative errno
1524 * else zero on success.
1525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1527{
1528 union i2c_smbus_data data;
1529 data.word = value;
1530 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1531 I2C_SMBUS_WRITE,command,
1532 I2C_SMBUS_WORD_DATA,&data);
1533}
David Brownellc0564602007-05-01 23:26:31 +02001534EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
David Brownella64ec072007-10-13 23:56:31 +02001536/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001537 * i2c_smbus_process_call - SMBus "process call" protocol
1538 * @client: Handle to slave device
1539 * @command: Byte interpreted by slave
1540 * @value: 16-bit "word" being written
1541 *
1542 * This executes the SMBus "process call" protocol, returning negative errno
1543 * else a 16-bit unsigned "word" received from the device.
1544 */
1545s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1546{
1547 union i2c_smbus_data data;
1548 int status;
1549 data.word = value;
1550
1551 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1552 I2C_SMBUS_WRITE, command,
1553 I2C_SMBUS_PROC_CALL, &data);
1554 return (status < 0) ? status : data.word;
1555}
1556EXPORT_SYMBOL(i2c_smbus_process_call);
1557
1558/**
David Brownella1cdeda2008-07-14 22:38:24 +02001559 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001560 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001561 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001562 * @values: Byte array into which data will be read; big enough to hold
1563 * the data returned by the slave. SMBus allows at most 32 bytes.
1564 *
David Brownella1cdeda2008-07-14 22:38:24 +02001565 * This executes the SMBus "block read" protocol, returning negative errno
1566 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001567 *
1568 * Note that using this function requires that the client's adapter support
1569 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1570 * support this; its emulation through I2C messaging relies on a specific
1571 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1572 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001573s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1574 u8 *values)
1575{
1576 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001577 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001578
David Brownell24a5bb72008-07-14 22:38:23 +02001579 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1580 I2C_SMBUS_READ, command,
1581 I2C_SMBUS_BLOCK_DATA, &data);
1582 if (status)
1583 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001584
1585 memcpy(values, &data.block[1], data.block[0]);
1586 return data.block[0];
1587}
1588EXPORT_SYMBOL(i2c_smbus_read_block_data);
1589
David Brownella1cdeda2008-07-14 22:38:24 +02001590/**
1591 * i2c_smbus_write_block_data - SMBus "block write" protocol
1592 * @client: Handle to slave device
1593 * @command: Byte interpreted by slave
1594 * @length: Size of data block; SMBus allows at most 32 bytes
1595 * @values: Byte array which will be written.
1596 *
1597 * This executes the SMBus "block write" protocol, returning negative errno
1598 * else zero on success.
1599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001601 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (length > I2C_SMBUS_BLOCK_MAX)
1606 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001608 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1610 I2C_SMBUS_WRITE,command,
1611 I2C_SMBUS_BLOCK_DATA,&data);
1612}
David Brownellc0564602007-05-01 23:26:31 +02001613EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001616s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1617 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001620 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001621
Jean Delvare4b2643d2007-07-12 14:12:29 +02001622 if (length > I2C_SMBUS_BLOCK_MAX)
1623 length = I2C_SMBUS_BLOCK_MAX;
1624 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001625 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1626 I2C_SMBUS_READ, command,
1627 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1628 if (status < 0)
1629 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001630
1631 memcpy(values, &data.block[1], data.block[0]);
1632 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
David Brownellc0564602007-05-01 23:26:31 +02001634EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Jean Delvare21bbd692006-01-09 15:19:18 +11001636s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001637 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001638{
1639 union i2c_smbus_data data;
1640
1641 if (length > I2C_SMBUS_BLOCK_MAX)
1642 length = I2C_SMBUS_BLOCK_MAX;
1643 data.block[0] = length;
1644 memcpy(data.block + 1, values, length);
1645 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1646 I2C_SMBUS_WRITE, command,
1647 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1648}
David Brownellc0564602007-05-01 23:26:31 +02001649EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001650
David Brownell438d6c22006-12-10 21:21:31 +01001651/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001653static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001655 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 union i2c_smbus_data * data)
1657{
1658 /* So we need to generate a series of msgs. In the case of writing, we
1659 need to use only one message; when reading, we need two. We initialize
1660 most things with sane defaults, to keep the code below somewhat
1661 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001662 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1663 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001665 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1667 };
1668 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001669 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001670 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 msgbuf0[0] = command;
1673 switch(size) {
1674 case I2C_SMBUS_QUICK:
1675 msg[0].len = 0;
1676 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001677 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1678 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 num = 1;
1680 break;
1681 case I2C_SMBUS_BYTE:
1682 if (read_write == I2C_SMBUS_READ) {
1683 /* Special case: only a read! */
1684 msg[0].flags = I2C_M_RD | flags;
1685 num = 1;
1686 }
1687 break;
1688 case I2C_SMBUS_BYTE_DATA:
1689 if (read_write == I2C_SMBUS_READ)
1690 msg[1].len = 1;
1691 else {
1692 msg[0].len = 2;
1693 msgbuf0[1] = data->byte;
1694 }
1695 break;
1696 case I2C_SMBUS_WORD_DATA:
1697 if (read_write == I2C_SMBUS_READ)
1698 msg[1].len = 2;
1699 else {
1700 msg[0].len=3;
1701 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001702 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704 break;
1705 case I2C_SMBUS_PROC_CALL:
1706 num = 2; /* Special case */
1707 read_write = I2C_SMBUS_READ;
1708 msg[0].len = 3;
1709 msg[1].len = 2;
1710 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001711 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
1713 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001715 msg[1].flags |= I2C_M_RECV_LEN;
1716 msg[1].len = 1; /* block length will be added by
1717 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 } else {
1719 msg[0].len = data->block[0] + 2;
1720 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001721 dev_err(&adapter->dev,
1722 "Invalid block write size %d\n",
1723 data->block[0]);
1724 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001726 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 msgbuf0[i] = data->block[i-1];
1728 }
1729 break;
1730 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001731 num = 2; /* Another special case */
1732 read_write = I2C_SMBUS_READ;
1733 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001734 dev_err(&adapter->dev,
1735 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001736 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001737 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001738 }
1739 msg[0].len = data->block[0] + 2;
1740 for (i = 1; i < msg[0].len; i++)
1741 msgbuf0[i] = data->block[i-1];
1742 msg[1].flags |= I2C_M_RECV_LEN;
1743 msg[1].len = 1; /* block length will be added by
1744 the underlying bus driver */
1745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 case I2C_SMBUS_I2C_BLOCK_DATA:
1747 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001748 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 } else {
1750 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001751 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001752 dev_err(&adapter->dev,
1753 "Invalid block write size %d\n",
1754 data->block[0]);
1755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 for (i = 1; i <= data->block[0]; i++)
1758 msgbuf0[i] = data->block[i];
1759 }
1760 break;
1761 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001762 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1763 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765
Jean Delvare421ef472005-10-26 21:28:55 +02001766 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1767 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1768 if (i) {
1769 /* Compute PEC if first message is a write */
1770 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001771 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001772 i2c_smbus_add_pec(&msg[0]);
1773 else /* Write followed by read */
1774 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1775 }
1776 /* Ask for PEC if last message is a read */
1777 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001778 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001779 }
1780
David Brownell24a5bb72008-07-14 22:38:23 +02001781 status = i2c_transfer(adapter, msg, num);
1782 if (status < 0)
1783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Jean Delvare421ef472005-10-26 21:28:55 +02001785 /* Check PEC if last message is a read */
1786 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001787 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1788 if (status < 0)
1789 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001790 }
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (read_write == I2C_SMBUS_READ)
1793 switch(size) {
1794 case I2C_SMBUS_BYTE:
1795 data->byte = msgbuf0[0];
1796 break;
1797 case I2C_SMBUS_BYTE_DATA:
1798 data->byte = msgbuf1[0];
1799 break;
David Brownell438d6c22006-12-10 21:21:31 +01001800 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 case I2C_SMBUS_PROC_CALL:
1802 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1803 break;
1804 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001805 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 data->block[i+1] = msgbuf1[i];
1807 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001808 case I2C_SMBUS_BLOCK_DATA:
1809 case I2C_SMBUS_BLOCK_PROC_CALL:
1810 for (i = 0; i < msgbuf1[0] + 1; i++)
1811 data->block[i] = msgbuf1[i];
1812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814 return 0;
1815}
1816
David Brownella1cdeda2008-07-14 22:38:24 +02001817/**
1818 * i2c_smbus_xfer - execute SMBus protocol operations
1819 * @adapter: Handle to I2C bus
1820 * @addr: Address of SMBus slave on that bus
1821 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1822 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1823 * @command: Byte interpreted by slave, for protocols which use such bytes
1824 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1825 * @data: Data to be read or written
1826 *
1827 * This executes an SMBus protocol operation, and returns a negative
1828 * errno code else zero on success.
1829 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001830s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001831 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001832 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001834 unsigned long orig_jiffies;
1835 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001841 mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001842
1843 /* Retry automatically on arbitration loss */
1844 orig_jiffies = jiffies;
1845 for (res = 0, try = 0; try <= adapter->retries; try++) {
1846 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1847 read_write, command,
1848 protocol, data);
1849 if (res != -EAGAIN)
1850 break;
1851 if (time_after(jiffies,
1852 orig_jiffies + adapter->timeout))
1853 break;
1854 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001855 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 } else
1857 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001858 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return res;
1861}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1865MODULE_DESCRIPTION("I2C-Bus main module");
1866MODULE_LICENSE("GPL");