blob: 9653f7f815617d65d14b2221f8b3df204c56e655 [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
20/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 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>
37
38
39static LIST_HEAD(adapters);
40static LIST_HEAD(drivers);
Arjan van de Venb3585e42006-01-11 10:50:26 +010041static DEFINE_MUTEX(core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_IDR(i2c_adapter_idr);
43
David Brownellf37dd802007-02-13 22:09:00 +010044
45/* ------------------------------------------------------------------------- */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* match always succeeds, as we want the probe() to tell if we really accept this match */
48static int i2c_device_match(struct device *dev, struct device_driver *drv)
49{
50 return 1;
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int i2c_device_probe(struct device *dev)
54{
55 return -ENODEV;
56}
57
58static int i2c_device_remove(struct device *dev)
59{
60 return 0;
61}
62
David Brownellf37dd802007-02-13 22:09:00 +010063static void i2c_device_shutdown(struct device *dev)
64{
65 struct i2c_driver *driver;
66
67 if (!dev->driver)
68 return;
69 driver = to_i2c_driver(dev->driver);
70 if (driver->shutdown)
71 driver->shutdown(to_i2c_client(dev));
72}
73
74static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
75{
76 struct i2c_driver *driver;
77
78 if (!dev->driver)
79 return 0;
80 driver = to_i2c_driver(dev->driver);
81 if (!driver->suspend)
82 return 0;
83 return driver->suspend(to_i2c_client(dev), mesg);
84}
85
86static int i2c_device_resume(struct device * dev)
87{
88 struct i2c_driver *driver;
89
90 if (!dev->driver)
91 return 0;
92 driver = to_i2c_driver(dev->driver);
93 if (!driver->resume)
94 return 0;
95 return driver->resume(to_i2c_client(dev));
96}
97
Russell Kingb864c7d2006-01-05 14:37:50 +000098struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +010099 .name = "i2c",
100 .match = i2c_device_match,
101 .probe = i2c_device_probe,
102 .remove = i2c_device_remove,
103 .shutdown = i2c_device_shutdown,
104 .suspend = i2c_device_suspend,
105 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000106};
107
David Brownellf37dd802007-02-13 22:09:00 +0100108/* ------------------------------------------------------------------------- */
109
Jean Delvareefde7232005-07-20 23:03:50 +0200110void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
113 complete(&adap->dev_released);
114}
115
Jean Delvareefde7232005-07-20 23:03:50 +0200116struct device_driver i2c_adapter_driver = {
Laurent Riffard6586bcd2005-10-17 22:54:45 +0200117 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .name = "i2c_adapter",
119 .bus = &i2c_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
David Brownellb119dc32007-01-04 13:07:04 +0100122/* ------------------------------------------------------------------------- */
123
124/* I2C bus adapters -- one roots each I2C or SMBUS segment */
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void i2c_adapter_class_dev_release(struct class_device *dev)
127{
128 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
129 complete(&adap->class_dev_released);
130}
131
David Brownellb119dc32007-01-04 13:07:04 +0100132static ssize_t i2c_adapter_show_name(struct class_device *cdev, char *buf)
133{
134 struct i2c_adapter *adap = class_dev_to_i2c_adapter(cdev);
135 return sprintf(buf, "%s\n", adap->name);
136}
137
138static struct class_device_attribute i2c_adapter_attrs[] = {
139 __ATTR(name, S_IRUGO, i2c_adapter_show_name, NULL),
140 { },
141};
142
Jean Delvareefde7232005-07-20 23:03:50 +0200143struct class i2c_adapter_class = {
David Brownellb119dc32007-01-04 13:07:04 +0100144 .owner = THIS_MODULE,
145 .name = "i2c-adapter",
146 .class_dev_attrs = i2c_adapter_attrs,
147 .release = &i2c_adapter_class_dev_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Yani Ioannoue404e272005-05-17 06:42:58 -0400150static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
153 return sprintf(buf, "%s\n", adap->name);
154}
155static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
156
157
158static void i2c_client_release(struct device *dev)
159{
160 struct i2c_client *client = to_i2c_client(dev);
161 complete(&client->released);
162}
163
Yani Ioannoue404e272005-05-17 06:42:58 -0400164static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct i2c_client *client = to_i2c_client(dev);
167 return sprintf(buf, "%s\n", client->name);
168}
169
David Brownell438d6c22006-12-10 21:21:31 +0100170/*
Jean Delvare7b77d062006-12-10 21:21:31 +0100171 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
172 * an i2c adapter attribute (above).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Jean Delvare7b77d062006-12-10 21:21:31 +0100174static struct device_attribute dev_attr_client_name =
175 __ATTR(name, S_IRUGO, &show_client_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177
178/* ---------------------------------------------------
David Brownell438d6c22006-12-10 21:21:31 +0100179 * registering functions
180 * ---------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
182
183/* -----
184 * i2c_add_adapter is called from within the algorithm layer,
185 * when a new hw adapter registers. A new device is register to be
186 * available for clients.
187 */
188int i2c_add_adapter(struct i2c_adapter *adap)
189{
190 int id, res = 0;
191 struct list_head *item;
192 struct i2c_driver *driver;
193
Arjan van de Venb3585e42006-01-11 10:50:26 +0100194 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
197 res = -ENOMEM;
198 goto out_unlock;
199 }
200
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400201 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (res < 0) {
203 if (res == -EAGAIN)
204 res = -ENOMEM;
205 goto out_unlock;
206 }
207
208 adap->nr = id & MAX_ID_MASK;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100209 mutex_init(&adap->bus_lock);
210 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 list_add_tail(&adap->list,&adapters);
212 INIT_LIST_HEAD(&adap->clients);
213
214 /* Add the adapter to the driver core.
215 * If the parent pointer is not set up,
216 * we add this adapter to the host bus.
217 */
David Brownellb119dc32007-01-04 13:07:04 +0100218 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 adap->dev.parent = &platform_bus;
David Brownellb119dc32007-01-04 13:07:04 +0100220 printk(KERN_WARNING "**WARNING** I2C adapter driver [%s] "
221 "forgot to specify physical device; fix it!\n",
222 adap->name);
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
225 adap->dev.driver = &i2c_adapter_driver;
226 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200227 res = device_register(&adap->dev);
228 if (res)
229 goto out_list;
230 res = device_create_file(&adap->dev, &dev_attr_name);
231 if (res)
232 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* Add this adapter to the i2c_adapter class */
235 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
236 adap->class_dev.dev = &adap->dev;
237 adap->class_dev.class = &i2c_adapter_class;
238 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200239 res = class_device_register(&adap->class_dev);
240 if (res)
241 goto out_remove_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200243 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* inform drivers of new adapters */
246 list_for_each(item,&drivers) {
247 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100248 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* We ignore the return code; if it fails, too bad */
250 driver->attach_adapter(adap);
251 }
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100254 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200256
257out_remove_name:
258 device_remove_file(&adap->dev, &dev_attr_name);
259out_unregister:
260 init_completion(&adap->dev_released); /* Needed? */
261 device_unregister(&adap->dev);
262 wait_for_completion(&adap->dev_released);
263out_list:
264 list_del(&adap->list);
265 idr_remove(&i2c_adapter_idr, adap->nr);
266 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270int i2c_del_adapter(struct i2c_adapter *adap)
271{
272 struct list_head *item, *_n;
273 struct i2c_adapter *adap_from_list;
274 struct i2c_driver *driver;
275 struct i2c_client *client;
276 int res = 0;
277
Arjan van de Venb3585e42006-01-11 10:50:26 +0100278 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* First make sure that this adapter was ever added */
281 list_for_each_entry(adap_from_list, &adapters, list) {
282 if (adap_from_list == adap)
283 break;
284 }
285 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200286 pr_debug("i2c-core: attempting to delete unregistered "
287 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 res = -EINVAL;
289 goto out_unlock;
290 }
291
292 list_for_each(item,&drivers) {
293 driver = list_entry(item, struct i2c_driver, list);
294 if (driver->detach_adapter)
295 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200296 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100297 "for driver [%s]\n",
298 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto out_unlock;
300 }
301 }
302
303 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200304 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 list_for_each_safe(item, _n, &adap->clients) {
306 client = list_entry(item, struct i2c_client, list);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200309 dev_err(&adap->dev, "detach_client failed for client "
310 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 client->addr);
312 goto out_unlock;
313 }
314 }
315
316 /* clean up the sysfs representation */
317 init_completion(&adap->dev_released);
318 init_completion(&adap->class_dev_released);
319 class_device_unregister(&adap->class_dev);
320 device_remove_file(&adap->dev, &dev_attr_name);
321 device_unregister(&adap->dev);
322 list_del(&adap->list);
323
324 /* wait for sysfs to drop all references */
325 wait_for_completion(&adap->dev_released);
326 wait_for_completion(&adap->class_dev_released);
327
328 /* free dynamically allocated bus id */
329 idr_remove(&i2c_adapter_idr, adap->nr);
330
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200331 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100334 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return res;
336}
337
338
339/* -----
340 * What follows is the "upwards" interface: commands for talking to clients,
341 * which implement the functions to access the physical information of the
342 * chips.
343 */
344
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800345int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct list_head *item;
348 struct i2c_adapter *adapter;
Jean Delvare7eebcb72006-02-05 23:28:21 +0100349 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800352 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 res = driver_register(&driver->driver);
356 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100357 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100358
Jean Delvare7eebcb72006-02-05 23:28:21 +0100359 mutex_lock(&core_lists);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100362 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* now look for instances of driver on our adapters */
Jean Delvare8a994752005-11-26 20:28:06 +0100365 if (driver->attach_adapter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 list_for_each(item,&adapters) {
367 adapter = list_entry(item, struct i2c_adapter, list);
368 driver->attach_adapter(adapter);
369 }
370 }
371
Arjan van de Venb3585e42006-01-11 10:50:26 +0100372 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800375EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377int i2c_del_driver(struct i2c_driver *driver)
378{
379 struct list_head *item1, *item2, *_n;
380 struct i2c_client *client;
381 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int res = 0;
384
Arjan van de Venb3585e42006-01-11 10:50:26 +0100385 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100388 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391 list_for_each(item1,&adapters) {
392 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (driver->detach_adapter) {
394 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200395 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100396 "for driver [%s]\n",
397 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto out_unlock;
399 }
400 } else {
401 list_for_each_safe(item2, _n, &adap->clients) {
402 client = list_entry(item2, struct i2c_client, list);
403 if (client->driver != driver)
404 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200405 dev_dbg(&adap->dev, "detaching client [%s] "
406 "at 0x%02x\n", client->name,
407 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200409 dev_err(&adap->dev, "detach_client "
410 "failed for client [%s] at "
411 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 client->addr);
413 goto out_unlock;
414 }
415 }
416 }
417 }
418
419 driver_unregister(&driver->driver);
420 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100421 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100424 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 0;
426}
427
428static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
429{
430 struct list_head *item;
431 struct i2c_client *client;
432
433 list_for_each(item,&adapter->clients) {
434 client = list_entry(item, struct i2c_client, list);
435 if (client->addr == addr)
436 return -EBUSY;
437 }
438 return 0;
439}
440
441int i2c_check_addr(struct i2c_adapter *adapter, int addr)
442{
443 int rval;
444
Ingo Molnar5c085d32006-01-18 23:16:04 +0100445 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100447 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return rval;
450}
451
452int i2c_attach_client(struct i2c_client *client)
453{
454 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200455 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Ingo Molnar5c085d32006-01-18 23:16:04 +0100457 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200459 res = -EBUSY;
460 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100463
Jean Delvarecde78592005-11-26 21:00:54 +0100464 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 client->dev.parent = &client->adapter->dev;
467 client->dev.driver = &client->driver->driver;
468 client->dev.bus = &i2c_bus_type;
469 client->dev.release = &i2c_client_release;
David Brownell438d6c22006-12-10 21:21:31 +0100470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
472 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200473 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
474 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200475 res = device_register(&client->dev);
476 if (res)
477 goto out_list;
478 res = device_create_file(&client->dev, &dev_attr_client_name);
479 if (res)
480 goto out_unregister;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200481 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200482
483 if (adapter->client_register) {
484 if (adapter->client_register(client)) {
485 dev_dbg(&adapter->dev, "client_register "
486 "failed for client [%s] at 0x%02x\n",
487 client->name, client->addr);
488 }
489 }
490
491 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200492
493out_unregister:
494 init_completion(&client->released); /* Needed? */
495 device_unregister(&client->dev);
496 wait_for_completion(&client->released);
497out_list:
498 list_del(&client->list);
499 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
500 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200501out_unlock:
502 mutex_unlock(&adapter->clist_lock);
503 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506
507int i2c_detach_client(struct i2c_client *client)
508{
509 struct i2c_adapter *adapter = client->adapter;
510 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100511
Jean Delvarecde78592005-11-26 21:00:54 +0100512 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200513 dev_warn(&client->dev, "Client [%s] still busy, "
514 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (adapter->client_unregister) {
519 res = adapter->client_unregister(client);
520 if (res) {
521 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700522 "client_unregister [%s] failed, "
523 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto out;
525 }
526 }
527
Ingo Molnar5c085d32006-01-18 23:16:04 +0100528 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 list_del(&client->list);
530 init_completion(&client->released);
531 device_remove_file(&client->dev, &dev_attr_client_name);
532 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100533 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 wait_for_completion(&client->released);
535
536 out:
537 return res;
538}
539
540static int i2c_inc_use_client(struct i2c_client *client)
541{
542
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100543 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -ENODEV;
545 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100546 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -ENODEV;
548 }
549
550 return 0;
551}
552
553static void i2c_dec_use_client(struct i2c_client *client)
554{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100555 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 module_put(client->adapter->owner);
557}
558
559int i2c_use_client(struct i2c_client *client)
560{
561 int ret;
562
563 ret = i2c_inc_use_client(client);
564 if (ret)
565 return ret;
566
Jean Delvarecde78592005-11-26 21:00:54 +0100567 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572int i2c_release_client(struct i2c_client *client)
573{
Jean Delvarecde78592005-11-26 21:00:54 +0100574 if (!client->usage_count) {
575 pr_debug("i2c-core: %s used one too many times\n",
576 __FUNCTION__);
577 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
David Brownell438d6c22006-12-10 21:21:31 +0100579
Jean Delvarecde78592005-11-26 21:00:54 +0100580 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584}
585
586void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
587{
588 struct list_head *item;
589 struct i2c_client *client;
590
Ingo Molnar5c085d32006-01-18 23:16:04 +0100591 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 list_for_each(item,&adap->clients) {
593 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100594 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 continue;
596 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100597 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100599 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100601 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100603 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606static int __init i2c_init(void)
607{
608 int retval;
609
610 retval = bus_register(&i2c_bus_type);
611 if (retval)
612 return retval;
613 retval = driver_register(&i2c_adapter_driver);
614 if (retval)
615 return retval;
616 return class_register(&i2c_adapter_class);
617}
618
619static void __exit i2c_exit(void)
620{
621 class_unregister(&i2c_adapter_class);
622 driver_unregister(&i2c_adapter_driver);
623 bus_unregister(&i2c_bus_type);
624}
625
626subsys_initcall(i2c_init);
627module_exit(i2c_exit);
628
629/* ----------------------------------------------------
630 * the functional interface to the i2c busses.
631 * ----------------------------------------------------
632 */
633
634int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
635{
636 int ret;
637
638 if (adap->algo->master_xfer) {
639#ifdef DEBUG
640 for (ret = 0; ret < num; ret++) {
641 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
642 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
643 'R' : 'W', msgs[ret].addr, msgs[ret].len);
644 }
645#endif
646
Jiri Kosina6ea23032006-12-10 21:21:30 +0100647 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100649 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 return ret;
652 } else {
653 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
654 return -ENOSYS;
655 }
656}
657
658int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
659{
660 int ret;
661 struct i2c_adapter *adap=client->adapter;
662 struct i2c_msg msg;
663
Jean Delvare815f55f2005-05-07 22:58:46 +0200664 msg.addr = client->addr;
665 msg.flags = client->flags & I2C_M_TEN;
666 msg.len = count;
667 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100668
Jean Delvare815f55f2005-05-07 22:58:46 +0200669 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jean Delvare815f55f2005-05-07 22:58:46 +0200671 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
672 transmitted, else error code. */
673 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
677{
678 struct i2c_adapter *adap=client->adapter;
679 struct i2c_msg msg;
680 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jean Delvare815f55f2005-05-07 22:58:46 +0200682 msg.addr = client->addr;
683 msg.flags = client->flags & I2C_M_TEN;
684 msg.flags |= I2C_M_RD;
685 msg.len = count;
686 msg.buf = buf;
687
688 ret = i2c_transfer(adap, &msg, 1);
689
690 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
691 transmitted, else error code. */
692 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695
696int i2c_control(struct i2c_client *client,
697 unsigned int cmd, unsigned long arg)
698{
699 int ret = 0;
700 struct i2c_adapter *adap = client->adapter;
701
702 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
703 switch (cmd) {
704 case I2C_RETRIES:
705 adap->retries = arg;
706 break;
707 case I2C_TIMEOUT:
708 adap->timeout = arg;
709 break;
710 default:
711 if (adap->algo->algo_control!=NULL)
712 ret = adap->algo->algo_control(adap,cmd,arg);
713 }
714 return ret;
715}
716
717/* ----------------------------------------------------
718 * the i2c address scanning function
719 * Will not work for 10-bit addresses!
720 * ----------------------------------------------------
721 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200722static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
723 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200724{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200725 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200726
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200727 /* Make sure the address is valid */
728 if (addr < 0x03 || addr > 0x77) {
729 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
730 addr);
731 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200732 }
733
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200734 /* Skip if already in use */
735 if (i2c_check_addr(adapter, addr))
736 return 0;
737
738 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200739 if (kind < 0) {
740 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
741 I2C_SMBUS_QUICK, NULL) < 0)
742 return 0;
743
744 /* prevent 24RF08 corruption */
745 if ((addr & ~0x0f) == 0x50)
746 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
747 I2C_SMBUS_QUICK, NULL);
748 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200749
750 /* Finally call the custom detection function */
751 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200752 /* -ENODEV can be returned if there is a chip at the given address
753 but it isn't supported by this chip driver. We catch it here as
754 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200755 if (err == -ENODEV)
756 err = 0;
757
758 if (err)
759 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
760 addr, err);
761 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764int i2c_probe(struct i2c_adapter *adapter,
765 struct i2c_client_address_data *address_data,
766 int (*found_proc) (struct i2c_adapter *, int, int))
767{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200768 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int adap_id = i2c_adapter_id(adapter);
770
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200771 /* Force entries are done first, and are not affected by ignore
772 entries */
773 if (address_data->forces) {
774 unsigned short **forces = address_data->forces;
775 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200777 for (kind = 0; forces[kind]; kind++) {
778 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
779 i += 2) {
780 if (forces[kind][i] == adap_id
781 || forces[kind][i] == ANY_I2C_BUS) {
782 dev_dbg(&adapter->dev, "found force "
783 "parameter for adapter %d, "
784 "addr 0x%02x, kind %d\n",
785 adap_id, forces[kind][i + 1],
786 kind);
787 err = i2c_probe_address(adapter,
788 forces[kind][i + 1],
789 kind, found_proc);
790 if (err)
791 return err;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200796
Jean Delvare4366dc92005-09-25 16:50:06 +0200797 /* Stop here if we can't use SMBUS_QUICK */
798 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
799 if (address_data->probe[0] == I2C_CLIENT_END
800 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100801 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200802
803 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
804 "can't probe for chips\n");
805 return -1;
806 }
807
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200808 /* Probe entries are done second, and are not affected by ignore
809 entries either */
810 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
811 if (address_data->probe[i] == adap_id
812 || address_data->probe[i] == ANY_I2C_BUS) {
813 dev_dbg(&adapter->dev, "found probe parameter for "
814 "adapter %d, addr 0x%02x\n", adap_id,
815 address_data->probe[i + 1]);
816 err = i2c_probe_address(adapter,
817 address_data->probe[i + 1],
818 -1, found_proc);
819 if (err)
820 return err;
821 }
822 }
823
824 /* Normal entries are done last, unless shadowed by an ignore entry */
825 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
826 int j, ignore;
827
828 ignore = 0;
829 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
830 j += 2) {
831 if ((address_data->ignore[j] == adap_id ||
832 address_data->ignore[j] == ANY_I2C_BUS)
833 && address_data->ignore[j + 1]
834 == address_data->normal_i2c[i]) {
835 dev_dbg(&adapter->dev, "found ignore "
836 "parameter for adapter %d, "
837 "addr 0x%02x\n", adap_id,
838 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +0200839 ignore = 1;
840 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200841 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200842 }
843 if (ignore)
844 continue;
845
846 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
847 "addr 0x%02x\n", adap_id,
848 address_data->normal_i2c[i]);
849 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
850 -1, found_proc);
851 if (err)
852 return err;
853 }
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return 0;
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858struct i2c_adapter* i2c_get_adapter(int id)
859{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +0100861
Arjan van de Venb3585e42006-01-11 10:50:26 +0100862 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400863 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
864 if (adapter && !try_module_get(adapter->owner))
865 adapter = NULL;
866
Arjan van de Venb3585e42006-01-11 10:50:26 +0100867 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400868 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871void i2c_put_adapter(struct i2c_adapter *adap)
872{
873 module_put(adap->owner);
874}
875
876/* The SMBus parts */
877
David Brownell438d6c22006-12-10 21:21:31 +0100878#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879static u8
880crc8(u16 data)
881{
882 int i;
David Brownell438d6c22006-12-10 21:21:31 +0100883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +0100885 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 data = data ^ POLY;
887 data = data << 1;
888 }
889 return (u8)(data >> 8);
890}
891
Jean Delvare421ef472005-10-26 21:28:55 +0200892/* Incremental CRC8 over count bytes in the array pointed to by p */
893static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 int i;
896
897 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200898 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return crc;
900}
901
Jean Delvare421ef472005-10-26 21:28:55 +0200902/* Assume a 7-bit address, which is reasonable for SMBus */
903static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Jean Delvare421ef472005-10-26 21:28:55 +0200905 /* The address will be sent first */
906 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
907 pec = i2c_smbus_pec(pec, &addr, 1);
908
909 /* The data buffer follows */
910 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Jean Delvare421ef472005-10-26 21:28:55 +0200913/* Used for write only transactions */
914static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Jean Delvare421ef472005-10-26 21:28:55 +0200916 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
917 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Jean Delvare421ef472005-10-26 21:28:55 +0200920/* Return <0 on CRC error
921 If there was a write before this read (most cases) we need to take the
922 partial CRC from the write part into account.
923 Note that this function does modify the message (we need to decrease the
924 message length to hide the CRC byte from the caller). */
925static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Jean Delvare421ef472005-10-26 21:28:55 +0200927 u8 rpec = msg->buf[--msg->len];
928 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (rpec != cpec) {
931 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
932 rpec, cpec);
933 return -1;
934 }
David Brownell438d6c22006-12-10 21:21:31 +0100935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
939{
940 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +0100941 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944s32 i2c_smbus_read_byte(struct i2c_client *client)
945{
946 union i2c_smbus_data data;
947 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
948 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
949 return -1;
950 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200951 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
955{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200957 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
961{
962 union i2c_smbus_data data;
963 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
964 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
965 return -1;
966 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200967 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
971{
972 union i2c_smbus_data data;
973 data.byte = value;
974 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
975 I2C_SMBUS_WRITE,command,
976 I2C_SMBUS_BYTE_DATA,&data);
977}
978
979s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
980{
981 union i2c_smbus_data data;
982 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
983 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
984 return -1;
985 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200986 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
990{
991 union i2c_smbus_data data;
992 data.word = value;
993 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
994 I2C_SMBUS_WRITE,command,
995 I2C_SMBUS_WORD_DATA,&data);
996}
997
998s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200999 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (length > I2C_SMBUS_BLOCK_MAX)
1004 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001006 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1008 I2C_SMBUS_WRITE,command,
1009 I2C_SMBUS_BLOCK_DATA,&data);
1010}
1011
1012/* Returns the number of read bytes */
1013s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1014{
1015 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1018 I2C_SMBUS_READ,command,
1019 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1020 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001021
1022 memcpy(values, &data.block[1], data.block[0]);
1023 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Jean Delvare21bbd692006-01-09 15:19:18 +11001026s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001027 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001028{
1029 union i2c_smbus_data data;
1030
1031 if (length > I2C_SMBUS_BLOCK_MAX)
1032 length = I2C_SMBUS_BLOCK_MAX;
1033 data.block[0] = length;
1034 memcpy(data.block + 1, values, length);
1035 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1036 I2C_SMBUS_WRITE, command,
1037 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1038}
1039
David Brownell438d6c22006-12-10 21:21:31 +01001040/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001042static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001044 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 union i2c_smbus_data * data)
1046{
1047 /* So we need to generate a series of msgs. In the case of writing, we
1048 need to use only one message; when reading, we need two. We initialize
1049 most things with sane defaults, to keep the code below somewhat
1050 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001051 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1052 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001054 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1056 };
1057 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001058 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 msgbuf0[0] = command;
1061 switch(size) {
1062 case I2C_SMBUS_QUICK:
1063 msg[0].len = 0;
1064 /* Special case: The read/write field is used as data */
1065 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1066 num = 1;
1067 break;
1068 case I2C_SMBUS_BYTE:
1069 if (read_write == I2C_SMBUS_READ) {
1070 /* Special case: only a read! */
1071 msg[0].flags = I2C_M_RD | flags;
1072 num = 1;
1073 }
1074 break;
1075 case I2C_SMBUS_BYTE_DATA:
1076 if (read_write == I2C_SMBUS_READ)
1077 msg[1].len = 1;
1078 else {
1079 msg[0].len = 2;
1080 msgbuf0[1] = data->byte;
1081 }
1082 break;
1083 case I2C_SMBUS_WORD_DATA:
1084 if (read_write == I2C_SMBUS_READ)
1085 msg[1].len = 2;
1086 else {
1087 msg[0].len=3;
1088 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001089 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 break;
1092 case I2C_SMBUS_PROC_CALL:
1093 num = 2; /* Special case */
1094 read_write = I2C_SMBUS_READ;
1095 msg[0].len = 3;
1096 msg[1].len = 2;
1097 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001098 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 break;
1100 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (read_write == I2C_SMBUS_READ) {
1102 dev_err(&adapter->dev, "Block read not supported "
1103 "under I2C emulation!\n");
1104 return -1;
1105 } else {
1106 msg[0].len = data->block[0] + 2;
1107 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1108 dev_err(&adapter->dev, "smbus_access called with "
1109 "invalid block write size (%d)\n",
1110 data->block[0]);
1111 return -1;
1112 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001113 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 msgbuf0[i] = data->block[i-1];
1115 }
1116 break;
1117 case I2C_SMBUS_BLOCK_PROC_CALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 dev_dbg(&adapter->dev, "Block process call not supported "
1119 "under I2C emulation!\n");
1120 return -1;
1121 case I2C_SMBUS_I2C_BLOCK_DATA:
1122 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001123 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 } else {
1125 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001126 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1128 "invalid block write size (%d)\n",
1129 data->block[0]);
1130 return -1;
1131 }
1132 for (i = 1; i <= data->block[0]; i++)
1133 msgbuf0[i] = data->block[i];
1134 }
1135 break;
1136 default:
1137 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1138 size);
1139 return -1;
1140 }
1141
Jean Delvare421ef472005-10-26 21:28:55 +02001142 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1143 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1144 if (i) {
1145 /* Compute PEC if first message is a write */
1146 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001147 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001148 i2c_smbus_add_pec(&msg[0]);
1149 else /* Write followed by read */
1150 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1151 }
1152 /* Ask for PEC if last message is a read */
1153 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001154 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001155 }
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (i2c_transfer(adapter, msg, num) < 0)
1158 return -1;
1159
Jean Delvare421ef472005-10-26 21:28:55 +02001160 /* Check PEC if last message is a read */
1161 if (i && (msg[num-1].flags & I2C_M_RD)) {
1162 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1163 return -1;
1164 }
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (read_write == I2C_SMBUS_READ)
1167 switch(size) {
1168 case I2C_SMBUS_BYTE:
1169 data->byte = msgbuf0[0];
1170 break;
1171 case I2C_SMBUS_BYTE_DATA:
1172 data->byte = msgbuf1[0];
1173 break;
David Brownell438d6c22006-12-10 21:21:31 +01001174 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 case I2C_SMBUS_PROC_CALL:
1176 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1177 break;
1178 case I2C_SMBUS_I2C_BLOCK_DATA:
1179 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001180 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1181 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 data->block[i+1] = msgbuf1[i];
1183 break;
1184 }
1185 return 0;
1186}
1187
1188
1189s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001190 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 union i2c_smbus_data * data)
1192{
1193 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001198 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1200 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001201 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 } else
1203 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1204 command,size,data);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return res;
1207}
1208
1209
Jean Delvareefde7232005-07-20 23:03:50 +02001210/* Next four are needed by i2c-isa */
1211EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1212EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1213EXPORT_SYMBOL_GPL(i2c_adapter_class);
1214EXPORT_SYMBOL_GPL(i2c_bus_type);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216EXPORT_SYMBOL(i2c_add_adapter);
1217EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218EXPORT_SYMBOL(i2c_del_driver);
1219EXPORT_SYMBOL(i2c_attach_client);
1220EXPORT_SYMBOL(i2c_detach_client);
1221EXPORT_SYMBOL(i2c_use_client);
1222EXPORT_SYMBOL(i2c_release_client);
1223EXPORT_SYMBOL(i2c_clients_command);
1224EXPORT_SYMBOL(i2c_check_addr);
1225
1226EXPORT_SYMBOL(i2c_master_send);
1227EXPORT_SYMBOL(i2c_master_recv);
1228EXPORT_SYMBOL(i2c_control);
1229EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230EXPORT_SYMBOL(i2c_get_adapter);
1231EXPORT_SYMBOL(i2c_put_adapter);
1232EXPORT_SYMBOL(i2c_probe);
1233
1234EXPORT_SYMBOL(i2c_smbus_xfer);
1235EXPORT_SYMBOL(i2c_smbus_write_quick);
1236EXPORT_SYMBOL(i2c_smbus_read_byte);
1237EXPORT_SYMBOL(i2c_smbus_write_byte);
1238EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1239EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1240EXPORT_SYMBOL(i2c_smbus_read_word_data);
1241EXPORT_SYMBOL(i2c_smbus_write_word_data);
1242EXPORT_SYMBOL(i2c_smbus_write_block_data);
1243EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001244EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1247MODULE_DESCRIPTION("I2C-Bus main module");
1248MODULE_LICENSE("GPL");