blob: b05378a3d673fb67de094c3c0e4c33e9c7836d87 [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
David Brownellb119dc32007-01-04 13:07:04 +010098/* ------------------------------------------------------------------------- */
99
100/* I2C bus adapters -- one roots each I2C or SMBUS segment */
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static void i2c_adapter_class_dev_release(struct class_device *dev)
103{
104 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
105 complete(&adap->class_dev_released);
106}
107
David Brownellb119dc32007-01-04 13:07:04 +0100108static ssize_t i2c_adapter_show_name(struct class_device *cdev, char *buf)
109{
110 struct i2c_adapter *adap = class_dev_to_i2c_adapter(cdev);
111 return sprintf(buf, "%s\n", adap->name);
112}
113
114static struct class_device_attribute i2c_adapter_attrs[] = {
115 __ATTR(name, S_IRUGO, i2c_adapter_show_name, NULL),
116 { },
117};
118
Jean Delvareefde7232005-07-20 23:03:50 +0200119struct class i2c_adapter_class = {
David Brownellb119dc32007-01-04 13:07:04 +0100120 .owner = THIS_MODULE,
121 .name = "i2c-adapter",
122 .class_dev_attrs = i2c_adapter_attrs,
123 .release = &i2c_adapter_class_dev_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Yani Ioannoue404e272005-05-17 06:42:58 -0400126static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
129 return sprintf(buf, "%s\n", adap->name);
130}
131static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
132
133
134static void i2c_client_release(struct device *dev)
135{
136 struct i2c_client *client = to_i2c_client(dev);
137 complete(&client->released);
138}
139
Yani Ioannoue404e272005-05-17 06:42:58 -0400140static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct i2c_client *client = to_i2c_client(dev);
143 return sprintf(buf, "%s\n", client->name);
144}
145
David Brownell438d6c22006-12-10 21:21:31 +0100146/*
Jean Delvare7b77d062006-12-10 21:21:31 +0100147 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
148 * an i2c adapter attribute (above).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Jean Delvare7b77d062006-12-10 21:21:31 +0100150static struct device_attribute dev_attr_client_name =
151 __ATTR(name, S_IRUGO, &show_client_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153
154/* ---------------------------------------------------
David Brownell438d6c22006-12-10 21:21:31 +0100155 * registering functions
156 * ---------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
158
159/* -----
160 * i2c_add_adapter is called from within the algorithm layer,
161 * when a new hw adapter registers. A new device is register to be
162 * available for clients.
163 */
164int i2c_add_adapter(struct i2c_adapter *adap)
165{
166 int id, res = 0;
167 struct list_head *item;
168 struct i2c_driver *driver;
169
Arjan van de Venb3585e42006-01-11 10:50:26 +0100170 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
173 res = -ENOMEM;
174 goto out_unlock;
175 }
176
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400177 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (res < 0) {
179 if (res == -EAGAIN)
180 res = -ENOMEM;
181 goto out_unlock;
182 }
183
184 adap->nr = id & MAX_ID_MASK;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100185 mutex_init(&adap->bus_lock);
186 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 list_add_tail(&adap->list,&adapters);
188 INIT_LIST_HEAD(&adap->clients);
189
190 /* Add the adapter to the driver core.
191 * If the parent pointer is not set up,
192 * we add this adapter to the host bus.
193 */
David Brownellb119dc32007-01-04 13:07:04 +0100194 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 adap->dev.parent = &platform_bus;
David Brownellb119dc32007-01-04 13:07:04 +0100196 printk(KERN_WARNING "**WARNING** I2C adapter driver [%s] "
197 "forgot to specify physical device; fix it!\n",
198 adap->name);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
201 adap->dev.driver = &i2c_adapter_driver;
202 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200203 res = device_register(&adap->dev);
204 if (res)
205 goto out_list;
206 res = device_create_file(&adap->dev, &dev_attr_name);
207 if (res)
208 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* Add this adapter to the i2c_adapter class */
211 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
212 adap->class_dev.dev = &adap->dev;
213 adap->class_dev.class = &i2c_adapter_class;
214 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200215 res = class_device_register(&adap->class_dev);
216 if (res)
217 goto out_remove_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200219 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* inform drivers of new adapters */
222 list_for_each(item,&drivers) {
223 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100224 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* We ignore the return code; if it fails, too bad */
226 driver->attach_adapter(adap);
227 }
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100230 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200232
233out_remove_name:
234 device_remove_file(&adap->dev, &dev_attr_name);
235out_unregister:
236 init_completion(&adap->dev_released); /* Needed? */
237 device_unregister(&adap->dev);
238 wait_for_completion(&adap->dev_released);
239out_list:
240 list_del(&adap->list);
241 idr_remove(&i2c_adapter_idr, adap->nr);
242 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245
246int i2c_del_adapter(struct i2c_adapter *adap)
247{
248 struct list_head *item, *_n;
249 struct i2c_adapter *adap_from_list;
250 struct i2c_driver *driver;
251 struct i2c_client *client;
252 int res = 0;
253
Arjan van de Venb3585e42006-01-11 10:50:26 +0100254 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /* First make sure that this adapter was ever added */
257 list_for_each_entry(adap_from_list, &adapters, list) {
258 if (adap_from_list == adap)
259 break;
260 }
261 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200262 pr_debug("i2c-core: attempting to delete unregistered "
263 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 res = -EINVAL;
265 goto out_unlock;
266 }
267
268 list_for_each(item,&drivers) {
269 driver = list_entry(item, struct i2c_driver, list);
270 if (driver->detach_adapter)
271 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200272 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100273 "for driver [%s]\n",
274 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto out_unlock;
276 }
277 }
278
279 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200280 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 list_for_each_safe(item, _n, &adap->clients) {
282 client = list_entry(item, struct i2c_client, list);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200285 dev_err(&adap->dev, "detach_client failed for client "
286 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 client->addr);
288 goto out_unlock;
289 }
290 }
291
292 /* clean up the sysfs representation */
293 init_completion(&adap->dev_released);
294 init_completion(&adap->class_dev_released);
295 class_device_unregister(&adap->class_dev);
296 device_remove_file(&adap->dev, &dev_attr_name);
297 device_unregister(&adap->dev);
298 list_del(&adap->list);
299
300 /* wait for sysfs to drop all references */
301 wait_for_completion(&adap->dev_released);
302 wait_for_completion(&adap->class_dev_released);
303
304 /* free dynamically allocated bus id */
305 idr_remove(&i2c_adapter_idr, adap->nr);
306
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200307 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100310 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return res;
312}
313
314
315/* -----
316 * What follows is the "upwards" interface: commands for talking to clients,
317 * which implement the functions to access the physical information of the
318 * chips.
319 */
320
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800321int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct list_head *item;
324 struct i2c_adapter *adapter;
Jean Delvare7eebcb72006-02-05 23:28:21 +0100325 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800328 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 res = driver_register(&driver->driver);
332 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100333 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100334
Jean Delvare7eebcb72006-02-05 23:28:21 +0100335 mutex_lock(&core_lists);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100338 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* now look for instances of driver on our adapters */
Jean Delvare8a994752005-11-26 20:28:06 +0100341 if (driver->attach_adapter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 list_for_each(item,&adapters) {
343 adapter = list_entry(item, struct i2c_adapter, list);
344 driver->attach_adapter(adapter);
345 }
346 }
347
Arjan van de Venb3585e42006-01-11 10:50:26 +0100348 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800351EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353int i2c_del_driver(struct i2c_driver *driver)
354{
355 struct list_head *item1, *item2, *_n;
356 struct i2c_client *client;
357 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int res = 0;
360
Arjan van de Venb3585e42006-01-11 10:50:26 +0100361 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100364 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
367 list_for_each(item1,&adapters) {
368 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (driver->detach_adapter) {
370 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200371 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100372 "for driver [%s]\n",
373 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto out_unlock;
375 }
376 } else {
377 list_for_each_safe(item2, _n, &adap->clients) {
378 client = list_entry(item2, struct i2c_client, list);
379 if (client->driver != driver)
380 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200381 dev_dbg(&adap->dev, "detaching client [%s] "
382 "at 0x%02x\n", client->name,
383 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200385 dev_err(&adap->dev, "detach_client "
386 "failed for client [%s] at "
387 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 client->addr);
389 goto out_unlock;
390 }
391 }
392 }
393 }
394
395 driver_unregister(&driver->driver);
396 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100397 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100400 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
402}
403
404static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
405{
406 struct list_head *item;
407 struct i2c_client *client;
408
409 list_for_each(item,&adapter->clients) {
410 client = list_entry(item, struct i2c_client, list);
411 if (client->addr == addr)
412 return -EBUSY;
413 }
414 return 0;
415}
416
417int i2c_check_addr(struct i2c_adapter *adapter, int addr)
418{
419 int rval;
420
Ingo Molnar5c085d32006-01-18 23:16:04 +0100421 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100423 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 return rval;
426}
427
428int i2c_attach_client(struct i2c_client *client)
429{
430 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200431 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Ingo Molnar5c085d32006-01-18 23:16:04 +0100433 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200435 res = -EBUSY;
436 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100439
Jean Delvarecde78592005-11-26 21:00:54 +0100440 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 client->dev.parent = &client->adapter->dev;
443 client->dev.driver = &client->driver->driver;
444 client->dev.bus = &i2c_bus_type;
445 client->dev.release = &i2c_client_release;
David Brownell438d6c22006-12-10 21:21:31 +0100446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
448 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200449 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
450 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200451 res = device_register(&client->dev);
452 if (res)
453 goto out_list;
454 res = device_create_file(&client->dev, &dev_attr_client_name);
455 if (res)
456 goto out_unregister;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200457 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200458
459 if (adapter->client_register) {
460 if (adapter->client_register(client)) {
461 dev_dbg(&adapter->dev, "client_register "
462 "failed for client [%s] at 0x%02x\n",
463 client->name, client->addr);
464 }
465 }
466
467 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200468
469out_unregister:
470 init_completion(&client->released); /* Needed? */
471 device_unregister(&client->dev);
472 wait_for_completion(&client->released);
473out_list:
474 list_del(&client->list);
475 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
476 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200477out_unlock:
478 mutex_unlock(&adapter->clist_lock);
479 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482
483int i2c_detach_client(struct i2c_client *client)
484{
485 struct i2c_adapter *adapter = client->adapter;
486 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100487
Jean Delvarecde78592005-11-26 21:00:54 +0100488 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200489 dev_warn(&client->dev, "Client [%s] still busy, "
490 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (adapter->client_unregister) {
495 res = adapter->client_unregister(client);
496 if (res) {
497 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700498 "client_unregister [%s] failed, "
499 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto out;
501 }
502 }
503
Ingo Molnar5c085d32006-01-18 23:16:04 +0100504 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 list_del(&client->list);
506 init_completion(&client->released);
507 device_remove_file(&client->dev, &dev_attr_client_name);
508 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100509 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 wait_for_completion(&client->released);
511
512 out:
513 return res;
514}
515
516static int i2c_inc_use_client(struct i2c_client *client)
517{
518
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100519 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -ENODEV;
521 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100522 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -ENODEV;
524 }
525
526 return 0;
527}
528
529static void i2c_dec_use_client(struct i2c_client *client)
530{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100531 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 module_put(client->adapter->owner);
533}
534
535int i2c_use_client(struct i2c_client *client)
536{
537 int ret;
538
539 ret = i2c_inc_use_client(client);
540 if (ret)
541 return ret;
542
Jean Delvarecde78592005-11-26 21:00:54 +0100543 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548int i2c_release_client(struct i2c_client *client)
549{
Jean Delvarecde78592005-11-26 21:00:54 +0100550 if (!client->usage_count) {
551 pr_debug("i2c-core: %s used one too many times\n",
552 __FUNCTION__);
553 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
David Brownell438d6c22006-12-10 21:21:31 +0100555
Jean Delvarecde78592005-11-26 21:00:54 +0100556 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
560}
561
562void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
563{
564 struct list_head *item;
565 struct i2c_client *client;
566
Ingo Molnar5c085d32006-01-18 23:16:04 +0100567 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 list_for_each(item,&adap->clients) {
569 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100570 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 continue;
572 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100573 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100575 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100577 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100579 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582static int __init i2c_init(void)
583{
584 int retval;
585
586 retval = bus_register(&i2c_bus_type);
587 if (retval)
588 return retval;
589 retval = driver_register(&i2c_adapter_driver);
590 if (retval)
591 return retval;
592 return class_register(&i2c_adapter_class);
593}
594
595static void __exit i2c_exit(void)
596{
597 class_unregister(&i2c_adapter_class);
598 driver_unregister(&i2c_adapter_driver);
599 bus_unregister(&i2c_bus_type);
600}
601
602subsys_initcall(i2c_init);
603module_exit(i2c_exit);
604
605/* ----------------------------------------------------
606 * the functional interface to the i2c busses.
607 * ----------------------------------------------------
608 */
609
610int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
611{
612 int ret;
613
614 if (adap->algo->master_xfer) {
615#ifdef DEBUG
616 for (ret = 0; ret < num; ret++) {
617 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
618 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
619 'R' : 'W', msgs[ret].addr, msgs[ret].len);
620 }
621#endif
622
Jiri Kosina6ea23032006-12-10 21:21:30 +0100623 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100625 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 return ret;
628 } else {
629 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
630 return -ENOSYS;
631 }
632}
633
634int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
635{
636 int ret;
637 struct i2c_adapter *adap=client->adapter;
638 struct i2c_msg msg;
639
Jean Delvare815f55f2005-05-07 22:58:46 +0200640 msg.addr = client->addr;
641 msg.flags = client->flags & I2C_M_TEN;
642 msg.len = count;
643 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100644
Jean Delvare815f55f2005-05-07 22:58:46 +0200645 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jean Delvare815f55f2005-05-07 22:58:46 +0200647 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
648 transmitted, else error code. */
649 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
653{
654 struct i2c_adapter *adap=client->adapter;
655 struct i2c_msg msg;
656 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jean Delvare815f55f2005-05-07 22:58:46 +0200658 msg.addr = client->addr;
659 msg.flags = client->flags & I2C_M_TEN;
660 msg.flags |= I2C_M_RD;
661 msg.len = count;
662 msg.buf = buf;
663
664 ret = i2c_transfer(adap, &msg, 1);
665
666 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
667 transmitted, else error code. */
668 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671
672int i2c_control(struct i2c_client *client,
673 unsigned int cmd, unsigned long arg)
674{
675 int ret = 0;
676 struct i2c_adapter *adap = client->adapter;
677
678 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
679 switch (cmd) {
680 case I2C_RETRIES:
681 adap->retries = arg;
682 break;
683 case I2C_TIMEOUT:
684 adap->timeout = arg;
685 break;
686 default:
687 if (adap->algo->algo_control!=NULL)
688 ret = adap->algo->algo_control(adap,cmd,arg);
689 }
690 return ret;
691}
692
693/* ----------------------------------------------------
694 * the i2c address scanning function
695 * Will not work for 10-bit addresses!
696 * ----------------------------------------------------
697 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200698static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
699 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200700{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200701 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200702
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200703 /* Make sure the address is valid */
704 if (addr < 0x03 || addr > 0x77) {
705 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
706 addr);
707 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200708 }
709
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200710 /* Skip if already in use */
711 if (i2c_check_addr(adapter, addr))
712 return 0;
713
714 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200715 if (kind < 0) {
716 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
717 I2C_SMBUS_QUICK, NULL) < 0)
718 return 0;
719
720 /* prevent 24RF08 corruption */
721 if ((addr & ~0x0f) == 0x50)
722 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
723 I2C_SMBUS_QUICK, NULL);
724 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200725
726 /* Finally call the custom detection function */
727 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200728 /* -ENODEV can be returned if there is a chip at the given address
729 but it isn't supported by this chip driver. We catch it here as
730 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200731 if (err == -ENODEV)
732 err = 0;
733
734 if (err)
735 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
736 addr, err);
737 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740int i2c_probe(struct i2c_adapter *adapter,
741 struct i2c_client_address_data *address_data,
742 int (*found_proc) (struct i2c_adapter *, int, int))
743{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200744 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int adap_id = i2c_adapter_id(adapter);
746
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200747 /* Force entries are done first, and are not affected by ignore
748 entries */
749 if (address_data->forces) {
750 unsigned short **forces = address_data->forces;
751 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200753 for (kind = 0; forces[kind]; kind++) {
754 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
755 i += 2) {
756 if (forces[kind][i] == adap_id
757 || forces[kind][i] == ANY_I2C_BUS) {
758 dev_dbg(&adapter->dev, "found force "
759 "parameter for adapter %d, "
760 "addr 0x%02x, kind %d\n",
761 adap_id, forces[kind][i + 1],
762 kind);
763 err = i2c_probe_address(adapter,
764 forces[kind][i + 1],
765 kind, found_proc);
766 if (err)
767 return err;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200772
Jean Delvare4366dc92005-09-25 16:50:06 +0200773 /* Stop here if we can't use SMBUS_QUICK */
774 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
775 if (address_data->probe[0] == I2C_CLIENT_END
776 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100777 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200778
779 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
780 "can't probe for chips\n");
781 return -1;
782 }
783
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200784 /* Probe entries are done second, and are not affected by ignore
785 entries either */
786 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
787 if (address_data->probe[i] == adap_id
788 || address_data->probe[i] == ANY_I2C_BUS) {
789 dev_dbg(&adapter->dev, "found probe parameter for "
790 "adapter %d, addr 0x%02x\n", adap_id,
791 address_data->probe[i + 1]);
792 err = i2c_probe_address(adapter,
793 address_data->probe[i + 1],
794 -1, found_proc);
795 if (err)
796 return err;
797 }
798 }
799
800 /* Normal entries are done last, unless shadowed by an ignore entry */
801 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
802 int j, ignore;
803
804 ignore = 0;
805 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
806 j += 2) {
807 if ((address_data->ignore[j] == adap_id ||
808 address_data->ignore[j] == ANY_I2C_BUS)
809 && address_data->ignore[j + 1]
810 == address_data->normal_i2c[i]) {
811 dev_dbg(&adapter->dev, "found ignore "
812 "parameter for adapter %d, "
813 "addr 0x%02x\n", adap_id,
814 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +0200815 ignore = 1;
816 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200817 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200818 }
819 if (ignore)
820 continue;
821
822 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
823 "addr 0x%02x\n", adap_id,
824 address_data->normal_i2c[i]);
825 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
826 -1, found_proc);
827 if (err)
828 return err;
829 }
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834struct i2c_adapter* i2c_get_adapter(int id)
835{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +0100837
Arjan van de Venb3585e42006-01-11 10:50:26 +0100838 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400839 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
840 if (adapter && !try_module_get(adapter->owner))
841 adapter = NULL;
842
Arjan van de Venb3585e42006-01-11 10:50:26 +0100843 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400844 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847void i2c_put_adapter(struct i2c_adapter *adap)
848{
849 module_put(adap->owner);
850}
851
852/* The SMBus parts */
853
David Brownell438d6c22006-12-10 21:21:31 +0100854#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static u8
856crc8(u16 data)
857{
858 int i;
David Brownell438d6c22006-12-10 21:21:31 +0100859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +0100861 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 data = data ^ POLY;
863 data = data << 1;
864 }
865 return (u8)(data >> 8);
866}
867
Jean Delvare421ef472005-10-26 21:28:55 +0200868/* Incremental CRC8 over count bytes in the array pointed to by p */
869static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 int i;
872
873 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200874 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return crc;
876}
877
Jean Delvare421ef472005-10-26 21:28:55 +0200878/* Assume a 7-bit address, which is reasonable for SMBus */
879static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Jean Delvare421ef472005-10-26 21:28:55 +0200881 /* The address will be sent first */
882 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
883 pec = i2c_smbus_pec(pec, &addr, 1);
884
885 /* The data buffer follows */
886 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Jean Delvare421ef472005-10-26 21:28:55 +0200889/* Used for write only transactions */
890static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jean Delvare421ef472005-10-26 21:28:55 +0200892 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
893 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Jean Delvare421ef472005-10-26 21:28:55 +0200896/* Return <0 on CRC error
897 If there was a write before this read (most cases) we need to take the
898 partial CRC from the write part into account.
899 Note that this function does modify the message (we need to decrease the
900 message length to hide the CRC byte from the caller). */
901static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Jean Delvare421ef472005-10-26 21:28:55 +0200903 u8 rpec = msg->buf[--msg->len];
904 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (rpec != cpec) {
907 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
908 rpec, cpec);
909 return -1;
910 }
David Brownell438d6c22006-12-10 21:21:31 +0100911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
915{
916 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +0100917 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920s32 i2c_smbus_read_byte(struct i2c_client *client)
921{
922 union i2c_smbus_data data;
923 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
924 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
925 return -1;
926 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200927 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
931{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200933 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
937{
938 union i2c_smbus_data data;
939 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
940 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
941 return -1;
942 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200943 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
947{
948 union i2c_smbus_data data;
949 data.byte = value;
950 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
951 I2C_SMBUS_WRITE,command,
952 I2C_SMBUS_BYTE_DATA,&data);
953}
954
955s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
956{
957 union i2c_smbus_data data;
958 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
959 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
960 return -1;
961 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200962 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
966{
967 union i2c_smbus_data data;
968 data.word = value;
969 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
970 I2C_SMBUS_WRITE,command,
971 I2C_SMBUS_WORD_DATA,&data);
972}
973
974s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200975 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (length > I2C_SMBUS_BLOCK_MAX)
980 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +0100982 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
984 I2C_SMBUS_WRITE,command,
985 I2C_SMBUS_BLOCK_DATA,&data);
986}
987
988/* Returns the number of read bytes */
989s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
990{
991 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
994 I2C_SMBUS_READ,command,
995 I2C_SMBUS_I2C_BLOCK_DATA,&data))
996 return -1;
Jean Delvare76560322006-01-18 23:14:55 +0100997
998 memcpy(values, &data.block[1], data.block[0]);
999 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Jean Delvare21bbd692006-01-09 15:19:18 +11001002s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001003 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001004{
1005 union i2c_smbus_data data;
1006
1007 if (length > I2C_SMBUS_BLOCK_MAX)
1008 length = I2C_SMBUS_BLOCK_MAX;
1009 data.block[0] = length;
1010 memcpy(data.block + 1, values, length);
1011 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1012 I2C_SMBUS_WRITE, command,
1013 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1014}
1015
David Brownell438d6c22006-12-10 21:21:31 +01001016/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001018static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001020 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 union i2c_smbus_data * data)
1022{
1023 /* So we need to generate a series of msgs. In the case of writing, we
1024 need to use only one message; when reading, we need two. We initialize
1025 most things with sane defaults, to keep the code below somewhat
1026 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001027 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1028 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001030 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1032 };
1033 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001034 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 msgbuf0[0] = command;
1037 switch(size) {
1038 case I2C_SMBUS_QUICK:
1039 msg[0].len = 0;
1040 /* Special case: The read/write field is used as data */
1041 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1042 num = 1;
1043 break;
1044 case I2C_SMBUS_BYTE:
1045 if (read_write == I2C_SMBUS_READ) {
1046 /* Special case: only a read! */
1047 msg[0].flags = I2C_M_RD | flags;
1048 num = 1;
1049 }
1050 break;
1051 case I2C_SMBUS_BYTE_DATA:
1052 if (read_write == I2C_SMBUS_READ)
1053 msg[1].len = 1;
1054 else {
1055 msg[0].len = 2;
1056 msgbuf0[1] = data->byte;
1057 }
1058 break;
1059 case I2C_SMBUS_WORD_DATA:
1060 if (read_write == I2C_SMBUS_READ)
1061 msg[1].len = 2;
1062 else {
1063 msg[0].len=3;
1064 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001065 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 break;
1068 case I2C_SMBUS_PROC_CALL:
1069 num = 2; /* Special case */
1070 read_write = I2C_SMBUS_READ;
1071 msg[0].len = 3;
1072 msg[1].len = 2;
1073 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001074 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (read_write == I2C_SMBUS_READ) {
1078 dev_err(&adapter->dev, "Block read not supported "
1079 "under I2C emulation!\n");
1080 return -1;
1081 } else {
1082 msg[0].len = data->block[0] + 2;
1083 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1084 dev_err(&adapter->dev, "smbus_access called with "
1085 "invalid block write size (%d)\n",
1086 data->block[0]);
1087 return -1;
1088 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001089 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 msgbuf0[i] = data->block[i-1];
1091 }
1092 break;
1093 case I2C_SMBUS_BLOCK_PROC_CALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 dev_dbg(&adapter->dev, "Block process call not supported "
1095 "under I2C emulation!\n");
1096 return -1;
1097 case I2C_SMBUS_I2C_BLOCK_DATA:
1098 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001099 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 } else {
1101 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001102 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1104 "invalid block write size (%d)\n",
1105 data->block[0]);
1106 return -1;
1107 }
1108 for (i = 1; i <= data->block[0]; i++)
1109 msgbuf0[i] = data->block[i];
1110 }
1111 break;
1112 default:
1113 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1114 size);
1115 return -1;
1116 }
1117
Jean Delvare421ef472005-10-26 21:28:55 +02001118 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1119 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1120 if (i) {
1121 /* Compute PEC if first message is a write */
1122 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001123 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001124 i2c_smbus_add_pec(&msg[0]);
1125 else /* Write followed by read */
1126 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1127 }
1128 /* Ask for PEC if last message is a read */
1129 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001130 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001131 }
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (i2c_transfer(adapter, msg, num) < 0)
1134 return -1;
1135
Jean Delvare421ef472005-10-26 21:28:55 +02001136 /* Check PEC if last message is a read */
1137 if (i && (msg[num-1].flags & I2C_M_RD)) {
1138 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1139 return -1;
1140 }
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (read_write == I2C_SMBUS_READ)
1143 switch(size) {
1144 case I2C_SMBUS_BYTE:
1145 data->byte = msgbuf0[0];
1146 break;
1147 case I2C_SMBUS_BYTE_DATA:
1148 data->byte = msgbuf1[0];
1149 break;
David Brownell438d6c22006-12-10 21:21:31 +01001150 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 case I2C_SMBUS_PROC_CALL:
1152 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1153 break;
1154 case I2C_SMBUS_I2C_BLOCK_DATA:
1155 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001156 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1157 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 data->block[i+1] = msgbuf1[i];
1159 break;
1160 }
1161 return 0;
1162}
1163
1164
1165s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001166 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 union i2c_smbus_data * data)
1168{
1169 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001174 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1176 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001177 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 } else
1179 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1180 command,size,data);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return res;
1183}
1184
1185
Jean Delvareefde7232005-07-20 23:03:50 +02001186/* Next four are needed by i2c-isa */
1187EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1188EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1189EXPORT_SYMBOL_GPL(i2c_adapter_class);
1190EXPORT_SYMBOL_GPL(i2c_bus_type);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192EXPORT_SYMBOL(i2c_add_adapter);
1193EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194EXPORT_SYMBOL(i2c_del_driver);
1195EXPORT_SYMBOL(i2c_attach_client);
1196EXPORT_SYMBOL(i2c_detach_client);
1197EXPORT_SYMBOL(i2c_use_client);
1198EXPORT_SYMBOL(i2c_release_client);
1199EXPORT_SYMBOL(i2c_clients_command);
1200EXPORT_SYMBOL(i2c_check_addr);
1201
1202EXPORT_SYMBOL(i2c_master_send);
1203EXPORT_SYMBOL(i2c_master_recv);
1204EXPORT_SYMBOL(i2c_control);
1205EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206EXPORT_SYMBOL(i2c_get_adapter);
1207EXPORT_SYMBOL(i2c_put_adapter);
1208EXPORT_SYMBOL(i2c_probe);
1209
1210EXPORT_SYMBOL(i2c_smbus_xfer);
1211EXPORT_SYMBOL(i2c_smbus_write_quick);
1212EXPORT_SYMBOL(i2c_smbus_read_byte);
1213EXPORT_SYMBOL(i2c_smbus_write_byte);
1214EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1215EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1216EXPORT_SYMBOL(i2c_smbus_read_word_data);
1217EXPORT_SYMBOL(i2c_smbus_write_word_data);
1218EXPORT_SYMBOL(i2c_smbus_write_block_data);
1219EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001220EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1223MODULE_DESCRIPTION("I2C-Bus main module");
1224MODULE_LICENSE("GPL");