blob: f236b34296ee7b9eb9d561ede4eac76c9f660d2e [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>
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{
Jean Delvare51298d12009-09-18 22:45:45 +020067 struct i2c_client *client = i2c_verify_client(dev);
68 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020069
Jean Delvare51298d12009-09-18 22:45:45 +020070 if (!client)
71 return 0;
72
73 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020074 /* match on an id table if there is one */
75 if (driver->id_table)
76 return i2c_match_id(driver->id_table, client) != NULL;
77
Jean Delvareeb8a7902008-05-18 20:49:41 +020078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
David Brownell7b4fbc52007-05-01 23:26:30 +020081#ifdef CONFIG_HOTPLUG
82
83/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020084static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020085{
86 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020087
Jean Delvareeb8a7902008-05-18 20:49:41 +020088 if (add_uevent_var(env, "MODALIAS=%s%s",
89 I2C_MODULE_PREFIX, client->name))
90 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020091 dev_dbg(dev, "uevent\n");
92 return 0;
93}
94
95#else
96#define i2c_device_uevent NULL
97#endif /* CONFIG_HOTPLUG */
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int i2c_device_probe(struct device *dev)
100{
Jean Delvare51298d12009-09-18 22:45:45 +0200101 struct i2c_client *client = i2c_verify_client(dev);
102 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100103 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200104
Jean Delvare51298d12009-09-18 22:45:45 +0200105 if (!client)
106 return 0;
107
108 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200109 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200110 return -ENODEV;
111 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200112 if (!device_can_wakeup(&client->dev))
113 device_init_wakeup(&client->dev,
114 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200115 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200116
Jean Delvaree0457442008-07-14 22:38:30 +0200117 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100118 if (status)
119 client->driver = NULL;
120 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static int i2c_device_remove(struct device *dev)
124{
Jean Delvare51298d12009-09-18 22:45:45 +0200125 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200126 struct i2c_driver *driver;
127 int status;
128
Jean Delvare51298d12009-09-18 22:45:45 +0200129 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200130 return 0;
131
132 driver = to_i2c_driver(dev->driver);
133 if (driver->remove) {
134 dev_dbg(dev, "remove\n");
135 status = driver->remove(client);
136 } else {
137 dev->driver = NULL;
138 status = 0;
139 }
140 if (status == 0)
141 client->driver = NULL;
142 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
David Brownellf37dd802007-02-13 22:09:00 +0100145static void i2c_device_shutdown(struct device *dev)
146{
Jean Delvare51298d12009-09-18 22:45:45 +0200147 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100148 struct i2c_driver *driver;
149
Jean Delvare51298d12009-09-18 22:45:45 +0200150 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100151 return;
152 driver = to_i2c_driver(dev->driver);
153 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200154 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100155}
156
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100157static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100158{
Jean Delvare51298d12009-09-18 22:45:45 +0200159 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100160 struct i2c_driver *driver;
161
Jean Delvare51298d12009-09-18 22:45:45 +0200162 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100163 return 0;
164 driver = to_i2c_driver(dev->driver);
165 if (!driver->suspend)
166 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200167 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100168}
169
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100170static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100171{
Jean Delvare51298d12009-09-18 22:45:45 +0200172 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100173 struct i2c_driver *driver;
174
Jean Delvare51298d12009-09-18 22:45:45 +0200175 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100176 return 0;
177 driver = to_i2c_driver(dev->driver);
178 if (!driver->resume)
179 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200180 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100181}
182
David Brownell9c1600e2007-05-01 23:26:31 +0200183static void i2c_client_dev_release(struct device *dev)
184{
185 kfree(to_i2c_client(dev));
186}
187
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100188static ssize_t
189show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200190{
191 struct i2c_client *client = to_i2c_client(dev);
192 return sprintf(buf, "%s\n", client->name);
193}
194
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100195static ssize_t
196show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200197{
198 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200199 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200200}
201
Jean Delvare51298d12009-09-18 22:45:45 +0200202static DEVICE_ATTR(name, S_IRUGO, show_client_name, NULL);
203static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
204
205static struct attribute *i2c_dev_attrs[] = {
206 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200207 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200208 &dev_attr_modalias.attr,
209 NULL
210};
211
212static struct attribute_group i2c_dev_attr_group = {
213 .attrs = i2c_dev_attrs,
214};
215
216static const struct attribute_group *i2c_dev_attr_groups[] = {
217 &i2c_dev_attr_group,
218 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200219};
220
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200221struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100222 .name = "i2c",
223 .match = i2c_device_match,
224 .probe = i2c_device_probe,
225 .remove = i2c_device_remove,
226 .shutdown = i2c_device_shutdown,
227 .suspend = i2c_device_suspend,
228 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000229};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200230EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000231
Jean Delvare51298d12009-09-18 22:45:45 +0200232static struct device_type i2c_client_type = {
233 .groups = i2c_dev_attr_groups,
234 .uevent = i2c_device_uevent,
235 .release = i2c_client_dev_release,
236};
237
David Brownell9b766b82008-01-27 18:14:51 +0100238
239/**
240 * i2c_verify_client - return parameter as i2c_client, or NULL
241 * @dev: device, probably from some driver model iterator
242 *
243 * When traversing the driver model tree, perhaps using driver model
244 * iterators like @device_for_each_child(), you can't assume very much
245 * about the nodes you find. Use this function to avoid oopses caused
246 * by wrongly treating some non-I2C device as an i2c_client.
247 */
248struct i2c_client *i2c_verify_client(struct device *dev)
249{
Jean Delvare51298d12009-09-18 22:45:45 +0200250 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100251 ? to_i2c_client(dev)
252 : NULL;
253}
254EXPORT_SYMBOL(i2c_verify_client);
255
256
David Brownell9c1600e2007-05-01 23:26:31 +0200257/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200258 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200259 * @adap: the adapter managing the device
260 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200261 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200262 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200263 * Create an i2c device. Binding is handled through driver model
264 * probe()/remove() methods. A driver may be bound to this device when we
265 * return from this function, or any later moment (e.g. maybe hotplugging will
266 * load the driver module). This call is not appropriate for use by mainboard
267 * initialization logic, which usually runs during an arch_initcall() long
268 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200269 *
270 * This returns the new i2c client, which may be saved for later use with
271 * i2c_unregister_device(); or NULL to indicate an error.
272 */
273struct i2c_client *
274i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
275{
276 struct i2c_client *client;
277 int status;
278
279 client = kzalloc(sizeof *client, GFP_KERNEL);
280 if (!client)
281 return NULL;
282
283 client->adapter = adap;
284
285 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200286
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200287 if (info->archdata)
288 client->dev.archdata = *info->archdata;
289
Marc Pignatee354252008-08-28 08:33:22 +0200290 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200291 client->addr = info->addr;
292 client->irq = info->irq;
293
David Brownell9c1600e2007-05-01 23:26:31 +0200294 strlcpy(client->name, info->type, sizeof(client->name));
295
Jean Delvaref8a227e2009-06-19 16:58:18 +0200296 /* Check for address business */
297 status = i2c_check_addr(adap, client->addr);
298 if (status)
299 goto out_err;
300
301 client->dev.parent = &client->adapter->dev;
302 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200303 client->dev.type = &i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200304
305 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
306 client->addr);
307 status = device_register(&client->dev);
308 if (status)
309 goto out_err;
310
Jean Delvaref8a227e2009-06-19 16:58:18 +0200311 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
312 client->name, dev_name(&client->dev));
313
David Brownell9c1600e2007-05-01 23:26:31 +0200314 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200315
316out_err:
317 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
318 "(%d)\n", client->name, client->addr, status);
319 kfree(client);
320 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200321}
322EXPORT_SYMBOL_GPL(i2c_new_device);
323
324
325/**
326 * i2c_unregister_device - reverse effect of i2c_new_device()
327 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200328 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200329 */
330void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200331{
David Brownella1d9e6e2007-05-01 23:26:30 +0200332 device_unregister(&client->dev);
333}
David Brownell9c1600e2007-05-01 23:26:31 +0200334EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200335
336
Jean Delvare60b129d2008-05-11 20:37:06 +0200337static const struct i2c_device_id dummy_id[] = {
338 { "dummy", 0 },
339 { },
340};
341
Jean Delvared2653e92008-04-29 23:11:39 +0200342static int dummy_probe(struct i2c_client *client,
343 const struct i2c_device_id *id)
344{
345 return 0;
346}
347
348static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100349{
350 return 0;
351}
352
353static struct i2c_driver dummy_driver = {
354 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200355 .probe = dummy_probe,
356 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200357 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100358};
359
360/**
361 * i2c_new_dummy - return a new i2c device bound to a dummy driver
362 * @adapter: the adapter managing the device
363 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100364 * Context: can sleep
365 *
366 * This returns an I2C client bound to the "dummy" driver, intended for use
367 * with devices that consume multiple addresses. Examples of such chips
368 * include various EEPROMS (like 24c04 and 24c08 models).
369 *
370 * These dummy devices have two main uses. First, most I2C and SMBus calls
371 * except i2c_transfer() need a client handle; the dummy will be that handle.
372 * And second, this prevents the specified address from being bound to a
373 * different driver.
374 *
375 * This returns the new i2c client, which should be saved for later use with
376 * i2c_unregister_device(); or NULL to indicate an error.
377 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100378struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100379{
380 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200381 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100382 };
383
David Brownelle9f13732008-01-27 18:14:52 +0100384 return i2c_new_device(adapter, &info);
385}
386EXPORT_SYMBOL_GPL(i2c_new_dummy);
387
David Brownellf37dd802007-02-13 22:09:00 +0100388/* ------------------------------------------------------------------------- */
389
David Brownell16ffadf2007-05-01 23:26:28 +0200390/* I2C bus adapters -- one roots each I2C or SMBUS segment */
391
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200392static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
David Brownellef2c83212007-05-01 23:26:28 +0200394 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 complete(&adap->dev_released);
396}
397
David Brownell16ffadf2007-05-01 23:26:28 +0200398static ssize_t
399show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
David Brownellef2c83212007-05-01 23:26:28 +0200401 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return sprintf(buf, "%s\n", adap->name);
403}
David Brownell16ffadf2007-05-01 23:26:28 +0200404
Jean Delvare99cd8e22009-06-19 16:58:20 +0200405/*
406 * Let users instantiate I2C devices through sysfs. This can be used when
407 * platform initialization code doesn't contain the proper data for
408 * whatever reason. Also useful for drivers that do device detection and
409 * detection fails, either because the device uses an unexpected address,
410 * or this is a compatible device with different ID register values.
411 *
412 * Parameter checking may look overzealous, but we really don't want
413 * the user to provide incorrect parameters.
414 */
415static ssize_t
416i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
417 const char *buf, size_t count)
418{
419 struct i2c_adapter *adap = to_i2c_adapter(dev);
420 struct i2c_board_info info;
421 struct i2c_client *client;
422 char *blank, end;
423 int res;
424
425 dev_warn(dev, "The new_device interface is still experimental "
426 "and may change in a near future\n");
427 memset(&info, 0, sizeof(struct i2c_board_info));
428
429 blank = strchr(buf, ' ');
430 if (!blank) {
431 dev_err(dev, "%s: Missing parameters\n", "new_device");
432 return -EINVAL;
433 }
434 if (blank - buf > I2C_NAME_SIZE - 1) {
435 dev_err(dev, "%s: Invalid device name\n", "new_device");
436 return -EINVAL;
437 }
438 memcpy(info.type, buf, blank - buf);
439
440 /* Parse remaining parameters, reject extra parameters */
441 res = sscanf(++blank, "%hi%c", &info.addr, &end);
442 if (res < 1) {
443 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
444 return -EINVAL;
445 }
446 if (res > 1 && end != '\n') {
447 dev_err(dev, "%s: Extra parameters\n", "new_device");
448 return -EINVAL;
449 }
450
451 if (info.addr < 0x03 || info.addr > 0x77) {
452 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
453 info.addr);
454 return -EINVAL;
455 }
456
457 client = i2c_new_device(adap, &info);
458 if (!client)
459 return -EEXIST;
460
461 /* Keep track of the added device */
462 mutex_lock(&core_lock);
463 list_add_tail(&client->detected, &userspace_devices);
464 mutex_unlock(&core_lock);
465 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
466 info.type, info.addr);
467
468 return count;
469}
470
471/*
472 * And of course let the users delete the devices they instantiated, if
473 * they got it wrong. This interface can only be used to delete devices
474 * instantiated by i2c_sysfs_new_device above. This guarantees that we
475 * don't delete devices to which some kernel code still has references.
476 *
477 * Parameter checking may look overzealous, but we really don't want
478 * the user to delete the wrong device.
479 */
480static ssize_t
481i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
482 const char *buf, size_t count)
483{
484 struct i2c_adapter *adap = to_i2c_adapter(dev);
485 struct i2c_client *client, *next;
486 unsigned short addr;
487 char end;
488 int res;
489
490 /* Parse parameters, reject extra parameters */
491 res = sscanf(buf, "%hi%c", &addr, &end);
492 if (res < 1) {
493 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
494 return -EINVAL;
495 }
496 if (res > 1 && end != '\n') {
497 dev_err(dev, "%s: Extra parameters\n", "delete_device");
498 return -EINVAL;
499 }
500
501 /* Make sure the device was added through sysfs */
502 res = -ENOENT;
503 mutex_lock(&core_lock);
504 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
505 if (client->addr == addr && client->adapter == adap) {
506 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
507 "delete_device", client->name, client->addr);
508
509 list_del(&client->detected);
510 i2c_unregister_device(client);
511 res = count;
512 break;
513 }
514 }
515 mutex_unlock(&core_lock);
516
517 if (res < 0)
518 dev_err(dev, "%s: Can't find device in list\n",
519 "delete_device");
520 return res;
521}
522
David Brownell16ffadf2007-05-01 23:26:28 +0200523static struct device_attribute i2c_adapter_attrs[] = {
524 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
Jean Delvare99cd8e22009-06-19 16:58:20 +0200525 __ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device),
526 __ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device),
David Brownell16ffadf2007-05-01 23:26:28 +0200527 { },
528};
529
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200530static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200531 .owner = THIS_MODULE,
532 .name = "i2c-adapter",
533 .dev_attrs = i2c_adapter_attrs,
534};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
David Brownell9c1600e2007-05-01 23:26:31 +0200536static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
537{
538 struct i2c_devinfo *devinfo;
539
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200540 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200541 list_for_each_entry(devinfo, &__i2c_board_list, list) {
542 if (devinfo->busnum == adapter->nr
543 && !i2c_new_device(adapter,
544 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100545 dev_err(&adapter->dev,
546 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200547 devinfo->board_info.addr);
548 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200549 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200550}
551
Jean Delvare026526f2008-01-27 18:14:49 +0100552static int i2c_do_add_adapter(struct device_driver *d, void *data)
553{
554 struct i2c_driver *driver = to_i2c_driver(d);
555 struct i2c_adapter *adap = data;
556
Jean Delvare4735c982008-07-14 22:38:36 +0200557 /* Detect supported devices on that bus, and instantiate them */
558 i2c_detect(adap, driver);
559
560 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100561 if (driver->attach_adapter) {
562 /* We ignore the return code; if it fails, too bad */
563 driver->attach_adapter(adap);
564 }
565 return 0;
566}
567
David Brownell6e13e642007-05-01 23:26:31 +0200568static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Jean Delvare026526f2008-01-27 18:14:49 +0100570 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
David Brownell1d0b19c2008-10-14 17:30:05 +0200572 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200573 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
574 res = -EAGAIN;
575 goto out_list;
576 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200577
Ingo Molnar5c085d32006-01-18 23:16:04 +0100578 mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jean Delvare8fcfef62009-03-28 21:34:43 +0100580 /* Set default timeout to 1 second if not already set */
581 if (adap->timeout == 0)
582 adap->timeout = HZ;
583
Kay Sievers27d9c182009-01-07 14:29:16 +0100584 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200586 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200587 res = device_register(&adap->dev);
588 if (res)
589 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200591 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
592
Jean Delvare729d6dd2009-06-19 16:58:18 +0200593 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200594 if (adap->nr < __i2c_first_dynamic_bus_num)
595 i2c_scan_static_board_info(adap);
596
Jean Delvare4735c982008-07-14 22:38:36 +0200597 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200598 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100599 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
600 i2c_do_add_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100601 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200602
603 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200604
Jean Delvareb119c6c2006-08-15 18:26:30 +0200605out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200606 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200607 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200608 mutex_unlock(&core_lock);
609 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
David Brownell6e13e642007-05-01 23:26:31 +0200612/**
613 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
614 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200615 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200616 *
617 * This routine is used to declare an I2C adapter when its bus number
618 * doesn't matter. Examples: for I2C adapters dynamically added by
619 * USB links or PCI plugin cards.
620 *
621 * When this returns zero, a new bus number was allocated and stored
622 * in adap->nr, and the specified adapter became available for clients.
623 * Otherwise, a negative errno value is returned.
624 */
625int i2c_add_adapter(struct i2c_adapter *adapter)
626{
627 int id, res = 0;
628
629retry:
630 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
631 return -ENOMEM;
632
Jean Delvarecaada322008-01-27 18:14:49 +0100633 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200634 /* "above" here means "above or equal to", sigh */
635 res = idr_get_new_above(&i2c_adapter_idr, adapter,
636 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100637 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200638
639 if (res < 0) {
640 if (res == -EAGAIN)
641 goto retry;
642 return res;
643 }
644
645 adapter->nr = id;
646 return i2c_register_adapter(adapter);
647}
648EXPORT_SYMBOL(i2c_add_adapter);
649
650/**
651 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
652 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200653 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200654 *
655 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100656 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
657 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200658 * is used to properly configure I2C devices.
659 *
660 * If no devices have pre-been declared for this bus, then be sure to
661 * register the adapter before any dynamically allocated ones. Otherwise
662 * the required bus ID may not be available.
663 *
664 * When this returns zero, the specified adapter became available for
665 * clients using the bus number provided in adap->nr. Also, the table
666 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
667 * and the appropriate driver model device nodes are created. Otherwise, a
668 * negative errno value is returned.
669 */
670int i2c_add_numbered_adapter(struct i2c_adapter *adap)
671{
672 int id;
673 int status;
674
675 if (adap->nr & ~MAX_ID_MASK)
676 return -EINVAL;
677
678retry:
679 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
680 return -ENOMEM;
681
Jean Delvarecaada322008-01-27 18:14:49 +0100682 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200683 /* "above" here means "above or equal to", sigh;
684 * we need the "equal to" result to force the result
685 */
686 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
687 if (status == 0 && id != adap->nr) {
688 status = -EBUSY;
689 idr_remove(&i2c_adapter_idr, id);
690 }
Jean Delvarecaada322008-01-27 18:14:49 +0100691 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200692 if (status == -EAGAIN)
693 goto retry;
694
695 if (status == 0)
696 status = i2c_register_adapter(adap);
697 return status;
698}
699EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
700
Jean Delvare026526f2008-01-27 18:14:49 +0100701static int i2c_do_del_adapter(struct device_driver *d, void *data)
702{
703 struct i2c_driver *driver = to_i2c_driver(d);
704 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200705 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100706 int res;
707
Jean Delvareacec2112009-03-28 21:34:40 +0100708 /* Remove the devices we created ourselves as the result of hardware
709 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200710 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
711 if (client->adapter == adapter) {
712 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
713 client->name, client->addr);
714 list_del(&client->detected);
715 i2c_unregister_device(client);
716 }
717 }
718
Jean Delvare026526f2008-01-27 18:14:49 +0100719 if (!driver->detach_adapter)
720 return 0;
721 res = driver->detach_adapter(adapter);
722 if (res)
723 dev_err(&adapter->dev, "detach_adapter failed (%d) "
724 "for driver [%s]\n", res, driver->driver.name);
725 return res;
726}
727
Jean Delvaree549c2b2009-06-19 16:58:19 +0200728static int __unregister_client(struct device *dev, void *dummy)
729{
730 struct i2c_client *client = i2c_verify_client(dev);
731 if (client)
732 i2c_unregister_device(client);
733 return 0;
734}
735
David Brownelld64f73b2007-07-12 14:12:28 +0200736/**
737 * i2c_del_adapter - unregister I2C adapter
738 * @adap: the adapter being unregistered
739 * Context: can sleep
740 *
741 * This unregisters an I2C adapter which was previously registered
742 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744int i2c_del_adapter(struct i2c_adapter *adap)
745{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200747 struct i2c_adapter *found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200750 mutex_lock(&core_lock);
751 found = idr_find(&i2c_adapter_idr, adap->nr);
752 mutex_unlock(&core_lock);
753 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200754 pr_debug("i2c-core: attempting to delete unregistered "
755 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Jean Delvare026526f2008-01-27 18:14:49 +0100759 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200760 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100761 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
762 i2c_do_del_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200763 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100764 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200765 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jean Delvaree549c2b2009-06-19 16:58:19 +0200767 /* Detach any active clients. This can't fail, thus we do not
768 checking the returned value. */
769 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* clean up the sysfs representation */
772 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 /* wait for sysfs to drop all references */
776 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David Brownell6e13e642007-05-01 23:26:31 +0200778 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200779 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200781 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200783 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200785 /* Clear the device structure in case this adapter is ever going to be
786 added again */
787 memset(&adap->dev, 0, sizeof(adap->dev));
788
Jean Delvare35fc37f2009-06-19 16:58:19 +0200789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
David Brownellc0564602007-05-01 23:26:31 +0200791EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793
David Brownell7b4fbc52007-05-01 23:26:30 +0200794/* ------------------------------------------------------------------------- */
795
Dave Young7f101a92008-07-14 22:38:19 +0200796static int __attach_adapter(struct device *dev, void *data)
797{
798 struct i2c_adapter *adapter = to_i2c_adapter(dev);
799 struct i2c_driver *driver = data;
800
Jean Delvare4735c982008-07-14 22:38:36 +0200801 i2c_detect(adapter, driver);
802
803 /* Legacy drivers scan i2c busses directly */
804 if (driver->attach_adapter)
805 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200806
807 return 0;
808}
809
David Brownell7b4fbc52007-05-01 23:26:30 +0200810/*
811 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200812 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
814
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800815int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100817 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
David Brownell1d0b19c2008-10-14 17:30:05 +0200819 /* Can't register until after driver model init */
820 if (unlikely(WARN_ON(!i2c_bus_type.p)))
821 return -EAGAIN;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800824 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jean Delvare729d6dd2009-06-19 16:58:18 +0200827 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200828 * will have called probe() for all matching-but-unbound devices.
829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 res = driver_register(&driver->driver);
831 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100832 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100833
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100834 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Jean Delvare4735c982008-07-14 22:38:36 +0200836 INIT_LIST_HEAD(&driver->clients);
837 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200838 mutex_lock(&core_lock);
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400839 class_for_each_device(&i2c_adapter_class, NULL, driver,
840 __attach_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100841 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200842
Jean Delvare7eebcb72006-02-05 23:28:21 +0100843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800845EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Dave Young7f101a92008-07-14 22:38:19 +0200847static int __detach_adapter(struct device *dev, void *data)
848{
849 struct i2c_adapter *adapter = to_i2c_adapter(dev);
850 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200851 struct i2c_client *client, *_n;
852
Jean Delvareacec2112009-03-28 21:34:40 +0100853 /* Remove the devices we created ourselves as the result of hardware
854 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200855 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
856 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
857 client->name, client->addr);
858 list_del(&client->detected);
859 i2c_unregister_device(client);
860 }
861
Dave Young7f101a92008-07-14 22:38:19 +0200862 if (driver->detach_adapter) {
863 if (driver->detach_adapter(adapter))
864 dev_err(&adapter->dev,
865 "detach_adapter failed for driver [%s]\n",
866 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200867 }
868
869 return 0;
870}
871
David Brownella1d9e6e2007-05-01 23:26:30 +0200872/**
873 * i2c_del_driver - unregister I2C driver
874 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200875 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200876 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200877void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Jean Delvarecaada322008-01-27 18:14:49 +0100879 mutex_lock(&core_lock);
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400880 class_for_each_device(&i2c_adapter_class, NULL, driver,
881 __detach_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200882 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100885 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
David Brownellc0564602007-05-01 23:26:31 +0200887EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
David Brownell7b4fbc52007-05-01 23:26:30 +0200889/* ------------------------------------------------------------------------- */
890
David Brownell9b766b82008-01-27 18:14:51 +0100891static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
David Brownell9b766b82008-01-27 18:14:51 +0100893 struct i2c_client *client = i2c_verify_client(dev);
894 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
David Brownell9b766b82008-01-27 18:14:51 +0100896 if (client && client->addr == addr)
897 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return 0;
899}
900
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100901static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
David Brownell9b766b82008-01-27 18:14:51 +0100903 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Jean Delvaree48d3312008-01-27 18:14:48 +0100906/**
907 * i2c_use_client - increments the reference count of the i2c client structure
908 * @client: the client being referenced
909 *
910 * Each live reference to a client should be refcounted. The driver model does
911 * that automatically as part of driver binding, so that most drivers don't
912 * need to do this explicitly: they hold a reference until they're unbound
913 * from the device.
914 *
915 * A pointer to the client with the incremented reference counter is returned.
916 */
917struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
David Brownell6ea438e2008-07-14 22:38:24 +0200919 if (client && get_device(&client->dev))
920 return client;
921 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
David Brownellc0564602007-05-01 23:26:31 +0200923EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Jean Delvaree48d3312008-01-27 18:14:48 +0100925/**
926 * i2c_release_client - release a use of the i2c client structure
927 * @client: the client being no longer referenced
928 *
929 * Must be called when a user of a client is finished with it.
930 */
931void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
David Brownell6ea438e2008-07-14 22:38:24 +0200933 if (client)
934 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
David Brownellc0564602007-05-01 23:26:31 +0200936EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
David Brownell9b766b82008-01-27 18:14:51 +0100938struct i2c_cmd_arg {
939 unsigned cmd;
940 void *arg;
941};
942
943static int i2c_cmd(struct device *dev, void *_arg)
944{
945 struct i2c_client *client = i2c_verify_client(dev);
946 struct i2c_cmd_arg *arg = _arg;
947
948 if (client && client->driver && client->driver->command)
949 client->driver->command(client, arg->cmd, arg->arg);
950 return 0;
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
954{
David Brownell9b766b82008-01-27 18:14:51 +0100955 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
David Brownell9b766b82008-01-27 18:14:51 +0100957 cmd_arg.cmd = cmd;
958 cmd_arg.arg = arg;
959 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
David Brownellc0564602007-05-01 23:26:31 +0200961EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963static int __init i2c_init(void)
964{
965 int retval;
966
967 retval = bus_register(&i2c_bus_type);
968 if (retval)
969 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100970 retval = class_register(&i2c_adapter_class);
971 if (retval)
972 goto bus_err;
973 retval = i2c_add_driver(&dummy_driver);
974 if (retval)
975 goto class_err;
976 return 0;
977
978class_err:
979 class_unregister(&i2c_adapter_class);
980bus_err:
981 bus_unregister(&i2c_bus_type);
982 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static void __exit i2c_exit(void)
986{
David Brownelle9f13732008-01-27 18:14:52 +0100987 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 bus_unregister(&i2c_bus_type);
990}
991
David Brownella10f9e72008-10-14 17:30:06 +0200992/* We must initialize early, because some subsystems register i2c drivers
993 * in subsys_initcall() code, but are linked (and initialized) before i2c.
994 */
995postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996module_exit(i2c_exit);
997
998/* ----------------------------------------------------
999 * the functional interface to the i2c busses.
1000 * ----------------------------------------------------
1001 */
1002
David Brownella1cdeda2008-07-14 22:38:24 +02001003/**
1004 * i2c_transfer - execute a single or combined I2C message
1005 * @adap: Handle to I2C bus
1006 * @msgs: One or more messages to execute before STOP is issued to
1007 * terminate the operation; each message begins with a START.
1008 * @num: Number of messages to be executed.
1009 *
1010 * Returns negative errno, else the number of messages executed.
1011 *
1012 * Note that there is no requirement that each message be sent to
1013 * the same slave address, although that is the most common model.
1014 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001015int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001017 unsigned long orig_jiffies;
1018 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
David Brownella1cdeda2008-07-14 22:38:24 +02001020 /* REVISIT the fault reporting model here is weak:
1021 *
1022 * - When we get an error after receiving N bytes from a slave,
1023 * there is no way to report "N".
1024 *
1025 * - When we get a NAK after transmitting N bytes to a slave,
1026 * there is no way to report "N" ... or to let the master
1027 * continue executing the rest of this combined message, if
1028 * that's the appropriate response.
1029 *
1030 * - When for example "num" is two and we successfully complete
1031 * the first message but get an error part way through the
1032 * second, it's unclear whether that should be reported as
1033 * one (discarding status on the second message) or errno
1034 * (discarding status on the first one).
1035 */
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (adap->algo->master_xfer) {
1038#ifdef DEBUG
1039 for (ret = 0; ret < num; ret++) {
1040 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001041 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1042 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1043 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045#endif
1046
Mike Rapoportcea443a2008-01-27 18:14:50 +01001047 if (in_atomic() || irqs_disabled()) {
1048 ret = mutex_trylock(&adap->bus_lock);
1049 if (!ret)
1050 /* I2C activity is ongoing. */
1051 return -EAGAIN;
1052 } else {
1053 mutex_lock_nested(&adap->bus_lock, adap->level);
1054 }
1055
Clifford Wolf66b650f2009-06-15 18:01:46 +02001056 /* Retry automatically on arbitration loss */
1057 orig_jiffies = jiffies;
1058 for (ret = 0, try = 0; try <= adap->retries; try++) {
1059 ret = adap->algo->master_xfer(adap, msgs, num);
1060 if (ret != -EAGAIN)
1061 break;
1062 if (time_after(jiffies, orig_jiffies + adap->timeout))
1063 break;
1064 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001065 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return ret;
1068 } else {
1069 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001070 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072}
David Brownellc0564602007-05-01 23:26:31 +02001073EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
David Brownella1cdeda2008-07-14 22:38:24 +02001075/**
1076 * i2c_master_send - issue a single I2C message in master transmit mode
1077 * @client: Handle to slave device
1078 * @buf: Data that will be written to the slave
1079 * @count: How many bytes to write
1080 *
1081 * Returns negative errno, or else the number of bytes written.
1082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1084{
1085 int ret;
1086 struct i2c_adapter *adap=client->adapter;
1087 struct i2c_msg msg;
1088
Jean Delvare815f55f2005-05-07 22:58:46 +02001089 msg.addr = client->addr;
1090 msg.flags = client->flags & I2C_M_TEN;
1091 msg.len = count;
1092 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001093
Jean Delvare815f55f2005-05-07 22:58:46 +02001094 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jean Delvare815f55f2005-05-07 22:58:46 +02001096 /* 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_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
David Brownella1cdeda2008-07-14 22:38:24 +02001102/**
1103 * i2c_master_recv - issue a single I2C message in master receive mode
1104 * @client: Handle to slave device
1105 * @buf: Where to store data read from slave
1106 * @count: How many bytes to read
1107 *
1108 * Returns negative errno, or else the number of bytes read.
1109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1111{
1112 struct i2c_adapter *adap=client->adapter;
1113 struct i2c_msg msg;
1114 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Jean Delvare815f55f2005-05-07 22:58:46 +02001116 msg.addr = client->addr;
1117 msg.flags = client->flags & I2C_M_TEN;
1118 msg.flags |= I2C_M_RD;
1119 msg.len = count;
1120 msg.buf = buf;
1121
1122 ret = i2c_transfer(adap, &msg, 1);
1123
1124 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1125 transmitted, else error code. */
1126 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
David Brownellc0564602007-05-01 23:26:31 +02001128EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/* ----------------------------------------------------
1131 * the i2c address scanning function
1132 * Will not work for 10-bit addresses!
1133 * ----------------------------------------------------
1134 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001135
Jean Delvare4735c982008-07-14 22:38:36 +02001136static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1137 struct i2c_driver *driver)
1138{
1139 struct i2c_board_info info;
1140 struct i2c_adapter *adapter = temp_client->adapter;
1141 int addr = temp_client->addr;
1142 int err;
1143
1144 /* Make sure the address is valid */
1145 if (addr < 0x03 || addr > 0x77) {
1146 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1147 addr);
1148 return -EINVAL;
1149 }
1150
1151 /* Skip if already in use */
1152 if (i2c_check_addr(adapter, addr))
1153 return 0;
1154
1155 /* Make sure there is something at this address, unless forced */
1156 if (kind < 0) {
1157 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1158 I2C_SMBUS_QUICK, NULL) < 0)
1159 return 0;
1160
1161 /* prevent 24RF08 corruption */
1162 if ((addr & ~0x0f) == 0x50)
1163 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1164 I2C_SMBUS_QUICK, NULL);
1165 }
1166
1167 /* Finally call the custom detection function */
1168 memset(&info, 0, sizeof(struct i2c_board_info));
1169 info.addr = addr;
1170 err = driver->detect(temp_client, kind, &info);
1171 if (err) {
1172 /* -ENODEV is returned if the detection fails. We catch it
1173 here as this isn't an error. */
1174 return err == -ENODEV ? 0 : err;
1175 }
1176
1177 /* Consistency check */
1178 if (info.type[0] == '\0') {
1179 dev_err(&adapter->dev, "%s detection function provided "
1180 "no name for 0x%x\n", driver->driver.name,
1181 addr);
1182 } else {
1183 struct i2c_client *client;
1184
1185 /* Detection succeeded, instantiate the device */
1186 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1187 info.type, info.addr);
1188 client = i2c_new_device(adapter, &info);
1189 if (client)
1190 list_add_tail(&client->detected, &driver->clients);
1191 else
1192 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1193 info.type, info.addr);
1194 }
1195 return 0;
1196}
1197
1198static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1199{
1200 const struct i2c_client_address_data *address_data;
1201 struct i2c_client *temp_client;
1202 int i, err = 0;
1203 int adap_id = i2c_adapter_id(adapter);
1204
1205 address_data = driver->address_data;
1206 if (!driver->detect || !address_data)
1207 return 0;
1208
1209 /* Set up a temporary client to help detect callback */
1210 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1211 if (!temp_client)
1212 return -ENOMEM;
1213 temp_client->adapter = adapter;
1214
1215 /* Force entries are done first, and are not affected by ignore
1216 entries */
1217 if (address_data->forces) {
1218 const unsigned short * const *forces = address_data->forces;
1219 int kind;
1220
1221 for (kind = 0; forces[kind]; kind++) {
1222 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1223 i += 2) {
1224 if (forces[kind][i] == adap_id
1225 || forces[kind][i] == ANY_I2C_BUS) {
1226 dev_dbg(&adapter->dev, "found force "
1227 "parameter for adapter %d, "
1228 "addr 0x%02x, kind %d\n",
1229 adap_id, forces[kind][i + 1],
1230 kind);
1231 temp_client->addr = forces[kind][i + 1];
1232 err = i2c_detect_address(temp_client,
1233 kind, driver);
1234 if (err)
1235 goto exit_free;
1236 }
1237 }
1238 }
1239 }
1240
Jean Delvare4329cf82008-08-28 08:33:23 +02001241 /* Stop here if the classes do not match */
1242 if (!(adapter->class & driver->class))
1243 goto exit_free;
1244
Jean Delvare4735c982008-07-14 22:38:36 +02001245 /* Stop here if we can't use SMBUS_QUICK */
1246 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1247 if (address_data->probe[0] == I2C_CLIENT_END
1248 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1249 goto exit_free;
1250
1251 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1252 "can't probe for chips\n");
1253 err = -EOPNOTSUPP;
1254 goto exit_free;
1255 }
1256
Jean Delvare4735c982008-07-14 22:38:36 +02001257 /* Probe entries are done second, and are not affected by ignore
1258 entries either */
1259 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1260 if (address_data->probe[i] == adap_id
1261 || address_data->probe[i] == ANY_I2C_BUS) {
1262 dev_dbg(&adapter->dev, "found probe parameter for "
1263 "adapter %d, addr 0x%02x\n", adap_id,
1264 address_data->probe[i + 1]);
1265 temp_client->addr = address_data->probe[i + 1];
1266 err = i2c_detect_address(temp_client, -1, driver);
1267 if (err)
1268 goto exit_free;
1269 }
1270 }
1271
1272 /* Normal entries are done last, unless shadowed by an ignore entry */
1273 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1274 int j, ignore;
1275
1276 ignore = 0;
1277 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1278 j += 2) {
1279 if ((address_data->ignore[j] == adap_id ||
1280 address_data->ignore[j] == ANY_I2C_BUS)
1281 && address_data->ignore[j + 1]
1282 == address_data->normal_i2c[i]) {
1283 dev_dbg(&adapter->dev, "found ignore "
1284 "parameter for adapter %d, "
1285 "addr 0x%02x\n", adap_id,
1286 address_data->ignore[j + 1]);
1287 ignore = 1;
1288 break;
1289 }
1290 }
1291 if (ignore)
1292 continue;
1293
1294 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1295 "addr 0x%02x\n", adap_id,
1296 address_data->normal_i2c[i]);
1297 temp_client->addr = address_data->normal_i2c[i];
1298 err = i2c_detect_address(temp_client, -1, driver);
1299 if (err)
1300 goto exit_free;
1301 }
1302
1303 exit_free:
1304 kfree(temp_client);
1305 return err;
1306}
1307
Jean Delvare12b5053a2007-05-01 23:26:31 +02001308struct i2c_client *
1309i2c_new_probed_device(struct i2c_adapter *adap,
1310 struct i2c_board_info *info,
1311 unsigned short const *addr_list)
1312{
1313 int i;
1314
1315 /* Stop here if the bus doesn't support probing */
1316 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1317 dev_err(&adap->dev, "Probing not supported\n");
1318 return NULL;
1319 }
1320
Jean Delvare12b5053a2007-05-01 23:26:31 +02001321 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1322 /* Check address validity */
1323 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1324 dev_warn(&adap->dev, "Invalid 7-bit address "
1325 "0x%02x\n", addr_list[i]);
1326 continue;
1327 }
1328
1329 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001330 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001331 dev_dbg(&adap->dev, "Address 0x%02x already in "
1332 "use, not probing\n", addr_list[i]);
1333 continue;
1334 }
1335
1336 /* Test address responsiveness
1337 The default probe method is a quick write, but it is known
1338 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1339 and could also irreversibly write-protect some EEPROMs, so
1340 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1341 read instead. Also, some bus drivers don't implement
1342 quick write, so we fallback to a byte read it that case
1343 too. */
1344 if ((addr_list[i] & ~0x07) == 0x30
1345 || (addr_list[i] & ~0x0f) == 0x50
1346 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001347 union i2c_smbus_data data;
1348
Jean Delvare12b5053a2007-05-01 23:26:31 +02001349 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1350 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001351 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001352 break;
1353 } else {
1354 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1355 I2C_SMBUS_WRITE, 0,
1356 I2C_SMBUS_QUICK, NULL) >= 0)
1357 break;
1358 }
1359 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001360
1361 if (addr_list[i] == I2C_CLIENT_END) {
1362 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1363 return NULL;
1364 }
1365
1366 info->addr = addr_list[i];
1367 return i2c_new_device(adap, info);
1368}
1369EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371struct i2c_adapter* i2c_get_adapter(int id)
1372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001374
Jean Delvarecaada322008-01-27 18:14:49 +01001375 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001376 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001377 if (adapter && !try_module_get(adapter->owner))
1378 adapter = NULL;
1379
Jean Delvarecaada322008-01-27 18:14:49 +01001380 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001381 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
David Brownellc0564602007-05-01 23:26:31 +02001383EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385void i2c_put_adapter(struct i2c_adapter *adap)
1386{
1387 module_put(adap->owner);
1388}
David Brownellc0564602007-05-01 23:26:31 +02001389EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391/* The SMBus parts */
1392
David Brownell438d6c22006-12-10 21:21:31 +01001393#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001394static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001399 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 data = data ^ POLY;
1401 data = data << 1;
1402 }
1403 return (u8)(data >> 8);
1404}
1405
Jean Delvare421ef472005-10-26 21:28:55 +02001406/* Incremental CRC8 over count bytes in the array pointed to by p */
1407static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 int i;
1410
1411 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001412 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return crc;
1414}
1415
Jean Delvare421ef472005-10-26 21:28:55 +02001416/* Assume a 7-bit address, which is reasonable for SMBus */
1417static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Jean Delvare421ef472005-10-26 21:28:55 +02001419 /* The address will be sent first */
1420 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1421 pec = i2c_smbus_pec(pec, &addr, 1);
1422
1423 /* The data buffer follows */
1424 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Jean Delvare421ef472005-10-26 21:28:55 +02001427/* Used for write only transactions */
1428static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Jean Delvare421ef472005-10-26 21:28:55 +02001430 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1431 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Jean Delvare421ef472005-10-26 21:28:55 +02001434/* Return <0 on CRC error
1435 If there was a write before this read (most cases) we need to take the
1436 partial CRC from the write part into account.
1437 Note that this function does modify the message (we need to decrease the
1438 message length to hide the CRC byte from the caller). */
1439static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Jean Delvare421ef472005-10-26 21:28:55 +02001441 u8 rpec = msg->buf[--msg->len];
1442 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (rpec != cpec) {
1445 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1446 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001447 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
David Brownell438d6c22006-12-10 21:21:31 +01001449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
David Brownella1cdeda2008-07-14 22:38:24 +02001452/**
1453 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1454 * @client: Handle to slave device
1455 *
1456 * This executes the SMBus "receive byte" protocol, returning negative errno
1457 * else the byte received from the device.
1458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459s32 i2c_smbus_read_byte(struct i2c_client *client)
1460{
1461 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001462 int status;
1463
1464 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1465 I2C_SMBUS_READ, 0,
1466 I2C_SMBUS_BYTE, &data);
1467 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
David Brownellc0564602007-05-01 23:26:31 +02001469EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
David Brownella1cdeda2008-07-14 22:38:24 +02001471/**
1472 * i2c_smbus_write_byte - SMBus "send byte" protocol
1473 * @client: Handle to slave device
1474 * @value: Byte to be sent
1475 *
1476 * This executes the SMBus "send byte" protocol, returning negative errno
1477 * else zero on success.
1478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001482 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
David Brownellc0564602007-05-01 23:26:31 +02001484EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
David Brownella1cdeda2008-07-14 22:38:24 +02001486/**
1487 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1488 * @client: Handle to slave device
1489 * @command: Byte interpreted by slave
1490 *
1491 * This executes the SMBus "read byte" protocol, returning negative errno
1492 * else a data byte received from the device.
1493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1495{
1496 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001497 int status;
1498
1499 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1500 I2C_SMBUS_READ, command,
1501 I2C_SMBUS_BYTE_DATA, &data);
1502 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
David Brownellc0564602007-05-01 23:26:31 +02001504EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
David Brownella1cdeda2008-07-14 22:38:24 +02001506/**
1507 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1508 * @client: Handle to slave device
1509 * @command: Byte interpreted by slave
1510 * @value: Byte being written
1511 *
1512 * This executes the SMBus "write byte" protocol, returning negative errno
1513 * else zero on success.
1514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1516{
1517 union i2c_smbus_data data;
1518 data.byte = value;
1519 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1520 I2C_SMBUS_WRITE,command,
1521 I2C_SMBUS_BYTE_DATA,&data);
1522}
David Brownellc0564602007-05-01 23:26:31 +02001523EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
David Brownella1cdeda2008-07-14 22:38:24 +02001525/**
1526 * i2c_smbus_read_word_data - SMBus "read word" protocol
1527 * @client: Handle to slave device
1528 * @command: Byte interpreted by slave
1529 *
1530 * This executes the SMBus "read word" protocol, returning negative errno
1531 * else a 16-bit unsigned "word" received from the device.
1532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1534{
1535 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001536 int status;
1537
1538 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1539 I2C_SMBUS_READ, command,
1540 I2C_SMBUS_WORD_DATA, &data);
1541 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
David Brownellc0564602007-05-01 23:26:31 +02001543EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
David Brownella1cdeda2008-07-14 22:38:24 +02001545/**
1546 * i2c_smbus_write_word_data - SMBus "write word" protocol
1547 * @client: Handle to slave device
1548 * @command: Byte interpreted by slave
1549 * @value: 16-bit "word" being written
1550 *
1551 * This executes the SMBus "write word" protocol, returning negative errno
1552 * else zero on success.
1553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1555{
1556 union i2c_smbus_data data;
1557 data.word = value;
1558 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1559 I2C_SMBUS_WRITE,command,
1560 I2C_SMBUS_WORD_DATA,&data);
1561}
David Brownellc0564602007-05-01 23:26:31 +02001562EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
David Brownella64ec072007-10-13 23:56:31 +02001564/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001565 * i2c_smbus_process_call - SMBus "process call" protocol
1566 * @client: Handle to slave device
1567 * @command: Byte interpreted by slave
1568 * @value: 16-bit "word" being written
1569 *
1570 * This executes the SMBus "process call" protocol, returning negative errno
1571 * else a 16-bit unsigned "word" received from the device.
1572 */
1573s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1574{
1575 union i2c_smbus_data data;
1576 int status;
1577 data.word = value;
1578
1579 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1580 I2C_SMBUS_WRITE, command,
1581 I2C_SMBUS_PROC_CALL, &data);
1582 return (status < 0) ? status : data.word;
1583}
1584EXPORT_SYMBOL(i2c_smbus_process_call);
1585
1586/**
David Brownella1cdeda2008-07-14 22:38:24 +02001587 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001588 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001589 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001590 * @values: Byte array into which data will be read; big enough to hold
1591 * the data returned by the slave. SMBus allows at most 32 bytes.
1592 *
David Brownella1cdeda2008-07-14 22:38:24 +02001593 * This executes the SMBus "block read" protocol, returning negative errno
1594 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001595 *
1596 * Note that using this function requires that the client's adapter support
1597 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1598 * support this; its emulation through I2C messaging relies on a specific
1599 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1600 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001601s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1602 u8 *values)
1603{
1604 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001605 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001606
David Brownell24a5bb72008-07-14 22:38:23 +02001607 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1608 I2C_SMBUS_READ, command,
1609 I2C_SMBUS_BLOCK_DATA, &data);
1610 if (status)
1611 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001612
1613 memcpy(values, &data.block[1], data.block[0]);
1614 return data.block[0];
1615}
1616EXPORT_SYMBOL(i2c_smbus_read_block_data);
1617
David Brownella1cdeda2008-07-14 22:38:24 +02001618/**
1619 * i2c_smbus_write_block_data - SMBus "block write" protocol
1620 * @client: Handle to slave device
1621 * @command: Byte interpreted by slave
1622 * @length: Size of data block; SMBus allows at most 32 bytes
1623 * @values: Byte array which will be written.
1624 *
1625 * This executes the SMBus "block write" protocol, returning negative errno
1626 * else zero on success.
1627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001629 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (length > I2C_SMBUS_BLOCK_MAX)
1634 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001636 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1638 I2C_SMBUS_WRITE,command,
1639 I2C_SMBUS_BLOCK_DATA,&data);
1640}
David Brownellc0564602007-05-01 23:26:31 +02001641EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001644s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1645 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001648 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001649
Jean Delvare4b2643d2007-07-12 14:12:29 +02001650 if (length > I2C_SMBUS_BLOCK_MAX)
1651 length = I2C_SMBUS_BLOCK_MAX;
1652 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001653 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1654 I2C_SMBUS_READ, command,
1655 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1656 if (status < 0)
1657 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001658
1659 memcpy(values, &data.block[1], data.block[0]);
1660 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
David Brownellc0564602007-05-01 23:26:31 +02001662EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Jean Delvare21bbd692006-01-09 15:19:18 +11001664s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001665 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001666{
1667 union i2c_smbus_data data;
1668
1669 if (length > I2C_SMBUS_BLOCK_MAX)
1670 length = I2C_SMBUS_BLOCK_MAX;
1671 data.block[0] = length;
1672 memcpy(data.block + 1, values, length);
1673 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1674 I2C_SMBUS_WRITE, command,
1675 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1676}
David Brownellc0564602007-05-01 23:26:31 +02001677EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001678
David Brownell438d6c22006-12-10 21:21:31 +01001679/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001681static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001683 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 union i2c_smbus_data * data)
1685{
1686 /* So we need to generate a series of msgs. In the case of writing, we
1687 need to use only one message; when reading, we need two. We initialize
1688 most things with sane defaults, to keep the code below somewhat
1689 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001690 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1691 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001693 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1695 };
1696 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001697 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001698 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 msgbuf0[0] = command;
1701 switch(size) {
1702 case I2C_SMBUS_QUICK:
1703 msg[0].len = 0;
1704 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001705 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1706 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 num = 1;
1708 break;
1709 case I2C_SMBUS_BYTE:
1710 if (read_write == I2C_SMBUS_READ) {
1711 /* Special case: only a read! */
1712 msg[0].flags = I2C_M_RD | flags;
1713 num = 1;
1714 }
1715 break;
1716 case I2C_SMBUS_BYTE_DATA:
1717 if (read_write == I2C_SMBUS_READ)
1718 msg[1].len = 1;
1719 else {
1720 msg[0].len = 2;
1721 msgbuf0[1] = data->byte;
1722 }
1723 break;
1724 case I2C_SMBUS_WORD_DATA:
1725 if (read_write == I2C_SMBUS_READ)
1726 msg[1].len = 2;
1727 else {
1728 msg[0].len=3;
1729 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001730 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732 break;
1733 case I2C_SMBUS_PROC_CALL:
1734 num = 2; /* Special case */
1735 read_write = I2C_SMBUS_READ;
1736 msg[0].len = 3;
1737 msg[1].len = 2;
1738 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001739 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
1741 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001743 msg[1].flags |= I2C_M_RECV_LEN;
1744 msg[1].len = 1; /* block length will be added by
1745 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 } else {
1747 msg[0].len = data->block[0] + 2;
1748 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001749 dev_err(&adapter->dev,
1750 "Invalid block write size %d\n",
1751 data->block[0]);
1752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001754 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 msgbuf0[i] = data->block[i-1];
1756 }
1757 break;
1758 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001759 num = 2; /* Another special case */
1760 read_write = I2C_SMBUS_READ;
1761 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001762 dev_err(&adapter->dev,
1763 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001764 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001765 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001766 }
1767 msg[0].len = data->block[0] + 2;
1768 for (i = 1; i < msg[0].len; i++)
1769 msgbuf0[i] = data->block[i-1];
1770 msg[1].flags |= I2C_M_RECV_LEN;
1771 msg[1].len = 1; /* block length will be added by
1772 the underlying bus driver */
1773 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 case I2C_SMBUS_I2C_BLOCK_DATA:
1775 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001776 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 } else {
1778 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001779 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001780 dev_err(&adapter->dev,
1781 "Invalid block write size %d\n",
1782 data->block[0]);
1783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 for (i = 1; i <= data->block[0]; i++)
1786 msgbuf0[i] = data->block[i];
1787 }
1788 break;
1789 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001790 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1791 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
Jean Delvare421ef472005-10-26 21:28:55 +02001794 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1795 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1796 if (i) {
1797 /* Compute PEC if first message is a write */
1798 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001799 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001800 i2c_smbus_add_pec(&msg[0]);
1801 else /* Write followed by read */
1802 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1803 }
1804 /* Ask for PEC if last message is a read */
1805 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001806 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001807 }
1808
David Brownell24a5bb72008-07-14 22:38:23 +02001809 status = i2c_transfer(adapter, msg, num);
1810 if (status < 0)
1811 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Jean Delvare421ef472005-10-26 21:28:55 +02001813 /* Check PEC if last message is a read */
1814 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001815 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1816 if (status < 0)
1817 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (read_write == I2C_SMBUS_READ)
1821 switch(size) {
1822 case I2C_SMBUS_BYTE:
1823 data->byte = msgbuf0[0];
1824 break;
1825 case I2C_SMBUS_BYTE_DATA:
1826 data->byte = msgbuf1[0];
1827 break;
David Brownell438d6c22006-12-10 21:21:31 +01001828 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 case I2C_SMBUS_PROC_CALL:
1830 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1831 break;
1832 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001833 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 data->block[i+1] = msgbuf1[i];
1835 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001836 case I2C_SMBUS_BLOCK_DATA:
1837 case I2C_SMBUS_BLOCK_PROC_CALL:
1838 for (i = 0; i < msgbuf1[0] + 1; i++)
1839 data->block[i] = msgbuf1[i];
1840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842 return 0;
1843}
1844
David Brownella1cdeda2008-07-14 22:38:24 +02001845/**
1846 * i2c_smbus_xfer - execute SMBus protocol operations
1847 * @adapter: Handle to I2C bus
1848 * @addr: Address of SMBus slave on that bus
1849 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1850 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1851 * @command: Byte interpreted by slave, for protocols which use such bytes
1852 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1853 * @data: Data to be read or written
1854 *
1855 * This executes an SMBus protocol operation, and returns a negative
1856 * errno code else zero on success.
1857 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001858s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001859 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001860 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001862 unsigned long orig_jiffies;
1863 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001869 mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001870
1871 /* Retry automatically on arbitration loss */
1872 orig_jiffies = jiffies;
1873 for (res = 0, try = 0; try <= adapter->retries; try++) {
1874 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1875 read_write, command,
1876 protocol, data);
1877 if (res != -EAGAIN)
1878 break;
1879 if (time_after(jiffies,
1880 orig_jiffies + adapter->timeout))
1881 break;
1882 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001883 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 } else
1885 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001886 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return res;
1889}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1893MODULE_DESCRIPTION("I2C-Bus main module");
1894MODULE_LICENSE("GPL");