blob: 60f6eb194046386535a8f8d7883921501d1f635c [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
44/* match always succeeds, as we want the probe() to tell if we really accept this match */
45static int i2c_device_match(struct device *dev, struct device_driver *drv)
46{
47 return 1;
48}
49
50static int i2c_bus_suspend(struct device * dev, pm_message_t state)
51{
52 int rc = 0;
53
54 if (dev->driver && dev->driver->suspend)
Russell King9480e302005-10-28 09:52:56 -070055 rc = dev->driver->suspend(dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return rc;
57}
58
59static int i2c_bus_resume(struct device * dev)
60{
61 int rc = 0;
62
63 if (dev->driver && dev->driver->resume)
Russell King9480e302005-10-28 09:52:56 -070064 rc = dev->driver->resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return rc;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int i2c_device_probe(struct device *dev)
69{
70 return -ENODEV;
71}
72
73static int i2c_device_remove(struct device *dev)
74{
75 return 0;
76}
77
Russell Kingb864c7d2006-01-05 14:37:50 +000078struct bus_type i2c_bus_type = {
79 .name = "i2c",
80 .match = i2c_device_match,
81 .probe = i2c_device_probe,
82 .remove = i2c_device_remove,
83 .suspend = i2c_bus_suspend,
84 .resume = i2c_bus_resume,
85};
86
Jean Delvareefde7232005-07-20 23:03:50 +020087void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
90 complete(&adap->dev_released);
91}
92
Jean Delvareefde7232005-07-20 23:03:50 +020093struct device_driver i2c_adapter_driver = {
Laurent Riffard6586bcd2005-10-17 22:54:45 +020094 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .name = "i2c_adapter",
96 .bus = &i2c_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
David Brownellb119dc32007-01-04 13:07:04 +010099/* ------------------------------------------------------------------------- */
100
101/* I2C bus adapters -- one roots each I2C or SMBUS segment */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static void i2c_adapter_class_dev_release(struct class_device *dev)
104{
105 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
106 complete(&adap->class_dev_released);
107}
108
David Brownellb119dc32007-01-04 13:07:04 +0100109static ssize_t i2c_adapter_show_name(struct class_device *cdev, char *buf)
110{
111 struct i2c_adapter *adap = class_dev_to_i2c_adapter(cdev);
112 return sprintf(buf, "%s\n", adap->name);
113}
114
115static struct class_device_attribute i2c_adapter_attrs[] = {
116 __ATTR(name, S_IRUGO, i2c_adapter_show_name, NULL),
117 { },
118};
119
Jean Delvareefde7232005-07-20 23:03:50 +0200120struct class i2c_adapter_class = {
David Brownellb119dc32007-01-04 13:07:04 +0100121 .owner = THIS_MODULE,
122 .name = "i2c-adapter",
123 .class_dev_attrs = i2c_adapter_attrs,
124 .release = &i2c_adapter_class_dev_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Yani Ioannoue404e272005-05-17 06:42:58 -0400127static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
130 return sprintf(buf, "%s\n", adap->name);
131}
132static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
133
134
135static void i2c_client_release(struct device *dev)
136{
137 struct i2c_client *client = to_i2c_client(dev);
138 complete(&client->released);
139}
140
Yani Ioannoue404e272005-05-17 06:42:58 -0400141static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct i2c_client *client = to_i2c_client(dev);
144 return sprintf(buf, "%s\n", client->name);
145}
146
David Brownell438d6c22006-12-10 21:21:31 +0100147/*
Jean Delvare7b77d062006-12-10 21:21:31 +0100148 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
149 * an i2c adapter attribute (above).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
Jean Delvare7b77d062006-12-10 21:21:31 +0100151static struct device_attribute dev_attr_client_name =
152 __ATTR(name, S_IRUGO, &show_client_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154
155/* ---------------------------------------------------
David Brownell438d6c22006-12-10 21:21:31 +0100156 * registering functions
157 * ---------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
159
160/* -----
161 * i2c_add_adapter is called from within the algorithm layer,
162 * when a new hw adapter registers. A new device is register to be
163 * available for clients.
164 */
165int i2c_add_adapter(struct i2c_adapter *adap)
166{
167 int id, res = 0;
168 struct list_head *item;
169 struct i2c_driver *driver;
170
Arjan van de Venb3585e42006-01-11 10:50:26 +0100171 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
174 res = -ENOMEM;
175 goto out_unlock;
176 }
177
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400178 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (res < 0) {
180 if (res == -EAGAIN)
181 res = -ENOMEM;
182 goto out_unlock;
183 }
184
185 adap->nr = id & MAX_ID_MASK;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100186 mutex_init(&adap->bus_lock);
187 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 list_add_tail(&adap->list,&adapters);
189 INIT_LIST_HEAD(&adap->clients);
190
191 /* Add the adapter to the driver core.
192 * If the parent pointer is not set up,
193 * we add this adapter to the host bus.
194 */
David Brownellb119dc32007-01-04 13:07:04 +0100195 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 adap->dev.parent = &platform_bus;
David Brownellb119dc32007-01-04 13:07:04 +0100197 printk(KERN_WARNING "**WARNING** I2C adapter driver [%s] "
198 "forgot to specify physical device; fix it!\n",
199 adap->name);
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
202 adap->dev.driver = &i2c_adapter_driver;
203 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200204 res = device_register(&adap->dev);
205 if (res)
206 goto out_list;
207 res = device_create_file(&adap->dev, &dev_attr_name);
208 if (res)
209 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* Add this adapter to the i2c_adapter class */
212 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
213 adap->class_dev.dev = &adap->dev;
214 adap->class_dev.class = &i2c_adapter_class;
215 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200216 res = class_device_register(&adap->class_dev);
217 if (res)
218 goto out_remove_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200220 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* inform drivers of new adapters */
223 list_for_each(item,&drivers) {
224 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100225 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* We ignore the return code; if it fails, too bad */
227 driver->attach_adapter(adap);
228 }
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100231 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200233
234out_remove_name:
235 device_remove_file(&adap->dev, &dev_attr_name);
236out_unregister:
237 init_completion(&adap->dev_released); /* Needed? */
238 device_unregister(&adap->dev);
239 wait_for_completion(&adap->dev_released);
240out_list:
241 list_del(&adap->list);
242 idr_remove(&i2c_adapter_idr, adap->nr);
243 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246
247int i2c_del_adapter(struct i2c_adapter *adap)
248{
249 struct list_head *item, *_n;
250 struct i2c_adapter *adap_from_list;
251 struct i2c_driver *driver;
252 struct i2c_client *client;
253 int res = 0;
254
Arjan van de Venb3585e42006-01-11 10:50:26 +0100255 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* First make sure that this adapter was ever added */
258 list_for_each_entry(adap_from_list, &adapters, list) {
259 if (adap_from_list == adap)
260 break;
261 }
262 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200263 pr_debug("i2c-core: attempting to delete unregistered "
264 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 res = -EINVAL;
266 goto out_unlock;
267 }
268
269 list_for_each(item,&drivers) {
270 driver = list_entry(item, struct i2c_driver, list);
271 if (driver->detach_adapter)
272 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200273 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100274 "for driver [%s]\n",
275 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto out_unlock;
277 }
278 }
279
280 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200281 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 list_for_each_safe(item, _n, &adap->clients) {
283 client = list_entry(item, struct i2c_client, list);
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200286 dev_err(&adap->dev, "detach_client failed for client "
287 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 client->addr);
289 goto out_unlock;
290 }
291 }
292
293 /* clean up the sysfs representation */
294 init_completion(&adap->dev_released);
295 init_completion(&adap->class_dev_released);
296 class_device_unregister(&adap->class_dev);
297 device_remove_file(&adap->dev, &dev_attr_name);
298 device_unregister(&adap->dev);
299 list_del(&adap->list);
300
301 /* wait for sysfs to drop all references */
302 wait_for_completion(&adap->dev_released);
303 wait_for_completion(&adap->class_dev_released);
304
305 /* free dynamically allocated bus id */
306 idr_remove(&i2c_adapter_idr, adap->nr);
307
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200308 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100311 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return res;
313}
314
315
316/* -----
317 * What follows is the "upwards" interface: commands for talking to clients,
318 * which implement the functions to access the physical information of the
319 * chips.
320 */
321
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800322int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct list_head *item;
325 struct i2c_adapter *adapter;
Jean Delvare7eebcb72006-02-05 23:28:21 +0100326 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800329 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 res = driver_register(&driver->driver);
333 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100334 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100335
Jean Delvare7eebcb72006-02-05 23:28:21 +0100336 mutex_lock(&core_lists);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100339 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* now look for instances of driver on our adapters */
Jean Delvare8a994752005-11-26 20:28:06 +0100342 if (driver->attach_adapter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 list_for_each(item,&adapters) {
344 adapter = list_entry(item, struct i2c_adapter, list);
345 driver->attach_adapter(adapter);
346 }
347 }
348
Arjan van de Venb3585e42006-01-11 10:50:26 +0100349 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800352EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354int i2c_del_driver(struct i2c_driver *driver)
355{
356 struct list_head *item1, *item2, *_n;
357 struct i2c_client *client;
358 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int res = 0;
361
Arjan van de Venb3585e42006-01-11 10:50:26 +0100362 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100365 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
368 list_for_each(item1,&adapters) {
369 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (driver->detach_adapter) {
371 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200372 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100373 "for driver [%s]\n",
374 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 goto out_unlock;
376 }
377 } else {
378 list_for_each_safe(item2, _n, &adap->clients) {
379 client = list_entry(item2, struct i2c_client, list);
380 if (client->driver != driver)
381 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200382 dev_dbg(&adap->dev, "detaching client [%s] "
383 "at 0x%02x\n", client->name,
384 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200386 dev_err(&adap->dev, "detach_client "
387 "failed for client [%s] at "
388 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 client->addr);
390 goto out_unlock;
391 }
392 }
393 }
394 }
395
396 driver_unregister(&driver->driver);
397 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100398 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100401 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403}
404
405static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
406{
407 struct list_head *item;
408 struct i2c_client *client;
409
410 list_for_each(item,&adapter->clients) {
411 client = list_entry(item, struct i2c_client, list);
412 if (client->addr == addr)
413 return -EBUSY;
414 }
415 return 0;
416}
417
418int i2c_check_addr(struct i2c_adapter *adapter, int addr)
419{
420 int rval;
421
Ingo Molnar5c085d32006-01-18 23:16:04 +0100422 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100424 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return rval;
427}
428
429int i2c_attach_client(struct i2c_client *client)
430{
431 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200432 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Ingo Molnar5c085d32006-01-18 23:16:04 +0100434 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200436 res = -EBUSY;
437 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100440
Jean Delvarecde78592005-11-26 21:00:54 +0100441 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 client->dev.parent = &client->adapter->dev;
444 client->dev.driver = &client->driver->driver;
445 client->dev.bus = &i2c_bus_type;
446 client->dev.release = &i2c_client_release;
David Brownell438d6c22006-12-10 21:21:31 +0100447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
449 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200450 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
451 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200452 res = device_register(&client->dev);
453 if (res)
454 goto out_list;
455 res = device_create_file(&client->dev, &dev_attr_client_name);
456 if (res)
457 goto out_unregister;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200458 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200459
460 if (adapter->client_register) {
461 if (adapter->client_register(client)) {
462 dev_dbg(&adapter->dev, "client_register "
463 "failed for client [%s] at 0x%02x\n",
464 client->name, client->addr);
465 }
466 }
467
468 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200469
470out_unregister:
471 init_completion(&client->released); /* Needed? */
472 device_unregister(&client->dev);
473 wait_for_completion(&client->released);
474out_list:
475 list_del(&client->list);
476 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
477 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200478out_unlock:
479 mutex_unlock(&adapter->clist_lock);
480 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483
484int i2c_detach_client(struct i2c_client *client)
485{
486 struct i2c_adapter *adapter = client->adapter;
487 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100488
Jean Delvarecde78592005-11-26 21:00:54 +0100489 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200490 dev_warn(&client->dev, "Client [%s] still busy, "
491 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (adapter->client_unregister) {
496 res = adapter->client_unregister(client);
497 if (res) {
498 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700499 "client_unregister [%s] failed, "
500 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto out;
502 }
503 }
504
Ingo Molnar5c085d32006-01-18 23:16:04 +0100505 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 list_del(&client->list);
507 init_completion(&client->released);
508 device_remove_file(&client->dev, &dev_attr_client_name);
509 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100510 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 wait_for_completion(&client->released);
512
513 out:
514 return res;
515}
516
517static int i2c_inc_use_client(struct i2c_client *client)
518{
519
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100520 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return -ENODEV;
522 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100523 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return -ENODEV;
525 }
526
527 return 0;
528}
529
530static void i2c_dec_use_client(struct i2c_client *client)
531{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100532 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 module_put(client->adapter->owner);
534}
535
536int i2c_use_client(struct i2c_client *client)
537{
538 int ret;
539
540 ret = i2c_inc_use_client(client);
541 if (ret)
542 return ret;
543
Jean Delvarecde78592005-11-26 21:00:54 +0100544 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549int i2c_release_client(struct i2c_client *client)
550{
Jean Delvarecde78592005-11-26 21:00:54 +0100551 if (!client->usage_count) {
552 pr_debug("i2c-core: %s used one too many times\n",
553 __FUNCTION__);
554 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
David Brownell438d6c22006-12-10 21:21:31 +0100556
Jean Delvarecde78592005-11-26 21:00:54 +0100557 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return 0;
561}
562
563void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
564{
565 struct list_head *item;
566 struct i2c_client *client;
567
Ingo Molnar5c085d32006-01-18 23:16:04 +0100568 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 list_for_each(item,&adap->clients) {
570 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100571 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 continue;
573 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100574 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100576 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100578 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100580 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583static int __init i2c_init(void)
584{
585 int retval;
586
587 retval = bus_register(&i2c_bus_type);
588 if (retval)
589 return retval;
590 retval = driver_register(&i2c_adapter_driver);
591 if (retval)
592 return retval;
593 return class_register(&i2c_adapter_class);
594}
595
596static void __exit i2c_exit(void)
597{
598 class_unregister(&i2c_adapter_class);
599 driver_unregister(&i2c_adapter_driver);
600 bus_unregister(&i2c_bus_type);
601}
602
603subsys_initcall(i2c_init);
604module_exit(i2c_exit);
605
606/* ----------------------------------------------------
607 * the functional interface to the i2c busses.
608 * ----------------------------------------------------
609 */
610
611int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
612{
613 int ret;
614
615 if (adap->algo->master_xfer) {
616#ifdef DEBUG
617 for (ret = 0; ret < num; ret++) {
618 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
619 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
620 'R' : 'W', msgs[ret].addr, msgs[ret].len);
621 }
622#endif
623
Jiri Kosina6ea23032006-12-10 21:21:30 +0100624 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100626 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 return ret;
629 } else {
630 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
631 return -ENOSYS;
632 }
633}
634
635int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
636{
637 int ret;
638 struct i2c_adapter *adap=client->adapter;
639 struct i2c_msg msg;
640
Jean Delvare815f55f2005-05-07 22:58:46 +0200641 msg.addr = client->addr;
642 msg.flags = client->flags & I2C_M_TEN;
643 msg.len = count;
644 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100645
Jean Delvare815f55f2005-05-07 22:58:46 +0200646 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Jean Delvare815f55f2005-05-07 22:58:46 +0200648 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
649 transmitted, else error code. */
650 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
654{
655 struct i2c_adapter *adap=client->adapter;
656 struct i2c_msg msg;
657 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Jean Delvare815f55f2005-05-07 22:58:46 +0200659 msg.addr = client->addr;
660 msg.flags = client->flags & I2C_M_TEN;
661 msg.flags |= I2C_M_RD;
662 msg.len = count;
663 msg.buf = buf;
664
665 ret = i2c_transfer(adap, &msg, 1);
666
667 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
668 transmitted, else error code. */
669 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672
673int i2c_control(struct i2c_client *client,
674 unsigned int cmd, unsigned long arg)
675{
676 int ret = 0;
677 struct i2c_adapter *adap = client->adapter;
678
679 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
680 switch (cmd) {
681 case I2C_RETRIES:
682 adap->retries = arg;
683 break;
684 case I2C_TIMEOUT:
685 adap->timeout = arg;
686 break;
687 default:
688 if (adap->algo->algo_control!=NULL)
689 ret = adap->algo->algo_control(adap,cmd,arg);
690 }
691 return ret;
692}
693
694/* ----------------------------------------------------
695 * the i2c address scanning function
696 * Will not work for 10-bit addresses!
697 * ----------------------------------------------------
698 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200699static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
700 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200701{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200702 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200703
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200704 /* Make sure the address is valid */
705 if (addr < 0x03 || addr > 0x77) {
706 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
707 addr);
708 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200709 }
710
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200711 /* Skip if already in use */
712 if (i2c_check_addr(adapter, addr))
713 return 0;
714
715 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200716 if (kind < 0) {
717 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
718 I2C_SMBUS_QUICK, NULL) < 0)
719 return 0;
720
721 /* prevent 24RF08 corruption */
722 if ((addr & ~0x0f) == 0x50)
723 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
724 I2C_SMBUS_QUICK, NULL);
725 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200726
727 /* Finally call the custom detection function */
728 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200729 /* -ENODEV can be returned if there is a chip at the given address
730 but it isn't supported by this chip driver. We catch it here as
731 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200732 if (err == -ENODEV)
733 err = 0;
734
735 if (err)
736 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
737 addr, err);
738 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741int i2c_probe(struct i2c_adapter *adapter,
742 struct i2c_client_address_data *address_data,
743 int (*found_proc) (struct i2c_adapter *, int, int))
744{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200745 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int adap_id = i2c_adapter_id(adapter);
747
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200748 /* Force entries are done first, and are not affected by ignore
749 entries */
750 if (address_data->forces) {
751 unsigned short **forces = address_data->forces;
752 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200754 for (kind = 0; forces[kind]; kind++) {
755 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
756 i += 2) {
757 if (forces[kind][i] == adap_id
758 || forces[kind][i] == ANY_I2C_BUS) {
759 dev_dbg(&adapter->dev, "found force "
760 "parameter for adapter %d, "
761 "addr 0x%02x, kind %d\n",
762 adap_id, forces[kind][i + 1],
763 kind);
764 err = i2c_probe_address(adapter,
765 forces[kind][i + 1],
766 kind, found_proc);
767 if (err)
768 return err;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200773
Jean Delvare4366dc92005-09-25 16:50:06 +0200774 /* Stop here if we can't use SMBUS_QUICK */
775 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
776 if (address_data->probe[0] == I2C_CLIENT_END
777 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100778 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200779
780 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
781 "can't probe for chips\n");
782 return -1;
783 }
784
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200785 /* Probe entries are done second, and are not affected by ignore
786 entries either */
787 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
788 if (address_data->probe[i] == adap_id
789 || address_data->probe[i] == ANY_I2C_BUS) {
790 dev_dbg(&adapter->dev, "found probe parameter for "
791 "adapter %d, addr 0x%02x\n", adap_id,
792 address_data->probe[i + 1]);
793 err = i2c_probe_address(adapter,
794 address_data->probe[i + 1],
795 -1, found_proc);
796 if (err)
797 return err;
798 }
799 }
800
801 /* Normal entries are done last, unless shadowed by an ignore entry */
802 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
803 int j, ignore;
804
805 ignore = 0;
806 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
807 j += 2) {
808 if ((address_data->ignore[j] == adap_id ||
809 address_data->ignore[j] == ANY_I2C_BUS)
810 && address_data->ignore[j + 1]
811 == address_data->normal_i2c[i]) {
812 dev_dbg(&adapter->dev, "found ignore "
813 "parameter for adapter %d, "
814 "addr 0x%02x\n", adap_id,
815 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +0200816 ignore = 1;
817 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200818 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200819 }
820 if (ignore)
821 continue;
822
823 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
824 "addr 0x%02x\n", adap_id,
825 address_data->normal_i2c[i]);
826 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
827 -1, found_proc);
828 if (err)
829 return err;
830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835struct i2c_adapter* i2c_get_adapter(int id)
836{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +0100838
Arjan van de Venb3585e42006-01-11 10:50:26 +0100839 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400840 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
841 if (adapter && !try_module_get(adapter->owner))
842 adapter = NULL;
843
Arjan van de Venb3585e42006-01-11 10:50:26 +0100844 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400845 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848void i2c_put_adapter(struct i2c_adapter *adap)
849{
850 module_put(adap->owner);
851}
852
853/* The SMBus parts */
854
David Brownell438d6c22006-12-10 21:21:31 +0100855#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856static u8
857crc8(u16 data)
858{
859 int i;
David Brownell438d6c22006-12-10 21:21:31 +0100860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +0100862 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 data = data ^ POLY;
864 data = data << 1;
865 }
866 return (u8)(data >> 8);
867}
868
Jean Delvare421ef472005-10-26 21:28:55 +0200869/* Incremental CRC8 over count bytes in the array pointed to by p */
870static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int i;
873
874 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200875 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return crc;
877}
878
Jean Delvare421ef472005-10-26 21:28:55 +0200879/* Assume a 7-bit address, which is reasonable for SMBus */
880static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jean Delvare421ef472005-10-26 21:28:55 +0200882 /* The address will be sent first */
883 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
884 pec = i2c_smbus_pec(pec, &addr, 1);
885
886 /* The data buffer follows */
887 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Jean Delvare421ef472005-10-26 21:28:55 +0200890/* Used for write only transactions */
891static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Jean Delvare421ef472005-10-26 21:28:55 +0200893 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
894 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Jean Delvare421ef472005-10-26 21:28:55 +0200897/* Return <0 on CRC error
898 If there was a write before this read (most cases) we need to take the
899 partial CRC from the write part into account.
900 Note that this function does modify the message (we need to decrease the
901 message length to hide the CRC byte from the caller). */
902static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Jean Delvare421ef472005-10-26 21:28:55 +0200904 u8 rpec = msg->buf[--msg->len];
905 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (rpec != cpec) {
908 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
909 rpec, cpec);
910 return -1;
911 }
David Brownell438d6c22006-12-10 21:21:31 +0100912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
916{
917 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +0100918 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921s32 i2c_smbus_read_byte(struct i2c_client *client)
922{
923 union i2c_smbus_data data;
924 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
925 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
926 return -1;
927 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200928 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
932{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200934 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
938{
939 union i2c_smbus_data data;
940 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
941 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
942 return -1;
943 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200944 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
948{
949 union i2c_smbus_data data;
950 data.byte = value;
951 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
952 I2C_SMBUS_WRITE,command,
953 I2C_SMBUS_BYTE_DATA,&data);
954}
955
956s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
957{
958 union i2c_smbus_data data;
959 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
960 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
961 return -1;
962 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200963 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
967{
968 union i2c_smbus_data data;
969 data.word = value;
970 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
971 I2C_SMBUS_WRITE,command,
972 I2C_SMBUS_WORD_DATA,&data);
973}
974
975s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200976 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (length > I2C_SMBUS_BLOCK_MAX)
981 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +0100983 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
985 I2C_SMBUS_WRITE,command,
986 I2C_SMBUS_BLOCK_DATA,&data);
987}
988
989/* Returns the number of read bytes */
990s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
991{
992 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
995 I2C_SMBUS_READ,command,
996 I2C_SMBUS_I2C_BLOCK_DATA,&data))
997 return -1;
Jean Delvare76560322006-01-18 23:14:55 +0100998
999 memcpy(values, &data.block[1], data.block[0]);
1000 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Jean Delvare21bbd692006-01-09 15:19:18 +11001003s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001004 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001005{
1006 union i2c_smbus_data data;
1007
1008 if (length > I2C_SMBUS_BLOCK_MAX)
1009 length = I2C_SMBUS_BLOCK_MAX;
1010 data.block[0] = length;
1011 memcpy(data.block + 1, values, length);
1012 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1013 I2C_SMBUS_WRITE, command,
1014 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1015}
1016
David Brownell438d6c22006-12-10 21:21:31 +01001017/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001019static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001021 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 union i2c_smbus_data * data)
1023{
1024 /* So we need to generate a series of msgs. In the case of writing, we
1025 need to use only one message; when reading, we need two. We initialize
1026 most things with sane defaults, to keep the code below somewhat
1027 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001028 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1029 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001031 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1033 };
1034 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001035 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 msgbuf0[0] = command;
1038 switch(size) {
1039 case I2C_SMBUS_QUICK:
1040 msg[0].len = 0;
1041 /* Special case: The read/write field is used as data */
1042 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1043 num = 1;
1044 break;
1045 case I2C_SMBUS_BYTE:
1046 if (read_write == I2C_SMBUS_READ) {
1047 /* Special case: only a read! */
1048 msg[0].flags = I2C_M_RD | flags;
1049 num = 1;
1050 }
1051 break;
1052 case I2C_SMBUS_BYTE_DATA:
1053 if (read_write == I2C_SMBUS_READ)
1054 msg[1].len = 1;
1055 else {
1056 msg[0].len = 2;
1057 msgbuf0[1] = data->byte;
1058 }
1059 break;
1060 case I2C_SMBUS_WORD_DATA:
1061 if (read_write == I2C_SMBUS_READ)
1062 msg[1].len = 2;
1063 else {
1064 msg[0].len=3;
1065 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001066 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 break;
1069 case I2C_SMBUS_PROC_CALL:
1070 num = 2; /* Special case */
1071 read_write = I2C_SMBUS_READ;
1072 msg[0].len = 3;
1073 msg[1].len = 2;
1074 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001075 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 break;
1077 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (read_write == I2C_SMBUS_READ) {
1079 dev_err(&adapter->dev, "Block read not supported "
1080 "under I2C emulation!\n");
1081 return -1;
1082 } else {
1083 msg[0].len = data->block[0] + 2;
1084 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1085 dev_err(&adapter->dev, "smbus_access called with "
1086 "invalid block write size (%d)\n",
1087 data->block[0]);
1088 return -1;
1089 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001090 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 msgbuf0[i] = data->block[i-1];
1092 }
1093 break;
1094 case I2C_SMBUS_BLOCK_PROC_CALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 dev_dbg(&adapter->dev, "Block process call not supported "
1096 "under I2C emulation!\n");
1097 return -1;
1098 case I2C_SMBUS_I2C_BLOCK_DATA:
1099 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001100 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 } else {
1102 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001103 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1105 "invalid block write size (%d)\n",
1106 data->block[0]);
1107 return -1;
1108 }
1109 for (i = 1; i <= data->block[0]; i++)
1110 msgbuf0[i] = data->block[i];
1111 }
1112 break;
1113 default:
1114 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1115 size);
1116 return -1;
1117 }
1118
Jean Delvare421ef472005-10-26 21:28:55 +02001119 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1120 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1121 if (i) {
1122 /* Compute PEC if first message is a write */
1123 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001124 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001125 i2c_smbus_add_pec(&msg[0]);
1126 else /* Write followed by read */
1127 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1128 }
1129 /* Ask for PEC if last message is a read */
1130 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001131 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001132 }
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (i2c_transfer(adapter, msg, num) < 0)
1135 return -1;
1136
Jean Delvare421ef472005-10-26 21:28:55 +02001137 /* Check PEC if last message is a read */
1138 if (i && (msg[num-1].flags & I2C_M_RD)) {
1139 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1140 return -1;
1141 }
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (read_write == I2C_SMBUS_READ)
1144 switch(size) {
1145 case I2C_SMBUS_BYTE:
1146 data->byte = msgbuf0[0];
1147 break;
1148 case I2C_SMBUS_BYTE_DATA:
1149 data->byte = msgbuf1[0];
1150 break;
David Brownell438d6c22006-12-10 21:21:31 +01001151 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 case I2C_SMBUS_PROC_CALL:
1153 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1154 break;
1155 case I2C_SMBUS_I2C_BLOCK_DATA:
1156 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001157 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1158 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 data->block[i+1] = msgbuf1[i];
1160 break;
1161 }
1162 return 0;
1163}
1164
1165
1166s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001167 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 union i2c_smbus_data * data)
1169{
1170 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001175 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1177 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001178 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 } else
1180 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1181 command,size,data);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return res;
1184}
1185
1186
Jean Delvareefde7232005-07-20 23:03:50 +02001187/* Next four are needed by i2c-isa */
1188EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1189EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1190EXPORT_SYMBOL_GPL(i2c_adapter_class);
1191EXPORT_SYMBOL_GPL(i2c_bus_type);
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193EXPORT_SYMBOL(i2c_add_adapter);
1194EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195EXPORT_SYMBOL(i2c_del_driver);
1196EXPORT_SYMBOL(i2c_attach_client);
1197EXPORT_SYMBOL(i2c_detach_client);
1198EXPORT_SYMBOL(i2c_use_client);
1199EXPORT_SYMBOL(i2c_release_client);
1200EXPORT_SYMBOL(i2c_clients_command);
1201EXPORT_SYMBOL(i2c_check_addr);
1202
1203EXPORT_SYMBOL(i2c_master_send);
1204EXPORT_SYMBOL(i2c_master_recv);
1205EXPORT_SYMBOL(i2c_control);
1206EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207EXPORT_SYMBOL(i2c_get_adapter);
1208EXPORT_SYMBOL(i2c_put_adapter);
1209EXPORT_SYMBOL(i2c_probe);
1210
1211EXPORT_SYMBOL(i2c_smbus_xfer);
1212EXPORT_SYMBOL(i2c_smbus_write_quick);
1213EXPORT_SYMBOL(i2c_smbus_read_byte);
1214EXPORT_SYMBOL(i2c_smbus_write_byte);
1215EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1216EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1217EXPORT_SYMBOL(i2c_smbus_read_word_data);
1218EXPORT_SYMBOL(i2c_smbus_write_word_data);
1219EXPORT_SYMBOL(i2c_smbus_write_block_data);
1220EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001221EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1224MODULE_DESCRIPTION("I2C-Bus main module");
1225MODULE_LICENSE("GPL");