blob: b346a687ab593a89834270e64b91f2b8b1c4c66f [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>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010033#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010034#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010035#include <linux/hardirq.h>
36#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
David Brownell9c1600e2007-05-01 23:26:31 +020039#include "i2c-core.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jean Delvarecaada322008-01-27 18:14:49 +010042static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static DEFINE_IDR(i2c_adapter_idr);
44
Jean Delvare4735c982008-07-14 22:38:36 +020045#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
46
47static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010048
49/* ------------------------------------------------------------------------- */
50
Jean Delvared2653e92008-04-29 23:11:39 +020051static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
52 const struct i2c_client *client)
53{
54 while (id->name[0]) {
55 if (strcmp(client->name, id->name) == 0)
56 return id;
57 id++;
58 }
59 return NULL;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int i2c_device_match(struct device *dev, struct device_driver *drv)
63{
David Brownell7b4fbc52007-05-01 23:26:30 +020064 struct i2c_client *client = to_i2c_client(dev);
65 struct i2c_driver *driver = to_i2c_driver(drv);
66
67 /* make legacy i2c drivers bypass driver model probing entirely;
68 * such drivers scan each i2c adapter/bus themselves.
69 */
David Brownella1d9e6e2007-05-01 23:26:30 +020070 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020071 return 0;
72
Jean Delvared2653e92008-04-29 23:11:39 +020073 /* match on an id table if there is one */
74 if (driver->id_table)
75 return i2c_match_id(driver->id_table, client) != NULL;
76
Jean Delvareeb8a7902008-05-18 20:49:41 +020077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
David Brownell7b4fbc52007-05-01 23:26:30 +020080#ifdef CONFIG_HOTPLUG
81
82/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020083static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020084{
85 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020086
87 /* by definition, legacy drivers can't hotplug */
Jean Delvared2653e92008-04-29 23:11:39 +020088 if (dev->driver)
David Brownell7b4fbc52007-05-01 23:26:30 +020089 return 0;
90
Jean Delvareeb8a7902008-05-18 20:49:41 +020091 if (add_uevent_var(env, "MODALIAS=%s%s",
92 I2C_MODULE_PREFIX, client->name))
93 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020094 dev_dbg(dev, "uevent\n");
95 return 0;
96}
97
98#else
99#define i2c_device_uevent NULL
100#endif /* CONFIG_HOTPLUG */
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int i2c_device_probe(struct device *dev)
103{
David Brownell7b4fbc52007-05-01 23:26:30 +0200104 struct i2c_client *client = to_i2c_client(dev);
105 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +0100106 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200107
Jean Delvaree0457442008-07-14 22:38:30 +0200108 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200109 return -ENODEV;
110 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200111 if (!device_can_wakeup(&client->dev))
112 device_init_wakeup(&client->dev,
113 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200114 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200115
Jean Delvaree0457442008-07-14 22:38:30 +0200116 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100117 if (status)
118 client->driver = NULL;
119 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static int i2c_device_remove(struct device *dev)
123{
David Brownella1d9e6e2007-05-01 23:26:30 +0200124 struct i2c_client *client = to_i2c_client(dev);
125 struct i2c_driver *driver;
126 int status;
127
128 if (!dev->driver)
129 return 0;
130
131 driver = to_i2c_driver(dev->driver);
132 if (driver->remove) {
133 dev_dbg(dev, "remove\n");
134 status = driver->remove(client);
135 } else {
136 dev->driver = NULL;
137 status = 0;
138 }
139 if (status == 0)
140 client->driver = NULL;
141 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
David Brownellf37dd802007-02-13 22:09:00 +0100144static void i2c_device_shutdown(struct device *dev)
145{
146 struct i2c_driver *driver;
147
148 if (!dev->driver)
149 return;
150 driver = to_i2c_driver(dev->driver);
151 if (driver->shutdown)
152 driver->shutdown(to_i2c_client(dev));
153}
154
155static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
156{
157 struct i2c_driver *driver;
158
159 if (!dev->driver)
160 return 0;
161 driver = to_i2c_driver(dev->driver);
162 if (!driver->suspend)
163 return 0;
164 return driver->suspend(to_i2c_client(dev), mesg);
165}
166
167static int i2c_device_resume(struct device * dev)
168{
169 struct i2c_driver *driver;
170
171 if (!dev->driver)
172 return 0;
173 driver = to_i2c_driver(dev->driver);
174 if (!driver->resume)
175 return 0;
176 return driver->resume(to_i2c_client(dev));
177}
178
David Brownell7b4fbc52007-05-01 23:26:30 +0200179static void i2c_client_release(struct device *dev)
180{
181 struct i2c_client *client = to_i2c_client(dev);
182 complete(&client->released);
183}
184
David Brownell9c1600e2007-05-01 23:26:31 +0200185static void i2c_client_dev_release(struct device *dev)
186{
187 kfree(to_i2c_client(dev));
188}
189
David Brownell7b4fbc52007-05-01 23:26:30 +0200190static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
191{
192 struct i2c_client *client = to_i2c_client(dev);
193 return sprintf(buf, "%s\n", client->name);
194}
195
196static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
197{
198 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200199 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200200}
201
202static struct device_attribute i2c_dev_attrs[] = {
203 __ATTR(name, S_IRUGO, show_client_name, NULL),
204 /* modalias helps coldplug: modprobe $(cat .../modalias) */
205 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
206 { },
207};
208
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200209struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100210 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200211 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100212 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200213 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100214 .probe = i2c_device_probe,
215 .remove = i2c_device_remove,
216 .shutdown = i2c_device_shutdown,
217 .suspend = i2c_device_suspend,
218 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000219};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200220EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000221
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
Marc Pignatee354252008-08-28 08:33:22 +0200269 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200270 client->addr = info->addr;
271 client->irq = info->irq;
272
David Brownell9c1600e2007-05-01 23:26:31 +0200273 strlcpy(client->name, info->type, sizeof(client->name));
274
275 /* a new style driver may be bound to this device when we
276 * return from this function, or any later moment (e.g. maybe
277 * hotplugging will load the driver module). and the device
278 * refcount model is the standard driver model one.
279 */
280 status = i2c_attach_client(client);
281 if (status < 0) {
282 kfree(client);
283 client = NULL;
284 }
285 return client;
286}
287EXPORT_SYMBOL_GPL(i2c_new_device);
288
289
290/**
291 * i2c_unregister_device - reverse effect of i2c_new_device()
292 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200293 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200294 */
295void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200296{
297 struct i2c_adapter *adapter = client->adapter;
298 struct i2c_driver *driver = client->driver;
299
300 if (driver && !is_newstyle_driver(driver)) {
301 dev_err(&client->dev, "can't unregister devices "
302 "with legacy drivers\n");
303 WARN_ON(1);
304 return;
305 }
306
Jean Delvare85081592008-07-14 22:38:36 +0200307 if (adapter->client_unregister) {
308 if (adapter->client_unregister(client)) {
309 dev_warn(&client->dev,
310 "client_unregister [%s] failed\n",
311 client->name);
312 }
313 }
314
David Brownella1d9e6e2007-05-01 23:26:30 +0200315 mutex_lock(&adapter->clist_lock);
316 list_del(&client->list);
317 mutex_unlock(&adapter->clist_lock);
318
319 device_unregister(&client->dev);
320}
David Brownell9c1600e2007-05-01 23:26:31 +0200321EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200322
323
Jean Delvare60b129d2008-05-11 20:37:06 +0200324static const struct i2c_device_id dummy_id[] = {
325 { "dummy", 0 },
326 { },
327};
328
Jean Delvared2653e92008-04-29 23:11:39 +0200329static int dummy_probe(struct i2c_client *client,
330 const struct i2c_device_id *id)
331{
332 return 0;
333}
334
335static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100336{
337 return 0;
338}
339
340static struct i2c_driver dummy_driver = {
341 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200342 .probe = dummy_probe,
343 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200344 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100345};
346
347/**
348 * i2c_new_dummy - return a new i2c device bound to a dummy driver
349 * @adapter: the adapter managing the device
350 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100351 * Context: can sleep
352 *
353 * This returns an I2C client bound to the "dummy" driver, intended for use
354 * with devices that consume multiple addresses. Examples of such chips
355 * include various EEPROMS (like 24c04 and 24c08 models).
356 *
357 * These dummy devices have two main uses. First, most I2C and SMBus calls
358 * except i2c_transfer() need a client handle; the dummy will be that handle.
359 * And second, this prevents the specified address from being bound to a
360 * different driver.
361 *
362 * This returns the new i2c client, which should be saved for later use with
363 * i2c_unregister_device(); or NULL to indicate an error.
364 */
365struct i2c_client *
Jean Delvare60b129d2008-05-11 20:37:06 +0200366i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100367{
368 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200369 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100370 };
371
David Brownelle9f13732008-01-27 18:14:52 +0100372 return i2c_new_device(adapter, &info);
373}
374EXPORT_SYMBOL_GPL(i2c_new_dummy);
375
David Brownellf37dd802007-02-13 22:09:00 +0100376/* ------------------------------------------------------------------------- */
377
David Brownell16ffadf2007-05-01 23:26:28 +0200378/* I2C bus adapters -- one roots each I2C or SMBUS segment */
379
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200380static void i2c_adapter_dev_release(struct device *dev)
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 complete(&adap->dev_released);
384}
385
David Brownell16ffadf2007-05-01 23:26:28 +0200386static ssize_t
387show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
David Brownellef2c83212007-05-01 23:26:28 +0200389 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return sprintf(buf, "%s\n", adap->name);
391}
David Brownell16ffadf2007-05-01 23:26:28 +0200392
393static struct device_attribute i2c_adapter_attrs[] = {
394 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
395 { },
396};
397
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200398static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200399 .owner = THIS_MODULE,
400 .name = "i2c-adapter",
401 .dev_attrs = i2c_adapter_attrs,
402};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
David Brownell9c1600e2007-05-01 23:26:31 +0200404static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
405{
406 struct i2c_devinfo *devinfo;
407
408 mutex_lock(&__i2c_board_lock);
409 list_for_each_entry(devinfo, &__i2c_board_list, list) {
410 if (devinfo->busnum == adapter->nr
411 && !i2c_new_device(adapter,
412 &devinfo->board_info))
413 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
414 i2c_adapter_id(adapter),
415 devinfo->board_info.addr);
416 }
417 mutex_unlock(&__i2c_board_lock);
418}
419
Jean Delvare026526f2008-01-27 18:14:49 +0100420static int i2c_do_add_adapter(struct device_driver *d, void *data)
421{
422 struct i2c_driver *driver = to_i2c_driver(d);
423 struct i2c_adapter *adap = data;
424
Jean Delvare4735c982008-07-14 22:38:36 +0200425 /* Detect supported devices on that bus, and instantiate them */
426 i2c_detect(adap, driver);
427
428 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100429 if (driver->attach_adapter) {
430 /* We ignore the return code; if it fails, too bad */
431 driver->attach_adapter(adap);
432 }
433 return 0;
434}
435
David Brownell6e13e642007-05-01 23:26:31 +0200436static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jean Delvare026526f2008-01-27 18:14:49 +0100438 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Ingo Molnar5c085d32006-01-18 23:16:04 +0100440 mutex_init(&adap->bus_lock);
441 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 INIT_LIST_HEAD(&adap->clients);
443
Jean Delvarecaada322008-01-27 18:14:49 +0100444 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* Add the adapter to the driver core.
447 * If the parent pointer is not set up,
448 * we add this adapter to the host bus.
449 */
David Brownellb119dc32007-01-04 13:07:04 +0100450 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100452 pr_debug("I2C adapter driver [%s] forgot to specify "
453 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200457 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200458 res = device_register(&adap->dev);
459 if (res)
460 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200462 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
463
David Brownell6e13e642007-05-01 23:26:31 +0200464 /* create pre-declared device nodes for new-style drivers */
465 if (adap->nr < __i2c_first_dynamic_bus_num)
466 i2c_scan_static_board_info(adap);
467
Jean Delvare4735c982008-07-14 22:38:36 +0200468 /* Notify drivers */
Jean Delvare026526f2008-01-27 18:14:49 +0100469 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
470 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100473 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200475
Jean Delvareb119c6c2006-08-15 18:26:30 +0200476out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200477 idr_remove(&i2c_adapter_idr, adap->nr);
478 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
David Brownell6e13e642007-05-01 23:26:31 +0200481/**
482 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
483 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200484 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200485 *
486 * This routine is used to declare an I2C adapter when its bus number
487 * doesn't matter. Examples: for I2C adapters dynamically added by
488 * USB links or PCI plugin cards.
489 *
490 * When this returns zero, a new bus number was allocated and stored
491 * in adap->nr, and the specified adapter became available for clients.
492 * Otherwise, a negative errno value is returned.
493 */
494int i2c_add_adapter(struct i2c_adapter *adapter)
495{
496 int id, res = 0;
497
498retry:
499 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
500 return -ENOMEM;
501
Jean Delvarecaada322008-01-27 18:14:49 +0100502 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200503 /* "above" here means "above or equal to", sigh */
504 res = idr_get_new_above(&i2c_adapter_idr, adapter,
505 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100506 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200507
508 if (res < 0) {
509 if (res == -EAGAIN)
510 goto retry;
511 return res;
512 }
513
514 adapter->nr = id;
515 return i2c_register_adapter(adapter);
516}
517EXPORT_SYMBOL(i2c_add_adapter);
518
519/**
520 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
521 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200522 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200523 *
524 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100525 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
526 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200527 * is used to properly configure I2C devices.
528 *
529 * If no devices have pre-been declared for this bus, then be sure to
530 * register the adapter before any dynamically allocated ones. Otherwise
531 * the required bus ID may not be available.
532 *
533 * When this returns zero, the specified adapter became available for
534 * clients using the bus number provided in adap->nr. Also, the table
535 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
536 * and the appropriate driver model device nodes are created. Otherwise, a
537 * negative errno value is returned.
538 */
539int i2c_add_numbered_adapter(struct i2c_adapter *adap)
540{
541 int id;
542 int status;
543
544 if (adap->nr & ~MAX_ID_MASK)
545 return -EINVAL;
546
547retry:
548 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
549 return -ENOMEM;
550
Jean Delvarecaada322008-01-27 18:14:49 +0100551 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200552 /* "above" here means "above or equal to", sigh;
553 * we need the "equal to" result to force the result
554 */
555 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
556 if (status == 0 && id != adap->nr) {
557 status = -EBUSY;
558 idr_remove(&i2c_adapter_idr, id);
559 }
Jean Delvarecaada322008-01-27 18:14:49 +0100560 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200561 if (status == -EAGAIN)
562 goto retry;
563
564 if (status == 0)
565 status = i2c_register_adapter(adap);
566 return status;
567}
568EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
569
Jean Delvare026526f2008-01-27 18:14:49 +0100570static int i2c_do_del_adapter(struct device_driver *d, void *data)
571{
572 struct i2c_driver *driver = to_i2c_driver(d);
573 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200574 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100575 int res;
576
Jean Delvare4735c982008-07-14 22:38:36 +0200577 /* Remove the devices we created ourselves */
578 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
579 if (client->adapter == adapter) {
580 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
581 client->name, client->addr);
582 list_del(&client->detected);
583 i2c_unregister_device(client);
584 }
585 }
586
Jean Delvare026526f2008-01-27 18:14:49 +0100587 if (!driver->detach_adapter)
588 return 0;
589 res = driver->detach_adapter(adapter);
590 if (res)
591 dev_err(&adapter->dev, "detach_adapter failed (%d) "
592 "for driver [%s]\n", res, driver->driver.name);
593 return res;
594}
595
David Brownelld64f73b2007-07-12 14:12:28 +0200596/**
597 * i2c_del_adapter - unregister I2C adapter
598 * @adap: the adapter being unregistered
599 * Context: can sleep
600 *
601 * This unregisters an I2C adapter which was previously registered
602 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604int i2c_del_adapter(struct i2c_adapter *adap)
605{
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200606 struct i2c_client *client, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int res = 0;
608
Jean Delvarecaada322008-01-27 18:14:49 +0100609 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100612 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200613 pr_debug("i2c-core: attempting to delete unregistered "
614 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 res = -EINVAL;
616 goto out_unlock;
617 }
618
Jean Delvare026526f2008-01-27 18:14:49 +0100619 /* Tell drivers about this removal */
620 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
621 i2c_do_del_adapter);
622 if (res)
623 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200626 * it can fail; in which case we give up. */
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200627 list_for_each_entry_safe(client, _n, &adap->clients, list) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200628 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
David Brownella1d9e6e2007-05-01 23:26:30 +0200630 driver = client->driver;
631
632 /* new style, follow standard driver model */
633 if (!driver || is_newstyle_driver(driver)) {
634 i2c_unregister_device(client);
635 continue;
636 }
637
638 /* legacy drivers create and remove clients themselves */
639 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200640 dev_err(&adap->dev, "detach_client failed for client "
641 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 client->addr);
643 goto out_unlock;
644 }
645 }
646
647 /* clean up the sysfs representation */
648 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 /* wait for sysfs to drop all references */
652 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
David Brownell6e13e642007-05-01 23:26:31 +0200654 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 idr_remove(&i2c_adapter_idr, adap->nr);
656
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200657 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200659 /* Clear the device structure in case this adapter is ever going to be
660 added again */
661 memset(&adap->dev, 0, sizeof(adap->dev));
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100664 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return res;
666}
David Brownellc0564602007-05-01 23:26:31 +0200667EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669
David Brownell7b4fbc52007-05-01 23:26:30 +0200670/* ------------------------------------------------------------------------- */
671
Dave Young7f101a92008-07-14 22:38:19 +0200672static int __attach_adapter(struct device *dev, void *data)
673{
674 struct i2c_adapter *adapter = to_i2c_adapter(dev);
675 struct i2c_driver *driver = data;
676
Jean Delvare4735c982008-07-14 22:38:36 +0200677 i2c_detect(adapter, driver);
678
679 /* Legacy drivers scan i2c busses directly */
680 if (driver->attach_adapter)
681 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200682
683 return 0;
684}
685
David Brownell7b4fbc52007-05-01 23:26:30 +0200686/*
687 * An i2c_driver is used with one or more i2c_client (device) nodes to access
688 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
689 * are two models for binding the driver to its device: "new style" drivers
690 * follow the standard Linux driver model and just respond to probe() calls
691 * issued if the driver core sees they match(); "legacy" drivers create device
692 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
694
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800695int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100697 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
David Brownell7b4fbc52007-05-01 23:26:30 +0200699 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200700 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200701 if (driver->attach_adapter || driver->detach_adapter
702 || driver->detach_client) {
703 printk(KERN_WARNING
704 "i2c-core: driver [%s] is confused\n",
705 driver->driver.name);
706 return -EINVAL;
707 }
708 }
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800711 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
David Brownell6e13e642007-05-01 23:26:31 +0200714 /* for new style drivers, when registration returns the driver core
715 * will have called probe() for all matching-but-unbound devices.
716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 res = driver_register(&driver->driver);
718 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100719 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100720
Jean Delvarecaada322008-01-27 18:14:49 +0100721 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100722
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100723 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jean Delvare4735c982008-07-14 22:38:36 +0200725 INIT_LIST_HEAD(&driver->clients);
726 /* Walk the adapters that are already present */
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400727 class_for_each_device(&i2c_adapter_class, NULL, driver,
728 __attach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jean Delvarecaada322008-01-27 18:14:49 +0100730 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800733EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Dave Young7f101a92008-07-14 22:38:19 +0200735static int __detach_adapter(struct device *dev, void *data)
736{
737 struct i2c_adapter *adapter = to_i2c_adapter(dev);
738 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200739 struct i2c_client *client, *_n;
740
741 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
742 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
743 client->name, client->addr);
744 list_del(&client->detected);
745 i2c_unregister_device(client);
746 }
747
748 if (is_newstyle_driver(driver))
749 return 0;
Dave Young7f101a92008-07-14 22:38:19 +0200750
751 /* Have a look at each adapter, if clients of this driver are still
752 * attached. If so, detach them to be able to kill the driver
753 * afterwards.
754 */
755 if (driver->detach_adapter) {
756 if (driver->detach_adapter(adapter))
757 dev_err(&adapter->dev,
758 "detach_adapter failed for driver [%s]\n",
759 driver->driver.name);
760 } else {
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200761 struct i2c_client *client, *_n;
Dave Young7f101a92008-07-14 22:38:19 +0200762
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200763 list_for_each_entry_safe(client, _n, &adapter->clients, list) {
Dave Young7f101a92008-07-14 22:38:19 +0200764 if (client->driver != driver)
765 continue;
766 dev_dbg(&adapter->dev,
767 "detaching client [%s] at 0x%02x\n",
768 client->name, client->addr);
769 if (driver->detach_client(client))
770 dev_err(&adapter->dev, "detach_client "
771 "failed for client [%s] at 0x%02x\n",
772 client->name, client->addr);
773 }
774 }
775
776 return 0;
777}
778
David Brownella1d9e6e2007-05-01 23:26:30 +0200779/**
780 * i2c_del_driver - unregister I2C driver
781 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200782 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200783 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200784void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Jean Delvarecaada322008-01-27 18:14:49 +0100786 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400788 class_for_each_device(&i2c_adapter_class, NULL, driver,
789 __detach_adapter);
David Brownella1d9e6e2007-05-01 23:26:30 +0200790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100792 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Jean Delvarecaada322008-01-27 18:14:49 +0100794 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
David Brownellc0564602007-05-01 23:26:31 +0200796EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
David Brownell7b4fbc52007-05-01 23:26:30 +0200798/* ------------------------------------------------------------------------- */
799
David Brownell9b766b82008-01-27 18:14:51 +0100800static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
David Brownell9b766b82008-01-27 18:14:51 +0100802 struct i2c_client *client = i2c_verify_client(dev);
803 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
David Brownell9b766b82008-01-27 18:14:51 +0100805 if (client && client->addr == addr)
806 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808}
809
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100810static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
David Brownell9b766b82008-01-27 18:14:51 +0100812 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815int i2c_attach_client(struct i2c_client *client)
816{
817 struct i2c_adapter *adapter = client->adapter;
Jean Delvarec1159f92008-08-10 22:56:16 +0200818 int res;
819
820 /* Check for address business */
821 res = i2c_check_addr(adapter, client->addr);
822 if (res)
823 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200827
828 if (client->driver)
829 client->dev.driver = &client->driver->driver;
830
David Brownellde81d2a2007-05-22 19:49:16 +0200831 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200832 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200833 client->dev.uevent_suppress = 1;
834 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200835 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
838 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200839 res = device_register(&client->dev);
840 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100841 goto out_err;
842
843 mutex_lock(&adapter->clist_lock);
844 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200845 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200846
David Brownell86ec5ec2008-01-27 18:14:51 +0100847 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
848 client->name, client->dev.bus_id);
849
Jean Delvare77ed74d2006-09-30 17:18:59 +0200850 if (adapter->client_register) {
851 if (adapter->client_register(client)) {
852 dev_dbg(&adapter->dev, "client_register "
853 "failed for client [%s] at 0x%02x\n",
854 client->name, client->addr);
855 }
856 }
857
858 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200859
David Brownell86ec5ec2008-01-27 18:14:51 +0100860out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200861 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
862 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200863 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
David Brownellc0564602007-05-01 23:26:31 +0200865EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867int i2c_detach_client(struct i2c_client *client)
868{
869 struct i2c_adapter *adapter = client->adapter;
870 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (adapter->client_unregister) {
873 res = adapter->client_unregister(client);
874 if (res) {
875 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700876 "client_unregister [%s] failed, "
877 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto out;
879 }
880 }
881
Ingo Molnar5c085d32006-01-18 23:16:04 +0100882 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 list_del(&client->list);
Jean Delvare9ddced12008-01-27 18:14:51 +0100884 mutex_unlock(&adapter->clist_lock);
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 device_unregister(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 wait_for_completion(&client->released);
889
890 out:
891 return res;
892}
David Brownellc0564602007-05-01 23:26:31 +0200893EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Jean Delvaree48d3312008-01-27 18:14:48 +0100895/**
896 * i2c_use_client - increments the reference count of the i2c client structure
897 * @client: the client being referenced
898 *
899 * Each live reference to a client should be refcounted. The driver model does
900 * that automatically as part of driver binding, so that most drivers don't
901 * need to do this explicitly: they hold a reference until they're unbound
902 * from the device.
903 *
904 * A pointer to the client with the incremented reference counter is returned.
905 */
906struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
David Brownell6ea438e2008-07-14 22:38:24 +0200908 if (client && get_device(&client->dev))
909 return client;
910 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
David Brownellc0564602007-05-01 23:26:31 +0200912EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Jean Delvaree48d3312008-01-27 18:14:48 +0100914/**
915 * i2c_release_client - release a use of the i2c client structure
916 * @client: the client being no longer referenced
917 *
918 * Must be called when a user of a client is finished with it.
919 */
920void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
David Brownell6ea438e2008-07-14 22:38:24 +0200922 if (client)
923 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
David Brownellc0564602007-05-01 23:26:31 +0200925EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
David Brownell9b766b82008-01-27 18:14:51 +0100927struct i2c_cmd_arg {
928 unsigned cmd;
929 void *arg;
930};
931
932static int i2c_cmd(struct device *dev, void *_arg)
933{
934 struct i2c_client *client = i2c_verify_client(dev);
935 struct i2c_cmd_arg *arg = _arg;
936
937 if (client && client->driver && client->driver->command)
938 client->driver->command(client, arg->cmd, arg->arg);
939 return 0;
940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
943{
David Brownell9b766b82008-01-27 18:14:51 +0100944 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
David Brownell9b766b82008-01-27 18:14:51 +0100946 cmd_arg.cmd = cmd;
947 cmd_arg.arg = arg;
948 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
David Brownellc0564602007-05-01 23:26:31 +0200950EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952static int __init i2c_init(void)
953{
954 int retval;
955
956 retval = bus_register(&i2c_bus_type);
957 if (retval)
958 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100959 retval = class_register(&i2c_adapter_class);
960 if (retval)
961 goto bus_err;
962 retval = i2c_add_driver(&dummy_driver);
963 if (retval)
964 goto class_err;
965 return 0;
966
967class_err:
968 class_unregister(&i2c_adapter_class);
969bus_err:
970 bus_unregister(&i2c_bus_type);
971 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974static void __exit i2c_exit(void)
975{
David Brownelle9f13732008-01-27 18:14:52 +0100976 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 bus_unregister(&i2c_bus_type);
979}
980
981subsys_initcall(i2c_init);
982module_exit(i2c_exit);
983
984/* ----------------------------------------------------
985 * the functional interface to the i2c busses.
986 * ----------------------------------------------------
987 */
988
David Brownella1cdeda2008-07-14 22:38:24 +0200989/**
990 * i2c_transfer - execute a single or combined I2C message
991 * @adap: Handle to I2C bus
992 * @msgs: One or more messages to execute before STOP is issued to
993 * terminate the operation; each message begins with a START.
994 * @num: Number of messages to be executed.
995 *
996 * Returns negative errno, else the number of messages executed.
997 *
998 * Note that there is no requirement that each message be sent to
999 * the same slave address, although that is the most common model.
1000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
1002{
1003 int ret;
1004
David Brownella1cdeda2008-07-14 22:38:24 +02001005 /* REVISIT the fault reporting model here is weak:
1006 *
1007 * - When we get an error after receiving N bytes from a slave,
1008 * there is no way to report "N".
1009 *
1010 * - When we get a NAK after transmitting N bytes to a slave,
1011 * there is no way to report "N" ... or to let the master
1012 * continue executing the rest of this combined message, if
1013 * that's the appropriate response.
1014 *
1015 * - When for example "num" is two and we successfully complete
1016 * the first message but get an error part way through the
1017 * second, it's unclear whether that should be reported as
1018 * one (discarding status on the second message) or errno
1019 * (discarding status on the first one).
1020 */
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (adap->algo->master_xfer) {
1023#ifdef DEBUG
1024 for (ret = 0; ret < num; ret++) {
1025 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001026 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1027 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1028 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030#endif
1031
Mike Rapoportcea443a82008-01-27 18:14:50 +01001032 if (in_atomic() || irqs_disabled()) {
1033 ret = mutex_trylock(&adap->bus_lock);
1034 if (!ret)
1035 /* I2C activity is ongoing. */
1036 return -EAGAIN;
1037 } else {
1038 mutex_lock_nested(&adap->bus_lock, adap->level);
1039 }
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001042 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 return ret;
1045 } else {
1046 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001047 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049}
David Brownellc0564602007-05-01 23:26:31 +02001050EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
David Brownella1cdeda2008-07-14 22:38:24 +02001052/**
1053 * i2c_master_send - issue a single I2C message in master transmit mode
1054 * @client: Handle to slave device
1055 * @buf: Data that will be written to the slave
1056 * @count: How many bytes to write
1057 *
1058 * Returns negative errno, or else the number of bytes written.
1059 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1061{
1062 int ret;
1063 struct i2c_adapter *adap=client->adapter;
1064 struct i2c_msg msg;
1065
Jean Delvare815f55f2005-05-07 22:58:46 +02001066 msg.addr = client->addr;
1067 msg.flags = client->flags & I2C_M_TEN;
1068 msg.len = count;
1069 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001070
Jean Delvare815f55f2005-05-07 22:58:46 +02001071 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Jean Delvare815f55f2005-05-07 22:58:46 +02001073 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1074 transmitted, else error code. */
1075 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
David Brownellc0564602007-05-01 23:26:31 +02001077EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
David Brownella1cdeda2008-07-14 22:38:24 +02001079/**
1080 * i2c_master_recv - issue a single I2C message in master receive mode
1081 * @client: Handle to slave device
1082 * @buf: Where to store data read from slave
1083 * @count: How many bytes to read
1084 *
1085 * Returns negative errno, or else the number of bytes read.
1086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1088{
1089 struct i2c_adapter *adap=client->adapter;
1090 struct i2c_msg msg;
1091 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Jean Delvare815f55f2005-05-07 22:58:46 +02001093 msg.addr = client->addr;
1094 msg.flags = client->flags & I2C_M_TEN;
1095 msg.flags |= I2C_M_RD;
1096 msg.len = count;
1097 msg.buf = buf;
1098
1099 ret = i2c_transfer(adap, &msg, 1);
1100
1101 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1102 transmitted, else error code. */
1103 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
David Brownellc0564602007-05-01 23:26:31 +02001105EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/* ----------------------------------------------------
1108 * the i2c address scanning function
1109 * Will not work for 10-bit addresses!
1110 * ----------------------------------------------------
1111 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001112static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1113 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001114{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001115 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001116
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001117 /* Make sure the address is valid */
1118 if (addr < 0x03 || addr > 0x77) {
1119 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1120 addr);
1121 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001122 }
1123
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001124 /* Skip if already in use */
1125 if (i2c_check_addr(adapter, addr))
1126 return 0;
1127
1128 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +02001129 if (kind < 0) {
1130 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1131 I2C_SMBUS_QUICK, NULL) < 0)
1132 return 0;
1133
1134 /* prevent 24RF08 corruption */
1135 if ((addr & ~0x0f) == 0x50)
1136 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1137 I2C_SMBUS_QUICK, NULL);
1138 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001139
1140 /* Finally call the custom detection function */
1141 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001142 /* -ENODEV can be returned if there is a chip at the given address
1143 but it isn't supported by this chip driver. We catch it here as
1144 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +02001145 if (err == -ENODEV)
1146 err = 0;
1147
1148 if (err)
1149 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1150 addr, err);
1151 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001155 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int (*found_proc) (struct i2c_adapter *, int, int))
1157{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001158 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int adap_id = i2c_adapter_id(adapter);
1160
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001161 /* Force entries are done first, and are not affected by ignore
1162 entries */
1163 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001164 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001165 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001167 for (kind = 0; forces[kind]; kind++) {
1168 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1169 i += 2) {
1170 if (forces[kind][i] == adap_id
1171 || forces[kind][i] == ANY_I2C_BUS) {
1172 dev_dbg(&adapter->dev, "found force "
1173 "parameter for adapter %d, "
1174 "addr 0x%02x, kind %d\n",
1175 adap_id, forces[kind][i + 1],
1176 kind);
1177 err = i2c_probe_address(adapter,
1178 forces[kind][i + 1],
1179 kind, found_proc);
1180 if (err)
1181 return err;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001186
Jean Delvare4366dc92005-09-25 16:50:06 +02001187 /* Stop here if we can't use SMBUS_QUICK */
1188 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1189 if (address_data->probe[0] == I2C_CLIENT_END
1190 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001191 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001192
Jean Delvare4329cf82008-08-28 08:33:23 +02001193 dev_dbg(&adapter->dev, "SMBus Quick command not supported, "
1194 "can't probe for chips\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001195 return -EOPNOTSUPP;
Jean Delvare4366dc92005-09-25 16:50:06 +02001196 }
1197
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001198 /* Probe entries are done second, and are not affected by ignore
1199 entries either */
1200 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1201 if (address_data->probe[i] == adap_id
1202 || address_data->probe[i] == ANY_I2C_BUS) {
1203 dev_dbg(&adapter->dev, "found probe parameter for "
1204 "adapter %d, addr 0x%02x\n", adap_id,
1205 address_data->probe[i + 1]);
1206 err = i2c_probe_address(adapter,
1207 address_data->probe[i + 1],
1208 -1, found_proc);
1209 if (err)
1210 return err;
1211 }
1212 }
1213
1214 /* Normal entries are done last, unless shadowed by an ignore entry */
1215 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1216 int j, ignore;
1217
1218 ignore = 0;
1219 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1220 j += 2) {
1221 if ((address_data->ignore[j] == adap_id ||
1222 address_data->ignore[j] == ANY_I2C_BUS)
1223 && address_data->ignore[j + 1]
1224 == address_data->normal_i2c[i]) {
1225 dev_dbg(&adapter->dev, "found ignore "
1226 "parameter for adapter %d, "
1227 "addr 0x%02x\n", adap_id,
1228 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001229 ignore = 1;
1230 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001231 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001232 }
1233 if (ignore)
1234 continue;
1235
1236 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1237 "addr 0x%02x\n", adap_id,
1238 address_data->normal_i2c[i]);
1239 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1240 -1, found_proc);
1241 if (err)
1242 return err;
1243 }
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 0;
1246}
David Brownellc0564602007-05-01 23:26:31 +02001247EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Jean Delvare4735c982008-07-14 22:38:36 +02001249/* Separate detection function for new-style drivers */
1250static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1251 struct i2c_driver *driver)
1252{
1253 struct i2c_board_info info;
1254 struct i2c_adapter *adapter = temp_client->adapter;
1255 int addr = temp_client->addr;
1256 int err;
1257
1258 /* Make sure the address is valid */
1259 if (addr < 0x03 || addr > 0x77) {
1260 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1261 addr);
1262 return -EINVAL;
1263 }
1264
1265 /* Skip if already in use */
1266 if (i2c_check_addr(adapter, addr))
1267 return 0;
1268
1269 /* Make sure there is something at this address, unless forced */
1270 if (kind < 0) {
1271 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1272 I2C_SMBUS_QUICK, NULL) < 0)
1273 return 0;
1274
1275 /* prevent 24RF08 corruption */
1276 if ((addr & ~0x0f) == 0x50)
1277 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1278 I2C_SMBUS_QUICK, NULL);
1279 }
1280
1281 /* Finally call the custom detection function */
1282 memset(&info, 0, sizeof(struct i2c_board_info));
1283 info.addr = addr;
1284 err = driver->detect(temp_client, kind, &info);
1285 if (err) {
1286 /* -ENODEV is returned if the detection fails. We catch it
1287 here as this isn't an error. */
1288 return err == -ENODEV ? 0 : err;
1289 }
1290
1291 /* Consistency check */
1292 if (info.type[0] == '\0') {
1293 dev_err(&adapter->dev, "%s detection function provided "
1294 "no name for 0x%x\n", driver->driver.name,
1295 addr);
1296 } else {
1297 struct i2c_client *client;
1298
1299 /* Detection succeeded, instantiate the device */
1300 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1301 info.type, info.addr);
1302 client = i2c_new_device(adapter, &info);
1303 if (client)
1304 list_add_tail(&client->detected, &driver->clients);
1305 else
1306 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1307 info.type, info.addr);
1308 }
1309 return 0;
1310}
1311
1312static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1313{
1314 const struct i2c_client_address_data *address_data;
1315 struct i2c_client *temp_client;
1316 int i, err = 0;
1317 int adap_id = i2c_adapter_id(adapter);
1318
1319 address_data = driver->address_data;
1320 if (!driver->detect || !address_data)
1321 return 0;
1322
1323 /* Set up a temporary client to help detect callback */
1324 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1325 if (!temp_client)
1326 return -ENOMEM;
1327 temp_client->adapter = adapter;
1328
1329 /* Force entries are done first, and are not affected by ignore
1330 entries */
1331 if (address_data->forces) {
1332 const unsigned short * const *forces = address_data->forces;
1333 int kind;
1334
1335 for (kind = 0; forces[kind]; kind++) {
1336 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1337 i += 2) {
1338 if (forces[kind][i] == adap_id
1339 || forces[kind][i] == ANY_I2C_BUS) {
1340 dev_dbg(&adapter->dev, "found force "
1341 "parameter for adapter %d, "
1342 "addr 0x%02x, kind %d\n",
1343 adap_id, forces[kind][i + 1],
1344 kind);
1345 temp_client->addr = forces[kind][i + 1];
1346 err = i2c_detect_address(temp_client,
1347 kind, driver);
1348 if (err)
1349 goto exit_free;
1350 }
1351 }
1352 }
1353 }
1354
Jean Delvare4329cf82008-08-28 08:33:23 +02001355 /* Stop here if the classes do not match */
1356 if (!(adapter->class & driver->class))
1357 goto exit_free;
1358
Jean Delvare4735c982008-07-14 22:38:36 +02001359 /* Stop here if we can't use SMBUS_QUICK */
1360 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1361 if (address_data->probe[0] == I2C_CLIENT_END
1362 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1363 goto exit_free;
1364
1365 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1366 "can't probe for chips\n");
1367 err = -EOPNOTSUPP;
1368 goto exit_free;
1369 }
1370
Jean Delvare4735c982008-07-14 22:38:36 +02001371 /* Probe entries are done second, and are not affected by ignore
1372 entries either */
1373 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1374 if (address_data->probe[i] == adap_id
1375 || address_data->probe[i] == ANY_I2C_BUS) {
1376 dev_dbg(&adapter->dev, "found probe parameter for "
1377 "adapter %d, addr 0x%02x\n", adap_id,
1378 address_data->probe[i + 1]);
1379 temp_client->addr = address_data->probe[i + 1];
1380 err = i2c_detect_address(temp_client, -1, driver);
1381 if (err)
1382 goto exit_free;
1383 }
1384 }
1385
1386 /* Normal entries are done last, unless shadowed by an ignore entry */
1387 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1388 int j, ignore;
1389
1390 ignore = 0;
1391 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1392 j += 2) {
1393 if ((address_data->ignore[j] == adap_id ||
1394 address_data->ignore[j] == ANY_I2C_BUS)
1395 && address_data->ignore[j + 1]
1396 == address_data->normal_i2c[i]) {
1397 dev_dbg(&adapter->dev, "found ignore "
1398 "parameter for adapter %d, "
1399 "addr 0x%02x\n", adap_id,
1400 address_data->ignore[j + 1]);
1401 ignore = 1;
1402 break;
1403 }
1404 }
1405 if (ignore)
1406 continue;
1407
1408 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1409 "addr 0x%02x\n", adap_id,
1410 address_data->normal_i2c[i]);
1411 temp_client->addr = address_data->normal_i2c[i];
1412 err = i2c_detect_address(temp_client, -1, driver);
1413 if (err)
1414 goto exit_free;
1415 }
1416
1417 exit_free:
1418 kfree(temp_client);
1419 return err;
1420}
1421
Jean Delvare12b5053a2007-05-01 23:26:31 +02001422struct i2c_client *
1423i2c_new_probed_device(struct i2c_adapter *adap,
1424 struct i2c_board_info *info,
1425 unsigned short const *addr_list)
1426{
1427 int i;
1428
1429 /* Stop here if the bus doesn't support probing */
1430 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1431 dev_err(&adap->dev, "Probing not supported\n");
1432 return NULL;
1433 }
1434
Jean Delvare12b5053a2007-05-01 23:26:31 +02001435 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1436 /* Check address validity */
1437 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1438 dev_warn(&adap->dev, "Invalid 7-bit address "
1439 "0x%02x\n", addr_list[i]);
1440 continue;
1441 }
1442
1443 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001444 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001445 dev_dbg(&adap->dev, "Address 0x%02x already in "
1446 "use, not probing\n", addr_list[i]);
1447 continue;
1448 }
1449
1450 /* Test address responsiveness
1451 The default probe method is a quick write, but it is known
1452 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1453 and could also irreversibly write-protect some EEPROMs, so
1454 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1455 read instead. Also, some bus drivers don't implement
1456 quick write, so we fallback to a byte read it that case
1457 too. */
1458 if ((addr_list[i] & ~0x07) == 0x30
1459 || (addr_list[i] & ~0x0f) == 0x50
1460 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001461 union i2c_smbus_data data;
1462
Jean Delvare12b5053a2007-05-01 23:26:31 +02001463 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1464 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001465 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001466 break;
1467 } else {
1468 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1469 I2C_SMBUS_WRITE, 0,
1470 I2C_SMBUS_QUICK, NULL) >= 0)
1471 break;
1472 }
1473 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001474
1475 if (addr_list[i] == I2C_CLIENT_END) {
1476 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1477 return NULL;
1478 }
1479
1480 info->addr = addr_list[i];
1481 return i2c_new_device(adap, info);
1482}
1483EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485struct i2c_adapter* i2c_get_adapter(int id)
1486{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001488
Jean Delvarecaada322008-01-27 18:14:49 +01001489 mutex_lock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001490 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1491 if (adapter && !try_module_get(adapter->owner))
1492 adapter = NULL;
1493
Jean Delvarecaada322008-01-27 18:14:49 +01001494 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001495 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
David Brownellc0564602007-05-01 23:26:31 +02001497EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499void i2c_put_adapter(struct i2c_adapter *adap)
1500{
1501 module_put(adap->owner);
1502}
David Brownellc0564602007-05-01 23:26:31 +02001503EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505/* The SMBus parts */
1506
David Brownell438d6c22006-12-10 21:21:31 +01001507#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508static u8
1509crc8(u16 data)
1510{
1511 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001514 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 data = data ^ POLY;
1516 data = data << 1;
1517 }
1518 return (u8)(data >> 8);
1519}
1520
Jean Delvare421ef472005-10-26 21:28:55 +02001521/* Incremental CRC8 over count bytes in the array pointed to by p */
1522static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 int i;
1525
1526 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001527 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return crc;
1529}
1530
Jean Delvare421ef472005-10-26 21:28:55 +02001531/* Assume a 7-bit address, which is reasonable for SMBus */
1532static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Jean Delvare421ef472005-10-26 21:28:55 +02001534 /* The address will be sent first */
1535 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1536 pec = i2c_smbus_pec(pec, &addr, 1);
1537
1538 /* The data buffer follows */
1539 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Jean Delvare421ef472005-10-26 21:28:55 +02001542/* Used for write only transactions */
1543static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Jean Delvare421ef472005-10-26 21:28:55 +02001545 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1546 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Jean Delvare421ef472005-10-26 21:28:55 +02001549/* Return <0 on CRC error
1550 If there was a write before this read (most cases) we need to take the
1551 partial CRC from the write part into account.
1552 Note that this function does modify the message (we need to decrease the
1553 message length to hide the CRC byte from the caller). */
1554static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Jean Delvare421ef472005-10-26 21:28:55 +02001556 u8 rpec = msg->buf[--msg->len];
1557 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (rpec != cpec) {
1560 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1561 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001562 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
David Brownell438d6c22006-12-10 21:21:31 +01001564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
David Brownella1cdeda2008-07-14 22:38:24 +02001567/**
1568 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1569 * @client: Handle to slave device
1570 *
1571 * This executes the SMBus "receive byte" protocol, returning negative errno
1572 * else the byte received from the device.
1573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574s32 i2c_smbus_read_byte(struct i2c_client *client)
1575{
1576 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001577 int status;
1578
1579 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1580 I2C_SMBUS_READ, 0,
1581 I2C_SMBUS_BYTE, &data);
1582 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
David Brownellc0564602007-05-01 23:26:31 +02001584EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
David Brownella1cdeda2008-07-14 22:38:24 +02001586/**
1587 * i2c_smbus_write_byte - SMBus "send byte" protocol
1588 * @client: Handle to slave device
1589 * @value: Byte to be sent
1590 *
1591 * This executes the SMBus "send byte" protocol, returning negative errno
1592 * else zero on success.
1593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001597 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
David Brownellc0564602007-05-01 23:26:31 +02001599EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
David Brownella1cdeda2008-07-14 22:38:24 +02001601/**
1602 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1603 * @client: Handle to slave device
1604 * @command: Byte interpreted by slave
1605 *
1606 * This executes the SMBus "read byte" protocol, returning negative errno
1607 * else a data byte received from the device.
1608 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1610{
1611 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001612 int status;
1613
1614 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1615 I2C_SMBUS_READ, command,
1616 I2C_SMBUS_BYTE_DATA, &data);
1617 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
David Brownellc0564602007-05-01 23:26:31 +02001619EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
David Brownella1cdeda2008-07-14 22:38:24 +02001621/**
1622 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1623 * @client: Handle to slave device
1624 * @command: Byte interpreted by slave
1625 * @value: Byte being written
1626 *
1627 * This executes the SMBus "write byte" protocol, returning negative errno
1628 * else zero on success.
1629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1631{
1632 union i2c_smbus_data data;
1633 data.byte = value;
1634 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1635 I2C_SMBUS_WRITE,command,
1636 I2C_SMBUS_BYTE_DATA,&data);
1637}
David Brownellc0564602007-05-01 23:26:31 +02001638EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
David Brownella1cdeda2008-07-14 22:38:24 +02001640/**
1641 * i2c_smbus_read_word_data - SMBus "read word" protocol
1642 * @client: Handle to slave device
1643 * @command: Byte interpreted by slave
1644 *
1645 * This executes the SMBus "read word" protocol, returning negative errno
1646 * else a 16-bit unsigned "word" received from the device.
1647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1649{
1650 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001651 int status;
1652
1653 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1654 I2C_SMBUS_READ, command,
1655 I2C_SMBUS_WORD_DATA, &data);
1656 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
David Brownellc0564602007-05-01 23:26:31 +02001658EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
David Brownella1cdeda2008-07-14 22:38:24 +02001660/**
1661 * i2c_smbus_write_word_data - SMBus "write word" protocol
1662 * @client: Handle to slave device
1663 * @command: Byte interpreted by slave
1664 * @value: 16-bit "word" being written
1665 *
1666 * This executes the SMBus "write word" protocol, returning negative errno
1667 * else zero on success.
1668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1670{
1671 union i2c_smbus_data data;
1672 data.word = value;
1673 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1674 I2C_SMBUS_WRITE,command,
1675 I2C_SMBUS_WORD_DATA,&data);
1676}
David Brownellc0564602007-05-01 23:26:31 +02001677EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
David Brownella64ec072007-10-13 23:56:31 +02001679/**
David Brownella1cdeda2008-07-14 22:38:24 +02001680 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001681 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001682 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001683 * @values: Byte array into which data will be read; big enough to hold
1684 * the data returned by the slave. SMBus allows at most 32 bytes.
1685 *
David Brownella1cdeda2008-07-14 22:38:24 +02001686 * This executes the SMBus "block read" protocol, returning negative errno
1687 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001688 *
1689 * Note that using this function requires that the client's adapter support
1690 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1691 * support this; its emulation through I2C messaging relies on a specific
1692 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1693 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001694s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1695 u8 *values)
1696{
1697 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001698 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001699
David Brownell24a5bb72008-07-14 22:38:23 +02001700 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1701 I2C_SMBUS_READ, command,
1702 I2C_SMBUS_BLOCK_DATA, &data);
1703 if (status)
1704 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001705
1706 memcpy(values, &data.block[1], data.block[0]);
1707 return data.block[0];
1708}
1709EXPORT_SYMBOL(i2c_smbus_read_block_data);
1710
David Brownella1cdeda2008-07-14 22:38:24 +02001711/**
1712 * i2c_smbus_write_block_data - SMBus "block write" protocol
1713 * @client: Handle to slave device
1714 * @command: Byte interpreted by slave
1715 * @length: Size of data block; SMBus allows at most 32 bytes
1716 * @values: Byte array which will be written.
1717 *
1718 * This executes the SMBus "block write" protocol, returning negative errno
1719 * else zero on success.
1720 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001722 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (length > I2C_SMBUS_BLOCK_MAX)
1727 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001729 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1731 I2C_SMBUS_WRITE,command,
1732 I2C_SMBUS_BLOCK_DATA,&data);
1733}
David Brownellc0564602007-05-01 23:26:31 +02001734EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001737s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1738 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001741 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001742
Jean Delvare4b2643d2007-07-12 14:12:29 +02001743 if (length > I2C_SMBUS_BLOCK_MAX)
1744 length = I2C_SMBUS_BLOCK_MAX;
1745 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001746 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1747 I2C_SMBUS_READ, command,
1748 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1749 if (status < 0)
1750 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001751
1752 memcpy(values, &data.block[1], data.block[0]);
1753 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
David Brownellc0564602007-05-01 23:26:31 +02001755EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Jean Delvare21bbd692006-01-09 15:19:18 +11001757s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001758 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001759{
1760 union i2c_smbus_data data;
1761
1762 if (length > I2C_SMBUS_BLOCK_MAX)
1763 length = I2C_SMBUS_BLOCK_MAX;
1764 data.block[0] = length;
1765 memcpy(data.block + 1, values, length);
1766 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1767 I2C_SMBUS_WRITE, command,
1768 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1769}
David Brownellc0564602007-05-01 23:26:31 +02001770EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001771
David Brownell438d6c22006-12-10 21:21:31 +01001772/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001774static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001776 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 union i2c_smbus_data * data)
1778{
1779 /* So we need to generate a series of msgs. In the case of writing, we
1780 need to use only one message; when reading, we need two. We initialize
1781 most things with sane defaults, to keep the code below somewhat
1782 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001783 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1784 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001786 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1788 };
1789 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001790 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001791 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 msgbuf0[0] = command;
1794 switch(size) {
1795 case I2C_SMBUS_QUICK:
1796 msg[0].len = 0;
1797 /* Special case: The read/write field is used as data */
1798 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1799 num = 1;
1800 break;
1801 case I2C_SMBUS_BYTE:
1802 if (read_write == I2C_SMBUS_READ) {
1803 /* Special case: only a read! */
1804 msg[0].flags = I2C_M_RD | flags;
1805 num = 1;
1806 }
1807 break;
1808 case I2C_SMBUS_BYTE_DATA:
1809 if (read_write == I2C_SMBUS_READ)
1810 msg[1].len = 1;
1811 else {
1812 msg[0].len = 2;
1813 msgbuf0[1] = data->byte;
1814 }
1815 break;
1816 case I2C_SMBUS_WORD_DATA:
1817 if (read_write == I2C_SMBUS_READ)
1818 msg[1].len = 2;
1819 else {
1820 msg[0].len=3;
1821 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001822 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 break;
1825 case I2C_SMBUS_PROC_CALL:
1826 num = 2; /* Special case */
1827 read_write = I2C_SMBUS_READ;
1828 msg[0].len = 3;
1829 msg[1].len = 2;
1830 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001831 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 break;
1833 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001835 msg[1].flags |= I2C_M_RECV_LEN;
1836 msg[1].len = 1; /* block length will be added by
1837 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 } else {
1839 msg[0].len = data->block[0] + 2;
1840 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001841 dev_err(&adapter->dev,
1842 "Invalid block write size %d\n",
1843 data->block[0]);
1844 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001846 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 msgbuf0[i] = data->block[i-1];
1848 }
1849 break;
1850 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001851 num = 2; /* Another special case */
1852 read_write = I2C_SMBUS_READ;
1853 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001854 dev_err(&adapter->dev,
1855 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001856 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001857 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001858 }
1859 msg[0].len = data->block[0] + 2;
1860 for (i = 1; i < msg[0].len; i++)
1861 msgbuf0[i] = data->block[i-1];
1862 msg[1].flags |= I2C_M_RECV_LEN;
1863 msg[1].len = 1; /* block length will be added by
1864 the underlying bus driver */
1865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 case I2C_SMBUS_I2C_BLOCK_DATA:
1867 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001868 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 } else {
1870 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001871 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001872 dev_err(&adapter->dev,
1873 "Invalid block write size %d\n",
1874 data->block[0]);
1875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877 for (i = 1; i <= data->block[0]; i++)
1878 msgbuf0[i] = data->block[i];
1879 }
1880 break;
1881 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001882 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1883 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885
Jean Delvare421ef472005-10-26 21:28:55 +02001886 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1887 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1888 if (i) {
1889 /* Compute PEC if first message is a write */
1890 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001891 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001892 i2c_smbus_add_pec(&msg[0]);
1893 else /* Write followed by read */
1894 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1895 }
1896 /* Ask for PEC if last message is a read */
1897 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001898 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001899 }
1900
David Brownell24a5bb72008-07-14 22:38:23 +02001901 status = i2c_transfer(adapter, msg, num);
1902 if (status < 0)
1903 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Jean Delvare421ef472005-10-26 21:28:55 +02001905 /* Check PEC if last message is a read */
1906 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001907 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1908 if (status < 0)
1909 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001910 }
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (read_write == I2C_SMBUS_READ)
1913 switch(size) {
1914 case I2C_SMBUS_BYTE:
1915 data->byte = msgbuf0[0];
1916 break;
1917 case I2C_SMBUS_BYTE_DATA:
1918 data->byte = msgbuf1[0];
1919 break;
David Brownell438d6c22006-12-10 21:21:31 +01001920 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 case I2C_SMBUS_PROC_CALL:
1922 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1923 break;
1924 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001925 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 data->block[i+1] = msgbuf1[i];
1927 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001928 case I2C_SMBUS_BLOCK_DATA:
1929 case I2C_SMBUS_BLOCK_PROC_CALL:
1930 for (i = 0; i < msgbuf1[0] + 1; i++)
1931 data->block[i] = msgbuf1[i];
1932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934 return 0;
1935}
1936
David Brownella1cdeda2008-07-14 22:38:24 +02001937/**
1938 * i2c_smbus_xfer - execute SMBus protocol operations
1939 * @adapter: Handle to I2C bus
1940 * @addr: Address of SMBus slave on that bus
1941 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1942 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1943 * @command: Byte interpreted by slave, for protocols which use such bytes
1944 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1945 * @data: Data to be read or written
1946 *
1947 * This executes an SMBus protocol operation, and returns a negative
1948 * errno code else zero on success.
1949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001951 char read_write, u8 command, int protocol,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 union i2c_smbus_data * data)
1953{
1954 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001959 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001961 command, protocol, data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001962 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 } else
1964 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001965 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return res;
1968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1972MODULE_DESCRIPTION("I2C-Bus main module");
1973MODULE_LICENSE("GPL");