blob: d0175f4f8fc683941057bf4866e97a8a9abdcfd3 [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
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);
Jean Delvared2653e92008-04-29 23:11:39 +0200106 const struct i2c_device_id *id;
Hans Verkuil50c33042008-03-12 14:15:00 +0100107 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200108
109 if (!driver->probe)
110 return -ENODEV;
111 client->driver = driver;
112 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200113
114 if (driver->id_table)
115 id = i2c_match_id(driver->id_table, client);
116 else
117 id = NULL;
118 status = driver->probe(client, id);
Hans Verkuil50c33042008-03-12 14:15:00 +0100119 if (status)
120 client->driver = NULL;
121 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static int i2c_device_remove(struct device *dev)
125{
David Brownella1d9e6e2007-05-01 23:26:30 +0200126 struct i2c_client *client = to_i2c_client(dev);
127 struct i2c_driver *driver;
128 int status;
129
130 if (!dev->driver)
131 return 0;
132
133 driver = to_i2c_driver(dev->driver);
134 if (driver->remove) {
135 dev_dbg(dev, "remove\n");
136 status = driver->remove(client);
137 } else {
138 dev->driver = NULL;
139 status = 0;
140 }
141 if (status == 0)
142 client->driver = NULL;
143 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
David Brownellf37dd802007-02-13 22:09:00 +0100146static void i2c_device_shutdown(struct device *dev)
147{
148 struct i2c_driver *driver;
149
150 if (!dev->driver)
151 return;
152 driver = to_i2c_driver(dev->driver);
153 if (driver->shutdown)
154 driver->shutdown(to_i2c_client(dev));
155}
156
157static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
158{
159 struct i2c_driver *driver;
160
161 if (!dev->driver)
162 return 0;
163 driver = to_i2c_driver(dev->driver);
164 if (!driver->suspend)
165 return 0;
166 return driver->suspend(to_i2c_client(dev), mesg);
167}
168
169static int i2c_device_resume(struct device * dev)
170{
171 struct i2c_driver *driver;
172
173 if (!dev->driver)
174 return 0;
175 driver = to_i2c_driver(dev->driver);
176 if (!driver->resume)
177 return 0;
178 return driver->resume(to_i2c_client(dev));
179}
180
David Brownell7b4fbc52007-05-01 23:26:30 +0200181static void i2c_client_release(struct device *dev)
182{
183 struct i2c_client *client = to_i2c_client(dev);
184 complete(&client->released);
185}
186
David Brownell9c1600e2007-05-01 23:26:31 +0200187static void i2c_client_dev_release(struct device *dev)
188{
189 kfree(to_i2c_client(dev));
190}
191
David Brownell7b4fbc52007-05-01 23:26:30 +0200192static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
193{
194 struct i2c_client *client = to_i2c_client(dev);
195 return sprintf(buf, "%s\n", client->name);
196}
197
198static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
199{
200 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200201 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200202}
203
204static struct device_attribute i2c_dev_attrs[] = {
205 __ATTR(name, S_IRUGO, show_client_name, NULL),
206 /* modalias helps coldplug: modprobe $(cat .../modalias) */
207 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
208 { },
209};
210
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200211static struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100212 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200213 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100214 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200215 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100216 .probe = i2c_device_probe,
217 .remove = i2c_device_remove,
218 .shutdown = i2c_device_shutdown,
219 .suspend = i2c_device_suspend,
220 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000221};
222
David Brownell9b766b82008-01-27 18:14:51 +0100223
224/**
225 * i2c_verify_client - return parameter as i2c_client, or NULL
226 * @dev: device, probably from some driver model iterator
227 *
228 * When traversing the driver model tree, perhaps using driver model
229 * iterators like @device_for_each_child(), you can't assume very much
230 * about the nodes you find. Use this function to avoid oopses caused
231 * by wrongly treating some non-I2C device as an i2c_client.
232 */
233struct i2c_client *i2c_verify_client(struct device *dev)
234{
235 return (dev->bus == &i2c_bus_type)
236 ? to_i2c_client(dev)
237 : NULL;
238}
239EXPORT_SYMBOL(i2c_verify_client);
240
241
David Brownell9c1600e2007-05-01 23:26:31 +0200242/**
243 * i2c_new_device - instantiate an i2c device for use with a new style driver
244 * @adap: the adapter managing the device
245 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200246 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200247 *
248 * Create a device to work with a new style i2c driver, where binding is
249 * handled through driver model probe()/remove() methods. This call is not
250 * appropriate for use by mainboad initialization logic, which usually runs
251 * during an arch_initcall() long before any i2c_adapter could exist.
252 *
253 * This returns the new i2c client, which may be saved for later use with
254 * i2c_unregister_device(); or NULL to indicate an error.
255 */
256struct i2c_client *
257i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
258{
259 struct i2c_client *client;
260 int status;
261
262 client = kzalloc(sizeof *client, GFP_KERNEL);
263 if (!client)
264 return NULL;
265
266 client->adapter = adap;
267
268 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200269 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
270
271 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200272 client->addr = info->addr;
273 client->irq = info->irq;
274
David Brownell9c1600e2007-05-01 23:26:31 +0200275 strlcpy(client->name, info->type, sizeof(client->name));
276
277 /* a new style driver may be bound to this device when we
278 * return from this function, or any later moment (e.g. maybe
279 * hotplugging will load the driver module). and the device
280 * refcount model is the standard driver model one.
281 */
282 status = i2c_attach_client(client);
283 if (status < 0) {
284 kfree(client);
285 client = NULL;
286 }
287 return client;
288}
289EXPORT_SYMBOL_GPL(i2c_new_device);
290
291
292/**
293 * i2c_unregister_device - reverse effect of i2c_new_device()
294 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200295 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200296 */
297void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200298{
299 struct i2c_adapter *adapter = client->adapter;
300 struct i2c_driver *driver = client->driver;
301
302 if (driver && !is_newstyle_driver(driver)) {
303 dev_err(&client->dev, "can't unregister devices "
304 "with legacy drivers\n");
305 WARN_ON(1);
306 return;
307 }
308
309 mutex_lock(&adapter->clist_lock);
310 list_del(&client->list);
311 mutex_unlock(&adapter->clist_lock);
312
313 device_unregister(&client->dev);
314}
David Brownell9c1600e2007-05-01 23:26:31 +0200315EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200316
317
Jean Delvare60b129d2008-05-11 20:37:06 +0200318static const struct i2c_device_id dummy_id[] = {
319 { "dummy", 0 },
320 { },
321};
322
Jean Delvared2653e92008-04-29 23:11:39 +0200323static int dummy_probe(struct i2c_client *client,
324 const struct i2c_device_id *id)
325{
326 return 0;
327}
328
329static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100330{
331 return 0;
332}
333
334static struct i2c_driver dummy_driver = {
335 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200336 .probe = dummy_probe,
337 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200338 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100339};
340
341/**
342 * i2c_new_dummy - return a new i2c device bound to a dummy driver
343 * @adapter: the adapter managing the device
344 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100345 * Context: can sleep
346 *
347 * This returns an I2C client bound to the "dummy" driver, intended for use
348 * with devices that consume multiple addresses. Examples of such chips
349 * include various EEPROMS (like 24c04 and 24c08 models).
350 *
351 * These dummy devices have two main uses. First, most I2C and SMBus calls
352 * except i2c_transfer() need a client handle; the dummy will be that handle.
353 * And second, this prevents the specified address from being bound to a
354 * different driver.
355 *
356 * This returns the new i2c client, which should be saved for later use with
357 * i2c_unregister_device(); or NULL to indicate an error.
358 */
359struct i2c_client *
Jean Delvare60b129d2008-05-11 20:37:06 +0200360i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100361{
362 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200363 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100364 };
365
David Brownelle9f13732008-01-27 18:14:52 +0100366 return i2c_new_device(adapter, &info);
367}
368EXPORT_SYMBOL_GPL(i2c_new_dummy);
369
David Brownellf37dd802007-02-13 22:09:00 +0100370/* ------------------------------------------------------------------------- */
371
David Brownell16ffadf2007-05-01 23:26:28 +0200372/* I2C bus adapters -- one roots each I2C or SMBUS segment */
373
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200374static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
David Brownellef2c83212007-05-01 23:26:28 +0200376 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 complete(&adap->dev_released);
378}
379
David Brownell16ffadf2007-05-01 23:26:28 +0200380static ssize_t
381show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
David Brownellef2c83212007-05-01 23:26:28 +0200383 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return sprintf(buf, "%s\n", adap->name);
385}
David Brownell16ffadf2007-05-01 23:26:28 +0200386
387static struct device_attribute i2c_adapter_attrs[] = {
388 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
389 { },
390};
391
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200392static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200393 .owner = THIS_MODULE,
394 .name = "i2c-adapter",
395 .dev_attrs = i2c_adapter_attrs,
396};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David Brownell9c1600e2007-05-01 23:26:31 +0200398static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
399{
400 struct i2c_devinfo *devinfo;
401
402 mutex_lock(&__i2c_board_lock);
403 list_for_each_entry(devinfo, &__i2c_board_list, list) {
404 if (devinfo->busnum == adapter->nr
405 && !i2c_new_device(adapter,
406 &devinfo->board_info))
407 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
408 i2c_adapter_id(adapter),
409 devinfo->board_info.addr);
410 }
411 mutex_unlock(&__i2c_board_lock);
412}
413
Jean Delvare026526f2008-01-27 18:14:49 +0100414static int i2c_do_add_adapter(struct device_driver *d, void *data)
415{
416 struct i2c_driver *driver = to_i2c_driver(d);
417 struct i2c_adapter *adap = data;
418
419 if (driver->attach_adapter) {
420 /* We ignore the return code; if it fails, too bad */
421 driver->attach_adapter(adap);
422 }
423 return 0;
424}
425
David Brownell6e13e642007-05-01 23:26:31 +0200426static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Jean Delvare026526f2008-01-27 18:14:49 +0100428 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Ingo Molnar5c085d32006-01-18 23:16:04 +0100430 mutex_init(&adap->bus_lock);
431 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 INIT_LIST_HEAD(&adap->clients);
433
Jean Delvarecaada322008-01-27 18:14:49 +0100434 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* Add the adapter to the driver core.
437 * If the parent pointer is not set up,
438 * we add this adapter to the host bus.
439 */
David Brownellb119dc32007-01-04 13:07:04 +0100440 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100442 pr_debug("I2C adapter driver [%s] forgot to specify "
443 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200447 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200448 res = device_register(&adap->dev);
449 if (res)
450 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200452 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
453
David Brownell6e13e642007-05-01 23:26:31 +0200454 /* create pre-declared device nodes for new-style drivers */
455 if (adap->nr < __i2c_first_dynamic_bus_num)
456 i2c_scan_static_board_info(adap);
457
David Brownell7b4fbc52007-05-01 23:26:30 +0200458 /* let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100459 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
460 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100463 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200465
Jean Delvareb119c6c2006-08-15 18:26:30 +0200466out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200467 idr_remove(&i2c_adapter_idr, adap->nr);
468 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
David Brownell6e13e642007-05-01 23:26:31 +0200471/**
472 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
473 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200474 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200475 *
476 * This routine is used to declare an I2C adapter when its bus number
477 * doesn't matter. Examples: for I2C adapters dynamically added by
478 * USB links or PCI plugin cards.
479 *
480 * When this returns zero, a new bus number was allocated and stored
481 * in adap->nr, and the specified adapter became available for clients.
482 * Otherwise, a negative errno value is returned.
483 */
484int i2c_add_adapter(struct i2c_adapter *adapter)
485{
486 int id, res = 0;
487
488retry:
489 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
490 return -ENOMEM;
491
Jean Delvarecaada322008-01-27 18:14:49 +0100492 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200493 /* "above" here means "above or equal to", sigh */
494 res = idr_get_new_above(&i2c_adapter_idr, adapter,
495 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100496 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200497
498 if (res < 0) {
499 if (res == -EAGAIN)
500 goto retry;
501 return res;
502 }
503
504 adapter->nr = id;
505 return i2c_register_adapter(adapter);
506}
507EXPORT_SYMBOL(i2c_add_adapter);
508
509/**
510 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
511 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200512 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200513 *
514 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100515 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
516 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200517 * is used to properly configure I2C devices.
518 *
519 * If no devices have pre-been declared for this bus, then be sure to
520 * register the adapter before any dynamically allocated ones. Otherwise
521 * the required bus ID may not be available.
522 *
523 * When this returns zero, the specified adapter became available for
524 * clients using the bus number provided in adap->nr. Also, the table
525 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
526 * and the appropriate driver model device nodes are created. Otherwise, a
527 * negative errno value is returned.
528 */
529int i2c_add_numbered_adapter(struct i2c_adapter *adap)
530{
531 int id;
532 int status;
533
534 if (adap->nr & ~MAX_ID_MASK)
535 return -EINVAL;
536
537retry:
538 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
539 return -ENOMEM;
540
Jean Delvarecaada322008-01-27 18:14:49 +0100541 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200542 /* "above" here means "above or equal to", sigh;
543 * we need the "equal to" result to force the result
544 */
545 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
546 if (status == 0 && id != adap->nr) {
547 status = -EBUSY;
548 idr_remove(&i2c_adapter_idr, id);
549 }
Jean Delvarecaada322008-01-27 18:14:49 +0100550 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200551 if (status == -EAGAIN)
552 goto retry;
553
554 if (status == 0)
555 status = i2c_register_adapter(adap);
556 return status;
557}
558EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
559
Jean Delvare026526f2008-01-27 18:14:49 +0100560static int i2c_do_del_adapter(struct device_driver *d, void *data)
561{
562 struct i2c_driver *driver = to_i2c_driver(d);
563 struct i2c_adapter *adapter = data;
564 int res;
565
566 if (!driver->detach_adapter)
567 return 0;
568 res = driver->detach_adapter(adapter);
569 if (res)
570 dev_err(&adapter->dev, "detach_adapter failed (%d) "
571 "for driver [%s]\n", res, driver->driver.name);
572 return res;
573}
574
David Brownelld64f73b2007-07-12 14:12:28 +0200575/**
576 * i2c_del_adapter - unregister I2C adapter
577 * @adap: the adapter being unregistered
578 * Context: can sleep
579 *
580 * This unregisters an I2C adapter which was previously registered
581 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583int i2c_del_adapter(struct i2c_adapter *adap)
584{
585 struct list_head *item, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct i2c_client *client;
587 int res = 0;
588
Jean Delvarecaada322008-01-27 18:14:49 +0100589 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100592 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200593 pr_debug("i2c-core: attempting to delete unregistered "
594 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 res = -EINVAL;
596 goto out_unlock;
597 }
598
Jean Delvare026526f2008-01-27 18:14:49 +0100599 /* Tell drivers about this removal */
600 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
601 i2c_do_del_adapter);
602 if (res)
603 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200606 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200608 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
David Brownella1d9e6e2007-05-01 23:26:30 +0200610 client = list_entry(item, struct i2c_client, list);
611 driver = client->driver;
612
613 /* new style, follow standard driver model */
614 if (!driver || is_newstyle_driver(driver)) {
615 i2c_unregister_device(client);
616 continue;
617 }
618
619 /* legacy drivers create and remove clients themselves */
620 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200621 dev_err(&adap->dev, "detach_client failed for client "
622 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 client->addr);
624 goto out_unlock;
625 }
626 }
627
628 /* clean up the sysfs representation */
629 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* wait for sysfs to drop all references */
633 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
David Brownell6e13e642007-05-01 23:26:31 +0200635 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 idr_remove(&i2c_adapter_idr, adap->nr);
637
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200638 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100641 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return res;
643}
David Brownellc0564602007-05-01 23:26:31 +0200644EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646
David Brownell7b4fbc52007-05-01 23:26:30 +0200647/* ------------------------------------------------------------------------- */
648
649/*
650 * An i2c_driver is used with one or more i2c_client (device) nodes to access
651 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
652 * are two models for binding the driver to its device: "new style" drivers
653 * follow the standard Linux driver model and just respond to probe() calls
654 * issued if the driver core sees they match(); "legacy" drivers create device
655 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800658int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100660 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
David Brownell7b4fbc52007-05-01 23:26:30 +0200662 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200663 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200664 if (driver->attach_adapter || driver->detach_adapter
665 || driver->detach_client) {
666 printk(KERN_WARNING
667 "i2c-core: driver [%s] is confused\n",
668 driver->driver.name);
669 return -EINVAL;
670 }
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800674 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
David Brownell6e13e642007-05-01 23:26:31 +0200677 /* for new style drivers, when registration returns the driver core
678 * will have called probe() for all matching-but-unbound devices.
679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 res = driver_register(&driver->driver);
681 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100682 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100683
Jean Delvarecaada322008-01-27 18:14:49 +0100684 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100685
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100686 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
David Brownell7b4fbc52007-05-01 23:26:30 +0200688 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100689 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200690 struct i2c_adapter *adapter;
691
Jean Delvare87c6c222008-01-27 18:14:48 +0100692 down(&i2c_adapter_class.sem);
693 list_for_each_entry(adapter, &i2c_adapter_class.devices,
694 dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 driver->attach_adapter(adapter);
696 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100697 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
Jean Delvarecaada322008-01-27 18:14:49 +0100700 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800703EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
David Brownella1d9e6e2007-05-01 23:26:30 +0200705/**
706 * i2c_del_driver - unregister I2C driver
707 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200708 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200709 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200710void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Jean Delvare87c6c222008-01-27 18:14:48 +0100712 struct list_head *item2, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct i2c_client *client;
714 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100715
Jean Delvarecaada322008-01-27 18:14:49 +0100716 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
David Brownella1d9e6e2007-05-01 23:26:30 +0200718 /* new-style driver? */
719 if (is_newstyle_driver(driver))
720 goto unregister;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100723 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Jean Delvare87c6c222008-01-27 18:14:48 +0100726 down(&i2c_adapter_class.sem);
727 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (driver->detach_adapter) {
Jean Delvareb3e82092007-05-01 23:26:32 +0200729 if (driver->detach_adapter(adap)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200730 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100731 "for driver [%s]\n",
732 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 } else {
735 list_for_each_safe(item2, _n, &adap->clients) {
736 client = list_entry(item2, struct i2c_client, list);
737 if (client->driver != driver)
738 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200739 dev_dbg(&adap->dev, "detaching client [%s] "
740 "at 0x%02x\n", client->name,
741 client->addr);
Jean Delvareb3e82092007-05-01 23:26:32 +0200742 if (driver->detach_client(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200743 dev_err(&adap->dev, "detach_client "
744 "failed for client [%s] at "
745 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 }
749 }
750 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100751 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
David Brownella1d9e6e2007-05-01 23:26:30 +0200753 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100755 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Jean Delvarecaada322008-01-27 18:14:49 +0100757 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
David Brownellc0564602007-05-01 23:26:31 +0200759EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
David Brownell7b4fbc52007-05-01 23:26:30 +0200761/* ------------------------------------------------------------------------- */
762
David Brownell9b766b82008-01-27 18:14:51 +0100763static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
David Brownell9b766b82008-01-27 18:14:51 +0100765 struct i2c_client *client = i2c_verify_client(dev);
766 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
David Brownell9b766b82008-01-27 18:14:51 +0100768 if (client && client->addr == addr)
769 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
771}
772
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100773static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
David Brownell9b766b82008-01-27 18:14:51 +0100775 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778int i2c_attach_client(struct i2c_client *client)
779{
780 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200781 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200785
786 if (client->driver)
787 client->dev.driver = &client->driver->driver;
788
David Brownellde81d2a2007-05-22 19:49:16 +0200789 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200790 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200791 client->dev.uevent_suppress = 1;
792 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200793 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
796 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200797 res = device_register(&client->dev);
798 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100799 goto out_err;
800
801 mutex_lock(&adapter->clist_lock);
802 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200803 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200804
David Brownell86ec5ec2008-01-27 18:14:51 +0100805 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
806 client->name, client->dev.bus_id);
807
Jean Delvare77ed74d2006-09-30 17:18:59 +0200808 if (adapter->client_register) {
809 if (adapter->client_register(client)) {
810 dev_dbg(&adapter->dev, "client_register "
811 "failed for client [%s] at 0x%02x\n",
812 client->name, client->addr);
813 }
814 }
815
816 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200817
David Brownell86ec5ec2008-01-27 18:14:51 +0100818out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200819 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
820 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200821 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
David Brownellc0564602007-05-01 23:26:31 +0200823EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825int i2c_detach_client(struct i2c_client *client)
826{
827 struct i2c_adapter *adapter = client->adapter;
828 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (adapter->client_unregister) {
831 res = adapter->client_unregister(client);
832 if (res) {
833 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700834 "client_unregister [%s] failed, "
835 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto out;
837 }
838 }
839
Ingo Molnar5c085d32006-01-18 23:16:04 +0100840 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 list_del(&client->list);
Jean Delvare9ddced12008-01-27 18:14:51 +0100842 mutex_unlock(&adapter->clist_lock);
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 device_unregister(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 wait_for_completion(&client->released);
847
848 out:
849 return res;
850}
David Brownellc0564602007-05-01 23:26:31 +0200851EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Jean Delvaree48d3312008-01-27 18:14:48 +0100853/**
854 * i2c_use_client - increments the reference count of the i2c client structure
855 * @client: the client being referenced
856 *
857 * Each live reference to a client should be refcounted. The driver model does
858 * that automatically as part of driver binding, so that most drivers don't
859 * need to do this explicitly: they hold a reference until they're unbound
860 * from the device.
861 *
862 * A pointer to the client with the incremented reference counter is returned.
863 */
864struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100866 get_device(&client->dev);
Jean Delvaree48d3312008-01-27 18:14:48 +0100867 return client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
David Brownellc0564602007-05-01 23:26:31 +0200869EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Jean Delvaree48d3312008-01-27 18:14:48 +0100871/**
872 * i2c_release_client - release a use of the i2c client structure
873 * @client: the client being no longer referenced
874 *
875 * Must be called when a user of a client is finished with it.
876 */
877void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100879 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
David Brownellc0564602007-05-01 23:26:31 +0200881EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
David Brownell9b766b82008-01-27 18:14:51 +0100883struct i2c_cmd_arg {
884 unsigned cmd;
885 void *arg;
886};
887
888static int i2c_cmd(struct device *dev, void *_arg)
889{
890 struct i2c_client *client = i2c_verify_client(dev);
891 struct i2c_cmd_arg *arg = _arg;
892
893 if (client && client->driver && client->driver->command)
894 client->driver->command(client, arg->cmd, arg->arg);
895 return 0;
896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
899{
David Brownell9b766b82008-01-27 18:14:51 +0100900 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
David Brownell9b766b82008-01-27 18:14:51 +0100902 cmd_arg.cmd = cmd;
903 cmd_arg.arg = arg;
904 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
David Brownellc0564602007-05-01 23:26:31 +0200906EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908static int __init i2c_init(void)
909{
910 int retval;
911
912 retval = bus_register(&i2c_bus_type);
913 if (retval)
914 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100915 retval = class_register(&i2c_adapter_class);
916 if (retval)
917 goto bus_err;
918 retval = i2c_add_driver(&dummy_driver);
919 if (retval)
920 goto class_err;
921 return 0;
922
923class_err:
924 class_unregister(&i2c_adapter_class);
925bus_err:
926 bus_unregister(&i2c_bus_type);
927 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930static void __exit i2c_exit(void)
931{
David Brownelle9f13732008-01-27 18:14:52 +0100932 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 bus_unregister(&i2c_bus_type);
935}
936
937subsys_initcall(i2c_init);
938module_exit(i2c_exit);
939
940/* ----------------------------------------------------
941 * the functional interface to the i2c busses.
942 * ----------------------------------------------------
943 */
944
945int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
946{
947 int ret;
948
949 if (adap->algo->master_xfer) {
950#ifdef DEBUG
951 for (ret = 0; ret < num; ret++) {
952 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200953 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
954 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
955 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957#endif
958
Mike Rapoportcea443a2008-01-27 18:14:50 +0100959 if (in_atomic() || irqs_disabled()) {
960 ret = mutex_trylock(&adap->bus_lock);
961 if (!ret)
962 /* I2C activity is ongoing. */
963 return -EAGAIN;
964 } else {
965 mutex_lock_nested(&adap->bus_lock, adap->level);
966 }
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100969 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 return ret;
972 } else {
973 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
974 return -ENOSYS;
975 }
976}
David Brownellc0564602007-05-01 23:26:31 +0200977EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
980{
981 int ret;
982 struct i2c_adapter *adap=client->adapter;
983 struct i2c_msg msg;
984
Jean Delvare815f55f2005-05-07 22:58:46 +0200985 msg.addr = client->addr;
986 msg.flags = client->flags & I2C_M_TEN;
987 msg.len = count;
988 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100989
Jean Delvare815f55f2005-05-07 22:58:46 +0200990 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Jean Delvare815f55f2005-05-07 22:58:46 +0200992 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
993 transmitted, else error code. */
994 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
David Brownellc0564602007-05-01 23:26:31 +0200996EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
999{
1000 struct i2c_adapter *adap=client->adapter;
1001 struct i2c_msg msg;
1002 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Jean Delvare815f55f2005-05-07 22:58:46 +02001004 msg.addr = client->addr;
1005 msg.flags = client->flags & I2C_M_TEN;
1006 msg.flags |= I2C_M_RD;
1007 msg.len = count;
1008 msg.buf = buf;
1009
1010 ret = i2c_transfer(adap, &msg, 1);
1011
1012 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1013 transmitted, else error code. */
1014 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
David Brownellc0564602007-05-01 23:26:31 +02001016EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018/* ----------------------------------------------------
1019 * the i2c address scanning function
1020 * Will not work for 10-bit addresses!
1021 * ----------------------------------------------------
1022 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001023static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1024 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001025{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001026 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001027
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001028 /* Make sure the address is valid */
1029 if (addr < 0x03 || addr > 0x77) {
1030 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1031 addr);
1032 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001033 }
1034
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001035 /* Skip if already in use */
1036 if (i2c_check_addr(adapter, addr))
1037 return 0;
1038
1039 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +02001040 if (kind < 0) {
1041 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1042 I2C_SMBUS_QUICK, NULL) < 0)
1043 return 0;
1044
1045 /* prevent 24RF08 corruption */
1046 if ((addr & ~0x0f) == 0x50)
1047 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1048 I2C_SMBUS_QUICK, NULL);
1049 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001050
1051 /* Finally call the custom detection function */
1052 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001053 /* -ENODEV can be returned if there is a chip at the given address
1054 but it isn't supported by this chip driver. We catch it here as
1055 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +02001056 if (err == -ENODEV)
1057 err = 0;
1058
1059 if (err)
1060 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1061 addr, err);
1062 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001066 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 int (*found_proc) (struct i2c_adapter *, int, int))
1068{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001069 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int adap_id = i2c_adapter_id(adapter);
1071
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001072 /* Force entries are done first, and are not affected by ignore
1073 entries */
1074 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001075 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001076 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001078 for (kind = 0; forces[kind]; kind++) {
1079 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1080 i += 2) {
1081 if (forces[kind][i] == adap_id
1082 || forces[kind][i] == ANY_I2C_BUS) {
1083 dev_dbg(&adapter->dev, "found force "
1084 "parameter for adapter %d, "
1085 "addr 0x%02x, kind %d\n",
1086 adap_id, forces[kind][i + 1],
1087 kind);
1088 err = i2c_probe_address(adapter,
1089 forces[kind][i + 1],
1090 kind, found_proc);
1091 if (err)
1092 return err;
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001097
Jean Delvare4366dc92005-09-25 16:50:06 +02001098 /* Stop here if we can't use SMBUS_QUICK */
1099 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1100 if (address_data->probe[0] == I2C_CLIENT_END
1101 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001102 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001103
1104 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1105 "can't probe for chips\n");
1106 return -1;
1107 }
1108
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001109 /* Probe entries are done second, and are not affected by ignore
1110 entries either */
1111 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1112 if (address_data->probe[i] == adap_id
1113 || address_data->probe[i] == ANY_I2C_BUS) {
1114 dev_dbg(&adapter->dev, "found probe parameter for "
1115 "adapter %d, addr 0x%02x\n", adap_id,
1116 address_data->probe[i + 1]);
1117 err = i2c_probe_address(adapter,
1118 address_data->probe[i + 1],
1119 -1, found_proc);
1120 if (err)
1121 return err;
1122 }
1123 }
1124
1125 /* Normal entries are done last, unless shadowed by an ignore entry */
1126 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1127 int j, ignore;
1128
1129 ignore = 0;
1130 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1131 j += 2) {
1132 if ((address_data->ignore[j] == adap_id ||
1133 address_data->ignore[j] == ANY_I2C_BUS)
1134 && address_data->ignore[j + 1]
1135 == address_data->normal_i2c[i]) {
1136 dev_dbg(&adapter->dev, "found ignore "
1137 "parameter for adapter %d, "
1138 "addr 0x%02x\n", adap_id,
1139 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001140 ignore = 1;
1141 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001142 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001143 }
1144 if (ignore)
1145 continue;
1146
1147 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1148 "addr 0x%02x\n", adap_id,
1149 address_data->normal_i2c[i]);
1150 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1151 -1, found_proc);
1152 if (err)
1153 return err;
1154 }
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return 0;
1157}
David Brownellc0564602007-05-01 23:26:31 +02001158EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jean Delvare12b5053a2007-05-01 23:26:31 +02001160struct i2c_client *
1161i2c_new_probed_device(struct i2c_adapter *adap,
1162 struct i2c_board_info *info,
1163 unsigned short const *addr_list)
1164{
1165 int i;
1166
1167 /* Stop here if the bus doesn't support probing */
1168 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1169 dev_err(&adap->dev, "Probing not supported\n");
1170 return NULL;
1171 }
1172
Jean Delvare12b5053a2007-05-01 23:26:31 +02001173 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1174 /* Check address validity */
1175 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1176 dev_warn(&adap->dev, "Invalid 7-bit address "
1177 "0x%02x\n", addr_list[i]);
1178 continue;
1179 }
1180
1181 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001182 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001183 dev_dbg(&adap->dev, "Address 0x%02x already in "
1184 "use, not probing\n", addr_list[i]);
1185 continue;
1186 }
1187
1188 /* Test address responsiveness
1189 The default probe method is a quick write, but it is known
1190 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1191 and could also irreversibly write-protect some EEPROMs, so
1192 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1193 read instead. Also, some bus drivers don't implement
1194 quick write, so we fallback to a byte read it that case
1195 too. */
1196 if ((addr_list[i] & ~0x07) == 0x30
1197 || (addr_list[i] & ~0x0f) == 0x50
1198 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1199 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1200 I2C_SMBUS_READ, 0,
1201 I2C_SMBUS_BYTE, NULL) >= 0)
1202 break;
1203 } else {
1204 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1205 I2C_SMBUS_WRITE, 0,
1206 I2C_SMBUS_QUICK, NULL) >= 0)
1207 break;
1208 }
1209 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001210
1211 if (addr_list[i] == I2C_CLIENT_END) {
1212 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1213 return NULL;
1214 }
1215
1216 info->addr = addr_list[i];
1217 return i2c_new_device(adap, info);
1218}
1219EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221struct i2c_adapter* i2c_get_adapter(int id)
1222{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001224
Jean Delvarecaada322008-01-27 18:14:49 +01001225 mutex_lock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001226 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1227 if (adapter && !try_module_get(adapter->owner))
1228 adapter = NULL;
1229
Jean Delvarecaada322008-01-27 18:14:49 +01001230 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001231 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
David Brownellc0564602007-05-01 23:26:31 +02001233EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235void i2c_put_adapter(struct i2c_adapter *adap)
1236{
1237 module_put(adap->owner);
1238}
David Brownellc0564602007-05-01 23:26:31 +02001239EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241/* The SMBus parts */
1242
David Brownell438d6c22006-12-10 21:21:31 +01001243#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244static u8
1245crc8(u16 data)
1246{
1247 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001250 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 data = data ^ POLY;
1252 data = data << 1;
1253 }
1254 return (u8)(data >> 8);
1255}
1256
Jean Delvare421ef472005-10-26 21:28:55 +02001257/* Incremental CRC8 over count bytes in the array pointed to by p */
1258static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 int i;
1261
1262 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001263 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return crc;
1265}
1266
Jean Delvare421ef472005-10-26 21:28:55 +02001267/* Assume a 7-bit address, which is reasonable for SMBus */
1268static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Jean Delvare421ef472005-10-26 21:28:55 +02001270 /* The address will be sent first */
1271 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1272 pec = i2c_smbus_pec(pec, &addr, 1);
1273
1274 /* The data buffer follows */
1275 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Jean Delvare421ef472005-10-26 21:28:55 +02001278/* Used for write only transactions */
1279static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Jean Delvare421ef472005-10-26 21:28:55 +02001281 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1282 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Jean Delvare421ef472005-10-26 21:28:55 +02001285/* Return <0 on CRC error
1286 If there was a write before this read (most cases) we need to take the
1287 partial CRC from the write part into account.
1288 Note that this function does modify the message (we need to decrease the
1289 message length to hide the CRC byte from the caller). */
1290static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Jean Delvare421ef472005-10-26 21:28:55 +02001292 u8 rpec = msg->buf[--msg->len];
1293 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (rpec != cpec) {
1296 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1297 rpec, cpec);
1298 return -1;
1299 }
David Brownell438d6c22006-12-10 21:21:31 +01001300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1304{
1305 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001306 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
David Brownellc0564602007-05-01 23:26:31 +02001308EXPORT_SYMBOL(i2c_smbus_write_quick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310s32 i2c_smbus_read_byte(struct i2c_client *client)
1311{
1312 union i2c_smbus_data data;
1313 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1314 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1315 return -1;
1316 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001317 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
David Brownellc0564602007-05-01 23:26:31 +02001319EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001324 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
David Brownellc0564602007-05-01 23:26:31 +02001326EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1329{
1330 union i2c_smbus_data data;
1331 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1332 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1333 return -1;
1334 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001335 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
David Brownellc0564602007-05-01 23:26:31 +02001337EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1340{
1341 union i2c_smbus_data data;
1342 data.byte = value;
1343 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1344 I2C_SMBUS_WRITE,command,
1345 I2C_SMBUS_BYTE_DATA,&data);
1346}
David Brownellc0564602007-05-01 23:26:31 +02001347EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1350{
1351 union i2c_smbus_data data;
1352 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1353 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1354 return -1;
1355 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001356 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
David Brownellc0564602007-05-01 23:26:31 +02001358EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1361{
1362 union i2c_smbus_data data;
1363 data.word = value;
1364 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1365 I2C_SMBUS_WRITE,command,
1366 I2C_SMBUS_WORD_DATA,&data);
1367}
David Brownellc0564602007-05-01 23:26:31 +02001368EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
David Brownella64ec072007-10-13 23:56:31 +02001370/**
1371 * i2c_smbus_read_block_data - SMBus block read request
1372 * @client: Handle to slave device
1373 * @command: Command byte issued to let the slave know what data should
1374 * be returned
1375 * @values: Byte array into which data will be read; big enough to hold
1376 * the data returned by the slave. SMBus allows at most 32 bytes.
1377 *
1378 * Returns the number of bytes read in the slave's response, else a
1379 * negative number to indicate some kind of error.
1380 *
1381 * Note that using this function requires that the client's adapter support
1382 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1383 * support this; its emulation through I2C messaging relies on a specific
1384 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1385 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001386s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1387 u8 *values)
1388{
1389 union i2c_smbus_data data;
1390
1391 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1392 I2C_SMBUS_READ, command,
1393 I2C_SMBUS_BLOCK_DATA, &data))
1394 return -1;
1395
1396 memcpy(values, &data.block[1], data.block[0]);
1397 return data.block[0];
1398}
1399EXPORT_SYMBOL(i2c_smbus_read_block_data);
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001402 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (length > I2C_SMBUS_BLOCK_MAX)
1407 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001409 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1411 I2C_SMBUS_WRITE,command,
1412 I2C_SMBUS_BLOCK_DATA,&data);
1413}
David Brownellc0564602007-05-01 23:26:31 +02001414EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001417s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1418 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001421
Jean Delvare4b2643d2007-07-12 14:12:29 +02001422 if (length > I2C_SMBUS_BLOCK_MAX)
1423 length = I2C_SMBUS_BLOCK_MAX;
1424 data.block[0] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1426 I2C_SMBUS_READ,command,
1427 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1428 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001429
1430 memcpy(values, &data.block[1], data.block[0]);
1431 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
David Brownellc0564602007-05-01 23:26:31 +02001433EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Jean Delvare21bbd692006-01-09 15:19:18 +11001435s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001436 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001437{
1438 union i2c_smbus_data data;
1439
1440 if (length > I2C_SMBUS_BLOCK_MAX)
1441 length = I2C_SMBUS_BLOCK_MAX;
1442 data.block[0] = length;
1443 memcpy(data.block + 1, values, length);
1444 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1445 I2C_SMBUS_WRITE, command,
1446 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1447}
David Brownellc0564602007-05-01 23:26:31 +02001448EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001449
David Brownell438d6c22006-12-10 21:21:31 +01001450/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001452static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001454 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 union i2c_smbus_data * data)
1456{
1457 /* So we need to generate a series of msgs. In the case of writing, we
1458 need to use only one message; when reading, we need two. We initialize
1459 most things with sane defaults, to keep the code below somewhat
1460 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001461 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1462 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001464 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1466 };
1467 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001468 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 msgbuf0[0] = command;
1471 switch(size) {
1472 case I2C_SMBUS_QUICK:
1473 msg[0].len = 0;
1474 /* Special case: The read/write field is used as data */
1475 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1476 num = 1;
1477 break;
1478 case I2C_SMBUS_BYTE:
1479 if (read_write == I2C_SMBUS_READ) {
1480 /* Special case: only a read! */
1481 msg[0].flags = I2C_M_RD | flags;
1482 num = 1;
1483 }
1484 break;
1485 case I2C_SMBUS_BYTE_DATA:
1486 if (read_write == I2C_SMBUS_READ)
1487 msg[1].len = 1;
1488 else {
1489 msg[0].len = 2;
1490 msgbuf0[1] = data->byte;
1491 }
1492 break;
1493 case I2C_SMBUS_WORD_DATA:
1494 if (read_write == I2C_SMBUS_READ)
1495 msg[1].len = 2;
1496 else {
1497 msg[0].len=3;
1498 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001499 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 break;
1502 case I2C_SMBUS_PROC_CALL:
1503 num = 2; /* Special case */
1504 read_write = I2C_SMBUS_READ;
1505 msg[0].len = 3;
1506 msg[1].len = 2;
1507 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001508 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
1510 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001512 msg[1].flags |= I2C_M_RECV_LEN;
1513 msg[1].len = 1; /* block length will be added by
1514 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 } else {
1516 msg[0].len = data->block[0] + 2;
1517 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1518 dev_err(&adapter->dev, "smbus_access called with "
1519 "invalid block write size (%d)\n",
1520 data->block[0]);
1521 return -1;
1522 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001523 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 msgbuf0[i] = data->block[i-1];
1525 }
1526 break;
1527 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001528 num = 2; /* Another special case */
1529 read_write = I2C_SMBUS_READ;
1530 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1531 dev_err(&adapter->dev, "%s called with invalid "
Harvey Harrison08882d22008-04-22 22:16:47 +02001532 "block proc call size (%d)\n", __func__,
Jean Delvare209d27c2007-05-01 23:26:29 +02001533 data->block[0]);
1534 return -1;
1535 }
1536 msg[0].len = data->block[0] + 2;
1537 for (i = 1; i < msg[0].len; i++)
1538 msgbuf0[i] = data->block[i-1];
1539 msg[1].flags |= I2C_M_RECV_LEN;
1540 msg[1].len = 1; /* block length will be added by
1541 the underlying bus driver */
1542 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 case I2C_SMBUS_I2C_BLOCK_DATA:
1544 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001545 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 } else {
1547 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001548 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1550 "invalid block write size (%d)\n",
1551 data->block[0]);
1552 return -1;
1553 }
1554 for (i = 1; i <= data->block[0]; i++)
1555 msgbuf0[i] = data->block[i];
1556 }
1557 break;
1558 default:
1559 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1560 size);
1561 return -1;
1562 }
1563
Jean Delvare421ef472005-10-26 21:28:55 +02001564 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1565 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1566 if (i) {
1567 /* Compute PEC if first message is a write */
1568 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001569 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001570 i2c_smbus_add_pec(&msg[0]);
1571 else /* Write followed by read */
1572 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1573 }
1574 /* Ask for PEC if last message is a read */
1575 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001576 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001577 }
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (i2c_transfer(adapter, msg, num) < 0)
1580 return -1;
1581
Jean Delvare421ef472005-10-26 21:28:55 +02001582 /* Check PEC if last message is a read */
1583 if (i && (msg[num-1].flags & I2C_M_RD)) {
1584 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1585 return -1;
1586 }
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (read_write == I2C_SMBUS_READ)
1589 switch(size) {
1590 case I2C_SMBUS_BYTE:
1591 data->byte = msgbuf0[0];
1592 break;
1593 case I2C_SMBUS_BYTE_DATA:
1594 data->byte = msgbuf1[0];
1595 break;
David Brownell438d6c22006-12-10 21:21:31 +01001596 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 case I2C_SMBUS_PROC_CALL:
1598 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1599 break;
1600 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001601 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 data->block[i+1] = msgbuf1[i];
1603 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001604 case I2C_SMBUS_BLOCK_DATA:
1605 case I2C_SMBUS_BLOCK_PROC_CALL:
1606 for (i = 0; i < msgbuf1[0] + 1; i++)
1607 data->block[i] = msgbuf1[i];
1608 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 return 0;
1611}
1612
1613
1614s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001615 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 union i2c_smbus_data * data)
1617{
1618 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001623 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1625 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001626 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 } else
1628 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1629 command,size,data);
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return res;
1632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1636MODULE_DESCRIPTION("I2C-Bus main module");
1637MODULE_LICENSE("GPL");