blob: 06c428b5822e85a42b6998adb84b7c56ffb78d91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010032#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010033#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010034#include <linux/hardirq.h>
35#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
David Brownell9c1600e2007-05-01 23:26:31 +020038#include "i2c-core.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jean Delvarecaada322008-01-27 18:14:49 +010041static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_IDR(i2c_adapter_idr);
43
Jean Delvare4735c982008-07-14 22:38:36 +020044#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
45
Jean Delvaref8a227e2009-06-19 16:58:18 +020046static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020047static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010048
49/* ------------------------------------------------------------------------- */
50
Jean Delvared2653e92008-04-29 23:11:39 +020051static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
52 const struct i2c_client *client)
53{
54 while (id->name[0]) {
55 if (strcmp(client->name, id->name) == 0)
56 return id;
57 id++;
58 }
59 return NULL;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int i2c_device_match(struct device *dev, struct device_driver *drv)
63{
David Brownell7b4fbc52007-05-01 23:26:30 +020064 struct i2c_client *client = to_i2c_client(dev);
65 struct i2c_driver *driver = to_i2c_driver(drv);
66
67 /* make legacy i2c drivers bypass driver model probing entirely;
68 * such drivers scan each i2c adapter/bus themselves.
69 */
David Brownella1d9e6e2007-05-01 23:26:30 +020070 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020071 return 0;
72
Jean Delvared2653e92008-04-29 23:11:39 +020073 /* match on an id table if there is one */
74 if (driver->id_table)
75 return i2c_match_id(driver->id_table, client) != NULL;
76
Jean Delvareeb8a7902008-05-18 20:49:41 +020077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
David Brownell7b4fbc52007-05-01 23:26:30 +020080#ifdef CONFIG_HOTPLUG
81
82/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020083static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020084{
85 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020086
Jean Delvareeb8a7902008-05-18 20:49:41 +020087 if (add_uevent_var(env, "MODALIAS=%s%s",
88 I2C_MODULE_PREFIX, client->name))
89 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020090 dev_dbg(dev, "uevent\n");
91 return 0;
92}
93
94#else
95#define i2c_device_uevent NULL
96#endif /* CONFIG_HOTPLUG */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int i2c_device_probe(struct device *dev)
99{
David Brownell7b4fbc52007-05-01 23:26:30 +0200100 struct i2c_client *client = to_i2c_client(dev);
101 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +0100102 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200103
Jean Delvaree0457442008-07-14 22:38:30 +0200104 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200105 return -ENODEV;
106 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200107 if (!device_can_wakeup(&client->dev))
108 device_init_wakeup(&client->dev,
109 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200110 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200111
Jean Delvaree0457442008-07-14 22:38:30 +0200112 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100113 if (status)
114 client->driver = NULL;
115 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static int i2c_device_remove(struct device *dev)
119{
David Brownella1d9e6e2007-05-01 23:26:30 +0200120 struct i2c_client *client = to_i2c_client(dev);
121 struct i2c_driver *driver;
122 int status;
123
124 if (!dev->driver)
125 return 0;
126
127 driver = to_i2c_driver(dev->driver);
128 if (driver->remove) {
129 dev_dbg(dev, "remove\n");
130 status = driver->remove(client);
131 } else {
132 dev->driver = NULL;
133 status = 0;
134 }
135 if (status == 0)
136 client->driver = NULL;
137 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
David Brownellf37dd802007-02-13 22:09:00 +0100140static void i2c_device_shutdown(struct device *dev)
141{
142 struct i2c_driver *driver;
143
144 if (!dev->driver)
145 return;
146 driver = to_i2c_driver(dev->driver);
147 if (driver->shutdown)
148 driver->shutdown(to_i2c_client(dev));
149}
150
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100151static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100152{
153 struct i2c_driver *driver;
154
155 if (!dev->driver)
156 return 0;
157 driver = to_i2c_driver(dev->driver);
158 if (!driver->suspend)
159 return 0;
160 return driver->suspend(to_i2c_client(dev), mesg);
161}
162
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100163static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100164{
165 struct i2c_driver *driver;
166
167 if (!dev->driver)
168 return 0;
169 driver = to_i2c_driver(dev->driver);
170 if (!driver->resume)
171 return 0;
172 return driver->resume(to_i2c_client(dev));
173}
174
David Brownell7b4fbc52007-05-01 23:26:30 +0200175static void i2c_client_release(struct device *dev)
176{
177 struct i2c_client *client = to_i2c_client(dev);
178 complete(&client->released);
179}
180
David Brownell9c1600e2007-05-01 23:26:31 +0200181static void i2c_client_dev_release(struct device *dev)
182{
183 kfree(to_i2c_client(dev));
184}
185
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100186static ssize_t
187show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200188{
189 struct i2c_client *client = to_i2c_client(dev);
190 return sprintf(buf, "%s\n", client->name);
191}
192
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100193static ssize_t
194show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200195{
196 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200197 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200198}
199
200static struct device_attribute i2c_dev_attrs[] = {
201 __ATTR(name, S_IRUGO, show_client_name, NULL),
202 /* modalias helps coldplug: modprobe $(cat .../modalias) */
203 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
204 { },
205};
206
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200207struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100208 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200209 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100210 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200211 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100212 .probe = i2c_device_probe,
213 .remove = i2c_device_remove,
214 .shutdown = i2c_device_shutdown,
215 .suspend = i2c_device_suspend,
216 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000217};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200218EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000219
David Brownell9b766b82008-01-27 18:14:51 +0100220
221/**
222 * i2c_verify_client - return parameter as i2c_client, or NULL
223 * @dev: device, probably from some driver model iterator
224 *
225 * When traversing the driver model tree, perhaps using driver model
226 * iterators like @device_for_each_child(), you can't assume very much
227 * about the nodes you find. Use this function to avoid oopses caused
228 * by wrongly treating some non-I2C device as an i2c_client.
229 */
230struct i2c_client *i2c_verify_client(struct device *dev)
231{
232 return (dev->bus == &i2c_bus_type)
233 ? to_i2c_client(dev)
234 : NULL;
235}
236EXPORT_SYMBOL(i2c_verify_client);
237
238
David Brownell9c1600e2007-05-01 23:26:31 +0200239/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200240 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200241 * @adap: the adapter managing the device
242 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200243 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200244 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200245 * Create an i2c device. Binding is handled through driver model
246 * probe()/remove() methods. A driver may be bound to this device when we
247 * return from this function, or any later moment (e.g. maybe hotplugging will
248 * load the driver module). This call is not appropriate for use by mainboard
249 * initialization logic, which usually runs during an arch_initcall() long
250 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200251 *
252 * This returns the new i2c client, which may be saved for later use with
253 * i2c_unregister_device(); or NULL to indicate an error.
254 */
255struct i2c_client *
256i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
257{
258 struct i2c_client *client;
259 int status;
260
261 client = kzalloc(sizeof *client, GFP_KERNEL);
262 if (!client)
263 return NULL;
264
265 client->adapter = adap;
266
267 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200268
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200269 if (info->archdata)
270 client->dev.archdata = *info->archdata;
271
Marc Pignatee354252008-08-28 08:33:22 +0200272 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200273 client->addr = info->addr;
274 client->irq = info->irq;
275
David Brownell9c1600e2007-05-01 23:26:31 +0200276 strlcpy(client->name, info->type, sizeof(client->name));
277
Jean Delvaref8a227e2009-06-19 16:58:18 +0200278 /* Check for address business */
279 status = i2c_check_addr(adap, client->addr);
280 if (status)
281 goto out_err;
282
283 client->dev.parent = &client->adapter->dev;
284 client->dev.bus = &i2c_bus_type;
285
286 if (client->driver && !is_newstyle_driver(client->driver)) {
287 client->dev.release = i2c_client_release;
288 dev_set_uevent_suppress(&client->dev, 1);
289 } else
290 client->dev.release = i2c_client_dev_release;
291
292 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
293 client->addr);
294 status = device_register(&client->dev);
295 if (status)
296 goto out_err;
297
298 mutex_lock(&adap->clist_lock);
299 list_add_tail(&client->list, &adap->clients);
300 mutex_unlock(&adap->clist_lock);
301
302 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
303 client->name, dev_name(&client->dev));
304
David Brownell9c1600e2007-05-01 23:26:31 +0200305 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200306
307out_err:
308 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
309 "(%d)\n", client->name, client->addr, status);
310 kfree(client);
311 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200312}
313EXPORT_SYMBOL_GPL(i2c_new_device);
314
315
316/**
317 * i2c_unregister_device - reverse effect of i2c_new_device()
318 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200319 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200320 */
321void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200322{
323 struct i2c_adapter *adapter = client->adapter;
324 struct i2c_driver *driver = client->driver;
325
326 if (driver && !is_newstyle_driver(driver)) {
327 dev_err(&client->dev, "can't unregister devices "
328 "with legacy drivers\n");
329 WARN_ON(1);
330 return;
331 }
332
333 mutex_lock(&adapter->clist_lock);
334 list_del(&client->list);
335 mutex_unlock(&adapter->clist_lock);
336
337 device_unregister(&client->dev);
338}
David Brownell9c1600e2007-05-01 23:26:31 +0200339EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200340
341
Jean Delvare60b129d2008-05-11 20:37:06 +0200342static const struct i2c_device_id dummy_id[] = {
343 { "dummy", 0 },
344 { },
345};
346
Jean Delvared2653e92008-04-29 23:11:39 +0200347static int dummy_probe(struct i2c_client *client,
348 const struct i2c_device_id *id)
349{
350 return 0;
351}
352
353static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100354{
355 return 0;
356}
357
358static struct i2c_driver dummy_driver = {
359 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200360 .probe = dummy_probe,
361 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200362 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100363};
364
365/**
366 * i2c_new_dummy - return a new i2c device bound to a dummy driver
367 * @adapter: the adapter managing the device
368 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100369 * Context: can sleep
370 *
371 * This returns an I2C client bound to the "dummy" driver, intended for use
372 * with devices that consume multiple addresses. Examples of such chips
373 * include various EEPROMS (like 24c04 and 24c08 models).
374 *
375 * These dummy devices have two main uses. First, most I2C and SMBus calls
376 * except i2c_transfer() need a client handle; the dummy will be that handle.
377 * And second, this prevents the specified address from being bound to a
378 * different driver.
379 *
380 * This returns the new i2c client, which should be saved for later use with
381 * i2c_unregister_device(); or NULL to indicate an error.
382 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100383struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100384{
385 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200386 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100387 };
388
David Brownelle9f13732008-01-27 18:14:52 +0100389 return i2c_new_device(adapter, &info);
390}
391EXPORT_SYMBOL_GPL(i2c_new_dummy);
392
David Brownellf37dd802007-02-13 22:09:00 +0100393/* ------------------------------------------------------------------------- */
394
David Brownell16ffadf2007-05-01 23:26:28 +0200395/* I2C bus adapters -- one roots each I2C or SMBUS segment */
396
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200397static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
David Brownellef2c8322007-05-01 23:26:28 +0200399 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 complete(&adap->dev_released);
401}
402
David Brownell16ffadf2007-05-01 23:26:28 +0200403static ssize_t
404show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
David Brownellef2c8322007-05-01 23:26:28 +0200406 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return sprintf(buf, "%s\n", adap->name);
408}
David Brownell16ffadf2007-05-01 23:26:28 +0200409
410static struct device_attribute i2c_adapter_attrs[] = {
411 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
412 { },
413};
414
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200415static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200416 .owner = THIS_MODULE,
417 .name = "i2c-adapter",
418 .dev_attrs = i2c_adapter_attrs,
419};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
David Brownell9c1600e2007-05-01 23:26:31 +0200421static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
422{
423 struct i2c_devinfo *devinfo;
424
425 mutex_lock(&__i2c_board_lock);
426 list_for_each_entry(devinfo, &__i2c_board_list, list) {
427 if (devinfo->busnum == adapter->nr
428 && !i2c_new_device(adapter,
429 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100430 dev_err(&adapter->dev,
431 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200432 devinfo->board_info.addr);
433 }
434 mutex_unlock(&__i2c_board_lock);
435}
436
Jean Delvare026526f2008-01-27 18:14:49 +0100437static int i2c_do_add_adapter(struct device_driver *d, void *data)
438{
439 struct i2c_driver *driver = to_i2c_driver(d);
440 struct i2c_adapter *adap = data;
441
Jean Delvare4735c982008-07-14 22:38:36 +0200442 /* Detect supported devices on that bus, and instantiate them */
443 i2c_detect(adap, driver);
444
445 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100446 if (driver->attach_adapter) {
447 /* We ignore the return code; if it fails, too bad */
448 driver->attach_adapter(adap);
449 }
450 return 0;
451}
452
David Brownell6e13e642007-05-01 23:26:31 +0200453static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jean Delvare026526f2008-01-27 18:14:49 +0100455 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
David Brownell1d0b19c2008-10-14 17:30:05 +0200457 /* Can't register until after driver model init */
458 if (unlikely(WARN_ON(!i2c_bus_type.p)))
459 return -EAGAIN;
460
Ingo Molnar5c085d32006-01-18 23:16:04 +0100461 mutex_init(&adap->bus_lock);
462 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 INIT_LIST_HEAD(&adap->clients);
464
Jean Delvarecaada322008-01-27 18:14:49 +0100465 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200466
Jean Delvare8fcfef62009-03-28 21:34:43 +0100467 /* Set default timeout to 1 second if not already set */
468 if (adap->timeout == 0)
469 adap->timeout = HZ;
470
Kay Sievers27d9c182009-01-07 14:29:16 +0100471 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200473 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200474 res = device_register(&adap->dev);
475 if (res)
476 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200478 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
479
Jean Delvare729d6dd2009-06-19 16:58:18 +0200480 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200481 if (adap->nr < __i2c_first_dynamic_bus_num)
482 i2c_scan_static_board_info(adap);
483
Jean Delvare4735c982008-07-14 22:38:36 +0200484 /* Notify drivers */
Jean Delvare026526f2008-01-27 18:14:49 +0100485 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
486 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100489 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200491
Jean Delvareb119c6c2006-08-15 18:26:30 +0200492out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200493 idr_remove(&i2c_adapter_idr, adap->nr);
494 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
David Brownell6e13e642007-05-01 23:26:31 +0200497/**
498 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
499 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200500 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200501 *
502 * This routine is used to declare an I2C adapter when its bus number
503 * doesn't matter. Examples: for I2C adapters dynamically added by
504 * USB links or PCI plugin cards.
505 *
506 * When this returns zero, a new bus number was allocated and stored
507 * in adap->nr, and the specified adapter became available for clients.
508 * Otherwise, a negative errno value is returned.
509 */
510int i2c_add_adapter(struct i2c_adapter *adapter)
511{
512 int id, res = 0;
513
514retry:
515 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
516 return -ENOMEM;
517
Jean Delvarecaada322008-01-27 18:14:49 +0100518 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200519 /* "above" here means "above or equal to", sigh */
520 res = idr_get_new_above(&i2c_adapter_idr, adapter,
521 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100522 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200523
524 if (res < 0) {
525 if (res == -EAGAIN)
526 goto retry;
527 return res;
528 }
529
530 adapter->nr = id;
531 return i2c_register_adapter(adapter);
532}
533EXPORT_SYMBOL(i2c_add_adapter);
534
535/**
536 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
537 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200538 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200539 *
540 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100541 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
542 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200543 * is used to properly configure I2C devices.
544 *
545 * If no devices have pre-been declared for this bus, then be sure to
546 * register the adapter before any dynamically allocated ones. Otherwise
547 * the required bus ID may not be available.
548 *
549 * When this returns zero, the specified adapter became available for
550 * clients using the bus number provided in adap->nr. Also, the table
551 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
552 * and the appropriate driver model device nodes are created. Otherwise, a
553 * negative errno value is returned.
554 */
555int i2c_add_numbered_adapter(struct i2c_adapter *adap)
556{
557 int id;
558 int status;
559
560 if (adap->nr & ~MAX_ID_MASK)
561 return -EINVAL;
562
563retry:
564 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
565 return -ENOMEM;
566
Jean Delvarecaada322008-01-27 18:14:49 +0100567 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200568 /* "above" here means "above or equal to", sigh;
569 * we need the "equal to" result to force the result
570 */
571 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
572 if (status == 0 && id != adap->nr) {
573 status = -EBUSY;
574 idr_remove(&i2c_adapter_idr, id);
575 }
Jean Delvarecaada322008-01-27 18:14:49 +0100576 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200577 if (status == -EAGAIN)
578 goto retry;
579
580 if (status == 0)
581 status = i2c_register_adapter(adap);
582 return status;
583}
584EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
585
Jean Delvare026526f2008-01-27 18:14:49 +0100586static int i2c_do_del_adapter(struct device_driver *d, void *data)
587{
588 struct i2c_driver *driver = to_i2c_driver(d);
589 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200590 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100591 int res;
592
Jean Delvareacec2112009-03-28 21:34:40 +0100593 /* Remove the devices we created ourselves as the result of hardware
594 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200595 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
596 if (client->adapter == adapter) {
597 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
598 client->name, client->addr);
599 list_del(&client->detected);
600 i2c_unregister_device(client);
601 }
602 }
603
Jean Delvare026526f2008-01-27 18:14:49 +0100604 if (!driver->detach_adapter)
605 return 0;
606 res = driver->detach_adapter(adapter);
607 if (res)
608 dev_err(&adapter->dev, "detach_adapter failed (%d) "
609 "for driver [%s]\n", res, driver->driver.name);
610 return res;
611}
612
David Brownelld64f73b2007-07-12 14:12:28 +0200613/**
614 * i2c_del_adapter - unregister I2C adapter
615 * @adap: the adapter being unregistered
616 * Context: can sleep
617 *
618 * This unregisters an I2C adapter which was previously registered
619 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621int i2c_del_adapter(struct i2c_adapter *adap)
622{
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200623 struct i2c_client *client, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int res = 0;
625
Jean Delvarecaada322008-01-27 18:14:49 +0100626 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100629 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200630 pr_debug("i2c-core: attempting to delete unregistered "
631 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 res = -EINVAL;
633 goto out_unlock;
634 }
635
Jean Delvare026526f2008-01-27 18:14:49 +0100636 /* Tell drivers about this removal */
637 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
638 i2c_do_del_adapter);
639 if (res)
640 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jean Delvare729d6dd2009-06-19 16:58:18 +0200642 /* Detach any active clients */
Jean Delvare79b93e12008-11-28 15:24:38 +0100643 list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
Jean Delvare729d6dd2009-06-19 16:58:18 +0200644 i2c_unregister_device(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 /* clean up the sysfs representation */
648 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 /* wait for sysfs to drop all references */
652 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
David Brownell6e13e642007-05-01 23:26:31 +0200654 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 idr_remove(&i2c_adapter_idr, adap->nr);
656
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200657 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Jean Delvarebd4bc3d2008-07-16 19:30:05 +0200659 /* Clear the device structure in case this adapter is ever going to be
660 added again */
661 memset(&adap->dev, 0, sizeof(adap->dev));
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100664 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return res;
666}
David Brownellc0564602007-05-01 23:26:31 +0200667EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669
David Brownell7b4fbc52007-05-01 23:26:30 +0200670/* ------------------------------------------------------------------------- */
671
Dave Young7f101a92008-07-14 22:38:19 +0200672static int __attach_adapter(struct device *dev, void *data)
673{
674 struct i2c_adapter *adapter = to_i2c_adapter(dev);
675 struct i2c_driver *driver = data;
676
Jean Delvare4735c982008-07-14 22:38:36 +0200677 i2c_detect(adapter, driver);
678
679 /* Legacy drivers scan i2c busses directly */
680 if (driver->attach_adapter)
681 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200682
683 return 0;
684}
685
David Brownell7b4fbc52007-05-01 23:26:30 +0200686/*
687 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200688 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
690
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800691int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100693 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David Brownell1d0b19c2008-10-14 17:30:05 +0200695 /* Can't register until after driver model init */
696 if (unlikely(WARN_ON(!i2c_bus_type.p)))
697 return -EAGAIN;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800700 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Jean Delvare729d6dd2009-06-19 16:58:18 +0200703 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200704 * will have called probe() for all matching-but-unbound devices.
705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 res = driver_register(&driver->driver);
707 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100708 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100709
Jean Delvarecaada322008-01-27 18:14:49 +0100710 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100711
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100712 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Jean Delvare4735c982008-07-14 22:38:36 +0200714 INIT_LIST_HEAD(&driver->clients);
715 /* Walk the adapters that are already present */
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400716 class_for_each_device(&i2c_adapter_class, NULL, driver,
717 __attach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Jean Delvarecaada322008-01-27 18:14:49 +0100719 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800722EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Dave Young7f101a92008-07-14 22:38:19 +0200724static int __detach_adapter(struct device *dev, void *data)
725{
726 struct i2c_adapter *adapter = to_i2c_adapter(dev);
727 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200728 struct i2c_client *client, *_n;
729
Jean Delvareacec2112009-03-28 21:34:40 +0100730 /* Remove the devices we created ourselves as the result of hardware
731 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200732 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
733 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
734 client->name, client->addr);
735 list_del(&client->detected);
736 i2c_unregister_device(client);
737 }
738
739 if (is_newstyle_driver(driver))
740 return 0;
Dave Young7f101a92008-07-14 22:38:19 +0200741
Dave Young7f101a92008-07-14 22:38:19 +0200742 if (driver->detach_adapter) {
743 if (driver->detach_adapter(adapter))
744 dev_err(&adapter->dev,
745 "detach_adapter failed for driver [%s]\n",
746 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200747 }
748
749 return 0;
750}
751
David Brownella1d9e6e2007-05-01 23:26:30 +0200752/**
753 * i2c_del_driver - unregister I2C driver
754 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200755 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200756 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200757void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Jean Delvarecaada322008-01-27 18:14:49 +0100759 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400761 class_for_each_device(&i2c_adapter_class, NULL, driver,
762 __detach_adapter);
David Brownella1d9e6e2007-05-01 23:26:30 +0200763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100765 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jean Delvarecaada322008-01-27 18:14:49 +0100767 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
David Brownellc0564602007-05-01 23:26:31 +0200769EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
David Brownell7b4fbc52007-05-01 23:26:30 +0200771/* ------------------------------------------------------------------------- */
772
David Brownell9b766b82008-01-27 18:14:51 +0100773static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
David Brownell9b766b82008-01-27 18:14:51 +0100775 struct i2c_client *client = i2c_verify_client(dev);
776 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David Brownell9b766b82008-01-27 18:14:51 +0100778 if (client && client->addr == addr)
779 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781}
782
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100783static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David Brownell9b766b82008-01-27 18:14:51 +0100785 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Jean Delvaree48d3312008-01-27 18:14:48 +0100788/**
789 * i2c_use_client - increments the reference count of the i2c client structure
790 * @client: the client being referenced
791 *
792 * Each live reference to a client should be refcounted. The driver model does
793 * that automatically as part of driver binding, so that most drivers don't
794 * need to do this explicitly: they hold a reference until they're unbound
795 * from the device.
796 *
797 * A pointer to the client with the incremented reference counter is returned.
798 */
799struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
David Brownell6ea438e2008-07-14 22:38:24 +0200801 if (client && get_device(&client->dev))
802 return client;
803 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
David Brownellc0564602007-05-01 23:26:31 +0200805EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Jean Delvaree48d3312008-01-27 18:14:48 +0100807/**
808 * i2c_release_client - release a use of the i2c client structure
809 * @client: the client being no longer referenced
810 *
811 * Must be called when a user of a client is finished with it.
812 */
813void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
David Brownell6ea438e2008-07-14 22:38:24 +0200815 if (client)
816 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
David Brownellc0564602007-05-01 23:26:31 +0200818EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
David Brownell9b766b82008-01-27 18:14:51 +0100820struct i2c_cmd_arg {
821 unsigned cmd;
822 void *arg;
823};
824
825static int i2c_cmd(struct device *dev, void *_arg)
826{
827 struct i2c_client *client = i2c_verify_client(dev);
828 struct i2c_cmd_arg *arg = _arg;
829
830 if (client && client->driver && client->driver->command)
831 client->driver->command(client, arg->cmd, arg->arg);
832 return 0;
833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
836{
David Brownell9b766b82008-01-27 18:14:51 +0100837 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
David Brownell9b766b82008-01-27 18:14:51 +0100839 cmd_arg.cmd = cmd;
840 cmd_arg.arg = arg;
841 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
David Brownellc0564602007-05-01 23:26:31 +0200843EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845static int __init i2c_init(void)
846{
847 int retval;
848
849 retval = bus_register(&i2c_bus_type);
850 if (retval)
851 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100852 retval = class_register(&i2c_adapter_class);
853 if (retval)
854 goto bus_err;
855 retval = i2c_add_driver(&dummy_driver);
856 if (retval)
857 goto class_err;
858 return 0;
859
860class_err:
861 class_unregister(&i2c_adapter_class);
862bus_err:
863 bus_unregister(&i2c_bus_type);
864 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867static void __exit i2c_exit(void)
868{
David Brownelle9f13732008-01-27 18:14:52 +0100869 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 bus_unregister(&i2c_bus_type);
872}
873
David Brownella10f9e72008-10-14 17:30:06 +0200874/* We must initialize early, because some subsystems register i2c drivers
875 * in subsys_initcall() code, but are linked (and initialized) before i2c.
876 */
877postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878module_exit(i2c_exit);
879
880/* ----------------------------------------------------
881 * the functional interface to the i2c busses.
882 * ----------------------------------------------------
883 */
884
David Brownella1cdeda2008-07-14 22:38:24 +0200885/**
886 * i2c_transfer - execute a single or combined I2C message
887 * @adap: Handle to I2C bus
888 * @msgs: One or more messages to execute before STOP is issued to
889 * terminate the operation; each message begins with a START.
890 * @num: Number of messages to be executed.
891 *
892 * Returns negative errno, else the number of messages executed.
893 *
894 * Note that there is no requirement that each message be sent to
895 * the same slave address, although that is the most common model.
896 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100897int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Clifford Wolf66b650f2009-06-15 18:01:46 +0200899 unsigned long orig_jiffies;
900 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
David Brownella1cdeda2008-07-14 22:38:24 +0200902 /* REVISIT the fault reporting model here is weak:
903 *
904 * - When we get an error after receiving N bytes from a slave,
905 * there is no way to report "N".
906 *
907 * - When we get a NAK after transmitting N bytes to a slave,
908 * there is no way to report "N" ... or to let the master
909 * continue executing the rest of this combined message, if
910 * that's the appropriate response.
911 *
912 * - When for example "num" is two and we successfully complete
913 * the first message but get an error part way through the
914 * second, it's unclear whether that should be reported as
915 * one (discarding status on the second message) or errno
916 * (discarding status on the first one).
917 */
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (adap->algo->master_xfer) {
920#ifdef DEBUG
921 for (ret = 0; ret < num; ret++) {
922 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200923 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
924 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
925 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927#endif
928
Mike Rapoportcea443a2008-01-27 18:14:50 +0100929 if (in_atomic() || irqs_disabled()) {
930 ret = mutex_trylock(&adap->bus_lock);
931 if (!ret)
932 /* I2C activity is ongoing. */
933 return -EAGAIN;
934 } else {
935 mutex_lock_nested(&adap->bus_lock, adap->level);
936 }
937
Clifford Wolf66b650f2009-06-15 18:01:46 +0200938 /* Retry automatically on arbitration loss */
939 orig_jiffies = jiffies;
940 for (ret = 0, try = 0; try <= adap->retries; try++) {
941 ret = adap->algo->master_xfer(adap, msgs, num);
942 if (ret != -EAGAIN)
943 break;
944 if (time_after(jiffies, orig_jiffies + adap->timeout))
945 break;
946 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100947 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 return ret;
950 } else {
951 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +0200952 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954}
David Brownellc0564602007-05-01 23:26:31 +0200955EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
David Brownella1cdeda2008-07-14 22:38:24 +0200957/**
958 * i2c_master_send - issue a single I2C message in master transmit mode
959 * @client: Handle to slave device
960 * @buf: Data that will be written to the slave
961 * @count: How many bytes to write
962 *
963 * Returns negative errno, or else the number of bytes written.
964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
966{
967 int ret;
968 struct i2c_adapter *adap=client->adapter;
969 struct i2c_msg msg;
970
Jean Delvare815f55f2005-05-07 22:58:46 +0200971 msg.addr = client->addr;
972 msg.flags = client->flags & I2C_M_TEN;
973 msg.len = count;
974 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100975
Jean Delvare815f55f2005-05-07 22:58:46 +0200976 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Jean Delvare815f55f2005-05-07 22:58:46 +0200978 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
979 transmitted, else error code. */
980 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
David Brownellc0564602007-05-01 23:26:31 +0200982EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
David Brownella1cdeda2008-07-14 22:38:24 +0200984/**
985 * i2c_master_recv - issue a single I2C message in master receive mode
986 * @client: Handle to slave device
987 * @buf: Where to store data read from slave
988 * @count: How many bytes to read
989 *
990 * Returns negative errno, or else the number of bytes read.
991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
993{
994 struct i2c_adapter *adap=client->adapter;
995 struct i2c_msg msg;
996 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Jean Delvare815f55f2005-05-07 22:58:46 +0200998 msg.addr = client->addr;
999 msg.flags = client->flags & I2C_M_TEN;
1000 msg.flags |= I2C_M_RD;
1001 msg.len = count;
1002 msg.buf = buf;
1003
1004 ret = i2c_transfer(adap, &msg, 1);
1005
1006 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1007 transmitted, else error code. */
1008 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
David Brownellc0564602007-05-01 23:26:31 +02001010EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012/* ----------------------------------------------------
1013 * the i2c address scanning function
1014 * Will not work for 10-bit addresses!
1015 * ----------------------------------------------------
1016 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001017
Jean Delvare4735c982008-07-14 22:38:36 +02001018static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1019 struct i2c_driver *driver)
1020{
1021 struct i2c_board_info info;
1022 struct i2c_adapter *adapter = temp_client->adapter;
1023 int addr = temp_client->addr;
1024 int err;
1025
1026 /* Make sure the address is valid */
1027 if (addr < 0x03 || addr > 0x77) {
1028 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1029 addr);
1030 return -EINVAL;
1031 }
1032
1033 /* Skip if already in use */
1034 if (i2c_check_addr(adapter, addr))
1035 return 0;
1036
1037 /* Make sure there is something at this address, unless forced */
1038 if (kind < 0) {
1039 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1040 I2C_SMBUS_QUICK, NULL) < 0)
1041 return 0;
1042
1043 /* prevent 24RF08 corruption */
1044 if ((addr & ~0x0f) == 0x50)
1045 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1046 I2C_SMBUS_QUICK, NULL);
1047 }
1048
1049 /* Finally call the custom detection function */
1050 memset(&info, 0, sizeof(struct i2c_board_info));
1051 info.addr = addr;
1052 err = driver->detect(temp_client, kind, &info);
1053 if (err) {
1054 /* -ENODEV is returned if the detection fails. We catch it
1055 here as this isn't an error. */
1056 return err == -ENODEV ? 0 : err;
1057 }
1058
1059 /* Consistency check */
1060 if (info.type[0] == '\0') {
1061 dev_err(&adapter->dev, "%s detection function provided "
1062 "no name for 0x%x\n", driver->driver.name,
1063 addr);
1064 } else {
1065 struct i2c_client *client;
1066
1067 /* Detection succeeded, instantiate the device */
1068 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1069 info.type, info.addr);
1070 client = i2c_new_device(adapter, &info);
1071 if (client)
1072 list_add_tail(&client->detected, &driver->clients);
1073 else
1074 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1075 info.type, info.addr);
1076 }
1077 return 0;
1078}
1079
1080static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1081{
1082 const struct i2c_client_address_data *address_data;
1083 struct i2c_client *temp_client;
1084 int i, err = 0;
1085 int adap_id = i2c_adapter_id(adapter);
1086
1087 address_data = driver->address_data;
1088 if (!driver->detect || !address_data)
1089 return 0;
1090
1091 /* Set up a temporary client to help detect callback */
1092 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1093 if (!temp_client)
1094 return -ENOMEM;
1095 temp_client->adapter = adapter;
1096
1097 /* Force entries are done first, and are not affected by ignore
1098 entries */
1099 if (address_data->forces) {
1100 const unsigned short * const *forces = address_data->forces;
1101 int kind;
1102
1103 for (kind = 0; forces[kind]; kind++) {
1104 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1105 i += 2) {
1106 if (forces[kind][i] == adap_id
1107 || forces[kind][i] == ANY_I2C_BUS) {
1108 dev_dbg(&adapter->dev, "found force "
1109 "parameter for adapter %d, "
1110 "addr 0x%02x, kind %d\n",
1111 adap_id, forces[kind][i + 1],
1112 kind);
1113 temp_client->addr = forces[kind][i + 1];
1114 err = i2c_detect_address(temp_client,
1115 kind, driver);
1116 if (err)
1117 goto exit_free;
1118 }
1119 }
1120 }
1121 }
1122
Jean Delvare4329cf82008-08-28 08:33:23 +02001123 /* Stop here if the classes do not match */
1124 if (!(adapter->class & driver->class))
1125 goto exit_free;
1126
Jean Delvare4735c982008-07-14 22:38:36 +02001127 /* Stop here if we can't use SMBUS_QUICK */
1128 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1129 if (address_data->probe[0] == I2C_CLIENT_END
1130 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1131 goto exit_free;
1132
1133 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1134 "can't probe for chips\n");
1135 err = -EOPNOTSUPP;
1136 goto exit_free;
1137 }
1138
Jean Delvare4735c982008-07-14 22:38:36 +02001139 /* Probe entries are done second, and are not affected by ignore
1140 entries either */
1141 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1142 if (address_data->probe[i] == adap_id
1143 || address_data->probe[i] == ANY_I2C_BUS) {
1144 dev_dbg(&adapter->dev, "found probe parameter for "
1145 "adapter %d, addr 0x%02x\n", adap_id,
1146 address_data->probe[i + 1]);
1147 temp_client->addr = address_data->probe[i + 1];
1148 err = i2c_detect_address(temp_client, -1, driver);
1149 if (err)
1150 goto exit_free;
1151 }
1152 }
1153
1154 /* Normal entries are done last, unless shadowed by an ignore entry */
1155 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1156 int j, ignore;
1157
1158 ignore = 0;
1159 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1160 j += 2) {
1161 if ((address_data->ignore[j] == adap_id ||
1162 address_data->ignore[j] == ANY_I2C_BUS)
1163 && address_data->ignore[j + 1]
1164 == address_data->normal_i2c[i]) {
1165 dev_dbg(&adapter->dev, "found ignore "
1166 "parameter for adapter %d, "
1167 "addr 0x%02x\n", adap_id,
1168 address_data->ignore[j + 1]);
1169 ignore = 1;
1170 break;
1171 }
1172 }
1173 if (ignore)
1174 continue;
1175
1176 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1177 "addr 0x%02x\n", adap_id,
1178 address_data->normal_i2c[i]);
1179 temp_client->addr = address_data->normal_i2c[i];
1180 err = i2c_detect_address(temp_client, -1, driver);
1181 if (err)
1182 goto exit_free;
1183 }
1184
1185 exit_free:
1186 kfree(temp_client);
1187 return err;
1188}
1189
Jean Delvare12b50532007-05-01 23:26:31 +02001190struct i2c_client *
1191i2c_new_probed_device(struct i2c_adapter *adap,
1192 struct i2c_board_info *info,
1193 unsigned short const *addr_list)
1194{
1195 int i;
1196
1197 /* Stop here if the bus doesn't support probing */
1198 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1199 dev_err(&adap->dev, "Probing not supported\n");
1200 return NULL;
1201 }
1202
Jean Delvare12b50532007-05-01 23:26:31 +02001203 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1204 /* Check address validity */
1205 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1206 dev_warn(&adap->dev, "Invalid 7-bit address "
1207 "0x%02x\n", addr_list[i]);
1208 continue;
1209 }
1210
1211 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001212 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b50532007-05-01 23:26:31 +02001213 dev_dbg(&adap->dev, "Address 0x%02x already in "
1214 "use, not probing\n", addr_list[i]);
1215 continue;
1216 }
1217
1218 /* Test address responsiveness
1219 The default probe method is a quick write, but it is known
1220 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1221 and could also irreversibly write-protect some EEPROMs, so
1222 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1223 read instead. Also, some bus drivers don't implement
1224 quick write, so we fallback to a byte read it that case
1225 too. */
1226 if ((addr_list[i] & ~0x07) == 0x30
1227 || (addr_list[i] & ~0x0f) == 0x50
1228 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001229 union i2c_smbus_data data;
1230
Jean Delvare12b50532007-05-01 23:26:31 +02001231 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1232 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001233 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b50532007-05-01 23:26:31 +02001234 break;
1235 } else {
1236 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1237 I2C_SMBUS_WRITE, 0,
1238 I2C_SMBUS_QUICK, NULL) >= 0)
1239 break;
1240 }
1241 }
Jean Delvare12b50532007-05-01 23:26:31 +02001242
1243 if (addr_list[i] == I2C_CLIENT_END) {
1244 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1245 return NULL;
1246 }
1247
1248 info->addr = addr_list[i];
1249 return i2c_new_device(adap, info);
1250}
1251EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253struct i2c_adapter* i2c_get_adapter(int id)
1254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001256
Jean Delvarecaada322008-01-27 18:14:49 +01001257 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001258 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001259 if (adapter && !try_module_get(adapter->owner))
1260 adapter = NULL;
1261
Jean Delvarecaada322008-01-27 18:14:49 +01001262 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001263 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
David Brownellc0564602007-05-01 23:26:31 +02001265EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267void i2c_put_adapter(struct i2c_adapter *adap)
1268{
1269 module_put(adap->owner);
1270}
David Brownellc0564602007-05-01 23:26:31 +02001271EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273/* The SMBus parts */
1274
David Brownell438d6c22006-12-10 21:21:31 +01001275#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001276static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001281 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 data = data ^ POLY;
1283 data = data << 1;
1284 }
1285 return (u8)(data >> 8);
1286}
1287
Jean Delvare421ef472005-10-26 21:28:55 +02001288/* Incremental CRC8 over count bytes in the array pointed to by p */
1289static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 int i;
1292
1293 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001294 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return crc;
1296}
1297
Jean Delvare421ef472005-10-26 21:28:55 +02001298/* Assume a 7-bit address, which is reasonable for SMBus */
1299static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Jean Delvare421ef472005-10-26 21:28:55 +02001301 /* The address will be sent first */
1302 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1303 pec = i2c_smbus_pec(pec, &addr, 1);
1304
1305 /* The data buffer follows */
1306 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Jean Delvare421ef472005-10-26 21:28:55 +02001309/* Used for write only transactions */
1310static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Jean Delvare421ef472005-10-26 21:28:55 +02001312 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1313 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Jean Delvare421ef472005-10-26 21:28:55 +02001316/* Return <0 on CRC error
1317 If there was a write before this read (most cases) we need to take the
1318 partial CRC from the write part into account.
1319 Note that this function does modify the message (we need to decrease the
1320 message length to hide the CRC byte from the caller). */
1321static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Jean Delvare421ef472005-10-26 21:28:55 +02001323 u8 rpec = msg->buf[--msg->len];
1324 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (rpec != cpec) {
1327 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1328 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001329 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
David Brownell438d6c22006-12-10 21:21:31 +01001331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
David Brownella1cdeda2008-07-14 22:38:24 +02001334/**
1335 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1336 * @client: Handle to slave device
1337 *
1338 * This executes the SMBus "receive byte" protocol, returning negative errno
1339 * else the byte received from the device.
1340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341s32 i2c_smbus_read_byte(struct i2c_client *client)
1342{
1343 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001344 int status;
1345
1346 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1347 I2C_SMBUS_READ, 0,
1348 I2C_SMBUS_BYTE, &data);
1349 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
David Brownellc0564602007-05-01 23:26:31 +02001351EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
David Brownella1cdeda2008-07-14 22:38:24 +02001353/**
1354 * i2c_smbus_write_byte - SMBus "send byte" protocol
1355 * @client: Handle to slave device
1356 * @value: Byte to be sent
1357 *
1358 * This executes the SMBus "send byte" protocol, returning negative errno
1359 * else zero on success.
1360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001364 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
David Brownellc0564602007-05-01 23:26:31 +02001366EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
David Brownella1cdeda2008-07-14 22:38:24 +02001368/**
1369 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1370 * @client: Handle to slave device
1371 * @command: Byte interpreted by slave
1372 *
1373 * This executes the SMBus "read byte" protocol, returning negative errno
1374 * else a data byte received from the device.
1375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1377{
1378 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001379 int status;
1380
1381 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1382 I2C_SMBUS_READ, command,
1383 I2C_SMBUS_BYTE_DATA, &data);
1384 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
David Brownellc0564602007-05-01 23:26:31 +02001386EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
David Brownella1cdeda2008-07-14 22:38:24 +02001388/**
1389 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1390 * @client: Handle to slave device
1391 * @command: Byte interpreted by slave
1392 * @value: Byte being written
1393 *
1394 * This executes the SMBus "write byte" protocol, returning negative errno
1395 * else zero on success.
1396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1398{
1399 union i2c_smbus_data data;
1400 data.byte = value;
1401 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1402 I2C_SMBUS_WRITE,command,
1403 I2C_SMBUS_BYTE_DATA,&data);
1404}
David Brownellc0564602007-05-01 23:26:31 +02001405EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
David Brownella1cdeda2008-07-14 22:38:24 +02001407/**
1408 * i2c_smbus_read_word_data - SMBus "read word" protocol
1409 * @client: Handle to slave device
1410 * @command: Byte interpreted by slave
1411 *
1412 * This executes the SMBus "read word" protocol, returning negative errno
1413 * else a 16-bit unsigned "word" received from the device.
1414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1416{
1417 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001418 int status;
1419
1420 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1421 I2C_SMBUS_READ, command,
1422 I2C_SMBUS_WORD_DATA, &data);
1423 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
David Brownellc0564602007-05-01 23:26:31 +02001425EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
David Brownella1cdeda2008-07-14 22:38:24 +02001427/**
1428 * i2c_smbus_write_word_data - SMBus "write word" protocol
1429 * @client: Handle to slave device
1430 * @command: Byte interpreted by slave
1431 * @value: 16-bit "word" being written
1432 *
1433 * This executes the SMBus "write word" protocol, returning negative errno
1434 * else zero on success.
1435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1437{
1438 union i2c_smbus_data data;
1439 data.word = value;
1440 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1441 I2C_SMBUS_WRITE,command,
1442 I2C_SMBUS_WORD_DATA,&data);
1443}
David Brownellc0564602007-05-01 23:26:31 +02001444EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
David Brownella64ec072007-10-13 23:56:31 +02001446/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001447 * i2c_smbus_process_call - SMBus "process call" protocol
1448 * @client: Handle to slave device
1449 * @command: Byte interpreted by slave
1450 * @value: 16-bit "word" being written
1451 *
1452 * This executes the SMBus "process call" protocol, returning negative errno
1453 * else a 16-bit unsigned "word" received from the device.
1454 */
1455s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1456{
1457 union i2c_smbus_data data;
1458 int status;
1459 data.word = value;
1460
1461 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1462 I2C_SMBUS_WRITE, command,
1463 I2C_SMBUS_PROC_CALL, &data);
1464 return (status < 0) ? status : data.word;
1465}
1466EXPORT_SYMBOL(i2c_smbus_process_call);
1467
1468/**
David Brownella1cdeda2008-07-14 22:38:24 +02001469 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001470 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001471 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001472 * @values: Byte array into which data will be read; big enough to hold
1473 * the data returned by the slave. SMBus allows at most 32 bytes.
1474 *
David Brownella1cdeda2008-07-14 22:38:24 +02001475 * This executes the SMBus "block read" protocol, returning negative errno
1476 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001477 *
1478 * Note that using this function requires that the client's adapter support
1479 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1480 * support this; its emulation through I2C messaging relies on a specific
1481 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1482 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001483s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1484 u8 *values)
1485{
1486 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001487 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001488
David Brownell24a5bb72008-07-14 22:38:23 +02001489 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1490 I2C_SMBUS_READ, command,
1491 I2C_SMBUS_BLOCK_DATA, &data);
1492 if (status)
1493 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001494
1495 memcpy(values, &data.block[1], data.block[0]);
1496 return data.block[0];
1497}
1498EXPORT_SYMBOL(i2c_smbus_read_block_data);
1499
David Brownella1cdeda2008-07-14 22:38:24 +02001500/**
1501 * i2c_smbus_write_block_data - SMBus "block write" protocol
1502 * @client: Handle to slave device
1503 * @command: Byte interpreted by slave
1504 * @length: Size of data block; SMBus allows at most 32 bytes
1505 * @values: Byte array which will be written.
1506 *
1507 * This executes the SMBus "block write" protocol, returning negative errno
1508 * else zero on success.
1509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001511 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (length > I2C_SMBUS_BLOCK_MAX)
1516 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001518 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1520 I2C_SMBUS_WRITE,command,
1521 I2C_SMBUS_BLOCK_DATA,&data);
1522}
David Brownellc0564602007-05-01 23:26:31 +02001523EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001526s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1527 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001530 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001531
Jean Delvare4b2643d2007-07-12 14:12:29 +02001532 if (length > I2C_SMBUS_BLOCK_MAX)
1533 length = I2C_SMBUS_BLOCK_MAX;
1534 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001535 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1536 I2C_SMBUS_READ, command,
1537 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1538 if (status < 0)
1539 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001540
1541 memcpy(values, &data.block[1], data.block[0]);
1542 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
David Brownellc0564602007-05-01 23:26:31 +02001544EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Jean Delvare21bbd692006-01-09 15:19:18 +11001546s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001547 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001548{
1549 union i2c_smbus_data data;
1550
1551 if (length > I2C_SMBUS_BLOCK_MAX)
1552 length = I2C_SMBUS_BLOCK_MAX;
1553 data.block[0] = length;
1554 memcpy(data.block + 1, values, length);
1555 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1556 I2C_SMBUS_WRITE, command,
1557 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1558}
David Brownellc0564602007-05-01 23:26:31 +02001559EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001560
David Brownell438d6c22006-12-10 21:21:31 +01001561/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001563static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001565 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 union i2c_smbus_data * data)
1567{
1568 /* So we need to generate a series of msgs. In the case of writing, we
1569 need to use only one message; when reading, we need two. We initialize
1570 most things with sane defaults, to keep the code below somewhat
1571 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001572 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1573 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001575 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1577 };
1578 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001579 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001580 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 msgbuf0[0] = command;
1583 switch(size) {
1584 case I2C_SMBUS_QUICK:
1585 msg[0].len = 0;
1586 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001587 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1588 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 num = 1;
1590 break;
1591 case I2C_SMBUS_BYTE:
1592 if (read_write == I2C_SMBUS_READ) {
1593 /* Special case: only a read! */
1594 msg[0].flags = I2C_M_RD | flags;
1595 num = 1;
1596 }
1597 break;
1598 case I2C_SMBUS_BYTE_DATA:
1599 if (read_write == I2C_SMBUS_READ)
1600 msg[1].len = 1;
1601 else {
1602 msg[0].len = 2;
1603 msgbuf0[1] = data->byte;
1604 }
1605 break;
1606 case I2C_SMBUS_WORD_DATA:
1607 if (read_write == I2C_SMBUS_READ)
1608 msg[1].len = 2;
1609 else {
1610 msg[0].len=3;
1611 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001612 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 break;
1615 case I2C_SMBUS_PROC_CALL:
1616 num = 2; /* Special case */
1617 read_write = I2C_SMBUS_READ;
1618 msg[0].len = 3;
1619 msg[1].len = 2;
1620 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001621 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 break;
1623 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001625 msg[1].flags |= I2C_M_RECV_LEN;
1626 msg[1].len = 1; /* block length will be added by
1627 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 } else {
1629 msg[0].len = data->block[0] + 2;
1630 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001631 dev_err(&adapter->dev,
1632 "Invalid block write size %d\n",
1633 data->block[0]);
1634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001636 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 msgbuf0[i] = data->block[i-1];
1638 }
1639 break;
1640 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001641 num = 2; /* Another special case */
1642 read_write = I2C_SMBUS_READ;
1643 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001644 dev_err(&adapter->dev,
1645 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001646 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001647 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001648 }
1649 msg[0].len = data->block[0] + 2;
1650 for (i = 1; i < msg[0].len; i++)
1651 msgbuf0[i] = data->block[i-1];
1652 msg[1].flags |= I2C_M_RECV_LEN;
1653 msg[1].len = 1; /* block length will be added by
1654 the underlying bus driver */
1655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 case I2C_SMBUS_I2C_BLOCK_DATA:
1657 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001658 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 } else {
1660 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001661 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001662 dev_err(&adapter->dev,
1663 "Invalid block write size %d\n",
1664 data->block[0]);
1665 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 for (i = 1; i <= data->block[0]; i++)
1668 msgbuf0[i] = data->block[i];
1669 }
1670 break;
1671 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001672 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1673 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675
Jean Delvare421ef472005-10-26 21:28:55 +02001676 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1677 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1678 if (i) {
1679 /* Compute PEC if first message is a write */
1680 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001681 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001682 i2c_smbus_add_pec(&msg[0]);
1683 else /* Write followed by read */
1684 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1685 }
1686 /* Ask for PEC if last message is a read */
1687 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001688 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001689 }
1690
David Brownell24a5bb72008-07-14 22:38:23 +02001691 status = i2c_transfer(adapter, msg, num);
1692 if (status < 0)
1693 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Jean Delvare421ef472005-10-26 21:28:55 +02001695 /* Check PEC if last message is a read */
1696 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001697 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1698 if (status < 0)
1699 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001700 }
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (read_write == I2C_SMBUS_READ)
1703 switch(size) {
1704 case I2C_SMBUS_BYTE:
1705 data->byte = msgbuf0[0];
1706 break;
1707 case I2C_SMBUS_BYTE_DATA:
1708 data->byte = msgbuf1[0];
1709 break;
David Brownell438d6c22006-12-10 21:21:31 +01001710 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 case I2C_SMBUS_PROC_CALL:
1712 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1713 break;
1714 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001715 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 data->block[i+1] = msgbuf1[i];
1717 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001718 case I2C_SMBUS_BLOCK_DATA:
1719 case I2C_SMBUS_BLOCK_PROC_CALL:
1720 for (i = 0; i < msgbuf1[0] + 1; i++)
1721 data->block[i] = msgbuf1[i];
1722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 return 0;
1725}
1726
David Brownella1cdeda2008-07-14 22:38:24 +02001727/**
1728 * i2c_smbus_xfer - execute SMBus protocol operations
1729 * @adapter: Handle to I2C bus
1730 * @addr: Address of SMBus slave on that bus
1731 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1732 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1733 * @command: Byte interpreted by slave, for protocols which use such bytes
1734 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1735 * @data: Data to be read or written
1736 *
1737 * This executes an SMBus protocol operation, and returns a negative
1738 * errno code else zero on success.
1739 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001740s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001741 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001742 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001744 unsigned long orig_jiffies;
1745 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001751 mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001752
1753 /* Retry automatically on arbitration loss */
1754 orig_jiffies = jiffies;
1755 for (res = 0, try = 0; try <= adapter->retries; try++) {
1756 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1757 read_write, command,
1758 protocol, data);
1759 if (res != -EAGAIN)
1760 break;
1761 if (time_after(jiffies,
1762 orig_jiffies + adapter->timeout))
1763 break;
1764 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001765 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 } else
1767 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001768 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return res;
1771}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1775MODULE_DESCRIPTION("I2C-Bus main module");
1776MODULE_LICENSE("GPL");