blob: 97334433e534cd1f72354a742b152a5a0b1ed78c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36
37
38static LIST_HEAD(adapters);
39static LIST_HEAD(drivers);
Arjan van de Venb3585e42006-01-11 10:50:26 +010040static DEFINE_MUTEX(core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static DEFINE_IDR(i2c_adapter_idr);
42
43/* match always succeeds, as we want the probe() to tell if we really accept this match */
44static int i2c_device_match(struct device *dev, struct device_driver *drv)
45{
46 return 1;
47}
48
49static int i2c_bus_suspend(struct device * dev, pm_message_t state)
50{
51 int rc = 0;
52
53 if (dev->driver && dev->driver->suspend)
Russell King9480e302005-10-28 09:52:56 -070054 rc = dev->driver->suspend(dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return rc;
56}
57
58static int i2c_bus_resume(struct device * dev)
59{
60 int rc = 0;
61
62 if (dev->driver && dev->driver->resume)
Russell King9480e302005-10-28 09:52:56 -070063 rc = dev->driver->resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return rc;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int i2c_device_probe(struct device *dev)
68{
69 return -ENODEV;
70}
71
72static int i2c_device_remove(struct device *dev)
73{
74 return 0;
75}
76
Russell Kingb864c7d2006-01-05 14:37:50 +000077struct bus_type i2c_bus_type = {
78 .name = "i2c",
79 .match = i2c_device_match,
80 .probe = i2c_device_probe,
81 .remove = i2c_device_remove,
82 .suspend = i2c_bus_suspend,
83 .resume = i2c_bus_resume,
84};
85
Jean Delvareefde7232005-07-20 23:03:50 +020086void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
89 complete(&adap->dev_released);
90}
91
Jean Delvareefde7232005-07-20 23:03:50 +020092struct device_driver i2c_adapter_driver = {
Laurent Riffard6586bcd2005-10-17 22:54:45 +020093 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .name = "i2c_adapter",
95 .bus = &i2c_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98static void i2c_adapter_class_dev_release(struct class_device *dev)
99{
100 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
101 complete(&adap->class_dev_released);
102}
103
Jean Delvareefde7232005-07-20 23:03:50 +0200104struct class i2c_adapter_class = {
Laurent Riffard6586bcd2005-10-17 22:54:45 +0200105 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .name = "i2c-adapter",
107 .release = &i2c_adapter_class_dev_release,
108};
109
Yani Ioannoue404e272005-05-17 06:42:58 -0400110static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
113 return sprintf(buf, "%s\n", adap->name);
114}
115static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
116
117
118static void i2c_client_release(struct device *dev)
119{
120 struct i2c_client *client = to_i2c_client(dev);
121 complete(&client->released);
122}
123
Yani Ioannoue404e272005-05-17 06:42:58 -0400124static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct i2c_client *client = to_i2c_client(dev);
127 return sprintf(buf, "%s\n", client->name);
128}
129
130/*
131 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
132 * different type of a device. So beware if the DEVICE_ATTR() macro ever
133 * changes, this definition will also have to change.
134 */
135static struct device_attribute dev_attr_client_name = {
136 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
137 .show = &show_client_name,
138};
139
140
141/* ---------------------------------------------------
142 * registering functions
143 * ---------------------------------------------------
144 */
145
146/* -----
147 * i2c_add_adapter is called from within the algorithm layer,
148 * when a new hw adapter registers. A new device is register to be
149 * available for clients.
150 */
151int i2c_add_adapter(struct i2c_adapter *adap)
152{
153 int id, res = 0;
154 struct list_head *item;
155 struct i2c_driver *driver;
156
Arjan van de Venb3585e42006-01-11 10:50:26 +0100157 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
160 res = -ENOMEM;
161 goto out_unlock;
162 }
163
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400164 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (res < 0) {
166 if (res == -EAGAIN)
167 res = -ENOMEM;
168 goto out_unlock;
169 }
170
171 adap->nr = id & MAX_ID_MASK;
172 init_MUTEX(&adap->bus_lock);
173 init_MUTEX(&adap->clist_lock);
174 list_add_tail(&adap->list,&adapters);
175 INIT_LIST_HEAD(&adap->clients);
176
177 /* Add the adapter to the driver core.
178 * If the parent pointer is not set up,
179 * we add this adapter to the host bus.
180 */
181 if (adap->dev.parent == NULL)
182 adap->dev.parent = &platform_bus;
183 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
184 adap->dev.driver = &i2c_adapter_driver;
185 adap->dev.release = &i2c_adapter_dev_release;
186 device_register(&adap->dev);
187 device_create_file(&adap->dev, &dev_attr_name);
188
189 /* Add this adapter to the i2c_adapter class */
190 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
191 adap->class_dev.dev = &adap->dev;
192 adap->class_dev.class = &i2c_adapter_class;
193 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
194 class_device_register(&adap->class_dev);
195
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200196 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* inform drivers of new adapters */
199 list_for_each(item,&drivers) {
200 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100201 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* We ignore the return code; if it fails, too bad */
203 driver->attach_adapter(adap);
204 }
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100207 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return res;
209}
210
211
212int i2c_del_adapter(struct i2c_adapter *adap)
213{
214 struct list_head *item, *_n;
215 struct i2c_adapter *adap_from_list;
216 struct i2c_driver *driver;
217 struct i2c_client *client;
218 int res = 0;
219
Arjan van de Venb3585e42006-01-11 10:50:26 +0100220 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* First make sure that this adapter was ever added */
223 list_for_each_entry(adap_from_list, &adapters, list) {
224 if (adap_from_list == adap)
225 break;
226 }
227 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200228 pr_debug("i2c-core: attempting to delete unregistered "
229 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 res = -EINVAL;
231 goto out_unlock;
232 }
233
234 list_for_each(item,&drivers) {
235 driver = list_entry(item, struct i2c_driver, list);
236 if (driver->detach_adapter)
237 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200238 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100239 "for driver [%s]\n",
240 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out_unlock;
242 }
243 }
244
245 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200246 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 list_for_each_safe(item, _n, &adap->clients) {
248 client = list_entry(item, struct i2c_client, list);
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200251 dev_err(&adap->dev, "detach_client failed for client "
252 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 client->addr);
254 goto out_unlock;
255 }
256 }
257
258 /* clean up the sysfs representation */
259 init_completion(&adap->dev_released);
260 init_completion(&adap->class_dev_released);
261 class_device_unregister(&adap->class_dev);
262 device_remove_file(&adap->dev, &dev_attr_name);
263 device_unregister(&adap->dev);
264 list_del(&adap->list);
265
266 /* wait for sysfs to drop all references */
267 wait_for_completion(&adap->dev_released);
268 wait_for_completion(&adap->class_dev_released);
269
270 /* free dynamically allocated bus id */
271 idr_remove(&i2c_adapter_idr, adap->nr);
272
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200273 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100276 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return res;
278}
279
280
281/* -----
282 * What follows is the "upwards" interface: commands for talking to clients,
283 * which implement the functions to access the physical information of the
284 * chips.
285 */
286
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800287int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct list_head *item;
290 struct i2c_adapter *adapter;
291 int res = 0;
292
Arjan van de Venb3585e42006-01-11 10:50:26 +0100293 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800296 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 res = driver_register(&driver->driver);
300 if (res)
301 goto out_unlock;
302
303 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100304 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* now look for instances of driver on our adapters */
Jean Delvare8a994752005-11-26 20:28:06 +0100307 if (driver->attach_adapter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 list_for_each(item,&adapters) {
309 adapter = list_entry(item, struct i2c_adapter, list);
310 driver->attach_adapter(adapter);
311 }
312 }
313
314 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100315 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return res;
317}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800318EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320int i2c_del_driver(struct i2c_driver *driver)
321{
322 struct list_head *item1, *item2, *_n;
323 struct i2c_client *client;
324 struct i2c_adapter *adap;
325
326 int res = 0;
327
Arjan van de Venb3585e42006-01-11 10:50:26 +0100328 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Have a look at each adapter, if clients of this driver are still
331 * attached. If so, detach them to be able to kill the driver
332 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334 list_for_each(item1,&adapters) {
335 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (driver->detach_adapter) {
337 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200338 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100339 "for driver [%s]\n",
340 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto out_unlock;
342 }
343 } else {
344 list_for_each_safe(item2, _n, &adap->clients) {
345 client = list_entry(item2, struct i2c_client, list);
346 if (client->driver != driver)
347 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200348 dev_dbg(&adap->dev, "detaching client [%s] "
349 "at 0x%02x\n", client->name,
350 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200352 dev_err(&adap->dev, "detach_client "
353 "failed for client [%s] at "
354 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 client->addr);
356 goto out_unlock;
357 }
358 }
359 }
360 }
361
362 driver_unregister(&driver->driver);
363 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100364 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100367 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
371static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
372{
373 struct list_head *item;
374 struct i2c_client *client;
375
376 list_for_each(item,&adapter->clients) {
377 client = list_entry(item, struct i2c_client, list);
378 if (client->addr == addr)
379 return -EBUSY;
380 }
381 return 0;
382}
383
384int i2c_check_addr(struct i2c_adapter *adapter, int addr)
385{
386 int rval;
387
388 down(&adapter->clist_lock);
389 rval = __i2c_check_addr(adapter, addr);
390 up(&adapter->clist_lock);
391
392 return rval;
393}
394
395int i2c_attach_client(struct i2c_client *client)
396{
397 struct i2c_adapter *adapter = client->adapter;
398
399 down(&adapter->clist_lock);
400 if (__i2c_check_addr(client->adapter, client->addr)) {
401 up(&adapter->clist_lock);
402 return -EBUSY;
403 }
404 list_add_tail(&client->list,&adapter->clients);
405 up(&adapter->clist_lock);
406
407 if (adapter->client_register) {
408 if (adapter->client_register(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200409 dev_dbg(&adapter->dev, "client_register "
410 "failed for client [%s] at 0x%02x\n",
411 client->name, client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
414
Jean Delvarecde78592005-11-26 21:00:54 +0100415 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 client->dev.parent = &client->adapter->dev;
418 client->dev.driver = &client->driver->driver;
419 client->dev.bus = &i2c_bus_type;
420 client->dev.release = &i2c_client_release;
421
422 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
423 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200424 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
425 client->name, client->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 device_register(&client->dev);
427 device_create_file(&client->dev, &dev_attr_client_name);
428
429 return 0;
430}
431
432
433int i2c_detach_client(struct i2c_client *client)
434{
435 struct i2c_adapter *adapter = client->adapter;
436 int res = 0;
437
Jean Delvarecde78592005-11-26 21:00:54 +0100438 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200439 dev_warn(&client->dev, "Client [%s] still busy, "
440 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (adapter->client_unregister) {
445 res = adapter->client_unregister(client);
446 if (res) {
447 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700448 "client_unregister [%s] failed, "
449 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto out;
451 }
452 }
453
454 down(&adapter->clist_lock);
455 list_del(&client->list);
456 init_completion(&client->released);
457 device_remove_file(&client->dev, &dev_attr_client_name);
458 device_unregister(&client->dev);
459 up(&adapter->clist_lock);
460 wait_for_completion(&client->released);
461
462 out:
463 return res;
464}
465
466static int i2c_inc_use_client(struct i2c_client *client)
467{
468
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100469 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENODEV;
471 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100472 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return -ENODEV;
474 }
475
476 return 0;
477}
478
479static void i2c_dec_use_client(struct i2c_client *client)
480{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100481 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 module_put(client->adapter->owner);
483}
484
485int i2c_use_client(struct i2c_client *client)
486{
487 int ret;
488
489 ret = i2c_inc_use_client(client);
490 if (ret)
491 return ret;
492
Jean Delvarecde78592005-11-26 21:00:54 +0100493 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498int i2c_release_client(struct i2c_client *client)
499{
Jean Delvarecde78592005-11-26 21:00:54 +0100500 if (!client->usage_count) {
501 pr_debug("i2c-core: %s used one too many times\n",
502 __FUNCTION__);
503 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
Jean Delvarecde78592005-11-26 21:00:54 +0100506 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 i2c_dec_use_client(client);
508
509 return 0;
510}
511
512void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
513{
514 struct list_head *item;
515 struct i2c_client *client;
516
517 down(&adap->clist_lock);
518 list_for_each(item,&adap->clients) {
519 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100520 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 continue;
522 if (NULL != client->driver->command) {
523 up(&adap->clist_lock);
524 client->driver->command(client,cmd,arg);
525 down(&adap->clist_lock);
526 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100527 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 up(&adap->clist_lock);
530}
531
532static int __init i2c_init(void)
533{
534 int retval;
535
536 retval = bus_register(&i2c_bus_type);
537 if (retval)
538 return retval;
539 retval = driver_register(&i2c_adapter_driver);
540 if (retval)
541 return retval;
542 return class_register(&i2c_adapter_class);
543}
544
545static void __exit i2c_exit(void)
546{
547 class_unregister(&i2c_adapter_class);
548 driver_unregister(&i2c_adapter_driver);
549 bus_unregister(&i2c_bus_type);
550}
551
552subsys_initcall(i2c_init);
553module_exit(i2c_exit);
554
555/* ----------------------------------------------------
556 * the functional interface to the i2c busses.
557 * ----------------------------------------------------
558 */
559
560int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
561{
562 int ret;
563
564 if (adap->algo->master_xfer) {
565#ifdef DEBUG
566 for (ret = 0; ret < num; ret++) {
567 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
568 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
569 'R' : 'W', msgs[ret].addr, msgs[ret].len);
570 }
571#endif
572
573 down(&adap->bus_lock);
574 ret = adap->algo->master_xfer(adap,msgs,num);
575 up(&adap->bus_lock);
576
577 return ret;
578 } else {
579 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
580 return -ENOSYS;
581 }
582}
583
584int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
585{
586 int ret;
587 struct i2c_adapter *adap=client->adapter;
588 struct i2c_msg msg;
589
Jean Delvare815f55f2005-05-07 22:58:46 +0200590 msg.addr = client->addr;
591 msg.flags = client->flags & I2C_M_TEN;
592 msg.len = count;
593 msg.buf = (char *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Jean Delvare815f55f2005-05-07 22:58:46 +0200595 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Jean Delvare815f55f2005-05-07 22:58:46 +0200597 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
598 transmitted, else error code. */
599 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
603{
604 struct i2c_adapter *adap=client->adapter;
605 struct i2c_msg msg;
606 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Jean Delvare815f55f2005-05-07 22:58:46 +0200608 msg.addr = client->addr;
609 msg.flags = client->flags & I2C_M_TEN;
610 msg.flags |= I2C_M_RD;
611 msg.len = count;
612 msg.buf = buf;
613
614 ret = i2c_transfer(adap, &msg, 1);
615
616 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
617 transmitted, else error code. */
618 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621
622int i2c_control(struct i2c_client *client,
623 unsigned int cmd, unsigned long arg)
624{
625 int ret = 0;
626 struct i2c_adapter *adap = client->adapter;
627
628 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
629 switch (cmd) {
630 case I2C_RETRIES:
631 adap->retries = arg;
632 break;
633 case I2C_TIMEOUT:
634 adap->timeout = arg;
635 break;
636 default:
637 if (adap->algo->algo_control!=NULL)
638 ret = adap->algo->algo_control(adap,cmd,arg);
639 }
640 return ret;
641}
642
643/* ----------------------------------------------------
644 * the i2c address scanning function
645 * Will not work for 10-bit addresses!
646 * ----------------------------------------------------
647 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200648static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
649 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200650{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200651 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200652
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200653 /* Make sure the address is valid */
654 if (addr < 0x03 || addr > 0x77) {
655 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
656 addr);
657 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200658 }
659
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200660 /* Skip if already in use */
661 if (i2c_check_addr(adapter, addr))
662 return 0;
663
664 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200665 if (kind < 0) {
666 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
667 I2C_SMBUS_QUICK, NULL) < 0)
668 return 0;
669
670 /* prevent 24RF08 corruption */
671 if ((addr & ~0x0f) == 0x50)
672 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
673 I2C_SMBUS_QUICK, NULL);
674 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200675
676 /* Finally call the custom detection function */
677 err = found_proc(adapter, addr, kind);
678
679 /* -ENODEV can be returned if there is a chip at the given address
680 but it isn't supported by this chip driver. We catch it here as
681 this isn't an error. */
682 return (err == -ENODEV) ? 0 : err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685int i2c_probe(struct i2c_adapter *adapter,
686 struct i2c_client_address_data *address_data,
687 int (*found_proc) (struct i2c_adapter *, int, int))
688{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200689 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int adap_id = i2c_adapter_id(adapter);
691
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200692 /* Force entries are done first, and are not affected by ignore
693 entries */
694 if (address_data->forces) {
695 unsigned short **forces = address_data->forces;
696 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200698 for (kind = 0; forces[kind]; kind++) {
699 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
700 i += 2) {
701 if (forces[kind][i] == adap_id
702 || forces[kind][i] == ANY_I2C_BUS) {
703 dev_dbg(&adapter->dev, "found force "
704 "parameter for adapter %d, "
705 "addr 0x%02x, kind %d\n",
706 adap_id, forces[kind][i + 1],
707 kind);
708 err = i2c_probe_address(adapter,
709 forces[kind][i + 1],
710 kind, found_proc);
711 if (err)
712 return err;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200717
Jean Delvare4366dc92005-09-25 16:50:06 +0200718 /* Stop here if we can't use SMBUS_QUICK */
719 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
720 if (address_data->probe[0] == I2C_CLIENT_END
721 && address_data->normal_i2c[0] == I2C_CLIENT_END)
722 return 0;
723
724 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
725 "can't probe for chips\n");
726 return -1;
727 }
728
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200729 /* Probe entries are done second, and are not affected by ignore
730 entries either */
731 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
732 if (address_data->probe[i] == adap_id
733 || address_data->probe[i] == ANY_I2C_BUS) {
734 dev_dbg(&adapter->dev, "found probe parameter for "
735 "adapter %d, addr 0x%02x\n", adap_id,
736 address_data->probe[i + 1]);
737 err = i2c_probe_address(adapter,
738 address_data->probe[i + 1],
739 -1, found_proc);
740 if (err)
741 return err;
742 }
743 }
744
745 /* Normal entries are done last, unless shadowed by an ignore entry */
746 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
747 int j, ignore;
748
749 ignore = 0;
750 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
751 j += 2) {
752 if ((address_data->ignore[j] == adap_id ||
753 address_data->ignore[j] == ANY_I2C_BUS)
754 && address_data->ignore[j + 1]
755 == address_data->normal_i2c[i]) {
756 dev_dbg(&adapter->dev, "found ignore "
757 "parameter for adapter %d, "
758 "addr 0x%02x\n", adap_id,
759 address_data->ignore[j + 1]);
760 }
761 ignore = 1;
762 break;
763 }
764 if (ignore)
765 continue;
766
767 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
768 "addr 0x%02x\n", adap_id,
769 address_data->normal_i2c[i]);
770 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
771 -1, found_proc);
772 if (err)
773 return err;
774 }
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779struct i2c_adapter* i2c_get_adapter(int id)
780{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct i2c_adapter *adapter;
782
Arjan van de Venb3585e42006-01-11 10:50:26 +0100783 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400784 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
785 if (adapter && !try_module_get(adapter->owner))
786 adapter = NULL;
787
Arjan van de Venb3585e42006-01-11 10:50:26 +0100788 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400789 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792void i2c_put_adapter(struct i2c_adapter *adap)
793{
794 module_put(adap->owner);
795}
796
797/* The SMBus parts */
798
799#define POLY (0x1070U << 3)
800static u8
801crc8(u16 data)
802{
803 int i;
804
805 for(i = 0; i < 8; i++) {
806 if (data & 0x8000)
807 data = data ^ POLY;
808 data = data << 1;
809 }
810 return (u8)(data >> 8);
811}
812
Jean Delvare421ef472005-10-26 21:28:55 +0200813/* Incremental CRC8 over count bytes in the array pointed to by p */
814static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 int i;
817
818 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200819 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return crc;
821}
822
Jean Delvare421ef472005-10-26 21:28:55 +0200823/* Assume a 7-bit address, which is reasonable for SMBus */
824static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jean Delvare421ef472005-10-26 21:28:55 +0200826 /* The address will be sent first */
827 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
828 pec = i2c_smbus_pec(pec, &addr, 1);
829
830 /* The data buffer follows */
831 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Jean Delvare421ef472005-10-26 21:28:55 +0200834/* Used for write only transactions */
835static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jean Delvare421ef472005-10-26 21:28:55 +0200837 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
838 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Jean Delvare421ef472005-10-26 21:28:55 +0200841/* Return <0 on CRC error
842 If there was a write before this read (most cases) we need to take the
843 partial CRC from the write part into account.
844 Note that this function does modify the message (we need to decrease the
845 message length to hide the CRC byte from the caller). */
846static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Jean Delvare421ef472005-10-26 21:28:55 +0200848 u8 rpec = msg->buf[--msg->len];
849 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (rpec != cpec) {
852 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
853 rpec, cpec);
854 return -1;
855 }
856 return 0;
857}
858
859s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
860{
861 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
862 value,0,I2C_SMBUS_QUICK,NULL);
863}
864
865s32 i2c_smbus_read_byte(struct i2c_client *client)
866{
867 union i2c_smbus_data data;
868 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
869 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
870 return -1;
871 else
872 return 0x0FF & data.byte;
873}
874
875s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
876{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200878 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
882{
883 union i2c_smbus_data data;
884 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
885 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
886 return -1;
887 else
888 return 0x0FF & data.byte;
889}
890
891s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
892{
893 union i2c_smbus_data data;
894 data.byte = value;
895 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
896 I2C_SMBUS_WRITE,command,
897 I2C_SMBUS_BYTE_DATA,&data);
898}
899
900s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
901{
902 union i2c_smbus_data data;
903 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
904 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
905 return -1;
906 else
907 return 0x0FFFF & data.word;
908}
909
910s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
911{
912 union i2c_smbus_data data;
913 data.word = value;
914 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
915 I2C_SMBUS_WRITE,command,
916 I2C_SMBUS_WORD_DATA,&data);
917}
918
919s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
920 u8 length, u8 *values)
921{
922 union i2c_smbus_data data;
923 int i;
924 if (length > I2C_SMBUS_BLOCK_MAX)
925 length = I2C_SMBUS_BLOCK_MAX;
926 for (i = 1; i <= length; i++)
927 data.block[i] = values[i-1];
928 data.block[0] = length;
929 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
930 I2C_SMBUS_WRITE,command,
931 I2C_SMBUS_BLOCK_DATA,&data);
932}
933
934/* Returns the number of read bytes */
935s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
936{
937 union i2c_smbus_data data;
938 int i;
939 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
940 I2C_SMBUS_READ,command,
941 I2C_SMBUS_I2C_BLOCK_DATA,&data))
942 return -1;
943 else {
944 for (i = 1; i <= data.block[0]; i++)
945 values[i-1] = data.block[i];
946 return data.block[0];
947 }
948}
949
Jean Delvare21bbd692006-01-09 15:19:18 +1100950s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
951 u8 length, u8 *values)
952{
953 union i2c_smbus_data data;
954
955 if (length > I2C_SMBUS_BLOCK_MAX)
956 length = I2C_SMBUS_BLOCK_MAX;
957 data.block[0] = length;
958 memcpy(data.block + 1, values, length);
959 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
960 I2C_SMBUS_WRITE, command,
961 I2C_SMBUS_I2C_BLOCK_DATA, &data);
962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/* Simulate a SMBus command using the i2c protocol
965 No checking of parameters is done! */
966static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
967 unsigned short flags,
968 char read_write, u8 command, int size,
969 union i2c_smbus_data * data)
970{
971 /* So we need to generate a series of msgs. In the case of writing, we
972 need to use only one message; when reading, we need two. We initialize
973 most things with sane defaults, to keep the code below somewhat
974 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +0200975 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
976 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int num = read_write == I2C_SMBUS_READ?2:1;
978 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
979 { addr, flags | I2C_M_RD, 0, msgbuf1 }
980 };
981 int i;
Jean Delvare421ef472005-10-26 21:28:55 +0200982 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 msgbuf0[0] = command;
985 switch(size) {
986 case I2C_SMBUS_QUICK:
987 msg[0].len = 0;
988 /* Special case: The read/write field is used as data */
989 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
990 num = 1;
991 break;
992 case I2C_SMBUS_BYTE:
993 if (read_write == I2C_SMBUS_READ) {
994 /* Special case: only a read! */
995 msg[0].flags = I2C_M_RD | flags;
996 num = 1;
997 }
998 break;
999 case I2C_SMBUS_BYTE_DATA:
1000 if (read_write == I2C_SMBUS_READ)
1001 msg[1].len = 1;
1002 else {
1003 msg[0].len = 2;
1004 msgbuf0[1] = data->byte;
1005 }
1006 break;
1007 case I2C_SMBUS_WORD_DATA:
1008 if (read_write == I2C_SMBUS_READ)
1009 msg[1].len = 2;
1010 else {
1011 msg[0].len=3;
1012 msgbuf0[1] = data->word & 0xff;
1013 msgbuf0[2] = (data->word >> 8) & 0xff;
1014 }
1015 break;
1016 case I2C_SMBUS_PROC_CALL:
1017 num = 2; /* Special case */
1018 read_write = I2C_SMBUS_READ;
1019 msg[0].len = 3;
1020 msg[1].len = 2;
1021 msgbuf0[1] = data->word & 0xff;
1022 msgbuf0[2] = (data->word >> 8) & 0xff;
1023 break;
1024 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (read_write == I2C_SMBUS_READ) {
1026 dev_err(&adapter->dev, "Block read not supported "
1027 "under I2C emulation!\n");
1028 return -1;
1029 } else {
1030 msg[0].len = data->block[0] + 2;
1031 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1032 dev_err(&adapter->dev, "smbus_access called with "
1033 "invalid block write size (%d)\n",
1034 data->block[0]);
1035 return -1;
1036 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001037 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 msgbuf0[i] = data->block[i-1];
1039 }
1040 break;
1041 case I2C_SMBUS_BLOCK_PROC_CALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 dev_dbg(&adapter->dev, "Block process call not supported "
1043 "under I2C emulation!\n");
1044 return -1;
1045 case I2C_SMBUS_I2C_BLOCK_DATA:
1046 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001047 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 } else {
1049 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001050 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1052 "invalid block write size (%d)\n",
1053 data->block[0]);
1054 return -1;
1055 }
1056 for (i = 1; i <= data->block[0]; i++)
1057 msgbuf0[i] = data->block[i];
1058 }
1059 break;
1060 default:
1061 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1062 size);
1063 return -1;
1064 }
1065
Jean Delvare421ef472005-10-26 21:28:55 +02001066 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1067 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1068 if (i) {
1069 /* Compute PEC if first message is a write */
1070 if (!(msg[0].flags & I2C_M_RD)) {
1071 if (num == 1) /* Write only */
1072 i2c_smbus_add_pec(&msg[0]);
1073 else /* Write followed by read */
1074 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1075 }
1076 /* Ask for PEC if last message is a read */
1077 if (msg[num-1].flags & I2C_M_RD)
1078 msg[num-1].len++;
1079 }
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (i2c_transfer(adapter, msg, num) < 0)
1082 return -1;
1083
Jean Delvare421ef472005-10-26 21:28:55 +02001084 /* Check PEC if last message is a read */
1085 if (i && (msg[num-1].flags & I2C_M_RD)) {
1086 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1087 return -1;
1088 }
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (read_write == I2C_SMBUS_READ)
1091 switch(size) {
1092 case I2C_SMBUS_BYTE:
1093 data->byte = msgbuf0[0];
1094 break;
1095 case I2C_SMBUS_BYTE_DATA:
1096 data->byte = msgbuf1[0];
1097 break;
1098 case I2C_SMBUS_WORD_DATA:
1099 case I2C_SMBUS_PROC_CALL:
1100 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1101 break;
1102 case I2C_SMBUS_I2C_BLOCK_DATA:
1103 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001104 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1105 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 data->block[i+1] = msgbuf1[i];
1107 break;
1108 }
1109 return 0;
1110}
1111
1112
1113s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1114 char read_write, u8 command, int size,
1115 union i2c_smbus_data * data)
1116{
1117 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (adapter->algo->smbus_xfer) {
1122 down(&adapter->bus_lock);
1123 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1124 command,size,data);
1125 up(&adapter->bus_lock);
1126 } else
1127 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1128 command,size,data);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return res;
1131}
1132
1133
Jean Delvareefde7232005-07-20 23:03:50 +02001134/* Next four are needed by i2c-isa */
1135EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1136EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1137EXPORT_SYMBOL_GPL(i2c_adapter_class);
1138EXPORT_SYMBOL_GPL(i2c_bus_type);
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140EXPORT_SYMBOL(i2c_add_adapter);
1141EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142EXPORT_SYMBOL(i2c_del_driver);
1143EXPORT_SYMBOL(i2c_attach_client);
1144EXPORT_SYMBOL(i2c_detach_client);
1145EXPORT_SYMBOL(i2c_use_client);
1146EXPORT_SYMBOL(i2c_release_client);
1147EXPORT_SYMBOL(i2c_clients_command);
1148EXPORT_SYMBOL(i2c_check_addr);
1149
1150EXPORT_SYMBOL(i2c_master_send);
1151EXPORT_SYMBOL(i2c_master_recv);
1152EXPORT_SYMBOL(i2c_control);
1153EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154EXPORT_SYMBOL(i2c_get_adapter);
1155EXPORT_SYMBOL(i2c_put_adapter);
1156EXPORT_SYMBOL(i2c_probe);
1157
1158EXPORT_SYMBOL(i2c_smbus_xfer);
1159EXPORT_SYMBOL(i2c_smbus_write_quick);
1160EXPORT_SYMBOL(i2c_smbus_read_byte);
1161EXPORT_SYMBOL(i2c_smbus_write_byte);
1162EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1163EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1164EXPORT_SYMBOL(i2c_smbus_read_word_data);
1165EXPORT_SYMBOL(i2c_smbus_write_word_data);
1166EXPORT_SYMBOL(i2c_smbus_write_block_data);
1167EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001168EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1171MODULE_DESCRIPTION("I2C-Bus main module");
1172MODULE_LICENSE("GPL");