blob: b1fd00d9a5c7f4d476581ae887c71e7b25fd8e78 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010032#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010033#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010034#include <linux/hardirq.h>
35#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
David Brownell9c1600e2007-05-01 23:26:31 +020038#include "i2c-core.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jean Delvarecaada322008-01-27 18:14:49 +010041static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_IDR(i2c_adapter_idr);
43
Jean Delvaref8a227e2009-06-19 16:58:18 +020044static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020045static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010046
47/* ------------------------------------------------------------------------- */
48
Jean Delvared2653e92008-04-29 23:11:39 +020049static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
50 const struct i2c_client *client)
51{
52 while (id->name[0]) {
53 if (strcmp(client->name, id->name) == 0)
54 return id;
55 id++;
56 }
57 return NULL;
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int i2c_device_match(struct device *dev, struct device_driver *drv)
61{
David Brownell7b4fbc52007-05-01 23:26:30 +020062 struct i2c_client *client = to_i2c_client(dev);
63 struct i2c_driver *driver = to_i2c_driver(drv);
64
Jean Delvared2653e92008-04-29 23:11:39 +020065 /* match on an id table if there is one */
66 if (driver->id_table)
67 return i2c_match_id(driver->id_table, client) != NULL;
68
Jean Delvareeb8a7902008-05-18 20:49:41 +020069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
David Brownell7b4fbc52007-05-01 23:26:30 +020072#ifdef CONFIG_HOTPLUG
73
74/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020075static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020076{
77 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020078
Jean Delvareeb8a7902008-05-18 20:49:41 +020079 if (add_uevent_var(env, "MODALIAS=%s%s",
80 I2C_MODULE_PREFIX, client->name))
81 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020082 dev_dbg(dev, "uevent\n");
83 return 0;
84}
85
86#else
87#define i2c_device_uevent NULL
88#endif /* CONFIG_HOTPLUG */
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int i2c_device_probe(struct device *dev)
91{
David Brownell7b4fbc52007-05-01 23:26:30 +020092 struct i2c_client *client = to_i2c_client(dev);
93 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +010094 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +020095
Jean Delvaree0457442008-07-14 22:38:30 +020096 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +020097 return -ENODEV;
98 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +020099 if (!device_can_wakeup(&client->dev))
100 device_init_wakeup(&client->dev,
101 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200102 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200103
Jean Delvaree0457442008-07-14 22:38:30 +0200104 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100105 if (status)
106 client->driver = NULL;
107 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110static int i2c_device_remove(struct device *dev)
111{
David Brownella1d9e6e2007-05-01 23:26:30 +0200112 struct i2c_client *client = to_i2c_client(dev);
113 struct i2c_driver *driver;
114 int status;
115
116 if (!dev->driver)
117 return 0;
118
119 driver = to_i2c_driver(dev->driver);
120 if (driver->remove) {
121 dev_dbg(dev, "remove\n");
122 status = driver->remove(client);
123 } else {
124 dev->driver = NULL;
125 status = 0;
126 }
127 if (status == 0)
128 client->driver = NULL;
129 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
David Brownellf37dd802007-02-13 22:09:00 +0100132static void i2c_device_shutdown(struct device *dev)
133{
134 struct i2c_driver *driver;
135
136 if (!dev->driver)
137 return;
138 driver = to_i2c_driver(dev->driver);
139 if (driver->shutdown)
140 driver->shutdown(to_i2c_client(dev));
141}
142
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100143static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100144{
145 struct i2c_driver *driver;
146
147 if (!dev->driver)
148 return 0;
149 driver = to_i2c_driver(dev->driver);
150 if (!driver->suspend)
151 return 0;
152 return driver->suspend(to_i2c_client(dev), mesg);
153}
154
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100155static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100156{
157 struct i2c_driver *driver;
158
159 if (!dev->driver)
160 return 0;
161 driver = to_i2c_driver(dev->driver);
162 if (!driver->resume)
163 return 0;
164 return driver->resume(to_i2c_client(dev));
165}
166
David Brownell9c1600e2007-05-01 23:26:31 +0200167static void i2c_client_dev_release(struct device *dev)
168{
169 kfree(to_i2c_client(dev));
170}
171
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100172static ssize_t
173show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200174{
175 struct i2c_client *client = to_i2c_client(dev);
176 return sprintf(buf, "%s\n", client->name);
177}
178
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100179static ssize_t
180show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200181{
182 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200183 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200184}
185
186static struct device_attribute i2c_dev_attrs[] = {
187 __ATTR(name, S_IRUGO, show_client_name, NULL),
188 /* modalias helps coldplug: modprobe $(cat .../modalias) */
189 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
190 { },
191};
192
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200193struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100194 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200195 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100196 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200197 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100198 .probe = i2c_device_probe,
199 .remove = i2c_device_remove,
200 .shutdown = i2c_device_shutdown,
201 .suspend = i2c_device_suspend,
202 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000203};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200204EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000205
David Brownell9b766b82008-01-27 18:14:51 +0100206
207/**
208 * i2c_verify_client - return parameter as i2c_client, or NULL
209 * @dev: device, probably from some driver model iterator
210 *
211 * When traversing the driver model tree, perhaps using driver model
212 * iterators like @device_for_each_child(), you can't assume very much
213 * about the nodes you find. Use this function to avoid oopses caused
214 * by wrongly treating some non-I2C device as an i2c_client.
215 */
216struct i2c_client *i2c_verify_client(struct device *dev)
217{
218 return (dev->bus == &i2c_bus_type)
219 ? to_i2c_client(dev)
220 : NULL;
221}
222EXPORT_SYMBOL(i2c_verify_client);
223
224
David Brownell9c1600e2007-05-01 23:26:31 +0200225/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200226 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200227 * @adap: the adapter managing the device
228 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200229 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200230 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200231 * Create an i2c device. Binding is handled through driver model
232 * probe()/remove() methods. A driver may be bound to this device when we
233 * return from this function, or any later moment (e.g. maybe hotplugging will
234 * load the driver module). This call is not appropriate for use by mainboard
235 * initialization logic, which usually runs during an arch_initcall() long
236 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200237 *
238 * This returns the new i2c client, which may be saved for later use with
239 * i2c_unregister_device(); or NULL to indicate an error.
240 */
241struct i2c_client *
242i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
243{
244 struct i2c_client *client;
245 int status;
246
247 client = kzalloc(sizeof *client, GFP_KERNEL);
248 if (!client)
249 return NULL;
250
251 client->adapter = adap;
252
253 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200254
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200255 if (info->archdata)
256 client->dev.archdata = *info->archdata;
257
Marc Pignatee354252008-08-28 08:33:22 +0200258 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200259 client->addr = info->addr;
260 client->irq = info->irq;
261
David Brownell9c1600e2007-05-01 23:26:31 +0200262 strlcpy(client->name, info->type, sizeof(client->name));
263
Jean Delvaref8a227e2009-06-19 16:58:18 +0200264 /* Check for address business */
265 status = i2c_check_addr(adap, client->addr);
266 if (status)
267 goto out_err;
268
269 client->dev.parent = &client->adapter->dev;
270 client->dev.bus = &i2c_bus_type;
Jean Delvare1e40ac12009-06-19 16:58:19 +0200271 client->dev.release = i2c_client_dev_release;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200272
273 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
274 client->addr);
275 status = device_register(&client->dev);
276 if (status)
277 goto out_err;
278
Jean Delvaref8a227e2009-06-19 16:58:18 +0200279 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
280 client->name, dev_name(&client->dev));
281
David Brownell9c1600e2007-05-01 23:26:31 +0200282 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200283
284out_err:
285 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
286 "(%d)\n", client->name, client->addr, status);
287 kfree(client);
288 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200289}
290EXPORT_SYMBOL_GPL(i2c_new_device);
291
292
293/**
294 * i2c_unregister_device - reverse effect of i2c_new_device()
295 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200296 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200297 */
298void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200299{
David Brownella1d9e6e2007-05-01 23:26:30 +0200300 device_unregister(&client->dev);
301}
David Brownell9c1600e2007-05-01 23:26:31 +0200302EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200303
304
Jean Delvare60b129d2008-05-11 20:37:06 +0200305static const struct i2c_device_id dummy_id[] = {
306 { "dummy", 0 },
307 { },
308};
309
Jean Delvared2653e92008-04-29 23:11:39 +0200310static int dummy_probe(struct i2c_client *client,
311 const struct i2c_device_id *id)
312{
313 return 0;
314}
315
316static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100317{
318 return 0;
319}
320
321static struct i2c_driver dummy_driver = {
322 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200323 .probe = dummy_probe,
324 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200325 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100326};
327
328/**
329 * i2c_new_dummy - return a new i2c device bound to a dummy driver
330 * @adapter: the adapter managing the device
331 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100332 * Context: can sleep
333 *
334 * This returns an I2C client bound to the "dummy" driver, intended for use
335 * with devices that consume multiple addresses. Examples of such chips
336 * include various EEPROMS (like 24c04 and 24c08 models).
337 *
338 * These dummy devices have two main uses. First, most I2C and SMBus calls
339 * except i2c_transfer() need a client handle; the dummy will be that handle.
340 * And second, this prevents the specified address from being bound to a
341 * different driver.
342 *
343 * This returns the new i2c client, which should be saved for later use with
344 * i2c_unregister_device(); or NULL to indicate an error.
345 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100346struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100347{
348 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200349 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100350 };
351
David Brownelle9f13732008-01-27 18:14:52 +0100352 return i2c_new_device(adapter, &info);
353}
354EXPORT_SYMBOL_GPL(i2c_new_dummy);
355
David Brownellf37dd802007-02-13 22:09:00 +0100356/* ------------------------------------------------------------------------- */
357
David Brownell16ffadf2007-05-01 23:26:28 +0200358/* I2C bus adapters -- one roots each I2C or SMBUS segment */
359
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200360static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
David Brownellef2c83212007-05-01 23:26:28 +0200362 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 complete(&adap->dev_released);
364}
365
David Brownell16ffadf2007-05-01 23:26:28 +0200366static ssize_t
367show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
David Brownellef2c83212007-05-01 23:26:28 +0200369 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return sprintf(buf, "%s\n", adap->name);
371}
David Brownell16ffadf2007-05-01 23:26:28 +0200372
373static struct device_attribute i2c_adapter_attrs[] = {
374 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
375 { },
376};
377
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200378static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200379 .owner = THIS_MODULE,
380 .name = "i2c-adapter",
381 .dev_attrs = i2c_adapter_attrs,
382};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
David Brownell9c1600e2007-05-01 23:26:31 +0200384static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
385{
386 struct i2c_devinfo *devinfo;
387
388 mutex_lock(&__i2c_board_lock);
389 list_for_each_entry(devinfo, &__i2c_board_list, list) {
390 if (devinfo->busnum == adapter->nr
391 && !i2c_new_device(adapter,
392 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100393 dev_err(&adapter->dev,
394 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200395 devinfo->board_info.addr);
396 }
397 mutex_unlock(&__i2c_board_lock);
398}
399
Jean Delvare026526f2008-01-27 18:14:49 +0100400static int i2c_do_add_adapter(struct device_driver *d, void *data)
401{
402 struct i2c_driver *driver = to_i2c_driver(d);
403 struct i2c_adapter *adap = data;
404
Jean Delvare4735c982008-07-14 22:38:36 +0200405 /* Detect supported devices on that bus, and instantiate them */
406 i2c_detect(adap, driver);
407
408 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100409 if (driver->attach_adapter) {
410 /* We ignore the return code; if it fails, too bad */
411 driver->attach_adapter(adap);
412 }
413 return 0;
414}
415
David Brownell6e13e642007-05-01 23:26:31 +0200416static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Jean Delvare026526f2008-01-27 18:14:49 +0100418 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David Brownell1d0b19c2008-10-14 17:30:05 +0200420 /* Can't register until after driver model init */
421 if (unlikely(WARN_ON(!i2c_bus_type.p)))
422 return -EAGAIN;
423
Ingo Molnar5c085d32006-01-18 23:16:04 +0100424 mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jean Delvarecaada322008-01-27 18:14:49 +0100426 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200427
Jean Delvare8fcfef62009-03-28 21:34:43 +0100428 /* Set default timeout to 1 second if not already set */
429 if (adap->timeout == 0)
430 adap->timeout = HZ;
431
Kay Sievers27d9c182009-01-07 14:29:16 +0100432 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200434 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200435 res = device_register(&adap->dev);
436 if (res)
437 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200439 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
440
Jean Delvare729d6dd2009-06-19 16:58:18 +0200441 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200442 if (adap->nr < __i2c_first_dynamic_bus_num)
443 i2c_scan_static_board_info(adap);
444
Jean Delvare4735c982008-07-14 22:38:36 +0200445 /* Notify drivers */
Jean Delvare026526f2008-01-27 18:14:49 +0100446 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
447 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100450 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200452
Jean Delvareb119c6c2006-08-15 18:26:30 +0200453out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200454 idr_remove(&i2c_adapter_idr, adap->nr);
455 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
David Brownell6e13e642007-05-01 23:26:31 +0200458/**
459 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
460 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200461 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200462 *
463 * This routine is used to declare an I2C adapter when its bus number
464 * doesn't matter. Examples: for I2C adapters dynamically added by
465 * USB links or PCI plugin cards.
466 *
467 * When this returns zero, a new bus number was allocated and stored
468 * in adap->nr, and the specified adapter became available for clients.
469 * Otherwise, a negative errno value is returned.
470 */
471int i2c_add_adapter(struct i2c_adapter *adapter)
472{
473 int id, res = 0;
474
475retry:
476 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
477 return -ENOMEM;
478
Jean Delvarecaada322008-01-27 18:14:49 +0100479 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200480 /* "above" here means "above or equal to", sigh */
481 res = idr_get_new_above(&i2c_adapter_idr, adapter,
482 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100483 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200484
485 if (res < 0) {
486 if (res == -EAGAIN)
487 goto retry;
488 return res;
489 }
490
491 adapter->nr = id;
492 return i2c_register_adapter(adapter);
493}
494EXPORT_SYMBOL(i2c_add_adapter);
495
496/**
497 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
498 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200499 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200500 *
501 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100502 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
503 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200504 * is used to properly configure I2C devices.
505 *
506 * If no devices have pre-been declared for this bus, then be sure to
507 * register the adapter before any dynamically allocated ones. Otherwise
508 * the required bus ID may not be available.
509 *
510 * When this returns zero, the specified adapter became available for
511 * clients using the bus number provided in adap->nr. Also, the table
512 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
513 * and the appropriate driver model device nodes are created. Otherwise, a
514 * negative errno value is returned.
515 */
516int i2c_add_numbered_adapter(struct i2c_adapter *adap)
517{
518 int id;
519 int status;
520
521 if (adap->nr & ~MAX_ID_MASK)
522 return -EINVAL;
523
524retry:
525 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
526 return -ENOMEM;
527
Jean Delvarecaada322008-01-27 18:14:49 +0100528 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200529 /* "above" here means "above or equal to", sigh;
530 * we need the "equal to" result to force the result
531 */
532 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
533 if (status == 0 && id != adap->nr) {
534 status = -EBUSY;
535 idr_remove(&i2c_adapter_idr, id);
536 }
Jean Delvarecaada322008-01-27 18:14:49 +0100537 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200538 if (status == -EAGAIN)
539 goto retry;
540
541 if (status == 0)
542 status = i2c_register_adapter(adap);
543 return status;
544}
545EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
546
Jean Delvare026526f2008-01-27 18:14:49 +0100547static int i2c_do_del_adapter(struct device_driver *d, void *data)
548{
549 struct i2c_driver *driver = to_i2c_driver(d);
550 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200551 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100552 int res;
553
Jean Delvareacec2112009-03-28 21:34:40 +0100554 /* Remove the devices we created ourselves as the result of hardware
555 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200556 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
557 if (client->adapter == adapter) {
558 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
559 client->name, client->addr);
560 list_del(&client->detected);
561 i2c_unregister_device(client);
562 }
563 }
564
Jean Delvare026526f2008-01-27 18:14:49 +0100565 if (!driver->detach_adapter)
566 return 0;
567 res = driver->detach_adapter(adapter);
568 if (res)
569 dev_err(&adapter->dev, "detach_adapter failed (%d) "
570 "for driver [%s]\n", res, driver->driver.name);
571 return res;
572}
573
Jean Delvaree549c2b2009-06-19 16:58:19 +0200574static int __unregister_client(struct device *dev, void *dummy)
575{
576 struct i2c_client *client = i2c_verify_client(dev);
577 if (client)
578 i2c_unregister_device(client);
579 return 0;
580}
581
David Brownelld64f73b2007-07-12 14:12:28 +0200582/**
583 * i2c_del_adapter - unregister I2C adapter
584 * @adap: the adapter being unregistered
585 * Context: can sleep
586 *
587 * This unregisters an I2C adapter which was previously registered
588 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590int i2c_del_adapter(struct i2c_adapter *adap)
591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int res = 0;
593
Jean Delvarecaada322008-01-27 18:14:49 +0100594 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100597 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200598 pr_debug("i2c-core: attempting to delete unregistered "
599 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 res = -EINVAL;
601 goto out_unlock;
602 }
603
Jean Delvare026526f2008-01-27 18:14:49 +0100604 /* Tell drivers about this removal */
605 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
606 i2c_do_del_adapter);
607 if (res)
608 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jean Delvaree549c2b2009-06-19 16:58:19 +0200610 /* Detach any active clients. This can't fail, thus we do not
611 checking the returned value. */
612 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* clean up the sysfs representation */
615 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* wait for sysfs to drop all references */
619 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
David Brownell6e13e642007-05-01 23:26:31 +0200621 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 idr_remove(&i2c_adapter_idr, adap->nr);
623
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200624 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200626 /* Clear the device structure in case this adapter is ever going to be
627 added again */
628 memset(&adap->dev, 0, sizeof(adap->dev));
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100631 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return res;
633}
David Brownellc0564602007-05-01 23:26:31 +0200634EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636
David Brownell7b4fbc52007-05-01 23:26:30 +0200637/* ------------------------------------------------------------------------- */
638
Dave Young7f101a92008-07-14 22:38:19 +0200639static int __attach_adapter(struct device *dev, void *data)
640{
641 struct i2c_adapter *adapter = to_i2c_adapter(dev);
642 struct i2c_driver *driver = data;
643
Jean Delvare4735c982008-07-14 22:38:36 +0200644 i2c_detect(adapter, driver);
645
646 /* Legacy drivers scan i2c busses directly */
647 if (driver->attach_adapter)
648 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200649
650 return 0;
651}
652
David Brownell7b4fbc52007-05-01 23:26:30 +0200653/*
654 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200655 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800658int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100660 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
David Brownell1d0b19c2008-10-14 17:30:05 +0200662 /* Can't register until after driver model init */
663 if (unlikely(WARN_ON(!i2c_bus_type.p)))
664 return -EAGAIN;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800667 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jean Delvare729d6dd2009-06-19 16:58:18 +0200670 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200671 * will have called probe() for all matching-but-unbound devices.
672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 res = driver_register(&driver->driver);
674 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100675 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100676
Jean Delvarecaada322008-01-27 18:14:49 +0100677 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100678
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100679 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Jean Delvare4735c982008-07-14 22:38:36 +0200681 INIT_LIST_HEAD(&driver->clients);
682 /* Walk the adapters that are already present */
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400683 class_for_each_device(&i2c_adapter_class, NULL, driver,
684 __attach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jean Delvarecaada322008-01-27 18:14:49 +0100686 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800689EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Dave Young7f101a92008-07-14 22:38:19 +0200691static int __detach_adapter(struct device *dev, void *data)
692{
693 struct i2c_adapter *adapter = to_i2c_adapter(dev);
694 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200695 struct i2c_client *client, *_n;
696
Jean Delvareacec2112009-03-28 21:34:40 +0100697 /* Remove the devices we created ourselves as the result of hardware
698 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200699 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
700 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
701 client->name, client->addr);
702 list_del(&client->detected);
703 i2c_unregister_device(client);
704 }
705
Dave Young7f101a92008-07-14 22:38:19 +0200706 if (driver->detach_adapter) {
707 if (driver->detach_adapter(adapter))
708 dev_err(&adapter->dev,
709 "detach_adapter failed for driver [%s]\n",
710 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200711 }
712
713 return 0;
714}
715
David Brownella1d9e6e2007-05-01 23:26:30 +0200716/**
717 * i2c_del_driver - unregister I2C driver
718 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200719 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200720 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200721void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Jean Delvarecaada322008-01-27 18:14:49 +0100723 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400725 class_for_each_device(&i2c_adapter_class, NULL, driver,
726 __detach_adapter);
David Brownella1d9e6e2007-05-01 23:26:30 +0200727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100729 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jean Delvarecaada322008-01-27 18:14:49 +0100731 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
David Brownellc0564602007-05-01 23:26:31 +0200733EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David Brownell7b4fbc52007-05-01 23:26:30 +0200735/* ------------------------------------------------------------------------- */
736
David Brownell9b766b82008-01-27 18:14:51 +0100737static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
David Brownell9b766b82008-01-27 18:14:51 +0100739 struct i2c_client *client = i2c_verify_client(dev);
740 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
David Brownell9b766b82008-01-27 18:14:51 +0100742 if (client && client->addr == addr)
743 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
745}
746
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100747static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
David Brownell9b766b82008-01-27 18:14:51 +0100749 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Jean Delvaree48d3312008-01-27 18:14:48 +0100752/**
753 * i2c_use_client - increments the reference count of the i2c client structure
754 * @client: the client being referenced
755 *
756 * Each live reference to a client should be refcounted. The driver model does
757 * that automatically as part of driver binding, so that most drivers don't
758 * need to do this explicitly: they hold a reference until they're unbound
759 * from the device.
760 *
761 * A pointer to the client with the incremented reference counter is returned.
762 */
763struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
David Brownell6ea438e2008-07-14 22:38:24 +0200765 if (client && get_device(&client->dev))
766 return client;
767 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
David Brownellc0564602007-05-01 23:26:31 +0200769EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Jean Delvaree48d3312008-01-27 18:14:48 +0100771/**
772 * i2c_release_client - release a use of the i2c client structure
773 * @client: the client being no longer referenced
774 *
775 * Must be called when a user of a client is finished with it.
776 */
777void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
David Brownell6ea438e2008-07-14 22:38:24 +0200779 if (client)
780 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
David Brownellc0564602007-05-01 23:26:31 +0200782EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
David Brownell9b766b82008-01-27 18:14:51 +0100784struct i2c_cmd_arg {
785 unsigned cmd;
786 void *arg;
787};
788
789static int i2c_cmd(struct device *dev, void *_arg)
790{
791 struct i2c_client *client = i2c_verify_client(dev);
792 struct i2c_cmd_arg *arg = _arg;
793
794 if (client && client->driver && client->driver->command)
795 client->driver->command(client, arg->cmd, arg->arg);
796 return 0;
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
800{
David Brownell9b766b82008-01-27 18:14:51 +0100801 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
David Brownell9b766b82008-01-27 18:14:51 +0100803 cmd_arg.cmd = cmd;
804 cmd_arg.arg = arg;
805 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
David Brownellc0564602007-05-01 23:26:31 +0200807EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809static int __init i2c_init(void)
810{
811 int retval;
812
813 retval = bus_register(&i2c_bus_type);
814 if (retval)
815 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100816 retval = class_register(&i2c_adapter_class);
817 if (retval)
818 goto bus_err;
819 retval = i2c_add_driver(&dummy_driver);
820 if (retval)
821 goto class_err;
822 return 0;
823
824class_err:
825 class_unregister(&i2c_adapter_class);
826bus_err:
827 bus_unregister(&i2c_bus_type);
828 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831static void __exit i2c_exit(void)
832{
David Brownelle9f13732008-01-27 18:14:52 +0100833 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 bus_unregister(&i2c_bus_type);
836}
837
David Brownella10f9e72008-10-14 17:30:06 +0200838/* We must initialize early, because some subsystems register i2c drivers
839 * in subsys_initcall() code, but are linked (and initialized) before i2c.
840 */
841postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842module_exit(i2c_exit);
843
844/* ----------------------------------------------------
845 * the functional interface to the i2c busses.
846 * ----------------------------------------------------
847 */
848
David Brownella1cdeda2008-07-14 22:38:24 +0200849/**
850 * i2c_transfer - execute a single or combined I2C message
851 * @adap: Handle to I2C bus
852 * @msgs: One or more messages to execute before STOP is issued to
853 * terminate the operation; each message begins with a START.
854 * @num: Number of messages to be executed.
855 *
856 * Returns negative errno, else the number of messages executed.
857 *
858 * Note that there is no requirement that each message be sent to
859 * the same slave address, although that is the most common model.
860 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100861int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Clifford Wolf66b650f2009-06-15 18:01:46 +0200863 unsigned long orig_jiffies;
864 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
David Brownella1cdeda2008-07-14 22:38:24 +0200866 /* REVISIT the fault reporting model here is weak:
867 *
868 * - When we get an error after receiving N bytes from a slave,
869 * there is no way to report "N".
870 *
871 * - When we get a NAK after transmitting N bytes to a slave,
872 * there is no way to report "N" ... or to let the master
873 * continue executing the rest of this combined message, if
874 * that's the appropriate response.
875 *
876 * - When for example "num" is two and we successfully complete
877 * the first message but get an error part way through the
878 * second, it's unclear whether that should be reported as
879 * one (discarding status on the second message) or errno
880 * (discarding status on the first one).
881 */
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (adap->algo->master_xfer) {
884#ifdef DEBUG
885 for (ret = 0; ret < num; ret++) {
886 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200887 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
888 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
889 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891#endif
892
Mike Rapoportcea443a82008-01-27 18:14:50 +0100893 if (in_atomic() || irqs_disabled()) {
894 ret = mutex_trylock(&adap->bus_lock);
895 if (!ret)
896 /* I2C activity is ongoing. */
897 return -EAGAIN;
898 } else {
899 mutex_lock_nested(&adap->bus_lock, adap->level);
900 }
901
Clifford Wolf66b650f2009-06-15 18:01:46 +0200902 /* Retry automatically on arbitration loss */
903 orig_jiffies = jiffies;
904 for (ret = 0, try = 0; try <= adap->retries; try++) {
905 ret = adap->algo->master_xfer(adap, msgs, num);
906 if (ret != -EAGAIN)
907 break;
908 if (time_after(jiffies, orig_jiffies + adap->timeout))
909 break;
910 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100911 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 return ret;
914 } else {
915 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +0200916 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918}
David Brownellc0564602007-05-01 23:26:31 +0200919EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
David Brownella1cdeda2008-07-14 22:38:24 +0200921/**
922 * i2c_master_send - issue a single I2C message in master transmit mode
923 * @client: Handle to slave device
924 * @buf: Data that will be written to the slave
925 * @count: How many bytes to write
926 *
927 * Returns negative errno, or else the number of bytes written.
928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
930{
931 int ret;
932 struct i2c_adapter *adap=client->adapter;
933 struct i2c_msg msg;
934
Jean Delvare815f55f2005-05-07 22:58:46 +0200935 msg.addr = client->addr;
936 msg.flags = client->flags & I2C_M_TEN;
937 msg.len = count;
938 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100939
Jean Delvare815f55f2005-05-07 22:58:46 +0200940 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Jean Delvare815f55f2005-05-07 22:58:46 +0200942 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
943 transmitted, else error code. */
944 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
David Brownellc0564602007-05-01 23:26:31 +0200946EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
David Brownella1cdeda2008-07-14 22:38:24 +0200948/**
949 * i2c_master_recv - issue a single I2C message in master receive mode
950 * @client: Handle to slave device
951 * @buf: Where to store data read from slave
952 * @count: How many bytes to read
953 *
954 * Returns negative errno, or else the number of bytes read.
955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
957{
958 struct i2c_adapter *adap=client->adapter;
959 struct i2c_msg msg;
960 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Jean Delvare815f55f2005-05-07 22:58:46 +0200962 msg.addr = client->addr;
963 msg.flags = client->flags & I2C_M_TEN;
964 msg.flags |= I2C_M_RD;
965 msg.len = count;
966 msg.buf = buf;
967
968 ret = i2c_transfer(adap, &msg, 1);
969
970 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
971 transmitted, else error code. */
972 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
David Brownellc0564602007-05-01 23:26:31 +0200974EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976/* ----------------------------------------------------
977 * the i2c address scanning function
978 * Will not work for 10-bit addresses!
979 * ----------------------------------------------------
980 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200981
Jean Delvare4735c982008-07-14 22:38:36 +0200982static int i2c_detect_address(struct i2c_client *temp_client, int kind,
983 struct i2c_driver *driver)
984{
985 struct i2c_board_info info;
986 struct i2c_adapter *adapter = temp_client->adapter;
987 int addr = temp_client->addr;
988 int err;
989
990 /* Make sure the address is valid */
991 if (addr < 0x03 || addr > 0x77) {
992 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
993 addr);
994 return -EINVAL;
995 }
996
997 /* Skip if already in use */
998 if (i2c_check_addr(adapter, addr))
999 return 0;
1000
1001 /* Make sure there is something at this address, unless forced */
1002 if (kind < 0) {
1003 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1004 I2C_SMBUS_QUICK, NULL) < 0)
1005 return 0;
1006
1007 /* prevent 24RF08 corruption */
1008 if ((addr & ~0x0f) == 0x50)
1009 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1010 I2C_SMBUS_QUICK, NULL);
1011 }
1012
1013 /* Finally call the custom detection function */
1014 memset(&info, 0, sizeof(struct i2c_board_info));
1015 info.addr = addr;
1016 err = driver->detect(temp_client, kind, &info);
1017 if (err) {
1018 /* -ENODEV is returned if the detection fails. We catch it
1019 here as this isn't an error. */
1020 return err == -ENODEV ? 0 : err;
1021 }
1022
1023 /* Consistency check */
1024 if (info.type[0] == '\0') {
1025 dev_err(&adapter->dev, "%s detection function provided "
1026 "no name for 0x%x\n", driver->driver.name,
1027 addr);
1028 } else {
1029 struct i2c_client *client;
1030
1031 /* Detection succeeded, instantiate the device */
1032 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1033 info.type, info.addr);
1034 client = i2c_new_device(adapter, &info);
1035 if (client)
1036 list_add_tail(&client->detected, &driver->clients);
1037 else
1038 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1039 info.type, info.addr);
1040 }
1041 return 0;
1042}
1043
1044static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1045{
1046 const struct i2c_client_address_data *address_data;
1047 struct i2c_client *temp_client;
1048 int i, err = 0;
1049 int adap_id = i2c_adapter_id(adapter);
1050
1051 address_data = driver->address_data;
1052 if (!driver->detect || !address_data)
1053 return 0;
1054
1055 /* Set up a temporary client to help detect callback */
1056 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1057 if (!temp_client)
1058 return -ENOMEM;
1059 temp_client->adapter = adapter;
1060
1061 /* Force entries are done first, and are not affected by ignore
1062 entries */
1063 if (address_data->forces) {
1064 const unsigned short * const *forces = address_data->forces;
1065 int kind;
1066
1067 for (kind = 0; forces[kind]; kind++) {
1068 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1069 i += 2) {
1070 if (forces[kind][i] == adap_id
1071 || forces[kind][i] == ANY_I2C_BUS) {
1072 dev_dbg(&adapter->dev, "found force "
1073 "parameter for adapter %d, "
1074 "addr 0x%02x, kind %d\n",
1075 adap_id, forces[kind][i + 1],
1076 kind);
1077 temp_client->addr = forces[kind][i + 1];
1078 err = i2c_detect_address(temp_client,
1079 kind, driver);
1080 if (err)
1081 goto exit_free;
1082 }
1083 }
1084 }
1085 }
1086
Jean Delvare4329cf82008-08-28 08:33:23 +02001087 /* Stop here if the classes do not match */
1088 if (!(adapter->class & driver->class))
1089 goto exit_free;
1090
Jean Delvare4735c982008-07-14 22:38:36 +02001091 /* Stop here if we can't use SMBUS_QUICK */
1092 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1093 if (address_data->probe[0] == I2C_CLIENT_END
1094 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1095 goto exit_free;
1096
1097 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1098 "can't probe for chips\n");
1099 err = -EOPNOTSUPP;
1100 goto exit_free;
1101 }
1102
Jean Delvare4735c982008-07-14 22:38:36 +02001103 /* Probe entries are done second, and are not affected by ignore
1104 entries either */
1105 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1106 if (address_data->probe[i] == adap_id
1107 || address_data->probe[i] == ANY_I2C_BUS) {
1108 dev_dbg(&adapter->dev, "found probe parameter for "
1109 "adapter %d, addr 0x%02x\n", adap_id,
1110 address_data->probe[i + 1]);
1111 temp_client->addr = address_data->probe[i + 1];
1112 err = i2c_detect_address(temp_client, -1, driver);
1113 if (err)
1114 goto exit_free;
1115 }
1116 }
1117
1118 /* Normal entries are done last, unless shadowed by an ignore entry */
1119 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1120 int j, ignore;
1121
1122 ignore = 0;
1123 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1124 j += 2) {
1125 if ((address_data->ignore[j] == adap_id ||
1126 address_data->ignore[j] == ANY_I2C_BUS)
1127 && address_data->ignore[j + 1]
1128 == address_data->normal_i2c[i]) {
1129 dev_dbg(&adapter->dev, "found ignore "
1130 "parameter for adapter %d, "
1131 "addr 0x%02x\n", adap_id,
1132 address_data->ignore[j + 1]);
1133 ignore = 1;
1134 break;
1135 }
1136 }
1137 if (ignore)
1138 continue;
1139
1140 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1141 "addr 0x%02x\n", adap_id,
1142 address_data->normal_i2c[i]);
1143 temp_client->addr = address_data->normal_i2c[i];
1144 err = i2c_detect_address(temp_client, -1, driver);
1145 if (err)
1146 goto exit_free;
1147 }
1148
1149 exit_free:
1150 kfree(temp_client);
1151 return err;
1152}
1153
Jean Delvare12b5053a2007-05-01 23:26:31 +02001154struct i2c_client *
1155i2c_new_probed_device(struct i2c_adapter *adap,
1156 struct i2c_board_info *info,
1157 unsigned short const *addr_list)
1158{
1159 int i;
1160
1161 /* Stop here if the bus doesn't support probing */
1162 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1163 dev_err(&adap->dev, "Probing not supported\n");
1164 return NULL;
1165 }
1166
Jean Delvare12b5053a2007-05-01 23:26:31 +02001167 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1168 /* Check address validity */
1169 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1170 dev_warn(&adap->dev, "Invalid 7-bit address "
1171 "0x%02x\n", addr_list[i]);
1172 continue;
1173 }
1174
1175 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001176 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001177 dev_dbg(&adap->dev, "Address 0x%02x already in "
1178 "use, not probing\n", addr_list[i]);
1179 continue;
1180 }
1181
1182 /* Test address responsiveness
1183 The default probe method is a quick write, but it is known
1184 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1185 and could also irreversibly write-protect some EEPROMs, so
1186 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1187 read instead. Also, some bus drivers don't implement
1188 quick write, so we fallback to a byte read it that case
1189 too. */
1190 if ((addr_list[i] & ~0x07) == 0x30
1191 || (addr_list[i] & ~0x0f) == 0x50
1192 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001193 union i2c_smbus_data data;
1194
Jean Delvare12b5053a2007-05-01 23:26:31 +02001195 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1196 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001197 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001198 break;
1199 } else {
1200 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1201 I2C_SMBUS_WRITE, 0,
1202 I2C_SMBUS_QUICK, NULL) >= 0)
1203 break;
1204 }
1205 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001206
1207 if (addr_list[i] == I2C_CLIENT_END) {
1208 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1209 return NULL;
1210 }
1211
1212 info->addr = addr_list[i];
1213 return i2c_new_device(adap, info);
1214}
1215EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217struct i2c_adapter* i2c_get_adapter(int id)
1218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001220
Jean Delvarecaada322008-01-27 18:14:49 +01001221 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001222 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001223 if (adapter && !try_module_get(adapter->owner))
1224 adapter = NULL;
1225
Jean Delvarecaada322008-01-27 18:14:49 +01001226 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001227 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
David Brownellc0564602007-05-01 23:26:31 +02001229EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231void i2c_put_adapter(struct i2c_adapter *adap)
1232{
1233 module_put(adap->owner);
1234}
David Brownellc0564602007-05-01 23:26:31 +02001235EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237/* The SMBus parts */
1238
David Brownell438d6c22006-12-10 21:21:31 +01001239#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001240static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001245 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 data = data ^ POLY;
1247 data = data << 1;
1248 }
1249 return (u8)(data >> 8);
1250}
1251
Jean Delvare421ef472005-10-26 21:28:55 +02001252/* Incremental CRC8 over count bytes in the array pointed to by p */
1253static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 int i;
1256
1257 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001258 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return crc;
1260}
1261
Jean Delvare421ef472005-10-26 21:28:55 +02001262/* Assume a 7-bit address, which is reasonable for SMBus */
1263static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Jean Delvare421ef472005-10-26 21:28:55 +02001265 /* The address will be sent first */
1266 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1267 pec = i2c_smbus_pec(pec, &addr, 1);
1268
1269 /* The data buffer follows */
1270 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Jean Delvare421ef472005-10-26 21:28:55 +02001273/* Used for write only transactions */
1274static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Jean Delvare421ef472005-10-26 21:28:55 +02001276 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1277 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Jean Delvare421ef472005-10-26 21:28:55 +02001280/* Return <0 on CRC error
1281 If there was a write before this read (most cases) we need to take the
1282 partial CRC from the write part into account.
1283 Note that this function does modify the message (we need to decrease the
1284 message length to hide the CRC byte from the caller). */
1285static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Jean Delvare421ef472005-10-26 21:28:55 +02001287 u8 rpec = msg->buf[--msg->len];
1288 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (rpec != cpec) {
1291 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1292 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001293 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
David Brownell438d6c22006-12-10 21:21:31 +01001295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
David Brownella1cdeda2008-07-14 22:38:24 +02001298/**
1299 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1300 * @client: Handle to slave device
1301 *
1302 * This executes the SMBus "receive byte" protocol, returning negative errno
1303 * else the byte received from the device.
1304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305s32 i2c_smbus_read_byte(struct i2c_client *client)
1306{
1307 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001308 int status;
1309
1310 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1311 I2C_SMBUS_READ, 0,
1312 I2C_SMBUS_BYTE, &data);
1313 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
David Brownellc0564602007-05-01 23:26:31 +02001315EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
David Brownella1cdeda2008-07-14 22:38:24 +02001317/**
1318 * i2c_smbus_write_byte - SMBus "send byte" protocol
1319 * @client: Handle to slave device
1320 * @value: Byte to be sent
1321 *
1322 * This executes the SMBus "send byte" protocol, returning negative errno
1323 * else zero on success.
1324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001328 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
David Brownellc0564602007-05-01 23:26:31 +02001330EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
David Brownella1cdeda2008-07-14 22:38:24 +02001332/**
1333 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1334 * @client: Handle to slave device
1335 * @command: Byte interpreted by slave
1336 *
1337 * This executes the SMBus "read byte" protocol, returning negative errno
1338 * else a data byte received from the device.
1339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1341{
1342 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001343 int status;
1344
1345 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1346 I2C_SMBUS_READ, command,
1347 I2C_SMBUS_BYTE_DATA, &data);
1348 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
David Brownellc0564602007-05-01 23:26:31 +02001350EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
David Brownella1cdeda2008-07-14 22:38:24 +02001352/**
1353 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1354 * @client: Handle to slave device
1355 * @command: Byte interpreted by slave
1356 * @value: Byte being written
1357 *
1358 * This executes the SMBus "write byte" protocol, returning negative errno
1359 * else zero on success.
1360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1362{
1363 union i2c_smbus_data data;
1364 data.byte = value;
1365 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1366 I2C_SMBUS_WRITE,command,
1367 I2C_SMBUS_BYTE_DATA,&data);
1368}
David Brownellc0564602007-05-01 23:26:31 +02001369EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
David Brownella1cdeda2008-07-14 22:38:24 +02001371/**
1372 * i2c_smbus_read_word_data - SMBus "read word" protocol
1373 * @client: Handle to slave device
1374 * @command: Byte interpreted by slave
1375 *
1376 * This executes the SMBus "read word" protocol, returning negative errno
1377 * else a 16-bit unsigned "word" received from the device.
1378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1380{
1381 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001382 int status;
1383
1384 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1385 I2C_SMBUS_READ, command,
1386 I2C_SMBUS_WORD_DATA, &data);
1387 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
David Brownellc0564602007-05-01 23:26:31 +02001389EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
David Brownella1cdeda2008-07-14 22:38:24 +02001391/**
1392 * i2c_smbus_write_word_data - SMBus "write word" protocol
1393 * @client: Handle to slave device
1394 * @command: Byte interpreted by slave
1395 * @value: 16-bit "word" being written
1396 *
1397 * This executes the SMBus "write word" protocol, returning negative errno
1398 * else zero on success.
1399 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1401{
1402 union i2c_smbus_data data;
1403 data.word = value;
1404 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1405 I2C_SMBUS_WRITE,command,
1406 I2C_SMBUS_WORD_DATA,&data);
1407}
David Brownellc0564602007-05-01 23:26:31 +02001408EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
David Brownella64ec072007-10-13 23:56:31 +02001410/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001411 * i2c_smbus_process_call - SMBus "process call" protocol
1412 * @client: Handle to slave device
1413 * @command: Byte interpreted by slave
1414 * @value: 16-bit "word" being written
1415 *
1416 * This executes the SMBus "process call" protocol, returning negative errno
1417 * else a 16-bit unsigned "word" received from the device.
1418 */
1419s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1420{
1421 union i2c_smbus_data data;
1422 int status;
1423 data.word = value;
1424
1425 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1426 I2C_SMBUS_WRITE, command,
1427 I2C_SMBUS_PROC_CALL, &data);
1428 return (status < 0) ? status : data.word;
1429}
1430EXPORT_SYMBOL(i2c_smbus_process_call);
1431
1432/**
David Brownella1cdeda2008-07-14 22:38:24 +02001433 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001434 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001435 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001436 * @values: Byte array into which data will be read; big enough to hold
1437 * the data returned by the slave. SMBus allows at most 32 bytes.
1438 *
David Brownella1cdeda2008-07-14 22:38:24 +02001439 * This executes the SMBus "block read" protocol, returning negative errno
1440 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001441 *
1442 * Note that using this function requires that the client's adapter support
1443 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1444 * support this; its emulation through I2C messaging relies on a specific
1445 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1446 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001447s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1448 u8 *values)
1449{
1450 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001451 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001452
David Brownell24a5bb72008-07-14 22:38:23 +02001453 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1454 I2C_SMBUS_READ, command,
1455 I2C_SMBUS_BLOCK_DATA, &data);
1456 if (status)
1457 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001458
1459 memcpy(values, &data.block[1], data.block[0]);
1460 return data.block[0];
1461}
1462EXPORT_SYMBOL(i2c_smbus_read_block_data);
1463
David Brownella1cdeda2008-07-14 22:38:24 +02001464/**
1465 * i2c_smbus_write_block_data - SMBus "block write" protocol
1466 * @client: Handle to slave device
1467 * @command: Byte interpreted by slave
1468 * @length: Size of data block; SMBus allows at most 32 bytes
1469 * @values: Byte array which will be written.
1470 *
1471 * This executes the SMBus "block write" protocol, returning negative errno
1472 * else zero on success.
1473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001475 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (length > I2C_SMBUS_BLOCK_MAX)
1480 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001482 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1484 I2C_SMBUS_WRITE,command,
1485 I2C_SMBUS_BLOCK_DATA,&data);
1486}
David Brownellc0564602007-05-01 23:26:31 +02001487EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001490s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1491 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001494 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001495
Jean Delvare4b2643d2007-07-12 14:12:29 +02001496 if (length > I2C_SMBUS_BLOCK_MAX)
1497 length = I2C_SMBUS_BLOCK_MAX;
1498 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001499 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1500 I2C_SMBUS_READ, command,
1501 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1502 if (status < 0)
1503 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001504
1505 memcpy(values, &data.block[1], data.block[0]);
1506 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
David Brownellc0564602007-05-01 23:26:31 +02001508EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Jean Delvare21bbd692006-01-09 15:19:18 +11001510s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001511 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001512{
1513 union i2c_smbus_data data;
1514
1515 if (length > I2C_SMBUS_BLOCK_MAX)
1516 length = I2C_SMBUS_BLOCK_MAX;
1517 data.block[0] = length;
1518 memcpy(data.block + 1, values, length);
1519 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1520 I2C_SMBUS_WRITE, command,
1521 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1522}
David Brownellc0564602007-05-01 23:26:31 +02001523EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001524
David Brownell438d6c22006-12-10 21:21:31 +01001525/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001527static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001529 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 union i2c_smbus_data * data)
1531{
1532 /* So we need to generate a series of msgs. In the case of writing, we
1533 need to use only one message; when reading, we need two. We initialize
1534 most things with sane defaults, to keep the code below somewhat
1535 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001536 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1537 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001539 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1541 };
1542 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001543 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001544 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 msgbuf0[0] = command;
1547 switch(size) {
1548 case I2C_SMBUS_QUICK:
1549 msg[0].len = 0;
1550 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001551 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1552 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 num = 1;
1554 break;
1555 case I2C_SMBUS_BYTE:
1556 if (read_write == I2C_SMBUS_READ) {
1557 /* Special case: only a read! */
1558 msg[0].flags = I2C_M_RD | flags;
1559 num = 1;
1560 }
1561 break;
1562 case I2C_SMBUS_BYTE_DATA:
1563 if (read_write == I2C_SMBUS_READ)
1564 msg[1].len = 1;
1565 else {
1566 msg[0].len = 2;
1567 msgbuf0[1] = data->byte;
1568 }
1569 break;
1570 case I2C_SMBUS_WORD_DATA:
1571 if (read_write == I2C_SMBUS_READ)
1572 msg[1].len = 2;
1573 else {
1574 msg[0].len=3;
1575 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001576 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578 break;
1579 case I2C_SMBUS_PROC_CALL:
1580 num = 2; /* Special case */
1581 read_write = I2C_SMBUS_READ;
1582 msg[0].len = 3;
1583 msg[1].len = 2;
1584 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001585 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 break;
1587 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001589 msg[1].flags |= I2C_M_RECV_LEN;
1590 msg[1].len = 1; /* block length will be added by
1591 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 } else {
1593 msg[0].len = data->block[0] + 2;
1594 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001595 dev_err(&adapter->dev,
1596 "Invalid block write size %d\n",
1597 data->block[0]);
1598 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001600 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 msgbuf0[i] = data->block[i-1];
1602 }
1603 break;
1604 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001605 num = 2; /* Another special case */
1606 read_write = I2C_SMBUS_READ;
1607 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001608 dev_err(&adapter->dev,
1609 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001610 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001611 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001612 }
1613 msg[0].len = data->block[0] + 2;
1614 for (i = 1; i < msg[0].len; i++)
1615 msgbuf0[i] = data->block[i-1];
1616 msg[1].flags |= I2C_M_RECV_LEN;
1617 msg[1].len = 1; /* block length will be added by
1618 the underlying bus driver */
1619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 case I2C_SMBUS_I2C_BLOCK_DATA:
1621 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001622 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 } else {
1624 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001625 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001626 dev_err(&adapter->dev,
1627 "Invalid block write size %d\n",
1628 data->block[0]);
1629 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 for (i = 1; i <= data->block[0]; i++)
1632 msgbuf0[i] = data->block[i];
1633 }
1634 break;
1635 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001636 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1637 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
Jean Delvare421ef472005-10-26 21:28:55 +02001640 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1641 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1642 if (i) {
1643 /* Compute PEC if first message is a write */
1644 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001645 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001646 i2c_smbus_add_pec(&msg[0]);
1647 else /* Write followed by read */
1648 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1649 }
1650 /* Ask for PEC if last message is a read */
1651 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001652 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001653 }
1654
David Brownell24a5bb72008-07-14 22:38:23 +02001655 status = i2c_transfer(adapter, msg, num);
1656 if (status < 0)
1657 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Jean Delvare421ef472005-10-26 21:28:55 +02001659 /* Check PEC if last message is a read */
1660 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001661 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1662 if (status < 0)
1663 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001664 }
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (read_write == I2C_SMBUS_READ)
1667 switch(size) {
1668 case I2C_SMBUS_BYTE:
1669 data->byte = msgbuf0[0];
1670 break;
1671 case I2C_SMBUS_BYTE_DATA:
1672 data->byte = msgbuf1[0];
1673 break;
David Brownell438d6c22006-12-10 21:21:31 +01001674 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 case I2C_SMBUS_PROC_CALL:
1676 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1677 break;
1678 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001679 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 data->block[i+1] = msgbuf1[i];
1681 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001682 case I2C_SMBUS_BLOCK_DATA:
1683 case I2C_SMBUS_BLOCK_PROC_CALL:
1684 for (i = 0; i < msgbuf1[0] + 1; i++)
1685 data->block[i] = msgbuf1[i];
1686 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688 return 0;
1689}
1690
David Brownella1cdeda2008-07-14 22:38:24 +02001691/**
1692 * i2c_smbus_xfer - execute SMBus protocol operations
1693 * @adapter: Handle to I2C bus
1694 * @addr: Address of SMBus slave on that bus
1695 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1696 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1697 * @command: Byte interpreted by slave, for protocols which use such bytes
1698 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1699 * @data: Data to be read or written
1700 *
1701 * This executes an SMBus protocol operation, and returns a negative
1702 * errno code else zero on success.
1703 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001704s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001705 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001706 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001708 unsigned long orig_jiffies;
1709 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001715 mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001716
1717 /* Retry automatically on arbitration loss */
1718 orig_jiffies = jiffies;
1719 for (res = 0, try = 0; try <= adapter->retries; try++) {
1720 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1721 read_write, command,
1722 protocol, data);
1723 if (res != -EAGAIN)
1724 break;
1725 if (time_after(jiffies,
1726 orig_jiffies + adapter->timeout))
1727 break;
1728 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001729 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 } else
1731 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001732 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return res;
1735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1739MODULE_DESCRIPTION("I2C-Bus main module");
1740MODULE_LICENSE("GPL");