blob: 26384daccb968180c369207e70f35e0a3598e04d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
32#include <linux/seq_file.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010034#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010035#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010036#include <linux/hardirq.h>
37#include <linux/irqflags.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040038#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40
David Brownell9c1600e2007-05-01 23:26:31 +020041#include "i2c-core.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jean Delvarecaada322008-01-27 18:14:49 +010044static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static DEFINE_IDR(i2c_adapter_idr);
46
David Brownella1d9e6e2007-05-01 23:26:30 +020047#define is_newstyle_driver(d) ((d)->probe || (d)->remove)
David Brownellf37dd802007-02-13 22:09:00 +010048
49/* ------------------------------------------------------------------------- */
50
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
David Brownell7b4fbc52007-05-01 23:26:30 +020077 /* new style drivers use the same kind of driver matching policy
78 * as platform devices or SPI: compare device and driver IDs.
79 */
80 return strcmp(client->driver_name, drv->name) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
David Brownell7b4fbc52007-05-01 23:26:30 +020083#ifdef CONFIG_HOTPLUG
84
85/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020086static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020087{
88 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020089
90 /* by definition, legacy drivers can't hotplug */
Jean Delvared2653e92008-04-29 23:11:39 +020091 if (dev->driver)
David Brownell7b4fbc52007-05-01 23:26:30 +020092 return 0;
93
Jean Delvared2653e92008-04-29 23:11:39 +020094 if (client->driver_name[0]) {
95 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
96 return -ENOMEM;
97 } else {
98 if (add_uevent_var(env, "MODALIAS=%s%s",
99 I2C_MODULE_PREFIX, client->name))
100 return -ENOMEM;
101 }
David Brownell7b4fbc52007-05-01 23:26:30 +0200102 dev_dbg(dev, "uevent\n");
103 return 0;
104}
105
106#else
107#define i2c_device_uevent NULL
108#endif /* CONFIG_HOTPLUG */
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int i2c_device_probe(struct device *dev)
111{
David Brownell7b4fbc52007-05-01 23:26:30 +0200112 struct i2c_client *client = to_i2c_client(dev);
113 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Jean Delvared2653e92008-04-29 23:11:39 +0200114 const struct i2c_device_id *id;
Hans Verkuil50c33042008-03-12 14:15:00 +0100115 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200116
117 if (!driver->probe)
118 return -ENODEV;
119 client->driver = driver;
120 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200121
122 if (driver->id_table)
123 id = i2c_match_id(driver->id_table, client);
124 else
125 id = NULL;
126 status = driver->probe(client, id);
Hans Verkuil50c33042008-03-12 14:15:00 +0100127 if (status)
128 client->driver = NULL;
129 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static int i2c_device_remove(struct device *dev)
133{
David Brownella1d9e6e2007-05-01 23:26:30 +0200134 struct i2c_client *client = to_i2c_client(dev);
135 struct i2c_driver *driver;
136 int status;
137
138 if (!dev->driver)
139 return 0;
140
141 driver = to_i2c_driver(dev->driver);
142 if (driver->remove) {
143 dev_dbg(dev, "remove\n");
144 status = driver->remove(client);
145 } else {
146 dev->driver = NULL;
147 status = 0;
148 }
149 if (status == 0)
150 client->driver = NULL;
151 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
David Brownellf37dd802007-02-13 22:09:00 +0100154static void i2c_device_shutdown(struct device *dev)
155{
156 struct i2c_driver *driver;
157
158 if (!dev->driver)
159 return;
160 driver = to_i2c_driver(dev->driver);
161 if (driver->shutdown)
162 driver->shutdown(to_i2c_client(dev));
163}
164
165static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
166{
167 struct i2c_driver *driver;
168
169 if (!dev->driver)
170 return 0;
171 driver = to_i2c_driver(dev->driver);
172 if (!driver->suspend)
173 return 0;
174 return driver->suspend(to_i2c_client(dev), mesg);
175}
176
177static int i2c_device_resume(struct device * dev)
178{
179 struct i2c_driver *driver;
180
181 if (!dev->driver)
182 return 0;
183 driver = to_i2c_driver(dev->driver);
184 if (!driver->resume)
185 return 0;
186 return driver->resume(to_i2c_client(dev));
187}
188
David Brownell7b4fbc52007-05-01 23:26:30 +0200189static void i2c_client_release(struct device *dev)
190{
191 struct i2c_client *client = to_i2c_client(dev);
192 complete(&client->released);
193}
194
David Brownell9c1600e2007-05-01 23:26:31 +0200195static void i2c_client_dev_release(struct device *dev)
196{
197 kfree(to_i2c_client(dev));
198}
199
David Brownell7b4fbc52007-05-01 23:26:30 +0200200static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
201{
202 struct i2c_client *client = to_i2c_client(dev);
203 return sprintf(buf, "%s\n", client->name);
204}
205
206static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
207{
208 struct i2c_client *client = to_i2c_client(dev);
Jean Delvared2653e92008-04-29 23:11:39 +0200209 return client->driver_name[0]
David Brownell7b4fbc52007-05-01 23:26:30 +0200210 ? sprintf(buf, "%s\n", client->driver_name)
Jean Delvared2653e92008-04-29 23:11:39 +0200211 : sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200212}
213
214static struct device_attribute i2c_dev_attrs[] = {
215 __ATTR(name, S_IRUGO, show_client_name, NULL),
216 /* modalias helps coldplug: modprobe $(cat .../modalias) */
217 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
218 { },
219};
220
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200221static struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100222 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200223 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100224 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200225 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100226 .probe = i2c_device_probe,
227 .remove = i2c_device_remove,
228 .shutdown = i2c_device_shutdown,
229 .suspend = i2c_device_suspend,
230 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000231};
232
David Brownell9b766b82008-01-27 18:14:51 +0100233
234/**
235 * i2c_verify_client - return parameter as i2c_client, or NULL
236 * @dev: device, probably from some driver model iterator
237 *
238 * When traversing the driver model tree, perhaps using driver model
239 * iterators like @device_for_each_child(), you can't assume very much
240 * about the nodes you find. Use this function to avoid oopses caused
241 * by wrongly treating some non-I2C device as an i2c_client.
242 */
243struct i2c_client *i2c_verify_client(struct device *dev)
244{
245 return (dev->bus == &i2c_bus_type)
246 ? to_i2c_client(dev)
247 : NULL;
248}
249EXPORT_SYMBOL(i2c_verify_client);
250
251
David Brownell9c1600e2007-05-01 23:26:31 +0200252/**
253 * i2c_new_device - instantiate an i2c device for use with a new style driver
254 * @adap: the adapter managing the device
255 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200256 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200257 *
258 * Create a device to work with a new style i2c driver, where binding is
259 * handled through driver model probe()/remove() methods. This call is not
260 * appropriate for use by mainboad initialization logic, which usually runs
261 * during an arch_initcall() long before any i2c_adapter could exist.
262 *
263 * This returns the new i2c client, which may be saved for later use with
264 * i2c_unregister_device(); or NULL to indicate an error.
265 */
266struct i2c_client *
267i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
268{
269 struct i2c_client *client;
270 int status;
271
272 client = kzalloc(sizeof *client, GFP_KERNEL);
273 if (!client)
274 return NULL;
275
276 client->adapter = adap;
277
278 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200279 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
280
281 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200282 client->addr = info->addr;
283 client->irq = info->irq;
284
285 strlcpy(client->driver_name, info->driver_name,
286 sizeof(client->driver_name));
287 strlcpy(client->name, info->type, sizeof(client->name));
288
289 /* a new style driver may be bound to this device when we
290 * return from this function, or any later moment (e.g. maybe
291 * hotplugging will load the driver module). and the device
292 * refcount model is the standard driver model one.
293 */
294 status = i2c_attach_client(client);
295 if (status < 0) {
296 kfree(client);
297 client = NULL;
298 }
299 return client;
300}
301EXPORT_SYMBOL_GPL(i2c_new_device);
302
303
304/**
305 * i2c_unregister_device - reverse effect of i2c_new_device()
306 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200307 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200308 */
309void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200310{
311 struct i2c_adapter *adapter = client->adapter;
312 struct i2c_driver *driver = client->driver;
313
314 if (driver && !is_newstyle_driver(driver)) {
315 dev_err(&client->dev, "can't unregister devices "
316 "with legacy drivers\n");
317 WARN_ON(1);
318 return;
319 }
320
321 mutex_lock(&adapter->clist_lock);
322 list_del(&client->list);
323 mutex_unlock(&adapter->clist_lock);
324
325 device_unregister(&client->dev);
326}
David Brownell9c1600e2007-05-01 23:26:31 +0200327EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200328
329
Jean Delvared2653e92008-04-29 23:11:39 +0200330static int dummy_probe(struct i2c_client *client,
331 const struct i2c_device_id *id)
332{
333 return 0;
334}
335
336static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100337{
338 return 0;
339}
340
341static struct i2c_driver dummy_driver = {
342 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200343 .probe = dummy_probe,
344 .remove = dummy_remove,
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
351 * @type: optional label used for i2c_client.name
352 * Context: can sleep
353 *
354 * This returns an I2C client bound to the "dummy" driver, intended for use
355 * with devices that consume multiple addresses. Examples of such chips
356 * include various EEPROMS (like 24c04 and 24c08 models).
357 *
358 * These dummy devices have two main uses. First, most I2C and SMBus calls
359 * except i2c_transfer() need a client handle; the dummy will be that handle.
360 * And second, this prevents the specified address from being bound to a
361 * different driver.
362 *
363 * This returns the new i2c client, which should be saved for later use with
364 * i2c_unregister_device(); or NULL to indicate an error.
365 */
366struct i2c_client *
367i2c_new_dummy(struct i2c_adapter *adapter, u16 address, const char *type)
368{
369 struct i2c_board_info info = {
370 .driver_name = "dummy",
371 .addr = address,
372 };
373
374 if (type)
375 strlcpy(info.type, type, sizeof info.type);
376 return i2c_new_device(adapter, &info);
377}
378EXPORT_SYMBOL_GPL(i2c_new_dummy);
379
David Brownellf37dd802007-02-13 22:09:00 +0100380/* ------------------------------------------------------------------------- */
381
David Brownell16ffadf2007-05-01 23:26:28 +0200382/* I2C bus adapters -- one roots each I2C or SMBUS segment */
383
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200384static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
David Brownellef2c83212007-05-01 23:26:28 +0200386 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 complete(&adap->dev_released);
388}
389
David Brownell16ffadf2007-05-01 23:26:28 +0200390static ssize_t
391show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
David Brownellef2c83212007-05-01 23:26:28 +0200393 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return sprintf(buf, "%s\n", adap->name);
395}
David Brownell16ffadf2007-05-01 23:26:28 +0200396
397static struct device_attribute i2c_adapter_attrs[] = {
398 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
399 { },
400};
401
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200402static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200403 .owner = THIS_MODULE,
404 .name = "i2c-adapter",
405 .dev_attrs = i2c_adapter_attrs,
406};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
David Brownell9c1600e2007-05-01 23:26:31 +0200408static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
409{
410 struct i2c_devinfo *devinfo;
411
412 mutex_lock(&__i2c_board_lock);
413 list_for_each_entry(devinfo, &__i2c_board_list, list) {
414 if (devinfo->busnum == adapter->nr
415 && !i2c_new_device(adapter,
416 &devinfo->board_info))
417 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
418 i2c_adapter_id(adapter),
419 devinfo->board_info.addr);
420 }
421 mutex_unlock(&__i2c_board_lock);
422}
423
Jean Delvare026526f2008-01-27 18:14:49 +0100424static int i2c_do_add_adapter(struct device_driver *d, void *data)
425{
426 struct i2c_driver *driver = to_i2c_driver(d);
427 struct i2c_adapter *adap = data;
428
429 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
David Brownell7b4fbc52007-05-01 23:26:30 +0200468 /* let legacy drivers scan this bus for matching devices */
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;
574 int res;
575
576 if (!driver->detach_adapter)
577 return 0;
578 res = driver->detach_adapter(adapter);
579 if (res)
580 dev_err(&adapter->dev, "detach_adapter failed (%d) "
581 "for driver [%s]\n", res, driver->driver.name);
582 return res;
583}
584
David Brownelld64f73b2007-07-12 14:12:28 +0200585/**
586 * i2c_del_adapter - unregister I2C adapter
587 * @adap: the adapter being unregistered
588 * Context: can sleep
589 *
590 * This unregisters an I2C adapter which was previously registered
591 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593int i2c_del_adapter(struct i2c_adapter *adap)
594{
595 struct list_head *item, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct i2c_client *client;
597 int res = 0;
598
Jean Delvarecaada322008-01-27 18:14:49 +0100599 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100602 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200603 pr_debug("i2c-core: attempting to delete unregistered "
604 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 res = -EINVAL;
606 goto out_unlock;
607 }
608
Jean Delvare026526f2008-01-27 18:14:49 +0100609 /* Tell drivers about this removal */
610 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
611 i2c_do_del_adapter);
612 if (res)
613 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200616 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200618 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
David Brownella1d9e6e2007-05-01 23:26:30 +0200620 client = list_entry(item, struct i2c_client, list);
621 driver = client->driver;
622
623 /* new style, follow standard driver model */
624 if (!driver || is_newstyle_driver(driver)) {
625 i2c_unregister_device(client);
626 continue;
627 }
628
629 /* legacy drivers create and remove clients themselves */
630 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200631 dev_err(&adap->dev, "detach_client failed for client "
632 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 client->addr);
634 goto out_unlock;
635 }
636 }
637
638 /* clean up the sysfs representation */
639 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* wait for sysfs to drop all references */
643 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
David Brownell6e13e642007-05-01 23:26:31 +0200645 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 idr_remove(&i2c_adapter_idr, adap->nr);
647
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200648 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100651 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return res;
653}
David Brownellc0564602007-05-01 23:26:31 +0200654EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656
David Brownell7b4fbc52007-05-01 23:26:30 +0200657/* ------------------------------------------------------------------------- */
658
659/*
660 * An i2c_driver is used with one or more i2c_client (device) nodes to access
661 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
662 * are two models for binding the driver to its device: "new style" drivers
663 * follow the standard Linux driver model and just respond to probe() calls
664 * issued if the driver core sees they match(); "legacy" drivers create device
665 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
667
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800668int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100670 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
David Brownell7b4fbc52007-05-01 23:26:30 +0200672 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200673 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200674 if (driver->attach_adapter || driver->detach_adapter
675 || driver->detach_client) {
676 printk(KERN_WARNING
677 "i2c-core: driver [%s] is confused\n",
678 driver->driver.name);
679 return -EINVAL;
680 }
681 }
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800684 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
David Brownell6e13e642007-05-01 23:26:31 +0200687 /* for new style drivers, when registration returns the driver core
688 * will have called probe() for all matching-but-unbound devices.
689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 res = driver_register(&driver->driver);
691 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100692 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100693
Jean Delvarecaada322008-01-27 18:14:49 +0100694 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100695
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100696 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
David Brownell7b4fbc52007-05-01 23:26:30 +0200698 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100699 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200700 struct i2c_adapter *adapter;
701
Jean Delvare87c6c222008-01-27 18:14:48 +0100702 down(&i2c_adapter_class.sem);
703 list_for_each_entry(adapter, &i2c_adapter_class.devices,
704 dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 driver->attach_adapter(adapter);
706 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100707 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Jean Delvarecaada322008-01-27 18:14:49 +0100710 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800713EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
David Brownella1d9e6e2007-05-01 23:26:30 +0200715/**
716 * i2c_del_driver - unregister I2C driver
717 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200718 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200719 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200720void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Jean Delvare87c6c222008-01-27 18:14:48 +0100722 struct list_head *item2, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct i2c_client *client;
724 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100725
Jean Delvarecaada322008-01-27 18:14:49 +0100726 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
David Brownella1d9e6e2007-05-01 23:26:30 +0200728 /* new-style driver? */
729 if (is_newstyle_driver(driver))
730 goto unregister;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100733 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
Jean Delvare87c6c222008-01-27 18:14:48 +0100736 down(&i2c_adapter_class.sem);
737 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (driver->detach_adapter) {
Jean Delvareb3e82092007-05-01 23:26:32 +0200739 if (driver->detach_adapter(adap)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200740 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100741 "for driver [%s]\n",
742 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 } else {
745 list_for_each_safe(item2, _n, &adap->clients) {
746 client = list_entry(item2, struct i2c_client, list);
747 if (client->driver != driver)
748 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200749 dev_dbg(&adap->dev, "detaching client [%s] "
750 "at 0x%02x\n", client->name,
751 client->addr);
Jean Delvareb3e82092007-05-01 23:26:32 +0200752 if (driver->detach_client(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200753 dev_err(&adap->dev, "detach_client "
754 "failed for client [%s] at "
755 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 }
759 }
760 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100761 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
David Brownella1d9e6e2007-05-01 23:26:30 +0200763 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100765 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jean Delvarecaada322008-01-27 18:14:49 +0100767 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
David Brownellc0564602007-05-01 23:26:31 +0200769EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
David Brownell7b4fbc52007-05-01 23:26:30 +0200771/* ------------------------------------------------------------------------- */
772
David Brownell9b766b82008-01-27 18:14:51 +0100773static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
David Brownell9b766b82008-01-27 18:14:51 +0100775 struct i2c_client *client = i2c_verify_client(dev);
776 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David Brownell9b766b82008-01-27 18:14:51 +0100778 if (client && client->addr == addr)
779 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781}
782
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100783static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David Brownell9b766b82008-01-27 18:14:51 +0100785 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788int i2c_attach_client(struct i2c_client *client)
789{
790 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200791 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200795
796 if (client->driver)
797 client->dev.driver = &client->driver->driver;
798
David Brownellde81d2a2007-05-22 19:49:16 +0200799 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200800 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200801 client->dev.uevent_suppress = 1;
802 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200803 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
806 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200807 res = device_register(&client->dev);
808 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100809 goto out_err;
810
811 mutex_lock(&adapter->clist_lock);
812 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200813 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200814
David Brownell86ec5ec2008-01-27 18:14:51 +0100815 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
816 client->name, client->dev.bus_id);
817
Jean Delvare77ed74d2006-09-30 17:18:59 +0200818 if (adapter->client_register) {
819 if (adapter->client_register(client)) {
820 dev_dbg(&adapter->dev, "client_register "
821 "failed for client [%s] at 0x%02x\n",
822 client->name, client->addr);
823 }
824 }
825
826 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200827
David Brownell86ec5ec2008-01-27 18:14:51 +0100828out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200829 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
830 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200831 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
David Brownellc0564602007-05-01 23:26:31 +0200833EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835int i2c_detach_client(struct i2c_client *client)
836{
837 struct i2c_adapter *adapter = client->adapter;
838 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (adapter->client_unregister) {
841 res = adapter->client_unregister(client);
842 if (res) {
843 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700844 "client_unregister [%s] failed, "
845 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto out;
847 }
848 }
849
Ingo Molnar5c085d32006-01-18 23:16:04 +0100850 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 list_del(&client->list);
Jean Delvare9ddced12008-01-27 18:14:51 +0100852 mutex_unlock(&adapter->clist_lock);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 device_unregister(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 wait_for_completion(&client->released);
857
858 out:
859 return res;
860}
David Brownellc0564602007-05-01 23:26:31 +0200861EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Jean Delvaree48d3312008-01-27 18:14:48 +0100863/**
864 * i2c_use_client - increments the reference count of the i2c client structure
865 * @client: the client being referenced
866 *
867 * Each live reference to a client should be refcounted. The driver model does
868 * that automatically as part of driver binding, so that most drivers don't
869 * need to do this explicitly: they hold a reference until they're unbound
870 * from the device.
871 *
872 * A pointer to the client with the incremented reference counter is returned.
873 */
874struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100876 get_device(&client->dev);
Jean Delvaree48d3312008-01-27 18:14:48 +0100877 return client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
David Brownellc0564602007-05-01 23:26:31 +0200879EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Jean Delvaree48d3312008-01-27 18:14:48 +0100881/**
882 * i2c_release_client - release a use of the i2c client structure
883 * @client: the client being no longer referenced
884 *
885 * Must be called when a user of a client is finished with it.
886 */
887void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100889 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
David Brownellc0564602007-05-01 23:26:31 +0200891EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
David Brownell9b766b82008-01-27 18:14:51 +0100893struct i2c_cmd_arg {
894 unsigned cmd;
895 void *arg;
896};
897
898static int i2c_cmd(struct device *dev, void *_arg)
899{
900 struct i2c_client *client = i2c_verify_client(dev);
901 struct i2c_cmd_arg *arg = _arg;
902
903 if (client && client->driver && client->driver->command)
904 client->driver->command(client, arg->cmd, arg->arg);
905 return 0;
906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
909{
David Brownell9b766b82008-01-27 18:14:51 +0100910 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
David Brownell9b766b82008-01-27 18:14:51 +0100912 cmd_arg.cmd = cmd;
913 cmd_arg.arg = arg;
914 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
David Brownellc0564602007-05-01 23:26:31 +0200916EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918static int __init i2c_init(void)
919{
920 int retval;
921
922 retval = bus_register(&i2c_bus_type);
923 if (retval)
924 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100925 retval = class_register(&i2c_adapter_class);
926 if (retval)
927 goto bus_err;
928 retval = i2c_add_driver(&dummy_driver);
929 if (retval)
930 goto class_err;
931 return 0;
932
933class_err:
934 class_unregister(&i2c_adapter_class);
935bus_err:
936 bus_unregister(&i2c_bus_type);
937 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940static void __exit i2c_exit(void)
941{
David Brownelle9f13732008-01-27 18:14:52 +0100942 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 bus_unregister(&i2c_bus_type);
945}
946
947subsys_initcall(i2c_init);
948module_exit(i2c_exit);
949
950/* ----------------------------------------------------
951 * the functional interface to the i2c busses.
952 * ----------------------------------------------------
953 */
954
955int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
956{
957 int ret;
958
959 if (adap->algo->master_xfer) {
960#ifdef DEBUG
961 for (ret = 0; ret < num; ret++) {
962 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200963 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
964 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
965 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967#endif
968
Mike Rapoportcea443a2008-01-27 18:14:50 +0100969 if (in_atomic() || irqs_disabled()) {
970 ret = mutex_trylock(&adap->bus_lock);
971 if (!ret)
972 /* I2C activity is ongoing. */
973 return -EAGAIN;
974 } else {
975 mutex_lock_nested(&adap->bus_lock, adap->level);
976 }
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100979 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 return ret;
982 } else {
983 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
984 return -ENOSYS;
985 }
986}
David Brownellc0564602007-05-01 23:26:31 +0200987EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
990{
991 int ret;
992 struct i2c_adapter *adap=client->adapter;
993 struct i2c_msg msg;
994
Jean Delvare815f55f2005-05-07 22:58:46 +0200995 msg.addr = client->addr;
996 msg.flags = client->flags & I2C_M_TEN;
997 msg.len = count;
998 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100999
Jean Delvare815f55f2005-05-07 22:58:46 +02001000 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jean Delvare815f55f2005-05-07 22:58:46 +02001002 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1003 transmitted, else error code. */
1004 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
David Brownellc0564602007-05-01 23:26:31 +02001006EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1009{
1010 struct i2c_adapter *adap=client->adapter;
1011 struct i2c_msg msg;
1012 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Jean Delvare815f55f2005-05-07 22:58:46 +02001014 msg.addr = client->addr;
1015 msg.flags = client->flags & I2C_M_TEN;
1016 msg.flags |= I2C_M_RD;
1017 msg.len = count;
1018 msg.buf = buf;
1019
1020 ret = i2c_transfer(adap, &msg, 1);
1021
1022 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1023 transmitted, else error code. */
1024 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
David Brownellc0564602007-05-01 23:26:31 +02001026EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028/* ----------------------------------------------------
1029 * the i2c address scanning function
1030 * Will not work for 10-bit addresses!
1031 * ----------------------------------------------------
1032 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001033static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1034 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001035{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001036 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001037
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001038 /* Make sure the address is valid */
1039 if (addr < 0x03 || addr > 0x77) {
1040 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1041 addr);
1042 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001043 }
1044
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001045 /* Skip if already in use */
1046 if (i2c_check_addr(adapter, addr))
1047 return 0;
1048
1049 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +02001050 if (kind < 0) {
1051 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1052 I2C_SMBUS_QUICK, NULL) < 0)
1053 return 0;
1054
1055 /* prevent 24RF08 corruption */
1056 if ((addr & ~0x0f) == 0x50)
1057 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1058 I2C_SMBUS_QUICK, NULL);
1059 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001060
1061 /* Finally call the custom detection function */
1062 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001063 /* -ENODEV can be returned if there is a chip at the given address
1064 but it isn't supported by this chip driver. We catch it here as
1065 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +02001066 if (err == -ENODEV)
1067 err = 0;
1068
1069 if (err)
1070 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1071 addr, err);
1072 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001076 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int (*found_proc) (struct i2c_adapter *, int, int))
1078{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001079 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int adap_id = i2c_adapter_id(adapter);
1081
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001082 /* Force entries are done first, and are not affected by ignore
1083 entries */
1084 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001085 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001086 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001088 for (kind = 0; forces[kind]; kind++) {
1089 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1090 i += 2) {
1091 if (forces[kind][i] == adap_id
1092 || forces[kind][i] == ANY_I2C_BUS) {
1093 dev_dbg(&adapter->dev, "found force "
1094 "parameter for adapter %d, "
1095 "addr 0x%02x, kind %d\n",
1096 adap_id, forces[kind][i + 1],
1097 kind);
1098 err = i2c_probe_address(adapter,
1099 forces[kind][i + 1],
1100 kind, found_proc);
1101 if (err)
1102 return err;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001107
Jean Delvare4366dc92005-09-25 16:50:06 +02001108 /* Stop here if we can't use SMBUS_QUICK */
1109 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1110 if (address_data->probe[0] == I2C_CLIENT_END
1111 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001112 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001113
1114 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1115 "can't probe for chips\n");
1116 return -1;
1117 }
1118
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001119 /* Probe entries are done second, and are not affected by ignore
1120 entries either */
1121 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1122 if (address_data->probe[i] == adap_id
1123 || address_data->probe[i] == ANY_I2C_BUS) {
1124 dev_dbg(&adapter->dev, "found probe parameter for "
1125 "adapter %d, addr 0x%02x\n", adap_id,
1126 address_data->probe[i + 1]);
1127 err = i2c_probe_address(adapter,
1128 address_data->probe[i + 1],
1129 -1, found_proc);
1130 if (err)
1131 return err;
1132 }
1133 }
1134
1135 /* Normal entries are done last, unless shadowed by an ignore entry */
1136 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1137 int j, ignore;
1138
1139 ignore = 0;
1140 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1141 j += 2) {
1142 if ((address_data->ignore[j] == adap_id ||
1143 address_data->ignore[j] == ANY_I2C_BUS)
1144 && address_data->ignore[j + 1]
1145 == address_data->normal_i2c[i]) {
1146 dev_dbg(&adapter->dev, "found ignore "
1147 "parameter for adapter %d, "
1148 "addr 0x%02x\n", adap_id,
1149 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001150 ignore = 1;
1151 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001152 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001153 }
1154 if (ignore)
1155 continue;
1156
1157 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1158 "addr 0x%02x\n", adap_id,
1159 address_data->normal_i2c[i]);
1160 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1161 -1, found_proc);
1162 if (err)
1163 return err;
1164 }
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
1167}
David Brownellc0564602007-05-01 23:26:31 +02001168EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Jean Delvare12b5053a2007-05-01 23:26:31 +02001170struct i2c_client *
1171i2c_new_probed_device(struct i2c_adapter *adap,
1172 struct i2c_board_info *info,
1173 unsigned short const *addr_list)
1174{
1175 int i;
1176
1177 /* Stop here if the bus doesn't support probing */
1178 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1179 dev_err(&adap->dev, "Probing not supported\n");
1180 return NULL;
1181 }
1182
Jean Delvare12b5053a2007-05-01 23:26:31 +02001183 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1184 /* Check address validity */
1185 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1186 dev_warn(&adap->dev, "Invalid 7-bit address "
1187 "0x%02x\n", addr_list[i]);
1188 continue;
1189 }
1190
1191 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001192 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001193 dev_dbg(&adap->dev, "Address 0x%02x already in "
1194 "use, not probing\n", addr_list[i]);
1195 continue;
1196 }
1197
1198 /* Test address responsiveness
1199 The default probe method is a quick write, but it is known
1200 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1201 and could also irreversibly write-protect some EEPROMs, so
1202 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1203 read instead. Also, some bus drivers don't implement
1204 quick write, so we fallback to a byte read it that case
1205 too. */
1206 if ((addr_list[i] & ~0x07) == 0x30
1207 || (addr_list[i] & ~0x0f) == 0x50
1208 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1209 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1210 I2C_SMBUS_READ, 0,
1211 I2C_SMBUS_BYTE, NULL) >= 0)
1212 break;
1213 } else {
1214 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1215 I2C_SMBUS_WRITE, 0,
1216 I2C_SMBUS_QUICK, NULL) >= 0)
1217 break;
1218 }
1219 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001220
1221 if (addr_list[i] == I2C_CLIENT_END) {
1222 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1223 return NULL;
1224 }
1225
1226 info->addr = addr_list[i];
1227 return i2c_new_device(adap, info);
1228}
1229EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231struct i2c_adapter* i2c_get_adapter(int id)
1232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001234
Jean Delvarecaada322008-01-27 18:14:49 +01001235 mutex_lock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001236 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1237 if (adapter && !try_module_get(adapter->owner))
1238 adapter = NULL;
1239
Jean Delvarecaada322008-01-27 18:14:49 +01001240 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001241 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
David Brownellc0564602007-05-01 23:26:31 +02001243EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245void i2c_put_adapter(struct i2c_adapter *adap)
1246{
1247 module_put(adap->owner);
1248}
David Brownellc0564602007-05-01 23:26:31 +02001249EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251/* The SMBus parts */
1252
David Brownell438d6c22006-12-10 21:21:31 +01001253#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254static u8
1255crc8(u16 data)
1256{
1257 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001260 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 data = data ^ POLY;
1262 data = data << 1;
1263 }
1264 return (u8)(data >> 8);
1265}
1266
Jean Delvare421ef472005-10-26 21:28:55 +02001267/* Incremental CRC8 over count bytes in the array pointed to by p */
1268static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 int i;
1271
1272 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001273 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return crc;
1275}
1276
Jean Delvare421ef472005-10-26 21:28:55 +02001277/* Assume a 7-bit address, which is reasonable for SMBus */
1278static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Jean Delvare421ef472005-10-26 21:28:55 +02001280 /* The address will be sent first */
1281 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1282 pec = i2c_smbus_pec(pec, &addr, 1);
1283
1284 /* The data buffer follows */
1285 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Jean Delvare421ef472005-10-26 21:28:55 +02001288/* Used for write only transactions */
1289static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Jean Delvare421ef472005-10-26 21:28:55 +02001291 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1292 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Jean Delvare421ef472005-10-26 21:28:55 +02001295/* Return <0 on CRC error
1296 If there was a write before this read (most cases) we need to take the
1297 partial CRC from the write part into account.
1298 Note that this function does modify the message (we need to decrease the
1299 message length to hide the CRC byte from the caller). */
1300static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Jean Delvare421ef472005-10-26 21:28:55 +02001302 u8 rpec = msg->buf[--msg->len];
1303 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (rpec != cpec) {
1306 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1307 rpec, cpec);
1308 return -1;
1309 }
David Brownell438d6c22006-12-10 21:21:31 +01001310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1314{
1315 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001316 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
David Brownellc0564602007-05-01 23:26:31 +02001318EXPORT_SYMBOL(i2c_smbus_write_quick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320s32 i2c_smbus_read_byte(struct i2c_client *client)
1321{
1322 union i2c_smbus_data data;
1323 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1324 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1325 return -1;
1326 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001327 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
David Brownellc0564602007-05-01 23:26:31 +02001329EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001334 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
David Brownellc0564602007-05-01 23:26:31 +02001336EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1339{
1340 union i2c_smbus_data data;
1341 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1342 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1343 return -1;
1344 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001345 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
David Brownellc0564602007-05-01 23:26:31 +02001347EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1350{
1351 union i2c_smbus_data data;
1352 data.byte = value;
1353 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1354 I2C_SMBUS_WRITE,command,
1355 I2C_SMBUS_BYTE_DATA,&data);
1356}
David Brownellc0564602007-05-01 23:26:31 +02001357EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1360{
1361 union i2c_smbus_data data;
1362 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1363 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1364 return -1;
1365 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001366 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
David Brownellc0564602007-05-01 23:26:31 +02001368EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1371{
1372 union i2c_smbus_data data;
1373 data.word = value;
1374 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1375 I2C_SMBUS_WRITE,command,
1376 I2C_SMBUS_WORD_DATA,&data);
1377}
David Brownellc0564602007-05-01 23:26:31 +02001378EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
David Brownella64ec072007-10-13 23:56:31 +02001380/**
1381 * i2c_smbus_read_block_data - SMBus block read request
1382 * @client: Handle to slave device
1383 * @command: Command byte issued to let the slave know what data should
1384 * be returned
1385 * @values: Byte array into which data will be read; big enough to hold
1386 * the data returned by the slave. SMBus allows at most 32 bytes.
1387 *
1388 * Returns the number of bytes read in the slave's response, else a
1389 * negative number to indicate some kind of error.
1390 *
1391 * Note that using this function requires that the client's adapter support
1392 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1393 * support this; its emulation through I2C messaging relies on a specific
1394 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1395 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001396s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1397 u8 *values)
1398{
1399 union i2c_smbus_data data;
1400
1401 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1402 I2C_SMBUS_READ, command,
1403 I2C_SMBUS_BLOCK_DATA, &data))
1404 return -1;
1405
1406 memcpy(values, &data.block[1], data.block[0]);
1407 return data.block[0];
1408}
1409EXPORT_SYMBOL(i2c_smbus_read_block_data);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001412 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (length > I2C_SMBUS_BLOCK_MAX)
1417 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001419 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1421 I2C_SMBUS_WRITE,command,
1422 I2C_SMBUS_BLOCK_DATA,&data);
1423}
David Brownellc0564602007-05-01 23:26:31 +02001424EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001427s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1428 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001431
Jean Delvare4b2643d2007-07-12 14:12:29 +02001432 if (length > I2C_SMBUS_BLOCK_MAX)
1433 length = I2C_SMBUS_BLOCK_MAX;
1434 data.block[0] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1436 I2C_SMBUS_READ,command,
1437 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1438 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001439
1440 memcpy(values, &data.block[1], data.block[0]);
1441 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
David Brownellc0564602007-05-01 23:26:31 +02001443EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Jean Delvare21bbd692006-01-09 15:19:18 +11001445s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001446 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001447{
1448 union i2c_smbus_data data;
1449
1450 if (length > I2C_SMBUS_BLOCK_MAX)
1451 length = I2C_SMBUS_BLOCK_MAX;
1452 data.block[0] = length;
1453 memcpy(data.block + 1, values, length);
1454 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1455 I2C_SMBUS_WRITE, command,
1456 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1457}
David Brownellc0564602007-05-01 23:26:31 +02001458EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001459
David Brownell438d6c22006-12-10 21:21:31 +01001460/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001462static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001464 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 union i2c_smbus_data * data)
1466{
1467 /* So we need to generate a series of msgs. In the case of writing, we
1468 need to use only one message; when reading, we need two. We initialize
1469 most things with sane defaults, to keep the code below somewhat
1470 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001471 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1472 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001474 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1476 };
1477 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001478 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 msgbuf0[0] = command;
1481 switch(size) {
1482 case I2C_SMBUS_QUICK:
1483 msg[0].len = 0;
1484 /* Special case: The read/write field is used as data */
1485 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1486 num = 1;
1487 break;
1488 case I2C_SMBUS_BYTE:
1489 if (read_write == I2C_SMBUS_READ) {
1490 /* Special case: only a read! */
1491 msg[0].flags = I2C_M_RD | flags;
1492 num = 1;
1493 }
1494 break;
1495 case I2C_SMBUS_BYTE_DATA:
1496 if (read_write == I2C_SMBUS_READ)
1497 msg[1].len = 1;
1498 else {
1499 msg[0].len = 2;
1500 msgbuf0[1] = data->byte;
1501 }
1502 break;
1503 case I2C_SMBUS_WORD_DATA:
1504 if (read_write == I2C_SMBUS_READ)
1505 msg[1].len = 2;
1506 else {
1507 msg[0].len=3;
1508 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001509 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511 break;
1512 case I2C_SMBUS_PROC_CALL:
1513 num = 2; /* Special case */
1514 read_write = I2C_SMBUS_READ;
1515 msg[0].len = 3;
1516 msg[1].len = 2;
1517 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001518 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
1520 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001522 msg[1].flags |= I2C_M_RECV_LEN;
1523 msg[1].len = 1; /* block length will be added by
1524 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 } else {
1526 msg[0].len = data->block[0] + 2;
1527 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1528 dev_err(&adapter->dev, "smbus_access called with "
1529 "invalid block write size (%d)\n",
1530 data->block[0]);
1531 return -1;
1532 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001533 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 msgbuf0[i] = data->block[i-1];
1535 }
1536 break;
1537 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001538 num = 2; /* Another special case */
1539 read_write = I2C_SMBUS_READ;
1540 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1541 dev_err(&adapter->dev, "%s called with invalid "
Harvey Harrison08882d22008-04-22 22:16:47 +02001542 "block proc call size (%d)\n", __func__,
Jean Delvare209d27c2007-05-01 23:26:29 +02001543 data->block[0]);
1544 return -1;
1545 }
1546 msg[0].len = data->block[0] + 2;
1547 for (i = 1; i < msg[0].len; i++)
1548 msgbuf0[i] = data->block[i-1];
1549 msg[1].flags |= I2C_M_RECV_LEN;
1550 msg[1].len = 1; /* block length will be added by
1551 the underlying bus driver */
1552 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 case I2C_SMBUS_I2C_BLOCK_DATA:
1554 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001555 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 } else {
1557 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001558 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1560 "invalid block write size (%d)\n",
1561 data->block[0]);
1562 return -1;
1563 }
1564 for (i = 1; i <= data->block[0]; i++)
1565 msgbuf0[i] = data->block[i];
1566 }
1567 break;
1568 default:
1569 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1570 size);
1571 return -1;
1572 }
1573
Jean Delvare421ef472005-10-26 21:28:55 +02001574 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1575 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1576 if (i) {
1577 /* Compute PEC if first message is a write */
1578 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001579 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001580 i2c_smbus_add_pec(&msg[0]);
1581 else /* Write followed by read */
1582 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1583 }
1584 /* Ask for PEC if last message is a read */
1585 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001586 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001587 }
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (i2c_transfer(adapter, msg, num) < 0)
1590 return -1;
1591
Jean Delvare421ef472005-10-26 21:28:55 +02001592 /* Check PEC if last message is a read */
1593 if (i && (msg[num-1].flags & I2C_M_RD)) {
1594 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1595 return -1;
1596 }
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (read_write == I2C_SMBUS_READ)
1599 switch(size) {
1600 case I2C_SMBUS_BYTE:
1601 data->byte = msgbuf0[0];
1602 break;
1603 case I2C_SMBUS_BYTE_DATA:
1604 data->byte = msgbuf1[0];
1605 break;
David Brownell438d6c22006-12-10 21:21:31 +01001606 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 case I2C_SMBUS_PROC_CALL:
1608 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1609 break;
1610 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001611 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 data->block[i+1] = msgbuf1[i];
1613 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001614 case I2C_SMBUS_BLOCK_DATA:
1615 case I2C_SMBUS_BLOCK_PROC_CALL:
1616 for (i = 0; i < msgbuf1[0] + 1; i++)
1617 data->block[i] = msgbuf1[i];
1618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
1620 return 0;
1621}
1622
1623
1624s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001625 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 union i2c_smbus_data * data)
1627{
1628 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001633 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1635 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001636 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 } else
1638 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1639 command,size,data);
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return res;
1642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1646MODULE_DESCRIPTION("I2C-Bus main module");
1647MODULE_LICENSE("GPL");