blob: fd84b2a363381a6571a09d883cc3d7fe3cc676fa [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>
32#include <linux/seq_file.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010034#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010035#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010036#include <linux/hardirq.h>
37#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
Jean Delvare87c6c222008-01-27 18:14:48 +010039#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Brownell9c1600e2007-05-01 23:26:31 +020041#include "i2c-core.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jean Delvarecaada322008-01-27 18:14:49 +010044static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static DEFINE_IDR(i2c_adapter_idr);
46
David Brownella1d9e6e2007-05-01 23:26:30 +020047#define is_newstyle_driver(d) ((d)->probe || (d)->remove)
David Brownellf37dd802007-02-13 22:09:00 +010048
49/* ------------------------------------------------------------------------- */
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int i2c_device_match(struct device *dev, struct device_driver *drv)
52{
David Brownell7b4fbc52007-05-01 23:26:30 +020053 struct i2c_client *client = to_i2c_client(dev);
54 struct i2c_driver *driver = to_i2c_driver(drv);
55
56 /* make legacy i2c drivers bypass driver model probing entirely;
57 * such drivers scan each i2c adapter/bus themselves.
58 */
David Brownella1d9e6e2007-05-01 23:26:30 +020059 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020060 return 0;
61
62 /* new style drivers use the same kind of driver matching policy
63 * as platform devices or SPI: compare device and driver IDs.
64 */
65 return strcmp(client->driver_name, drv->name) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
David Brownell7b4fbc52007-05-01 23:26:30 +020068#ifdef CONFIG_HOTPLUG
69
70/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020071static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020072{
73 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020074
75 /* by definition, legacy drivers can't hotplug */
76 if (dev->driver || !client->driver_name)
77 return 0;
78
Kay Sievers7eff2e72007-08-14 15:15:12 +020079 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
David Brownell7b4fbc52007-05-01 23:26:30 +020080 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020081 dev_dbg(dev, "uevent\n");
82 return 0;
83}
84
85#else
86#define i2c_device_uevent NULL
87#endif /* CONFIG_HOTPLUG */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static int i2c_device_probe(struct device *dev)
90{
David Brownell7b4fbc52007-05-01 23:26:30 +020091 struct i2c_client *client = to_i2c_client(dev);
92 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +010093 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +020094
95 if (!driver->probe)
96 return -ENODEV;
97 client->driver = driver;
98 dev_dbg(dev, "probe\n");
Hans Verkuil50c33042008-03-12 14:15:00 +010099 status = driver->probe(client);
100 if (status)
101 client->driver = NULL;
102 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105static int i2c_device_remove(struct device *dev)
106{
David Brownella1d9e6e2007-05-01 23:26:30 +0200107 struct i2c_client *client = to_i2c_client(dev);
108 struct i2c_driver *driver;
109 int status;
110
111 if (!dev->driver)
112 return 0;
113
114 driver = to_i2c_driver(dev->driver);
115 if (driver->remove) {
116 dev_dbg(dev, "remove\n");
117 status = driver->remove(client);
118 } else {
119 dev->driver = NULL;
120 status = 0;
121 }
122 if (status == 0)
123 client->driver = NULL;
124 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
David Brownellf37dd802007-02-13 22:09:00 +0100127static void i2c_device_shutdown(struct device *dev)
128{
129 struct i2c_driver *driver;
130
131 if (!dev->driver)
132 return;
133 driver = to_i2c_driver(dev->driver);
134 if (driver->shutdown)
135 driver->shutdown(to_i2c_client(dev));
136}
137
138static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
139{
140 struct i2c_driver *driver;
141
142 if (!dev->driver)
143 return 0;
144 driver = to_i2c_driver(dev->driver);
145 if (!driver->suspend)
146 return 0;
147 return driver->suspend(to_i2c_client(dev), mesg);
148}
149
150static int i2c_device_resume(struct device * dev)
151{
152 struct i2c_driver *driver;
153
154 if (!dev->driver)
155 return 0;
156 driver = to_i2c_driver(dev->driver);
157 if (!driver->resume)
158 return 0;
159 return driver->resume(to_i2c_client(dev));
160}
161
David Brownell7b4fbc52007-05-01 23:26:30 +0200162static void i2c_client_release(struct device *dev)
163{
164 struct i2c_client *client = to_i2c_client(dev);
165 complete(&client->released);
166}
167
David Brownell9c1600e2007-05-01 23:26:31 +0200168static void i2c_client_dev_release(struct device *dev)
169{
170 kfree(to_i2c_client(dev));
171}
172
David Brownell7b4fbc52007-05-01 23:26:30 +0200173static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
174{
175 struct i2c_client *client = to_i2c_client(dev);
176 return sprintf(buf, "%s\n", client->name);
177}
178
179static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
180{
181 struct i2c_client *client = to_i2c_client(dev);
182 return client->driver_name
183 ? sprintf(buf, "%s\n", client->driver_name)
184 : 0;
185}
186
187static struct device_attribute i2c_dev_attrs[] = {
188 __ATTR(name, S_IRUGO, show_client_name, NULL),
189 /* modalias helps coldplug: modprobe $(cat .../modalias) */
190 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
191 { },
192};
193
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200194static struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100195 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200196 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100197 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200198 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100199 .probe = i2c_device_probe,
200 .remove = i2c_device_remove,
201 .shutdown = i2c_device_shutdown,
202 .suspend = i2c_device_suspend,
203 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000204};
205
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/**
226 * i2c_new_device - instantiate an i2c device for use with a new style driver
227 * @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 *
231 * Create a device to work with a new style i2c driver, where binding is
232 * handled through driver model probe()/remove() methods. This call is not
233 * appropriate for use by mainboad initialization logic, which usually runs
234 * during an arch_initcall() long before any i2c_adapter could exist.
235 *
236 * This returns the new i2c client, which may be saved for later use with
237 * i2c_unregister_device(); or NULL to indicate an error.
238 */
239struct i2c_client *
240i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
241{
242 struct i2c_client *client;
243 int status;
244
245 client = kzalloc(sizeof *client, GFP_KERNEL);
246 if (!client)
247 return NULL;
248
249 client->adapter = adap;
250
251 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200252 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
253
254 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200255 client->addr = info->addr;
256 client->irq = info->irq;
257
258 strlcpy(client->driver_name, info->driver_name,
259 sizeof(client->driver_name));
260 strlcpy(client->name, info->type, sizeof(client->name));
261
262 /* a new style driver may be bound to this device when we
263 * return from this function, or any later moment (e.g. maybe
264 * hotplugging will load the driver module). and the device
265 * refcount model is the standard driver model one.
266 */
267 status = i2c_attach_client(client);
268 if (status < 0) {
269 kfree(client);
270 client = NULL;
271 }
272 return client;
273}
274EXPORT_SYMBOL_GPL(i2c_new_device);
275
276
277/**
278 * i2c_unregister_device - reverse effect of i2c_new_device()
279 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200280 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200281 */
282void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200283{
284 struct i2c_adapter *adapter = client->adapter;
285 struct i2c_driver *driver = client->driver;
286
287 if (driver && !is_newstyle_driver(driver)) {
288 dev_err(&client->dev, "can't unregister devices "
289 "with legacy drivers\n");
290 WARN_ON(1);
291 return;
292 }
293
294 mutex_lock(&adapter->clist_lock);
295 list_del(&client->list);
296 mutex_unlock(&adapter->clist_lock);
297
298 device_unregister(&client->dev);
299}
David Brownell9c1600e2007-05-01 23:26:31 +0200300EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200301
302
David Brownelle9f13732008-01-27 18:14:52 +0100303static int dummy_nop(struct i2c_client *client)
304{
305 return 0;
306}
307
308static struct i2c_driver dummy_driver = {
309 .driver.name = "dummy",
310 .probe = dummy_nop,
311 .remove = dummy_nop,
312};
313
314/**
315 * i2c_new_dummy - return a new i2c device bound to a dummy driver
316 * @adapter: the adapter managing the device
317 * @address: seven bit address to be used
318 * @type: optional label used for i2c_client.name
319 * Context: can sleep
320 *
321 * This returns an I2C client bound to the "dummy" driver, intended for use
322 * with devices that consume multiple addresses. Examples of such chips
323 * include various EEPROMS (like 24c04 and 24c08 models).
324 *
325 * These dummy devices have two main uses. First, most I2C and SMBus calls
326 * except i2c_transfer() need a client handle; the dummy will be that handle.
327 * And second, this prevents the specified address from being bound to a
328 * different driver.
329 *
330 * This returns the new i2c client, which should be saved for later use with
331 * i2c_unregister_device(); or NULL to indicate an error.
332 */
333struct i2c_client *
334i2c_new_dummy(struct i2c_adapter *adapter, u16 address, const char *type)
335{
336 struct i2c_board_info info = {
337 .driver_name = "dummy",
338 .addr = address,
339 };
340
341 if (type)
342 strlcpy(info.type, type, sizeof info.type);
343 return i2c_new_device(adapter, &info);
344}
345EXPORT_SYMBOL_GPL(i2c_new_dummy);
346
David Brownellf37dd802007-02-13 22:09:00 +0100347/* ------------------------------------------------------------------------- */
348
David Brownell16ffadf2007-05-01 23:26:28 +0200349/* I2C bus adapters -- one roots each I2C or SMBUS segment */
350
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200351static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
David Brownellef2c83212007-05-01 23:26:28 +0200353 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 complete(&adap->dev_released);
355}
356
David Brownell16ffadf2007-05-01 23:26:28 +0200357static ssize_t
358show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
David Brownellef2c83212007-05-01 23:26:28 +0200360 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return sprintf(buf, "%s\n", adap->name);
362}
David Brownell16ffadf2007-05-01 23:26:28 +0200363
364static struct device_attribute i2c_adapter_attrs[] = {
365 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
366 { },
367};
368
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200369static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200370 .owner = THIS_MODULE,
371 .name = "i2c-adapter",
372 .dev_attrs = i2c_adapter_attrs,
373};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
David Brownell9c1600e2007-05-01 23:26:31 +0200375static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
376{
377 struct i2c_devinfo *devinfo;
378
379 mutex_lock(&__i2c_board_lock);
380 list_for_each_entry(devinfo, &__i2c_board_list, list) {
381 if (devinfo->busnum == adapter->nr
382 && !i2c_new_device(adapter,
383 &devinfo->board_info))
384 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
385 i2c_adapter_id(adapter),
386 devinfo->board_info.addr);
387 }
388 mutex_unlock(&__i2c_board_lock);
389}
390
Jean Delvare026526f2008-01-27 18:14:49 +0100391static int i2c_do_add_adapter(struct device_driver *d, void *data)
392{
393 struct i2c_driver *driver = to_i2c_driver(d);
394 struct i2c_adapter *adap = data;
395
396 if (driver->attach_adapter) {
397 /* We ignore the return code; if it fails, too bad */
398 driver->attach_adapter(adap);
399 }
400 return 0;
401}
402
David Brownell6e13e642007-05-01 23:26:31 +0200403static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Jean Delvare026526f2008-01-27 18:14:49 +0100405 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Ingo Molnar5c085d32006-01-18 23:16:04 +0100407 mutex_init(&adap->bus_lock);
408 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 INIT_LIST_HEAD(&adap->clients);
410
Jean Delvarecaada322008-01-27 18:14:49 +0100411 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* Add the adapter to the driver core.
414 * If the parent pointer is not set up,
415 * we add this adapter to the host bus.
416 */
David Brownellb119dc32007-01-04 13:07:04 +0100417 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100419 pr_debug("I2C adapter driver [%s] forgot to specify "
420 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200424 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200425 res = device_register(&adap->dev);
426 if (res)
427 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200429 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
430
David Brownell6e13e642007-05-01 23:26:31 +0200431 /* create pre-declared device nodes for new-style drivers */
432 if (adap->nr < __i2c_first_dynamic_bus_num)
433 i2c_scan_static_board_info(adap);
434
David Brownell7b4fbc52007-05-01 23:26:30 +0200435 /* let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100436 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
437 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100440 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200442
Jean Delvareb119c6c2006-08-15 18:26:30 +0200443out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200444 idr_remove(&i2c_adapter_idr, adap->nr);
445 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
David Brownell6e13e642007-05-01 23:26:31 +0200448/**
449 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
450 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200451 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200452 *
453 * This routine is used to declare an I2C adapter when its bus number
454 * doesn't matter. Examples: for I2C adapters dynamically added by
455 * USB links or PCI plugin cards.
456 *
457 * When this returns zero, a new bus number was allocated and stored
458 * in adap->nr, and the specified adapter became available for clients.
459 * Otherwise, a negative errno value is returned.
460 */
461int i2c_add_adapter(struct i2c_adapter *adapter)
462{
463 int id, res = 0;
464
465retry:
466 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
467 return -ENOMEM;
468
Jean Delvarecaada322008-01-27 18:14:49 +0100469 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200470 /* "above" here means "above or equal to", sigh */
471 res = idr_get_new_above(&i2c_adapter_idr, adapter,
472 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100473 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200474
475 if (res < 0) {
476 if (res == -EAGAIN)
477 goto retry;
478 return res;
479 }
480
481 adapter->nr = id;
482 return i2c_register_adapter(adapter);
483}
484EXPORT_SYMBOL(i2c_add_adapter);
485
486/**
487 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
488 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200489 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200490 *
491 * This routine is used to declare an I2C adapter when its bus number
492 * matters. Example: for I2C adapters from system-on-chip CPUs, or
493 * otherwise built in to the system's mainboard, and where i2c_board_info
494 * is used to properly configure I2C devices.
495 *
496 * If no devices have pre-been declared for this bus, then be sure to
497 * register the adapter before any dynamically allocated ones. Otherwise
498 * the required bus ID may not be available.
499 *
500 * When this returns zero, the specified adapter became available for
501 * clients using the bus number provided in adap->nr. Also, the table
502 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
503 * and the appropriate driver model device nodes are created. Otherwise, a
504 * negative errno value is returned.
505 */
506int i2c_add_numbered_adapter(struct i2c_adapter *adap)
507{
508 int id;
509 int status;
510
511 if (adap->nr & ~MAX_ID_MASK)
512 return -EINVAL;
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 * we need the "equal to" result to force the result
521 */
522 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
523 if (status == 0 && id != adap->nr) {
524 status = -EBUSY;
525 idr_remove(&i2c_adapter_idr, id);
526 }
Jean Delvarecaada322008-01-27 18:14:49 +0100527 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200528 if (status == -EAGAIN)
529 goto retry;
530
531 if (status == 0)
532 status = i2c_register_adapter(adap);
533 return status;
534}
535EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
536
Jean Delvare026526f2008-01-27 18:14:49 +0100537static int i2c_do_del_adapter(struct device_driver *d, void *data)
538{
539 struct i2c_driver *driver = to_i2c_driver(d);
540 struct i2c_adapter *adapter = data;
541 int res;
542
543 if (!driver->detach_adapter)
544 return 0;
545 res = driver->detach_adapter(adapter);
546 if (res)
547 dev_err(&adapter->dev, "detach_adapter failed (%d) "
548 "for driver [%s]\n", res, driver->driver.name);
549 return res;
550}
551
David Brownelld64f73b2007-07-12 14:12:28 +0200552/**
553 * i2c_del_adapter - unregister I2C adapter
554 * @adap: the adapter being unregistered
555 * Context: can sleep
556 *
557 * This unregisters an I2C adapter which was previously registered
558 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560int i2c_del_adapter(struct i2c_adapter *adap)
561{
562 struct list_head *item, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct i2c_client *client;
564 int res = 0;
565
Jean Delvarecaada322008-01-27 18:14:49 +0100566 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100569 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200570 pr_debug("i2c-core: attempting to delete unregistered "
571 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 res = -EINVAL;
573 goto out_unlock;
574 }
575
Jean Delvare026526f2008-01-27 18:14:49 +0100576 /* Tell drivers about this removal */
577 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
578 i2c_do_del_adapter);
579 if (res)
580 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200583 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200585 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
David Brownella1d9e6e2007-05-01 23:26:30 +0200587 client = list_entry(item, struct i2c_client, list);
588 driver = client->driver;
589
590 /* new style, follow standard driver model */
591 if (!driver || is_newstyle_driver(driver)) {
592 i2c_unregister_device(client);
593 continue;
594 }
595
596 /* legacy drivers create and remove clients themselves */
597 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200598 dev_err(&adap->dev, "detach_client failed for client "
599 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 client->addr);
601 goto out_unlock;
602 }
603 }
604
605 /* clean up the sysfs representation */
606 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* wait for sysfs to drop all references */
610 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
David Brownell6e13e642007-05-01 23:26:31 +0200612 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 idr_remove(&i2c_adapter_idr, adap->nr);
614
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200615 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100618 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return res;
620}
David Brownellc0564602007-05-01 23:26:31 +0200621EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623
David Brownell7b4fbc52007-05-01 23:26:30 +0200624/* ------------------------------------------------------------------------- */
625
626/*
627 * An i2c_driver is used with one or more i2c_client (device) nodes to access
628 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
629 * are two models for binding the driver to its device: "new style" drivers
630 * follow the standard Linux driver model and just respond to probe() calls
631 * issued if the driver core sees they match(); "legacy" drivers create device
632 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
634
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800635int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100637 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David Brownell7b4fbc52007-05-01 23:26:30 +0200639 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200640 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200641 if (driver->attach_adapter || driver->detach_adapter
642 || driver->detach_client) {
643 printk(KERN_WARNING
644 "i2c-core: driver [%s] is confused\n",
645 driver->driver.name);
646 return -EINVAL;
647 }
648 }
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800651 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
David Brownell6e13e642007-05-01 23:26:31 +0200654 /* for new style drivers, when registration returns the driver core
655 * will have called probe() for all matching-but-unbound devices.
656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 res = driver_register(&driver->driver);
658 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100659 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100660
Jean Delvarecaada322008-01-27 18:14:49 +0100661 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100662
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100663 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
David Brownell7b4fbc52007-05-01 23:26:30 +0200665 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100666 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200667 struct i2c_adapter *adapter;
668
Jean Delvare87c6c222008-01-27 18:14:48 +0100669 down(&i2c_adapter_class.sem);
670 list_for_each_entry(adapter, &i2c_adapter_class.devices,
671 dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 driver->attach_adapter(adapter);
673 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100674 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Jean Delvarecaada322008-01-27 18:14:49 +0100677 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800680EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
David Brownella1d9e6e2007-05-01 23:26:30 +0200682/**
683 * i2c_del_driver - unregister I2C driver
684 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200685 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200686 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200687void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Jean Delvare87c6c222008-01-27 18:14:48 +0100689 struct list_head *item2, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct i2c_client *client;
691 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100692
Jean Delvarecaada322008-01-27 18:14:49 +0100693 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David Brownella1d9e6e2007-05-01 23:26:30 +0200695 /* new-style driver? */
696 if (is_newstyle_driver(driver))
697 goto unregister;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100700 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Jean Delvare87c6c222008-01-27 18:14:48 +0100703 down(&i2c_adapter_class.sem);
704 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (driver->detach_adapter) {
Jean Delvareb3e82092007-05-01 23:26:32 +0200706 if (driver->detach_adapter(adap)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200707 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100708 "for driver [%s]\n",
709 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 } else {
712 list_for_each_safe(item2, _n, &adap->clients) {
713 client = list_entry(item2, struct i2c_client, list);
714 if (client->driver != driver)
715 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200716 dev_dbg(&adap->dev, "detaching client [%s] "
717 "at 0x%02x\n", client->name,
718 client->addr);
Jean Delvareb3e82092007-05-01 23:26:32 +0200719 if (driver->detach_client(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200720 dev_err(&adap->dev, "detach_client "
721 "failed for client [%s] at "
722 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 }
726 }
727 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100728 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
David Brownella1d9e6e2007-05-01 23:26:30 +0200730 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100732 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Jean Delvarecaada322008-01-27 18:14:49 +0100734 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
David Brownellc0564602007-05-01 23:26:31 +0200736EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
David Brownell7b4fbc52007-05-01 23:26:30 +0200738/* ------------------------------------------------------------------------- */
739
David Brownell9b766b82008-01-27 18:14:51 +0100740static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
David Brownell9b766b82008-01-27 18:14:51 +0100742 struct i2c_client *client = i2c_verify_client(dev);
743 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
David Brownell9b766b82008-01-27 18:14:51 +0100745 if (client && client->addr == addr)
746 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 0;
748}
749
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100750static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
David Brownell9b766b82008-01-27 18:14:51 +0100752 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755int i2c_attach_client(struct i2c_client *client)
756{
757 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200758 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200762
763 if (client->driver)
764 client->dev.driver = &client->driver->driver;
765
David Brownellde81d2a2007-05-22 19:49:16 +0200766 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200767 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200768 client->dev.uevent_suppress = 1;
769 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200770 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
773 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200774 res = device_register(&client->dev);
775 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100776 goto out_err;
777
778 mutex_lock(&adapter->clist_lock);
779 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200780 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200781
David Brownell86ec5ec2008-01-27 18:14:51 +0100782 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
783 client->name, client->dev.bus_id);
784
Jean Delvare77ed74d2006-09-30 17:18:59 +0200785 if (adapter->client_register) {
786 if (adapter->client_register(client)) {
787 dev_dbg(&adapter->dev, "client_register "
788 "failed for client [%s] at 0x%02x\n",
789 client->name, client->addr);
790 }
791 }
792
793 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200794
David Brownell86ec5ec2008-01-27 18:14:51 +0100795out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200796 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
797 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200798 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
David Brownellc0564602007-05-01 23:26:31 +0200800EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802int i2c_detach_client(struct i2c_client *client)
803{
804 struct i2c_adapter *adapter = client->adapter;
805 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (adapter->client_unregister) {
808 res = adapter->client_unregister(client);
809 if (res) {
810 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700811 "client_unregister [%s] failed, "
812 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 goto out;
814 }
815 }
816
Ingo Molnar5c085d32006-01-18 23:16:04 +0100817 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 list_del(&client->list);
Jean Delvare9ddced12008-01-27 18:14:51 +0100819 mutex_unlock(&adapter->clist_lock);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 device_unregister(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 wait_for_completion(&client->released);
824
825 out:
826 return res;
827}
David Brownellc0564602007-05-01 23:26:31 +0200828EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Jean Delvaree48d3312008-01-27 18:14:48 +0100830/**
831 * i2c_use_client - increments the reference count of the i2c client structure
832 * @client: the client being referenced
833 *
834 * Each live reference to a client should be refcounted. The driver model does
835 * that automatically as part of driver binding, so that most drivers don't
836 * need to do this explicitly: they hold a reference until they're unbound
837 * from the device.
838 *
839 * A pointer to the client with the incremented reference counter is returned.
840 */
841struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100843 get_device(&client->dev);
Jean Delvaree48d3312008-01-27 18:14:48 +0100844 return client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
David Brownellc0564602007-05-01 23:26:31 +0200846EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Jean Delvaree48d3312008-01-27 18:14:48 +0100848/**
849 * i2c_release_client - release a use of the i2c client structure
850 * @client: the client being no longer referenced
851 *
852 * Must be called when a user of a client is finished with it.
853 */
854void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100856 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
David Brownellc0564602007-05-01 23:26:31 +0200858EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
David Brownell9b766b82008-01-27 18:14:51 +0100860struct i2c_cmd_arg {
861 unsigned cmd;
862 void *arg;
863};
864
865static int i2c_cmd(struct device *dev, void *_arg)
866{
867 struct i2c_client *client = i2c_verify_client(dev);
868 struct i2c_cmd_arg *arg = _arg;
869
870 if (client && client->driver && client->driver->command)
871 client->driver->command(client, arg->cmd, arg->arg);
872 return 0;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
876{
David Brownell9b766b82008-01-27 18:14:51 +0100877 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
David Brownell9b766b82008-01-27 18:14:51 +0100879 cmd_arg.cmd = cmd;
880 cmd_arg.arg = arg;
881 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
David Brownellc0564602007-05-01 23:26:31 +0200883EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885static int __init i2c_init(void)
886{
887 int retval;
888
889 retval = bus_register(&i2c_bus_type);
890 if (retval)
891 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100892 retval = class_register(&i2c_adapter_class);
893 if (retval)
894 goto bus_err;
895 retval = i2c_add_driver(&dummy_driver);
896 if (retval)
897 goto class_err;
898 return 0;
899
900class_err:
901 class_unregister(&i2c_adapter_class);
902bus_err:
903 bus_unregister(&i2c_bus_type);
904 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907static void __exit i2c_exit(void)
908{
David Brownelle9f13732008-01-27 18:14:52 +0100909 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 bus_unregister(&i2c_bus_type);
912}
913
914subsys_initcall(i2c_init);
915module_exit(i2c_exit);
916
917/* ----------------------------------------------------
918 * the functional interface to the i2c busses.
919 * ----------------------------------------------------
920 */
921
922int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
923{
924 int ret;
925
926 if (adap->algo->master_xfer) {
927#ifdef DEBUG
928 for (ret = 0; ret < num; ret++) {
929 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200930 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
931 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
932 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934#endif
935
Mike Rapoportcea443a2008-01-27 18:14:50 +0100936 if (in_atomic() || irqs_disabled()) {
937 ret = mutex_trylock(&adap->bus_lock);
938 if (!ret)
939 /* I2C activity is ongoing. */
940 return -EAGAIN;
941 } else {
942 mutex_lock_nested(&adap->bus_lock, adap->level);
943 }
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100946 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 return ret;
949 } else {
950 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
951 return -ENOSYS;
952 }
953}
David Brownellc0564602007-05-01 23:26:31 +0200954EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
957{
958 int ret;
959 struct i2c_adapter *adap=client->adapter;
960 struct i2c_msg msg;
961
Jean Delvare815f55f2005-05-07 22:58:46 +0200962 msg.addr = client->addr;
963 msg.flags = client->flags & I2C_M_TEN;
964 msg.len = count;
965 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100966
Jean Delvare815f55f2005-05-07 22:58:46 +0200967 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Jean Delvare815f55f2005-05-07 22:58:46 +0200969 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
970 transmitted, else error code. */
971 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
David Brownellc0564602007-05-01 23:26:31 +0200973EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
976{
977 struct i2c_adapter *adap=client->adapter;
978 struct i2c_msg msg;
979 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Jean Delvare815f55f2005-05-07 22:58:46 +0200981 msg.addr = client->addr;
982 msg.flags = client->flags & I2C_M_TEN;
983 msg.flags |= I2C_M_RD;
984 msg.len = count;
985 msg.buf = buf;
986
987 ret = i2c_transfer(adap, &msg, 1);
988
989 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
990 transmitted, else error code. */
991 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
David Brownellc0564602007-05-01 23:26:31 +0200993EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995/* ----------------------------------------------------
996 * the i2c address scanning function
997 * Will not work for 10-bit addresses!
998 * ----------------------------------------------------
999 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001000static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1001 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001002{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001003 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001004
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001005 /* Make sure the address is valid */
1006 if (addr < 0x03 || addr > 0x77) {
1007 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1008 addr);
1009 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001010 }
1011
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001012 /* Skip if already in use */
1013 if (i2c_check_addr(adapter, addr))
1014 return 0;
1015
1016 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +02001017 if (kind < 0) {
1018 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1019 I2C_SMBUS_QUICK, NULL) < 0)
1020 return 0;
1021
1022 /* prevent 24RF08 corruption */
1023 if ((addr & ~0x0f) == 0x50)
1024 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1025 I2C_SMBUS_QUICK, NULL);
1026 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001027
1028 /* Finally call the custom detection function */
1029 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001030 /* -ENODEV can be returned if there is a chip at the given address
1031 but it isn't supported by this chip driver. We catch it here as
1032 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +02001033 if (err == -ENODEV)
1034 err = 0;
1035
1036 if (err)
1037 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1038 addr, err);
1039 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001043 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int (*found_proc) (struct i2c_adapter *, int, int))
1045{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001046 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int adap_id = i2c_adapter_id(adapter);
1048
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001049 /* Force entries are done first, and are not affected by ignore
1050 entries */
1051 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001052 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001053 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001055 for (kind = 0; forces[kind]; kind++) {
1056 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1057 i += 2) {
1058 if (forces[kind][i] == adap_id
1059 || forces[kind][i] == ANY_I2C_BUS) {
1060 dev_dbg(&adapter->dev, "found force "
1061 "parameter for adapter %d, "
1062 "addr 0x%02x, kind %d\n",
1063 adap_id, forces[kind][i + 1],
1064 kind);
1065 err = i2c_probe_address(adapter,
1066 forces[kind][i + 1],
1067 kind, found_proc);
1068 if (err)
1069 return err;
1070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001074
Jean Delvare4366dc92005-09-25 16:50:06 +02001075 /* Stop here if we can't use SMBUS_QUICK */
1076 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1077 if (address_data->probe[0] == I2C_CLIENT_END
1078 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001079 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001080
1081 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1082 "can't probe for chips\n");
1083 return -1;
1084 }
1085
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001086 /* Probe entries are done second, and are not affected by ignore
1087 entries either */
1088 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1089 if (address_data->probe[i] == adap_id
1090 || address_data->probe[i] == ANY_I2C_BUS) {
1091 dev_dbg(&adapter->dev, "found probe parameter for "
1092 "adapter %d, addr 0x%02x\n", adap_id,
1093 address_data->probe[i + 1]);
1094 err = i2c_probe_address(adapter,
1095 address_data->probe[i + 1],
1096 -1, found_proc);
1097 if (err)
1098 return err;
1099 }
1100 }
1101
1102 /* Normal entries are done last, unless shadowed by an ignore entry */
1103 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1104 int j, ignore;
1105
1106 ignore = 0;
1107 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1108 j += 2) {
1109 if ((address_data->ignore[j] == adap_id ||
1110 address_data->ignore[j] == ANY_I2C_BUS)
1111 && address_data->ignore[j + 1]
1112 == address_data->normal_i2c[i]) {
1113 dev_dbg(&adapter->dev, "found ignore "
1114 "parameter for adapter %d, "
1115 "addr 0x%02x\n", adap_id,
1116 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001117 ignore = 1;
1118 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001119 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001120 }
1121 if (ignore)
1122 continue;
1123
1124 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1125 "addr 0x%02x\n", adap_id,
1126 address_data->normal_i2c[i]);
1127 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1128 -1, found_proc);
1129 if (err)
1130 return err;
1131 }
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return 0;
1134}
David Brownellc0564602007-05-01 23:26:31 +02001135EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jean Delvare12b5053a2007-05-01 23:26:31 +02001137struct i2c_client *
1138i2c_new_probed_device(struct i2c_adapter *adap,
1139 struct i2c_board_info *info,
1140 unsigned short const *addr_list)
1141{
1142 int i;
1143
1144 /* Stop here if the bus doesn't support probing */
1145 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1146 dev_err(&adap->dev, "Probing not supported\n");
1147 return NULL;
1148 }
1149
Jean Delvare12b5053a2007-05-01 23:26:31 +02001150 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1151 /* Check address validity */
1152 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1153 dev_warn(&adap->dev, "Invalid 7-bit address "
1154 "0x%02x\n", addr_list[i]);
1155 continue;
1156 }
1157
1158 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001159 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001160 dev_dbg(&adap->dev, "Address 0x%02x already in "
1161 "use, not probing\n", addr_list[i]);
1162 continue;
1163 }
1164
1165 /* Test address responsiveness
1166 The default probe method is a quick write, but it is known
1167 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1168 and could also irreversibly write-protect some EEPROMs, so
1169 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1170 read instead. Also, some bus drivers don't implement
1171 quick write, so we fallback to a byte read it that case
1172 too. */
1173 if ((addr_list[i] & ~0x07) == 0x30
1174 || (addr_list[i] & ~0x0f) == 0x50
1175 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1176 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1177 I2C_SMBUS_READ, 0,
1178 I2C_SMBUS_BYTE, NULL) >= 0)
1179 break;
1180 } else {
1181 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1182 I2C_SMBUS_WRITE, 0,
1183 I2C_SMBUS_QUICK, NULL) >= 0)
1184 break;
1185 }
1186 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001187
1188 if (addr_list[i] == I2C_CLIENT_END) {
1189 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1190 return NULL;
1191 }
1192
1193 info->addr = addr_list[i];
1194 return i2c_new_device(adap, info);
1195}
1196EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198struct i2c_adapter* i2c_get_adapter(int id)
1199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001201
Jean Delvarecaada322008-01-27 18:14:49 +01001202 mutex_lock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001203 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1204 if (adapter && !try_module_get(adapter->owner))
1205 adapter = NULL;
1206
Jean Delvarecaada322008-01-27 18:14:49 +01001207 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001208 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
David Brownellc0564602007-05-01 23:26:31 +02001210EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212void i2c_put_adapter(struct i2c_adapter *adap)
1213{
1214 module_put(adap->owner);
1215}
David Brownellc0564602007-05-01 23:26:31 +02001216EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218/* The SMBus parts */
1219
David Brownell438d6c22006-12-10 21:21:31 +01001220#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static u8
1222crc8(u16 data)
1223{
1224 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001227 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 data = data ^ POLY;
1229 data = data << 1;
1230 }
1231 return (u8)(data >> 8);
1232}
1233
Jean Delvare421ef472005-10-26 21:28:55 +02001234/* Incremental CRC8 over count bytes in the array pointed to by p */
1235static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 int i;
1238
1239 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001240 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return crc;
1242}
1243
Jean Delvare421ef472005-10-26 21:28:55 +02001244/* Assume a 7-bit address, which is reasonable for SMBus */
1245static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Jean Delvare421ef472005-10-26 21:28:55 +02001247 /* The address will be sent first */
1248 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1249 pec = i2c_smbus_pec(pec, &addr, 1);
1250
1251 /* The data buffer follows */
1252 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Jean Delvare421ef472005-10-26 21:28:55 +02001255/* Used for write only transactions */
1256static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Jean Delvare421ef472005-10-26 21:28:55 +02001258 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1259 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Jean Delvare421ef472005-10-26 21:28:55 +02001262/* Return <0 on CRC error
1263 If there was a write before this read (most cases) we need to take the
1264 partial CRC from the write part into account.
1265 Note that this function does modify the message (we need to decrease the
1266 message length to hide the CRC byte from the caller). */
1267static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Jean Delvare421ef472005-10-26 21:28:55 +02001269 u8 rpec = msg->buf[--msg->len];
1270 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (rpec != cpec) {
1273 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1274 rpec, cpec);
1275 return -1;
1276 }
David Brownell438d6c22006-12-10 21:21:31 +01001277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1281{
1282 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001283 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
David Brownellc0564602007-05-01 23:26:31 +02001285EXPORT_SYMBOL(i2c_smbus_write_quick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287s32 i2c_smbus_read_byte(struct i2c_client *client)
1288{
1289 union i2c_smbus_data data;
1290 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1291 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1292 return -1;
1293 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001294 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
David Brownellc0564602007-05-01 23:26:31 +02001296EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001301 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
David Brownellc0564602007-05-01 23:26:31 +02001303EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1306{
1307 union i2c_smbus_data data;
1308 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1309 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1310 return -1;
1311 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001312 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
David Brownellc0564602007-05-01 23:26:31 +02001314EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1317{
1318 union i2c_smbus_data data;
1319 data.byte = value;
1320 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1321 I2C_SMBUS_WRITE,command,
1322 I2C_SMBUS_BYTE_DATA,&data);
1323}
David Brownellc0564602007-05-01 23:26:31 +02001324EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1327{
1328 union i2c_smbus_data data;
1329 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1330 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1331 return -1;
1332 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001333 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
David Brownellc0564602007-05-01 23:26:31 +02001335EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1338{
1339 union i2c_smbus_data data;
1340 data.word = value;
1341 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1342 I2C_SMBUS_WRITE,command,
1343 I2C_SMBUS_WORD_DATA,&data);
1344}
David Brownellc0564602007-05-01 23:26:31 +02001345EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
David Brownella64ec072007-10-13 23:56:31 +02001347/**
1348 * i2c_smbus_read_block_data - SMBus block read request
1349 * @client: Handle to slave device
1350 * @command: Command byte issued to let the slave know what data should
1351 * be returned
1352 * @values: Byte array into which data will be read; big enough to hold
1353 * the data returned by the slave. SMBus allows at most 32 bytes.
1354 *
1355 * Returns the number of bytes read in the slave's response, else a
1356 * negative number to indicate some kind of error.
1357 *
1358 * Note that using this function requires that the client's adapter support
1359 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1360 * support this; its emulation through I2C messaging relies on a specific
1361 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1362 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001363s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1364 u8 *values)
1365{
1366 union i2c_smbus_data data;
1367
1368 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1369 I2C_SMBUS_READ, command,
1370 I2C_SMBUS_BLOCK_DATA, &data))
1371 return -1;
1372
1373 memcpy(values, &data.block[1], data.block[0]);
1374 return data.block[0];
1375}
1376EXPORT_SYMBOL(i2c_smbus_read_block_data);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001379 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (length > I2C_SMBUS_BLOCK_MAX)
1384 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001386 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1388 I2C_SMBUS_WRITE,command,
1389 I2C_SMBUS_BLOCK_DATA,&data);
1390}
David Brownellc0564602007-05-01 23:26:31 +02001391EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001394s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1395 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001398
Jean Delvare4b2643d2007-07-12 14:12:29 +02001399 if (length > I2C_SMBUS_BLOCK_MAX)
1400 length = I2C_SMBUS_BLOCK_MAX;
1401 data.block[0] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1403 I2C_SMBUS_READ,command,
1404 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1405 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001406
1407 memcpy(values, &data.block[1], data.block[0]);
1408 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
David Brownellc0564602007-05-01 23:26:31 +02001410EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Jean Delvare21bbd692006-01-09 15:19:18 +11001412s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001413 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001414{
1415 union i2c_smbus_data data;
1416
1417 if (length > I2C_SMBUS_BLOCK_MAX)
1418 length = I2C_SMBUS_BLOCK_MAX;
1419 data.block[0] = length;
1420 memcpy(data.block + 1, values, length);
1421 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1422 I2C_SMBUS_WRITE, command,
1423 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1424}
David Brownellc0564602007-05-01 23:26:31 +02001425EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001426
David Brownell438d6c22006-12-10 21:21:31 +01001427/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001429static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001431 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 union i2c_smbus_data * data)
1433{
1434 /* So we need to generate a series of msgs. In the case of writing, we
1435 need to use only one message; when reading, we need two. We initialize
1436 most things with sane defaults, to keep the code below somewhat
1437 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001438 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1439 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001441 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1443 };
1444 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001445 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 msgbuf0[0] = command;
1448 switch(size) {
1449 case I2C_SMBUS_QUICK:
1450 msg[0].len = 0;
1451 /* Special case: The read/write field is used as data */
1452 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1453 num = 1;
1454 break;
1455 case I2C_SMBUS_BYTE:
1456 if (read_write == I2C_SMBUS_READ) {
1457 /* Special case: only a read! */
1458 msg[0].flags = I2C_M_RD | flags;
1459 num = 1;
1460 }
1461 break;
1462 case I2C_SMBUS_BYTE_DATA:
1463 if (read_write == I2C_SMBUS_READ)
1464 msg[1].len = 1;
1465 else {
1466 msg[0].len = 2;
1467 msgbuf0[1] = data->byte;
1468 }
1469 break;
1470 case I2C_SMBUS_WORD_DATA:
1471 if (read_write == I2C_SMBUS_READ)
1472 msg[1].len = 2;
1473 else {
1474 msg[0].len=3;
1475 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001476 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 break;
1479 case I2C_SMBUS_PROC_CALL:
1480 num = 2; /* Special case */
1481 read_write = I2C_SMBUS_READ;
1482 msg[0].len = 3;
1483 msg[1].len = 2;
1484 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001485 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
1487 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001489 msg[1].flags |= I2C_M_RECV_LEN;
1490 msg[1].len = 1; /* block length will be added by
1491 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 } else {
1493 msg[0].len = data->block[0] + 2;
1494 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1495 dev_err(&adapter->dev, "smbus_access called with "
1496 "invalid block write size (%d)\n",
1497 data->block[0]);
1498 return -1;
1499 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001500 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 msgbuf0[i] = data->block[i-1];
1502 }
1503 break;
1504 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001505 num = 2; /* Another special case */
1506 read_write = I2C_SMBUS_READ;
1507 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1508 dev_err(&adapter->dev, "%s called with invalid "
1509 "block proc call size (%d)\n", __FUNCTION__,
1510 data->block[0]);
1511 return -1;
1512 }
1513 msg[0].len = data->block[0] + 2;
1514 for (i = 1; i < msg[0].len; i++)
1515 msgbuf0[i] = data->block[i-1];
1516 msg[1].flags |= I2C_M_RECV_LEN;
1517 msg[1].len = 1; /* block length will be added by
1518 the underlying bus driver */
1519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 case I2C_SMBUS_I2C_BLOCK_DATA:
1521 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001522 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 } else {
1524 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001525 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1527 "invalid block write size (%d)\n",
1528 data->block[0]);
1529 return -1;
1530 }
1531 for (i = 1; i <= data->block[0]; i++)
1532 msgbuf0[i] = data->block[i];
1533 }
1534 break;
1535 default:
1536 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1537 size);
1538 return -1;
1539 }
1540
Jean Delvare421ef472005-10-26 21:28:55 +02001541 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1542 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1543 if (i) {
1544 /* Compute PEC if first message is a write */
1545 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001546 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001547 i2c_smbus_add_pec(&msg[0]);
1548 else /* Write followed by read */
1549 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1550 }
1551 /* Ask for PEC if last message is a read */
1552 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001553 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001554 }
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (i2c_transfer(adapter, msg, num) < 0)
1557 return -1;
1558
Jean Delvare421ef472005-10-26 21:28:55 +02001559 /* Check PEC if last message is a read */
1560 if (i && (msg[num-1].flags & I2C_M_RD)) {
1561 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1562 return -1;
1563 }
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (read_write == I2C_SMBUS_READ)
1566 switch(size) {
1567 case I2C_SMBUS_BYTE:
1568 data->byte = msgbuf0[0];
1569 break;
1570 case I2C_SMBUS_BYTE_DATA:
1571 data->byte = msgbuf1[0];
1572 break;
David Brownell438d6c22006-12-10 21:21:31 +01001573 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 case I2C_SMBUS_PROC_CALL:
1575 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1576 break;
1577 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001578 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 data->block[i+1] = msgbuf1[i];
1580 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001581 case I2C_SMBUS_BLOCK_DATA:
1582 case I2C_SMBUS_BLOCK_PROC_CALL:
1583 for (i = 0; i < msgbuf1[0] + 1; i++)
1584 data->block[i] = msgbuf1[i];
1585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 return 0;
1588}
1589
1590
1591s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001592 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 union i2c_smbus_data * data)
1594{
1595 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001600 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1602 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001603 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 } else
1605 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1606 command,size,data);
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return res;
1609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1613MODULE_DESCRIPTION("I2C-Bus main module");
1614MODULE_LICENSE("GPL");