blob: e7d984866de0465f5846225c6b59a0072fec7fd1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010033#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010034#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010035#include <linux/hardirq.h>
36#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
David Brownell9c1600e2007-05-01 23:26:31 +020039#include "i2c-core.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jean Delvarecaada322008-01-27 18:14:49 +010042static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static DEFINE_IDR(i2c_adapter_idr);
44
Jean Delvare4735c982008-07-14 22:38:36 +020045#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
46
47static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010048
49/* ------------------------------------------------------------------------- */
50
Jean Delvared2653e92008-04-29 23:11:39 +020051static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
52 const struct i2c_client *client)
53{
54 while (id->name[0]) {
55 if (strcmp(client->name, id->name) == 0)
56 return id;
57 id++;
58 }
59 return NULL;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int i2c_device_match(struct device *dev, struct device_driver *drv)
63{
David Brownell7b4fbc52007-05-01 23:26:30 +020064 struct i2c_client *client = to_i2c_client(dev);
65 struct i2c_driver *driver = to_i2c_driver(drv);
66
67 /* make legacy i2c drivers bypass driver model probing entirely;
68 * such drivers scan each i2c adapter/bus themselves.
69 */
David Brownella1d9e6e2007-05-01 23:26:30 +020070 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020071 return 0;
72
Jean Delvared2653e92008-04-29 23:11:39 +020073 /* match on an id table if there is one */
74 if (driver->id_table)
75 return i2c_match_id(driver->id_table, client) != NULL;
76
Jean Delvareeb8a7902008-05-18 20:49:41 +020077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
David Brownell7b4fbc52007-05-01 23:26:30 +020080#ifdef CONFIG_HOTPLUG
81
82/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020083static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020084{
85 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020086
87 /* by definition, legacy drivers can't hotplug */
Jean Delvared2653e92008-04-29 23:11:39 +020088 if (dev->driver)
David Brownell7b4fbc52007-05-01 23:26:30 +020089 return 0;
90
Jean Delvareeb8a7902008-05-18 20:49:41 +020091 if (add_uevent_var(env, "MODALIAS=%s%s",
92 I2C_MODULE_PREFIX, client->name))
93 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020094 dev_dbg(dev, "uevent\n");
95 return 0;
96}
97
98#else
99#define i2c_device_uevent NULL
100#endif /* CONFIG_HOTPLUG */
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int i2c_device_probe(struct device *dev)
103{
David Brownell7b4fbc52007-05-01 23:26:30 +0200104 struct i2c_client *client = to_i2c_client(dev);
105 struct i2c_driver *driver = to_i2c_driver(dev->driver);
Hans Verkuil50c33042008-03-12 14:15:00 +0100106 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200107
Jean Delvaree0457442008-07-14 22:38:30 +0200108 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200109 return -ENODEV;
110 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200111 if (!device_can_wakeup(&client->dev))
112 device_init_wakeup(&client->dev,
113 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200114 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200115
Jean Delvaree0457442008-07-14 22:38:30 +0200116 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100117 if (status)
118 client->driver = NULL;
119 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static int i2c_device_remove(struct device *dev)
123{
David Brownella1d9e6e2007-05-01 23:26:30 +0200124 struct i2c_client *client = to_i2c_client(dev);
125 struct i2c_driver *driver;
126 int status;
127
128 if (!dev->driver)
129 return 0;
130
131 driver = to_i2c_driver(dev->driver);
132 if (driver->remove) {
133 dev_dbg(dev, "remove\n");
134 status = driver->remove(client);
135 } else {
136 dev->driver = NULL;
137 status = 0;
138 }
139 if (status == 0)
140 client->driver = NULL;
141 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
David Brownellf37dd802007-02-13 22:09:00 +0100144static void i2c_device_shutdown(struct device *dev)
145{
146 struct i2c_driver *driver;
147
148 if (!dev->driver)
149 return;
150 driver = to_i2c_driver(dev->driver);
151 if (driver->shutdown)
152 driver->shutdown(to_i2c_client(dev));
153}
154
155static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
156{
157 struct i2c_driver *driver;
158
159 if (!dev->driver)
160 return 0;
161 driver = to_i2c_driver(dev->driver);
162 if (!driver->suspend)
163 return 0;
164 return driver->suspend(to_i2c_client(dev), mesg);
165}
166
167static int i2c_device_resume(struct device * dev)
168{
169 struct i2c_driver *driver;
170
171 if (!dev->driver)
172 return 0;
173 driver = to_i2c_driver(dev->driver);
174 if (!driver->resume)
175 return 0;
176 return driver->resume(to_i2c_client(dev));
177}
178
David Brownell7b4fbc52007-05-01 23:26:30 +0200179static void i2c_client_release(struct device *dev)
180{
181 struct i2c_client *client = to_i2c_client(dev);
182 complete(&client->released);
183}
184
David Brownell9c1600e2007-05-01 23:26:31 +0200185static void i2c_client_dev_release(struct device *dev)
186{
187 kfree(to_i2c_client(dev));
188}
189
David Brownell7b4fbc52007-05-01 23:26:30 +0200190static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
191{
192 struct i2c_client *client = to_i2c_client(dev);
193 return sprintf(buf, "%s\n", client->name);
194}
195
196static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
197{
198 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200199 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200200}
201
202static struct device_attribute i2c_dev_attrs[] = {
203 __ATTR(name, S_IRUGO, show_client_name, NULL),
204 /* modalias helps coldplug: modprobe $(cat .../modalias) */
205 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
206 { },
207};
208
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200209struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100210 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200211 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100212 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200213 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100214 .probe = i2c_device_probe,
215 .remove = i2c_device_remove,
216 .shutdown = i2c_device_shutdown,
217 .suspend = i2c_device_suspend,
218 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000219};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200220EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000221
David Brownell9b766b82008-01-27 18:14:51 +0100222
223/**
224 * i2c_verify_client - return parameter as i2c_client, or NULL
225 * @dev: device, probably from some driver model iterator
226 *
227 * When traversing the driver model tree, perhaps using driver model
228 * iterators like @device_for_each_child(), you can't assume very much
229 * about the nodes you find. Use this function to avoid oopses caused
230 * by wrongly treating some non-I2C device as an i2c_client.
231 */
232struct i2c_client *i2c_verify_client(struct device *dev)
233{
234 return (dev->bus == &i2c_bus_type)
235 ? to_i2c_client(dev)
236 : NULL;
237}
238EXPORT_SYMBOL(i2c_verify_client);
239
240
David Brownell9c1600e2007-05-01 23:26:31 +0200241/**
242 * i2c_new_device - instantiate an i2c device for use with a new style driver
243 * @adap: the adapter managing the device
244 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200245 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200246 *
247 * Create a device to work with a new style i2c driver, where binding is
248 * handled through driver model probe()/remove() methods. This call is not
249 * appropriate for use by mainboad initialization logic, which usually runs
250 * during an arch_initcall() long before any i2c_adapter could exist.
251 *
252 * This returns the new i2c client, which may be saved for later use with
253 * i2c_unregister_device(); or NULL to indicate an error.
254 */
255struct i2c_client *
256i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
257{
258 struct i2c_client *client;
259 int status;
260
261 client = kzalloc(sizeof *client, GFP_KERNEL);
262 if (!client)
263 return NULL;
264
265 client->adapter = adap;
266
267 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200268
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200269 if (info->archdata)
270 client->dev.archdata = *info->archdata;
271
Marc Pignatee354252008-08-28 08:33:22 +0200272 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200273 client->addr = info->addr;
274 client->irq = info->irq;
275
David Brownell9c1600e2007-05-01 23:26:31 +0200276 strlcpy(client->name, info->type, sizeof(client->name));
277
278 /* a new style driver may be bound to this device when we
279 * return from this function, or any later moment (e.g. maybe
280 * hotplugging will load the driver module). and the device
281 * refcount model is the standard driver model one.
282 */
283 status = i2c_attach_client(client);
284 if (status < 0) {
285 kfree(client);
286 client = NULL;
287 }
288 return client;
289}
290EXPORT_SYMBOL_GPL(i2c_new_device);
291
292
293/**
294 * i2c_unregister_device - reverse effect of i2c_new_device()
295 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200296 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200297 */
298void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200299{
300 struct i2c_adapter *adapter = client->adapter;
301 struct i2c_driver *driver = client->driver;
302
303 if (driver && !is_newstyle_driver(driver)) {
304 dev_err(&client->dev, "can't unregister devices "
305 "with legacy drivers\n");
306 WARN_ON(1);
307 return;
308 }
309
Jean Delvare85081592008-07-14 22:38:36 +0200310 if (adapter->client_unregister) {
311 if (adapter->client_unregister(client)) {
312 dev_warn(&client->dev,
313 "client_unregister [%s] failed\n",
314 client->name);
315 }
316 }
317
David Brownella1d9e6e2007-05-01 23:26:30 +0200318 mutex_lock(&adapter->clist_lock);
319 list_del(&client->list);
320 mutex_unlock(&adapter->clist_lock);
321
322 device_unregister(&client->dev);
323}
David Brownell9c1600e2007-05-01 23:26:31 +0200324EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200325
326
Jean Delvare60b129d2008-05-11 20:37:06 +0200327static const struct i2c_device_id dummy_id[] = {
328 { "dummy", 0 },
329 { },
330};
331
Jean Delvared2653e92008-04-29 23:11:39 +0200332static int dummy_probe(struct i2c_client *client,
333 const struct i2c_device_id *id)
334{
335 return 0;
336}
337
338static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100339{
340 return 0;
341}
342
343static struct i2c_driver dummy_driver = {
344 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200345 .probe = dummy_probe,
346 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200347 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100348};
349
350/**
351 * i2c_new_dummy - return a new i2c device bound to a dummy driver
352 * @adapter: the adapter managing the device
353 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100354 * Context: can sleep
355 *
356 * This returns an I2C client bound to the "dummy" driver, intended for use
357 * with devices that consume multiple addresses. Examples of such chips
358 * include various EEPROMS (like 24c04 and 24c08 models).
359 *
360 * These dummy devices have two main uses. First, most I2C and SMBus calls
361 * except i2c_transfer() need a client handle; the dummy will be that handle.
362 * And second, this prevents the specified address from being bound to a
363 * different driver.
364 *
365 * This returns the new i2c client, which should be saved for later use with
366 * i2c_unregister_device(); or NULL to indicate an error.
367 */
368struct i2c_client *
Jean Delvare60b129d2008-05-11 20:37:06 +0200369i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100370{
371 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200372 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100373 };
374
David Brownelle9f13732008-01-27 18:14:52 +0100375 return i2c_new_device(adapter, &info);
376}
377EXPORT_SYMBOL_GPL(i2c_new_dummy);
378
David Brownellf37dd802007-02-13 22:09:00 +0100379/* ------------------------------------------------------------------------- */
380
David Brownell16ffadf2007-05-01 23:26:28 +0200381/* I2C bus adapters -- one roots each I2C or SMBUS segment */
382
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200383static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
David Brownellef2c83212007-05-01 23:26:28 +0200385 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 complete(&adap->dev_released);
387}
388
David Brownell16ffadf2007-05-01 23:26:28 +0200389static ssize_t
390show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
David Brownellef2c83212007-05-01 23:26:28 +0200392 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return sprintf(buf, "%s\n", adap->name);
394}
David Brownell16ffadf2007-05-01 23:26:28 +0200395
396static struct device_attribute i2c_adapter_attrs[] = {
397 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
398 { },
399};
400
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200401static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200402 .owner = THIS_MODULE,
403 .name = "i2c-adapter",
404 .dev_attrs = i2c_adapter_attrs,
405};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
David Brownell9c1600e2007-05-01 23:26:31 +0200407static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
408{
409 struct i2c_devinfo *devinfo;
410
411 mutex_lock(&__i2c_board_lock);
412 list_for_each_entry(devinfo, &__i2c_board_list, list) {
413 if (devinfo->busnum == adapter->nr
414 && !i2c_new_device(adapter,
415 &devinfo->board_info))
416 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
417 i2c_adapter_id(adapter),
418 devinfo->board_info.addr);
419 }
420 mutex_unlock(&__i2c_board_lock);
421}
422
Jean Delvare026526f2008-01-27 18:14:49 +0100423static int i2c_do_add_adapter(struct device_driver *d, void *data)
424{
425 struct i2c_driver *driver = to_i2c_driver(d);
426 struct i2c_adapter *adap = data;
427
Jean Delvare4735c982008-07-14 22:38:36 +0200428 /* Detect supported devices on that bus, and instantiate them */
429 i2c_detect(adap, driver);
430
431 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100432 if (driver->attach_adapter) {
433 /* We ignore the return code; if it fails, too bad */
434 driver->attach_adapter(adap);
435 }
436 return 0;
437}
438
David Brownell6e13e642007-05-01 23:26:31 +0200439static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Jean Delvare026526f2008-01-27 18:14:49 +0100441 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Brownell1d0b19c2008-10-14 17:30:05 +0200443 /* Can't register until after driver model init */
444 if (unlikely(WARN_ON(!i2c_bus_type.p)))
445 return -EAGAIN;
446
Ingo Molnar5c085d32006-01-18 23:16:04 +0100447 mutex_init(&adap->bus_lock);
448 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 INIT_LIST_HEAD(&adap->clients);
450
Jean Delvarecaada322008-01-27 18:14:49 +0100451 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* Add the adapter to the driver core.
454 * If the parent pointer is not set up,
455 * we add this adapter to the host bus.
456 */
David Brownellb119dc32007-01-04 13:07:04 +0100457 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100459 pr_debug("I2C adapter driver [%s] forgot to specify "
460 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100461 }
Kay Sievers27d9c182009-01-07 14:29:16 +0100462 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200464 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200465 res = device_register(&adap->dev);
466 if (res)
467 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200469 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
470
David Brownell6e13e642007-05-01 23:26:31 +0200471 /* create pre-declared device nodes for new-style drivers */
472 if (adap->nr < __i2c_first_dynamic_bus_num)
473 i2c_scan_static_board_info(adap);
474
Jean Delvare4735c982008-07-14 22:38:36 +0200475 /* Notify drivers */
Jean Delvare026526f2008-01-27 18:14:49 +0100476 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
477 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100480 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200482
Jean Delvareb119c6c2006-08-15 18:26:30 +0200483out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200484 idr_remove(&i2c_adapter_idr, adap->nr);
485 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
David Brownell6e13e642007-05-01 23:26:31 +0200488/**
489 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
490 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200491 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200492 *
493 * This routine is used to declare an I2C adapter when its bus number
494 * doesn't matter. Examples: for I2C adapters dynamically added by
495 * USB links or PCI plugin cards.
496 *
497 * When this returns zero, a new bus number was allocated and stored
498 * in adap->nr, and the specified adapter became available for clients.
499 * Otherwise, a negative errno value is returned.
500 */
501int i2c_add_adapter(struct i2c_adapter *adapter)
502{
503 int id, res = 0;
504
505retry:
506 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
507 return -ENOMEM;
508
Jean Delvarecaada322008-01-27 18:14:49 +0100509 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200510 /* "above" here means "above or equal to", sigh */
511 res = idr_get_new_above(&i2c_adapter_idr, adapter,
512 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100513 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200514
515 if (res < 0) {
516 if (res == -EAGAIN)
517 goto retry;
518 return res;
519 }
520
521 adapter->nr = id;
522 return i2c_register_adapter(adapter);
523}
524EXPORT_SYMBOL(i2c_add_adapter);
525
526/**
527 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
528 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200529 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200530 *
531 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100532 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
533 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200534 * is used to properly configure I2C devices.
535 *
536 * If no devices have pre-been declared for this bus, then be sure to
537 * register the adapter before any dynamically allocated ones. Otherwise
538 * the required bus ID may not be available.
539 *
540 * When this returns zero, the specified adapter became available for
541 * clients using the bus number provided in adap->nr. Also, the table
542 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
543 * and the appropriate driver model device nodes are created. Otherwise, a
544 * negative errno value is returned.
545 */
546int i2c_add_numbered_adapter(struct i2c_adapter *adap)
547{
548 int id;
549 int status;
550
551 if (adap->nr & ~MAX_ID_MASK)
552 return -EINVAL;
553
554retry:
555 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
556 return -ENOMEM;
557
Jean Delvarecaada322008-01-27 18:14:49 +0100558 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200559 /* "above" here means "above or equal to", sigh;
560 * we need the "equal to" result to force the result
561 */
562 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
563 if (status == 0 && id != adap->nr) {
564 status = -EBUSY;
565 idr_remove(&i2c_adapter_idr, id);
566 }
Jean Delvarecaada322008-01-27 18:14:49 +0100567 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200568 if (status == -EAGAIN)
569 goto retry;
570
571 if (status == 0)
572 status = i2c_register_adapter(adap);
573 return status;
574}
575EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
576
Jean Delvare026526f2008-01-27 18:14:49 +0100577static int i2c_do_del_adapter(struct device_driver *d, void *data)
578{
579 struct i2c_driver *driver = to_i2c_driver(d);
580 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200581 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100582 int res;
583
Jean Delvare4735c982008-07-14 22:38:36 +0200584 /* Remove the devices we created ourselves */
585 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
586 if (client->adapter == adapter) {
587 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
588 client->name, client->addr);
589 list_del(&client->detected);
590 i2c_unregister_device(client);
591 }
592 }
593
Jean Delvare026526f2008-01-27 18:14:49 +0100594 if (!driver->detach_adapter)
595 return 0;
596 res = driver->detach_adapter(adapter);
597 if (res)
598 dev_err(&adapter->dev, "detach_adapter failed (%d) "
599 "for driver [%s]\n", res, driver->driver.name);
600 return res;
601}
602
David Brownelld64f73b2007-07-12 14:12:28 +0200603/**
604 * i2c_del_adapter - unregister I2C adapter
605 * @adap: the adapter being unregistered
606 * Context: can sleep
607 *
608 * This unregisters an I2C adapter which was previously registered
609 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611int i2c_del_adapter(struct i2c_adapter *adap)
612{
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200613 struct i2c_client *client, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int res = 0;
615
Jean Delvarecaada322008-01-27 18:14:49 +0100616 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100619 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200620 pr_debug("i2c-core: attempting to delete unregistered "
621 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 res = -EINVAL;
623 goto out_unlock;
624 }
625
Jean Delvare026526f2008-01-27 18:14:49 +0100626 /* Tell drivers about this removal */
627 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
628 i2c_do_del_adapter);
629 if (res)
630 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200633 * it can fail; in which case we give up. */
Jean Delvare79b93e12008-11-28 15:24:38 +0100634 list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200635 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
David Brownella1d9e6e2007-05-01 23:26:30 +0200637 driver = client->driver;
638
639 /* new style, follow standard driver model */
640 if (!driver || is_newstyle_driver(driver)) {
641 i2c_unregister_device(client);
642 continue;
643 }
644
645 /* legacy drivers create and remove clients themselves */
646 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200647 dev_err(&adap->dev, "detach_client failed for client "
648 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 client->addr);
650 goto out_unlock;
651 }
652 }
653
654 /* clean up the sysfs representation */
655 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* wait for sysfs to drop all references */
659 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David Brownell6e13e642007-05-01 23:26:31 +0200661 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 idr_remove(&i2c_adapter_idr, adap->nr);
663
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200664 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200666 /* Clear the device structure in case this adapter is ever going to be
667 added again */
668 memset(&adap->dev, 0, sizeof(adap->dev));
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 out_unlock:
Jean Delvarecaada322008-01-27 18:14:49 +0100671 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return res;
673}
David Brownellc0564602007-05-01 23:26:31 +0200674EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676
David Brownell7b4fbc52007-05-01 23:26:30 +0200677/* ------------------------------------------------------------------------- */
678
Dave Young7f101a92008-07-14 22:38:19 +0200679static int __attach_adapter(struct device *dev, void *data)
680{
681 struct i2c_adapter *adapter = to_i2c_adapter(dev);
682 struct i2c_driver *driver = data;
683
Jean Delvare4735c982008-07-14 22:38:36 +0200684 i2c_detect(adapter, driver);
685
686 /* Legacy drivers scan i2c busses directly */
687 if (driver->attach_adapter)
688 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200689
690 return 0;
691}
692
David Brownell7b4fbc52007-05-01 23:26:30 +0200693/*
694 * An i2c_driver is used with one or more i2c_client (device) nodes to access
695 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
696 * are two models for binding the driver to its device: "new style" drivers
697 * follow the standard Linux driver model and just respond to probe() calls
698 * issued if the driver core sees they match(); "legacy" drivers create device
699 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
701
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800702int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100704 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
David Brownell1d0b19c2008-10-14 17:30:05 +0200706 /* Can't register until after driver model init */
707 if (unlikely(WARN_ON(!i2c_bus_type.p)))
708 return -EAGAIN;
709
David Brownell7b4fbc52007-05-01 23:26:30 +0200710 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200711 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200712 if (driver->attach_adapter || driver->detach_adapter
713 || driver->detach_client) {
714 printk(KERN_WARNING
715 "i2c-core: driver [%s] is confused\n",
716 driver->driver.name);
717 return -EINVAL;
718 }
719 }
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800722 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
David Brownell6e13e642007-05-01 23:26:31 +0200725 /* for new style drivers, when registration returns the driver core
726 * will have called probe() for all matching-but-unbound devices.
727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 res = driver_register(&driver->driver);
729 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100730 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100731
Jean Delvarecaada322008-01-27 18:14:49 +0100732 mutex_lock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100733
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100734 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jean Delvare4735c982008-07-14 22:38:36 +0200736 INIT_LIST_HEAD(&driver->clients);
737 /* Walk the adapters that are already present */
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400738 class_for_each_device(&i2c_adapter_class, NULL, driver,
739 __attach_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jean Delvarecaada322008-01-27 18:14:49 +0100741 mutex_unlock(&core_lock);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800744EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Dave Young7f101a92008-07-14 22:38:19 +0200746static int __detach_adapter(struct device *dev, void *data)
747{
748 struct i2c_adapter *adapter = to_i2c_adapter(dev);
749 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200750 struct i2c_client *client, *_n;
751
752 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
753 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
754 client->name, client->addr);
755 list_del(&client->detected);
756 i2c_unregister_device(client);
757 }
758
759 if (is_newstyle_driver(driver))
760 return 0;
Dave Young7f101a92008-07-14 22:38:19 +0200761
762 /* Have a look at each adapter, if clients of this driver are still
763 * attached. If so, detach them to be able to kill the driver
764 * afterwards.
765 */
766 if (driver->detach_adapter) {
767 if (driver->detach_adapter(adapter))
768 dev_err(&adapter->dev,
769 "detach_adapter failed for driver [%s]\n",
770 driver->driver.name);
771 } else {
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200772 struct i2c_client *client, *_n;
Dave Young7f101a92008-07-14 22:38:19 +0200773
Matthias Kaehlcke6a03cd92008-07-14 22:38:26 +0200774 list_for_each_entry_safe(client, _n, &adapter->clients, list) {
Dave Young7f101a92008-07-14 22:38:19 +0200775 if (client->driver != driver)
776 continue;
777 dev_dbg(&adapter->dev,
778 "detaching client [%s] at 0x%02x\n",
779 client->name, client->addr);
780 if (driver->detach_client(client))
781 dev_err(&adapter->dev, "detach_client "
782 "failed for client [%s] at 0x%02x\n",
783 client->name, client->addr);
784 }
785 }
786
787 return 0;
788}
789
David Brownella1d9e6e2007-05-01 23:26:30 +0200790/**
791 * i2c_del_driver - unregister I2C driver
792 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200793 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200794 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200795void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Jean Delvarecaada322008-01-27 18:14:49 +0100797 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400799 class_for_each_device(&i2c_adapter_class, NULL, driver,
800 __detach_adapter);
David Brownella1d9e6e2007-05-01 23:26:30 +0200801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100803 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jean Delvarecaada322008-01-27 18:14:49 +0100805 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
David Brownellc0564602007-05-01 23:26:31 +0200807EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
David Brownell7b4fbc52007-05-01 23:26:30 +0200809/* ------------------------------------------------------------------------- */
810
David Brownell9b766b82008-01-27 18:14:51 +0100811static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
David Brownell9b766b82008-01-27 18:14:51 +0100813 struct i2c_client *client = i2c_verify_client(dev);
814 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
David Brownell9b766b82008-01-27 18:14:51 +0100816 if (client && client->addr == addr)
817 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return 0;
819}
820
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100821static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
David Brownell9b766b82008-01-27 18:14:51 +0100823 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826int i2c_attach_client(struct i2c_client *client)
827{
828 struct i2c_adapter *adapter = client->adapter;
Jean Delvarec1159f92008-08-10 22:56:16 +0200829 int res;
830
831 /* Check for address business */
832 res = i2c_check_addr(adapter, client->addr);
833 if (res)
834 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200838
839 if (client->driver)
840 client->dev.driver = &client->driver->driver;
841
David Brownellde81d2a2007-05-22 19:49:16 +0200842 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200843 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200844 client->dev.uevent_suppress = 1;
845 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200846 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100847
Kay Sievers27d9c182009-01-07 14:29:16 +0100848 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
849 client->addr);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200850 res = device_register(&client->dev);
851 if (res)
David Brownell86ec5ec2008-01-27 18:14:51 +0100852 goto out_err;
853
854 mutex_lock(&adapter->clist_lock);
855 list_add_tail(&client->list, &adapter->clients);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200856 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200857
David Brownell86ec5ec2008-01-27 18:14:51 +0100858 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
Kay Sievers27d9c182009-01-07 14:29:16 +0100859 client->name, dev_name(&client->dev));
David Brownell86ec5ec2008-01-27 18:14:51 +0100860
Jean Delvare77ed74d2006-09-30 17:18:59 +0200861 if (adapter->client_register) {
862 if (adapter->client_register(client)) {
863 dev_dbg(&adapter->dev, "client_register "
864 "failed for client [%s] at 0x%02x\n",
865 client->name, client->addr);
866 }
867 }
868
869 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200870
David Brownell86ec5ec2008-01-27 18:14:51 +0100871out_err:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200872 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
873 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200874 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
David Brownellc0564602007-05-01 23:26:31 +0200876EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878int i2c_detach_client(struct i2c_client *client)
879{
880 struct i2c_adapter *adapter = client->adapter;
881 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (adapter->client_unregister) {
884 res = adapter->client_unregister(client);
885 if (res) {
886 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700887 "client_unregister [%s] failed, "
888 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto out;
890 }
891 }
892
Ingo Molnar5c085d32006-01-18 23:16:04 +0100893 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 list_del(&client->list);
Jean Delvare9ddced12008-01-27 18:14:51 +0100895 mutex_unlock(&adapter->clist_lock);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 device_unregister(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 wait_for_completion(&client->released);
900
901 out:
902 return res;
903}
David Brownellc0564602007-05-01 23:26:31 +0200904EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Jean Delvaree48d3312008-01-27 18:14:48 +0100906/**
907 * i2c_use_client - increments the reference count of the i2c client structure
908 * @client: the client being referenced
909 *
910 * Each live reference to a client should be refcounted. The driver model does
911 * that automatically as part of driver binding, so that most drivers don't
912 * need to do this explicitly: they hold a reference until they're unbound
913 * from the device.
914 *
915 * A pointer to the client with the incremented reference counter is returned.
916 */
917struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
David Brownell6ea438e2008-07-14 22:38:24 +0200919 if (client && get_device(&client->dev))
920 return client;
921 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
David Brownellc0564602007-05-01 23:26:31 +0200923EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Jean Delvaree48d3312008-01-27 18:14:48 +0100925/**
926 * i2c_release_client - release a use of the i2c client structure
927 * @client: the client being no longer referenced
928 *
929 * Must be called when a user of a client is finished with it.
930 */
931void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
David Brownell6ea438e2008-07-14 22:38:24 +0200933 if (client)
934 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
David Brownellc0564602007-05-01 23:26:31 +0200936EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
David Brownell9b766b82008-01-27 18:14:51 +0100938struct i2c_cmd_arg {
939 unsigned cmd;
940 void *arg;
941};
942
943static int i2c_cmd(struct device *dev, void *_arg)
944{
945 struct i2c_client *client = i2c_verify_client(dev);
946 struct i2c_cmd_arg *arg = _arg;
947
948 if (client && client->driver && client->driver->command)
949 client->driver->command(client, arg->cmd, arg->arg);
950 return 0;
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
954{
David Brownell9b766b82008-01-27 18:14:51 +0100955 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
David Brownell9b766b82008-01-27 18:14:51 +0100957 cmd_arg.cmd = cmd;
958 cmd_arg.arg = arg;
959 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
David Brownellc0564602007-05-01 23:26:31 +0200961EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963static int __init i2c_init(void)
964{
965 int retval;
966
967 retval = bus_register(&i2c_bus_type);
968 if (retval)
969 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100970 retval = class_register(&i2c_adapter_class);
971 if (retval)
972 goto bus_err;
973 retval = i2c_add_driver(&dummy_driver);
974 if (retval)
975 goto class_err;
976 return 0;
977
978class_err:
979 class_unregister(&i2c_adapter_class);
980bus_err:
981 bus_unregister(&i2c_bus_type);
982 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static void __exit i2c_exit(void)
986{
David Brownelle9f13732008-01-27 18:14:52 +0100987 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 bus_unregister(&i2c_bus_type);
990}
991
David Brownella10f9e72008-10-14 17:30:06 +0200992/* We must initialize early, because some subsystems register i2c drivers
993 * in subsys_initcall() code, but are linked (and initialized) before i2c.
994 */
995postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996module_exit(i2c_exit);
997
998/* ----------------------------------------------------
999 * the functional interface to the i2c busses.
1000 * ----------------------------------------------------
1001 */
1002
David Brownella1cdeda2008-07-14 22:38:24 +02001003/**
1004 * i2c_transfer - execute a single or combined I2C message
1005 * @adap: Handle to I2C bus
1006 * @msgs: One or more messages to execute before STOP is issued to
1007 * terminate the operation; each message begins with a START.
1008 * @num: Number of messages to be executed.
1009 *
1010 * Returns negative errno, else the number of messages executed.
1011 *
1012 * Note that there is no requirement that each message be sent to
1013 * the same slave address, although that is the most common model.
1014 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
1016{
1017 int ret;
1018
David Brownella1cdeda2008-07-14 22:38:24 +02001019 /* REVISIT the fault reporting model here is weak:
1020 *
1021 * - When we get an error after receiving N bytes from a slave,
1022 * there is no way to report "N".
1023 *
1024 * - When we get a NAK after transmitting N bytes to a slave,
1025 * there is no way to report "N" ... or to let the master
1026 * continue executing the rest of this combined message, if
1027 * that's the appropriate response.
1028 *
1029 * - When for example "num" is two and we successfully complete
1030 * the first message but get an error part way through the
1031 * second, it's unclear whether that should be reported as
1032 * one (discarding status on the second message) or errno
1033 * (discarding status on the first one).
1034 */
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (adap->algo->master_xfer) {
1037#ifdef DEBUG
1038 for (ret = 0; ret < num; ret++) {
1039 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001040 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1041 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1042 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044#endif
1045
Mike Rapoportcea443a2008-01-27 18:14:50 +01001046 if (in_atomic() || irqs_disabled()) {
1047 ret = mutex_trylock(&adap->bus_lock);
1048 if (!ret)
1049 /* I2C activity is ongoing. */
1050 return -EAGAIN;
1051 } else {
1052 mutex_lock_nested(&adap->bus_lock, adap->level);
1053 }
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001056 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 return ret;
1059 } else {
1060 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001061 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063}
David Brownellc0564602007-05-01 23:26:31 +02001064EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
David Brownella1cdeda2008-07-14 22:38:24 +02001066/**
1067 * i2c_master_send - issue a single I2C message in master transmit mode
1068 * @client: Handle to slave device
1069 * @buf: Data that will be written to the slave
1070 * @count: How many bytes to write
1071 *
1072 * Returns negative errno, or else the number of bytes written.
1073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1075{
1076 int ret;
1077 struct i2c_adapter *adap=client->adapter;
1078 struct i2c_msg msg;
1079
Jean Delvare815f55f2005-05-07 22:58:46 +02001080 msg.addr = client->addr;
1081 msg.flags = client->flags & I2C_M_TEN;
1082 msg.len = count;
1083 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001084
Jean Delvare815f55f2005-05-07 22:58:46 +02001085 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Jean Delvare815f55f2005-05-07 22:58:46 +02001087 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1088 transmitted, else error code. */
1089 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
David Brownellc0564602007-05-01 23:26:31 +02001091EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
David Brownella1cdeda2008-07-14 22:38:24 +02001093/**
1094 * i2c_master_recv - issue a single I2C message in master receive mode
1095 * @client: Handle to slave device
1096 * @buf: Where to store data read from slave
1097 * @count: How many bytes to read
1098 *
1099 * Returns negative errno, or else the number of bytes read.
1100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1102{
1103 struct i2c_adapter *adap=client->adapter;
1104 struct i2c_msg msg;
1105 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jean Delvare815f55f2005-05-07 22:58:46 +02001107 msg.addr = client->addr;
1108 msg.flags = client->flags & I2C_M_TEN;
1109 msg.flags |= I2C_M_RD;
1110 msg.len = count;
1111 msg.buf = buf;
1112
1113 ret = i2c_transfer(adap, &msg, 1);
1114
1115 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1116 transmitted, else error code. */
1117 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
David Brownellc0564602007-05-01 23:26:31 +02001119EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/* ----------------------------------------------------
1122 * the i2c address scanning function
1123 * Will not work for 10-bit addresses!
1124 * ----------------------------------------------------
1125 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001126static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1127 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001128{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001129 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001130
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001131 /* Make sure the address is valid */
1132 if (addr < 0x03 || addr > 0x77) {
1133 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1134 addr);
1135 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001136 }
1137
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001138 /* Skip if already in use */
1139 if (i2c_check_addr(adapter, addr))
1140 return 0;
1141
1142 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +02001143 if (kind < 0) {
1144 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1145 I2C_SMBUS_QUICK, NULL) < 0)
1146 return 0;
1147
1148 /* prevent 24RF08 corruption */
1149 if ((addr & ~0x0f) == 0x50)
1150 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1151 I2C_SMBUS_QUICK, NULL);
1152 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001153
1154 /* Finally call the custom detection function */
1155 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001156 /* -ENODEV can be returned if there is a chip at the given address
1157 but it isn't supported by this chip driver. We catch it here as
1158 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +02001159 if (err == -ENODEV)
1160 err = 0;
1161
1162 if (err)
1163 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1164 addr, err);
1165 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001169 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 int (*found_proc) (struct i2c_adapter *, int, int))
1171{
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001172 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int adap_id = i2c_adapter_id(adapter);
1174
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001175 /* Force entries are done first, and are not affected by ignore
1176 entries */
1177 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +01001178 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001179 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001181 for (kind = 0; forces[kind]; kind++) {
1182 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1183 i += 2) {
1184 if (forces[kind][i] == adap_id
1185 || forces[kind][i] == ANY_I2C_BUS) {
1186 dev_dbg(&adapter->dev, "found force "
1187 "parameter for adapter %d, "
1188 "addr 0x%02x, kind %d\n",
1189 adap_id, forces[kind][i + 1],
1190 kind);
1191 err = i2c_probe_address(adapter,
1192 forces[kind][i + 1],
1193 kind, found_proc);
1194 if (err)
1195 return err;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001200
Jean Delvare4366dc92005-09-25 16:50:06 +02001201 /* Stop here if we can't use SMBUS_QUICK */
1202 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1203 if (address_data->probe[0] == I2C_CLIENT_END
1204 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +01001205 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001206
Jean Delvare4329cf82008-08-28 08:33:23 +02001207 dev_dbg(&adapter->dev, "SMBus Quick command not supported, "
1208 "can't probe for chips\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001209 return -EOPNOTSUPP;
Jean Delvare4366dc92005-09-25 16:50:06 +02001210 }
1211
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001212 /* Probe entries are done second, and are not affected by ignore
1213 entries either */
1214 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1215 if (address_data->probe[i] == adap_id
1216 || address_data->probe[i] == ANY_I2C_BUS) {
1217 dev_dbg(&adapter->dev, "found probe parameter for "
1218 "adapter %d, addr 0x%02x\n", adap_id,
1219 address_data->probe[i + 1]);
1220 err = i2c_probe_address(adapter,
1221 address_data->probe[i + 1],
1222 -1, found_proc);
1223 if (err)
1224 return err;
1225 }
1226 }
1227
1228 /* Normal entries are done last, unless shadowed by an ignore entry */
1229 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1230 int j, ignore;
1231
1232 ignore = 0;
1233 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1234 j += 2) {
1235 if ((address_data->ignore[j] == adap_id ||
1236 address_data->ignore[j] == ANY_I2C_BUS)
1237 && address_data->ignore[j + 1]
1238 == address_data->normal_i2c[i]) {
1239 dev_dbg(&adapter->dev, "found ignore "
1240 "parameter for adapter %d, "
1241 "addr 0x%02x\n", adap_id,
1242 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001243 ignore = 1;
1244 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001245 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001246 }
1247 if (ignore)
1248 continue;
1249
1250 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1251 "addr 0x%02x\n", adap_id,
1252 address_data->normal_i2c[i]);
1253 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1254 -1, found_proc);
1255 if (err)
1256 return err;
1257 }
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260}
David Brownellc0564602007-05-01 23:26:31 +02001261EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jean Delvare4735c982008-07-14 22:38:36 +02001263/* Separate detection function for new-style drivers */
1264static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1265 struct i2c_driver *driver)
1266{
1267 struct i2c_board_info info;
1268 struct i2c_adapter *adapter = temp_client->adapter;
1269 int addr = temp_client->addr;
1270 int err;
1271
1272 /* Make sure the address is valid */
1273 if (addr < 0x03 || addr > 0x77) {
1274 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1275 addr);
1276 return -EINVAL;
1277 }
1278
1279 /* Skip if already in use */
1280 if (i2c_check_addr(adapter, addr))
1281 return 0;
1282
1283 /* Make sure there is something at this address, unless forced */
1284 if (kind < 0) {
1285 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1286 I2C_SMBUS_QUICK, NULL) < 0)
1287 return 0;
1288
1289 /* prevent 24RF08 corruption */
1290 if ((addr & ~0x0f) == 0x50)
1291 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1292 I2C_SMBUS_QUICK, NULL);
1293 }
1294
1295 /* Finally call the custom detection function */
1296 memset(&info, 0, sizeof(struct i2c_board_info));
1297 info.addr = addr;
1298 err = driver->detect(temp_client, kind, &info);
1299 if (err) {
1300 /* -ENODEV is returned if the detection fails. We catch it
1301 here as this isn't an error. */
1302 return err == -ENODEV ? 0 : err;
1303 }
1304
1305 /* Consistency check */
1306 if (info.type[0] == '\0') {
1307 dev_err(&adapter->dev, "%s detection function provided "
1308 "no name for 0x%x\n", driver->driver.name,
1309 addr);
1310 } else {
1311 struct i2c_client *client;
1312
1313 /* Detection succeeded, instantiate the device */
1314 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1315 info.type, info.addr);
1316 client = i2c_new_device(adapter, &info);
1317 if (client)
1318 list_add_tail(&client->detected, &driver->clients);
1319 else
1320 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1321 info.type, info.addr);
1322 }
1323 return 0;
1324}
1325
1326static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1327{
1328 const struct i2c_client_address_data *address_data;
1329 struct i2c_client *temp_client;
1330 int i, err = 0;
1331 int adap_id = i2c_adapter_id(adapter);
1332
1333 address_data = driver->address_data;
1334 if (!driver->detect || !address_data)
1335 return 0;
1336
1337 /* Set up a temporary client to help detect callback */
1338 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1339 if (!temp_client)
1340 return -ENOMEM;
1341 temp_client->adapter = adapter;
1342
1343 /* Force entries are done first, and are not affected by ignore
1344 entries */
1345 if (address_data->forces) {
1346 const unsigned short * const *forces = address_data->forces;
1347 int kind;
1348
1349 for (kind = 0; forces[kind]; kind++) {
1350 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1351 i += 2) {
1352 if (forces[kind][i] == adap_id
1353 || forces[kind][i] == ANY_I2C_BUS) {
1354 dev_dbg(&adapter->dev, "found force "
1355 "parameter for adapter %d, "
1356 "addr 0x%02x, kind %d\n",
1357 adap_id, forces[kind][i + 1],
1358 kind);
1359 temp_client->addr = forces[kind][i + 1];
1360 err = i2c_detect_address(temp_client,
1361 kind, driver);
1362 if (err)
1363 goto exit_free;
1364 }
1365 }
1366 }
1367 }
1368
Jean Delvare4329cf82008-08-28 08:33:23 +02001369 /* Stop here if the classes do not match */
1370 if (!(adapter->class & driver->class))
1371 goto exit_free;
1372
Jean Delvare4735c982008-07-14 22:38:36 +02001373 /* Stop here if we can't use SMBUS_QUICK */
1374 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1375 if (address_data->probe[0] == I2C_CLIENT_END
1376 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1377 goto exit_free;
1378
1379 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1380 "can't probe for chips\n");
1381 err = -EOPNOTSUPP;
1382 goto exit_free;
1383 }
1384
Jean Delvare4735c982008-07-14 22:38:36 +02001385 /* Probe entries are done second, and are not affected by ignore
1386 entries either */
1387 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1388 if (address_data->probe[i] == adap_id
1389 || address_data->probe[i] == ANY_I2C_BUS) {
1390 dev_dbg(&adapter->dev, "found probe parameter for "
1391 "adapter %d, addr 0x%02x\n", adap_id,
1392 address_data->probe[i + 1]);
1393 temp_client->addr = address_data->probe[i + 1];
1394 err = i2c_detect_address(temp_client, -1, driver);
1395 if (err)
1396 goto exit_free;
1397 }
1398 }
1399
1400 /* Normal entries are done last, unless shadowed by an ignore entry */
1401 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1402 int j, ignore;
1403
1404 ignore = 0;
1405 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1406 j += 2) {
1407 if ((address_data->ignore[j] == adap_id ||
1408 address_data->ignore[j] == ANY_I2C_BUS)
1409 && address_data->ignore[j + 1]
1410 == address_data->normal_i2c[i]) {
1411 dev_dbg(&adapter->dev, "found ignore "
1412 "parameter for adapter %d, "
1413 "addr 0x%02x\n", adap_id,
1414 address_data->ignore[j + 1]);
1415 ignore = 1;
1416 break;
1417 }
1418 }
1419 if (ignore)
1420 continue;
1421
1422 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1423 "addr 0x%02x\n", adap_id,
1424 address_data->normal_i2c[i]);
1425 temp_client->addr = address_data->normal_i2c[i];
1426 err = i2c_detect_address(temp_client, -1, driver);
1427 if (err)
1428 goto exit_free;
1429 }
1430
1431 exit_free:
1432 kfree(temp_client);
1433 return err;
1434}
1435
Jean Delvare12b5053a2007-05-01 23:26:31 +02001436struct i2c_client *
1437i2c_new_probed_device(struct i2c_adapter *adap,
1438 struct i2c_board_info *info,
1439 unsigned short const *addr_list)
1440{
1441 int i;
1442
1443 /* Stop here if the bus doesn't support probing */
1444 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1445 dev_err(&adap->dev, "Probing not supported\n");
1446 return NULL;
1447 }
1448
Jean Delvare12b5053a2007-05-01 23:26:31 +02001449 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1450 /* Check address validity */
1451 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1452 dev_warn(&adap->dev, "Invalid 7-bit address "
1453 "0x%02x\n", addr_list[i]);
1454 continue;
1455 }
1456
1457 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001458 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001459 dev_dbg(&adap->dev, "Address 0x%02x already in "
1460 "use, not probing\n", addr_list[i]);
1461 continue;
1462 }
1463
1464 /* Test address responsiveness
1465 The default probe method is a quick write, but it is known
1466 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1467 and could also irreversibly write-protect some EEPROMs, so
1468 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1469 read instead. Also, some bus drivers don't implement
1470 quick write, so we fallback to a byte read it that case
1471 too. */
1472 if ((addr_list[i] & ~0x07) == 0x30
1473 || (addr_list[i] & ~0x0f) == 0x50
1474 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001475 union i2c_smbus_data data;
1476
Jean Delvare12b5053a2007-05-01 23:26:31 +02001477 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1478 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001479 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001480 break;
1481 } else {
1482 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1483 I2C_SMBUS_WRITE, 0,
1484 I2C_SMBUS_QUICK, NULL) >= 0)
1485 break;
1486 }
1487 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001488
1489 if (addr_list[i] == I2C_CLIENT_END) {
1490 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1491 return NULL;
1492 }
1493
1494 info->addr = addr_list[i];
1495 return i2c_new_device(adap, info);
1496}
1497EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499struct i2c_adapter* i2c_get_adapter(int id)
1500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001502
Jean Delvarecaada322008-01-27 18:14:49 +01001503 mutex_lock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001504 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1505 if (adapter && !try_module_get(adapter->owner))
1506 adapter = NULL;
1507
Jean Delvarecaada322008-01-27 18:14:49 +01001508 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001509 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
David Brownellc0564602007-05-01 23:26:31 +02001511EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513void i2c_put_adapter(struct i2c_adapter *adap)
1514{
1515 module_put(adap->owner);
1516}
David Brownellc0564602007-05-01 23:26:31 +02001517EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519/* The SMBus parts */
1520
David Brownell438d6c22006-12-10 21:21:31 +01001521#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522static u8
1523crc8(u16 data)
1524{
1525 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001528 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 data = data ^ POLY;
1530 data = data << 1;
1531 }
1532 return (u8)(data >> 8);
1533}
1534
Jean Delvare421ef472005-10-26 21:28:55 +02001535/* Incremental CRC8 over count bytes in the array pointed to by p */
1536static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 int i;
1539
1540 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001541 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return crc;
1543}
1544
Jean Delvare421ef472005-10-26 21:28:55 +02001545/* Assume a 7-bit address, which is reasonable for SMBus */
1546static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Jean Delvare421ef472005-10-26 21:28:55 +02001548 /* The address will be sent first */
1549 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1550 pec = i2c_smbus_pec(pec, &addr, 1);
1551
1552 /* The data buffer follows */
1553 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Jean Delvare421ef472005-10-26 21:28:55 +02001556/* Used for write only transactions */
1557static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Jean Delvare421ef472005-10-26 21:28:55 +02001559 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1560 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Jean Delvare421ef472005-10-26 21:28:55 +02001563/* Return <0 on CRC error
1564 If there was a write before this read (most cases) we need to take the
1565 partial CRC from the write part into account.
1566 Note that this function does modify the message (we need to decrease the
1567 message length to hide the CRC byte from the caller). */
1568static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Jean Delvare421ef472005-10-26 21:28:55 +02001570 u8 rpec = msg->buf[--msg->len];
1571 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (rpec != cpec) {
1574 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1575 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001576 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
David Brownell438d6c22006-12-10 21:21:31 +01001578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
David Brownella1cdeda2008-07-14 22:38:24 +02001581/**
1582 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1583 * @client: Handle to slave device
1584 *
1585 * This executes the SMBus "receive byte" protocol, returning negative errno
1586 * else the byte received from the device.
1587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588s32 i2c_smbus_read_byte(struct i2c_client *client)
1589{
1590 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001591 int status;
1592
1593 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1594 I2C_SMBUS_READ, 0,
1595 I2C_SMBUS_BYTE, &data);
1596 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
David Brownellc0564602007-05-01 23:26:31 +02001598EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
David Brownella1cdeda2008-07-14 22:38:24 +02001600/**
1601 * i2c_smbus_write_byte - SMBus "send byte" protocol
1602 * @client: Handle to slave device
1603 * @value: Byte to be sent
1604 *
1605 * This executes the SMBus "send byte" protocol, returning negative errno
1606 * else zero on success.
1607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1609{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001611 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
David Brownellc0564602007-05-01 23:26:31 +02001613EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
David Brownella1cdeda2008-07-14 22:38:24 +02001615/**
1616 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1617 * @client: Handle to slave device
1618 * @command: Byte interpreted by slave
1619 *
1620 * This executes the SMBus "read byte" protocol, returning negative errno
1621 * else a data byte received from the device.
1622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1624{
1625 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001626 int status;
1627
1628 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1629 I2C_SMBUS_READ, command,
1630 I2C_SMBUS_BYTE_DATA, &data);
1631 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
David Brownellc0564602007-05-01 23:26:31 +02001633EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
David Brownella1cdeda2008-07-14 22:38:24 +02001635/**
1636 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1637 * @client: Handle to slave device
1638 * @command: Byte interpreted by slave
1639 * @value: Byte being written
1640 *
1641 * This executes the SMBus "write byte" protocol, returning negative errno
1642 * else zero on success.
1643 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1645{
1646 union i2c_smbus_data data;
1647 data.byte = value;
1648 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1649 I2C_SMBUS_WRITE,command,
1650 I2C_SMBUS_BYTE_DATA,&data);
1651}
David Brownellc0564602007-05-01 23:26:31 +02001652EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
David Brownella1cdeda2008-07-14 22:38:24 +02001654/**
1655 * i2c_smbus_read_word_data - SMBus "read word" protocol
1656 * @client: Handle to slave device
1657 * @command: Byte interpreted by slave
1658 *
1659 * This executes the SMBus "read word" protocol, returning negative errno
1660 * else a 16-bit unsigned "word" received from the device.
1661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1663{
1664 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001665 int status;
1666
1667 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1668 I2C_SMBUS_READ, command,
1669 I2C_SMBUS_WORD_DATA, &data);
1670 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
David Brownellc0564602007-05-01 23:26:31 +02001672EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
David Brownella1cdeda2008-07-14 22:38:24 +02001674/**
1675 * i2c_smbus_write_word_data - SMBus "write word" protocol
1676 * @client: Handle to slave device
1677 * @command: Byte interpreted by slave
1678 * @value: 16-bit "word" being written
1679 *
1680 * This executes the SMBus "write word" protocol, returning negative errno
1681 * else zero on success.
1682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1684{
1685 union i2c_smbus_data data;
1686 data.word = value;
1687 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1688 I2C_SMBUS_WRITE,command,
1689 I2C_SMBUS_WORD_DATA,&data);
1690}
David Brownellc0564602007-05-01 23:26:31 +02001691EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
David Brownella64ec072007-10-13 23:56:31 +02001693/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001694 * i2c_smbus_process_call - SMBus "process call" protocol
1695 * @client: Handle to slave device
1696 * @command: Byte interpreted by slave
1697 * @value: 16-bit "word" being written
1698 *
1699 * This executes the SMBus "process call" protocol, returning negative errno
1700 * else a 16-bit unsigned "word" received from the device.
1701 */
1702s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1703{
1704 union i2c_smbus_data data;
1705 int status;
1706 data.word = value;
1707
1708 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1709 I2C_SMBUS_WRITE, command,
1710 I2C_SMBUS_PROC_CALL, &data);
1711 return (status < 0) ? status : data.word;
1712}
1713EXPORT_SYMBOL(i2c_smbus_process_call);
1714
1715/**
David Brownella1cdeda2008-07-14 22:38:24 +02001716 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001717 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001718 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001719 * @values: Byte array into which data will be read; big enough to hold
1720 * the data returned by the slave. SMBus allows at most 32 bytes.
1721 *
David Brownella1cdeda2008-07-14 22:38:24 +02001722 * This executes the SMBus "block read" protocol, returning negative errno
1723 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001724 *
1725 * Note that using this function requires that the client's adapter support
1726 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1727 * support this; its emulation through I2C messaging relies on a specific
1728 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1729 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001730s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1731 u8 *values)
1732{
1733 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001734 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001735
David Brownell24a5bb72008-07-14 22:38:23 +02001736 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1737 I2C_SMBUS_READ, command,
1738 I2C_SMBUS_BLOCK_DATA, &data);
1739 if (status)
1740 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001741
1742 memcpy(values, &data.block[1], data.block[0]);
1743 return data.block[0];
1744}
1745EXPORT_SYMBOL(i2c_smbus_read_block_data);
1746
David Brownella1cdeda2008-07-14 22:38:24 +02001747/**
1748 * i2c_smbus_write_block_data - SMBus "block write" protocol
1749 * @client: Handle to slave device
1750 * @command: Byte interpreted by slave
1751 * @length: Size of data block; SMBus allows at most 32 bytes
1752 * @values: Byte array which will be written.
1753 *
1754 * This executes the SMBus "block write" protocol, returning negative errno
1755 * else zero on success.
1756 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001758 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (length > I2C_SMBUS_BLOCK_MAX)
1763 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001765 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1767 I2C_SMBUS_WRITE,command,
1768 I2C_SMBUS_BLOCK_DATA,&data);
1769}
David Brownellc0564602007-05-01 23:26:31 +02001770EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001773s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1774 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001777 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001778
Jean Delvare4b2643d2007-07-12 14:12:29 +02001779 if (length > I2C_SMBUS_BLOCK_MAX)
1780 length = I2C_SMBUS_BLOCK_MAX;
1781 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001782 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1783 I2C_SMBUS_READ, command,
1784 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1785 if (status < 0)
1786 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001787
1788 memcpy(values, &data.block[1], data.block[0]);
1789 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
David Brownellc0564602007-05-01 23:26:31 +02001791EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Jean Delvare21bbd692006-01-09 15:19:18 +11001793s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001794 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001795{
1796 union i2c_smbus_data data;
1797
1798 if (length > I2C_SMBUS_BLOCK_MAX)
1799 length = I2C_SMBUS_BLOCK_MAX;
1800 data.block[0] = length;
1801 memcpy(data.block + 1, values, length);
1802 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1803 I2C_SMBUS_WRITE, command,
1804 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1805}
David Brownellc0564602007-05-01 23:26:31 +02001806EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001807
David Brownell438d6c22006-12-10 21:21:31 +01001808/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001810static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001812 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 union i2c_smbus_data * data)
1814{
1815 /* So we need to generate a series of msgs. In the case of writing, we
1816 need to use only one message; when reading, we need two. We initialize
1817 most things with sane defaults, to keep the code below somewhat
1818 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001819 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1820 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001822 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1824 };
1825 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001826 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001827 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 msgbuf0[0] = command;
1830 switch(size) {
1831 case I2C_SMBUS_QUICK:
1832 msg[0].len = 0;
1833 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001834 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1835 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 num = 1;
1837 break;
1838 case I2C_SMBUS_BYTE:
1839 if (read_write == I2C_SMBUS_READ) {
1840 /* Special case: only a read! */
1841 msg[0].flags = I2C_M_RD | flags;
1842 num = 1;
1843 }
1844 break;
1845 case I2C_SMBUS_BYTE_DATA:
1846 if (read_write == I2C_SMBUS_READ)
1847 msg[1].len = 1;
1848 else {
1849 msg[0].len = 2;
1850 msgbuf0[1] = data->byte;
1851 }
1852 break;
1853 case I2C_SMBUS_WORD_DATA:
1854 if (read_write == I2C_SMBUS_READ)
1855 msg[1].len = 2;
1856 else {
1857 msg[0].len=3;
1858 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001859 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861 break;
1862 case I2C_SMBUS_PROC_CALL:
1863 num = 2; /* Special case */
1864 read_write = I2C_SMBUS_READ;
1865 msg[0].len = 3;
1866 msg[1].len = 2;
1867 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001868 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 break;
1870 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001872 msg[1].flags |= I2C_M_RECV_LEN;
1873 msg[1].len = 1; /* block length will be added by
1874 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 } else {
1876 msg[0].len = data->block[0] + 2;
1877 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001878 dev_err(&adapter->dev,
1879 "Invalid block write size %d\n",
1880 data->block[0]);
1881 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001883 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 msgbuf0[i] = data->block[i-1];
1885 }
1886 break;
1887 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001888 num = 2; /* Another special case */
1889 read_write = I2C_SMBUS_READ;
1890 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001891 dev_err(&adapter->dev,
1892 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001893 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001894 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001895 }
1896 msg[0].len = data->block[0] + 2;
1897 for (i = 1; i < msg[0].len; i++)
1898 msgbuf0[i] = data->block[i-1];
1899 msg[1].flags |= I2C_M_RECV_LEN;
1900 msg[1].len = 1; /* block length will be added by
1901 the underlying bus driver */
1902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 case I2C_SMBUS_I2C_BLOCK_DATA:
1904 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001905 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 } else {
1907 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001908 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001909 dev_err(&adapter->dev,
1910 "Invalid block write size %d\n",
1911 data->block[0]);
1912 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 for (i = 1; i <= data->block[0]; i++)
1915 msgbuf0[i] = data->block[i];
1916 }
1917 break;
1918 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001919 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1920 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922
Jean Delvare421ef472005-10-26 21:28:55 +02001923 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1924 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1925 if (i) {
1926 /* Compute PEC if first message is a write */
1927 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001928 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001929 i2c_smbus_add_pec(&msg[0]);
1930 else /* Write followed by read */
1931 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1932 }
1933 /* Ask for PEC if last message is a read */
1934 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001935 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001936 }
1937
David Brownell24a5bb72008-07-14 22:38:23 +02001938 status = i2c_transfer(adapter, msg, num);
1939 if (status < 0)
1940 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Jean Delvare421ef472005-10-26 21:28:55 +02001942 /* Check PEC if last message is a read */
1943 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001944 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1945 if (status < 0)
1946 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001947 }
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (read_write == I2C_SMBUS_READ)
1950 switch(size) {
1951 case I2C_SMBUS_BYTE:
1952 data->byte = msgbuf0[0];
1953 break;
1954 case I2C_SMBUS_BYTE_DATA:
1955 data->byte = msgbuf1[0];
1956 break;
David Brownell438d6c22006-12-10 21:21:31 +01001957 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 case I2C_SMBUS_PROC_CALL:
1959 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1960 break;
1961 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001962 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 data->block[i+1] = msgbuf1[i];
1964 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001965 case I2C_SMBUS_BLOCK_DATA:
1966 case I2C_SMBUS_BLOCK_PROC_CALL:
1967 for (i = 0; i < msgbuf1[0] + 1; i++)
1968 data->block[i] = msgbuf1[i];
1969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 return 0;
1972}
1973
David Brownella1cdeda2008-07-14 22:38:24 +02001974/**
1975 * i2c_smbus_xfer - execute SMBus protocol operations
1976 * @adapter: Handle to I2C bus
1977 * @addr: Address of SMBus slave on that bus
1978 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1979 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1980 * @command: Byte interpreted by slave, for protocols which use such bytes
1981 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1982 * @data: Data to be read or written
1983 *
1984 * This executes an SMBus protocol operation, and returns a negative
1985 * errno code else zero on success.
1986 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001988 char read_write, u8 command, int protocol,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 union i2c_smbus_data * data)
1990{
1991 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001996 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001998 command, protocol, data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001999 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 } else
2001 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02002002 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return res;
2005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2009MODULE_DESCRIPTION("I2C-Bus main module");
2010MODULE_LICENSE("GPL");