blob: f489683fd15f04b61575a2b5704f54f225013e09 [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>
39
David Brownell9c1600e2007-05-01 23:26:31 +020040#include "i2c-core.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jean Delvarecaada322008-01-27 18:14:49 +010043static DEFINE_MUTEX(core_lock);
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
Jean Delvared2653e92008-04-29 23:11:39 +020050static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
51 const struct i2c_client *client)
52{
53 while (id->name[0]) {
54 if (strcmp(client->name, id->name) == 0)
55 return id;
56 id++;
57 }
58 return NULL;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int i2c_device_match(struct device *dev, struct device_driver *drv)
62{
David Brownell7b4fbc52007-05-01 23:26:30 +020063 struct i2c_client *client = to_i2c_client(dev);
64 struct i2c_driver *driver = to_i2c_driver(drv);
65
66 /* make legacy i2c drivers bypass driver model probing entirely;
67 * such drivers scan each i2c adapter/bus themselves.
68 */
David Brownella1d9e6e2007-05-01 23:26:30 +020069 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020070 return 0;
71
Jean Delvared2653e92008-04-29 23:11:39 +020072 /* match on an id table if there is one */
73 if (driver->id_table)
74 return i2c_match_id(driver->id_table, client) != NULL;
75
Jean Delvareeb8a7902008-05-18 20:49:41 +020076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
David Brownell7b4fbc52007-05-01 23:26:30 +020079#ifdef CONFIG_HOTPLUG
80
81/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020082static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020083{
84 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020085
86 /* by definition, legacy drivers can't hotplug */
Jean Delvared2653e92008-04-29 23:11:39 +020087 if (dev->driver)
David Brownell7b4fbc52007-05-01 23:26:30 +020088 return 0;
89
Jean Delvareeb8a7902008-05-18 20:49:41 +020090 if (add_uevent_var(env, "MODALIAS=%s%s",
91 I2C_MODULE_PREFIX, client->name))
92 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020093 dev_dbg(dev, "uevent\n");
94 return 0;
95}
96
97#else
98#define i2c_device_uevent NULL
99#endif /* CONFIG_HOTPLUG */
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int i2c_device_probe(struct device *dev)
102{
David Brownell7b4fbc52007-05-01 23:26:30 +0200103 struct i2c_client *client = to_i2c_client(dev);
104 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Jean Delvared2653e92008-04-29 23:11:39 +0200105 const struct i2c_device_id *id;
Hans Verkuil50c33042008-03-12 14:15:00 +0100106 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200107
108 if (!driver->probe)
109 return -ENODEV;
110 client->driver = driver;
111 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200112
113 if (driver->id_table)
114 id = i2c_match_id(driver->id_table, client);
115 else
116 id = NULL;
117 status = driver->probe(client, id);
Hans Verkuil50c33042008-03-12 14:15:00 +0100118 if (status)
119 client->driver = NULL;
120 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static int i2c_device_remove(struct device *dev)
124{
David Brownella1d9e6e2007-05-01 23:26:30 +0200125 struct i2c_client *client = to_i2c_client(dev);
126 struct i2c_driver *driver;
127 int status;
128
129 if (!dev->driver)
130 return 0;
131
132 driver = to_i2c_driver(dev->driver);
133 if (driver->remove) {
134 dev_dbg(dev, "remove\n");
135 status = driver->remove(client);
136 } else {
137 dev->driver = NULL;
138 status = 0;
139 }
140 if (status == 0)
141 client->driver = NULL;
142 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
David Brownellf37dd802007-02-13 22:09:00 +0100145static void i2c_device_shutdown(struct device *dev)
146{
147 struct i2c_driver *driver;
148
149 if (!dev->driver)
150 return;
151 driver = to_i2c_driver(dev->driver);
152 if (driver->shutdown)
153 driver->shutdown(to_i2c_client(dev));
154}
155
156static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
157{
158 struct i2c_driver *driver;
159
160 if (!dev->driver)
161 return 0;
162 driver = to_i2c_driver(dev->driver);
163 if (!driver->suspend)
164 return 0;
165 return driver->suspend(to_i2c_client(dev), mesg);
166}
167
168static int i2c_device_resume(struct device * dev)
169{
170 struct i2c_driver *driver;
171
172 if (!dev->driver)
173 return 0;
174 driver = to_i2c_driver(dev->driver);
175 if (!driver->resume)
176 return 0;
177 return driver->resume(to_i2c_client(dev));
178}
179
David Brownell7b4fbc52007-05-01 23:26:30 +0200180static void i2c_client_release(struct device *dev)
181{
182 struct i2c_client *client = to_i2c_client(dev);
183 complete(&client->released);
184}
185
David Brownell9c1600e2007-05-01 23:26:31 +0200186static void i2c_client_dev_release(struct device *dev)
187{
188 kfree(to_i2c_client(dev));
189}
190
David Brownell7b4fbc52007-05-01 23:26:30 +0200191static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
192{
193 struct i2c_client *client = to_i2c_client(dev);
194 return sprintf(buf, "%s\n", client->name);
195}
196
197static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
198{
199 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200200 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200201}
202
203static struct device_attribute i2c_dev_attrs[] = {
204 __ATTR(name, S_IRUGO, show_client_name, NULL),
205 /* modalias helps coldplug: modprobe $(cat .../modalias) */
206 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
207 { },
208};
209
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200210static struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100211 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200212 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100213 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200214 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100215 .probe = i2c_device_probe,
216 .remove = i2c_device_remove,
217 .shutdown = i2c_device_shutdown,
218 .suspend = i2c_device_suspend,
219 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000220};
221
David Brownell9b766b82008-01-27 18:14:51 +0100222
223/**
224 * i2c_verify_client - return parameter as i2c_client, or NULL
225 * @dev: device, probably from some driver model iterator
226 *
227 * When traversing the driver model tree, perhaps using driver model
228 * iterators like @device_for_each_child(), you can't assume very much
229 * about the nodes you find. Use this function to avoid oopses caused
230 * by wrongly treating some non-I2C device as an i2c_client.
231 */
232struct i2c_client *i2c_verify_client(struct device *dev)
233{
234 return (dev->bus == &i2c_bus_type)
235 ? to_i2c_client(dev)
236 : NULL;
237}
238EXPORT_SYMBOL(i2c_verify_client);
239
240
David Brownell9c1600e2007-05-01 23:26:31 +0200241/**
242 * i2c_new_device - instantiate an i2c device for use with a new style driver
243 * @adap: the adapter managing the device
244 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200245 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200246 *
247 * Create a device to work with a new style i2c driver, where binding is
248 * handled through driver model probe()/remove() methods. This call is not
249 * appropriate for use by mainboad initialization logic, which usually runs
250 * during an arch_initcall() long before any i2c_adapter could exist.
251 *
252 * This returns the new i2c client, which may be saved for later use with
253 * i2c_unregister_device(); or NULL to indicate an error.
254 */
255struct i2c_client *
256i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
257{
258 struct i2c_client *client;
259 int status;
260
261 client = kzalloc(sizeof *client, GFP_KERNEL);
262 if (!client)
263 return NULL;
264
265 client->adapter = adap;
266
267 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200268 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
269
270 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200271 client->addr = info->addr;
272 client->irq = info->irq;
273
David Brownell9c1600e2007-05-01 23:26:31 +0200274 strlcpy(client->name, info->type, sizeof(client->name));
275
276 /* a new style driver may be bound to this device when we
277 * return from this function, or any later moment (e.g. maybe
278 * hotplugging will load the driver module). and the device
279 * refcount model is the standard driver model one.
280 */
281 status = i2c_attach_client(client);
282 if (status < 0) {
283 kfree(client);
284 client = NULL;
285 }
286 return client;
287}
288EXPORT_SYMBOL_GPL(i2c_new_device);
289
290
291/**
292 * i2c_unregister_device - reverse effect of i2c_new_device()
293 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200294 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200295 */
296void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200297{
298 struct i2c_adapter *adapter = client->adapter;
299 struct i2c_driver *driver = client->driver;
300
301 if (driver && !is_newstyle_driver(driver)) {
302 dev_err(&client->dev, "can't unregister devices "
303 "with legacy drivers\n");
304 WARN_ON(1);
305 return;
306 }
307
308 mutex_lock(&adapter->clist_lock);
309 list_del(&client->list);
310 mutex_unlock(&adapter->clist_lock);
311
312 device_unregister(&client->dev);
313}
David Brownell9c1600e2007-05-01 23:26:31 +0200314EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200315
316
Jean Delvare60b129d2008-05-11 20:37:06 +0200317static const struct i2c_device_id dummy_id[] = {
318 { "dummy", 0 },
319 { },
320};
321
Jean Delvared2653e92008-04-29 23:11:39 +0200322static int dummy_probe(struct i2c_client *client,
323 const struct i2c_device_id *id)
324{
325 return 0;
326}
327
328static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100329{
330 return 0;
331}
332
333static struct i2c_driver dummy_driver = {
334 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200335 .probe = dummy_probe,
336 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200337 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100338};
339
340/**
341 * i2c_new_dummy - return a new i2c device bound to a dummy driver
342 * @adapter: the adapter managing the device
343 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100344 * Context: can sleep
345 *
346 * This returns an I2C client bound to the "dummy" driver, intended for use
347 * with devices that consume multiple addresses. Examples of such chips
348 * include various EEPROMS (like 24c04 and 24c08 models).
349 *
350 * These dummy devices have two main uses. First, most I2C and SMBus calls
351 * except i2c_transfer() need a client handle; the dummy will be that handle.
352 * And second, this prevents the specified address from being bound to a
353 * different driver.
354 *
355 * This returns the new i2c client, which should be saved for later use with
356 * i2c_unregister_device(); or NULL to indicate an error.
357 */
358struct i2c_client *
Jean Delvare60b129d2008-05-11 20:37:06 +0200359i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100360{
361 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200362 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100363 };
364
David Brownelle9f13732008-01-27 18:14:52 +0100365 return i2c_new_device(adapter, &info);
366}
367EXPORT_SYMBOL_GPL(i2c_new_dummy);
368
David Brownellf37dd802007-02-13 22:09:00 +0100369/* ------------------------------------------------------------------------- */
370
David Brownell16ffadf2007-05-01 23:26:28 +0200371/* I2C bus adapters -- one roots each I2C or SMBUS segment */
372
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200373static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
David Brownellef2c83212007-05-01 23:26:28 +0200375 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 complete(&adap->dev_released);
377}
378
David Brownell16ffadf2007-05-01 23:26:28 +0200379static ssize_t
380show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
David Brownellef2c83212007-05-01 23:26:28 +0200382 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return sprintf(buf, "%s\n", adap->name);
384}
David Brownell16ffadf2007-05-01 23:26:28 +0200385
386static struct device_attribute i2c_adapter_attrs[] = {
387 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
388 { },
389};
390
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200391static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200392 .owner = THIS_MODULE,
393 .name = "i2c-adapter",
394 .dev_attrs = i2c_adapter_attrs,
395};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Brownell9c1600e2007-05-01 23:26:31 +0200397static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
398{
399 struct i2c_devinfo *devinfo;
400
401 mutex_lock(&__i2c_board_lock);
402 list_for_each_entry(devinfo, &__i2c_board_list, list) {
403 if (devinfo->busnum == adapter->nr
404 && !i2c_new_device(adapter,
405 &devinfo->board_info))
406 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
407 i2c_adapter_id(adapter),
408 devinfo->board_info.addr);
409 }
410 mutex_unlock(&__i2c_board_lock);
411}
412
Jean Delvare026526f2008-01-27 18:14:49 +0100413static int i2c_do_add_adapter(struct device_driver *d, void *data)
414{
415 struct i2c_driver *driver = to_i2c_driver(d);
416 struct i2c_adapter *adap = data;
417
418 if (driver->attach_adapter) {
419 /* We ignore the return code; if it fails, too bad */
420 driver->attach_adapter(adap);
421 }
422 return 0;
423}
424
David Brownell6e13e642007-05-01 23:26:31 +0200425static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Jean Delvare026526f2008-01-27 18:14:49 +0100427 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Ingo Molnar5c085d32006-01-18 23:16:04 +0100429 mutex_init(&adap->bus_lock);
430 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 INIT_LIST_HEAD(&adap->clients);
432
Jean Delvarecaada322008-01-27 18:14:49 +0100433 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* Add the adapter to the driver core.
436 * If the parent pointer is not set up,
437 * we add this adapter to the host bus.
438 */
David Brownellb119dc32007-01-04 13:07:04 +0100439 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100441 pr_debug("I2C adapter driver [%s] forgot to specify "
442 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200446 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200447 res = device_register(&adap->dev);
448 if (res)
449 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200451 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
452
David Brownell6e13e642007-05-01 23:26:31 +0200453 /* create pre-declared device nodes for new-style drivers */
454 if (adap->nr < __i2c_first_dynamic_bus_num)
455 i2c_scan_static_board_info(adap);
456
David Brownell7b4fbc52007-05-01 23:26:30 +0200457 /* let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100458 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
459 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100462 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200464
Jean Delvareb119c6c2006-08-15 18:26:30 +0200465out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200466 idr_remove(&i2c_adapter_idr, adap->nr);
467 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
David Brownell6e13e642007-05-01 23:26:31 +0200470/**
471 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
472 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200473 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200474 *
475 * This routine is used to declare an I2C adapter when its bus number
476 * doesn't matter. Examples: for I2C adapters dynamically added by
477 * USB links or PCI plugin cards.
478 *
479 * When this returns zero, a new bus number was allocated and stored
480 * in adap->nr, and the specified adapter became available for clients.
481 * Otherwise, a negative errno value is returned.
482 */
483int i2c_add_adapter(struct i2c_adapter *adapter)
484{
485 int id, res = 0;
486
487retry:
488 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
489 return -ENOMEM;
490
Jean Delvarecaada322008-01-27 18:14:49 +0100491 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200492 /* "above" here means "above or equal to", sigh */
493 res = idr_get_new_above(&i2c_adapter_idr, adapter,
494 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100495 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200496
497 if (res < 0) {
498 if (res == -EAGAIN)
499 goto retry;
500 return res;
501 }
502
503 adapter->nr = id;
504 return i2c_register_adapter(adapter);
505}
506EXPORT_SYMBOL(i2c_add_adapter);
507
508/**
509 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
510 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200511 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200512 *
513 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100514 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
515 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200516 * is used to properly configure I2C devices.
517 *
518 * If no devices have pre-been declared for this bus, then be sure to
519 * register the adapter before any dynamically allocated ones. Otherwise
520 * the required bus ID may not be available.
521 *
522 * When this returns zero, the specified adapter became available for
523 * clients using the bus number provided in adap->nr. Also, the table
524 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
525 * and the appropriate driver model device nodes are created. Otherwise, a
526 * negative errno value is returned.
527 */
528int i2c_add_numbered_adapter(struct i2c_adapter *adap)
529{
530 int id;
531 int status;
532
533 if (adap->nr & ~MAX_ID_MASK)
534 return -EINVAL;
535
536retry:
537 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
538 return -ENOMEM;
539
Jean Delvarecaada322008-01-27 18:14:49 +0100540 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200541 /* "above" here means "above or equal to", sigh;
542 * we need the "equal to" result to force the result
543 */
544 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
545 if (status == 0 && id != adap->nr) {
546 status = -EBUSY;
547 idr_remove(&i2c_adapter_idr, id);
548 }
Jean Delvarecaada322008-01-27 18:14:49 +0100549 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200550 if (status == -EAGAIN)
551 goto retry;
552
553 if (status == 0)
554 status = i2c_register_adapter(adap);
555 return status;
556}
557EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
558
Jean Delvare026526f2008-01-27 18:14:49 +0100559static int i2c_do_del_adapter(struct device_driver *d, void *data)
560{
561 struct i2c_driver *driver = to_i2c_driver(d);
562 struct i2c_adapter *adapter = data;
563 int res;
564
565 if (!driver->detach_adapter)
566 return 0;
567 res = driver->detach_adapter(adapter);
568 if (res)
569 dev_err(&adapter->dev, "detach_adapter failed (%d) "
570 "for driver [%s]\n", res, driver->driver.name);
571 return res;
572}
573
David Brownelld64f73b2007-07-12 14:12:28 +0200574/**
575 * i2c_del_adapter - unregister I2C adapter
576 * @adap: the adapter being unregistered
577 * Context: can sleep
578 *
579 * This unregisters an I2C adapter which was previously registered
580 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582int i2c_del_adapter(struct i2c_adapter *adap)
583{
584 struct list_head *item, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct i2c_client *client;
586 int res = 0;
587
Jean Delvarecaada322008-01-27 18:14:49 +0100588 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100591 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200592 pr_debug("i2c-core: attempting to delete unregistered "
593 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 res = -EINVAL;
595 goto out_unlock;
596 }
597
Jean Delvare026526f2008-01-27 18:14:49 +0100598 /* Tell drivers about this removal */
599 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
600 i2c_do_del_adapter);
601 if (res)
602 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200605 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200607 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David Brownella1d9e6e2007-05-01 23:26:30 +0200609 client = list_entry(item, struct i2c_client, list);
610 driver = client->driver;
611
612 /* new style, follow standard driver model */
613 if (!driver || is_newstyle_driver(driver)) {
614 i2c_unregister_device(client);
615 continue;
616 }
617
618 /* legacy drivers create and remove clients themselves */
619 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200620 dev_err(&adap->dev, "detach_client failed for client "
621 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 client->addr);
623 goto out_unlock;
624 }
625 }
626
627 /* clean up the sysfs representation */
628 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /* wait for sysfs to drop all references */
632 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
David Brownell6e13e642007-05-01 23:26:31 +0200634 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 idr_remove(&i2c_adapter_idr, adap->nr);
636
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200637 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100640 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return res;
642}
David Brownellc0564602007-05-01 23:26:31 +0200643EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645
David Brownell7b4fbc52007-05-01 23:26:30 +0200646/* ------------------------------------------------------------------------- */
647
Dave Young7f101a92008-07-14 22:38:19 +0200648static int __attach_adapter(struct device *dev, void *data)
649{
650 struct i2c_adapter *adapter = to_i2c_adapter(dev);
651 struct i2c_driver *driver = data;
652
653 driver->attach_adapter(adapter);
654
655 return 0;
656}
657
David Brownell7b4fbc52007-05-01 23:26:30 +0200658/*
659 * An i2c_driver is used with one or more i2c_client (device) nodes to access
660 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
661 * are two models for binding the driver to its device: "new style" drivers
662 * follow the standard Linux driver model and just respond to probe() calls
663 * issued if the driver core sees they match(); "legacy" drivers create device
664 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
666
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800667int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100669 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
David Brownell7b4fbc52007-05-01 23:26:30 +0200671 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200672 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200673 if (driver->attach_adapter || driver->detach_adapter
674 || driver->detach_client) {
675 printk(KERN_WARNING
676 "i2c-core: driver [%s] is confused\n",
677 driver->driver.name);
678 return -EINVAL;
679 }
680 }
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800683 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
David Brownell6e13e642007-05-01 23:26:31 +0200686 /* for new style drivers, when registration returns the driver core
687 * will have called probe() for all matching-but-unbound devices.
688 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 res = driver_register(&driver->driver);
690 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100691 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100692
Jean Delvarecaada322008-01-27 18:14:49 +0100693 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100694
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100695 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
David Brownell7b4fbc52007-05-01 23:26:30 +0200697 /* legacy drivers scan i2c busses directly */
Dave Young7f101a92008-07-14 22:38:19 +0200698 if (driver->attach_adapter)
699 class_for_each_device(&i2c_adapter_class, driver,
700 __attach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Jean Delvarecaada322008-01-27 18:14:49 +0100702 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800705EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Dave Young7f101a92008-07-14 22:38:19 +0200707static int __detach_adapter(struct device *dev, void *data)
708{
709 struct i2c_adapter *adapter = to_i2c_adapter(dev);
710 struct i2c_driver *driver = data;
711
712 /* Have a look at each adapter, if clients of this driver are still
713 * attached. If so, detach them to be able to kill the driver
714 * afterwards.
715 */
716 if (driver->detach_adapter) {
717 if (driver->detach_adapter(adapter))
718 dev_err(&adapter->dev,
719 "detach_adapter failed for driver [%s]\n",
720 driver->driver.name);
721 } else {
722 struct list_head *item, *_n;
723 struct i2c_client *client;
724
725 list_for_each_safe(item, _n, &adapter->clients) {
726 client = list_entry(item, struct i2c_client, list);
727 if (client->driver != driver)
728 continue;
729 dev_dbg(&adapter->dev,
730 "detaching client [%s] at 0x%02x\n",
731 client->name, client->addr);
732 if (driver->detach_client(client))
733 dev_err(&adapter->dev, "detach_client "
734 "failed for client [%s] at 0x%02x\n",
735 client->name, client->addr);
736 }
737 }
738
739 return 0;
740}
741
David Brownella1d9e6e2007-05-01 23:26:30 +0200742/**
743 * i2c_del_driver - unregister I2C driver
744 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200745 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200746 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200747void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Jean Delvarecaada322008-01-27 18:14:49 +0100749 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
David Brownella1d9e6e2007-05-01 23:26:30 +0200751 /* new-style driver? */
752 if (is_newstyle_driver(driver))
753 goto unregister;
754
Dave Young7f101a92008-07-14 22:38:19 +0200755 class_for_each_device(&i2c_adapter_class, driver, __detach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
David Brownella1d9e6e2007-05-01 23:26:30 +0200757 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100759 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Jean Delvarecaada322008-01-27 18:14:49 +0100761 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
David Brownellc0564602007-05-01 23:26:31 +0200763EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David Brownell7b4fbc52007-05-01 23:26:30 +0200765/* ------------------------------------------------------------------------- */
766
David Brownell9b766b82008-01-27 18:14:51 +0100767static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
David Brownell9b766b82008-01-27 18:14:51 +0100769 struct i2c_client *client = i2c_verify_client(dev);
770 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
David Brownell9b766b82008-01-27 18:14:51 +0100772 if (client && client->addr == addr)
773 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return 0;
775}
776
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100777static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
David Brownell9b766b82008-01-27 18:14:51 +0100779 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782int i2c_attach_client(struct i2c_client *client)
783{
784 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200785 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200789
790 if (client->driver)
791 client->dev.driver = &client->driver->driver;
792
David Brownellde81d2a2007-05-22 19:49:16 +0200793 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200794 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200795 client->dev.uevent_suppress = 1;
796 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200797 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
800 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200801 res = device_register(&client->dev);
802 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100803 goto out_err;
804
805 mutex_lock(&adapter->clist_lock);
806 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200807 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200808
David Brownell86ec5ec2008-01-27 18:14:51 +0100809 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
810 client->name, client->dev.bus_id);
811
Jean Delvare77ed74d2006-09-30 17:18:59 +0200812 if (adapter->client_register) {
813 if (adapter->client_register(client)) {
814 dev_dbg(&adapter->dev, "client_register "
815 "failed for client [%s] at 0x%02x\n",
816 client->name, client->addr);
817 }
818 }
819
820 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200821
David Brownell86ec5ec2008-01-27 18:14:51 +0100822out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200823 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
824 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200825 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
David Brownellc0564602007-05-01 23:26:31 +0200827EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829int i2c_detach_client(struct i2c_client *client)
830{
831 struct i2c_adapter *adapter = client->adapter;
832 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (adapter->client_unregister) {
835 res = adapter->client_unregister(client);
836 if (res) {
837 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700838 "client_unregister [%s] failed, "
839 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto out;
841 }
842 }
843
Ingo Molnar5c085d32006-01-18 23:16:04 +0100844 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 list_del(&client->list);
Jean Delvare9ddced12008-01-27 18:14:51 +0100846 mutex_unlock(&adapter->clist_lock);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 device_unregister(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 wait_for_completion(&client->released);
851
852 out:
853 return res;
854}
David Brownellc0564602007-05-01 23:26:31 +0200855EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jean Delvaree48d3312008-01-27 18:14:48 +0100857/**
858 * i2c_use_client - increments the reference count of the i2c client structure
859 * @client: the client being referenced
860 *
861 * Each live reference to a client should be refcounted. The driver model does
862 * that automatically as part of driver binding, so that most drivers don't
863 * need to do this explicitly: they hold a reference until they're unbound
864 * from the device.
865 *
866 * A pointer to the client with the incremented reference counter is returned.
867 */
868struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100870 get_device(&client->dev);
Jean Delvaree48d3312008-01-27 18:14:48 +0100871 return client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
David Brownellc0564602007-05-01 23:26:31 +0200873EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Jean Delvaree48d3312008-01-27 18:14:48 +0100875/**
876 * i2c_release_client - release a use of the i2c client structure
877 * @client: the client being no longer referenced
878 *
879 * Must be called when a user of a client is finished with it.
880 */
881void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100883 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
David Brownellc0564602007-05-01 23:26:31 +0200885EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
David Brownell9b766b82008-01-27 18:14:51 +0100887struct i2c_cmd_arg {
888 unsigned cmd;
889 void *arg;
890};
891
892static int i2c_cmd(struct device *dev, void *_arg)
893{
894 struct i2c_client *client = i2c_verify_client(dev);
895 struct i2c_cmd_arg *arg = _arg;
896
897 if (client && client->driver && client->driver->command)
898 client->driver->command(client, arg->cmd, arg->arg);
899 return 0;
900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
903{
David Brownell9b766b82008-01-27 18:14:51 +0100904 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
David Brownell9b766b82008-01-27 18:14:51 +0100906 cmd_arg.cmd = cmd;
907 cmd_arg.arg = arg;
908 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
David Brownellc0564602007-05-01 23:26:31 +0200910EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912static int __init i2c_init(void)
913{
914 int retval;
915
916 retval = bus_register(&i2c_bus_type);
917 if (retval)
918 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100919 retval = class_register(&i2c_adapter_class);
920 if (retval)
921 goto bus_err;
922 retval = i2c_add_driver(&dummy_driver);
923 if (retval)
924 goto class_err;
925 return 0;
926
927class_err:
928 class_unregister(&i2c_adapter_class);
929bus_err:
930 bus_unregister(&i2c_bus_type);
931 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934static void __exit i2c_exit(void)
935{
David Brownelle9f13732008-01-27 18:14:52 +0100936 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 bus_unregister(&i2c_bus_type);
939}
940
941subsys_initcall(i2c_init);
942module_exit(i2c_exit);
943
944/* ----------------------------------------------------
945 * the functional interface to the i2c busses.
946 * ----------------------------------------------------
947 */
948
949int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
950{
951 int ret;
952
953 if (adap->algo->master_xfer) {
954#ifdef DEBUG
955 for (ret = 0; ret < num; ret++) {
956 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200957 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
958 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
959 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961#endif
962
Mike Rapoportcea443a82008-01-27 18:14:50 +0100963 if (in_atomic() || irqs_disabled()) {
964 ret = mutex_trylock(&adap->bus_lock);
965 if (!ret)
966 /* I2C activity is ongoing. */
967 return -EAGAIN;
968 } else {
969 mutex_lock_nested(&adap->bus_lock, adap->level);
970 }
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100973 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 return ret;
976 } else {
977 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
978 return -ENOSYS;
979 }
980}
David Brownellc0564602007-05-01 23:26:31 +0200981EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
984{
985 int ret;
986 struct i2c_adapter *adap=client->adapter;
987 struct i2c_msg msg;
988
Jean Delvare815f55f2005-05-07 22:58:46 +0200989 msg.addr = client->addr;
990 msg.flags = client->flags & I2C_M_TEN;
991 msg.len = count;
992 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100993
Jean Delvare815f55f2005-05-07 22:58:46 +0200994 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Jean Delvare815f55f2005-05-07 22:58:46 +0200996 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
997 transmitted, else error code. */
998 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
David Brownellc0564602007-05-01 23:26:31 +02001000EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1003{
1004 struct i2c_adapter *adap=client->adapter;
1005 struct i2c_msg msg;
1006 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Jean Delvare815f55f2005-05-07 22:58:46 +02001008 msg.addr = client->addr;
1009 msg.flags = client->flags & I2C_M_TEN;
1010 msg.flags |= I2C_M_RD;
1011 msg.len = count;
1012 msg.buf = buf;
1013
1014 ret = i2c_transfer(adap, &msg, 1);
1015
1016 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1017 transmitted, else error code. */
1018 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
David Brownellc0564602007-05-01 23:26:31 +02001020EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/* ----------------------------------------------------
1023 * the i2c address scanning function
1024 * Will not work for 10-bit addresses!
1025 * ----------------------------------------------------
1026 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001027static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1028 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001029{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001030 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001031
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001032 /* Make sure the address is valid */
1033 if (addr < 0x03 || addr > 0x77) {
1034 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1035 addr);
1036 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001037 }
1038
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001039 /* Skip if already in use */
1040 if (i2c_check_addr(adapter, addr))
1041 return 0;
1042
1043 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +02001044 if (kind < 0) {
1045 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1046 I2C_SMBUS_QUICK, NULL) < 0)
1047 return 0;
1048
1049 /* prevent 24RF08 corruption */
1050 if ((addr & ~0x0f) == 0x50)
1051 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1052 I2C_SMBUS_QUICK, NULL);
1053 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001054
1055 /* Finally call the custom detection function */
1056 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001057 /* -ENODEV can be returned if there is a chip at the given address
1058 but it isn't supported by this chip driver. We catch it here as
1059 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +02001060 if (err == -ENODEV)
1061 err = 0;
1062
1063 if (err)
1064 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1065 addr, err);
1066 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001067}
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001070 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 int (*found_proc) (struct i2c_adapter *, int, int))
1072{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001073 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 int adap_id = i2c_adapter_id(adapter);
1075
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001076 /* Force entries are done first, and are not affected by ignore
1077 entries */
1078 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001079 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001080 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001082 for (kind = 0; forces[kind]; kind++) {
1083 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1084 i += 2) {
1085 if (forces[kind][i] == adap_id
1086 || forces[kind][i] == ANY_I2C_BUS) {
1087 dev_dbg(&adapter->dev, "found force "
1088 "parameter for adapter %d, "
1089 "addr 0x%02x, kind %d\n",
1090 adap_id, forces[kind][i + 1],
1091 kind);
1092 err = i2c_probe_address(adapter,
1093 forces[kind][i + 1],
1094 kind, found_proc);
1095 if (err)
1096 return err;
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001101
Jean Delvare4366dc92005-09-25 16:50:06 +02001102 /* Stop here if we can't use SMBUS_QUICK */
1103 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1104 if (address_data->probe[0] == I2C_CLIENT_END
1105 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001106 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001107
1108 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1109 "can't probe for chips\n");
1110 return -1;
1111 }
1112
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001113 /* Probe entries are done second, and are not affected by ignore
1114 entries either */
1115 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1116 if (address_data->probe[i] == adap_id
1117 || address_data->probe[i] == ANY_I2C_BUS) {
1118 dev_dbg(&adapter->dev, "found probe parameter for "
1119 "adapter %d, addr 0x%02x\n", adap_id,
1120 address_data->probe[i + 1]);
1121 err = i2c_probe_address(adapter,
1122 address_data->probe[i + 1],
1123 -1, found_proc);
1124 if (err)
1125 return err;
1126 }
1127 }
1128
1129 /* Normal entries are done last, unless shadowed by an ignore entry */
1130 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1131 int j, ignore;
1132
1133 ignore = 0;
1134 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1135 j += 2) {
1136 if ((address_data->ignore[j] == adap_id ||
1137 address_data->ignore[j] == ANY_I2C_BUS)
1138 && address_data->ignore[j + 1]
1139 == address_data->normal_i2c[i]) {
1140 dev_dbg(&adapter->dev, "found ignore "
1141 "parameter for adapter %d, "
1142 "addr 0x%02x\n", adap_id,
1143 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001144 ignore = 1;
1145 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001146 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001147 }
1148 if (ignore)
1149 continue;
1150
1151 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1152 "addr 0x%02x\n", adap_id,
1153 address_data->normal_i2c[i]);
1154 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1155 -1, found_proc);
1156 if (err)
1157 return err;
1158 }
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 0;
1161}
David Brownellc0564602007-05-01 23:26:31 +02001162EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jean Delvare12b5053a2007-05-01 23:26:31 +02001164struct i2c_client *
1165i2c_new_probed_device(struct i2c_adapter *adap,
1166 struct i2c_board_info *info,
1167 unsigned short const *addr_list)
1168{
1169 int i;
1170
1171 /* Stop here if the bus doesn't support probing */
1172 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1173 dev_err(&adap->dev, "Probing not supported\n");
1174 return NULL;
1175 }
1176
Jean Delvare12b5053a2007-05-01 23:26:31 +02001177 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1178 /* Check address validity */
1179 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1180 dev_warn(&adap->dev, "Invalid 7-bit address "
1181 "0x%02x\n", addr_list[i]);
1182 continue;
1183 }
1184
1185 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001186 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001187 dev_dbg(&adap->dev, "Address 0x%02x already in "
1188 "use, not probing\n", addr_list[i]);
1189 continue;
1190 }
1191
1192 /* Test address responsiveness
1193 The default probe method is a quick write, but it is known
1194 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1195 and could also irreversibly write-protect some EEPROMs, so
1196 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1197 read instead. Also, some bus drivers don't implement
1198 quick write, so we fallback to a byte read it that case
1199 too. */
1200 if ((addr_list[i] & ~0x07) == 0x30
1201 || (addr_list[i] & ~0x0f) == 0x50
1202 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1203 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1204 I2C_SMBUS_READ, 0,
1205 I2C_SMBUS_BYTE, NULL) >= 0)
1206 break;
1207 } else {
1208 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1209 I2C_SMBUS_WRITE, 0,
1210 I2C_SMBUS_QUICK, NULL) >= 0)
1211 break;
1212 }
1213 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001214
1215 if (addr_list[i] == I2C_CLIENT_END) {
1216 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1217 return NULL;
1218 }
1219
1220 info->addr = addr_list[i];
1221 return i2c_new_device(adap, info);
1222}
1223EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225struct i2c_adapter* i2c_get_adapter(int id)
1226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001228
Jean Delvarecaada322008-01-27 18:14:49 +01001229 mutex_lock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04001230 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1231 if (adapter && !try_module_get(adapter->owner))
1232 adapter = NULL;
1233
Jean Delvarecaada322008-01-27 18:14:49 +01001234 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04001235 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
David Brownellc0564602007-05-01 23:26:31 +02001237EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239void i2c_put_adapter(struct i2c_adapter *adap)
1240{
1241 module_put(adap->owner);
1242}
David Brownellc0564602007-05-01 23:26:31 +02001243EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245/* The SMBus parts */
1246
David Brownell438d6c22006-12-10 21:21:31 +01001247#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static u8
1249crc8(u16 data)
1250{
1251 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001254 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 data = data ^ POLY;
1256 data = data << 1;
1257 }
1258 return (u8)(data >> 8);
1259}
1260
Jean Delvare421ef472005-10-26 21:28:55 +02001261/* Incremental CRC8 over count bytes in the array pointed to by p */
1262static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 int i;
1265
1266 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001267 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return crc;
1269}
1270
Jean Delvare421ef472005-10-26 21:28:55 +02001271/* Assume a 7-bit address, which is reasonable for SMBus */
1272static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Jean Delvare421ef472005-10-26 21:28:55 +02001274 /* The address will be sent first */
1275 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1276 pec = i2c_smbus_pec(pec, &addr, 1);
1277
1278 /* The data buffer follows */
1279 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Jean Delvare421ef472005-10-26 21:28:55 +02001282/* Used for write only transactions */
1283static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Jean Delvare421ef472005-10-26 21:28:55 +02001285 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1286 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Jean Delvare421ef472005-10-26 21:28:55 +02001289/* Return <0 on CRC error
1290 If there was a write before this read (most cases) we need to take the
1291 partial CRC from the write part into account.
1292 Note that this function does modify the message (we need to decrease the
1293 message length to hide the CRC byte from the caller). */
1294static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Jean Delvare421ef472005-10-26 21:28:55 +02001296 u8 rpec = msg->buf[--msg->len];
1297 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (rpec != cpec) {
1300 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1301 rpec, cpec);
1302 return -1;
1303 }
David Brownell438d6c22006-12-10 21:21:31 +01001304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1308{
1309 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001310 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
David Brownellc0564602007-05-01 23:26:31 +02001312EXPORT_SYMBOL(i2c_smbus_write_quick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314s32 i2c_smbus_read_byte(struct i2c_client *client)
1315{
1316 union i2c_smbus_data data;
1317 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1318 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1319 return -1;
1320 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001321 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
David Brownellc0564602007-05-01 23:26:31 +02001323EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001328 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
David Brownellc0564602007-05-01 23:26:31 +02001330EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1333{
1334 union i2c_smbus_data data;
1335 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1336 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1337 return -1;
1338 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001339 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
David Brownellc0564602007-05-01 23:26:31 +02001341EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1344{
1345 union i2c_smbus_data data;
1346 data.byte = value;
1347 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1348 I2C_SMBUS_WRITE,command,
1349 I2C_SMBUS_BYTE_DATA,&data);
1350}
David Brownellc0564602007-05-01 23:26:31 +02001351EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1354{
1355 union i2c_smbus_data data;
1356 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1357 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1358 return -1;
1359 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001360 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
David Brownellc0564602007-05-01 23:26:31 +02001362EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1365{
1366 union i2c_smbus_data data;
1367 data.word = value;
1368 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1369 I2C_SMBUS_WRITE,command,
1370 I2C_SMBUS_WORD_DATA,&data);
1371}
David Brownellc0564602007-05-01 23:26:31 +02001372EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
David Brownella64ec072007-10-13 23:56:31 +02001374/**
1375 * i2c_smbus_read_block_data - SMBus block read request
1376 * @client: Handle to slave device
1377 * @command: Command byte issued to let the slave know what data should
1378 * be returned
1379 * @values: Byte array into which data will be read; big enough to hold
1380 * the data returned by the slave. SMBus allows at most 32 bytes.
1381 *
1382 * Returns the number of bytes read in the slave's response, else a
1383 * negative number to indicate some kind of error.
1384 *
1385 * Note that using this function requires that the client's adapter support
1386 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1387 * support this; its emulation through I2C messaging relies on a specific
1388 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1389 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001390s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1391 u8 *values)
1392{
1393 union i2c_smbus_data data;
1394
1395 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1396 I2C_SMBUS_READ, command,
1397 I2C_SMBUS_BLOCK_DATA, &data))
1398 return -1;
1399
1400 memcpy(values, &data.block[1], data.block[0]);
1401 return data.block[0];
1402}
1403EXPORT_SYMBOL(i2c_smbus_read_block_data);
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001406 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (length > I2C_SMBUS_BLOCK_MAX)
1411 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001413 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1415 I2C_SMBUS_WRITE,command,
1416 I2C_SMBUS_BLOCK_DATA,&data);
1417}
David Brownellc0564602007-05-01 23:26:31 +02001418EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001421s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1422 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001425
Jean Delvare4b2643d2007-07-12 14:12:29 +02001426 if (length > I2C_SMBUS_BLOCK_MAX)
1427 length = I2C_SMBUS_BLOCK_MAX;
1428 data.block[0] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1430 I2C_SMBUS_READ,command,
1431 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1432 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001433
1434 memcpy(values, &data.block[1], data.block[0]);
1435 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
David Brownellc0564602007-05-01 23:26:31 +02001437EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Jean Delvare21bbd692006-01-09 15:19:18 +11001439s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001440 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001441{
1442 union i2c_smbus_data data;
1443
1444 if (length > I2C_SMBUS_BLOCK_MAX)
1445 length = I2C_SMBUS_BLOCK_MAX;
1446 data.block[0] = length;
1447 memcpy(data.block + 1, values, length);
1448 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1449 I2C_SMBUS_WRITE, command,
1450 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1451}
David Brownellc0564602007-05-01 23:26:31 +02001452EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001453
David Brownell438d6c22006-12-10 21:21:31 +01001454/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001456static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001458 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 union i2c_smbus_data * data)
1460{
1461 /* So we need to generate a series of msgs. In the case of writing, we
1462 need to use only one message; when reading, we need two. We initialize
1463 most things with sane defaults, to keep the code below somewhat
1464 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001465 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1466 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001468 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1470 };
1471 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001472 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 msgbuf0[0] = command;
1475 switch(size) {
1476 case I2C_SMBUS_QUICK:
1477 msg[0].len = 0;
1478 /* Special case: The read/write field is used as data */
1479 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1480 num = 1;
1481 break;
1482 case I2C_SMBUS_BYTE:
1483 if (read_write == I2C_SMBUS_READ) {
1484 /* Special case: only a read! */
1485 msg[0].flags = I2C_M_RD | flags;
1486 num = 1;
1487 }
1488 break;
1489 case I2C_SMBUS_BYTE_DATA:
1490 if (read_write == I2C_SMBUS_READ)
1491 msg[1].len = 1;
1492 else {
1493 msg[0].len = 2;
1494 msgbuf0[1] = data->byte;
1495 }
1496 break;
1497 case I2C_SMBUS_WORD_DATA:
1498 if (read_write == I2C_SMBUS_READ)
1499 msg[1].len = 2;
1500 else {
1501 msg[0].len=3;
1502 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001503 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505 break;
1506 case I2C_SMBUS_PROC_CALL:
1507 num = 2; /* Special case */
1508 read_write = I2C_SMBUS_READ;
1509 msg[0].len = 3;
1510 msg[1].len = 2;
1511 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001512 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 break;
1514 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001516 msg[1].flags |= I2C_M_RECV_LEN;
1517 msg[1].len = 1; /* block length will be added by
1518 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 } else {
1520 msg[0].len = data->block[0] + 2;
1521 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1522 dev_err(&adapter->dev, "smbus_access called with "
1523 "invalid block write size (%d)\n",
1524 data->block[0]);
1525 return -1;
1526 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001527 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 msgbuf0[i] = data->block[i-1];
1529 }
1530 break;
1531 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001532 num = 2; /* Another special case */
1533 read_write = I2C_SMBUS_READ;
1534 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1535 dev_err(&adapter->dev, "%s called with invalid "
Harvey Harrison08882d22008-04-22 22:16:47 +02001536 "block proc call size (%d)\n", __func__,
Jean Delvare209d27c2007-05-01 23:26:29 +02001537 data->block[0]);
1538 return -1;
1539 }
1540 msg[0].len = data->block[0] + 2;
1541 for (i = 1; i < msg[0].len; i++)
1542 msgbuf0[i] = data->block[i-1];
1543 msg[1].flags |= I2C_M_RECV_LEN;
1544 msg[1].len = 1; /* block length will be added by
1545 the underlying bus driver */
1546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 case I2C_SMBUS_I2C_BLOCK_DATA:
1548 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001549 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 } else {
1551 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001552 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1554 "invalid block write size (%d)\n",
1555 data->block[0]);
1556 return -1;
1557 }
1558 for (i = 1; i <= data->block[0]; i++)
1559 msgbuf0[i] = data->block[i];
1560 }
1561 break;
1562 default:
1563 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1564 size);
1565 return -1;
1566 }
1567
Jean Delvare421ef472005-10-26 21:28:55 +02001568 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1569 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1570 if (i) {
1571 /* Compute PEC if first message is a write */
1572 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001573 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001574 i2c_smbus_add_pec(&msg[0]);
1575 else /* Write followed by read */
1576 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1577 }
1578 /* Ask for PEC if last message is a read */
1579 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001580 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001581 }
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (i2c_transfer(adapter, msg, num) < 0)
1584 return -1;
1585
Jean Delvare421ef472005-10-26 21:28:55 +02001586 /* Check PEC if last message is a read */
1587 if (i && (msg[num-1].flags & I2C_M_RD)) {
1588 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1589 return -1;
1590 }
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (read_write == I2C_SMBUS_READ)
1593 switch(size) {
1594 case I2C_SMBUS_BYTE:
1595 data->byte = msgbuf0[0];
1596 break;
1597 case I2C_SMBUS_BYTE_DATA:
1598 data->byte = msgbuf1[0];
1599 break;
David Brownell438d6c22006-12-10 21:21:31 +01001600 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 case I2C_SMBUS_PROC_CALL:
1602 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1603 break;
1604 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001605 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 data->block[i+1] = msgbuf1[i];
1607 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001608 case I2C_SMBUS_BLOCK_DATA:
1609 case I2C_SMBUS_BLOCK_PROC_CALL:
1610 for (i = 0; i < msgbuf1[0] + 1; i++)
1611 data->block[i] = msgbuf1[i];
1612 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 return 0;
1615}
1616
1617
1618s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001619 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 union i2c_smbus_data * data)
1621{
1622 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001627 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1629 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001630 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 } else
1632 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1633 command,size,data);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return res;
1636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1640MODULE_DESCRIPTION("I2C-Bus main module");
1641MODULE_LICENSE("GPL");