blob: 8dca4848ad9275db06b5659d9d3ca58f059cd3ed [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
David Brownellb119dc32007-01-04 13:07:04 +0100116/* ------------------------------------------------------------------------- */
117
118/* I2C bus adapters -- one roots each I2C or SMBUS segment */
119
Jean Delvareefde7232005-07-20 23:03:50 +0200120struct class i2c_adapter_class = {
David Brownellb119dc32007-01-04 13:07:04 +0100121 .owner = THIS_MODULE,
122 .name = "i2c-adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Yani Ioannoue404e272005-05-17 06:42:58 -0400125static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
128 return sprintf(buf, "%s\n", adap->name);
129}
130static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
131
132
133static void i2c_client_release(struct device *dev)
134{
135 struct i2c_client *client = to_i2c_client(dev);
136 complete(&client->released);
137}
138
Yani Ioannoue404e272005-05-17 06:42:58 -0400139static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct i2c_client *client = to_i2c_client(dev);
142 return sprintf(buf, "%s\n", client->name);
143}
144
David Brownell438d6c22006-12-10 21:21:31 +0100145/*
Jean Delvare7b77d062006-12-10 21:21:31 +0100146 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
147 * an i2c adapter attribute (above).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
Jean Delvare7b77d062006-12-10 21:21:31 +0100149static struct device_attribute dev_attr_client_name =
150 __ATTR(name, S_IRUGO, &show_client_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152
153/* ---------------------------------------------------
David Brownell438d6c22006-12-10 21:21:31 +0100154 * registering functions
155 * ---------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
157
158/* -----
159 * i2c_add_adapter is called from within the algorithm layer,
160 * when a new hw adapter registers. A new device is register to be
161 * available for clients.
162 */
163int i2c_add_adapter(struct i2c_adapter *adap)
164{
165 int id, res = 0;
166 struct list_head *item;
167 struct i2c_driver *driver;
168
Arjan van de Venb3585e42006-01-11 10:50:26 +0100169 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
172 res = -ENOMEM;
173 goto out_unlock;
174 }
175
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400176 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (res < 0) {
178 if (res == -EAGAIN)
179 res = -ENOMEM;
180 goto out_unlock;
181 }
182
183 adap->nr = id & MAX_ID_MASK;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100184 mutex_init(&adap->bus_lock);
185 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 list_add_tail(&adap->list,&adapters);
187 INIT_LIST_HEAD(&adap->clients);
188
189 /* Add the adapter to the driver core.
190 * If the parent pointer is not set up,
191 * we add this adapter to the host bus.
192 */
David Brownellb119dc32007-01-04 13:07:04 +0100193 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100195 pr_debug("I2C adapter driver [%s] forgot to specify "
196 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200200 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200201 res = device_register(&adap->dev);
202 if (res)
203 goto out_list;
204 res = device_create_file(&adap->dev, &dev_attr_name);
205 if (res)
206 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200208 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* inform drivers of new adapters */
211 list_for_each(item,&drivers) {
212 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100213 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* We ignore the return code; if it fails, too bad */
215 driver->attach_adapter(adap);
216 }
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100219 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200221
Jean Delvareb119c6c2006-08-15 18:26:30 +0200222out_unregister:
223 init_completion(&adap->dev_released); /* Needed? */
224 device_unregister(&adap->dev);
225 wait_for_completion(&adap->dev_released);
226out_list:
227 list_del(&adap->list);
228 idr_remove(&i2c_adapter_idr, adap->nr);
229 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232
233int i2c_del_adapter(struct i2c_adapter *adap)
234{
235 struct list_head *item, *_n;
236 struct i2c_adapter *adap_from_list;
237 struct i2c_driver *driver;
238 struct i2c_client *client;
239 int res = 0;
240
Arjan van de Venb3585e42006-01-11 10:50:26 +0100241 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* First make sure that this adapter was ever added */
244 list_for_each_entry(adap_from_list, &adapters, list) {
245 if (adap_from_list == adap)
246 break;
247 }
248 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200249 pr_debug("i2c-core: attempting to delete unregistered "
250 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 res = -EINVAL;
252 goto out_unlock;
253 }
254
255 list_for_each(item,&drivers) {
256 driver = list_entry(item, struct i2c_driver, list);
257 if (driver->detach_adapter)
258 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200259 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100260 "for driver [%s]\n",
261 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto out_unlock;
263 }
264 }
265
266 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200267 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 list_for_each_safe(item, _n, &adap->clients) {
269 client = list_entry(item, struct i2c_client, list);
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200272 dev_err(&adap->dev, "detach_client failed for client "
273 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 client->addr);
275 goto out_unlock;
276 }
277 }
278
279 /* clean up the sysfs representation */
280 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 device_remove_file(&adap->dev, &dev_attr_name);
282 device_unregister(&adap->dev);
283 list_del(&adap->list);
284
285 /* wait for sysfs to drop all references */
286 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /* free dynamically allocated bus id */
289 idr_remove(&i2c_adapter_idr, adap->nr);
290
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200291 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100294 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return res;
296}
297
298
299/* -----
300 * What follows is the "upwards" interface: commands for talking to clients,
301 * which implement the functions to access the physical information of the
302 * chips.
303 */
304
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800305int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct list_head *item;
308 struct i2c_adapter *adapter;
Jean Delvare7eebcb72006-02-05 23:28:21 +0100309 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800312 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 res = driver_register(&driver->driver);
316 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100317 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100318
Jean Delvare7eebcb72006-02-05 23:28:21 +0100319 mutex_lock(&core_lists);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100322 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* now look for instances of driver on our adapters */
Jean Delvare8a994752005-11-26 20:28:06 +0100325 if (driver->attach_adapter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 list_for_each(item,&adapters) {
327 adapter = list_entry(item, struct i2c_adapter, list);
328 driver->attach_adapter(adapter);
329 }
330 }
331
Arjan van de Venb3585e42006-01-11 10:50:26 +0100332 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800335EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337int i2c_del_driver(struct i2c_driver *driver)
338{
339 struct list_head *item1, *item2, *_n;
340 struct i2c_client *client;
341 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int res = 0;
344
Arjan van de Venb3585e42006-01-11 10:50:26 +0100345 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100348 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
351 list_for_each(item1,&adapters) {
352 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (driver->detach_adapter) {
354 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200355 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100356 "for driver [%s]\n",
357 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto out_unlock;
359 }
360 } else {
361 list_for_each_safe(item2, _n, &adap->clients) {
362 client = list_entry(item2, struct i2c_client, list);
363 if (client->driver != driver)
364 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200365 dev_dbg(&adap->dev, "detaching client [%s] "
366 "at 0x%02x\n", client->name,
367 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200369 dev_err(&adap->dev, "detach_client "
370 "failed for client [%s] at "
371 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 client->addr);
373 goto out_unlock;
374 }
375 }
376 }
377 }
378
379 driver_unregister(&driver->driver);
380 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100381 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100384 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386}
387
388static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
389{
390 struct list_head *item;
391 struct i2c_client *client;
392
393 list_for_each(item,&adapter->clients) {
394 client = list_entry(item, struct i2c_client, list);
395 if (client->addr == addr)
396 return -EBUSY;
397 }
398 return 0;
399}
400
401int i2c_check_addr(struct i2c_adapter *adapter, int addr)
402{
403 int rval;
404
Ingo Molnar5c085d32006-01-18 23:16:04 +0100405 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100407 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 return rval;
410}
411
412int i2c_attach_client(struct i2c_client *client)
413{
414 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200415 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Ingo Molnar5c085d32006-01-18 23:16:04 +0100417 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200419 res = -EBUSY;
420 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100423
Jean Delvarecde78592005-11-26 21:00:54 +0100424 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 client->dev.parent = &client->adapter->dev;
427 client->dev.driver = &client->driver->driver;
428 client->dev.bus = &i2c_bus_type;
429 client->dev.release = &i2c_client_release;
David Brownell438d6c22006-12-10 21:21:31 +0100430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
432 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200433 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
434 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200435 res = device_register(&client->dev);
436 if (res)
437 goto out_list;
438 res = device_create_file(&client->dev, &dev_attr_client_name);
439 if (res)
440 goto out_unregister;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200441 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200442
443 if (adapter->client_register) {
444 if (adapter->client_register(client)) {
445 dev_dbg(&adapter->dev, "client_register "
446 "failed for client [%s] at 0x%02x\n",
447 client->name, client->addr);
448 }
449 }
450
451 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200452
453out_unregister:
454 init_completion(&client->released); /* Needed? */
455 device_unregister(&client->dev);
456 wait_for_completion(&client->released);
457out_list:
458 list_del(&client->list);
459 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
460 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200461out_unlock:
462 mutex_unlock(&adapter->clist_lock);
463 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466
467int i2c_detach_client(struct i2c_client *client)
468{
469 struct i2c_adapter *adapter = client->adapter;
470 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100471
Jean Delvarecde78592005-11-26 21:00:54 +0100472 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200473 dev_warn(&client->dev, "Client [%s] still busy, "
474 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (adapter->client_unregister) {
479 res = adapter->client_unregister(client);
480 if (res) {
481 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700482 "client_unregister [%s] failed, "
483 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 goto out;
485 }
486 }
487
Ingo Molnar5c085d32006-01-18 23:16:04 +0100488 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 list_del(&client->list);
490 init_completion(&client->released);
491 device_remove_file(&client->dev, &dev_attr_client_name);
492 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100493 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 wait_for_completion(&client->released);
495
496 out:
497 return res;
498}
499
500static int i2c_inc_use_client(struct i2c_client *client)
501{
502
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100503 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -ENODEV;
505 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100506 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -ENODEV;
508 }
509
510 return 0;
511}
512
513static void i2c_dec_use_client(struct i2c_client *client)
514{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100515 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 module_put(client->adapter->owner);
517}
518
519int i2c_use_client(struct i2c_client *client)
520{
521 int ret;
522
523 ret = i2c_inc_use_client(client);
524 if (ret)
525 return ret;
526
Jean Delvarecde78592005-11-26 21:00:54 +0100527 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532int i2c_release_client(struct i2c_client *client)
533{
Jean Delvarecde78592005-11-26 21:00:54 +0100534 if (!client->usage_count) {
535 pr_debug("i2c-core: %s used one too many times\n",
536 __FUNCTION__);
537 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
David Brownell438d6c22006-12-10 21:21:31 +0100539
Jean Delvarecde78592005-11-26 21:00:54 +0100540 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return 0;
544}
545
546void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
547{
548 struct list_head *item;
549 struct i2c_client *client;
550
Ingo Molnar5c085d32006-01-18 23:16:04 +0100551 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 list_for_each(item,&adap->clients) {
553 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100554 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 continue;
556 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100557 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100559 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100561 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100563 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566static int __init i2c_init(void)
567{
568 int retval;
569
570 retval = bus_register(&i2c_bus_type);
571 if (retval)
572 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return class_register(&i2c_adapter_class);
574}
575
576static void __exit i2c_exit(void)
577{
578 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 bus_unregister(&i2c_bus_type);
580}
581
582subsys_initcall(i2c_init);
583module_exit(i2c_exit);
584
585/* ----------------------------------------------------
586 * the functional interface to the i2c busses.
587 * ----------------------------------------------------
588 */
589
590int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
591{
592 int ret;
593
594 if (adap->algo->master_xfer) {
595#ifdef DEBUG
596 for (ret = 0; ret < num; ret++) {
597 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
598 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
599 'R' : 'W', msgs[ret].addr, msgs[ret].len);
600 }
601#endif
602
Jiri Kosina6ea23032006-12-10 21:21:30 +0100603 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100605 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 return ret;
608 } else {
609 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
610 return -ENOSYS;
611 }
612}
613
614int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
615{
616 int ret;
617 struct i2c_adapter *adap=client->adapter;
618 struct i2c_msg msg;
619
Jean Delvare815f55f2005-05-07 22:58:46 +0200620 msg.addr = client->addr;
621 msg.flags = client->flags & I2C_M_TEN;
622 msg.len = count;
623 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100624
Jean Delvare815f55f2005-05-07 22:58:46 +0200625 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jean Delvare815f55f2005-05-07 22:58:46 +0200627 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
628 transmitted, else error code. */
629 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
633{
634 struct i2c_adapter *adap=client->adapter;
635 struct i2c_msg msg;
636 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jean Delvare815f55f2005-05-07 22:58:46 +0200638 msg.addr = client->addr;
639 msg.flags = client->flags & I2C_M_TEN;
640 msg.flags |= I2C_M_RD;
641 msg.len = count;
642 msg.buf = buf;
643
644 ret = i2c_transfer(adap, &msg, 1);
645
646 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
647 transmitted, else error code. */
648 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651
652int i2c_control(struct i2c_client *client,
653 unsigned int cmd, unsigned long arg)
654{
655 int ret = 0;
656 struct i2c_adapter *adap = client->adapter;
657
658 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
659 switch (cmd) {
660 case I2C_RETRIES:
661 adap->retries = arg;
662 break;
663 case I2C_TIMEOUT:
664 adap->timeout = arg;
665 break;
666 default:
667 if (adap->algo->algo_control!=NULL)
668 ret = adap->algo->algo_control(adap,cmd,arg);
669 }
670 return ret;
671}
672
673/* ----------------------------------------------------
674 * the i2c address scanning function
675 * Will not work for 10-bit addresses!
676 * ----------------------------------------------------
677 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200678static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
679 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200680{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200681 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200682
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200683 /* Make sure the address is valid */
684 if (addr < 0x03 || addr > 0x77) {
685 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
686 addr);
687 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200688 }
689
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200690 /* Skip if already in use */
691 if (i2c_check_addr(adapter, addr))
692 return 0;
693
694 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200695 if (kind < 0) {
696 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
697 I2C_SMBUS_QUICK, NULL) < 0)
698 return 0;
699
700 /* prevent 24RF08 corruption */
701 if ((addr & ~0x0f) == 0x50)
702 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
703 I2C_SMBUS_QUICK, NULL);
704 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200705
706 /* Finally call the custom detection function */
707 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200708 /* -ENODEV can be returned if there is a chip at the given address
709 but it isn't supported by this chip driver. We catch it here as
710 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200711 if (err == -ENODEV)
712 err = 0;
713
714 if (err)
715 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
716 addr, err);
717 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720int i2c_probe(struct i2c_adapter *adapter,
721 struct i2c_client_address_data *address_data,
722 int (*found_proc) (struct i2c_adapter *, int, int))
723{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200724 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int adap_id = i2c_adapter_id(adapter);
726
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200727 /* Force entries are done first, and are not affected by ignore
728 entries */
729 if (address_data->forces) {
730 unsigned short **forces = address_data->forces;
731 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200733 for (kind = 0; forces[kind]; kind++) {
734 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
735 i += 2) {
736 if (forces[kind][i] == adap_id
737 || forces[kind][i] == ANY_I2C_BUS) {
738 dev_dbg(&adapter->dev, "found force "
739 "parameter for adapter %d, "
740 "addr 0x%02x, kind %d\n",
741 adap_id, forces[kind][i + 1],
742 kind);
743 err = i2c_probe_address(adapter,
744 forces[kind][i + 1],
745 kind, found_proc);
746 if (err)
747 return err;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200752
Jean Delvare4366dc92005-09-25 16:50:06 +0200753 /* Stop here if we can't use SMBUS_QUICK */
754 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
755 if (address_data->probe[0] == I2C_CLIENT_END
756 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100757 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200758
759 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
760 "can't probe for chips\n");
761 return -1;
762 }
763
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200764 /* Probe entries are done second, and are not affected by ignore
765 entries either */
766 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
767 if (address_data->probe[i] == adap_id
768 || address_data->probe[i] == ANY_I2C_BUS) {
769 dev_dbg(&adapter->dev, "found probe parameter for "
770 "adapter %d, addr 0x%02x\n", adap_id,
771 address_data->probe[i + 1]);
772 err = i2c_probe_address(adapter,
773 address_data->probe[i + 1],
774 -1, found_proc);
775 if (err)
776 return err;
777 }
778 }
779
780 /* Normal entries are done last, unless shadowed by an ignore entry */
781 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
782 int j, ignore;
783
784 ignore = 0;
785 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
786 j += 2) {
787 if ((address_data->ignore[j] == adap_id ||
788 address_data->ignore[j] == ANY_I2C_BUS)
789 && address_data->ignore[j + 1]
790 == address_data->normal_i2c[i]) {
791 dev_dbg(&adapter->dev, "found ignore "
792 "parameter for adapter %d, "
793 "addr 0x%02x\n", adap_id,
794 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +0200795 ignore = 1;
796 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200797 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200798 }
799 if (ignore)
800 continue;
801
802 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
803 "addr 0x%02x\n", adap_id,
804 address_data->normal_i2c[i]);
805 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
806 -1, found_proc);
807 if (err)
808 return err;
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return 0;
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814struct i2c_adapter* i2c_get_adapter(int id)
815{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +0100817
Arjan van de Venb3585e42006-01-11 10:50:26 +0100818 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400819 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
820 if (adapter && !try_module_get(adapter->owner))
821 adapter = NULL;
822
Arjan van de Venb3585e42006-01-11 10:50:26 +0100823 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400824 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827void i2c_put_adapter(struct i2c_adapter *adap)
828{
829 module_put(adap->owner);
830}
831
832/* The SMBus parts */
833
David Brownell438d6c22006-12-10 21:21:31 +0100834#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835static u8
836crc8(u16 data)
837{
838 int i;
David Brownell438d6c22006-12-10 21:21:31 +0100839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +0100841 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 data = data ^ POLY;
843 data = data << 1;
844 }
845 return (u8)(data >> 8);
846}
847
Jean Delvare421ef472005-10-26 21:28:55 +0200848/* Incremental CRC8 over count bytes in the array pointed to by p */
849static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 int i;
852
853 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200854 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return crc;
856}
857
Jean Delvare421ef472005-10-26 21:28:55 +0200858/* Assume a 7-bit address, which is reasonable for SMBus */
859static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Jean Delvare421ef472005-10-26 21:28:55 +0200861 /* The address will be sent first */
862 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
863 pec = i2c_smbus_pec(pec, &addr, 1);
864
865 /* The data buffer follows */
866 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Jean Delvare421ef472005-10-26 21:28:55 +0200869/* Used for write only transactions */
870static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Jean Delvare421ef472005-10-26 21:28:55 +0200872 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
873 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Jean Delvare421ef472005-10-26 21:28:55 +0200876/* Return <0 on CRC error
877 If there was a write before this read (most cases) we need to take the
878 partial CRC from the write part into account.
879 Note that this function does modify the message (we need to decrease the
880 message length to hide the CRC byte from the caller). */
881static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jean Delvare421ef472005-10-26 21:28:55 +0200883 u8 rpec = msg->buf[--msg->len];
884 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (rpec != cpec) {
887 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
888 rpec, cpec);
889 return -1;
890 }
David Brownell438d6c22006-12-10 21:21:31 +0100891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
895{
896 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +0100897 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900s32 i2c_smbus_read_byte(struct i2c_client *client)
901{
902 union i2c_smbus_data data;
903 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
904 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
905 return -1;
906 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200907 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
911{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200913 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
917{
918 union i2c_smbus_data data;
919 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
920 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
921 return -1;
922 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200923 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
927{
928 union i2c_smbus_data data;
929 data.byte = value;
930 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
931 I2C_SMBUS_WRITE,command,
932 I2C_SMBUS_BYTE_DATA,&data);
933}
934
935s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
936{
937 union i2c_smbus_data data;
938 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
939 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
940 return -1;
941 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200942 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
946{
947 union i2c_smbus_data data;
948 data.word = value;
949 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
950 I2C_SMBUS_WRITE,command,
951 I2C_SMBUS_WORD_DATA,&data);
952}
953
954s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200955 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (length > I2C_SMBUS_BLOCK_MAX)
960 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +0100962 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
964 I2C_SMBUS_WRITE,command,
965 I2C_SMBUS_BLOCK_DATA,&data);
966}
967
968/* Returns the number of read bytes */
969s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
970{
971 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
974 I2C_SMBUS_READ,command,
975 I2C_SMBUS_I2C_BLOCK_DATA,&data))
976 return -1;
Jean Delvare76560322006-01-18 23:14:55 +0100977
978 memcpy(values, &data.block[1], data.block[0]);
979 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Jean Delvare21bbd692006-01-09 15:19:18 +1100982s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200983 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +1100984{
985 union i2c_smbus_data data;
986
987 if (length > I2C_SMBUS_BLOCK_MAX)
988 length = I2C_SMBUS_BLOCK_MAX;
989 data.block[0] = length;
990 memcpy(data.block + 1, values, length);
991 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
992 I2C_SMBUS_WRITE, command,
993 I2C_SMBUS_I2C_BLOCK_DATA, &data);
994}
995
David Brownell438d6c22006-12-10 21:21:31 +0100996/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +0100998static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001000 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 union i2c_smbus_data * data)
1002{
1003 /* So we need to generate a series of msgs. In the case of writing, we
1004 need to use only one message; when reading, we need two. We initialize
1005 most things with sane defaults, to keep the code below somewhat
1006 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001007 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1008 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001010 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1012 };
1013 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001014 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 msgbuf0[0] = command;
1017 switch(size) {
1018 case I2C_SMBUS_QUICK:
1019 msg[0].len = 0;
1020 /* Special case: The read/write field is used as data */
1021 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1022 num = 1;
1023 break;
1024 case I2C_SMBUS_BYTE:
1025 if (read_write == I2C_SMBUS_READ) {
1026 /* Special case: only a read! */
1027 msg[0].flags = I2C_M_RD | flags;
1028 num = 1;
1029 }
1030 break;
1031 case I2C_SMBUS_BYTE_DATA:
1032 if (read_write == I2C_SMBUS_READ)
1033 msg[1].len = 1;
1034 else {
1035 msg[0].len = 2;
1036 msgbuf0[1] = data->byte;
1037 }
1038 break;
1039 case I2C_SMBUS_WORD_DATA:
1040 if (read_write == I2C_SMBUS_READ)
1041 msg[1].len = 2;
1042 else {
1043 msg[0].len=3;
1044 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001045 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047 break;
1048 case I2C_SMBUS_PROC_CALL:
1049 num = 2; /* Special case */
1050 read_write = I2C_SMBUS_READ;
1051 msg[0].len = 3;
1052 msg[1].len = 2;
1053 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001054 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (read_write == I2C_SMBUS_READ) {
1058 dev_err(&adapter->dev, "Block read not supported "
1059 "under I2C emulation!\n");
1060 return -1;
1061 } else {
1062 msg[0].len = data->block[0] + 2;
1063 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1064 dev_err(&adapter->dev, "smbus_access called with "
1065 "invalid block write size (%d)\n",
1066 data->block[0]);
1067 return -1;
1068 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001069 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 msgbuf0[i] = data->block[i-1];
1071 }
1072 break;
1073 case I2C_SMBUS_BLOCK_PROC_CALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 dev_dbg(&adapter->dev, "Block process call not supported "
1075 "under I2C emulation!\n");
1076 return -1;
1077 case I2C_SMBUS_I2C_BLOCK_DATA:
1078 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001079 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 } else {
1081 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001082 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1084 "invalid block write size (%d)\n",
1085 data->block[0]);
1086 return -1;
1087 }
1088 for (i = 1; i <= data->block[0]; i++)
1089 msgbuf0[i] = data->block[i];
1090 }
1091 break;
1092 default:
1093 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1094 size);
1095 return -1;
1096 }
1097
Jean Delvare421ef472005-10-26 21:28:55 +02001098 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1099 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1100 if (i) {
1101 /* Compute PEC if first message is a write */
1102 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001103 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001104 i2c_smbus_add_pec(&msg[0]);
1105 else /* Write followed by read */
1106 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1107 }
1108 /* Ask for PEC if last message is a read */
1109 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001110 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001111 }
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (i2c_transfer(adapter, msg, num) < 0)
1114 return -1;
1115
Jean Delvare421ef472005-10-26 21:28:55 +02001116 /* Check PEC if last message is a read */
1117 if (i && (msg[num-1].flags & I2C_M_RD)) {
1118 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1119 return -1;
1120 }
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (read_write == I2C_SMBUS_READ)
1123 switch(size) {
1124 case I2C_SMBUS_BYTE:
1125 data->byte = msgbuf0[0];
1126 break;
1127 case I2C_SMBUS_BYTE_DATA:
1128 data->byte = msgbuf1[0];
1129 break;
David Brownell438d6c22006-12-10 21:21:31 +01001130 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 case I2C_SMBUS_PROC_CALL:
1132 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1133 break;
1134 case I2C_SMBUS_I2C_BLOCK_DATA:
1135 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001136 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1137 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 data->block[i+1] = msgbuf1[i];
1139 break;
1140 }
1141 return 0;
1142}
1143
1144
1145s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001146 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 union i2c_smbus_data * data)
1148{
1149 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001154 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1156 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001157 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 } else
1159 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1160 command,size,data);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return res;
1163}
1164
1165
Jean Delvareb31366f2007-05-01 23:26:28 +02001166/* Next three are needed by i2c-isa */
Jean Delvareefde7232005-07-20 23:03:50 +02001167EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
Jean Delvareefde7232005-07-20 23:03:50 +02001168EXPORT_SYMBOL_GPL(i2c_adapter_class);
1169EXPORT_SYMBOL_GPL(i2c_bus_type);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171EXPORT_SYMBOL(i2c_add_adapter);
1172EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173EXPORT_SYMBOL(i2c_del_driver);
1174EXPORT_SYMBOL(i2c_attach_client);
1175EXPORT_SYMBOL(i2c_detach_client);
1176EXPORT_SYMBOL(i2c_use_client);
1177EXPORT_SYMBOL(i2c_release_client);
1178EXPORT_SYMBOL(i2c_clients_command);
1179EXPORT_SYMBOL(i2c_check_addr);
1180
1181EXPORT_SYMBOL(i2c_master_send);
1182EXPORT_SYMBOL(i2c_master_recv);
1183EXPORT_SYMBOL(i2c_control);
1184EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185EXPORT_SYMBOL(i2c_get_adapter);
1186EXPORT_SYMBOL(i2c_put_adapter);
1187EXPORT_SYMBOL(i2c_probe);
1188
1189EXPORT_SYMBOL(i2c_smbus_xfer);
1190EXPORT_SYMBOL(i2c_smbus_write_quick);
1191EXPORT_SYMBOL(i2c_smbus_read_byte);
1192EXPORT_SYMBOL(i2c_smbus_write_byte);
1193EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1194EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1195EXPORT_SYMBOL(i2c_smbus_read_word_data);
1196EXPORT_SYMBOL(i2c_smbus_write_word_data);
1197EXPORT_SYMBOL(i2c_smbus_write_block_data);
1198EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001199EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1202MODULE_DESCRIPTION("I2C-Bus main module");
1203MODULE_LICENSE("GPL");