blob: ddd1b83f44d45ef4913eae28141daa8daeef60cb [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 Rapoportcea443a82008-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);
93
94 if (!driver->probe)
95 return -ENODEV;
96 client->driver = driver;
97 dev_dbg(dev, "probe\n");
98 return driver->probe(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static int i2c_device_remove(struct device *dev)
102{
David Brownella1d9e6e2007-05-01 23:26:30 +0200103 struct i2c_client *client = to_i2c_client(dev);
104 struct i2c_driver *driver;
105 int status;
106
107 if (!dev->driver)
108 return 0;
109
110 driver = to_i2c_driver(dev->driver);
111 if (driver->remove) {
112 dev_dbg(dev, "remove\n");
113 status = driver->remove(client);
114 } else {
115 dev->driver = NULL;
116 status = 0;
117 }
118 if (status == 0)
119 client->driver = NULL;
120 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
David Brownellf37dd802007-02-13 22:09:00 +0100123static void i2c_device_shutdown(struct device *dev)
124{
125 struct i2c_driver *driver;
126
127 if (!dev->driver)
128 return;
129 driver = to_i2c_driver(dev->driver);
130 if (driver->shutdown)
131 driver->shutdown(to_i2c_client(dev));
132}
133
134static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
135{
136 struct i2c_driver *driver;
137
138 if (!dev->driver)
139 return 0;
140 driver = to_i2c_driver(dev->driver);
141 if (!driver->suspend)
142 return 0;
143 return driver->suspend(to_i2c_client(dev), mesg);
144}
145
146static int i2c_device_resume(struct device * dev)
147{
148 struct i2c_driver *driver;
149
150 if (!dev->driver)
151 return 0;
152 driver = to_i2c_driver(dev->driver);
153 if (!driver->resume)
154 return 0;
155 return driver->resume(to_i2c_client(dev));
156}
157
David Brownell7b4fbc52007-05-01 23:26:30 +0200158static void i2c_client_release(struct device *dev)
159{
160 struct i2c_client *client = to_i2c_client(dev);
161 complete(&client->released);
162}
163
David Brownell9c1600e2007-05-01 23:26:31 +0200164static void i2c_client_dev_release(struct device *dev)
165{
166 kfree(to_i2c_client(dev));
167}
168
David Brownell7b4fbc52007-05-01 23:26:30 +0200169static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
170{
171 struct i2c_client *client = to_i2c_client(dev);
172 return sprintf(buf, "%s\n", client->name);
173}
174
175static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
176{
177 struct i2c_client *client = to_i2c_client(dev);
178 return client->driver_name
179 ? sprintf(buf, "%s\n", client->driver_name)
180 : 0;
181}
182
183static struct device_attribute i2c_dev_attrs[] = {
184 __ATTR(name, S_IRUGO, show_client_name, NULL),
185 /* modalias helps coldplug: modprobe $(cat .../modalias) */
186 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
187 { },
188};
189
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200190static struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100191 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200192 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100193 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200194 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100195 .probe = i2c_device_probe,
196 .remove = i2c_device_remove,
197 .shutdown = i2c_device_shutdown,
198 .suspend = i2c_device_suspend,
199 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000200};
201
David Brownell9c1600e2007-05-01 23:26:31 +0200202/**
203 * i2c_new_device - instantiate an i2c device for use with a new style driver
204 * @adap: the adapter managing the device
205 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200206 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200207 *
208 * Create a device to work with a new style i2c driver, where binding is
209 * handled through driver model probe()/remove() methods. This call is not
210 * appropriate for use by mainboad initialization logic, which usually runs
211 * during an arch_initcall() long before any i2c_adapter could exist.
212 *
213 * This returns the new i2c client, which may be saved for later use with
214 * i2c_unregister_device(); or NULL to indicate an error.
215 */
216struct i2c_client *
217i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
218{
219 struct i2c_client *client;
220 int status;
221
222 client = kzalloc(sizeof *client, GFP_KERNEL);
223 if (!client)
224 return NULL;
225
226 client->adapter = adap;
227
228 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200229 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
230
231 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200232 client->addr = info->addr;
233 client->irq = info->irq;
234
235 strlcpy(client->driver_name, info->driver_name,
236 sizeof(client->driver_name));
237 strlcpy(client->name, info->type, sizeof(client->name));
238
239 /* a new style driver may be bound to this device when we
240 * return from this function, or any later moment (e.g. maybe
241 * hotplugging will load the driver module). and the device
242 * refcount model is the standard driver model one.
243 */
244 status = i2c_attach_client(client);
245 if (status < 0) {
246 kfree(client);
247 client = NULL;
248 }
249 return client;
250}
251EXPORT_SYMBOL_GPL(i2c_new_device);
252
253
254/**
255 * i2c_unregister_device - reverse effect of i2c_new_device()
256 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200257 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200258 */
259void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200260{
261 struct i2c_adapter *adapter = client->adapter;
262 struct i2c_driver *driver = client->driver;
263
264 if (driver && !is_newstyle_driver(driver)) {
265 dev_err(&client->dev, "can't unregister devices "
266 "with legacy drivers\n");
267 WARN_ON(1);
268 return;
269 }
270
271 mutex_lock(&adapter->clist_lock);
272 list_del(&client->list);
273 mutex_unlock(&adapter->clist_lock);
274
275 device_unregister(&client->dev);
276}
David Brownell9c1600e2007-05-01 23:26:31 +0200277EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200278
279
David Brownellf37dd802007-02-13 22:09:00 +0100280/* ------------------------------------------------------------------------- */
281
David Brownell16ffadf2007-05-01 23:26:28 +0200282/* I2C bus adapters -- one roots each I2C or SMBUS segment */
283
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200284static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
David Brownellef2c83212007-05-01 23:26:28 +0200286 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 complete(&adap->dev_released);
288}
289
David Brownell16ffadf2007-05-01 23:26:28 +0200290static ssize_t
291show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
David Brownellef2c83212007-05-01 23:26:28 +0200293 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return sprintf(buf, "%s\n", adap->name);
295}
David Brownell16ffadf2007-05-01 23:26:28 +0200296
297static struct device_attribute i2c_adapter_attrs[] = {
298 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
299 { },
300};
301
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200302static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200303 .owner = THIS_MODULE,
304 .name = "i2c-adapter",
305 .dev_attrs = i2c_adapter_attrs,
306};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
David Brownell9c1600e2007-05-01 23:26:31 +0200308static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
309{
310 struct i2c_devinfo *devinfo;
311
312 mutex_lock(&__i2c_board_lock);
313 list_for_each_entry(devinfo, &__i2c_board_list, list) {
314 if (devinfo->busnum == adapter->nr
315 && !i2c_new_device(adapter,
316 &devinfo->board_info))
317 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
318 i2c_adapter_id(adapter),
319 devinfo->board_info.addr);
320 }
321 mutex_unlock(&__i2c_board_lock);
322}
323
Jean Delvare026526f2008-01-27 18:14:49 +0100324static int i2c_do_add_adapter(struct device_driver *d, void *data)
325{
326 struct i2c_driver *driver = to_i2c_driver(d);
327 struct i2c_adapter *adap = data;
328
329 if (driver->attach_adapter) {
330 /* We ignore the return code; if it fails, too bad */
331 driver->attach_adapter(adap);
332 }
333 return 0;
334}
335
David Brownell6e13e642007-05-01 23:26:31 +0200336static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Jean Delvare026526f2008-01-27 18:14:49 +0100338 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Ingo Molnar5c085d32006-01-18 23:16:04 +0100340 mutex_init(&adap->bus_lock);
341 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 INIT_LIST_HEAD(&adap->clients);
343
Jean Delvarecaada322008-01-27 18:14:49 +0100344 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Add the adapter to the driver core.
347 * If the parent pointer is not set up,
348 * we add this adapter to the host bus.
349 */
David Brownellb119dc32007-01-04 13:07:04 +0100350 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100352 pr_debug("I2C adapter driver [%s] forgot to specify "
353 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200357 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200358 res = device_register(&adap->dev);
359 if (res)
360 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200362 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
363
David Brownell6e13e642007-05-01 23:26:31 +0200364 /* create pre-declared device nodes for new-style drivers */
365 if (adap->nr < __i2c_first_dynamic_bus_num)
366 i2c_scan_static_board_info(adap);
367
David Brownell7b4fbc52007-05-01 23:26:30 +0200368 /* let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100369 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
370 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100373 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200375
Jean Delvareb119c6c2006-08-15 18:26:30 +0200376out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200377 idr_remove(&i2c_adapter_idr, adap->nr);
378 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
David Brownell6e13e642007-05-01 23:26:31 +0200381/**
382 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
383 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200384 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200385 *
386 * This routine is used to declare an I2C adapter when its bus number
387 * doesn't matter. Examples: for I2C adapters dynamically added by
388 * USB links or PCI plugin cards.
389 *
390 * When this returns zero, a new bus number was allocated and stored
391 * in adap->nr, and the specified adapter became available for clients.
392 * Otherwise, a negative errno value is returned.
393 */
394int i2c_add_adapter(struct i2c_adapter *adapter)
395{
396 int id, res = 0;
397
398retry:
399 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
400 return -ENOMEM;
401
Jean Delvarecaada322008-01-27 18:14:49 +0100402 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200403 /* "above" here means "above or equal to", sigh */
404 res = idr_get_new_above(&i2c_adapter_idr, adapter,
405 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100406 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200407
408 if (res < 0) {
409 if (res == -EAGAIN)
410 goto retry;
411 return res;
412 }
413
414 adapter->nr = id;
415 return i2c_register_adapter(adapter);
416}
417EXPORT_SYMBOL(i2c_add_adapter);
418
419/**
420 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
421 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200422 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200423 *
424 * This routine is used to declare an I2C adapter when its bus number
425 * matters. Example: for I2C adapters from system-on-chip CPUs, or
426 * otherwise built in to the system's mainboard, and where i2c_board_info
427 * is used to properly configure I2C devices.
428 *
429 * If no devices have pre-been declared for this bus, then be sure to
430 * register the adapter before any dynamically allocated ones. Otherwise
431 * the required bus ID may not be available.
432 *
433 * When this returns zero, the specified adapter became available for
434 * clients using the bus number provided in adap->nr. Also, the table
435 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
436 * and the appropriate driver model device nodes are created. Otherwise, a
437 * negative errno value is returned.
438 */
439int i2c_add_numbered_adapter(struct i2c_adapter *adap)
440{
441 int id;
442 int status;
443
444 if (adap->nr & ~MAX_ID_MASK)
445 return -EINVAL;
446
447retry:
448 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
449 return -ENOMEM;
450
Jean Delvarecaada322008-01-27 18:14:49 +0100451 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200452 /* "above" here means "above or equal to", sigh;
453 * we need the "equal to" result to force the result
454 */
455 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
456 if (status == 0 && id != adap->nr) {
457 status = -EBUSY;
458 idr_remove(&i2c_adapter_idr, id);
459 }
Jean Delvarecaada322008-01-27 18:14:49 +0100460 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200461 if (status == -EAGAIN)
462 goto retry;
463
464 if (status == 0)
465 status = i2c_register_adapter(adap);
466 return status;
467}
468EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
469
Jean Delvare026526f2008-01-27 18:14:49 +0100470static int i2c_do_del_adapter(struct device_driver *d, void *data)
471{
472 struct i2c_driver *driver = to_i2c_driver(d);
473 struct i2c_adapter *adapter = data;
474 int res;
475
476 if (!driver->detach_adapter)
477 return 0;
478 res = driver->detach_adapter(adapter);
479 if (res)
480 dev_err(&adapter->dev, "detach_adapter failed (%d) "
481 "for driver [%s]\n", res, driver->driver.name);
482 return res;
483}
484
David Brownelld64f73b2007-07-12 14:12:28 +0200485/**
486 * i2c_del_adapter - unregister I2C adapter
487 * @adap: the adapter being unregistered
488 * Context: can sleep
489 *
490 * This unregisters an I2C adapter which was previously registered
491 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493int i2c_del_adapter(struct i2c_adapter *adap)
494{
495 struct list_head *item, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct i2c_client *client;
497 int res = 0;
498
Jean Delvarecaada322008-01-27 18:14:49 +0100499 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100502 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200503 pr_debug("i2c-core: attempting to delete unregistered "
504 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 res = -EINVAL;
506 goto out_unlock;
507 }
508
Jean Delvare026526f2008-01-27 18:14:49 +0100509 /* Tell drivers about this removal */
510 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
511 i2c_do_del_adapter);
512 if (res)
513 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200516 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200518 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
David Brownella1d9e6e2007-05-01 23:26:30 +0200520 client = list_entry(item, struct i2c_client, list);
521 driver = client->driver;
522
523 /* new style, follow standard driver model */
524 if (!driver || is_newstyle_driver(driver)) {
525 i2c_unregister_device(client);
526 continue;
527 }
528
529 /* legacy drivers create and remove clients themselves */
530 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200531 dev_err(&adap->dev, "detach_client failed for client "
532 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 client->addr);
534 goto out_unlock;
535 }
536 }
537
538 /* clean up the sysfs representation */
539 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* wait for sysfs to drop all references */
543 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
David Brownell6e13e642007-05-01 23:26:31 +0200545 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 idr_remove(&i2c_adapter_idr, adap->nr);
547
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200548 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100551 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return res;
553}
David Brownellc0564602007-05-01 23:26:31 +0200554EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556
David Brownell7b4fbc52007-05-01 23:26:30 +0200557/* ------------------------------------------------------------------------- */
558
559/*
560 * An i2c_driver is used with one or more i2c_client (device) nodes to access
561 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
562 * are two models for binding the driver to its device: "new style" drivers
563 * follow the standard Linux driver model and just respond to probe() calls
564 * issued if the driver core sees they match(); "legacy" drivers create device
565 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
567
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800568int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100570 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
David Brownell7b4fbc52007-05-01 23:26:30 +0200572 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200573 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200574 if (driver->attach_adapter || driver->detach_adapter
575 || driver->detach_client) {
576 printk(KERN_WARNING
577 "i2c-core: driver [%s] is confused\n",
578 driver->driver.name);
579 return -EINVAL;
580 }
581 }
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800584 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
David Brownell6e13e642007-05-01 23:26:31 +0200587 /* for new style drivers, when registration returns the driver core
588 * will have called probe() for all matching-but-unbound devices.
589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 res = driver_register(&driver->driver);
591 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100592 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100593
Jean Delvarecaada322008-01-27 18:14:49 +0100594 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100595
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100596 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
David Brownell7b4fbc52007-05-01 23:26:30 +0200598 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100599 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200600 struct i2c_adapter *adapter;
601
Jean Delvare87c6c222008-01-27 18:14:48 +0100602 down(&i2c_adapter_class.sem);
603 list_for_each_entry(adapter, &i2c_adapter_class.devices,
604 dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 driver->attach_adapter(adapter);
606 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100607 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
Jean Delvarecaada322008-01-27 18:14:49 +0100610 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800613EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
David Brownella1d9e6e2007-05-01 23:26:30 +0200615/**
616 * i2c_del_driver - unregister I2C driver
617 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200618 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200619 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200620void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Jean Delvare87c6c222008-01-27 18:14:48 +0100622 struct list_head *item2, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct i2c_client *client;
624 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100625
Jean Delvarecaada322008-01-27 18:14:49 +0100626 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
David Brownella1d9e6e2007-05-01 23:26:30 +0200628 /* new-style driver? */
629 if (is_newstyle_driver(driver))
630 goto unregister;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100633 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Jean Delvare87c6c222008-01-27 18:14:48 +0100636 down(&i2c_adapter_class.sem);
637 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (driver->detach_adapter) {
Jean Delvareb3e82092007-05-01 23:26:32 +0200639 if (driver->detach_adapter(adap)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200640 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100641 "for driver [%s]\n",
642 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644 } else {
645 list_for_each_safe(item2, _n, &adap->clients) {
646 client = list_entry(item2, struct i2c_client, list);
647 if (client->driver != driver)
648 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200649 dev_dbg(&adap->dev, "detaching client [%s] "
650 "at 0x%02x\n", client->name,
651 client->addr);
Jean Delvareb3e82092007-05-01 23:26:32 +0200652 if (driver->detach_client(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200653 dev_err(&adap->dev, "detach_client "
654 "failed for client [%s] at "
655 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 }
659 }
660 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100661 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
David Brownella1d9e6e2007-05-01 23:26:30 +0200663 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100665 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Jean Delvarecaada322008-01-27 18:14:49 +0100667 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
David Brownellc0564602007-05-01 23:26:31 +0200669EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
David Brownell7b4fbc52007-05-01 23:26:30 +0200671/* ------------------------------------------------------------------------- */
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
674{
675 struct list_head *item;
676 struct i2c_client *client;
677
678 list_for_each(item,&adapter->clients) {
679 client = list_entry(item, struct i2c_client, list);
680 if (client->addr == addr)
681 return -EBUSY;
682 }
683 return 0;
684}
685
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100686static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 int rval;
689
Ingo Molnar5c085d32006-01-18 23:16:04 +0100690 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100692 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 return rval;
695}
696
697int i2c_attach_client(struct i2c_client *client)
698{
699 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200700 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Ingo Molnar5c085d32006-01-18 23:16:04 +0100702 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200704 res = -EBUSY;
705 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200711
712 if (client->driver)
713 client->dev.driver = &client->driver->driver;
714
David Brownellde81d2a2007-05-22 19:49:16 +0200715 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200716 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200717 client->dev.uevent_suppress = 1;
718 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200719 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
722 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200723 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
724 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200725 res = device_register(&client->dev);
726 if (res)
727 goto out_list;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200728 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200729
730 if (adapter->client_register) {
731 if (adapter->client_register(client)) {
732 dev_dbg(&adapter->dev, "client_register "
733 "failed for client [%s] at 0x%02x\n",
734 client->name, client->addr);
735 }
736 }
737
738 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200739
Jean Delvareb119c6c2006-08-15 18:26:30 +0200740out_list:
741 list_del(&client->list);
742 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
743 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200744out_unlock:
745 mutex_unlock(&adapter->clist_lock);
746 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
David Brownellc0564602007-05-01 23:26:31 +0200748EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750int i2c_detach_client(struct i2c_client *client)
751{
752 struct i2c_adapter *adapter = client->adapter;
753 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (adapter->client_unregister) {
756 res = adapter->client_unregister(client);
757 if (res) {
758 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700759 "client_unregister [%s] failed, "
760 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto out;
762 }
763 }
764
Ingo Molnar5c085d32006-01-18 23:16:04 +0100765 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 list_del(&client->list);
767 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100769 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 wait_for_completion(&client->released);
771
772 out:
773 return res;
774}
David Brownellc0564602007-05-01 23:26:31 +0200775EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jean Delvaree48d3312008-01-27 18:14:48 +0100777/**
778 * i2c_use_client - increments the reference count of the i2c client structure
779 * @client: the client being referenced
780 *
781 * Each live reference to a client should be refcounted. The driver model does
782 * that automatically as part of driver binding, so that most drivers don't
783 * need to do this explicitly: they hold a reference until they're unbound
784 * from the device.
785 *
786 * A pointer to the client with the incremented reference counter is returned.
787 */
788struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100790 get_device(&client->dev);
Jean Delvaree48d3312008-01-27 18:14:48 +0100791 return client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
David Brownellc0564602007-05-01 23:26:31 +0200793EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Jean Delvaree48d3312008-01-27 18:14:48 +0100795/**
796 * i2c_release_client - release a use of the i2c client structure
797 * @client: the client being no longer referenced
798 *
799 * Must be called when a user of a client is finished with it.
800 */
801void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100803 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
David Brownellc0564602007-05-01 23:26:31 +0200805EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
808{
809 struct list_head *item;
810 struct i2c_client *client;
811
Ingo Molnar5c085d32006-01-18 23:16:04 +0100812 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 list_for_each(item,&adap->clients) {
814 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100815 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 continue;
817 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100818 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100820 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100822 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100824 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
David Brownellc0564602007-05-01 23:26:31 +0200826EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828static int __init i2c_init(void)
829{
830 int retval;
831
832 retval = bus_register(&i2c_bus_type);
833 if (retval)
834 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return class_register(&i2c_adapter_class);
836}
837
838static void __exit i2c_exit(void)
839{
840 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 bus_unregister(&i2c_bus_type);
842}
843
844subsys_initcall(i2c_init);
845module_exit(i2c_exit);
846
847/* ----------------------------------------------------
848 * the functional interface to the i2c busses.
849 * ----------------------------------------------------
850 */
851
852int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
853{
854 int ret;
855
856 if (adap->algo->master_xfer) {
857#ifdef DEBUG
858 for (ret = 0; ret < num; ret++) {
859 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200860 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
861 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
862 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864#endif
865
Mike Rapoportcea443a82008-01-27 18:14:50 +0100866 if (in_atomic() || irqs_disabled()) {
867 ret = mutex_trylock(&adap->bus_lock);
868 if (!ret)
869 /* I2C activity is ongoing. */
870 return -EAGAIN;
871 } else {
872 mutex_lock_nested(&adap->bus_lock, adap->level);
873 }
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100876 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return ret;
879 } else {
880 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
881 return -ENOSYS;
882 }
883}
David Brownellc0564602007-05-01 23:26:31 +0200884EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
887{
888 int ret;
889 struct i2c_adapter *adap=client->adapter;
890 struct i2c_msg msg;
891
Jean Delvare815f55f2005-05-07 22:58:46 +0200892 msg.addr = client->addr;
893 msg.flags = client->flags & I2C_M_TEN;
894 msg.len = count;
895 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100896
Jean Delvare815f55f2005-05-07 22:58:46 +0200897 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Jean Delvare815f55f2005-05-07 22:58:46 +0200899 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
900 transmitted, else error code. */
901 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
David Brownellc0564602007-05-01 23:26:31 +0200903EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
906{
907 struct i2c_adapter *adap=client->adapter;
908 struct i2c_msg msg;
909 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Jean Delvare815f55f2005-05-07 22:58:46 +0200911 msg.addr = client->addr;
912 msg.flags = client->flags & I2C_M_TEN;
913 msg.flags |= I2C_M_RD;
914 msg.len = count;
915 msg.buf = buf;
916
917 ret = i2c_transfer(adap, &msg, 1);
918
919 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
920 transmitted, else error code. */
921 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
David Brownellc0564602007-05-01 23:26:31 +0200923EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/* ----------------------------------------------------
926 * the i2c address scanning function
927 * Will not work for 10-bit addresses!
928 * ----------------------------------------------------
929 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200930static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
931 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200932{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200933 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200934
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200935 /* Make sure the address is valid */
936 if (addr < 0x03 || addr > 0x77) {
937 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
938 addr);
939 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200940 }
941
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200942 /* Skip if already in use */
943 if (i2c_check_addr(adapter, addr))
944 return 0;
945
946 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200947 if (kind < 0) {
948 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
949 I2C_SMBUS_QUICK, NULL) < 0)
950 return 0;
951
952 /* prevent 24RF08 corruption */
953 if ((addr & ~0x0f) == 0x50)
954 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
955 I2C_SMBUS_QUICK, NULL);
956 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200957
958 /* Finally call the custom detection function */
959 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200960 /* -ENODEV can be returned if there is a chip at the given address
961 but it isn't supported by this chip driver. We catch it here as
962 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200963 if (err == -ENODEV)
964 err = 0;
965
966 if (err)
967 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
968 addr, err);
969 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +0100973 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int (*found_proc) (struct i2c_adapter *, int, int))
975{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200976 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int adap_id = i2c_adapter_id(adapter);
978
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200979 /* Force entries are done first, and are not affected by ignore
980 entries */
981 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +0100982 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200983 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200985 for (kind = 0; forces[kind]; kind++) {
986 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
987 i += 2) {
988 if (forces[kind][i] == adap_id
989 || forces[kind][i] == ANY_I2C_BUS) {
990 dev_dbg(&adapter->dev, "found force "
991 "parameter for adapter %d, "
992 "addr 0x%02x, kind %d\n",
993 adap_id, forces[kind][i + 1],
994 kind);
995 err = i2c_probe_address(adapter,
996 forces[kind][i + 1],
997 kind, found_proc);
998 if (err)
999 return err;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001004
Jean Delvare4366dc92005-09-25 16:50:06 +02001005 /* Stop here if we can't use SMBUS_QUICK */
1006 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1007 if (address_data->probe[0] == I2C_CLIENT_END
1008 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001009 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001010
1011 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1012 "can't probe for chips\n");
1013 return -1;
1014 }
1015
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001016 /* Probe entries are done second, and are not affected by ignore
1017 entries either */
1018 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1019 if (address_data->probe[i] == adap_id
1020 || address_data->probe[i] == ANY_I2C_BUS) {
1021 dev_dbg(&adapter->dev, "found probe parameter for "
1022 "adapter %d, addr 0x%02x\n", adap_id,
1023 address_data->probe[i + 1]);
1024 err = i2c_probe_address(adapter,
1025 address_data->probe[i + 1],
1026 -1, found_proc);
1027 if (err)
1028 return err;
1029 }
1030 }
1031
1032 /* Normal entries are done last, unless shadowed by an ignore entry */
1033 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1034 int j, ignore;
1035
1036 ignore = 0;
1037 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1038 j += 2) {
1039 if ((address_data->ignore[j] == adap_id ||
1040 address_data->ignore[j] == ANY_I2C_BUS)
1041 && address_data->ignore[j + 1]
1042 == address_data->normal_i2c[i]) {
1043 dev_dbg(&adapter->dev, "found ignore "
1044 "parameter for adapter %d, "
1045 "addr 0x%02x\n", adap_id,
1046 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001047 ignore = 1;
1048 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001049 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001050 }
1051 if (ignore)
1052 continue;
1053
1054 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1055 "addr 0x%02x\n", adap_id,
1056 address_data->normal_i2c[i]);
1057 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1058 -1, found_proc);
1059 if (err)
1060 return err;
1061 }
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064}
David Brownellc0564602007-05-01 23:26:31 +02001065EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Jean Delvare12b5053a2007-05-01 23:26:31 +02001067struct i2c_client *
1068i2c_new_probed_device(struct i2c_adapter *adap,
1069 struct i2c_board_info *info,
1070 unsigned short const *addr_list)
1071{
1072 int i;
1073
1074 /* Stop here if the bus doesn't support probing */
1075 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1076 dev_err(&adap->dev, "Probing not supported\n");
1077 return NULL;
1078 }
1079
1080 mutex_lock(&adap->clist_lock);
1081 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1082 /* Check address validity */
1083 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1084 dev_warn(&adap->dev, "Invalid 7-bit address "
1085 "0x%02x\n", addr_list[i]);
1086 continue;
1087 }
1088
1089 /* Check address availability */
1090 if (__i2c_check_addr(adap, addr_list[i])) {
1091 dev_dbg(&adap->dev, "Address 0x%02x already in "
1092 "use, not probing\n", addr_list[i]);
1093 continue;
1094 }
1095
1096 /* Test address responsiveness
1097 The default probe method is a quick write, but it is known
1098 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1099 and could also irreversibly write-protect some EEPROMs, so
1100 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1101 read instead. Also, some bus drivers don't implement
1102 quick write, so we fallback to a byte read it that case
1103 too. */
1104 if ((addr_list[i] & ~0x07) == 0x30
1105 || (addr_list[i] & ~0x0f) == 0x50
1106 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1107 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1108 I2C_SMBUS_READ, 0,
1109 I2C_SMBUS_BYTE, NULL) >= 0)
1110 break;
1111 } else {
1112 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1113 I2C_SMBUS_WRITE, 0,
1114 I2C_SMBUS_QUICK, NULL) >= 0)
1115 break;
1116 }
1117 }
1118 mutex_unlock(&adap->clist_lock);
1119
1120 if (addr_list[i] == I2C_CLIENT_END) {
1121 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1122 return NULL;
1123 }
1124
1125 info->addr = addr_list[i];
1126 return i2c_new_device(adap, info);
1127}
1128EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130struct i2c_adapter* i2c_get_adapter(int id)
1131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001133
Jean Delvarecaada322008-01-27 18:14:49 +01001134 mutex_lock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001135 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1136 if (adapter && !try_module_get(adapter->owner))
1137 adapter = NULL;
1138
Jean Delvarecaada322008-01-27 18:14:49 +01001139 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001140 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
David Brownellc0564602007-05-01 23:26:31 +02001142EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144void i2c_put_adapter(struct i2c_adapter *adap)
1145{
1146 module_put(adap->owner);
1147}
David Brownellc0564602007-05-01 23:26:31 +02001148EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150/* The SMBus parts */
1151
David Brownell438d6c22006-12-10 21:21:31 +01001152#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153static u8
1154crc8(u16 data)
1155{
1156 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001159 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 data = data ^ POLY;
1161 data = data << 1;
1162 }
1163 return (u8)(data >> 8);
1164}
1165
Jean Delvare421ef472005-10-26 21:28:55 +02001166/* Incremental CRC8 over count bytes in the array pointed to by p */
1167static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 int i;
1170
1171 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001172 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return crc;
1174}
1175
Jean Delvare421ef472005-10-26 21:28:55 +02001176/* Assume a 7-bit address, which is reasonable for SMBus */
1177static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Jean Delvare421ef472005-10-26 21:28:55 +02001179 /* The address will be sent first */
1180 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1181 pec = i2c_smbus_pec(pec, &addr, 1);
1182
1183 /* The data buffer follows */
1184 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Jean Delvare421ef472005-10-26 21:28:55 +02001187/* Used for write only transactions */
1188static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Jean Delvare421ef472005-10-26 21:28:55 +02001190 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1191 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Jean Delvare421ef472005-10-26 21:28:55 +02001194/* Return <0 on CRC error
1195 If there was a write before this read (most cases) we need to take the
1196 partial CRC from the write part into account.
1197 Note that this function does modify the message (we need to decrease the
1198 message length to hide the CRC byte from the caller). */
1199static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Jean Delvare421ef472005-10-26 21:28:55 +02001201 u8 rpec = msg->buf[--msg->len];
1202 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (rpec != cpec) {
1205 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1206 rpec, cpec);
1207 return -1;
1208 }
David Brownell438d6c22006-12-10 21:21:31 +01001209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1213{
1214 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001215 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
David Brownellc0564602007-05-01 23:26:31 +02001217EXPORT_SYMBOL(i2c_smbus_write_quick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219s32 i2c_smbus_read_byte(struct i2c_client *client)
1220{
1221 union i2c_smbus_data data;
1222 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1223 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1224 return -1;
1225 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001226 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
David Brownellc0564602007-05-01 23:26:31 +02001228EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001233 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
David Brownellc0564602007-05-01 23:26:31 +02001235EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1238{
1239 union i2c_smbus_data data;
1240 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1241 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1242 return -1;
1243 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001244 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
David Brownellc0564602007-05-01 23:26:31 +02001246EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1249{
1250 union i2c_smbus_data data;
1251 data.byte = value;
1252 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1253 I2C_SMBUS_WRITE,command,
1254 I2C_SMBUS_BYTE_DATA,&data);
1255}
David Brownellc0564602007-05-01 23:26:31 +02001256EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1259{
1260 union i2c_smbus_data data;
1261 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1262 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1263 return -1;
1264 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001265 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
David Brownellc0564602007-05-01 23:26:31 +02001267EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1270{
1271 union i2c_smbus_data data;
1272 data.word = value;
1273 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1274 I2C_SMBUS_WRITE,command,
1275 I2C_SMBUS_WORD_DATA,&data);
1276}
David Brownellc0564602007-05-01 23:26:31 +02001277EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
David Brownella64ec072007-10-13 23:56:31 +02001279/**
1280 * i2c_smbus_read_block_data - SMBus block read request
1281 * @client: Handle to slave device
1282 * @command: Command byte issued to let the slave know what data should
1283 * be returned
1284 * @values: Byte array into which data will be read; big enough to hold
1285 * the data returned by the slave. SMBus allows at most 32 bytes.
1286 *
1287 * Returns the number of bytes read in the slave's response, else a
1288 * negative number to indicate some kind of error.
1289 *
1290 * Note that using this function requires that the client's adapter support
1291 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1292 * support this; its emulation through I2C messaging relies on a specific
1293 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1294 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001295s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1296 u8 *values)
1297{
1298 union i2c_smbus_data data;
1299
1300 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1301 I2C_SMBUS_READ, command,
1302 I2C_SMBUS_BLOCK_DATA, &data))
1303 return -1;
1304
1305 memcpy(values, &data.block[1], data.block[0]);
1306 return data.block[0];
1307}
1308EXPORT_SYMBOL(i2c_smbus_read_block_data);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001311 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (length > I2C_SMBUS_BLOCK_MAX)
1316 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001318 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1320 I2C_SMBUS_WRITE,command,
1321 I2C_SMBUS_BLOCK_DATA,&data);
1322}
David Brownellc0564602007-05-01 23:26:31 +02001323EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001326s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1327 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001330
Jean Delvare4b2643d2007-07-12 14:12:29 +02001331 if (length > I2C_SMBUS_BLOCK_MAX)
1332 length = I2C_SMBUS_BLOCK_MAX;
1333 data.block[0] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1335 I2C_SMBUS_READ,command,
1336 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1337 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001338
1339 memcpy(values, &data.block[1], data.block[0]);
1340 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
David Brownellc0564602007-05-01 23:26:31 +02001342EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Jean Delvare21bbd692006-01-09 15:19:18 +11001344s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001345 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001346{
1347 union i2c_smbus_data data;
1348
1349 if (length > I2C_SMBUS_BLOCK_MAX)
1350 length = I2C_SMBUS_BLOCK_MAX;
1351 data.block[0] = length;
1352 memcpy(data.block + 1, values, length);
1353 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1354 I2C_SMBUS_WRITE, command,
1355 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1356}
David Brownellc0564602007-05-01 23:26:31 +02001357EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001358
David Brownell438d6c22006-12-10 21:21:31 +01001359/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001361static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001363 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 union i2c_smbus_data * data)
1365{
1366 /* So we need to generate a series of msgs. In the case of writing, we
1367 need to use only one message; when reading, we need two. We initialize
1368 most things with sane defaults, to keep the code below somewhat
1369 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001370 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1371 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001373 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1375 };
1376 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001377 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 msgbuf0[0] = command;
1380 switch(size) {
1381 case I2C_SMBUS_QUICK:
1382 msg[0].len = 0;
1383 /* Special case: The read/write field is used as data */
1384 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1385 num = 1;
1386 break;
1387 case I2C_SMBUS_BYTE:
1388 if (read_write == I2C_SMBUS_READ) {
1389 /* Special case: only a read! */
1390 msg[0].flags = I2C_M_RD | flags;
1391 num = 1;
1392 }
1393 break;
1394 case I2C_SMBUS_BYTE_DATA:
1395 if (read_write == I2C_SMBUS_READ)
1396 msg[1].len = 1;
1397 else {
1398 msg[0].len = 2;
1399 msgbuf0[1] = data->byte;
1400 }
1401 break;
1402 case I2C_SMBUS_WORD_DATA:
1403 if (read_write == I2C_SMBUS_READ)
1404 msg[1].len = 2;
1405 else {
1406 msg[0].len=3;
1407 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001408 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410 break;
1411 case I2C_SMBUS_PROC_CALL:
1412 num = 2; /* Special case */
1413 read_write = I2C_SMBUS_READ;
1414 msg[0].len = 3;
1415 msg[1].len = 2;
1416 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001417 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 break;
1419 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001421 msg[1].flags |= I2C_M_RECV_LEN;
1422 msg[1].len = 1; /* block length will be added by
1423 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 } else {
1425 msg[0].len = data->block[0] + 2;
1426 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1427 dev_err(&adapter->dev, "smbus_access called with "
1428 "invalid block write size (%d)\n",
1429 data->block[0]);
1430 return -1;
1431 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001432 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 msgbuf0[i] = data->block[i-1];
1434 }
1435 break;
1436 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001437 num = 2; /* Another special case */
1438 read_write = I2C_SMBUS_READ;
1439 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1440 dev_err(&adapter->dev, "%s called with invalid "
1441 "block proc call size (%d)\n", __FUNCTION__,
1442 data->block[0]);
1443 return -1;
1444 }
1445 msg[0].len = data->block[0] + 2;
1446 for (i = 1; i < msg[0].len; i++)
1447 msgbuf0[i] = data->block[i-1];
1448 msg[1].flags |= I2C_M_RECV_LEN;
1449 msg[1].len = 1; /* block length will be added by
1450 the underlying bus driver */
1451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 case I2C_SMBUS_I2C_BLOCK_DATA:
1453 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001454 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 } else {
1456 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001457 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1459 "invalid block write size (%d)\n",
1460 data->block[0]);
1461 return -1;
1462 }
1463 for (i = 1; i <= data->block[0]; i++)
1464 msgbuf0[i] = data->block[i];
1465 }
1466 break;
1467 default:
1468 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1469 size);
1470 return -1;
1471 }
1472
Jean Delvare421ef472005-10-26 21:28:55 +02001473 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1474 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1475 if (i) {
1476 /* Compute PEC if first message is a write */
1477 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001478 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001479 i2c_smbus_add_pec(&msg[0]);
1480 else /* Write followed by read */
1481 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1482 }
1483 /* Ask for PEC if last message is a read */
1484 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001485 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001486 }
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (i2c_transfer(adapter, msg, num) < 0)
1489 return -1;
1490
Jean Delvare421ef472005-10-26 21:28:55 +02001491 /* Check PEC if last message is a read */
1492 if (i && (msg[num-1].flags & I2C_M_RD)) {
1493 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1494 return -1;
1495 }
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (read_write == I2C_SMBUS_READ)
1498 switch(size) {
1499 case I2C_SMBUS_BYTE:
1500 data->byte = msgbuf0[0];
1501 break;
1502 case I2C_SMBUS_BYTE_DATA:
1503 data->byte = msgbuf1[0];
1504 break;
David Brownell438d6c22006-12-10 21:21:31 +01001505 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 case I2C_SMBUS_PROC_CALL:
1507 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1508 break;
1509 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001510 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 data->block[i+1] = msgbuf1[i];
1512 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001513 case I2C_SMBUS_BLOCK_DATA:
1514 case I2C_SMBUS_BLOCK_PROC_CALL:
1515 for (i = 0; i < msgbuf1[0] + 1; i++)
1516 data->block[i] = msgbuf1[i];
1517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 return 0;
1520}
1521
1522
1523s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001524 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 union i2c_smbus_data * data)
1526{
1527 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001532 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1534 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001535 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 } else
1537 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1538 command,size,data);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return res;
1541}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1545MODULE_DESCRIPTION("I2C-Bus main module");
1546MODULE_LICENSE("GPL");