blob: 29d2a1b4b464f12dfba1e9fc908640e9c7c9a7a7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
Jean Delvare87c6c222008-01-27 18:14:48 +010037#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
David Brownell9c1600e2007-05-01 23:26:31 +020039#include "i2c-core.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Arjan van de Venb3585e42006-01-11 10:50:26 +010042static DEFINE_MUTEX(core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static DEFINE_IDR(i2c_adapter_idr);
44
David Brownella1d9e6e2007-05-01 23:26:30 +020045#define is_newstyle_driver(d) ((d)->probe || (d)->remove)
David Brownellf37dd802007-02-13 22:09:00 +010046
47/* ------------------------------------------------------------------------- */
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int i2c_device_match(struct device *dev, struct device_driver *drv)
50{
David Brownell7b4fbc52007-05-01 23:26:30 +020051 struct i2c_client *client = to_i2c_client(dev);
52 struct i2c_driver *driver = to_i2c_driver(drv);
53
54 /* make legacy i2c drivers bypass driver model probing entirely;
55 * such drivers scan each i2c adapter/bus themselves.
56 */
David Brownella1d9e6e2007-05-01 23:26:30 +020057 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020058 return 0;
59
60 /* new style drivers use the same kind of driver matching policy
61 * as platform devices or SPI: compare device and driver IDs.
62 */
63 return strcmp(client->driver_name, drv->name) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
David Brownell7b4fbc52007-05-01 23:26:30 +020066#ifdef CONFIG_HOTPLUG
67
68/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020069static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020070{
71 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020072
73 /* by definition, legacy drivers can't hotplug */
74 if (dev->driver || !client->driver_name)
75 return 0;
76
Kay Sievers7eff2e72007-08-14 15:15:12 +020077 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
David Brownell7b4fbc52007-05-01 23:26:30 +020078 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020079 dev_dbg(dev, "uevent\n");
80 return 0;
81}
82
83#else
84#define i2c_device_uevent NULL
85#endif /* CONFIG_HOTPLUG */
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int i2c_device_probe(struct device *dev)
88{
David Brownell7b4fbc52007-05-01 23:26:30 +020089 struct i2c_client *client = to_i2c_client(dev);
90 struct i2c_driver *driver = to_i2c_driver(dev->driver);
91
92 if (!driver->probe)
93 return -ENODEV;
94 client->driver = driver;
95 dev_dbg(dev, "probe\n");
96 return driver->probe(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static int i2c_device_remove(struct device *dev)
100{
David Brownella1d9e6e2007-05-01 23:26:30 +0200101 struct i2c_client *client = to_i2c_client(dev);
102 struct i2c_driver *driver;
103 int status;
104
105 if (!dev->driver)
106 return 0;
107
108 driver = to_i2c_driver(dev->driver);
109 if (driver->remove) {
110 dev_dbg(dev, "remove\n");
111 status = driver->remove(client);
112 } else {
113 dev->driver = NULL;
114 status = 0;
115 }
116 if (status == 0)
117 client->driver = NULL;
118 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
David Brownellf37dd802007-02-13 22:09:00 +0100121static void i2c_device_shutdown(struct device *dev)
122{
123 struct i2c_driver *driver;
124
125 if (!dev->driver)
126 return;
127 driver = to_i2c_driver(dev->driver);
128 if (driver->shutdown)
129 driver->shutdown(to_i2c_client(dev));
130}
131
132static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
133{
134 struct i2c_driver *driver;
135
136 if (!dev->driver)
137 return 0;
138 driver = to_i2c_driver(dev->driver);
139 if (!driver->suspend)
140 return 0;
141 return driver->suspend(to_i2c_client(dev), mesg);
142}
143
144static int i2c_device_resume(struct device * dev)
145{
146 struct i2c_driver *driver;
147
148 if (!dev->driver)
149 return 0;
150 driver = to_i2c_driver(dev->driver);
151 if (!driver->resume)
152 return 0;
153 return driver->resume(to_i2c_client(dev));
154}
155
David Brownell7b4fbc52007-05-01 23:26:30 +0200156static void i2c_client_release(struct device *dev)
157{
158 struct i2c_client *client = to_i2c_client(dev);
159 complete(&client->released);
160}
161
David Brownell9c1600e2007-05-01 23:26:31 +0200162static void i2c_client_dev_release(struct device *dev)
163{
164 kfree(to_i2c_client(dev));
165}
166
David Brownell7b4fbc52007-05-01 23:26:30 +0200167static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
168{
169 struct i2c_client *client = to_i2c_client(dev);
170 return sprintf(buf, "%s\n", client->name);
171}
172
173static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
174{
175 struct i2c_client *client = to_i2c_client(dev);
176 return client->driver_name
177 ? sprintf(buf, "%s\n", client->driver_name)
178 : 0;
179}
180
181static struct device_attribute i2c_dev_attrs[] = {
182 __ATTR(name, S_IRUGO, show_client_name, NULL),
183 /* modalias helps coldplug: modprobe $(cat .../modalias) */
184 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
185 { },
186};
187
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200188static struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100189 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200190 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100191 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200192 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100193 .probe = i2c_device_probe,
194 .remove = i2c_device_remove,
195 .shutdown = i2c_device_shutdown,
196 .suspend = i2c_device_suspend,
197 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000198};
199
David Brownell9c1600e2007-05-01 23:26:31 +0200200/**
201 * i2c_new_device - instantiate an i2c device for use with a new style driver
202 * @adap: the adapter managing the device
203 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200204 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200205 *
206 * Create a device to work with a new style i2c driver, where binding is
207 * handled through driver model probe()/remove() methods. This call is not
208 * appropriate for use by mainboad initialization logic, which usually runs
209 * during an arch_initcall() long before any i2c_adapter could exist.
210 *
211 * This returns the new i2c client, which may be saved for later use with
212 * i2c_unregister_device(); or NULL to indicate an error.
213 */
214struct i2c_client *
215i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
216{
217 struct i2c_client *client;
218 int status;
219
220 client = kzalloc(sizeof *client, GFP_KERNEL);
221 if (!client)
222 return NULL;
223
224 client->adapter = adap;
225
226 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200227 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
228
229 client->flags = info->flags & ~I2C_CLIENT_WAKE;
David Brownell9c1600e2007-05-01 23:26:31 +0200230 client->addr = info->addr;
231 client->irq = info->irq;
232
233 strlcpy(client->driver_name, info->driver_name,
234 sizeof(client->driver_name));
235 strlcpy(client->name, info->type, sizeof(client->name));
236
237 /* a new style driver may be bound to this device when we
238 * return from this function, or any later moment (e.g. maybe
239 * hotplugging will load the driver module). and the device
240 * refcount model is the standard driver model one.
241 */
242 status = i2c_attach_client(client);
243 if (status < 0) {
244 kfree(client);
245 client = NULL;
246 }
247 return client;
248}
249EXPORT_SYMBOL_GPL(i2c_new_device);
250
251
252/**
253 * i2c_unregister_device - reverse effect of i2c_new_device()
254 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200255 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200256 */
257void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200258{
259 struct i2c_adapter *adapter = client->adapter;
260 struct i2c_driver *driver = client->driver;
261
262 if (driver && !is_newstyle_driver(driver)) {
263 dev_err(&client->dev, "can't unregister devices "
264 "with legacy drivers\n");
265 WARN_ON(1);
266 return;
267 }
268
269 mutex_lock(&adapter->clist_lock);
270 list_del(&client->list);
271 mutex_unlock(&adapter->clist_lock);
272
273 device_unregister(&client->dev);
274}
David Brownell9c1600e2007-05-01 23:26:31 +0200275EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200276
277
David Brownellf37dd802007-02-13 22:09:00 +0100278/* ------------------------------------------------------------------------- */
279
David Brownell16ffadf2007-05-01 23:26:28 +0200280/* I2C bus adapters -- one roots each I2C or SMBUS segment */
281
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200282static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
David Brownellef2c8322007-05-01 23:26:28 +0200284 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 complete(&adap->dev_released);
286}
287
David Brownell16ffadf2007-05-01 23:26:28 +0200288static ssize_t
289show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
David Brownellef2c8322007-05-01 23:26:28 +0200291 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return sprintf(buf, "%s\n", adap->name);
293}
David Brownell16ffadf2007-05-01 23:26:28 +0200294
295static struct device_attribute i2c_adapter_attrs[] = {
296 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
297 { },
298};
299
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200300static struct class i2c_adapter_class = {
David Brownell16ffadf2007-05-01 23:26:28 +0200301 .owner = THIS_MODULE,
302 .name = "i2c-adapter",
303 .dev_attrs = i2c_adapter_attrs,
304};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David Brownell9c1600e2007-05-01 23:26:31 +0200306static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
307{
308 struct i2c_devinfo *devinfo;
309
310 mutex_lock(&__i2c_board_lock);
311 list_for_each_entry(devinfo, &__i2c_board_list, list) {
312 if (devinfo->busnum == adapter->nr
313 && !i2c_new_device(adapter,
314 &devinfo->board_info))
315 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
316 i2c_adapter_id(adapter),
317 devinfo->board_info.addr);
318 }
319 mutex_unlock(&__i2c_board_lock);
320}
321
Jean Delvare026526f2008-01-27 18:14:49 +0100322static int i2c_do_add_adapter(struct device_driver *d, void *data)
323{
324 struct i2c_driver *driver = to_i2c_driver(d);
325 struct i2c_adapter *adap = data;
326
327 if (driver->attach_adapter) {
328 /* We ignore the return code; if it fails, too bad */
329 driver->attach_adapter(adap);
330 }
331 return 0;
332}
333
David Brownell6e13e642007-05-01 23:26:31 +0200334static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Jean Delvare026526f2008-01-27 18:14:49 +0100336 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Ingo Molnar5c085d32006-01-18 23:16:04 +0100338 mutex_init(&adap->bus_lock);
339 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 INIT_LIST_HEAD(&adap->clients);
341
David Brownell6e13e642007-05-01 23:26:31 +0200342 mutex_lock(&core_lists);
David Brownell6e13e642007-05-01 23:26:31 +0200343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* Add the adapter to the driver core.
345 * If the parent pointer is not set up,
346 * we add this adapter to the host bus.
347 */
David Brownellb119dc32007-01-04 13:07:04 +0100348 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100350 pr_debug("I2C adapter driver [%s] forgot to specify "
351 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200355 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200356 res = device_register(&adap->dev);
357 if (res)
358 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200360 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
361
David Brownell6e13e642007-05-01 23:26:31 +0200362 /* create pre-declared device nodes for new-style drivers */
363 if (adap->nr < __i2c_first_dynamic_bus_num)
364 i2c_scan_static_board_info(adap);
365
David Brownell7b4fbc52007-05-01 23:26:30 +0200366 /* let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100367 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
368 i2c_do_add_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100371 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200373
Jean Delvareb119c6c2006-08-15 18:26:30 +0200374out_list:
Jean Delvareb119c6c2006-08-15 18:26:30 +0200375 idr_remove(&i2c_adapter_idr, adap->nr);
376 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
David Brownell6e13e642007-05-01 23:26:31 +0200379/**
380 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
381 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200382 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200383 *
384 * This routine is used to declare an I2C adapter when its bus number
385 * doesn't matter. Examples: for I2C adapters dynamically added by
386 * USB links or PCI plugin cards.
387 *
388 * When this returns zero, a new bus number was allocated and stored
389 * in adap->nr, and the specified adapter became available for clients.
390 * Otherwise, a negative errno value is returned.
391 */
392int i2c_add_adapter(struct i2c_adapter *adapter)
393{
394 int id, res = 0;
395
396retry:
397 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
398 return -ENOMEM;
399
400 mutex_lock(&core_lists);
401 /* "above" here means "above or equal to", sigh */
402 res = idr_get_new_above(&i2c_adapter_idr, adapter,
403 __i2c_first_dynamic_bus_num, &id);
404 mutex_unlock(&core_lists);
405
406 if (res < 0) {
407 if (res == -EAGAIN)
408 goto retry;
409 return res;
410 }
411
412 adapter->nr = id;
413 return i2c_register_adapter(adapter);
414}
415EXPORT_SYMBOL(i2c_add_adapter);
416
417/**
418 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
419 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200420 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200421 *
422 * This routine is used to declare an I2C adapter when its bus number
423 * matters. Example: for I2C adapters from system-on-chip CPUs, or
424 * otherwise built in to the system's mainboard, and where i2c_board_info
425 * is used to properly configure I2C devices.
426 *
427 * If no devices have pre-been declared for this bus, then be sure to
428 * register the adapter before any dynamically allocated ones. Otherwise
429 * the required bus ID may not be available.
430 *
431 * When this returns zero, the specified adapter became available for
432 * clients using the bus number provided in adap->nr. Also, the table
433 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
434 * and the appropriate driver model device nodes are created. Otherwise, a
435 * negative errno value is returned.
436 */
437int i2c_add_numbered_adapter(struct i2c_adapter *adap)
438{
439 int id;
440 int status;
441
442 if (adap->nr & ~MAX_ID_MASK)
443 return -EINVAL;
444
445retry:
446 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
447 return -ENOMEM;
448
449 mutex_lock(&core_lists);
450 /* "above" here means "above or equal to", sigh;
451 * we need the "equal to" result to force the result
452 */
453 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
454 if (status == 0 && id != adap->nr) {
455 status = -EBUSY;
456 idr_remove(&i2c_adapter_idr, id);
457 }
458 mutex_unlock(&core_lists);
459 if (status == -EAGAIN)
460 goto retry;
461
462 if (status == 0)
463 status = i2c_register_adapter(adap);
464 return status;
465}
466EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
467
Jean Delvare026526f2008-01-27 18:14:49 +0100468static int i2c_do_del_adapter(struct device_driver *d, void *data)
469{
470 struct i2c_driver *driver = to_i2c_driver(d);
471 struct i2c_adapter *adapter = data;
472 int res;
473
474 if (!driver->detach_adapter)
475 return 0;
476 res = driver->detach_adapter(adapter);
477 if (res)
478 dev_err(&adapter->dev, "detach_adapter failed (%d) "
479 "for driver [%s]\n", res, driver->driver.name);
480 return res;
481}
482
David Brownelld64f73b2007-07-12 14:12:28 +0200483/**
484 * i2c_del_adapter - unregister I2C adapter
485 * @adap: the adapter being unregistered
486 * Context: can sleep
487 *
488 * This unregisters an I2C adapter which was previously registered
489 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491int i2c_del_adapter(struct i2c_adapter *adap)
492{
493 struct list_head *item, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct i2c_client *client;
495 int res = 0;
496
Arjan van de Venb3585e42006-01-11 10:50:26 +0100497 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* First make sure that this adapter was ever added */
Jean Delvare87c6c222008-01-27 18:14:48 +0100500 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200501 pr_debug("i2c-core: attempting to delete unregistered "
502 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 res = -EINVAL;
504 goto out_unlock;
505 }
506
Jean Delvare026526f2008-01-27 18:14:49 +0100507 /* Tell drivers about this removal */
508 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
509 i2c_do_del_adapter);
510 if (res)
511 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200514 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200516 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
David Brownella1d9e6e2007-05-01 23:26:30 +0200518 client = list_entry(item, struct i2c_client, list);
519 driver = client->driver;
520
521 /* new style, follow standard driver model */
522 if (!driver || is_newstyle_driver(driver)) {
523 i2c_unregister_device(client);
524 continue;
525 }
526
527 /* legacy drivers create and remove clients themselves */
528 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200529 dev_err(&adap->dev, "detach_client failed for client "
530 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 client->addr);
532 goto out_unlock;
533 }
534 }
535
536 /* clean up the sysfs representation */
537 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* wait for sysfs to drop all references */
541 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
David Brownell6e13e642007-05-01 23:26:31 +0200543 /* free bus id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 idr_remove(&i2c_adapter_idr, adap->nr);
545
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200546 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100549 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return res;
551}
David Brownellc0564602007-05-01 23:26:31 +0200552EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554
David Brownell7b4fbc52007-05-01 23:26:30 +0200555/* ------------------------------------------------------------------------- */
556
557/*
558 * An i2c_driver is used with one or more i2c_client (device) nodes to access
559 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
560 * are two models for binding the driver to its device: "new style" drivers
561 * follow the standard Linux driver model and just respond to probe() calls
562 * issued if the driver core sees they match(); "legacy" drivers create device
563 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
565
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800566int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100568 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
David Brownell7b4fbc52007-05-01 23:26:30 +0200570 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200571 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200572 if (driver->attach_adapter || driver->detach_adapter
573 || driver->detach_client) {
574 printk(KERN_WARNING
575 "i2c-core: driver [%s] is confused\n",
576 driver->driver.name);
577 return -EINVAL;
578 }
579 }
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800582 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
David Brownell6e13e642007-05-01 23:26:31 +0200585 /* for new style drivers, when registration returns the driver core
586 * will have called probe() for all matching-but-unbound devices.
587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 res = driver_register(&driver->driver);
589 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100590 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100591
Jean Delvare7eebcb72006-02-05 23:28:21 +0100592 mutex_lock(&core_lists);
593
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100594 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
David Brownell7b4fbc52007-05-01 23:26:30 +0200596 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100597 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200598 struct i2c_adapter *adapter;
599
Jean Delvare87c6c222008-01-27 18:14:48 +0100600 down(&i2c_adapter_class.sem);
601 list_for_each_entry(adapter, &i2c_adapter_class.devices,
602 dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 driver->attach_adapter(adapter);
604 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100605 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
Arjan van de Venb3585e42006-01-11 10:50:26 +0100608 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800611EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
David Brownella1d9e6e2007-05-01 23:26:30 +0200613/**
614 * i2c_del_driver - unregister I2C driver
615 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200616 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200617 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200618void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Jean Delvare87c6c222008-01-27 18:14:48 +0100620 struct list_head *item2, *_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct i2c_client *client;
622 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100623
Arjan van de Venb3585e42006-01-11 10:50:26 +0100624 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
David Brownella1d9e6e2007-05-01 23:26:30 +0200626 /* new-style driver? */
627 if (is_newstyle_driver(driver))
628 goto unregister;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100631 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Jean Delvare87c6c222008-01-27 18:14:48 +0100634 down(&i2c_adapter_class.sem);
635 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (driver->detach_adapter) {
Jean Delvareb3e82092007-05-01 23:26:32 +0200637 if (driver->detach_adapter(adap)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200638 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100639 "for driver [%s]\n",
640 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 } else {
643 list_for_each_safe(item2, _n, &adap->clients) {
644 client = list_entry(item2, struct i2c_client, list);
645 if (client->driver != driver)
646 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200647 dev_dbg(&adap->dev, "detaching client [%s] "
648 "at 0x%02x\n", client->name,
649 client->addr);
Jean Delvareb3e82092007-05-01 23:26:32 +0200650 if (driver->detach_client(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200651 dev_err(&adap->dev, "detach_client "
652 "failed for client [%s] at "
653 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 }
657 }
658 }
Jean Delvare87c6c222008-01-27 18:14:48 +0100659 up(&i2c_adapter_class.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David Brownella1d9e6e2007-05-01 23:26:30 +0200661 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100663 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Arjan van de Venb3585e42006-01-11 10:50:26 +0100665 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
David Brownellc0564602007-05-01 23:26:31 +0200667EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
David Brownell7b4fbc52007-05-01 23:26:30 +0200669/* ------------------------------------------------------------------------- */
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
672{
673 struct list_head *item;
674 struct i2c_client *client;
675
676 list_for_each(item,&adapter->clients) {
677 client = list_entry(item, struct i2c_client, list);
678 if (client->addr == addr)
679 return -EBUSY;
680 }
681 return 0;
682}
683
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100684static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 int rval;
687
Ingo Molnar5c085d32006-01-18 23:16:04 +0100688 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100690 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 return rval;
693}
694
695int i2c_attach_client(struct i2c_client *client)
696{
697 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200698 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Ingo Molnar5c085d32006-01-18 23:16:04 +0100700 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200702 res = -EBUSY;
703 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 client->dev.parent = &client->adapter->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 client->dev.bus = &i2c_bus_type;
David Brownell9c1600e2007-05-01 23:26:31 +0200709
710 if (client->driver)
711 client->dev.driver = &client->driver->driver;
712
David Brownellde81d2a2007-05-22 19:49:16 +0200713 if (client->driver && !is_newstyle_driver(client->driver)) {
David Brownell9c1600e2007-05-01 23:26:31 +0200714 client->dev.release = i2c_client_release;
David Brownellde81d2a2007-05-22 19:49:16 +0200715 client->dev.uevent_suppress = 1;
716 } else
David Brownell9c1600e2007-05-01 23:26:31 +0200717 client->dev.release = i2c_client_dev_release;
David Brownell438d6c22006-12-10 21:21:31 +0100718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
720 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200721 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
722 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200723 res = device_register(&client->dev);
724 if (res)
725 goto out_list;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200726 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200727
728 if (adapter->client_register) {
729 if (adapter->client_register(client)) {
730 dev_dbg(&adapter->dev, "client_register "
731 "failed for client [%s] at 0x%02x\n",
732 client->name, client->addr);
733 }
734 }
735
736 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200737
Jean Delvareb119c6c2006-08-15 18:26:30 +0200738out_list:
739 list_del(&client->list);
740 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
741 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200742out_unlock:
743 mutex_unlock(&adapter->clist_lock);
744 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
David Brownellc0564602007-05-01 23:26:31 +0200746EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748int i2c_detach_client(struct i2c_client *client)
749{
750 struct i2c_adapter *adapter = client->adapter;
751 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (adapter->client_unregister) {
754 res = adapter->client_unregister(client);
755 if (res) {
756 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700757 "client_unregister [%s] failed, "
758 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto out;
760 }
761 }
762
Ingo Molnar5c085d32006-01-18 23:16:04 +0100763 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 list_del(&client->list);
765 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100767 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 wait_for_completion(&client->released);
769
770 out:
771 return res;
772}
David Brownellc0564602007-05-01 23:26:31 +0200773EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Jean Delvaree48d3312008-01-27 18:14:48 +0100775/**
776 * i2c_use_client - increments the reference count of the i2c client structure
777 * @client: the client being referenced
778 *
779 * Each live reference to a client should be refcounted. The driver model does
780 * that automatically as part of driver binding, so that most drivers don't
781 * need to do this explicitly: they hold a reference until they're unbound
782 * from the device.
783 *
784 * A pointer to the client with the incremented reference counter is returned.
785 */
786struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100788 get_device(&client->dev);
Jean Delvaree48d3312008-01-27 18:14:48 +0100789 return client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
David Brownellc0564602007-05-01 23:26:31 +0200791EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jean Delvaree48d3312008-01-27 18:14:48 +0100793/**
794 * i2c_release_client - release a use of the i2c client structure
795 * @client: the client being no longer referenced
796 *
797 * Must be called when a user of a client is finished with it.
798 */
799void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Jean Delvarebdc511f2008-01-27 18:14:48 +0100801 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
David Brownellc0564602007-05-01 23:26:31 +0200803EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
806{
807 struct list_head *item;
808 struct i2c_client *client;
809
Ingo Molnar5c085d32006-01-18 23:16:04 +0100810 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 list_for_each(item,&adap->clients) {
812 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100813 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 continue;
815 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100816 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100818 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100820 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100822 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
David Brownellc0564602007-05-01 23:26:31 +0200824EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826static int __init i2c_init(void)
827{
828 int retval;
829
830 retval = bus_register(&i2c_bus_type);
831 if (retval)
832 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return class_register(&i2c_adapter_class);
834}
835
836static void __exit i2c_exit(void)
837{
838 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 bus_unregister(&i2c_bus_type);
840}
841
842subsys_initcall(i2c_init);
843module_exit(i2c_exit);
844
845/* ----------------------------------------------------
846 * the functional interface to the i2c busses.
847 * ----------------------------------------------------
848 */
849
850int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
851{
852 int ret;
853
854 if (adap->algo->master_xfer) {
855#ifdef DEBUG
856 for (ret = 0; ret < num; ret++) {
857 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200858 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
859 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
860 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862#endif
863
Jiri Kosina6ea23032006-12-10 21:21:30 +0100864 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100866 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 return ret;
869 } else {
870 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
871 return -ENOSYS;
872 }
873}
David Brownellc0564602007-05-01 23:26:31 +0200874EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
877{
878 int ret;
879 struct i2c_adapter *adap=client->adapter;
880 struct i2c_msg msg;
881
Jean Delvare815f55f2005-05-07 22:58:46 +0200882 msg.addr = client->addr;
883 msg.flags = client->flags & I2C_M_TEN;
884 msg.len = count;
885 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100886
Jean Delvare815f55f2005-05-07 22:58:46 +0200887 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Jean Delvare815f55f2005-05-07 22:58:46 +0200889 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
890 transmitted, else error code. */
891 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
David Brownellc0564602007-05-01 23:26:31 +0200893EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
896{
897 struct i2c_adapter *adap=client->adapter;
898 struct i2c_msg msg;
899 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Jean Delvare815f55f2005-05-07 22:58:46 +0200901 msg.addr = client->addr;
902 msg.flags = client->flags & I2C_M_TEN;
903 msg.flags |= I2C_M_RD;
904 msg.len = count;
905 msg.buf = buf;
906
907 ret = i2c_transfer(adap, &msg, 1);
908
909 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
910 transmitted, else error code. */
911 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
David Brownellc0564602007-05-01 23:26:31 +0200913EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/* ----------------------------------------------------
916 * the i2c address scanning function
917 * Will not work for 10-bit addresses!
918 * ----------------------------------------------------
919 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200920static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
921 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200922{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200923 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200924
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200925 /* Make sure the address is valid */
926 if (addr < 0x03 || addr > 0x77) {
927 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
928 addr);
929 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200930 }
931
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200932 /* Skip if already in use */
933 if (i2c_check_addr(adapter, addr))
934 return 0;
935
936 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200937 if (kind < 0) {
938 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
939 I2C_SMBUS_QUICK, NULL) < 0)
940 return 0;
941
942 /* prevent 24RF08 corruption */
943 if ((addr & ~0x0f) == 0x50)
944 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
945 I2C_SMBUS_QUICK, NULL);
946 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200947
948 /* Finally call the custom detection function */
949 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200950 /* -ENODEV can be returned if there is a chip at the given address
951 but it isn't supported by this chip driver. We catch it here as
952 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200953 if (err == -ENODEV)
954 err = 0;
955
956 if (err)
957 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
958 addr, err);
959 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962int i2c_probe(struct i2c_adapter *adapter,
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +0100963 const struct i2c_client_address_data *address_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int (*found_proc) (struct i2c_adapter *, int, int))
965{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200966 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int adap_id = i2c_adapter_id(adapter);
968
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200969 /* Force entries are done first, and are not affected by ignore
970 entries */
971 if (address_data->forces) {
Mark M. Hoffmanbfb6df22008-01-27 18:14:46 +0100972 const unsigned short * const *forces = address_data->forces;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200973 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200975 for (kind = 0; forces[kind]; kind++) {
976 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
977 i += 2) {
978 if (forces[kind][i] == adap_id
979 || forces[kind][i] == ANY_I2C_BUS) {
980 dev_dbg(&adapter->dev, "found force "
981 "parameter for adapter %d, "
982 "addr 0x%02x, kind %d\n",
983 adap_id, forces[kind][i + 1],
984 kind);
985 err = i2c_probe_address(adapter,
986 forces[kind][i + 1],
987 kind, found_proc);
988 if (err)
989 return err;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200994
Jean Delvare4366dc92005-09-25 16:50:06 +0200995 /* Stop here if we can't use SMBUS_QUICK */
996 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
997 if (address_data->probe[0] == I2C_CLIENT_END
998 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100999 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +02001000
1001 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1002 "can't probe for chips\n");
1003 return -1;
1004 }
1005
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001006 /* Probe entries are done second, and are not affected by ignore
1007 entries either */
1008 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1009 if (address_data->probe[i] == adap_id
1010 || address_data->probe[i] == ANY_I2C_BUS) {
1011 dev_dbg(&adapter->dev, "found probe parameter for "
1012 "adapter %d, addr 0x%02x\n", adap_id,
1013 address_data->probe[i + 1]);
1014 err = i2c_probe_address(adapter,
1015 address_data->probe[i + 1],
1016 -1, found_proc);
1017 if (err)
1018 return err;
1019 }
1020 }
1021
1022 /* Normal entries are done last, unless shadowed by an ignore entry */
1023 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1024 int j, ignore;
1025
1026 ignore = 0;
1027 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1028 j += 2) {
1029 if ((address_data->ignore[j] == adap_id ||
1030 address_data->ignore[j] == ANY_I2C_BUS)
1031 && address_data->ignore[j + 1]
1032 == address_data->normal_i2c[i]) {
1033 dev_dbg(&adapter->dev, "found ignore "
1034 "parameter for adapter %d, "
1035 "addr 0x%02x\n", adap_id,
1036 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +02001037 ignore = 1;
1038 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001039 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +02001040 }
1041 if (ignore)
1042 continue;
1043
1044 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1045 "addr 0x%02x\n", adap_id,
1046 address_data->normal_i2c[i]);
1047 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1048 -1, found_proc);
1049 if (err)
1050 return err;
1051 }
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054}
David Brownellc0564602007-05-01 23:26:31 +02001055EXPORT_SYMBOL(i2c_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Jean Delvare12b50532007-05-01 23:26:31 +02001057struct i2c_client *
1058i2c_new_probed_device(struct i2c_adapter *adap,
1059 struct i2c_board_info *info,
1060 unsigned short const *addr_list)
1061{
1062 int i;
1063
1064 /* Stop here if the bus doesn't support probing */
1065 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1066 dev_err(&adap->dev, "Probing not supported\n");
1067 return NULL;
1068 }
1069
1070 mutex_lock(&adap->clist_lock);
1071 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1072 /* Check address validity */
1073 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1074 dev_warn(&adap->dev, "Invalid 7-bit address "
1075 "0x%02x\n", addr_list[i]);
1076 continue;
1077 }
1078
1079 /* Check address availability */
1080 if (__i2c_check_addr(adap, addr_list[i])) {
1081 dev_dbg(&adap->dev, "Address 0x%02x already in "
1082 "use, not probing\n", addr_list[i]);
1083 continue;
1084 }
1085
1086 /* Test address responsiveness
1087 The default probe method is a quick write, but it is known
1088 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1089 and could also irreversibly write-protect some EEPROMs, so
1090 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1091 read instead. Also, some bus drivers don't implement
1092 quick write, so we fallback to a byte read it that case
1093 too. */
1094 if ((addr_list[i] & ~0x07) == 0x30
1095 || (addr_list[i] & ~0x0f) == 0x50
1096 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1097 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1098 I2C_SMBUS_READ, 0,
1099 I2C_SMBUS_BYTE, NULL) >= 0)
1100 break;
1101 } else {
1102 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1103 I2C_SMBUS_WRITE, 0,
1104 I2C_SMBUS_QUICK, NULL) >= 0)
1105 break;
1106 }
1107 }
1108 mutex_unlock(&adap->clist_lock);
1109
1110 if (addr_list[i] == I2C_CLIENT_END) {
1111 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1112 return NULL;
1113 }
1114
1115 info->addr = addr_list[i];
1116 return i2c_new_device(adap, info);
1117}
1118EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120struct i2c_adapter* i2c_get_adapter(int id)
1121{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001123
Arjan van de Venb3585e42006-01-11 10:50:26 +01001124 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001125 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1126 if (adapter && !try_module_get(adapter->owner))
1127 adapter = NULL;
1128
Arjan van de Venb3585e42006-01-11 10:50:26 +01001129 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001130 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
David Brownellc0564602007-05-01 23:26:31 +02001132EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134void i2c_put_adapter(struct i2c_adapter *adap)
1135{
1136 module_put(adap->owner);
1137}
David Brownellc0564602007-05-01 23:26:31 +02001138EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140/* The SMBus parts */
1141
David Brownell438d6c22006-12-10 21:21:31 +01001142#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static u8
1144crc8(u16 data)
1145{
1146 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001149 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 data = data ^ POLY;
1151 data = data << 1;
1152 }
1153 return (u8)(data >> 8);
1154}
1155
Jean Delvare421ef472005-10-26 21:28:55 +02001156/* Incremental CRC8 over count bytes in the array pointed to by p */
1157static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 int i;
1160
1161 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001162 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return crc;
1164}
1165
Jean Delvare421ef472005-10-26 21:28:55 +02001166/* Assume a 7-bit address, which is reasonable for SMBus */
1167static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Jean Delvare421ef472005-10-26 21:28:55 +02001169 /* The address will be sent first */
1170 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1171 pec = i2c_smbus_pec(pec, &addr, 1);
1172
1173 /* The data buffer follows */
1174 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Jean Delvare421ef472005-10-26 21:28:55 +02001177/* Used for write only transactions */
1178static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Jean Delvare421ef472005-10-26 21:28:55 +02001180 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1181 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Jean Delvare421ef472005-10-26 21:28:55 +02001184/* Return <0 on CRC error
1185 If there was a write before this read (most cases) we need to take the
1186 partial CRC from the write part into account.
1187 Note that this function does modify the message (we need to decrease the
1188 message length to hide the CRC byte from the caller). */
1189static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Jean Delvare421ef472005-10-26 21:28:55 +02001191 u8 rpec = msg->buf[--msg->len];
1192 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (rpec != cpec) {
1195 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1196 rpec, cpec);
1197 return -1;
1198 }
David Brownell438d6c22006-12-10 21:21:31 +01001199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1203{
1204 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001205 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
David Brownellc0564602007-05-01 23:26:31 +02001207EXPORT_SYMBOL(i2c_smbus_write_quick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209s32 i2c_smbus_read_byte(struct i2c_client *client)
1210{
1211 union i2c_smbus_data data;
1212 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1213 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1214 return -1;
1215 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001216 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
David Brownellc0564602007-05-01 23:26:31 +02001218EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1221{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001223 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
David Brownellc0564602007-05-01 23:26:31 +02001225EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1228{
1229 union i2c_smbus_data data;
1230 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1231 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1232 return -1;
1233 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001234 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
David Brownellc0564602007-05-01 23:26:31 +02001236EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1239{
1240 union i2c_smbus_data data;
1241 data.byte = value;
1242 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1243 I2C_SMBUS_WRITE,command,
1244 I2C_SMBUS_BYTE_DATA,&data);
1245}
David Brownellc0564602007-05-01 23:26:31 +02001246EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1249{
1250 union i2c_smbus_data data;
1251 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1252 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1253 return -1;
1254 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001255 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
David Brownellc0564602007-05-01 23:26:31 +02001257EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1260{
1261 union i2c_smbus_data data;
1262 data.word = value;
1263 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1264 I2C_SMBUS_WRITE,command,
1265 I2C_SMBUS_WORD_DATA,&data);
1266}
David Brownellc0564602007-05-01 23:26:31 +02001267EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
David Brownella64ec072007-10-13 23:56:31 +02001269/**
1270 * i2c_smbus_read_block_data - SMBus block read request
1271 * @client: Handle to slave device
1272 * @command: Command byte issued to let the slave know what data should
1273 * be returned
1274 * @values: Byte array into which data will be read; big enough to hold
1275 * the data returned by the slave. SMBus allows at most 32 bytes.
1276 *
1277 * Returns the number of bytes read in the slave's response, else a
1278 * negative number to indicate some kind of error.
1279 *
1280 * Note that using this function requires that the client's adapter support
1281 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1282 * support this; its emulation through I2C messaging relies on a specific
1283 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1284 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001285s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1286 u8 *values)
1287{
1288 union i2c_smbus_data data;
1289
1290 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1291 I2C_SMBUS_READ, command,
1292 I2C_SMBUS_BLOCK_DATA, &data))
1293 return -1;
1294
1295 memcpy(values, &data.block[1], data.block[0]);
1296 return data.block[0];
1297}
1298EXPORT_SYMBOL(i2c_smbus_read_block_data);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001301 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (length > I2C_SMBUS_BLOCK_MAX)
1306 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001308 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1310 I2C_SMBUS_WRITE,command,
1311 I2C_SMBUS_BLOCK_DATA,&data);
1312}
David Brownellc0564602007-05-01 23:26:31 +02001313EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001316s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1317 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001320
Jean Delvare4b2643d2007-07-12 14:12:29 +02001321 if (length > I2C_SMBUS_BLOCK_MAX)
1322 length = I2C_SMBUS_BLOCK_MAX;
1323 data.block[0] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1325 I2C_SMBUS_READ,command,
1326 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1327 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001328
1329 memcpy(values, &data.block[1], data.block[0]);
1330 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
David Brownellc0564602007-05-01 23:26:31 +02001332EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Jean Delvare21bbd692006-01-09 15:19:18 +11001334s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001335 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001336{
1337 union i2c_smbus_data data;
1338
1339 if (length > I2C_SMBUS_BLOCK_MAX)
1340 length = I2C_SMBUS_BLOCK_MAX;
1341 data.block[0] = length;
1342 memcpy(data.block + 1, values, length);
1343 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1344 I2C_SMBUS_WRITE, command,
1345 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1346}
David Brownellc0564602007-05-01 23:26:31 +02001347EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001348
David Brownell438d6c22006-12-10 21:21:31 +01001349/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001351static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001353 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 union i2c_smbus_data * data)
1355{
1356 /* So we need to generate a series of msgs. In the case of writing, we
1357 need to use only one message; when reading, we need two. We initialize
1358 most things with sane defaults, to keep the code below somewhat
1359 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001360 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1361 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001363 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1365 };
1366 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001367 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 msgbuf0[0] = command;
1370 switch(size) {
1371 case I2C_SMBUS_QUICK:
1372 msg[0].len = 0;
1373 /* Special case: The read/write field is used as data */
1374 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1375 num = 1;
1376 break;
1377 case I2C_SMBUS_BYTE:
1378 if (read_write == I2C_SMBUS_READ) {
1379 /* Special case: only a read! */
1380 msg[0].flags = I2C_M_RD | flags;
1381 num = 1;
1382 }
1383 break;
1384 case I2C_SMBUS_BYTE_DATA:
1385 if (read_write == I2C_SMBUS_READ)
1386 msg[1].len = 1;
1387 else {
1388 msg[0].len = 2;
1389 msgbuf0[1] = data->byte;
1390 }
1391 break;
1392 case I2C_SMBUS_WORD_DATA:
1393 if (read_write == I2C_SMBUS_READ)
1394 msg[1].len = 2;
1395 else {
1396 msg[0].len=3;
1397 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001398 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400 break;
1401 case I2C_SMBUS_PROC_CALL:
1402 num = 2; /* Special case */
1403 read_write = I2C_SMBUS_READ;
1404 msg[0].len = 3;
1405 msg[1].len = 2;
1406 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001407 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001411 msg[1].flags |= I2C_M_RECV_LEN;
1412 msg[1].len = 1; /* block length will be added by
1413 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 } else {
1415 msg[0].len = data->block[0] + 2;
1416 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1417 dev_err(&adapter->dev, "smbus_access called with "
1418 "invalid block write size (%d)\n",
1419 data->block[0]);
1420 return -1;
1421 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001422 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 msgbuf0[i] = data->block[i-1];
1424 }
1425 break;
1426 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001427 num = 2; /* Another special case */
1428 read_write = I2C_SMBUS_READ;
1429 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1430 dev_err(&adapter->dev, "%s called with invalid "
1431 "block proc call size (%d)\n", __FUNCTION__,
1432 data->block[0]);
1433 return -1;
1434 }
1435 msg[0].len = data->block[0] + 2;
1436 for (i = 1; i < msg[0].len; i++)
1437 msgbuf0[i] = data->block[i-1];
1438 msg[1].flags |= I2C_M_RECV_LEN;
1439 msg[1].len = 1; /* block length will be added by
1440 the underlying bus driver */
1441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 case I2C_SMBUS_I2C_BLOCK_DATA:
1443 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001444 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 } else {
1446 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001447 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1449 "invalid block write size (%d)\n",
1450 data->block[0]);
1451 return -1;
1452 }
1453 for (i = 1; i <= data->block[0]; i++)
1454 msgbuf0[i] = data->block[i];
1455 }
1456 break;
1457 default:
1458 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1459 size);
1460 return -1;
1461 }
1462
Jean Delvare421ef472005-10-26 21:28:55 +02001463 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1464 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1465 if (i) {
1466 /* Compute PEC if first message is a write */
1467 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001468 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001469 i2c_smbus_add_pec(&msg[0]);
1470 else /* Write followed by read */
1471 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1472 }
1473 /* Ask for PEC if last message is a read */
1474 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001475 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001476 }
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (i2c_transfer(adapter, msg, num) < 0)
1479 return -1;
1480
Jean Delvare421ef472005-10-26 21:28:55 +02001481 /* Check PEC if last message is a read */
1482 if (i && (msg[num-1].flags & I2C_M_RD)) {
1483 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1484 return -1;
1485 }
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (read_write == I2C_SMBUS_READ)
1488 switch(size) {
1489 case I2C_SMBUS_BYTE:
1490 data->byte = msgbuf0[0];
1491 break;
1492 case I2C_SMBUS_BYTE_DATA:
1493 data->byte = msgbuf1[0];
1494 break;
David Brownell438d6c22006-12-10 21:21:31 +01001495 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 case I2C_SMBUS_PROC_CALL:
1497 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1498 break;
1499 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001500 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 data->block[i+1] = msgbuf1[i];
1502 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001503 case I2C_SMBUS_BLOCK_DATA:
1504 case I2C_SMBUS_BLOCK_PROC_CALL:
1505 for (i = 0; i < msgbuf1[0] + 1; i++)
1506 data->block[i] = msgbuf1[i];
1507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509 return 0;
1510}
1511
1512
1513s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001514 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 union i2c_smbus_data * data)
1516{
1517 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001522 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1524 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001525 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 } else
1527 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1528 command,size,data);
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return res;
1531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1535MODULE_DESCRIPTION("I2C-Bus main module");
1536MODULE_LICENSE("GPL");