blob: 96da22e9a5a44dbe32b247694a2b72bdea20c0d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
32#include <linux/seq_file.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010034#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010035#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010036#include <linux/hardirq.h>
37#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
Jean Delvare87c6c222008-01-27 18:14:48 +010039#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Brownell9c1600e2007-05-01 23:26:31 +020041#include "i2c-core.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jean Delvarecaada322008-01-27 18:14:49 +010044static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static DEFINE_IDR(i2c_adapter_idr);
46
David Brownella1d9e6e2007-05-01 23:26:30 +020047#define is_newstyle_driver(d) ((d)->probe || (d)->remove)
David Brownellf37dd802007-02-13 22:09:00 +010048
49/* ------------------------------------------------------------------------- */
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int i2c_device_match(struct device *dev, struct device_driver *drv)
52{
David Brownell7b4fbc52007-05-01 23:26:30 +020053 struct i2c_client *client = to_i2c_client(dev);
54 struct i2c_driver *driver = to_i2c_driver(drv);
55
56 /* make legacy i2c drivers bypass driver model probing entirely;
57 * such drivers scan each i2c adapter/bus themselves.
58 */
David Brownella1d9e6e2007-05-01 23:26:30 +020059 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020060 return 0;
61
62 /* new style drivers use the same kind of driver matching policy
63 * as platform devices or SPI: compare device and driver IDs.
64 */
65 return strcmp(client->driver_name, drv->name) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
David Brownell7b4fbc52007-05-01 23:26:30 +020068#ifdef CONFIG_HOTPLUG
69
70/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020071static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020072{
73 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020074
75 /* by definition, legacy drivers can't hotplug */
76 if (dev->driver || !client->driver_name)
77 return 0;
78
Kay Sievers7eff2e72007-08-14 15:15:12 +020079 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
David Brownell7b4fbc52007-05-01 23:26:30 +020080 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020081 dev_dbg(dev, "uevent\n");
82 return 0;
83}
84
85#else
86#define i2c_device_uevent NULL
87#endif /* CONFIG_HOTPLUG */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static int i2c_device_probe(struct device *dev)
90{
David Brownell7b4fbc52007-05-01 23:26:30 +020091 struct i2c_client *client = to_i2c_client(dev);
92 struct i2c_driver *driver = to_i2c_driver(dev->driver);
93
94 if (!driver->probe)
95 return -ENODEV;
96 client->driver = driver;
97 dev_dbg(dev, "probe\n");
98 return driver->probe(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static int i2c_device_remove(struct device *dev)
102{
David Brownella1d9e6e2007-05-01 23:26:30 +0200103 struct i2c_client *client = to_i2c_client(dev);
104 struct i2c_driver *driver;
105 int status;
106
107 if (!dev->driver)
108 return 0;
109
110 driver = to_i2c_driver(dev->driver);
111 if (driver->remove) {
112 dev_dbg(dev, "remove\n");
113 status = driver->remove(client);
114 } else {
115 dev->driver = NULL;
116 status = 0;
117 }
118 if (status == 0)
119 client->driver = NULL;
120 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
David Brownellf37dd802007-02-13 22:09:00 +0100123static void i2c_device_shutdown(struct device *dev)
124{
125 struct i2c_driver *driver;
126
127 if (!dev->driver)
128 return;
129 driver = to_i2c_driver(dev->driver);
130 if (driver->shutdown)
131 driver->shutdown(to_i2c_client(dev));
132}
133
134static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
135{
136 struct i2c_driver *driver;
137
138 if (!dev->driver)
139 return 0;
140 driver = to_i2c_driver(dev->driver);
141 if (!driver->suspend)
142 return 0;
143 return driver->suspend(to_i2c_client(dev), mesg);
144}
145
146static int i2c_device_resume(struct device * dev)
147{
148 struct i2c_driver *driver;
149
150 if (!dev->driver)
151 return 0;
152 driver = to_i2c_driver(dev->driver);
153 if (!driver->resume)
154 return 0;
155 return driver->resume(to_i2c_client(dev));
156}
157
David Brownell7b4fbc52007-05-01 23:26:30 +0200158static void i2c_client_release(struct device *dev)
159{
160 struct i2c_client *client = to_i2c_client(dev);
161 complete(&client->released);
162}
163
David Brownell9c1600e2007-05-01 23:26:31 +0200164static void i2c_client_dev_release(struct device *dev)
165{
166 kfree(to_i2c_client(dev));
167}
168
David Brownell7b4fbc52007-05-01 23:26:30 +0200169static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
170{
171 struct i2c_client *client = to_i2c_client(dev);
172 return sprintf(buf, "%s\n", client->name);
173}
174
175static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
176{
177 struct i2c_client *client = to_i2c_client(dev);
178 return client->driver_name
179 ? sprintf(buf, "%s\n", client->driver_name)
180 : 0;
181}
182
183static struct device_attribute i2c_dev_attrs[] = {
184 __ATTR(name, S_IRUGO, show_client_name, NULL),
185 /* modalias helps coldplug: modprobe $(cat .../modalias) */
186 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
187 { },
188};
189
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200190static struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100191 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200192 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100193 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200194 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100195 .probe = i2c_device_probe,
196 .remove = i2c_device_remove,
197 .shutdown = i2c_device_shutdown,
198 .suspend = i2c_device_suspend,
199 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000200};
201
David Brownell9b766b82008-01-27 18:14:51 +0100202
203/**
204 * i2c_verify_client - return parameter as i2c_client, or NULL
205 * @dev: device, probably from some driver model iterator
206 *
207 * When traversing the driver model tree, perhaps using driver model
208 * iterators like @device_for_each_child(), you can't assume very much
209 * about the nodes you find. Use this function to avoid oopses caused
210 * by wrongly treating some non-I2C device as an i2c_client.
211 */
212struct i2c_client *i2c_verify_client(struct device *dev)
213{
214 return (dev->bus == &i2c_bus_type)
215 ? to_i2c_client(dev)
216 : NULL;
217}
218EXPORT_SYMBOL(i2c_verify_client);
219
220
David Brownell9c1600e2007-05-01 23:26:31 +0200221/**
222 * i2c_new_device - instantiate an i2c device for use with a new style driver
223 * @adap: the adapter managing the device
224 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200225 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200226 *
227 * Create a device to work with a new style i2c driver, where binding is
228 * handled through driver model probe()/remove() methods. This call is not
229 * appropriate for use by mainboad initialization logic, which usually runs
230 * during an arch_initcall() long before any i2c_adapter could exist.
231 *
232 * This returns the new i2c client, which may be saved for later use with
233 * i2c_unregister_device(); or NULL to indicate an error.
234 */
235struct i2c_client *
236i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
237{
238 struct i2c_client *client;
239 int status;
240
241 client = kzalloc(sizeof *client, GFP_KERNEL);
242 if (!client)
243 return NULL;
244
245 client->adapter = adap;
246
247 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200248 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
249
250 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200251 client->addr = info->addr;
252 client->irq = info->irq;
253
254 strlcpy(client->driver_name, info->driver_name,
255 sizeof(client->driver_name));
256 strlcpy(client->name, info->type, sizeof(client->name));
257
258 /* a new style driver may be bound to this device when we
259 * return from this function, or any later moment (e.g. maybe
260 * hotplugging will load the driver module). and the device
261 * refcount model is the standard driver model one.
262 */
263 status = i2c_attach_client(client);
264 if (status < 0) {
265 kfree(client);
266 client = NULL;
267 }
268 return client;
269}
270EXPORT_SYMBOL_GPL(i2c_new_device);
271
272
273/**
274 * i2c_unregister_device - reverse effect of i2c_new_device()
275 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200276 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200277 */
278void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200279{
280 struct i2c_adapter *adapter = client->adapter;
281 struct i2c_driver *driver = client->driver;
282
283 if (driver && !is_newstyle_driver(driver)) {
284 dev_err(&client->dev, "can't unregister devices "
285 "with legacy drivers\n");
286 WARN_ON(1);
287 return;
288 }
289
290 mutex_lock(&adapter->clist_lock);
291 list_del(&client->list);
292 mutex_unlock(&adapter->clist_lock);
293
294 device_unregister(&client->dev);
295}
David Brownell9c1600e2007-05-01 23:26:31 +0200296EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200297
298
David Brownelle9f13732008-01-27 18:14:52 +0100299static int dummy_nop(struct i2c_client *client)
300{
301 return 0;
302}
303
304static struct i2c_driver dummy_driver = {
305 .driver.name = "dummy",
306 .probe = dummy_nop,
307 .remove = dummy_nop,
308};
309
310/**
311 * i2c_new_dummy - return a new i2c device bound to a dummy driver
312 * @adapter: the adapter managing the device
313 * @address: seven bit address to be used
314 * @type: optional label used for i2c_client.name
315 * Context: can sleep
316 *
317 * This returns an I2C client bound to the "dummy" driver, intended for use
318 * with devices that consume multiple addresses. Examples of such chips
319 * include various EEPROMS (like 24c04 and 24c08 models).
320 *
321 * These dummy devices have two main uses. First, most I2C and SMBus calls
322 * except i2c_transfer() need a client handle; the dummy will be that handle.
323 * And second, this prevents the specified address from being bound to a
324 * different driver.
325 *
326 * This returns the new i2c client, which should be saved for later use with
327 * i2c_unregister_device(); or NULL to indicate an error.
328 */
329struct i2c_client *
330i2c_new_dummy(struct i2c_adapter *adapter, u16 address, const char *type)
331{
332 struct i2c_board_info info = {
333 .driver_name = "dummy",
334 .addr = address,
335 };
336
337 if (type)
338 strlcpy(info.type, type, sizeof info.type);
339 return i2c_new_device(adapter, &info);
340}
341EXPORT_SYMBOL_GPL(i2c_new_dummy);
342
David Brownellf37dd802007-02-13 22:09:00 +0100343/* ------------------------------------------------------------------------- */
344
David Brownell16ffadf2007-05-01 23:26:28 +0200345/* I2C bus adapters -- one roots each I2C or SMBUS segment */
346
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200347static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
David Brownellef2c83212007-05-01 23:26:28 +0200349 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 complete(&adap->dev_released);
351}
352
David Brownell16ffadf2007-05-01 23:26:28 +0200353static ssize_t
354show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
David Brownellef2c83212007-05-01 23:26:28 +0200356 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return sprintf(buf, "%s\n", adap->name);
358}
David Brownell16ffadf2007-05-01 23:26:28 +0200359
360static struct device_attribute i2c_adapter_attrs[] = {
361 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
362 { },
363};
364
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200365static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200366 .owner = THIS_MODULE,
367 .name = "i2c-adapter",
368 .dev_attrs = i2c_adapter_attrs,
369};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Brownell9c1600e2007-05-01 23:26:31 +0200371static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
372{
373 struct i2c_devinfo *devinfo;
374
375 mutex_lock(&__i2c_board_lock);
376 list_for_each_entry(devinfo, &__i2c_board_list, list) {
377 if (devinfo->busnum == adapter->nr
378 && !i2c_new_device(adapter,
379 &devinfo->board_info))
380 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
381 i2c_adapter_id(adapter),
382 devinfo->board_info.addr);
383 }
384 mutex_unlock(&__i2c_board_lock);
385}
386
Jean Delvare026526f2008-01-27 18:14:49 +0100387static int i2c_do_add_adapter(struct device_driver *d, void *data)
388{
389 struct i2c_driver *driver = to_i2c_driver(d);
390 struct i2c_adapter *adap = data;
391
392 if (driver->attach_adapter) {
393 /* We ignore the return code; if it fails, too bad */
394 driver->attach_adapter(adap);
395 }
396 return 0;
397}
398
David Brownell6e13e642007-05-01 23:26:31 +0200399static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Jean Delvare026526f2008-01-27 18:14:49 +0100401 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Ingo Molnar5c085d32006-01-18 23:16:04 +0100403 mutex_init(&adap->bus_lock);
404 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 INIT_LIST_HEAD(&adap->clients);
406
Jean Delvarecaada322008-01-27 18:14:49 +0100407 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Add the adapter to the driver core.
410 * If the parent pointer is not set up,
411 * we add this adapter to the host bus.
412 */
David Brownellb119dc32007-01-04 13:07:04 +0100413 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100415 pr_debug("I2C adapter driver [%s] forgot to specify "
416 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200420 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200421 res = device_register(&adap->dev);
422 if (res)
423 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200425 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
426
David Brownell6e13e642007-05-01 23:26:31 +0200427 /* create pre-declared device nodes for new-style drivers */
428 if (adap->nr < __i2c_first_dynamic_bus_num)
429 i2c_scan_static_board_info(adap);
430
David Brownell7b4fbc52007-05-01 23:26:30 +0200431 /* let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100432 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
433 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100436 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200438
Jean Delvareb119c6c2006-08-15 18:26:30 +0200439out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200440 idr_remove(&i2c_adapter_idr, adap->nr);
441 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
David Brownell6e13e642007-05-01 23:26:31 +0200444/**
445 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
446 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200447 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200448 *
449 * This routine is used to declare an I2C adapter when its bus number
450 * doesn't matter. Examples: for I2C adapters dynamically added by
451 * USB links or PCI plugin cards.
452 *
453 * When this returns zero, a new bus number was allocated and stored
454 * in adap->nr, and the specified adapter became available for clients.
455 * Otherwise, a negative errno value is returned.
456 */
457int i2c_add_adapter(struct i2c_adapter *adapter)
458{
459 int id, res = 0;
460
461retry:
462 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
463 return -ENOMEM;
464
Jean Delvarecaada322008-01-27 18:14:49 +0100465 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200466 /* "above" here means "above or equal to", sigh */
467 res = idr_get_new_above(&i2c_adapter_idr, adapter,
468 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100469 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200470
471 if (res < 0) {
472 if (res == -EAGAIN)
473 goto retry;
474 return res;
475 }
476
477 adapter->nr = id;
478 return i2c_register_adapter(adapter);
479}
480EXPORT_SYMBOL(i2c_add_adapter);
481
482/**
483 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
484 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200485 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200486 *
487 * This routine is used to declare an I2C adapter when its bus number
488 * matters. Example: for I2C adapters from system-on-chip CPUs, or
489 * otherwise built in to the system's mainboard, and where i2c_board_info
490 * is used to properly configure I2C devices.
491 *
492 * If no devices have pre-been declared for this bus, then be sure to
493 * register the adapter before any dynamically allocated ones. Otherwise
494 * the required bus ID may not be available.
495 *
496 * When this returns zero, the specified adapter became available for
497 * clients using the bus number provided in adap->nr. Also, the table
498 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
499 * and the appropriate driver model device nodes are created. Otherwise, a
500 * negative errno value is returned.
501 */
502int i2c_add_numbered_adapter(struct i2c_adapter *adap)
503{
504 int id;
505 int status;
506
507 if (adap->nr & ~MAX_ID_MASK)
508 return -EINVAL;
509
510retry:
511 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
512 return -ENOMEM;
513
Jean Delvarecaada322008-01-27 18:14:49 +0100514 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200515 /* "above" here means "above or equal to", sigh;
516 * we need the "equal to" result to force the result
517 */
518 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
519 if (status == 0 && id != adap->nr) {
520 status = -EBUSY;
521 idr_remove(&i2c_adapter_idr, id);
522 }
Jean Delvarecaada322008-01-27 18:14:49 +0100523 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200524 if (status == -EAGAIN)
525 goto retry;
526
527 if (status == 0)
528 status = i2c_register_adapter(adap);
529 return status;
530}
531EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
532
Jean Delvare026526f2008-01-27 18:14:49 +0100533static int i2c_do_del_adapter(struct device_driver *d, void *data)
534{
535 struct i2c_driver *driver = to_i2c_driver(d);
536 struct i2c_adapter *adapter = data;
537 int res;
538
539 if (!driver->detach_adapter)
540 return 0;
541 res = driver->detach_adapter(adapter);
542 if (res)
543 dev_err(&adapter->dev, "detach_adapter failed (%d) "
544 "for driver [%s]\n", res, driver->driver.name);
545 return res;
546}
547
David Brownelld64f73b2007-07-12 14:12:28 +0200548/**
549 * i2c_del_adapter - unregister I2C adapter
550 * @adap: the adapter being unregistered
551 * Context: can sleep
552 *
553 * This unregisters an I2C adapter which was previously registered
554 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556int i2c_del_adapter(struct i2c_adapter *adap)
557{
558 struct list_head *item, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct i2c_client *client;
560 int res = 0;
561
Jean Delvarecaada322008-01-27 18:14:49 +0100562 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100565 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200566 pr_debug("i2c-core: attempting to delete unregistered "
567 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 res = -EINVAL;
569 goto out_unlock;
570 }
571
Jean Delvare026526f2008-01-27 18:14:49 +0100572 /* Tell drivers about this removal */
573 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
574 i2c_do_del_adapter);
575 if (res)
576 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200579 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200581 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
David Brownella1d9e6e2007-05-01 23:26:30 +0200583 client = list_entry(item, struct i2c_client, list);
584 driver = client->driver;
585
586 /* new style, follow standard driver model */
587 if (!driver || is_newstyle_driver(driver)) {
588 i2c_unregister_device(client);
589 continue;
590 }
591
592 /* legacy drivers create and remove clients themselves */
593 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200594 dev_err(&adap->dev, "detach_client failed for client "
595 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 client->addr);
597 goto out_unlock;
598 }
599 }
600
601 /* clean up the sysfs representation */
602 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* wait for sysfs to drop all references */
606 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
David Brownell6e13e642007-05-01 23:26:31 +0200608 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 idr_remove(&i2c_adapter_idr, adap->nr);
610
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200611 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100614 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return res;
616}
David Brownellc0564602007-05-01 23:26:31 +0200617EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619
David Brownell7b4fbc52007-05-01 23:26:30 +0200620/* ------------------------------------------------------------------------- */
621
622/*
623 * An i2c_driver is used with one or more i2c_client (device) nodes to access
624 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
625 * are two models for binding the driver to its device: "new style" drivers
626 * follow the standard Linux driver model and just respond to probe() calls
627 * issued if the driver core sees they match(); "legacy" drivers create device
628 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
630
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800631int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100633 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
David Brownell7b4fbc52007-05-01 23:26:30 +0200635 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200636 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200637 if (driver->attach_adapter || driver->detach_adapter
638 || driver->detach_client) {
639 printk(KERN_WARNING
640 "i2c-core: driver [%s] is confused\n",
641 driver->driver.name);
642 return -EINVAL;
643 }
644 }
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800647 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
David Brownell6e13e642007-05-01 23:26:31 +0200650 /* for new style drivers, when registration returns the driver core
651 * will have called probe() for all matching-but-unbound devices.
652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 res = driver_register(&driver->driver);
654 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100655 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100656
Jean Delvarecaada322008-01-27 18:14:49 +0100657 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100658
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100659 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David Brownell7b4fbc52007-05-01 23:26:30 +0200661 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100662 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200663 struct i2c_adapter *adapter;
664
Jean Delvare87c6c222008-01-27 18:14:48 +0100665 down(&i2c_adapter_class.sem);
666 list_for_each_entry(adapter, &i2c_adapter_class.devices,
667 dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 driver->attach_adapter(adapter);
669 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100670 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
Jean Delvarecaada322008-01-27 18:14:49 +0100673 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800676EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David Brownella1d9e6e2007-05-01 23:26:30 +0200678/**
679 * i2c_del_driver - unregister I2C driver
680 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200681 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200682 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200683void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Jean Delvare87c6c222008-01-27 18:14:48 +0100685 struct list_head *item2, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct i2c_client *client;
687 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100688
Jean Delvarecaada322008-01-27 18:14:49 +0100689 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
David Brownella1d9e6e2007-05-01 23:26:30 +0200691 /* new-style driver? */
692 if (is_newstyle_driver(driver))
693 goto unregister;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100696 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
Jean Delvare87c6c222008-01-27 18:14:48 +0100699 down(&i2c_adapter_class.sem);
700 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (driver->detach_adapter) {
Jean Delvareb3e82092007-05-01 23:26:32 +0200702 if (driver->detach_adapter(adap)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200703 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100704 "for driver [%s]\n",
705 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 } else {
708 list_for_each_safe(item2, _n, &adap->clients) {
709 client = list_entry(item2, struct i2c_client, list);
710 if (client->driver != driver)
711 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200712 dev_dbg(&adap->dev, "detaching client [%s] "
713 "at 0x%02x\n", client->name,
714 client->addr);
Jean Delvareb3e82092007-05-01 23:26:32 +0200715 if (driver->detach_client(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200716 dev_err(&adap->dev, "detach_client "
717 "failed for client [%s] at "
718 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 }
722 }
723 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100724 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
David Brownella1d9e6e2007-05-01 23:26:30 +0200726 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100728 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jean Delvarecaada322008-01-27 18:14:49 +0100730 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
David Brownellc0564602007-05-01 23:26:31 +0200732EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
David Brownell7b4fbc52007-05-01 23:26:30 +0200734/* ------------------------------------------------------------------------- */
735
David Brownell9b766b82008-01-27 18:14:51 +0100736static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
David Brownell9b766b82008-01-27 18:14:51 +0100738 struct i2c_client *client = i2c_verify_client(dev);
739 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
David Brownell9b766b82008-01-27 18:14:51 +0100741 if (client && client->addr == addr)
742 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return 0;
744}
745
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100746static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
David Brownell9b766b82008-01-27 18:14:51 +0100748 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751int i2c_attach_client(struct i2c_client *client)
752{
753 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200754 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200758
759 if (client->driver)
760 client->dev.driver = &client->driver->driver;
761
David Brownellde81d2a2007-05-22 19:49:16 +0200762 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200763 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200764 client->dev.uevent_suppress = 1;
765 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200766 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
769 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200770 res = device_register(&client->dev);
771 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100772 goto out_err;
773
774 mutex_lock(&adapter->clist_lock);
775 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200776 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200777
David Brownell86ec5ec2008-01-27 18:14:51 +0100778 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
779 client->name, client->dev.bus_id);
780
Jean Delvare77ed74d2006-09-30 17:18:59 +0200781 if (adapter->client_register) {
782 if (adapter->client_register(client)) {
783 dev_dbg(&adapter->dev, "client_register "
784 "failed for client [%s] at 0x%02x\n",
785 client->name, client->addr);
786 }
787 }
788
789 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200790
David Brownell86ec5ec2008-01-27 18:14:51 +0100791out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200792 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
793 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200794 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
David Brownellc0564602007-05-01 23:26:31 +0200796EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798int i2c_detach_client(struct i2c_client *client)
799{
800 struct i2c_adapter *adapter = client->adapter;
801 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (adapter->client_unregister) {
804 res = adapter->client_unregister(client);
805 if (res) {
806 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700807 "client_unregister [%s] failed, "
808 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto out;
810 }
811 }
812
Ingo Molnar5c085d32006-01-18 23:16:04 +0100813 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 list_del(&client->list);
Jean Delvare9ddced12008-01-27 18:14:51 +0100815 mutex_unlock(&adapter->clist_lock);
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 device_unregister(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 wait_for_completion(&client->released);
820
821 out:
822 return res;
823}
David Brownellc0564602007-05-01 23:26:31 +0200824EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Jean Delvaree48d3312008-01-27 18:14:48 +0100826/**
827 * i2c_use_client - increments the reference count of the i2c client structure
828 * @client: the client being referenced
829 *
830 * Each live reference to a client should be refcounted. The driver model does
831 * that automatically as part of driver binding, so that most drivers don't
832 * need to do this explicitly: they hold a reference until they're unbound
833 * from the device.
834 *
835 * A pointer to the client with the incremented reference counter is returned.
836 */
837struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100839 get_device(&client->dev);
Jean Delvaree48d3312008-01-27 18:14:48 +0100840 return client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
David Brownellc0564602007-05-01 23:26:31 +0200842EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Jean Delvaree48d3312008-01-27 18:14:48 +0100844/**
845 * i2c_release_client - release a use of the i2c client structure
846 * @client: the client being no longer referenced
847 *
848 * Must be called when a user of a client is finished with it.
849 */
850void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100852 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
David Brownellc0564602007-05-01 23:26:31 +0200854EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
David Brownell9b766b82008-01-27 18:14:51 +0100856struct i2c_cmd_arg {
857 unsigned cmd;
858 void *arg;
859};
860
861static int i2c_cmd(struct device *dev, void *_arg)
862{
863 struct i2c_client *client = i2c_verify_client(dev);
864 struct i2c_cmd_arg *arg = _arg;
865
866 if (client && client->driver && client->driver->command)
867 client->driver->command(client, arg->cmd, arg->arg);
868 return 0;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
872{
David Brownell9b766b82008-01-27 18:14:51 +0100873 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
David Brownell9b766b82008-01-27 18:14:51 +0100875 cmd_arg.cmd = cmd;
876 cmd_arg.arg = arg;
877 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
David Brownellc0564602007-05-01 23:26:31 +0200879EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881static int __init i2c_init(void)
882{
883 int retval;
884
885 retval = bus_register(&i2c_bus_type);
886 if (retval)
887 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100888 retval = class_register(&i2c_adapter_class);
889 if (retval)
890 goto bus_err;
891 retval = i2c_add_driver(&dummy_driver);
892 if (retval)
893 goto class_err;
894 return 0;
895
896class_err:
897 class_unregister(&i2c_adapter_class);
898bus_err:
899 bus_unregister(&i2c_bus_type);
900 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903static void __exit i2c_exit(void)
904{
David Brownelle9f13732008-01-27 18:14:52 +0100905 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 bus_unregister(&i2c_bus_type);
908}
909
910subsys_initcall(i2c_init);
911module_exit(i2c_exit);
912
913/* ----------------------------------------------------
914 * the functional interface to the i2c busses.
915 * ----------------------------------------------------
916 */
917
918int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
919{
920 int ret;
921
922 if (adap->algo->master_xfer) {
923#ifdef DEBUG
924 for (ret = 0; ret < num; ret++) {
925 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200926 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
927 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
928 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930#endif
931
Mike Rapoportcea443a82008-01-27 18:14:50 +0100932 if (in_atomic() || irqs_disabled()) {
933 ret = mutex_trylock(&adap->bus_lock);
934 if (!ret)
935 /* I2C activity is ongoing. */
936 return -EAGAIN;
937 } else {
938 mutex_lock_nested(&adap->bus_lock, adap->level);
939 }
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100942 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 return ret;
945 } else {
946 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
947 return -ENOSYS;
948 }
949}
David Brownellc0564602007-05-01 23:26:31 +0200950EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
953{
954 int ret;
955 struct i2c_adapter *adap=client->adapter;
956 struct i2c_msg msg;
957
Jean Delvare815f55f2005-05-07 22:58:46 +0200958 msg.addr = client->addr;
959 msg.flags = client->flags & I2C_M_TEN;
960 msg.len = count;
961 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100962
Jean Delvare815f55f2005-05-07 22:58:46 +0200963 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jean Delvare815f55f2005-05-07 22:58:46 +0200965 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
966 transmitted, else error code. */
967 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
David Brownellc0564602007-05-01 23:26:31 +0200969EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
972{
973 struct i2c_adapter *adap=client->adapter;
974 struct i2c_msg msg;
975 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Jean Delvare815f55f2005-05-07 22:58:46 +0200977 msg.addr = client->addr;
978 msg.flags = client->flags & I2C_M_TEN;
979 msg.flags |= I2C_M_RD;
980 msg.len = count;
981 msg.buf = buf;
982
983 ret = i2c_transfer(adap, &msg, 1);
984
985 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
986 transmitted, else error code. */
987 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
David Brownellc0564602007-05-01 23:26:31 +0200989EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/* ----------------------------------------------------
992 * the i2c address scanning function
993 * Will not work for 10-bit addresses!
994 * ----------------------------------------------------
995 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200996static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
997 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200998{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200999 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001000
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001001 /* Make sure the address is valid */
1002 if (addr < 0x03 || addr > 0x77) {
1003 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1004 addr);
1005 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001006 }
1007
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001008 /* Skip if already in use */
1009 if (i2c_check_addr(adapter, addr))
1010 return 0;
1011
1012 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +02001013 if (kind < 0) {
1014 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1015 I2C_SMBUS_QUICK, NULL) < 0)
1016 return 0;
1017
1018 /* prevent 24RF08 corruption */
1019 if ((addr & ~0x0f) == 0x50)
1020 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1021 I2C_SMBUS_QUICK, NULL);
1022 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001023
1024 /* Finally call the custom detection function */
1025 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001026 /* -ENODEV can be returned if there is a chip at the given address
1027 but it isn't supported by this chip driver. We catch it here as
1028 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +02001029 if (err == -ENODEV)
1030 err = 0;
1031
1032 if (err)
1033 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1034 addr, err);
1035 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001039 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int (*found_proc) (struct i2c_adapter *, int, int))
1041{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001042 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int adap_id = i2c_adapter_id(adapter);
1044
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001045 /* Force entries are done first, and are not affected by ignore
1046 entries */
1047 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001048 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001049 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001051 for (kind = 0; forces[kind]; kind++) {
1052 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1053 i += 2) {
1054 if (forces[kind][i] == adap_id
1055 || forces[kind][i] == ANY_I2C_BUS) {
1056 dev_dbg(&adapter->dev, "found force "
1057 "parameter for adapter %d, "
1058 "addr 0x%02x, kind %d\n",
1059 adap_id, forces[kind][i + 1],
1060 kind);
1061 err = i2c_probe_address(adapter,
1062 forces[kind][i + 1],
1063 kind, found_proc);
1064 if (err)
1065 return err;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001070
Jean Delvare4366dc92005-09-25 16:50:06 +02001071 /* Stop here if we can't use SMBUS_QUICK */
1072 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1073 if (address_data->probe[0] == I2C_CLIENT_END
1074 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001075 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001076
1077 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1078 "can't probe for chips\n");
1079 return -1;
1080 }
1081
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001082 /* Probe entries are done second, and are not affected by ignore
1083 entries either */
1084 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1085 if (address_data->probe[i] == adap_id
1086 || address_data->probe[i] == ANY_I2C_BUS) {
1087 dev_dbg(&adapter->dev, "found probe parameter for "
1088 "adapter %d, addr 0x%02x\n", adap_id,
1089 address_data->probe[i + 1]);
1090 err = i2c_probe_address(adapter,
1091 address_data->probe[i + 1],
1092 -1, found_proc);
1093 if (err)
1094 return err;
1095 }
1096 }
1097
1098 /* Normal entries are done last, unless shadowed by an ignore entry */
1099 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1100 int j, ignore;
1101
1102 ignore = 0;
1103 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1104 j += 2) {
1105 if ((address_data->ignore[j] == adap_id ||
1106 address_data->ignore[j] == ANY_I2C_BUS)
1107 && address_data->ignore[j + 1]
1108 == address_data->normal_i2c[i]) {
1109 dev_dbg(&adapter->dev, "found ignore "
1110 "parameter for adapter %d, "
1111 "addr 0x%02x\n", adap_id,
1112 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001113 ignore = 1;
1114 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001115 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001116 }
1117 if (ignore)
1118 continue;
1119
1120 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1121 "addr 0x%02x\n", adap_id,
1122 address_data->normal_i2c[i]);
1123 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1124 -1, found_proc);
1125 if (err)
1126 return err;
1127 }
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return 0;
1130}
David Brownellc0564602007-05-01 23:26:31 +02001131EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Jean Delvare12b5053a2007-05-01 23:26:31 +02001133struct i2c_client *
1134i2c_new_probed_device(struct i2c_adapter *adap,
1135 struct i2c_board_info *info,
1136 unsigned short const *addr_list)
1137{
1138 int i;
1139
1140 /* Stop here if the bus doesn't support probing */
1141 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1142 dev_err(&adap->dev, "Probing not supported\n");
1143 return NULL;
1144 }
1145
Jean Delvare12b5053a2007-05-01 23:26:31 +02001146 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1147 /* Check address validity */
1148 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1149 dev_warn(&adap->dev, "Invalid 7-bit address "
1150 "0x%02x\n", addr_list[i]);
1151 continue;
1152 }
1153
1154 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001155 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001156 dev_dbg(&adap->dev, "Address 0x%02x already in "
1157 "use, not probing\n", addr_list[i]);
1158 continue;
1159 }
1160
1161 /* Test address responsiveness
1162 The default probe method is a quick write, but it is known
1163 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1164 and could also irreversibly write-protect some EEPROMs, so
1165 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1166 read instead. Also, some bus drivers don't implement
1167 quick write, so we fallback to a byte read it that case
1168 too. */
1169 if ((addr_list[i] & ~0x07) == 0x30
1170 || (addr_list[i] & ~0x0f) == 0x50
1171 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1172 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1173 I2C_SMBUS_READ, 0,
1174 I2C_SMBUS_BYTE, NULL) >= 0)
1175 break;
1176 } else {
1177 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1178 I2C_SMBUS_WRITE, 0,
1179 I2C_SMBUS_QUICK, NULL) >= 0)
1180 break;
1181 }
1182 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001183
1184 if (addr_list[i] == I2C_CLIENT_END) {
1185 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1186 return NULL;
1187 }
1188
1189 info->addr = addr_list[i];
1190 return i2c_new_device(adap, info);
1191}
1192EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194struct i2c_adapter* i2c_get_adapter(int id)
1195{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001197
Jean Delvarecaada322008-01-27 18:14:49 +01001198 mutex_lock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001199 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1200 if (adapter && !try_module_get(adapter->owner))
1201 adapter = NULL;
1202
Jean Delvarecaada322008-01-27 18:14:49 +01001203 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001204 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
David Brownellc0564602007-05-01 23:26:31 +02001206EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208void i2c_put_adapter(struct i2c_adapter *adap)
1209{
1210 module_put(adap->owner);
1211}
David Brownellc0564602007-05-01 23:26:31 +02001212EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214/* The SMBus parts */
1215
David Brownell438d6c22006-12-10 21:21:31 +01001216#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217static u8
1218crc8(u16 data)
1219{
1220 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001223 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 data = data ^ POLY;
1225 data = data << 1;
1226 }
1227 return (u8)(data >> 8);
1228}
1229
Jean Delvare421ef472005-10-26 21:28:55 +02001230/* Incremental CRC8 over count bytes in the array pointed to by p */
1231static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 int i;
1234
1235 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001236 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return crc;
1238}
1239
Jean Delvare421ef472005-10-26 21:28:55 +02001240/* Assume a 7-bit address, which is reasonable for SMBus */
1241static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Jean Delvare421ef472005-10-26 21:28:55 +02001243 /* The address will be sent first */
1244 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1245 pec = i2c_smbus_pec(pec, &addr, 1);
1246
1247 /* The data buffer follows */
1248 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Jean Delvare421ef472005-10-26 21:28:55 +02001251/* Used for write only transactions */
1252static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Jean Delvare421ef472005-10-26 21:28:55 +02001254 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1255 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Jean Delvare421ef472005-10-26 21:28:55 +02001258/* Return <0 on CRC error
1259 If there was a write before this read (most cases) we need to take the
1260 partial CRC from the write part into account.
1261 Note that this function does modify the message (we need to decrease the
1262 message length to hide the CRC byte from the caller). */
1263static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Jean Delvare421ef472005-10-26 21:28:55 +02001265 u8 rpec = msg->buf[--msg->len];
1266 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (rpec != cpec) {
1269 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1270 rpec, cpec);
1271 return -1;
1272 }
David Brownell438d6c22006-12-10 21:21:31 +01001273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1277{
1278 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001279 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
David Brownellc0564602007-05-01 23:26:31 +02001281EXPORT_SYMBOL(i2c_smbus_write_quick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283s32 i2c_smbus_read_byte(struct i2c_client *client)
1284{
1285 union i2c_smbus_data data;
1286 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1287 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1288 return -1;
1289 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001290 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
David Brownellc0564602007-05-01 23:26:31 +02001292EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001297 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
David Brownellc0564602007-05-01 23:26:31 +02001299EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1302{
1303 union i2c_smbus_data data;
1304 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1305 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1306 return -1;
1307 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001308 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
David Brownellc0564602007-05-01 23:26:31 +02001310EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1313{
1314 union i2c_smbus_data data;
1315 data.byte = value;
1316 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1317 I2C_SMBUS_WRITE,command,
1318 I2C_SMBUS_BYTE_DATA,&data);
1319}
David Brownellc0564602007-05-01 23:26:31 +02001320EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1323{
1324 union i2c_smbus_data data;
1325 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1326 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1327 return -1;
1328 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001329 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
David Brownellc0564602007-05-01 23:26:31 +02001331EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1334{
1335 union i2c_smbus_data data;
1336 data.word = value;
1337 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1338 I2C_SMBUS_WRITE,command,
1339 I2C_SMBUS_WORD_DATA,&data);
1340}
David Brownellc0564602007-05-01 23:26:31 +02001341EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
David Brownella64ec072007-10-13 23:56:31 +02001343/**
1344 * i2c_smbus_read_block_data - SMBus block read request
1345 * @client: Handle to slave device
1346 * @command: Command byte issued to let the slave know what data should
1347 * be returned
1348 * @values: Byte array into which data will be read; big enough to hold
1349 * the data returned by the slave. SMBus allows at most 32 bytes.
1350 *
1351 * Returns the number of bytes read in the slave's response, else a
1352 * negative number to indicate some kind of error.
1353 *
1354 * Note that using this function requires that the client's adapter support
1355 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1356 * support this; its emulation through I2C messaging relies on a specific
1357 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1358 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001359s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1360 u8 *values)
1361{
1362 union i2c_smbus_data data;
1363
1364 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1365 I2C_SMBUS_READ, command,
1366 I2C_SMBUS_BLOCK_DATA, &data))
1367 return -1;
1368
1369 memcpy(values, &data.block[1], data.block[0]);
1370 return data.block[0];
1371}
1372EXPORT_SYMBOL(i2c_smbus_read_block_data);
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001375 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (length > I2C_SMBUS_BLOCK_MAX)
1380 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001382 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1384 I2C_SMBUS_WRITE,command,
1385 I2C_SMBUS_BLOCK_DATA,&data);
1386}
David Brownellc0564602007-05-01 23:26:31 +02001387EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001390s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1391 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001394
Jean Delvare4b2643d2007-07-12 14:12:29 +02001395 if (length > I2C_SMBUS_BLOCK_MAX)
1396 length = I2C_SMBUS_BLOCK_MAX;
1397 data.block[0] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1399 I2C_SMBUS_READ,command,
1400 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1401 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001402
1403 memcpy(values, &data.block[1], data.block[0]);
1404 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
David Brownellc0564602007-05-01 23:26:31 +02001406EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Jean Delvare21bbd692006-01-09 15:19:18 +11001408s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001409 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001410{
1411 union i2c_smbus_data data;
1412
1413 if (length > I2C_SMBUS_BLOCK_MAX)
1414 length = I2C_SMBUS_BLOCK_MAX;
1415 data.block[0] = length;
1416 memcpy(data.block + 1, values, length);
1417 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1418 I2C_SMBUS_WRITE, command,
1419 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1420}
David Brownellc0564602007-05-01 23:26:31 +02001421EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001422
David Brownell438d6c22006-12-10 21:21:31 +01001423/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001425static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001427 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 union i2c_smbus_data * data)
1429{
1430 /* So we need to generate a series of msgs. In the case of writing, we
1431 need to use only one message; when reading, we need two. We initialize
1432 most things with sane defaults, to keep the code below somewhat
1433 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001434 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1435 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001437 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1439 };
1440 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001441 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 msgbuf0[0] = command;
1444 switch(size) {
1445 case I2C_SMBUS_QUICK:
1446 msg[0].len = 0;
1447 /* Special case: The read/write field is used as data */
1448 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1449 num = 1;
1450 break;
1451 case I2C_SMBUS_BYTE:
1452 if (read_write == I2C_SMBUS_READ) {
1453 /* Special case: only a read! */
1454 msg[0].flags = I2C_M_RD | flags;
1455 num = 1;
1456 }
1457 break;
1458 case I2C_SMBUS_BYTE_DATA:
1459 if (read_write == I2C_SMBUS_READ)
1460 msg[1].len = 1;
1461 else {
1462 msg[0].len = 2;
1463 msgbuf0[1] = data->byte;
1464 }
1465 break;
1466 case I2C_SMBUS_WORD_DATA:
1467 if (read_write == I2C_SMBUS_READ)
1468 msg[1].len = 2;
1469 else {
1470 msg[0].len=3;
1471 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001472 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474 break;
1475 case I2C_SMBUS_PROC_CALL:
1476 num = 2; /* Special case */
1477 read_write = I2C_SMBUS_READ;
1478 msg[0].len = 3;
1479 msg[1].len = 2;
1480 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001481 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
1483 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001485 msg[1].flags |= I2C_M_RECV_LEN;
1486 msg[1].len = 1; /* block length will be added by
1487 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 } else {
1489 msg[0].len = data->block[0] + 2;
1490 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1491 dev_err(&adapter->dev, "smbus_access called with "
1492 "invalid block write size (%d)\n",
1493 data->block[0]);
1494 return -1;
1495 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001496 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 msgbuf0[i] = data->block[i-1];
1498 }
1499 break;
1500 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001501 num = 2; /* Another special case */
1502 read_write = I2C_SMBUS_READ;
1503 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1504 dev_err(&adapter->dev, "%s called with invalid "
1505 "block proc call size (%d)\n", __FUNCTION__,
1506 data->block[0]);
1507 return -1;
1508 }
1509 msg[0].len = data->block[0] + 2;
1510 for (i = 1; i < msg[0].len; i++)
1511 msgbuf0[i] = data->block[i-1];
1512 msg[1].flags |= I2C_M_RECV_LEN;
1513 msg[1].len = 1; /* block length will be added by
1514 the underlying bus driver */
1515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 case I2C_SMBUS_I2C_BLOCK_DATA:
1517 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001518 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 } else {
1520 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001521 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1523 "invalid block write size (%d)\n",
1524 data->block[0]);
1525 return -1;
1526 }
1527 for (i = 1; i <= data->block[0]; i++)
1528 msgbuf0[i] = data->block[i];
1529 }
1530 break;
1531 default:
1532 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1533 size);
1534 return -1;
1535 }
1536
Jean Delvare421ef472005-10-26 21:28:55 +02001537 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1538 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1539 if (i) {
1540 /* Compute PEC if first message is a write */
1541 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001542 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001543 i2c_smbus_add_pec(&msg[0]);
1544 else /* Write followed by read */
1545 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1546 }
1547 /* Ask for PEC if last message is a read */
1548 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001549 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001550 }
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (i2c_transfer(adapter, msg, num) < 0)
1553 return -1;
1554
Jean Delvare421ef472005-10-26 21:28:55 +02001555 /* Check PEC if last message is a read */
1556 if (i && (msg[num-1].flags & I2C_M_RD)) {
1557 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1558 return -1;
1559 }
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (read_write == I2C_SMBUS_READ)
1562 switch(size) {
1563 case I2C_SMBUS_BYTE:
1564 data->byte = msgbuf0[0];
1565 break;
1566 case I2C_SMBUS_BYTE_DATA:
1567 data->byte = msgbuf1[0];
1568 break;
David Brownell438d6c22006-12-10 21:21:31 +01001569 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 case I2C_SMBUS_PROC_CALL:
1571 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1572 break;
1573 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001574 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 data->block[i+1] = msgbuf1[i];
1576 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001577 case I2C_SMBUS_BLOCK_DATA:
1578 case I2C_SMBUS_BLOCK_PROC_CALL:
1579 for (i = 0; i < msgbuf1[0] + 1; i++)
1580 data->block[i] = msgbuf1[i];
1581 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583 return 0;
1584}
1585
1586
1587s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001588 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 union i2c_smbus_data * data)
1590{
1591 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001596 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1598 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001599 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 } else
1601 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1602 command,size,data);
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return res;
1605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1609MODULE_DESCRIPTION("I2C-Bus main module");
1610MODULE_LICENSE("GPL");