blob: 3495ab4035ecc92158d331708ba2f5e597c409ef [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
Jean Delvare87c6c222008-01-27 18:14:48 +010037#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
David Brownell9c1600e2007-05-01 23:26:31 +020039#include "i2c-core.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static LIST_HEAD(drivers);
Arjan van de Venb3585e42006-01-11 10:50:26 +010043static DEFINE_MUTEX(core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static DEFINE_IDR(i2c_adapter_idr);
45
David Brownella1d9e6e2007-05-01 23:26:30 +020046#define is_newstyle_driver(d) ((d)->probe || (d)->remove)
David Brownellf37dd802007-02-13 22:09:00 +010047
48/* ------------------------------------------------------------------------- */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int i2c_device_match(struct device *dev, struct device_driver *drv)
51{
David Brownell7b4fbc52007-05-01 23:26:30 +020052 struct i2c_client *client = to_i2c_client(dev);
53 struct i2c_driver *driver = to_i2c_driver(drv);
54
55 /* make legacy i2c drivers bypass driver model probing entirely;
56 * such drivers scan each i2c adapter/bus themselves.
57 */
David Brownella1d9e6e2007-05-01 23:26:30 +020058 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020059 return 0;
60
61 /* new style drivers use the same kind of driver matching policy
62 * as platform devices or SPI: compare device and driver IDs.
63 */
64 return strcmp(client->driver_name, drv->name) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
David Brownell7b4fbc52007-05-01 23:26:30 +020067#ifdef CONFIG_HOTPLUG
68
69/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020070static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020071{
72 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020073
74 /* by definition, legacy drivers can't hotplug */
75 if (dev->driver || !client->driver_name)
76 return 0;
77
Kay Sievers7eff2e72007-08-14 15:15:12 +020078 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
David Brownell7b4fbc52007-05-01 23:26:30 +020079 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020080 dev_dbg(dev, "uevent\n");
81 return 0;
82}
83
84#else
85#define i2c_device_uevent NULL
86#endif /* CONFIG_HOTPLUG */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int i2c_device_probe(struct device *dev)
89{
David Brownell7b4fbc52007-05-01 23:26:30 +020090 struct i2c_client *client = to_i2c_client(dev);
91 struct i2c_driver *driver = to_i2c_driver(dev->driver);
92
93 if (!driver->probe)
94 return -ENODEV;
95 client->driver = driver;
96 dev_dbg(dev, "probe\n");
97 return driver->probe(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static int i2c_device_remove(struct device *dev)
101{
David Brownella1d9e6e2007-05-01 23:26:30 +0200102 struct i2c_client *client = to_i2c_client(dev);
103 struct i2c_driver *driver;
104 int status;
105
106 if (!dev->driver)
107 return 0;
108
109 driver = to_i2c_driver(dev->driver);
110 if (driver->remove) {
111 dev_dbg(dev, "remove\n");
112 status = driver->remove(client);
113 } else {
114 dev->driver = NULL;
115 status = 0;
116 }
117 if (status == 0)
118 client->driver = NULL;
119 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
David Brownellf37dd802007-02-13 22:09:00 +0100122static void i2c_device_shutdown(struct device *dev)
123{
124 struct i2c_driver *driver;
125
126 if (!dev->driver)
127 return;
128 driver = to_i2c_driver(dev->driver);
129 if (driver->shutdown)
130 driver->shutdown(to_i2c_client(dev));
131}
132
133static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
134{
135 struct i2c_driver *driver;
136
137 if (!dev->driver)
138 return 0;
139 driver = to_i2c_driver(dev->driver);
140 if (!driver->suspend)
141 return 0;
142 return driver->suspend(to_i2c_client(dev), mesg);
143}
144
145static int i2c_device_resume(struct device * dev)
146{
147 struct i2c_driver *driver;
148
149 if (!dev->driver)
150 return 0;
151 driver = to_i2c_driver(dev->driver);
152 if (!driver->resume)
153 return 0;
154 return driver->resume(to_i2c_client(dev));
155}
156
David Brownell7b4fbc52007-05-01 23:26:30 +0200157static void i2c_client_release(struct device *dev)
158{
159 struct i2c_client *client = to_i2c_client(dev);
160 complete(&client->released);
161}
162
David Brownell9c1600e2007-05-01 23:26:31 +0200163static void i2c_client_dev_release(struct device *dev)
164{
165 kfree(to_i2c_client(dev));
166}
167
David Brownell7b4fbc52007-05-01 23:26:30 +0200168static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
169{
170 struct i2c_client *client = to_i2c_client(dev);
171 return sprintf(buf, "%s\n", client->name);
172}
173
174static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
175{
176 struct i2c_client *client = to_i2c_client(dev);
177 return client->driver_name
178 ? sprintf(buf, "%s\n", client->driver_name)
179 : 0;
180}
181
182static struct device_attribute i2c_dev_attrs[] = {
183 __ATTR(name, S_IRUGO, show_client_name, NULL),
184 /* modalias helps coldplug: modprobe $(cat .../modalias) */
185 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
186 { },
187};
188
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200189static struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100190 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200191 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100192 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200193 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100194 .probe = i2c_device_probe,
195 .remove = i2c_device_remove,
196 .shutdown = i2c_device_shutdown,
197 .suspend = i2c_device_suspend,
198 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000199};
200
David Brownell9c1600e2007-05-01 23:26:31 +0200201/**
202 * i2c_new_device - instantiate an i2c device for use with a new style driver
203 * @adap: the adapter managing the device
204 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200205 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200206 *
207 * Create a device to work with a new style i2c driver, where binding is
208 * handled through driver model probe()/remove() methods. This call is not
209 * appropriate for use by mainboad initialization logic, which usually runs
210 * during an arch_initcall() long before any i2c_adapter could exist.
211 *
212 * This returns the new i2c client, which may be saved for later use with
213 * i2c_unregister_device(); or NULL to indicate an error.
214 */
215struct i2c_client *
216i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
217{
218 struct i2c_client *client;
219 int status;
220
221 client = kzalloc(sizeof *client, GFP_KERNEL);
222 if (!client)
223 return NULL;
224
225 client->adapter = adap;
226
227 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200228 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
229
230 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200231 client->addr = info->addr;
232 client->irq = info->irq;
233
234 strlcpy(client->driver_name, info->driver_name,
235 sizeof(client->driver_name));
236 strlcpy(client->name, info->type, sizeof(client->name));
237
238 /* a new style driver may be bound to this device when we
239 * return from this function, or any later moment (e.g. maybe
240 * hotplugging will load the driver module). and the device
241 * refcount model is the standard driver model one.
242 */
243 status = i2c_attach_client(client);
244 if (status < 0) {
245 kfree(client);
246 client = NULL;
247 }
248 return client;
249}
250EXPORT_SYMBOL_GPL(i2c_new_device);
251
252
253/**
254 * i2c_unregister_device - reverse effect of i2c_new_device()
255 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200256 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200257 */
258void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200259{
260 struct i2c_adapter *adapter = client->adapter;
261 struct i2c_driver *driver = client->driver;
262
263 if (driver && !is_newstyle_driver(driver)) {
264 dev_err(&client->dev, "can't unregister devices "
265 "with legacy drivers\n");
266 WARN_ON(1);
267 return;
268 }
269
270 mutex_lock(&adapter->clist_lock);
271 list_del(&client->list);
272 mutex_unlock(&adapter->clist_lock);
273
274 device_unregister(&client->dev);
275}
David Brownell9c1600e2007-05-01 23:26:31 +0200276EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200277
278
David Brownellf37dd802007-02-13 22:09:00 +0100279/* ------------------------------------------------------------------------- */
280
David Brownell16ffadf2007-05-01 23:26:28 +0200281/* I2C bus adapters -- one roots each I2C or SMBUS segment */
282
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200283static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
David Brownellef2c8322007-05-01 23:26:28 +0200285 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 complete(&adap->dev_released);
287}
288
David Brownell16ffadf2007-05-01 23:26:28 +0200289static ssize_t
290show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
David Brownellef2c8322007-05-01 23:26:28 +0200292 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return sprintf(buf, "%s\n", adap->name);
294}
David Brownell16ffadf2007-05-01 23:26:28 +0200295
296static struct device_attribute i2c_adapter_attrs[] = {
297 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
298 { },
299};
300
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200301static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200302 .owner = THIS_MODULE,
303 .name = "i2c-adapter",
304 .dev_attrs = i2c_adapter_attrs,
305};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Brownell9c1600e2007-05-01 23:26:31 +0200307static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
308{
309 struct i2c_devinfo *devinfo;
310
311 mutex_lock(&__i2c_board_lock);
312 list_for_each_entry(devinfo, &__i2c_board_list, list) {
313 if (devinfo->busnum == adapter->nr
314 && !i2c_new_device(adapter,
315 &devinfo->board_info))
316 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
317 i2c_adapter_id(adapter),
318 devinfo->board_info.addr);
319 }
320 mutex_unlock(&__i2c_board_lock);
321}
322
David Brownell6e13e642007-05-01 23:26:31 +0200323static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
David Brownell6e13e642007-05-01 23:26:31 +0200325 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct list_head *item;
327 struct i2c_driver *driver;
328
Ingo Molnar5c085d32006-01-18 23:16:04 +0100329 mutex_init(&adap->bus_lock);
330 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 INIT_LIST_HEAD(&adap->clients);
332
David Brownell6e13e642007-05-01 23:26:31 +0200333 mutex_lock(&core_lists);
David Brownell6e13e642007-05-01 23:26:31 +0200334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* Add the adapter to the driver core.
336 * If the parent pointer is not set up,
337 * we add this adapter to the host bus.
338 */
David Brownellb119dc32007-01-04 13:07:04 +0100339 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100341 pr_debug("I2C adapter driver [%s] forgot to specify "
342 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200346 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200347 res = device_register(&adap->dev);
348 if (res)
349 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200351 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
352
David Brownell6e13e642007-05-01 23:26:31 +0200353 /* create pre-declared device nodes for new-style drivers */
354 if (adap->nr < __i2c_first_dynamic_bus_num)
355 i2c_scan_static_board_info(adap);
356
David Brownell7b4fbc52007-05-01 23:26:30 +0200357 /* let legacy drivers scan this bus for matching devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 list_for_each(item,&drivers) {
359 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100360 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* We ignore the return code; if it fails, too bad */
362 driver->attach_adapter(adap);
363 }
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100366 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200368
Jean Delvareb119c6c2006-08-15 18:26:30 +0200369out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200370 idr_remove(&i2c_adapter_idr, adap->nr);
371 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
David Brownell6e13e642007-05-01 23:26:31 +0200374/**
375 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
376 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200377 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200378 *
379 * This routine is used to declare an I2C adapter when its bus number
380 * doesn't matter. Examples: for I2C adapters dynamically added by
381 * USB links or PCI plugin cards.
382 *
383 * When this returns zero, a new bus number was allocated and stored
384 * in adap->nr, and the specified adapter became available for clients.
385 * Otherwise, a negative errno value is returned.
386 */
387int i2c_add_adapter(struct i2c_adapter *adapter)
388{
389 int id, res = 0;
390
391retry:
392 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
393 return -ENOMEM;
394
395 mutex_lock(&core_lists);
396 /* "above" here means "above or equal to", sigh */
397 res = idr_get_new_above(&i2c_adapter_idr, adapter,
398 __i2c_first_dynamic_bus_num, &id);
399 mutex_unlock(&core_lists);
400
401 if (res < 0) {
402 if (res == -EAGAIN)
403 goto retry;
404 return res;
405 }
406
407 adapter->nr = id;
408 return i2c_register_adapter(adapter);
409}
410EXPORT_SYMBOL(i2c_add_adapter);
411
412/**
413 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
414 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200415 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200416 *
417 * This routine is used to declare an I2C adapter when its bus number
418 * matters. Example: for I2C adapters from system-on-chip CPUs, or
419 * otherwise built in to the system's mainboard, and where i2c_board_info
420 * is used to properly configure I2C devices.
421 *
422 * If no devices have pre-been declared for this bus, then be sure to
423 * register the adapter before any dynamically allocated ones. Otherwise
424 * the required bus ID may not be available.
425 *
426 * When this returns zero, the specified adapter became available for
427 * clients using the bus number provided in adap->nr. Also, the table
428 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
429 * and the appropriate driver model device nodes are created. Otherwise, a
430 * negative errno value is returned.
431 */
432int i2c_add_numbered_adapter(struct i2c_adapter *adap)
433{
434 int id;
435 int status;
436
437 if (adap->nr & ~MAX_ID_MASK)
438 return -EINVAL;
439
440retry:
441 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
442 return -ENOMEM;
443
444 mutex_lock(&core_lists);
445 /* "above" here means "above or equal to", sigh;
446 * we need the "equal to" result to force the result
447 */
448 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
449 if (status == 0 && id != adap->nr) {
450 status = -EBUSY;
451 idr_remove(&i2c_adapter_idr, id);
452 }
453 mutex_unlock(&core_lists);
454 if (status == -EAGAIN)
455 goto retry;
456
457 if (status == 0)
458 status = i2c_register_adapter(adap);
459 return status;
460}
461EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
462
David Brownelld64f73b2007-07-12 14:12:28 +0200463/**
464 * i2c_del_adapter - unregister I2C adapter
465 * @adap: the adapter being unregistered
466 * Context: can sleep
467 *
468 * This unregisters an I2C adapter which was previously registered
469 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471int i2c_del_adapter(struct i2c_adapter *adap)
472{
473 struct list_head *item, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct i2c_driver *driver;
475 struct i2c_client *client;
476 int res = 0;
477
Arjan van de Venb3585e42006-01-11 10:50:26 +0100478 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100481 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200482 pr_debug("i2c-core: attempting to delete unregistered "
483 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 res = -EINVAL;
485 goto out_unlock;
486 }
487
488 list_for_each(item,&drivers) {
489 driver = list_entry(item, struct i2c_driver, list);
490 if (driver->detach_adapter)
491 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200492 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100493 "for driver [%s]\n",
494 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto out_unlock;
496 }
497 }
498
499 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200500 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200502 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
David Brownella1d9e6e2007-05-01 23:26:30 +0200504 client = list_entry(item, struct i2c_client, list);
505 driver = client->driver;
506
507 /* new style, follow standard driver model */
508 if (!driver || is_newstyle_driver(driver)) {
509 i2c_unregister_device(client);
510 continue;
511 }
512
513 /* legacy drivers create and remove clients themselves */
514 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200515 dev_err(&adap->dev, "detach_client failed for client "
516 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 client->addr);
518 goto out_unlock;
519 }
520 }
521
522 /* clean up the sysfs representation */
523 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* wait for sysfs to drop all references */
527 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
David Brownell6e13e642007-05-01 23:26:31 +0200529 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 idr_remove(&i2c_adapter_idr, adap->nr);
531
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200532 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100535 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return res;
537}
David Brownellc0564602007-05-01 23:26:31 +0200538EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540
David Brownell7b4fbc52007-05-01 23:26:30 +0200541/* ------------------------------------------------------------------------- */
542
543/*
544 * An i2c_driver is used with one or more i2c_client (device) nodes to access
545 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
546 * are two models for binding the driver to its device: "new style" drivers
547 * follow the standard Linux driver model and just respond to probe() calls
548 * issued if the driver core sees they match(); "legacy" drivers create device
549 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
551
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800552int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100554 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
David Brownell7b4fbc52007-05-01 23:26:30 +0200556 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200557 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200558 if (driver->attach_adapter || driver->detach_adapter
559 || driver->detach_client) {
560 printk(KERN_WARNING
561 "i2c-core: driver [%s] is confused\n",
562 driver->driver.name);
563 return -EINVAL;
564 }
565 }
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800568 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
David Brownell6e13e642007-05-01 23:26:31 +0200571 /* for new style drivers, when registration returns the driver core
572 * will have called probe() for all matching-but-unbound devices.
573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 res = driver_register(&driver->driver);
575 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100576 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100577
Jean Delvare7eebcb72006-02-05 23:28:21 +0100578 mutex_lock(&core_lists);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100581 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
David Brownell7b4fbc52007-05-01 23:26:30 +0200583 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100584 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200585 struct i2c_adapter *adapter;
586
Jean Delvare87c6c222008-01-27 18:14:48 +0100587 down(&i2c_adapter_class.sem);
588 list_for_each_entry(adapter, &i2c_adapter_class.devices,
589 dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 driver->attach_adapter(adapter);
591 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100592 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Arjan van de Venb3585e42006-01-11 10:50:26 +0100595 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800598EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
David Brownella1d9e6e2007-05-01 23:26:30 +0200600/**
601 * i2c_del_driver - unregister I2C driver
602 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200603 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200604 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200605void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Jean Delvare87c6c222008-01-27 18:14:48 +0100607 struct list_head *item2, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct i2c_client *client;
609 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100610
Arjan van de Venb3585e42006-01-11 10:50:26 +0100611 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
David Brownella1d9e6e2007-05-01 23:26:30 +0200613 /* new-style driver? */
614 if (is_newstyle_driver(driver))
615 goto unregister;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100618 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Jean Delvare87c6c222008-01-27 18:14:48 +0100621 down(&i2c_adapter_class.sem);
622 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (driver->detach_adapter) {
Jean Delvareb3e82092007-05-01 23:26:32 +0200624 if (driver->detach_adapter(adap)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200625 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100626 "for driver [%s]\n",
627 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 } else {
630 list_for_each_safe(item2, _n, &adap->clients) {
631 client = list_entry(item2, struct i2c_client, list);
632 if (client->driver != driver)
633 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200634 dev_dbg(&adap->dev, "detaching client [%s] "
635 "at 0x%02x\n", client->name,
636 client->addr);
Jean Delvareb3e82092007-05-01 23:26:32 +0200637 if (driver->detach_client(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200638 dev_err(&adap->dev, "detach_client "
639 "failed for client [%s] at "
640 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 }
644 }
645 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100646 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
David Brownella1d9e6e2007-05-01 23:26:30 +0200648 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 driver_unregister(&driver->driver);
650 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100651 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Arjan van de Venb3585e42006-01-11 10:50:26 +0100653 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
David Brownellc0564602007-05-01 23:26:31 +0200655EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
David Brownell7b4fbc52007-05-01 23:26:30 +0200657/* ------------------------------------------------------------------------- */
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
660{
661 struct list_head *item;
662 struct i2c_client *client;
663
664 list_for_each(item,&adapter->clients) {
665 client = list_entry(item, struct i2c_client, list);
666 if (client->addr == addr)
667 return -EBUSY;
668 }
669 return 0;
670}
671
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100672static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 int rval;
675
Ingo Molnar5c085d32006-01-18 23:16:04 +0100676 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100678 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 return rval;
681}
682
683int i2c_attach_client(struct i2c_client *client)
684{
685 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200686 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Ingo Molnar5c085d32006-01-18 23:16:04 +0100688 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200690 res = -EBUSY;
691 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200697
698 if (client->driver)
699 client->dev.driver = &client->driver->driver;
700
David Brownellde81d2a2007-05-22 19:49:16 +0200701 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200702 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200703 client->dev.uevent_suppress = 1;
704 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200705 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
708 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200709 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
710 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200711 res = device_register(&client->dev);
712 if (res)
713 goto out_list;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200714 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200715
716 if (adapter->client_register) {
717 if (adapter->client_register(client)) {
718 dev_dbg(&adapter->dev, "client_register "
719 "failed for client [%s] at 0x%02x\n",
720 client->name, client->addr);
721 }
722 }
723
724 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200725
Jean Delvareb119c6c2006-08-15 18:26:30 +0200726out_list:
727 list_del(&client->list);
728 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
729 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200730out_unlock:
731 mutex_unlock(&adapter->clist_lock);
732 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
David Brownellc0564602007-05-01 23:26:31 +0200734EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736int i2c_detach_client(struct i2c_client *client)
737{
738 struct i2c_adapter *adapter = client->adapter;
739 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (adapter->client_unregister) {
742 res = adapter->client_unregister(client);
743 if (res) {
744 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700745 "client_unregister [%s] failed, "
746 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto out;
748 }
749 }
750
Ingo Molnar5c085d32006-01-18 23:16:04 +0100751 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 list_del(&client->list);
753 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100755 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 wait_for_completion(&client->released);
757
758 out:
759 return res;
760}
David Brownellc0564602007-05-01 23:26:31 +0200761EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Jean Delvaree48d3312008-01-27 18:14:48 +0100763/**
764 * i2c_use_client - increments the reference count of the i2c client structure
765 * @client: the client being referenced
766 *
767 * Each live reference to a client should be refcounted. The driver model does
768 * that automatically as part of driver binding, so that most drivers don't
769 * need to do this explicitly: they hold a reference until they're unbound
770 * from the device.
771 *
772 * A pointer to the client with the incremented reference counter is returned.
773 */
774struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100776 get_device(&client->dev);
Jean Delvaree48d3312008-01-27 18:14:48 +0100777 return client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
David Brownellc0564602007-05-01 23:26:31 +0200779EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Jean Delvaree48d3312008-01-27 18:14:48 +0100781/**
782 * i2c_release_client - release a use of the i2c client structure
783 * @client: the client being no longer referenced
784 *
785 * Must be called when a user of a client is finished with it.
786 */
787void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100789 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
David Brownellc0564602007-05-01 23:26:31 +0200791EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
794{
795 struct list_head *item;
796 struct i2c_client *client;
797
Ingo Molnar5c085d32006-01-18 23:16:04 +0100798 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 list_for_each(item,&adap->clients) {
800 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100801 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 continue;
803 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100804 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100806 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100808 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100810 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
David Brownellc0564602007-05-01 23:26:31 +0200812EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814static int __init i2c_init(void)
815{
816 int retval;
817
818 retval = bus_register(&i2c_bus_type);
819 if (retval)
820 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return class_register(&i2c_adapter_class);
822}
823
824static void __exit i2c_exit(void)
825{
826 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 bus_unregister(&i2c_bus_type);
828}
829
830subsys_initcall(i2c_init);
831module_exit(i2c_exit);
832
833/* ----------------------------------------------------
834 * the functional interface to the i2c busses.
835 * ----------------------------------------------------
836 */
837
838int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
839{
840 int ret;
841
842 if (adap->algo->master_xfer) {
843#ifdef DEBUG
844 for (ret = 0; ret < num; ret++) {
845 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200846 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
847 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
848 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850#endif
851
Jiri Kosina6ea23032006-12-10 21:21:30 +0100852 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100854 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 return ret;
857 } else {
858 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
859 return -ENOSYS;
860 }
861}
David Brownellc0564602007-05-01 23:26:31 +0200862EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
865{
866 int ret;
867 struct i2c_adapter *adap=client->adapter;
868 struct i2c_msg msg;
869
Jean Delvare815f55f2005-05-07 22:58:46 +0200870 msg.addr = client->addr;
871 msg.flags = client->flags & I2C_M_TEN;
872 msg.len = count;
873 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100874
Jean Delvare815f55f2005-05-07 22:58:46 +0200875 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Jean Delvare815f55f2005-05-07 22:58:46 +0200877 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
878 transmitted, else error code. */
879 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
David Brownellc0564602007-05-01 23:26:31 +0200881EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
884{
885 struct i2c_adapter *adap=client->adapter;
886 struct i2c_msg msg;
887 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Jean Delvare815f55f2005-05-07 22:58:46 +0200889 msg.addr = client->addr;
890 msg.flags = client->flags & I2C_M_TEN;
891 msg.flags |= I2C_M_RD;
892 msg.len = count;
893 msg.buf = buf;
894
895 ret = i2c_transfer(adap, &msg, 1);
896
897 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
898 transmitted, else error code. */
899 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
David Brownellc0564602007-05-01 23:26:31 +0200901EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/* ----------------------------------------------------
904 * the i2c address scanning function
905 * Will not work for 10-bit addresses!
906 * ----------------------------------------------------
907 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200908static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
909 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200910{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200911 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200912
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200913 /* Make sure the address is valid */
914 if (addr < 0x03 || addr > 0x77) {
915 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
916 addr);
917 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200918 }
919
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200920 /* Skip if already in use */
921 if (i2c_check_addr(adapter, addr))
922 return 0;
923
924 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200925 if (kind < 0) {
926 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
927 I2C_SMBUS_QUICK, NULL) < 0)
928 return 0;
929
930 /* prevent 24RF08 corruption */
931 if ((addr & ~0x0f) == 0x50)
932 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
933 I2C_SMBUS_QUICK, NULL);
934 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200935
936 /* Finally call the custom detection function */
937 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200938 /* -ENODEV can be returned if there is a chip at the given address
939 but it isn't supported by this chip driver. We catch it here as
940 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200941 if (err == -ENODEV)
942 err = 0;
943
944 if (err)
945 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
946 addr, err);
947 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +0100951 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int (*found_proc) (struct i2c_adapter *, int, int))
953{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200954 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int adap_id = i2c_adapter_id(adapter);
956
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200957 /* Force entries are done first, and are not affected by ignore
958 entries */
959 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +0100960 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200961 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200963 for (kind = 0; forces[kind]; kind++) {
964 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
965 i += 2) {
966 if (forces[kind][i] == adap_id
967 || forces[kind][i] == ANY_I2C_BUS) {
968 dev_dbg(&adapter->dev, "found force "
969 "parameter for adapter %d, "
970 "addr 0x%02x, kind %d\n",
971 adap_id, forces[kind][i + 1],
972 kind);
973 err = i2c_probe_address(adapter,
974 forces[kind][i + 1],
975 kind, found_proc);
976 if (err)
977 return err;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200982
Jean Delvare4366dc92005-09-25 16:50:06 +0200983 /* Stop here if we can't use SMBUS_QUICK */
984 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
985 if (address_data->probe[0] == I2C_CLIENT_END
986 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100987 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200988
989 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
990 "can't probe for chips\n");
991 return -1;
992 }
993
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200994 /* Probe entries are done second, and are not affected by ignore
995 entries either */
996 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
997 if (address_data->probe[i] == adap_id
998 || address_data->probe[i] == ANY_I2C_BUS) {
999 dev_dbg(&adapter->dev, "found probe parameter for "
1000 "adapter %d, addr 0x%02x\n", adap_id,
1001 address_data->probe[i + 1]);
1002 err = i2c_probe_address(adapter,
1003 address_data->probe[i + 1],
1004 -1, found_proc);
1005 if (err)
1006 return err;
1007 }
1008 }
1009
1010 /* Normal entries are done last, unless shadowed by an ignore entry */
1011 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1012 int j, ignore;
1013
1014 ignore = 0;
1015 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1016 j += 2) {
1017 if ((address_data->ignore[j] == adap_id ||
1018 address_data->ignore[j] == ANY_I2C_BUS)
1019 && address_data->ignore[j + 1]
1020 == address_data->normal_i2c[i]) {
1021 dev_dbg(&adapter->dev, "found ignore "
1022 "parameter for adapter %d, "
1023 "addr 0x%02x\n", adap_id,
1024 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001025 ignore = 1;
1026 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001027 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001028 }
1029 if (ignore)
1030 continue;
1031
1032 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1033 "addr 0x%02x\n", adap_id,
1034 address_data->normal_i2c[i]);
1035 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1036 -1, found_proc);
1037 if (err)
1038 return err;
1039 }
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 0;
1042}
David Brownellc0564602007-05-01 23:26:31 +02001043EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Jean Delvare12b50532007-05-01 23:26:31 +02001045struct i2c_client *
1046i2c_new_probed_device(struct i2c_adapter *adap,
1047 struct i2c_board_info *info,
1048 unsigned short const *addr_list)
1049{
1050 int i;
1051
1052 /* Stop here if the bus doesn't support probing */
1053 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1054 dev_err(&adap->dev, "Probing not supported\n");
1055 return NULL;
1056 }
1057
1058 mutex_lock(&adap->clist_lock);
1059 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1060 /* Check address validity */
1061 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1062 dev_warn(&adap->dev, "Invalid 7-bit address "
1063 "0x%02x\n", addr_list[i]);
1064 continue;
1065 }
1066
1067 /* Check address availability */
1068 if (__i2c_check_addr(adap, addr_list[i])) {
1069 dev_dbg(&adap->dev, "Address 0x%02x already in "
1070 "use, not probing\n", addr_list[i]);
1071 continue;
1072 }
1073
1074 /* Test address responsiveness
1075 The default probe method is a quick write, but it is known
1076 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1077 and could also irreversibly write-protect some EEPROMs, so
1078 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1079 read instead. Also, some bus drivers don't implement
1080 quick write, so we fallback to a byte read it that case
1081 too. */
1082 if ((addr_list[i] & ~0x07) == 0x30
1083 || (addr_list[i] & ~0x0f) == 0x50
1084 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1085 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1086 I2C_SMBUS_READ, 0,
1087 I2C_SMBUS_BYTE, NULL) >= 0)
1088 break;
1089 } else {
1090 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1091 I2C_SMBUS_WRITE, 0,
1092 I2C_SMBUS_QUICK, NULL) >= 0)
1093 break;
1094 }
1095 }
1096 mutex_unlock(&adap->clist_lock);
1097
1098 if (addr_list[i] == I2C_CLIENT_END) {
1099 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1100 return NULL;
1101 }
1102
1103 info->addr = addr_list[i];
1104 return i2c_new_device(adap, info);
1105}
1106EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108struct i2c_adapter* i2c_get_adapter(int id)
1109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001111
Arjan van de Venb3585e42006-01-11 10:50:26 +01001112 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001113 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1114 if (adapter && !try_module_get(adapter->owner))
1115 adapter = NULL;
1116
Arjan van de Venb3585e42006-01-11 10:50:26 +01001117 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001118 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
David Brownellc0564602007-05-01 23:26:31 +02001120EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122void i2c_put_adapter(struct i2c_adapter *adap)
1123{
1124 module_put(adap->owner);
1125}
David Brownellc0564602007-05-01 23:26:31 +02001126EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128/* The SMBus parts */
1129
David Brownell438d6c22006-12-10 21:21:31 +01001130#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131static u8
1132crc8(u16 data)
1133{
1134 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001137 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 data = data ^ POLY;
1139 data = data << 1;
1140 }
1141 return (u8)(data >> 8);
1142}
1143
Jean Delvare421ef472005-10-26 21:28:55 +02001144/* Incremental CRC8 over count bytes in the array pointed to by p */
1145static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int i;
1148
1149 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001150 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return crc;
1152}
1153
Jean Delvare421ef472005-10-26 21:28:55 +02001154/* Assume a 7-bit address, which is reasonable for SMBus */
1155static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Jean Delvare421ef472005-10-26 21:28:55 +02001157 /* The address will be sent first */
1158 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1159 pec = i2c_smbus_pec(pec, &addr, 1);
1160
1161 /* The data buffer follows */
1162 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Jean Delvare421ef472005-10-26 21:28:55 +02001165/* Used for write only transactions */
1166static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Jean Delvare421ef472005-10-26 21:28:55 +02001168 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1169 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Jean Delvare421ef472005-10-26 21:28:55 +02001172/* Return <0 on CRC error
1173 If there was a write before this read (most cases) we need to take the
1174 partial CRC from the write part into account.
1175 Note that this function does modify the message (we need to decrease the
1176 message length to hide the CRC byte from the caller). */
1177static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Jean Delvare421ef472005-10-26 21:28:55 +02001179 u8 rpec = msg->buf[--msg->len];
1180 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (rpec != cpec) {
1183 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1184 rpec, cpec);
1185 return -1;
1186 }
David Brownell438d6c22006-12-10 21:21:31 +01001187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1191{
1192 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001193 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
David Brownellc0564602007-05-01 23:26:31 +02001195EXPORT_SYMBOL(i2c_smbus_write_quick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197s32 i2c_smbus_read_byte(struct i2c_client *client)
1198{
1199 union i2c_smbus_data data;
1200 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1201 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1202 return -1;
1203 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001204 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
David Brownellc0564602007-05-01 23:26:31 +02001206EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001211 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
David Brownellc0564602007-05-01 23:26:31 +02001213EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1216{
1217 union i2c_smbus_data data;
1218 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1219 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1220 return -1;
1221 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001222 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
David Brownellc0564602007-05-01 23:26:31 +02001224EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1227{
1228 union i2c_smbus_data data;
1229 data.byte = value;
1230 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1231 I2C_SMBUS_WRITE,command,
1232 I2C_SMBUS_BYTE_DATA,&data);
1233}
David Brownellc0564602007-05-01 23:26:31 +02001234EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1237{
1238 union i2c_smbus_data data;
1239 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1240 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1241 return -1;
1242 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001243 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
David Brownellc0564602007-05-01 23:26:31 +02001245EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1248{
1249 union i2c_smbus_data data;
1250 data.word = value;
1251 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1252 I2C_SMBUS_WRITE,command,
1253 I2C_SMBUS_WORD_DATA,&data);
1254}
David Brownellc0564602007-05-01 23:26:31 +02001255EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
David Brownella64ec072007-10-13 23:56:31 +02001257/**
1258 * i2c_smbus_read_block_data - SMBus block read request
1259 * @client: Handle to slave device
1260 * @command: Command byte issued to let the slave know what data should
1261 * be returned
1262 * @values: Byte array into which data will be read; big enough to hold
1263 * the data returned by the slave. SMBus allows at most 32 bytes.
1264 *
1265 * Returns the number of bytes read in the slave's response, else a
1266 * negative number to indicate some kind of error.
1267 *
1268 * Note that using this function requires that the client's adapter support
1269 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1270 * support this; its emulation through I2C messaging relies on a specific
1271 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1272 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001273s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1274 u8 *values)
1275{
1276 union i2c_smbus_data data;
1277
1278 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1279 I2C_SMBUS_READ, command,
1280 I2C_SMBUS_BLOCK_DATA, &data))
1281 return -1;
1282
1283 memcpy(values, &data.block[1], data.block[0]);
1284 return data.block[0];
1285}
1286EXPORT_SYMBOL(i2c_smbus_read_block_data);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001289 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (length > I2C_SMBUS_BLOCK_MAX)
1294 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001296 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1298 I2C_SMBUS_WRITE,command,
1299 I2C_SMBUS_BLOCK_DATA,&data);
1300}
David Brownellc0564602007-05-01 23:26:31 +02001301EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001304s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1305 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001308
Jean Delvare4b2643d2007-07-12 14:12:29 +02001309 if (length > I2C_SMBUS_BLOCK_MAX)
1310 length = I2C_SMBUS_BLOCK_MAX;
1311 data.block[0] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1313 I2C_SMBUS_READ,command,
1314 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1315 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001316
1317 memcpy(values, &data.block[1], data.block[0]);
1318 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
David Brownellc0564602007-05-01 23:26:31 +02001320EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jean Delvare21bbd692006-01-09 15:19:18 +11001322s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001323 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001324{
1325 union i2c_smbus_data data;
1326
1327 if (length > I2C_SMBUS_BLOCK_MAX)
1328 length = I2C_SMBUS_BLOCK_MAX;
1329 data.block[0] = length;
1330 memcpy(data.block + 1, values, length);
1331 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1332 I2C_SMBUS_WRITE, command,
1333 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1334}
David Brownellc0564602007-05-01 23:26:31 +02001335EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001336
David Brownell438d6c22006-12-10 21:21:31 +01001337/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001339static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001341 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 union i2c_smbus_data * data)
1343{
1344 /* So we need to generate a series of msgs. In the case of writing, we
1345 need to use only one message; when reading, we need two. We initialize
1346 most things with sane defaults, to keep the code below somewhat
1347 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001348 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1349 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001351 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1353 };
1354 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001355 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 msgbuf0[0] = command;
1358 switch(size) {
1359 case I2C_SMBUS_QUICK:
1360 msg[0].len = 0;
1361 /* Special case: The read/write field is used as data */
1362 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1363 num = 1;
1364 break;
1365 case I2C_SMBUS_BYTE:
1366 if (read_write == I2C_SMBUS_READ) {
1367 /* Special case: only a read! */
1368 msg[0].flags = I2C_M_RD | flags;
1369 num = 1;
1370 }
1371 break;
1372 case I2C_SMBUS_BYTE_DATA:
1373 if (read_write == I2C_SMBUS_READ)
1374 msg[1].len = 1;
1375 else {
1376 msg[0].len = 2;
1377 msgbuf0[1] = data->byte;
1378 }
1379 break;
1380 case I2C_SMBUS_WORD_DATA:
1381 if (read_write == I2C_SMBUS_READ)
1382 msg[1].len = 2;
1383 else {
1384 msg[0].len=3;
1385 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001386 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 break;
1389 case I2C_SMBUS_PROC_CALL:
1390 num = 2; /* Special case */
1391 read_write = I2C_SMBUS_READ;
1392 msg[0].len = 3;
1393 msg[1].len = 2;
1394 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001395 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
1397 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001399 msg[1].flags |= I2C_M_RECV_LEN;
1400 msg[1].len = 1; /* block length will be added by
1401 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 } else {
1403 msg[0].len = data->block[0] + 2;
1404 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1405 dev_err(&adapter->dev, "smbus_access called with "
1406 "invalid block write size (%d)\n",
1407 data->block[0]);
1408 return -1;
1409 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001410 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 msgbuf0[i] = data->block[i-1];
1412 }
1413 break;
1414 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001415 num = 2; /* Another special case */
1416 read_write = I2C_SMBUS_READ;
1417 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1418 dev_err(&adapter->dev, "%s called with invalid "
1419 "block proc call size (%d)\n", __FUNCTION__,
1420 data->block[0]);
1421 return -1;
1422 }
1423 msg[0].len = data->block[0] + 2;
1424 for (i = 1; i < msg[0].len; i++)
1425 msgbuf0[i] = data->block[i-1];
1426 msg[1].flags |= I2C_M_RECV_LEN;
1427 msg[1].len = 1; /* block length will be added by
1428 the underlying bus driver */
1429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 case I2C_SMBUS_I2C_BLOCK_DATA:
1431 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001432 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 } else {
1434 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001435 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1437 "invalid block write size (%d)\n",
1438 data->block[0]);
1439 return -1;
1440 }
1441 for (i = 1; i <= data->block[0]; i++)
1442 msgbuf0[i] = data->block[i];
1443 }
1444 break;
1445 default:
1446 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1447 size);
1448 return -1;
1449 }
1450
Jean Delvare421ef472005-10-26 21:28:55 +02001451 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1452 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1453 if (i) {
1454 /* Compute PEC if first message is a write */
1455 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001456 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001457 i2c_smbus_add_pec(&msg[0]);
1458 else /* Write followed by read */
1459 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1460 }
1461 /* Ask for PEC if last message is a read */
1462 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001463 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001464 }
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (i2c_transfer(adapter, msg, num) < 0)
1467 return -1;
1468
Jean Delvare421ef472005-10-26 21:28:55 +02001469 /* Check PEC if last message is a read */
1470 if (i && (msg[num-1].flags & I2C_M_RD)) {
1471 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1472 return -1;
1473 }
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (read_write == I2C_SMBUS_READ)
1476 switch(size) {
1477 case I2C_SMBUS_BYTE:
1478 data->byte = msgbuf0[0];
1479 break;
1480 case I2C_SMBUS_BYTE_DATA:
1481 data->byte = msgbuf1[0];
1482 break;
David Brownell438d6c22006-12-10 21:21:31 +01001483 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 case I2C_SMBUS_PROC_CALL:
1485 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1486 break;
1487 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001488 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 data->block[i+1] = msgbuf1[i];
1490 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001491 case I2C_SMBUS_BLOCK_DATA:
1492 case I2C_SMBUS_BLOCK_PROC_CALL:
1493 for (i = 0; i < msgbuf1[0] + 1; i++)
1494 data->block[i] = msgbuf1[i];
1495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 return 0;
1498}
1499
1500
1501s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001502 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 union i2c_smbus_data * data)
1504{
1505 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001510 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1512 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001513 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 } else
1515 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1516 command,size,data);
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return res;
1519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1523MODULE_DESCRIPTION("I2C-Bus main module");
1524MODULE_LICENSE("GPL");