blob: 550853f79ae81dbba8399747423d87eb40bdd59d [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 Rapoportcea443a2008-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;
111 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200112
Jean Delvaree0457442008-07-14 22:38:30 +0200113 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100114 if (status)
115 client->driver = NULL;
116 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static int i2c_device_remove(struct device *dev)
120{
David Brownella1d9e6e2007-05-01 23:26:30 +0200121 struct i2c_client *client = to_i2c_client(dev);
122 struct i2c_driver *driver;
123 int status;
124
125 if (!dev->driver)
126 return 0;
127
128 driver = to_i2c_driver(dev->driver);
129 if (driver->remove) {
130 dev_dbg(dev, "remove\n");
131 status = driver->remove(client);
132 } else {
133 dev->driver = NULL;
134 status = 0;
135 }
136 if (status == 0)
137 client->driver = NULL;
138 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
David Brownellf37dd802007-02-13 22:09:00 +0100141static void i2c_device_shutdown(struct device *dev)
142{
143 struct i2c_driver *driver;
144
145 if (!dev->driver)
146 return;
147 driver = to_i2c_driver(dev->driver);
148 if (driver->shutdown)
149 driver->shutdown(to_i2c_client(dev));
150}
151
152static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
153{
154 struct i2c_driver *driver;
155
156 if (!dev->driver)
157 return 0;
158 driver = to_i2c_driver(dev->driver);
159 if (!driver->suspend)
160 return 0;
161 return driver->suspend(to_i2c_client(dev), mesg);
162}
163
164static int i2c_device_resume(struct device * dev)
165{
166 struct i2c_driver *driver;
167
168 if (!dev->driver)
169 return 0;
170 driver = to_i2c_driver(dev->driver);
171 if (!driver->resume)
172 return 0;
173 return driver->resume(to_i2c_client(dev));
174}
175
David Brownell7b4fbc52007-05-01 23:26:30 +0200176static void i2c_client_release(struct device *dev)
177{
178 struct i2c_client *client = to_i2c_client(dev);
179 complete(&client->released);
180}
181
David Brownell9c1600e2007-05-01 23:26:31 +0200182static void i2c_client_dev_release(struct device *dev)
183{
184 kfree(to_i2c_client(dev));
185}
186
David Brownell7b4fbc52007-05-01 23:26:30 +0200187static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
188{
189 struct i2c_client *client = to_i2c_client(dev);
190 return sprintf(buf, "%s\n", client->name);
191}
192
193static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
194{
195 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200196 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200197}
198
199static struct device_attribute i2c_dev_attrs[] = {
200 __ATTR(name, S_IRUGO, show_client_name, NULL),
201 /* modalias helps coldplug: modprobe $(cat .../modalias) */
202 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
203 { },
204};
205
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200206struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100207 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200208 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100209 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200210 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100211 .probe = i2c_device_probe,
212 .remove = i2c_device_remove,
213 .shutdown = i2c_device_shutdown,
214 .suspend = i2c_device_suspend,
215 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000216};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200217EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000218
David Brownell9b766b82008-01-27 18:14:51 +0100219
220/**
221 * i2c_verify_client - return parameter as i2c_client, or NULL
222 * @dev: device, probably from some driver model iterator
223 *
224 * When traversing the driver model tree, perhaps using driver model
225 * iterators like @device_for_each_child(), you can't assume very much
226 * about the nodes you find. Use this function to avoid oopses caused
227 * by wrongly treating some non-I2C device as an i2c_client.
228 */
229struct i2c_client *i2c_verify_client(struct device *dev)
230{
231 return (dev->bus == &i2c_bus_type)
232 ? to_i2c_client(dev)
233 : NULL;
234}
235EXPORT_SYMBOL(i2c_verify_client);
236
237
David Brownell9c1600e2007-05-01 23:26:31 +0200238/**
239 * i2c_new_device - instantiate an i2c device for use with a new style driver
240 * @adap: the adapter managing the device
241 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200242 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200243 *
244 * Create a device to work with a new style i2c driver, where binding is
245 * handled through driver model probe()/remove() methods. This call is not
246 * appropriate for use by mainboad initialization logic, which usually runs
247 * during an arch_initcall() long before any i2c_adapter could exist.
248 *
249 * This returns the new i2c client, which may be saved for later use with
250 * i2c_unregister_device(); or NULL to indicate an error.
251 */
252struct i2c_client *
253i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
254{
255 struct i2c_client *client;
256 int status;
257
258 client = kzalloc(sizeof *client, GFP_KERNEL);
259 if (!client)
260 return NULL;
261
262 client->adapter = adap;
263
264 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200265 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
266
267 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200268 client->addr = info->addr;
269 client->irq = info->irq;
270
David Brownell9c1600e2007-05-01 23:26:31 +0200271 strlcpy(client->name, info->type, sizeof(client->name));
272
273 /* a new style driver may be bound to this device when we
274 * return from this function, or any later moment (e.g. maybe
275 * hotplugging will load the driver module). and the device
276 * refcount model is the standard driver model one.
277 */
278 status = i2c_attach_client(client);
279 if (status < 0) {
280 kfree(client);
281 client = NULL;
282 }
283 return client;
284}
285EXPORT_SYMBOL_GPL(i2c_new_device);
286
287
288/**
289 * i2c_unregister_device - reverse effect of i2c_new_device()
290 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200291 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200292 */
293void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200294{
295 struct i2c_adapter *adapter = client->adapter;
296 struct i2c_driver *driver = client->driver;
297
298 if (driver && !is_newstyle_driver(driver)) {
299 dev_err(&client->dev, "can't unregister devices "
300 "with legacy drivers\n");
301 WARN_ON(1);
302 return;
303 }
304
Jean Delvare85081592008-07-14 22:38:36 +0200305 if (adapter->client_unregister) {
306 if (adapter->client_unregister(client)) {
307 dev_warn(&client->dev,
308 "client_unregister [%s] failed\n",
309 client->name);
310 }
311 }
312
David Brownella1d9e6e2007-05-01 23:26:30 +0200313 mutex_lock(&adapter->clist_lock);
314 list_del(&client->list);
315 mutex_unlock(&adapter->clist_lock);
316
317 device_unregister(&client->dev);
318}
David Brownell9c1600e2007-05-01 23:26:31 +0200319EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200320
321
Jean Delvare60b129d2008-05-11 20:37:06 +0200322static const struct i2c_device_id dummy_id[] = {
323 { "dummy", 0 },
324 { },
325};
326
Jean Delvared2653e92008-04-29 23:11:39 +0200327static int dummy_probe(struct i2c_client *client,
328 const struct i2c_device_id *id)
329{
330 return 0;
331}
332
333static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100334{
335 return 0;
336}
337
338static struct i2c_driver dummy_driver = {
339 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200340 .probe = dummy_probe,
341 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200342 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100343};
344
345/**
346 * i2c_new_dummy - return a new i2c device bound to a dummy driver
347 * @adapter: the adapter managing the device
348 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100349 * Context: can sleep
350 *
351 * This returns an I2C client bound to the "dummy" driver, intended for use
352 * with devices that consume multiple addresses. Examples of such chips
353 * include various EEPROMS (like 24c04 and 24c08 models).
354 *
355 * These dummy devices have two main uses. First, most I2C and SMBus calls
356 * except i2c_transfer() need a client handle; the dummy will be that handle.
357 * And second, this prevents the specified address from being bound to a
358 * different driver.
359 *
360 * This returns the new i2c client, which should be saved for later use with
361 * i2c_unregister_device(); or NULL to indicate an error.
362 */
363struct i2c_client *
Jean Delvare60b129d2008-05-11 20:37:06 +0200364i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100365{
366 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200367 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100368 };
369
David Brownelle9f13732008-01-27 18:14:52 +0100370 return i2c_new_device(adapter, &info);
371}
372EXPORT_SYMBOL_GPL(i2c_new_dummy);
373
David Brownellf37dd802007-02-13 22:09:00 +0100374/* ------------------------------------------------------------------------- */
375
David Brownell16ffadf2007-05-01 23:26:28 +0200376/* I2C bus adapters -- one roots each I2C or SMBUS segment */
377
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200378static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
David Brownellef2c8322007-05-01 23:26:28 +0200380 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 complete(&adap->dev_released);
382}
383
David Brownell16ffadf2007-05-01 23:26:28 +0200384static ssize_t
385show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
David Brownellef2c8322007-05-01 23:26:28 +0200387 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return sprintf(buf, "%s\n", adap->name);
389}
David Brownell16ffadf2007-05-01 23:26:28 +0200390
391static struct device_attribute i2c_adapter_attrs[] = {
392 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
393 { },
394};
395
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200396static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200397 .owner = THIS_MODULE,
398 .name = "i2c-adapter",
399 .dev_attrs = i2c_adapter_attrs,
400};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David Brownell9c1600e2007-05-01 23:26:31 +0200402static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
403{
404 struct i2c_devinfo *devinfo;
405
406 mutex_lock(&__i2c_board_lock);
407 list_for_each_entry(devinfo, &__i2c_board_list, list) {
408 if (devinfo->busnum == adapter->nr
409 && !i2c_new_device(adapter,
410 &devinfo->board_info))
411 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
412 i2c_adapter_id(adapter),
413 devinfo->board_info.addr);
414 }
415 mutex_unlock(&__i2c_board_lock);
416}
417
Jean Delvare026526f2008-01-27 18:14:49 +0100418static int i2c_do_add_adapter(struct device_driver *d, void *data)
419{
420 struct i2c_driver *driver = to_i2c_driver(d);
421 struct i2c_adapter *adap = data;
422
Jean Delvare4735c982008-07-14 22:38:36 +0200423 /* Detect supported devices on that bus, and instantiate them */
424 i2c_detect(adap, driver);
425
426 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100427 if (driver->attach_adapter) {
428 /* We ignore the return code; if it fails, too bad */
429 driver->attach_adapter(adap);
430 }
431 return 0;
432}
433
David Brownell6e13e642007-05-01 23:26:31 +0200434static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jean Delvare026526f2008-01-27 18:14:49 +0100436 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Ingo Molnar5c085d32006-01-18 23:16:04 +0100438 mutex_init(&adap->bus_lock);
439 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 INIT_LIST_HEAD(&adap->clients);
441
Jean Delvarecaada322008-01-27 18:14:49 +0100442 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Add the adapter to the driver core.
445 * If the parent pointer is not set up,
446 * we add this adapter to the host bus.
447 */
David Brownellb119dc32007-01-04 13:07:04 +0100448 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100450 pr_debug("I2C adapter driver [%s] forgot to specify "
451 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200455 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200456 res = device_register(&adap->dev);
457 if (res)
458 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200460 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
461
David Brownell6e13e642007-05-01 23:26:31 +0200462 /* create pre-declared device nodes for new-style drivers */
463 if (adap->nr < __i2c_first_dynamic_bus_num)
464 i2c_scan_static_board_info(adap);
465
Jean Delvare4735c982008-07-14 22:38:36 +0200466 /* Notify drivers */
Jean Delvare026526f2008-01-27 18:14:49 +0100467 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
468 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100471 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200473
Jean Delvareb119c6c2006-08-15 18:26:30 +0200474out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200475 idr_remove(&i2c_adapter_idr, adap->nr);
476 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
David Brownell6e13e642007-05-01 23:26:31 +0200479/**
480 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
481 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200482 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200483 *
484 * This routine is used to declare an I2C adapter when its bus number
485 * doesn't matter. Examples: for I2C adapters dynamically added by
486 * USB links or PCI plugin cards.
487 *
488 * When this returns zero, a new bus number was allocated and stored
489 * in adap->nr, and the specified adapter became available for clients.
490 * Otherwise, a negative errno value is returned.
491 */
492int i2c_add_adapter(struct i2c_adapter *adapter)
493{
494 int id, res = 0;
495
496retry:
497 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
498 return -ENOMEM;
499
Jean Delvarecaada322008-01-27 18:14:49 +0100500 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200501 /* "above" here means "above or equal to", sigh */
502 res = idr_get_new_above(&i2c_adapter_idr, adapter,
503 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100504 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200505
506 if (res < 0) {
507 if (res == -EAGAIN)
508 goto retry;
509 return res;
510 }
511
512 adapter->nr = id;
513 return i2c_register_adapter(adapter);
514}
515EXPORT_SYMBOL(i2c_add_adapter);
516
517/**
518 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
519 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200520 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200521 *
522 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100523 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
524 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200525 * is used to properly configure I2C devices.
526 *
527 * If no devices have pre-been declared for this bus, then be sure to
528 * register the adapter before any dynamically allocated ones. Otherwise
529 * the required bus ID may not be available.
530 *
531 * When this returns zero, the specified adapter became available for
532 * clients using the bus number provided in adap->nr. Also, the table
533 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
534 * and the appropriate driver model device nodes are created. Otherwise, a
535 * negative errno value is returned.
536 */
537int i2c_add_numbered_adapter(struct i2c_adapter *adap)
538{
539 int id;
540 int status;
541
542 if (adap->nr & ~MAX_ID_MASK)
543 return -EINVAL;
544
545retry:
546 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
547 return -ENOMEM;
548
Jean Delvarecaada322008-01-27 18:14:49 +0100549 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200550 /* "above" here means "above or equal to", sigh;
551 * we need the "equal to" result to force the result
552 */
553 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
554 if (status == 0 && id != adap->nr) {
555 status = -EBUSY;
556 idr_remove(&i2c_adapter_idr, id);
557 }
Jean Delvarecaada322008-01-27 18:14:49 +0100558 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200559 if (status == -EAGAIN)
560 goto retry;
561
562 if (status == 0)
563 status = i2c_register_adapter(adap);
564 return status;
565}
566EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
567
Jean Delvare026526f2008-01-27 18:14:49 +0100568static int i2c_do_del_adapter(struct device_driver *d, void *data)
569{
570 struct i2c_driver *driver = to_i2c_driver(d);
571 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200572 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100573 int res;
574
Jean Delvare4735c982008-07-14 22:38:36 +0200575 /* Remove the devices we created ourselves */
576 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
577 if (client->adapter == adapter) {
578 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
579 client->name, client->addr);
580 list_del(&client->detected);
581 i2c_unregister_device(client);
582 }
583 }
584
Jean Delvare026526f2008-01-27 18:14:49 +0100585 if (!driver->detach_adapter)
586 return 0;
587 res = driver->detach_adapter(adapter);
588 if (res)
589 dev_err(&adapter->dev, "detach_adapter failed (%d) "
590 "for driver [%s]\n", res, driver->driver.name);
591 return res;
592}
593
David Brownelld64f73b2007-07-12 14:12:28 +0200594/**
595 * i2c_del_adapter - unregister I2C adapter
596 * @adap: the adapter being unregistered
597 * Context: can sleep
598 *
599 * This unregisters an I2C adapter which was previously registered
600 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602int i2c_del_adapter(struct i2c_adapter *adap)
603{
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200604 struct i2c_client *client, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int res = 0;
606
Jean Delvarecaada322008-01-27 18:14:49 +0100607 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100610 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200611 pr_debug("i2c-core: attempting to delete unregistered "
612 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 res = -EINVAL;
614 goto out_unlock;
615 }
616
Jean Delvare026526f2008-01-27 18:14:49 +0100617 /* Tell drivers about this removal */
618 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
619 i2c_do_del_adapter);
620 if (res)
621 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200624 * it can fail; in which case we give up. */
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200625 list_for_each_entry_safe(client, _n, &adap->clients, list) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200626 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
David Brownella1d9e6e2007-05-01 23:26:30 +0200628 driver = client->driver;
629
630 /* new style, follow standard driver model */
631 if (!driver || is_newstyle_driver(driver)) {
632 i2c_unregister_device(client);
633 continue;
634 }
635
636 /* legacy drivers create and remove clients themselves */
637 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200638 dev_err(&adap->dev, "detach_client failed for client "
639 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 client->addr);
641 goto out_unlock;
642 }
643 }
644
645 /* clean up the sysfs representation */
646 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /* wait for sysfs to drop all references */
650 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
David Brownell6e13e642007-05-01 23:26:31 +0200652 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 idr_remove(&i2c_adapter_idr, adap->nr);
654
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200655 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jean Delvarebd4bc3d2008-07-16 19:30:05 +0200657 /* Clear the device structure in case this adapter is ever going to be
658 added again */
659 memset(&adap->dev, 0, sizeof(adap->dev));
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100662 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return res;
664}
David Brownellc0564602007-05-01 23:26:31 +0200665EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667
David Brownell7b4fbc52007-05-01 23:26:30 +0200668/* ------------------------------------------------------------------------- */
669
Dave Young7f101a92008-07-14 22:38:19 +0200670static int __attach_adapter(struct device *dev, void *data)
671{
672 struct i2c_adapter *adapter = to_i2c_adapter(dev);
673 struct i2c_driver *driver = data;
674
Jean Delvare4735c982008-07-14 22:38:36 +0200675 i2c_detect(adapter, driver);
676
677 /* Legacy drivers scan i2c busses directly */
678 if (driver->attach_adapter)
679 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200680
681 return 0;
682}
683
David Brownell7b4fbc52007-05-01 23:26:30 +0200684/*
685 * An i2c_driver is used with one or more i2c_client (device) nodes to access
686 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
687 * are two models for binding the driver to its device: "new style" drivers
688 * follow the standard Linux driver model and just respond to probe() calls
689 * issued if the driver core sees they match(); "legacy" drivers create device
690 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
692
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800693int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100695 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
David Brownell7b4fbc52007-05-01 23:26:30 +0200697 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200698 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200699 if (driver->attach_adapter || driver->detach_adapter
700 || driver->detach_client) {
701 printk(KERN_WARNING
702 "i2c-core: driver [%s] is confused\n",
703 driver->driver.name);
704 return -EINVAL;
705 }
706 }
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800709 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
David Brownell6e13e642007-05-01 23:26:31 +0200712 /* for new style drivers, when registration returns the driver core
713 * will have called probe() for all matching-but-unbound devices.
714 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 res = driver_register(&driver->driver);
716 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100717 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100718
Jean Delvarecaada322008-01-27 18:14:49 +0100719 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100720
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100721 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Jean Delvare4735c982008-07-14 22:38:36 +0200723 INIT_LIST_HEAD(&driver->clients);
724 /* Walk the adapters that are already present */
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400725 class_for_each_device(&i2c_adapter_class, NULL, driver,
726 __attach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jean Delvarecaada322008-01-27 18:14:49 +0100728 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800731EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dave Young7f101a92008-07-14 22:38:19 +0200733static int __detach_adapter(struct device *dev, void *data)
734{
735 struct i2c_adapter *adapter = to_i2c_adapter(dev);
736 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200737 struct i2c_client *client, *_n;
738
739 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
740 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
741 client->name, client->addr);
742 list_del(&client->detected);
743 i2c_unregister_device(client);
744 }
745
746 if (is_newstyle_driver(driver))
747 return 0;
Dave Young7f101a92008-07-14 22:38:19 +0200748
749 /* Have a look at each adapter, if clients of this driver are still
750 * attached. If so, detach them to be able to kill the driver
751 * afterwards.
752 */
753 if (driver->detach_adapter) {
754 if (driver->detach_adapter(adapter))
755 dev_err(&adapter->dev,
756 "detach_adapter failed for driver [%s]\n",
757 driver->driver.name);
758 } else {
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200759 struct i2c_client *client, *_n;
Dave Young7f101a92008-07-14 22:38:19 +0200760
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200761 list_for_each_entry_safe(client, _n, &adapter->clients, list) {
Dave Young7f101a92008-07-14 22:38:19 +0200762 if (client->driver != driver)
763 continue;
764 dev_dbg(&adapter->dev,
765 "detaching client [%s] at 0x%02x\n",
766 client->name, client->addr);
767 if (driver->detach_client(client))
768 dev_err(&adapter->dev, "detach_client "
769 "failed for client [%s] at 0x%02x\n",
770 client->name, client->addr);
771 }
772 }
773
774 return 0;
775}
776
David Brownella1d9e6e2007-05-01 23:26:30 +0200777/**
778 * i2c_del_driver - unregister I2C driver
779 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200780 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200781 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200782void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Jean Delvarecaada322008-01-27 18:14:49 +0100784 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400786 class_for_each_device(&i2c_adapter_class, NULL, driver,
787 __detach_adapter);
David Brownella1d9e6e2007-05-01 23:26:30 +0200788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100790 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Jean Delvarecaada322008-01-27 18:14:49 +0100792 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
David Brownellc0564602007-05-01 23:26:31 +0200794EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
David Brownell7b4fbc52007-05-01 23:26:30 +0200796/* ------------------------------------------------------------------------- */
797
David Brownell9b766b82008-01-27 18:14:51 +0100798static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
David Brownell9b766b82008-01-27 18:14:51 +0100800 struct i2c_client *client = i2c_verify_client(dev);
801 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
David Brownell9b766b82008-01-27 18:14:51 +0100803 if (client && client->addr == addr)
804 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return 0;
806}
807
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100808static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
David Brownell9b766b82008-01-27 18:14:51 +0100810 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813int i2c_attach_client(struct i2c_client *client)
814{
815 struct i2c_adapter *adapter = client->adapter;
Jean Delvarec1159f92008-08-10 22:56:16 +0200816 int res;
817
818 /* Check for address business */
819 res = i2c_check_addr(adapter, client->addr);
820 if (res)
821 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200825
826 if (client->driver)
827 client->dev.driver = &client->driver->driver;
828
David Brownellde81d2a2007-05-22 19:49:16 +0200829 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200830 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200831 client->dev.uevent_suppress = 1;
832 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200833 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
836 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200837 res = device_register(&client->dev);
838 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100839 goto out_err;
840
841 mutex_lock(&adapter->clist_lock);
842 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200843 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200844
David Brownell86ec5ec2008-01-27 18:14:51 +0100845 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
846 client->name, client->dev.bus_id);
847
Jean Delvare77ed74d2006-09-30 17:18:59 +0200848 if (adapter->client_register) {
849 if (adapter->client_register(client)) {
850 dev_dbg(&adapter->dev, "client_register "
851 "failed for client [%s] at 0x%02x\n",
852 client->name, client->addr);
853 }
854 }
855
856 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200857
David Brownell86ec5ec2008-01-27 18:14:51 +0100858out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200859 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
860 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200861 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
David Brownellc0564602007-05-01 23:26:31 +0200863EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865int i2c_detach_client(struct i2c_client *client)
866{
867 struct i2c_adapter *adapter = client->adapter;
868 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (adapter->client_unregister) {
871 res = adapter->client_unregister(client);
872 if (res) {
873 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700874 "client_unregister [%s] failed, "
875 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto out;
877 }
878 }
879
Ingo Molnar5c085d32006-01-18 23:16:04 +0100880 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 list_del(&client->list);
Jean Delvare9ddced12008-01-27 18:14:51 +0100882 mutex_unlock(&adapter->clist_lock);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 device_unregister(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 wait_for_completion(&client->released);
887
888 out:
889 return res;
890}
David Brownellc0564602007-05-01 23:26:31 +0200891EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Jean Delvaree48d3312008-01-27 18:14:48 +0100893/**
894 * i2c_use_client - increments the reference count of the i2c client structure
895 * @client: the client being referenced
896 *
897 * Each live reference to a client should be refcounted. The driver model does
898 * that automatically as part of driver binding, so that most drivers don't
899 * need to do this explicitly: they hold a reference until they're unbound
900 * from the device.
901 *
902 * A pointer to the client with the incremented reference counter is returned.
903 */
904struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
David Brownell6ea438e2008-07-14 22:38:24 +0200906 if (client && get_device(&client->dev))
907 return client;
908 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
David Brownellc0564602007-05-01 23:26:31 +0200910EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Jean Delvaree48d3312008-01-27 18:14:48 +0100912/**
913 * i2c_release_client - release a use of the i2c client structure
914 * @client: the client being no longer referenced
915 *
916 * Must be called when a user of a client is finished with it.
917 */
918void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
David Brownell6ea438e2008-07-14 22:38:24 +0200920 if (client)
921 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
David Brownellc0564602007-05-01 23:26:31 +0200923EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
David Brownell9b766b82008-01-27 18:14:51 +0100925struct i2c_cmd_arg {
926 unsigned cmd;
927 void *arg;
928};
929
930static int i2c_cmd(struct device *dev, void *_arg)
931{
932 struct i2c_client *client = i2c_verify_client(dev);
933 struct i2c_cmd_arg *arg = _arg;
934
935 if (client && client->driver && client->driver->command)
936 client->driver->command(client, arg->cmd, arg->arg);
937 return 0;
938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
941{
David Brownell9b766b82008-01-27 18:14:51 +0100942 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
David Brownell9b766b82008-01-27 18:14:51 +0100944 cmd_arg.cmd = cmd;
945 cmd_arg.arg = arg;
946 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
David Brownellc0564602007-05-01 23:26:31 +0200948EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950static int __init i2c_init(void)
951{
952 int retval;
953
954 retval = bus_register(&i2c_bus_type);
955 if (retval)
956 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100957 retval = class_register(&i2c_adapter_class);
958 if (retval)
959 goto bus_err;
960 retval = i2c_add_driver(&dummy_driver);
961 if (retval)
962 goto class_err;
963 return 0;
964
965class_err:
966 class_unregister(&i2c_adapter_class);
967bus_err:
968 bus_unregister(&i2c_bus_type);
969 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972static void __exit i2c_exit(void)
973{
David Brownelle9f13732008-01-27 18:14:52 +0100974 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 bus_unregister(&i2c_bus_type);
977}
978
979subsys_initcall(i2c_init);
980module_exit(i2c_exit);
981
982/* ----------------------------------------------------
983 * the functional interface to the i2c busses.
984 * ----------------------------------------------------
985 */
986
David Brownella1cdeda2008-07-14 22:38:24 +0200987/**
988 * i2c_transfer - execute a single or combined I2C message
989 * @adap: Handle to I2C bus
990 * @msgs: One or more messages to execute before STOP is issued to
991 * terminate the operation; each message begins with a START.
992 * @num: Number of messages to be executed.
993 *
994 * Returns negative errno, else the number of messages executed.
995 *
996 * Note that there is no requirement that each message be sent to
997 * the same slave address, although that is the most common model.
998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
1000{
1001 int ret;
1002
David Brownella1cdeda2008-07-14 22:38:24 +02001003 /* REVISIT the fault reporting model here is weak:
1004 *
1005 * - When we get an error after receiving N bytes from a slave,
1006 * there is no way to report "N".
1007 *
1008 * - When we get a NAK after transmitting N bytes to a slave,
1009 * there is no way to report "N" ... or to let the master
1010 * continue executing the rest of this combined message, if
1011 * that's the appropriate response.
1012 *
1013 * - When for example "num" is two and we successfully complete
1014 * the first message but get an error part way through the
1015 * second, it's unclear whether that should be reported as
1016 * one (discarding status on the second message) or errno
1017 * (discarding status on the first one).
1018 */
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (adap->algo->master_xfer) {
1021#ifdef DEBUG
1022 for (ret = 0; ret < num; ret++) {
1023 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001024 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1025 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1026 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028#endif
1029
Mike Rapoportcea443a2008-01-27 18:14:50 +01001030 if (in_atomic() || irqs_disabled()) {
1031 ret = mutex_trylock(&adap->bus_lock);
1032 if (!ret)
1033 /* I2C activity is ongoing. */
1034 return -EAGAIN;
1035 } else {
1036 mutex_lock_nested(&adap->bus_lock, adap->level);
1037 }
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001040 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 return ret;
1043 } else {
1044 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001045 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047}
David Brownellc0564602007-05-01 23:26:31 +02001048EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
David Brownella1cdeda2008-07-14 22:38:24 +02001050/**
1051 * i2c_master_send - issue a single I2C message in master transmit mode
1052 * @client: Handle to slave device
1053 * @buf: Data that will be written to the slave
1054 * @count: How many bytes to write
1055 *
1056 * Returns negative errno, or else the number of bytes written.
1057 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1059{
1060 int ret;
1061 struct i2c_adapter *adap=client->adapter;
1062 struct i2c_msg msg;
1063
Jean Delvare815f55f2005-05-07 22:58:46 +02001064 msg.addr = client->addr;
1065 msg.flags = client->flags & I2C_M_TEN;
1066 msg.len = count;
1067 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001068
Jean Delvare815f55f2005-05-07 22:58:46 +02001069 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Jean Delvare815f55f2005-05-07 22:58:46 +02001071 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1072 transmitted, else error code. */
1073 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
David Brownellc0564602007-05-01 23:26:31 +02001075EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
David Brownella1cdeda2008-07-14 22:38:24 +02001077/**
1078 * i2c_master_recv - issue a single I2C message in master receive mode
1079 * @client: Handle to slave device
1080 * @buf: Where to store data read from slave
1081 * @count: How many bytes to read
1082 *
1083 * Returns negative errno, or else the number of bytes read.
1084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1086{
1087 struct i2c_adapter *adap=client->adapter;
1088 struct i2c_msg msg;
1089 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Jean Delvare815f55f2005-05-07 22:58:46 +02001091 msg.addr = client->addr;
1092 msg.flags = client->flags & I2C_M_TEN;
1093 msg.flags |= I2C_M_RD;
1094 msg.len = count;
1095 msg.buf = buf;
1096
1097 ret = i2c_transfer(adap, &msg, 1);
1098
1099 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1100 transmitted, else error code. */
1101 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
David Brownellc0564602007-05-01 23:26:31 +02001103EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/* ----------------------------------------------------
1106 * the i2c address scanning function
1107 * Will not work for 10-bit addresses!
1108 * ----------------------------------------------------
1109 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001110static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1111 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001112{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001113 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001114
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001115 /* Make sure the address is valid */
1116 if (addr < 0x03 || addr > 0x77) {
1117 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1118 addr);
1119 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001120 }
1121
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001122 /* Skip if already in use */
1123 if (i2c_check_addr(adapter, addr))
1124 return 0;
1125
1126 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +02001127 if (kind < 0) {
1128 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1129 I2C_SMBUS_QUICK, NULL) < 0)
1130 return 0;
1131
1132 /* prevent 24RF08 corruption */
1133 if ((addr & ~0x0f) == 0x50)
1134 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1135 I2C_SMBUS_QUICK, NULL);
1136 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001137
1138 /* Finally call the custom detection function */
1139 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001140 /* -ENODEV can be returned if there is a chip at the given address
1141 but it isn't supported by this chip driver. We catch it here as
1142 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +02001143 if (err == -ENODEV)
1144 err = 0;
1145
1146 if (err)
1147 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1148 addr, err);
1149 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001153 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int (*found_proc) (struct i2c_adapter *, int, int))
1155{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001156 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int adap_id = i2c_adapter_id(adapter);
1158
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001159 /* Force entries are done first, and are not affected by ignore
1160 entries */
1161 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001162 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001163 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001165 for (kind = 0; forces[kind]; kind++) {
1166 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1167 i += 2) {
1168 if (forces[kind][i] == adap_id
1169 || forces[kind][i] == ANY_I2C_BUS) {
1170 dev_dbg(&adapter->dev, "found force "
1171 "parameter for adapter %d, "
1172 "addr 0x%02x, kind %d\n",
1173 adap_id, forces[kind][i + 1],
1174 kind);
1175 err = i2c_probe_address(adapter,
1176 forces[kind][i + 1],
1177 kind, found_proc);
1178 if (err)
1179 return err;
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001184
Jean Delvare4366dc92005-09-25 16:50:06 +02001185 /* Stop here if we can't use SMBUS_QUICK */
1186 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1187 if (address_data->probe[0] == I2C_CLIENT_END
1188 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001189 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001190
1191 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1192 "can't probe for chips\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001193 return -EOPNOTSUPP;
Jean Delvare4366dc92005-09-25 16:50:06 +02001194 }
1195
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001196 /* Probe entries are done second, and are not affected by ignore
1197 entries either */
1198 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1199 if (address_data->probe[i] == adap_id
1200 || address_data->probe[i] == ANY_I2C_BUS) {
1201 dev_dbg(&adapter->dev, "found probe parameter for "
1202 "adapter %d, addr 0x%02x\n", adap_id,
1203 address_data->probe[i + 1]);
1204 err = i2c_probe_address(adapter,
1205 address_data->probe[i + 1],
1206 -1, found_proc);
1207 if (err)
1208 return err;
1209 }
1210 }
1211
1212 /* Normal entries are done last, unless shadowed by an ignore entry */
1213 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1214 int j, ignore;
1215
1216 ignore = 0;
1217 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1218 j += 2) {
1219 if ((address_data->ignore[j] == adap_id ||
1220 address_data->ignore[j] == ANY_I2C_BUS)
1221 && address_data->ignore[j + 1]
1222 == address_data->normal_i2c[i]) {
1223 dev_dbg(&adapter->dev, "found ignore "
1224 "parameter for adapter %d, "
1225 "addr 0x%02x\n", adap_id,
1226 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001227 ignore = 1;
1228 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001229 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001230 }
1231 if (ignore)
1232 continue;
1233
1234 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1235 "addr 0x%02x\n", adap_id,
1236 address_data->normal_i2c[i]);
1237 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1238 -1, found_proc);
1239 if (err)
1240 return err;
1241 }
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return 0;
1244}
David Brownellc0564602007-05-01 23:26:31 +02001245EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Jean Delvare4735c982008-07-14 22:38:36 +02001247/* Separate detection function for new-style drivers */
1248static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1249 struct i2c_driver *driver)
1250{
1251 struct i2c_board_info info;
1252 struct i2c_adapter *adapter = temp_client->adapter;
1253 int addr = temp_client->addr;
1254 int err;
1255
1256 /* Make sure the address is valid */
1257 if (addr < 0x03 || addr > 0x77) {
1258 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1259 addr);
1260 return -EINVAL;
1261 }
1262
1263 /* Skip if already in use */
1264 if (i2c_check_addr(adapter, addr))
1265 return 0;
1266
1267 /* Make sure there is something at this address, unless forced */
1268 if (kind < 0) {
1269 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1270 I2C_SMBUS_QUICK, NULL) < 0)
1271 return 0;
1272
1273 /* prevent 24RF08 corruption */
1274 if ((addr & ~0x0f) == 0x50)
1275 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1276 I2C_SMBUS_QUICK, NULL);
1277 }
1278
1279 /* Finally call the custom detection function */
1280 memset(&info, 0, sizeof(struct i2c_board_info));
1281 info.addr = addr;
1282 err = driver->detect(temp_client, kind, &info);
1283 if (err) {
1284 /* -ENODEV is returned if the detection fails. We catch it
1285 here as this isn't an error. */
1286 return err == -ENODEV ? 0 : err;
1287 }
1288
1289 /* Consistency check */
1290 if (info.type[0] == '\0') {
1291 dev_err(&adapter->dev, "%s detection function provided "
1292 "no name for 0x%x\n", driver->driver.name,
1293 addr);
1294 } else {
1295 struct i2c_client *client;
1296
1297 /* Detection succeeded, instantiate the device */
1298 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1299 info.type, info.addr);
1300 client = i2c_new_device(adapter, &info);
1301 if (client)
1302 list_add_tail(&client->detected, &driver->clients);
1303 else
1304 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1305 info.type, info.addr);
1306 }
1307 return 0;
1308}
1309
1310static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1311{
1312 const struct i2c_client_address_data *address_data;
1313 struct i2c_client *temp_client;
1314 int i, err = 0;
1315 int adap_id = i2c_adapter_id(adapter);
1316
1317 address_data = driver->address_data;
1318 if (!driver->detect || !address_data)
1319 return 0;
1320
1321 /* Set up a temporary client to help detect callback */
1322 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1323 if (!temp_client)
1324 return -ENOMEM;
1325 temp_client->adapter = adapter;
1326
1327 /* Force entries are done first, and are not affected by ignore
1328 entries */
1329 if (address_data->forces) {
1330 const unsigned short * const *forces = address_data->forces;
1331 int kind;
1332
1333 for (kind = 0; forces[kind]; kind++) {
1334 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1335 i += 2) {
1336 if (forces[kind][i] == adap_id
1337 || forces[kind][i] == ANY_I2C_BUS) {
1338 dev_dbg(&adapter->dev, "found force "
1339 "parameter for adapter %d, "
1340 "addr 0x%02x, kind %d\n",
1341 adap_id, forces[kind][i + 1],
1342 kind);
1343 temp_client->addr = forces[kind][i + 1];
1344 err = i2c_detect_address(temp_client,
1345 kind, driver);
1346 if (err)
1347 goto exit_free;
1348 }
1349 }
1350 }
1351 }
1352
1353 /* Stop here if we can't use SMBUS_QUICK */
1354 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1355 if (address_data->probe[0] == I2C_CLIENT_END
1356 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1357 goto exit_free;
1358
1359 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1360 "can't probe for chips\n");
1361 err = -EOPNOTSUPP;
1362 goto exit_free;
1363 }
1364
1365 /* Stop here if the classes do not match */
1366 if (!(adapter->class & driver->class))
1367 goto exit_free;
1368
1369 /* Probe entries are done second, and are not affected by ignore
1370 entries either */
1371 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1372 if (address_data->probe[i] == adap_id
1373 || address_data->probe[i] == ANY_I2C_BUS) {
1374 dev_dbg(&adapter->dev, "found probe parameter for "
1375 "adapter %d, addr 0x%02x\n", adap_id,
1376 address_data->probe[i + 1]);
1377 temp_client->addr = address_data->probe[i + 1];
1378 err = i2c_detect_address(temp_client, -1, driver);
1379 if (err)
1380 goto exit_free;
1381 }
1382 }
1383
1384 /* Normal entries are done last, unless shadowed by an ignore entry */
1385 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1386 int j, ignore;
1387
1388 ignore = 0;
1389 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1390 j += 2) {
1391 if ((address_data->ignore[j] == adap_id ||
1392 address_data->ignore[j] == ANY_I2C_BUS)
1393 && address_data->ignore[j + 1]
1394 == address_data->normal_i2c[i]) {
1395 dev_dbg(&adapter->dev, "found ignore "
1396 "parameter for adapter %d, "
1397 "addr 0x%02x\n", adap_id,
1398 address_data->ignore[j + 1]);
1399 ignore = 1;
1400 break;
1401 }
1402 }
1403 if (ignore)
1404 continue;
1405
1406 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1407 "addr 0x%02x\n", adap_id,
1408 address_data->normal_i2c[i]);
1409 temp_client->addr = address_data->normal_i2c[i];
1410 err = i2c_detect_address(temp_client, -1, driver);
1411 if (err)
1412 goto exit_free;
1413 }
1414
1415 exit_free:
1416 kfree(temp_client);
1417 return err;
1418}
1419
Jean Delvare12b50532007-05-01 23:26:31 +02001420struct i2c_client *
1421i2c_new_probed_device(struct i2c_adapter *adap,
1422 struct i2c_board_info *info,
1423 unsigned short const *addr_list)
1424{
1425 int i;
1426
1427 /* Stop here if the bus doesn't support probing */
1428 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1429 dev_err(&adap->dev, "Probing not supported\n");
1430 return NULL;
1431 }
1432
Jean Delvare12b50532007-05-01 23:26:31 +02001433 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1434 /* Check address validity */
1435 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1436 dev_warn(&adap->dev, "Invalid 7-bit address "
1437 "0x%02x\n", addr_list[i]);
1438 continue;
1439 }
1440
1441 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001442 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b50532007-05-01 23:26:31 +02001443 dev_dbg(&adap->dev, "Address 0x%02x already in "
1444 "use, not probing\n", addr_list[i]);
1445 continue;
1446 }
1447
1448 /* Test address responsiveness
1449 The default probe method is a quick write, but it is known
1450 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1451 and could also irreversibly write-protect some EEPROMs, so
1452 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1453 read instead. Also, some bus drivers don't implement
1454 quick write, so we fallback to a byte read it that case
1455 too. */
1456 if ((addr_list[i] & ~0x07) == 0x30
1457 || (addr_list[i] & ~0x0f) == 0x50
1458 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001459 union i2c_smbus_data data;
1460
Jean Delvare12b50532007-05-01 23:26:31 +02001461 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1462 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001463 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b50532007-05-01 23:26:31 +02001464 break;
1465 } else {
1466 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1467 I2C_SMBUS_WRITE, 0,
1468 I2C_SMBUS_QUICK, NULL) >= 0)
1469 break;
1470 }
1471 }
Jean Delvare12b50532007-05-01 23:26:31 +02001472
1473 if (addr_list[i] == I2C_CLIENT_END) {
1474 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1475 return NULL;
1476 }
1477
1478 info->addr = addr_list[i];
1479 return i2c_new_device(adap, info);
1480}
1481EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483struct i2c_adapter* i2c_get_adapter(int id)
1484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001486
Jean Delvarecaada322008-01-27 18:14:49 +01001487 mutex_lock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001488 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1489 if (adapter && !try_module_get(adapter->owner))
1490 adapter = NULL;
1491
Jean Delvarecaada322008-01-27 18:14:49 +01001492 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001493 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
David Brownellc0564602007-05-01 23:26:31 +02001495EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497void i2c_put_adapter(struct i2c_adapter *adap)
1498{
1499 module_put(adap->owner);
1500}
David Brownellc0564602007-05-01 23:26:31 +02001501EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503/* The SMBus parts */
1504
David Brownell438d6c22006-12-10 21:21:31 +01001505#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506static u8
1507crc8(u16 data)
1508{
1509 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001512 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 data = data ^ POLY;
1514 data = data << 1;
1515 }
1516 return (u8)(data >> 8);
1517}
1518
Jean Delvare421ef472005-10-26 21:28:55 +02001519/* Incremental CRC8 over count bytes in the array pointed to by p */
1520static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 int i;
1523
1524 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001525 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return crc;
1527}
1528
Jean Delvare421ef472005-10-26 21:28:55 +02001529/* Assume a 7-bit address, which is reasonable for SMBus */
1530static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Jean Delvare421ef472005-10-26 21:28:55 +02001532 /* The address will be sent first */
1533 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1534 pec = i2c_smbus_pec(pec, &addr, 1);
1535
1536 /* The data buffer follows */
1537 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Jean Delvare421ef472005-10-26 21:28:55 +02001540/* Used for write only transactions */
1541static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Jean Delvare421ef472005-10-26 21:28:55 +02001543 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1544 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
Jean Delvare421ef472005-10-26 21:28:55 +02001547/* Return <0 on CRC error
1548 If there was a write before this read (most cases) we need to take the
1549 partial CRC from the write part into account.
1550 Note that this function does modify the message (we need to decrease the
1551 message length to hide the CRC byte from the caller). */
1552static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Jean Delvare421ef472005-10-26 21:28:55 +02001554 u8 rpec = msg->buf[--msg->len];
1555 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (rpec != cpec) {
1558 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1559 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001560 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
David Brownell438d6c22006-12-10 21:21:31 +01001562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
David Brownella1cdeda2008-07-14 22:38:24 +02001565/**
1566 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1567 * @client: Handle to slave device
1568 *
1569 * This executes the SMBus "receive byte" protocol, returning negative errno
1570 * else the byte received from the device.
1571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572s32 i2c_smbus_read_byte(struct i2c_client *client)
1573{
1574 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001575 int status;
1576
1577 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1578 I2C_SMBUS_READ, 0,
1579 I2C_SMBUS_BYTE, &data);
1580 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
David Brownellc0564602007-05-01 23:26:31 +02001582EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
David Brownella1cdeda2008-07-14 22:38:24 +02001584/**
1585 * i2c_smbus_write_byte - SMBus "send byte" protocol
1586 * @client: Handle to slave device
1587 * @value: Byte to be sent
1588 *
1589 * This executes the SMBus "send byte" protocol, returning negative errno
1590 * else zero on success.
1591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001595 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
David Brownellc0564602007-05-01 23:26:31 +02001597EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
David Brownella1cdeda2008-07-14 22:38:24 +02001599/**
1600 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1601 * @client: Handle to slave device
1602 * @command: Byte interpreted by slave
1603 *
1604 * This executes the SMBus "read byte" protocol, returning negative errno
1605 * else a data byte received from the device.
1606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1608{
1609 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001610 int status;
1611
1612 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1613 I2C_SMBUS_READ, command,
1614 I2C_SMBUS_BYTE_DATA, &data);
1615 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
David Brownellc0564602007-05-01 23:26:31 +02001617EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
David Brownella1cdeda2008-07-14 22:38:24 +02001619/**
1620 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1621 * @client: Handle to slave device
1622 * @command: Byte interpreted by slave
1623 * @value: Byte being written
1624 *
1625 * This executes the SMBus "write byte" protocol, returning negative errno
1626 * else zero on success.
1627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1629{
1630 union i2c_smbus_data data;
1631 data.byte = value;
1632 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1633 I2C_SMBUS_WRITE,command,
1634 I2C_SMBUS_BYTE_DATA,&data);
1635}
David Brownellc0564602007-05-01 23:26:31 +02001636EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
David Brownella1cdeda2008-07-14 22:38:24 +02001638/**
1639 * i2c_smbus_read_word_data - SMBus "read word" protocol
1640 * @client: Handle to slave device
1641 * @command: Byte interpreted by slave
1642 *
1643 * This executes the SMBus "read word" protocol, returning negative errno
1644 * else a 16-bit unsigned "word" received from the device.
1645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1647{
1648 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001649 int status;
1650
1651 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1652 I2C_SMBUS_READ, command,
1653 I2C_SMBUS_WORD_DATA, &data);
1654 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
David Brownellc0564602007-05-01 23:26:31 +02001656EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
David Brownella1cdeda2008-07-14 22:38:24 +02001658/**
1659 * i2c_smbus_write_word_data - SMBus "write word" protocol
1660 * @client: Handle to slave device
1661 * @command: Byte interpreted by slave
1662 * @value: 16-bit "word" being written
1663 *
1664 * This executes the SMBus "write word" protocol, returning negative errno
1665 * else zero on success.
1666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1668{
1669 union i2c_smbus_data data;
1670 data.word = value;
1671 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1672 I2C_SMBUS_WRITE,command,
1673 I2C_SMBUS_WORD_DATA,&data);
1674}
David Brownellc0564602007-05-01 23:26:31 +02001675EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
David Brownella64ec072007-10-13 23:56:31 +02001677/**
David Brownella1cdeda2008-07-14 22:38:24 +02001678 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001679 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001680 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001681 * @values: Byte array into which data will be read; big enough to hold
1682 * the data returned by the slave. SMBus allows at most 32 bytes.
1683 *
David Brownella1cdeda2008-07-14 22:38:24 +02001684 * This executes the SMBus "block read" protocol, returning negative errno
1685 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001686 *
1687 * Note that using this function requires that the client's adapter support
1688 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1689 * support this; its emulation through I2C messaging relies on a specific
1690 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1691 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001692s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1693 u8 *values)
1694{
1695 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001696 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001697
David Brownell24a5bb72008-07-14 22:38:23 +02001698 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1699 I2C_SMBUS_READ, command,
1700 I2C_SMBUS_BLOCK_DATA, &data);
1701 if (status)
1702 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001703
1704 memcpy(values, &data.block[1], data.block[0]);
1705 return data.block[0];
1706}
1707EXPORT_SYMBOL(i2c_smbus_read_block_data);
1708
David Brownella1cdeda2008-07-14 22:38:24 +02001709/**
1710 * i2c_smbus_write_block_data - SMBus "block write" protocol
1711 * @client: Handle to slave device
1712 * @command: Byte interpreted by slave
1713 * @length: Size of data block; SMBus allows at most 32 bytes
1714 * @values: Byte array which will be written.
1715 *
1716 * This executes the SMBus "block write" protocol, returning negative errno
1717 * else zero on success.
1718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001720 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (length > I2C_SMBUS_BLOCK_MAX)
1725 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001727 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1729 I2C_SMBUS_WRITE,command,
1730 I2C_SMBUS_BLOCK_DATA,&data);
1731}
David Brownellc0564602007-05-01 23:26:31 +02001732EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001735s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1736 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001739 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001740
Jean Delvare4b2643d2007-07-12 14:12:29 +02001741 if (length > I2C_SMBUS_BLOCK_MAX)
1742 length = I2C_SMBUS_BLOCK_MAX;
1743 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001744 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1745 I2C_SMBUS_READ, command,
1746 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1747 if (status < 0)
1748 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001749
1750 memcpy(values, &data.block[1], data.block[0]);
1751 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
David Brownellc0564602007-05-01 23:26:31 +02001753EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Jean Delvare21bbd692006-01-09 15:19:18 +11001755s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001756 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001757{
1758 union i2c_smbus_data data;
1759
1760 if (length > I2C_SMBUS_BLOCK_MAX)
1761 length = I2C_SMBUS_BLOCK_MAX;
1762 data.block[0] = length;
1763 memcpy(data.block + 1, values, length);
1764 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1765 I2C_SMBUS_WRITE, command,
1766 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1767}
David Brownellc0564602007-05-01 23:26:31 +02001768EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001769
David Brownell438d6c22006-12-10 21:21:31 +01001770/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001772static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001774 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 union i2c_smbus_data * data)
1776{
1777 /* So we need to generate a series of msgs. In the case of writing, we
1778 need to use only one message; when reading, we need two. We initialize
1779 most things with sane defaults, to keep the code below somewhat
1780 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001781 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1782 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001784 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1786 };
1787 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001788 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001789 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 msgbuf0[0] = command;
1792 switch(size) {
1793 case I2C_SMBUS_QUICK:
1794 msg[0].len = 0;
1795 /* Special case: The read/write field is used as data */
1796 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1797 num = 1;
1798 break;
1799 case I2C_SMBUS_BYTE:
1800 if (read_write == I2C_SMBUS_READ) {
1801 /* Special case: only a read! */
1802 msg[0].flags = I2C_M_RD | flags;
1803 num = 1;
1804 }
1805 break;
1806 case I2C_SMBUS_BYTE_DATA:
1807 if (read_write == I2C_SMBUS_READ)
1808 msg[1].len = 1;
1809 else {
1810 msg[0].len = 2;
1811 msgbuf0[1] = data->byte;
1812 }
1813 break;
1814 case I2C_SMBUS_WORD_DATA:
1815 if (read_write == I2C_SMBUS_READ)
1816 msg[1].len = 2;
1817 else {
1818 msg[0].len=3;
1819 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001820 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822 break;
1823 case I2C_SMBUS_PROC_CALL:
1824 num = 2; /* Special case */
1825 read_write = I2C_SMBUS_READ;
1826 msg[0].len = 3;
1827 msg[1].len = 2;
1828 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001829 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 break;
1831 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001833 msg[1].flags |= I2C_M_RECV_LEN;
1834 msg[1].len = 1; /* block length will be added by
1835 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 } else {
1837 msg[0].len = data->block[0] + 2;
1838 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001839 dev_err(&adapter->dev,
1840 "Invalid block write size %d\n",
1841 data->block[0]);
1842 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001844 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 msgbuf0[i] = data->block[i-1];
1846 }
1847 break;
1848 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001849 num = 2; /* Another special case */
1850 read_write = I2C_SMBUS_READ;
1851 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001852 dev_err(&adapter->dev,
1853 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001854 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001855 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001856 }
1857 msg[0].len = data->block[0] + 2;
1858 for (i = 1; i < msg[0].len; i++)
1859 msgbuf0[i] = data->block[i-1];
1860 msg[1].flags |= I2C_M_RECV_LEN;
1861 msg[1].len = 1; /* block length will be added by
1862 the underlying bus driver */
1863 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 case I2C_SMBUS_I2C_BLOCK_DATA:
1865 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001866 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 } else {
1868 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001869 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001870 dev_err(&adapter->dev,
1871 "Invalid block write size %d\n",
1872 data->block[0]);
1873 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875 for (i = 1; i <= data->block[0]; i++)
1876 msgbuf0[i] = data->block[i];
1877 }
1878 break;
1879 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001880 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1881 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
Jean Delvare421ef472005-10-26 21:28:55 +02001884 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1885 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1886 if (i) {
1887 /* Compute PEC if first message is a write */
1888 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001889 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001890 i2c_smbus_add_pec(&msg[0]);
1891 else /* Write followed by read */
1892 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1893 }
1894 /* Ask for PEC if last message is a read */
1895 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001896 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001897 }
1898
David Brownell24a5bb72008-07-14 22:38:23 +02001899 status = i2c_transfer(adapter, msg, num);
1900 if (status < 0)
1901 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Jean Delvare421ef472005-10-26 21:28:55 +02001903 /* Check PEC if last message is a read */
1904 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001905 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1906 if (status < 0)
1907 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001908 }
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (read_write == I2C_SMBUS_READ)
1911 switch(size) {
1912 case I2C_SMBUS_BYTE:
1913 data->byte = msgbuf0[0];
1914 break;
1915 case I2C_SMBUS_BYTE_DATA:
1916 data->byte = msgbuf1[0];
1917 break;
David Brownell438d6c22006-12-10 21:21:31 +01001918 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 case I2C_SMBUS_PROC_CALL:
1920 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1921 break;
1922 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001923 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 data->block[i+1] = msgbuf1[i];
1925 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001926 case I2C_SMBUS_BLOCK_DATA:
1927 case I2C_SMBUS_BLOCK_PROC_CALL:
1928 for (i = 0; i < msgbuf1[0] + 1; i++)
1929 data->block[i] = msgbuf1[i];
1930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932 return 0;
1933}
1934
David Brownella1cdeda2008-07-14 22:38:24 +02001935/**
1936 * i2c_smbus_xfer - execute SMBus protocol operations
1937 * @adapter: Handle to I2C bus
1938 * @addr: Address of SMBus slave on that bus
1939 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1940 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1941 * @command: Byte interpreted by slave, for protocols which use such bytes
1942 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1943 * @data: Data to be read or written
1944 *
1945 * This executes an SMBus protocol operation, and returns a negative
1946 * errno code else zero on success.
1947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001949 char read_write, u8 command, int protocol,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 union i2c_smbus_data * data)
1951{
1952 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001957 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001959 command, protocol, data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001960 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 } else
1962 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001963 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return res;
1966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1970MODULE_DESCRIPTION("I2C-Bus main module");
1971MODULE_LICENSE("GPL");