blob: 40a744d73e8cdc3f3734a42713e45085a190ba6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
20/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
32#include <linux/seq_file.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010034#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010035#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
38
39static LIST_HEAD(adapters);
40static LIST_HEAD(drivers);
Arjan van de Venb3585e42006-01-11 10:50:26 +010041static DEFINE_MUTEX(core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_IDR(i2c_adapter_idr);
43
David Brownellf37dd802007-02-13 22:09:00 +010044
45/* ------------------------------------------------------------------------- */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* match always succeeds, as we want the probe() to tell if we really accept this match */
48static int i2c_device_match(struct device *dev, struct device_driver *drv)
49{
50 return 1;
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int i2c_device_probe(struct device *dev)
54{
55 return -ENODEV;
56}
57
58static int i2c_device_remove(struct device *dev)
59{
60 return 0;
61}
62
David Brownellf37dd802007-02-13 22:09:00 +010063static void i2c_device_shutdown(struct device *dev)
64{
65 struct i2c_driver *driver;
66
67 if (!dev->driver)
68 return;
69 driver = to_i2c_driver(dev->driver);
70 if (driver->shutdown)
71 driver->shutdown(to_i2c_client(dev));
72}
73
74static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
75{
76 struct i2c_driver *driver;
77
78 if (!dev->driver)
79 return 0;
80 driver = to_i2c_driver(dev->driver);
81 if (!driver->suspend)
82 return 0;
83 return driver->suspend(to_i2c_client(dev), mesg);
84}
85
86static int i2c_device_resume(struct device * dev)
87{
88 struct i2c_driver *driver;
89
90 if (!dev->driver)
91 return 0;
92 driver = to_i2c_driver(dev->driver);
93 if (!driver->resume)
94 return 0;
95 return driver->resume(to_i2c_client(dev));
96}
97
Russell Kingb864c7d2006-01-05 14:37:50 +000098struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +010099 .name = "i2c",
100 .match = i2c_device_match,
101 .probe = i2c_device_probe,
102 .remove = i2c_device_remove,
103 .shutdown = i2c_device_shutdown,
104 .suspend = i2c_device_suspend,
105 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000106};
107
David Brownellf37dd802007-02-13 22:09:00 +0100108/* ------------------------------------------------------------------------- */
109
Jean Delvareefde7232005-07-20 23:03:50 +0200110void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
113 complete(&adap->dev_released);
114}
115
Jean Delvareefde7232005-07-20 23:03:50 +0200116struct device_driver i2c_adapter_driver = {
Laurent Riffard6586bcd2005-10-17 22:54:45 +0200117 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .name = "i2c_adapter",
119 .bus = &i2c_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
David Brownellb119dc32007-01-04 13:07:04 +0100122/* ------------------------------------------------------------------------- */
123
124/* I2C bus adapters -- one roots each I2C or SMBUS segment */
125
Jean Delvareefde7232005-07-20 23:03:50 +0200126struct class i2c_adapter_class = {
David Brownellb119dc32007-01-04 13:07:04 +0100127 .owner = THIS_MODULE,
128 .name = "i2c-adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Yani Ioannoue404e272005-05-17 06:42:58 -0400131static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
134 return sprintf(buf, "%s\n", adap->name);
135}
136static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
137
138
139static void i2c_client_release(struct device *dev)
140{
141 struct i2c_client *client = to_i2c_client(dev);
142 complete(&client->released);
143}
144
Yani Ioannoue404e272005-05-17 06:42:58 -0400145static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct i2c_client *client = to_i2c_client(dev);
148 return sprintf(buf, "%s\n", client->name);
149}
150
David Brownell438d6c22006-12-10 21:21:31 +0100151/*
Jean Delvare7b77d062006-12-10 21:21:31 +0100152 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
153 * an i2c adapter attribute (above).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Jean Delvare7b77d062006-12-10 21:21:31 +0100155static struct device_attribute dev_attr_client_name =
156 __ATTR(name, S_IRUGO, &show_client_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158
159/* ---------------------------------------------------
David Brownell438d6c22006-12-10 21:21:31 +0100160 * registering functions
161 * ---------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
163
164/* -----
165 * i2c_add_adapter is called from within the algorithm layer,
166 * when a new hw adapter registers. A new device is register to be
167 * available for clients.
168 */
169int i2c_add_adapter(struct i2c_adapter *adap)
170{
171 int id, res = 0;
172 struct list_head *item;
173 struct i2c_driver *driver;
174
Arjan van de Venb3585e42006-01-11 10:50:26 +0100175 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
178 res = -ENOMEM;
179 goto out_unlock;
180 }
181
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400182 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (res < 0) {
184 if (res == -EAGAIN)
185 res = -ENOMEM;
186 goto out_unlock;
187 }
188
189 adap->nr = id & MAX_ID_MASK;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100190 mutex_init(&adap->bus_lock);
191 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 list_add_tail(&adap->list,&adapters);
193 INIT_LIST_HEAD(&adap->clients);
194
195 /* Add the adapter to the driver core.
196 * If the parent pointer is not set up,
197 * we add this adapter to the host bus.
198 */
David Brownellb119dc32007-01-04 13:07:04 +0100199 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100201 pr_debug("I2C adapter driver [%s] forgot to specify "
202 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
205 adap->dev.driver = &i2c_adapter_driver;
206 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200207 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200208 res = device_register(&adap->dev);
209 if (res)
210 goto out_list;
211 res = device_create_file(&adap->dev, &dev_attr_name);
212 if (res)
213 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200215 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* inform drivers of new adapters */
218 list_for_each(item,&drivers) {
219 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100220 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* We ignore the return code; if it fails, too bad */
222 driver->attach_adapter(adap);
223 }
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100226 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200228
Jean Delvareb119c6c2006-08-15 18:26:30 +0200229out_unregister:
230 init_completion(&adap->dev_released); /* Needed? */
231 device_unregister(&adap->dev);
232 wait_for_completion(&adap->dev_released);
233out_list:
234 list_del(&adap->list);
235 idr_remove(&i2c_adapter_idr, adap->nr);
236 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239
240int i2c_del_adapter(struct i2c_adapter *adap)
241{
242 struct list_head *item, *_n;
243 struct i2c_adapter *adap_from_list;
244 struct i2c_driver *driver;
245 struct i2c_client *client;
246 int res = 0;
247
Arjan van de Venb3585e42006-01-11 10:50:26 +0100248 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* First make sure that this adapter was ever added */
251 list_for_each_entry(adap_from_list, &adapters, list) {
252 if (adap_from_list == adap)
253 break;
254 }
255 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200256 pr_debug("i2c-core: attempting to delete unregistered "
257 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 res = -EINVAL;
259 goto out_unlock;
260 }
261
262 list_for_each(item,&drivers) {
263 driver = list_entry(item, struct i2c_driver, list);
264 if (driver->detach_adapter)
265 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200266 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100267 "for driver [%s]\n",
268 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 goto out_unlock;
270 }
271 }
272
273 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200274 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 list_for_each_safe(item, _n, &adap->clients) {
276 client = list_entry(item, struct i2c_client, list);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200279 dev_err(&adap->dev, "detach_client failed for client "
280 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 client->addr);
282 goto out_unlock;
283 }
284 }
285
286 /* clean up the sysfs representation */
287 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 device_remove_file(&adap->dev, &dev_attr_name);
289 device_unregister(&adap->dev);
290 list_del(&adap->list);
291
292 /* wait for sysfs to drop all references */
293 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* free dynamically allocated bus id */
296 idr_remove(&i2c_adapter_idr, adap->nr);
297
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200298 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100301 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return res;
303}
304
305
306/* -----
307 * What follows is the "upwards" interface: commands for talking to clients,
308 * which implement the functions to access the physical information of the
309 * chips.
310 */
311
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800312int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct list_head *item;
315 struct i2c_adapter *adapter;
Jean Delvare7eebcb72006-02-05 23:28:21 +0100316 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800319 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 res = driver_register(&driver->driver);
323 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100324 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100325
Jean Delvare7eebcb72006-02-05 23:28:21 +0100326 mutex_lock(&core_lists);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100329 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* now look for instances of driver on our adapters */
Jean Delvare8a994752005-11-26 20:28:06 +0100332 if (driver->attach_adapter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 list_for_each(item,&adapters) {
334 adapter = list_entry(item, struct i2c_adapter, list);
335 driver->attach_adapter(adapter);
336 }
337 }
338
Arjan van de Venb3585e42006-01-11 10:50:26 +0100339 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800342EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344int i2c_del_driver(struct i2c_driver *driver)
345{
346 struct list_head *item1, *item2, *_n;
347 struct i2c_client *client;
348 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int res = 0;
351
Arjan van de Venb3585e42006-01-11 10:50:26 +0100352 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100355 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
358 list_for_each(item1,&adapters) {
359 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (driver->detach_adapter) {
361 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200362 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100363 "for driver [%s]\n",
364 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto out_unlock;
366 }
367 } else {
368 list_for_each_safe(item2, _n, &adap->clients) {
369 client = list_entry(item2, struct i2c_client, list);
370 if (client->driver != driver)
371 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200372 dev_dbg(&adap->dev, "detaching client [%s] "
373 "at 0x%02x\n", client->name,
374 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200376 dev_err(&adap->dev, "detach_client "
377 "failed for client [%s] at "
378 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 client->addr);
380 goto out_unlock;
381 }
382 }
383 }
384 }
385
386 driver_unregister(&driver->driver);
387 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100388 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100391 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
393}
394
395static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
396{
397 struct list_head *item;
398 struct i2c_client *client;
399
400 list_for_each(item,&adapter->clients) {
401 client = list_entry(item, struct i2c_client, list);
402 if (client->addr == addr)
403 return -EBUSY;
404 }
405 return 0;
406}
407
408int i2c_check_addr(struct i2c_adapter *adapter, int addr)
409{
410 int rval;
411
Ingo Molnar5c085d32006-01-18 23:16:04 +0100412 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100414 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 return rval;
417}
418
419int i2c_attach_client(struct i2c_client *client)
420{
421 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200422 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Ingo Molnar5c085d32006-01-18 23:16:04 +0100424 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200426 res = -EBUSY;
427 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100430
Jean Delvarecde78592005-11-26 21:00:54 +0100431 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 client->dev.parent = &client->adapter->dev;
434 client->dev.driver = &client->driver->driver;
435 client->dev.bus = &i2c_bus_type;
436 client->dev.release = &i2c_client_release;
David Brownell438d6c22006-12-10 21:21:31 +0100437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
439 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200440 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
441 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200442 res = device_register(&client->dev);
443 if (res)
444 goto out_list;
445 res = device_create_file(&client->dev, &dev_attr_client_name);
446 if (res)
447 goto out_unregister;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200448 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200449
450 if (adapter->client_register) {
451 if (adapter->client_register(client)) {
452 dev_dbg(&adapter->dev, "client_register "
453 "failed for client [%s] at 0x%02x\n",
454 client->name, client->addr);
455 }
456 }
457
458 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200459
460out_unregister:
461 init_completion(&client->released); /* Needed? */
462 device_unregister(&client->dev);
463 wait_for_completion(&client->released);
464out_list:
465 list_del(&client->list);
466 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
467 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200468out_unlock:
469 mutex_unlock(&adapter->clist_lock);
470 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473
474int i2c_detach_client(struct i2c_client *client)
475{
476 struct i2c_adapter *adapter = client->adapter;
477 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100478
Jean Delvarecde78592005-11-26 21:00:54 +0100479 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200480 dev_warn(&client->dev, "Client [%s] still busy, "
481 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (adapter->client_unregister) {
486 res = adapter->client_unregister(client);
487 if (res) {
488 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700489 "client_unregister [%s] failed, "
490 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto out;
492 }
493 }
494
Ingo Molnar5c085d32006-01-18 23:16:04 +0100495 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 list_del(&client->list);
497 init_completion(&client->released);
498 device_remove_file(&client->dev, &dev_attr_client_name);
499 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100500 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 wait_for_completion(&client->released);
502
503 out:
504 return res;
505}
506
507static int i2c_inc_use_client(struct i2c_client *client)
508{
509
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100510 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return -ENODEV;
512 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100513 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -ENODEV;
515 }
516
517 return 0;
518}
519
520static void i2c_dec_use_client(struct i2c_client *client)
521{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100522 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 module_put(client->adapter->owner);
524}
525
526int i2c_use_client(struct i2c_client *client)
527{
528 int ret;
529
530 ret = i2c_inc_use_client(client);
531 if (ret)
532 return ret;
533
Jean Delvarecde78592005-11-26 21:00:54 +0100534 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539int i2c_release_client(struct i2c_client *client)
540{
Jean Delvarecde78592005-11-26 21:00:54 +0100541 if (!client->usage_count) {
542 pr_debug("i2c-core: %s used one too many times\n",
543 __FUNCTION__);
544 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
David Brownell438d6c22006-12-10 21:21:31 +0100546
Jean Delvarecde78592005-11-26 21:00:54 +0100547 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551}
552
553void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
554{
555 struct list_head *item;
556 struct i2c_client *client;
557
Ingo Molnar5c085d32006-01-18 23:16:04 +0100558 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 list_for_each(item,&adap->clients) {
560 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100561 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 continue;
563 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100564 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100566 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100568 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100570 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573static int __init i2c_init(void)
574{
575 int retval;
576
577 retval = bus_register(&i2c_bus_type);
578 if (retval)
579 return retval;
580 retval = driver_register(&i2c_adapter_driver);
581 if (retval)
582 return retval;
583 return class_register(&i2c_adapter_class);
584}
585
586static void __exit i2c_exit(void)
587{
588 class_unregister(&i2c_adapter_class);
589 driver_unregister(&i2c_adapter_driver);
590 bus_unregister(&i2c_bus_type);
591}
592
593subsys_initcall(i2c_init);
594module_exit(i2c_exit);
595
596/* ----------------------------------------------------
597 * the functional interface to the i2c busses.
598 * ----------------------------------------------------
599 */
600
601int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
602{
603 int ret;
604
605 if (adap->algo->master_xfer) {
606#ifdef DEBUG
607 for (ret = 0; ret < num; ret++) {
608 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
609 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
610 'R' : 'W', msgs[ret].addr, msgs[ret].len);
611 }
612#endif
613
Jiri Kosina6ea23032006-12-10 21:21:30 +0100614 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100616 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 return ret;
619 } else {
620 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
621 return -ENOSYS;
622 }
623}
624
625int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
626{
627 int ret;
628 struct i2c_adapter *adap=client->adapter;
629 struct i2c_msg msg;
630
Jean Delvare815f55f2005-05-07 22:58:46 +0200631 msg.addr = client->addr;
632 msg.flags = client->flags & I2C_M_TEN;
633 msg.len = count;
634 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100635
Jean Delvare815f55f2005-05-07 22:58:46 +0200636 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jean Delvare815f55f2005-05-07 22:58:46 +0200638 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
639 transmitted, else error code. */
640 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
644{
645 struct i2c_adapter *adap=client->adapter;
646 struct i2c_msg msg;
647 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jean Delvare815f55f2005-05-07 22:58:46 +0200649 msg.addr = client->addr;
650 msg.flags = client->flags & I2C_M_TEN;
651 msg.flags |= I2C_M_RD;
652 msg.len = count;
653 msg.buf = buf;
654
655 ret = i2c_transfer(adap, &msg, 1);
656
657 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
658 transmitted, else error code. */
659 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662
663int i2c_control(struct i2c_client *client,
664 unsigned int cmd, unsigned long arg)
665{
666 int ret = 0;
667 struct i2c_adapter *adap = client->adapter;
668
669 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
670 switch (cmd) {
671 case I2C_RETRIES:
672 adap->retries = arg;
673 break;
674 case I2C_TIMEOUT:
675 adap->timeout = arg;
676 break;
677 default:
678 if (adap->algo->algo_control!=NULL)
679 ret = adap->algo->algo_control(adap,cmd,arg);
680 }
681 return ret;
682}
683
684/* ----------------------------------------------------
685 * the i2c address scanning function
686 * Will not work for 10-bit addresses!
687 * ----------------------------------------------------
688 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200689static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
690 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200691{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200692 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200693
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200694 /* Make sure the address is valid */
695 if (addr < 0x03 || addr > 0x77) {
696 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
697 addr);
698 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200699 }
700
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200701 /* Skip if already in use */
702 if (i2c_check_addr(adapter, addr))
703 return 0;
704
705 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200706 if (kind < 0) {
707 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
708 I2C_SMBUS_QUICK, NULL) < 0)
709 return 0;
710
711 /* prevent 24RF08 corruption */
712 if ((addr & ~0x0f) == 0x50)
713 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
714 I2C_SMBUS_QUICK, NULL);
715 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200716
717 /* Finally call the custom detection function */
718 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200719 /* -ENODEV can be returned if there is a chip at the given address
720 but it isn't supported by this chip driver. We catch it here as
721 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200722 if (err == -ENODEV)
723 err = 0;
724
725 if (err)
726 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
727 addr, err);
728 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731int i2c_probe(struct i2c_adapter *adapter,
732 struct i2c_client_address_data *address_data,
733 int (*found_proc) (struct i2c_adapter *, int, int))
734{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200735 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int adap_id = i2c_adapter_id(adapter);
737
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200738 /* Force entries are done first, and are not affected by ignore
739 entries */
740 if (address_data->forces) {
741 unsigned short **forces = address_data->forces;
742 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200744 for (kind = 0; forces[kind]; kind++) {
745 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
746 i += 2) {
747 if (forces[kind][i] == adap_id
748 || forces[kind][i] == ANY_I2C_BUS) {
749 dev_dbg(&adapter->dev, "found force "
750 "parameter for adapter %d, "
751 "addr 0x%02x, kind %d\n",
752 adap_id, forces[kind][i + 1],
753 kind);
754 err = i2c_probe_address(adapter,
755 forces[kind][i + 1],
756 kind, found_proc);
757 if (err)
758 return err;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200763
Jean Delvare4366dc92005-09-25 16:50:06 +0200764 /* Stop here if we can't use SMBUS_QUICK */
765 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
766 if (address_data->probe[0] == I2C_CLIENT_END
767 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100768 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200769
770 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
771 "can't probe for chips\n");
772 return -1;
773 }
774
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200775 /* Probe entries are done second, and are not affected by ignore
776 entries either */
777 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
778 if (address_data->probe[i] == adap_id
779 || address_data->probe[i] == ANY_I2C_BUS) {
780 dev_dbg(&adapter->dev, "found probe parameter for "
781 "adapter %d, addr 0x%02x\n", adap_id,
782 address_data->probe[i + 1]);
783 err = i2c_probe_address(adapter,
784 address_data->probe[i + 1],
785 -1, found_proc);
786 if (err)
787 return err;
788 }
789 }
790
791 /* Normal entries are done last, unless shadowed by an ignore entry */
792 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
793 int j, ignore;
794
795 ignore = 0;
796 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
797 j += 2) {
798 if ((address_data->ignore[j] == adap_id ||
799 address_data->ignore[j] == ANY_I2C_BUS)
800 && address_data->ignore[j + 1]
801 == address_data->normal_i2c[i]) {
802 dev_dbg(&adapter->dev, "found ignore "
803 "parameter for adapter %d, "
804 "addr 0x%02x\n", adap_id,
805 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +0200806 ignore = 1;
807 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200808 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200809 }
810 if (ignore)
811 continue;
812
813 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
814 "addr 0x%02x\n", adap_id,
815 address_data->normal_i2c[i]);
816 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
817 -1, found_proc);
818 if (err)
819 return err;
820 }
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825struct i2c_adapter* i2c_get_adapter(int id)
826{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +0100828
Arjan van de Venb3585e42006-01-11 10:50:26 +0100829 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400830 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
831 if (adapter && !try_module_get(adapter->owner))
832 adapter = NULL;
833
Arjan van de Venb3585e42006-01-11 10:50:26 +0100834 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400835 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838void i2c_put_adapter(struct i2c_adapter *adap)
839{
840 module_put(adap->owner);
841}
842
843/* The SMBus parts */
844
David Brownell438d6c22006-12-10 21:21:31 +0100845#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846static u8
847crc8(u16 data)
848{
849 int i;
David Brownell438d6c22006-12-10 21:21:31 +0100850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +0100852 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 data = data ^ POLY;
854 data = data << 1;
855 }
856 return (u8)(data >> 8);
857}
858
Jean Delvare421ef472005-10-26 21:28:55 +0200859/* Incremental CRC8 over count bytes in the array pointed to by p */
860static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 int i;
863
864 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200865 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return crc;
867}
868
Jean Delvare421ef472005-10-26 21:28:55 +0200869/* Assume a 7-bit address, which is reasonable for SMBus */
870static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Jean Delvare421ef472005-10-26 21:28:55 +0200872 /* The address will be sent first */
873 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
874 pec = i2c_smbus_pec(pec, &addr, 1);
875
876 /* The data buffer follows */
877 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Jean Delvare421ef472005-10-26 21:28:55 +0200880/* Used for write only transactions */
881static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jean Delvare421ef472005-10-26 21:28:55 +0200883 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
884 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Jean Delvare421ef472005-10-26 21:28:55 +0200887/* Return <0 on CRC error
888 If there was a write before this read (most cases) we need to take the
889 partial CRC from the write part into account.
890 Note that this function does modify the message (we need to decrease the
891 message length to hide the CRC byte from the caller). */
892static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Jean Delvare421ef472005-10-26 21:28:55 +0200894 u8 rpec = msg->buf[--msg->len];
895 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (rpec != cpec) {
898 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
899 rpec, cpec);
900 return -1;
901 }
David Brownell438d6c22006-12-10 21:21:31 +0100902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
906{
907 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +0100908 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911s32 i2c_smbus_read_byte(struct i2c_client *client)
912{
913 union i2c_smbus_data data;
914 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
915 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
916 return -1;
917 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200918 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
922{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200924 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
928{
929 union i2c_smbus_data data;
930 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
931 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
932 return -1;
933 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200934 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
938{
939 union i2c_smbus_data data;
940 data.byte = value;
941 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
942 I2C_SMBUS_WRITE,command,
943 I2C_SMBUS_BYTE_DATA,&data);
944}
945
946s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
947{
948 union i2c_smbus_data data;
949 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
950 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
951 return -1;
952 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200953 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
957{
958 union i2c_smbus_data data;
959 data.word = value;
960 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
961 I2C_SMBUS_WRITE,command,
962 I2C_SMBUS_WORD_DATA,&data);
963}
964
965s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200966 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (length > I2C_SMBUS_BLOCK_MAX)
971 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +0100973 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
975 I2C_SMBUS_WRITE,command,
976 I2C_SMBUS_BLOCK_DATA,&data);
977}
978
979/* Returns the number of read bytes */
980s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
981{
982 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
985 I2C_SMBUS_READ,command,
986 I2C_SMBUS_I2C_BLOCK_DATA,&data))
987 return -1;
Jean Delvare76560322006-01-18 23:14:55 +0100988
989 memcpy(values, &data.block[1], data.block[0]);
990 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Jean Delvare21bbd692006-01-09 15:19:18 +1100993s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200994 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +1100995{
996 union i2c_smbus_data data;
997
998 if (length > I2C_SMBUS_BLOCK_MAX)
999 length = I2C_SMBUS_BLOCK_MAX;
1000 data.block[0] = length;
1001 memcpy(data.block + 1, values, length);
1002 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1003 I2C_SMBUS_WRITE, command,
1004 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1005}
1006
David Brownell438d6c22006-12-10 21:21:31 +01001007/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001009static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001011 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 union i2c_smbus_data * data)
1013{
1014 /* So we need to generate a series of msgs. In the case of writing, we
1015 need to use only one message; when reading, we need two. We initialize
1016 most things with sane defaults, to keep the code below somewhat
1017 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001018 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1019 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001021 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1023 };
1024 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001025 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 msgbuf0[0] = command;
1028 switch(size) {
1029 case I2C_SMBUS_QUICK:
1030 msg[0].len = 0;
1031 /* Special case: The read/write field is used as data */
1032 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1033 num = 1;
1034 break;
1035 case I2C_SMBUS_BYTE:
1036 if (read_write == I2C_SMBUS_READ) {
1037 /* Special case: only a read! */
1038 msg[0].flags = I2C_M_RD | flags;
1039 num = 1;
1040 }
1041 break;
1042 case I2C_SMBUS_BYTE_DATA:
1043 if (read_write == I2C_SMBUS_READ)
1044 msg[1].len = 1;
1045 else {
1046 msg[0].len = 2;
1047 msgbuf0[1] = data->byte;
1048 }
1049 break;
1050 case I2C_SMBUS_WORD_DATA:
1051 if (read_write == I2C_SMBUS_READ)
1052 msg[1].len = 2;
1053 else {
1054 msg[0].len=3;
1055 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001056 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 break;
1059 case I2C_SMBUS_PROC_CALL:
1060 num = 2; /* Special case */
1061 read_write = I2C_SMBUS_READ;
1062 msg[0].len = 3;
1063 msg[1].len = 2;
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 break;
1067 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (read_write == I2C_SMBUS_READ) {
1069 dev_err(&adapter->dev, "Block read not supported "
1070 "under I2C emulation!\n");
1071 return -1;
1072 } else {
1073 msg[0].len = data->block[0] + 2;
1074 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1075 dev_err(&adapter->dev, "smbus_access called with "
1076 "invalid block write size (%d)\n",
1077 data->block[0]);
1078 return -1;
1079 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001080 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 msgbuf0[i] = data->block[i-1];
1082 }
1083 break;
1084 case I2C_SMBUS_BLOCK_PROC_CALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 dev_dbg(&adapter->dev, "Block process call not supported "
1086 "under I2C emulation!\n");
1087 return -1;
1088 case I2C_SMBUS_I2C_BLOCK_DATA:
1089 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001090 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 } else {
1092 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001093 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1095 "invalid block write size (%d)\n",
1096 data->block[0]);
1097 return -1;
1098 }
1099 for (i = 1; i <= data->block[0]; i++)
1100 msgbuf0[i] = data->block[i];
1101 }
1102 break;
1103 default:
1104 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1105 size);
1106 return -1;
1107 }
1108
Jean Delvare421ef472005-10-26 21:28:55 +02001109 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1110 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1111 if (i) {
1112 /* Compute PEC if first message is a write */
1113 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001114 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001115 i2c_smbus_add_pec(&msg[0]);
1116 else /* Write followed by read */
1117 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1118 }
1119 /* Ask for PEC if last message is a read */
1120 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001121 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001122 }
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (i2c_transfer(adapter, msg, num) < 0)
1125 return -1;
1126
Jean Delvare421ef472005-10-26 21:28:55 +02001127 /* Check PEC if last message is a read */
1128 if (i && (msg[num-1].flags & I2C_M_RD)) {
1129 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1130 return -1;
1131 }
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (read_write == I2C_SMBUS_READ)
1134 switch(size) {
1135 case I2C_SMBUS_BYTE:
1136 data->byte = msgbuf0[0];
1137 break;
1138 case I2C_SMBUS_BYTE_DATA:
1139 data->byte = msgbuf1[0];
1140 break;
David Brownell438d6c22006-12-10 21:21:31 +01001141 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 case I2C_SMBUS_PROC_CALL:
1143 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1144 break;
1145 case I2C_SMBUS_I2C_BLOCK_DATA:
1146 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001147 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1148 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 data->block[i+1] = msgbuf1[i];
1150 break;
1151 }
1152 return 0;
1153}
1154
1155
1156s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001157 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 union i2c_smbus_data * data)
1159{
1160 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001165 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1167 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001168 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } else
1170 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1171 command,size,data);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return res;
1174}
1175
1176
Jean Delvareefde7232005-07-20 23:03:50 +02001177/* Next four are needed by i2c-isa */
1178EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1179EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1180EXPORT_SYMBOL_GPL(i2c_adapter_class);
1181EXPORT_SYMBOL_GPL(i2c_bus_type);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183EXPORT_SYMBOL(i2c_add_adapter);
1184EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185EXPORT_SYMBOL(i2c_del_driver);
1186EXPORT_SYMBOL(i2c_attach_client);
1187EXPORT_SYMBOL(i2c_detach_client);
1188EXPORT_SYMBOL(i2c_use_client);
1189EXPORT_SYMBOL(i2c_release_client);
1190EXPORT_SYMBOL(i2c_clients_command);
1191EXPORT_SYMBOL(i2c_check_addr);
1192
1193EXPORT_SYMBOL(i2c_master_send);
1194EXPORT_SYMBOL(i2c_master_recv);
1195EXPORT_SYMBOL(i2c_control);
1196EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197EXPORT_SYMBOL(i2c_get_adapter);
1198EXPORT_SYMBOL(i2c_put_adapter);
1199EXPORT_SYMBOL(i2c_probe);
1200
1201EXPORT_SYMBOL(i2c_smbus_xfer);
1202EXPORT_SYMBOL(i2c_smbus_write_quick);
1203EXPORT_SYMBOL(i2c_smbus_read_byte);
1204EXPORT_SYMBOL(i2c_smbus_write_byte);
1205EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1206EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1207EXPORT_SYMBOL(i2c_smbus_read_word_data);
1208EXPORT_SYMBOL(i2c_smbus_write_word_data);
1209EXPORT_SYMBOL(i2c_smbus_write_block_data);
1210EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001211EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1214MODULE_DESCRIPTION("I2C-Bus main module");
1215MODULE_LICENSE("GPL");