blob: 19d8a994b3b7c6917618db3f46b28360a3e376fc [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>
22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/errno.h>
27#include <linux/slab.h>
28#include <linux/i2c.h>
29#include <linux/init.h>
30#include <linux/idr.h>
31#include <linux/seq_file.h>
32#include <asm/uaccess.h>
33
34
35static LIST_HEAD(adapters);
36static LIST_HEAD(drivers);
37static DECLARE_MUTEX(core_lists);
38static DEFINE_IDR(i2c_adapter_idr);
39
40/* match always succeeds, as we want the probe() to tell if we really accept this match */
41static int i2c_device_match(struct device *dev, struct device_driver *drv)
42{
43 return 1;
44}
45
46static int i2c_bus_suspend(struct device * dev, pm_message_t state)
47{
48 int rc = 0;
49
50 if (dev->driver && dev->driver->suspend)
51 rc = dev->driver->suspend(dev,state,0);
52 return rc;
53}
54
55static int i2c_bus_resume(struct device * dev)
56{
57 int rc = 0;
58
59 if (dev->driver && dev->driver->resume)
60 rc = dev->driver->resume(dev,0);
61 return rc;
62}
63
Jean Delvareefde7232005-07-20 23:03:50 +020064struct bus_type i2c_bus_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .name = "i2c",
66 .match = i2c_device_match,
67 .suspend = i2c_bus_suspend,
68 .resume = i2c_bus_resume,
69};
70
71static int i2c_device_probe(struct device *dev)
72{
73 return -ENODEV;
74}
75
76static int i2c_device_remove(struct device *dev)
77{
78 return 0;
79}
80
Jean Delvareefde7232005-07-20 23:03:50 +020081void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
84 complete(&adap->dev_released);
85}
86
Jean Delvareefde7232005-07-20 23:03:50 +020087struct device_driver i2c_adapter_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 .name = "i2c_adapter",
89 .bus = &i2c_bus_type,
90 .probe = i2c_device_probe,
91 .remove = i2c_device_remove,
92};
93
94static void i2c_adapter_class_dev_release(struct class_device *dev)
95{
96 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
97 complete(&adap->class_dev_released);
98}
99
Jean Delvareefde7232005-07-20 23:03:50 +0200100struct class i2c_adapter_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .name = "i2c-adapter",
102 .release = &i2c_adapter_class_dev_release,
103};
104
Yani Ioannoue404e272005-05-17 06:42:58 -0400105static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
108 return sprintf(buf, "%s\n", adap->name);
109}
110static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
111
112
113static void i2c_client_release(struct device *dev)
114{
115 struct i2c_client *client = to_i2c_client(dev);
116 complete(&client->released);
117}
118
Yani Ioannoue404e272005-05-17 06:42:58 -0400119static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct i2c_client *client = to_i2c_client(dev);
122 return sprintf(buf, "%s\n", client->name);
123}
124
125/*
126 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
127 * different type of a device. So beware if the DEVICE_ATTR() macro ever
128 * changes, this definition will also have to change.
129 */
130static struct device_attribute dev_attr_client_name = {
131 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
132 .show = &show_client_name,
133};
134
135
136/* ---------------------------------------------------
137 * registering functions
138 * ---------------------------------------------------
139 */
140
141/* -----
142 * i2c_add_adapter is called from within the algorithm layer,
143 * when a new hw adapter registers. A new device is register to be
144 * available for clients.
145 */
146int i2c_add_adapter(struct i2c_adapter *adap)
147{
148 int id, res = 0;
149 struct list_head *item;
150 struct i2c_driver *driver;
151
152 down(&core_lists);
153
154 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
155 res = -ENOMEM;
156 goto out_unlock;
157 }
158
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400159 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (res < 0) {
161 if (res == -EAGAIN)
162 res = -ENOMEM;
163 goto out_unlock;
164 }
165
166 adap->nr = id & MAX_ID_MASK;
167 init_MUTEX(&adap->bus_lock);
168 init_MUTEX(&adap->clist_lock);
169 list_add_tail(&adap->list,&adapters);
170 INIT_LIST_HEAD(&adap->clients);
171
172 /* Add the adapter to the driver core.
173 * If the parent pointer is not set up,
174 * we add this adapter to the host bus.
175 */
176 if (adap->dev.parent == NULL)
177 adap->dev.parent = &platform_bus;
178 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
179 adap->dev.driver = &i2c_adapter_driver;
180 adap->dev.release = &i2c_adapter_dev_release;
181 device_register(&adap->dev);
182 device_create_file(&adap->dev, &dev_attr_name);
183
184 /* Add this adapter to the i2c_adapter class */
185 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
186 adap->class_dev.dev = &adap->dev;
187 adap->class_dev.class = &i2c_adapter_class;
188 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
189 class_device_register(&adap->class_dev);
190
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200191 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* inform drivers of new adapters */
194 list_for_each(item,&drivers) {
195 driver = list_entry(item, struct i2c_driver, list);
196 if (driver->flags & I2C_DF_NOTIFY)
197 /* We ignore the return code; if it fails, too bad */
198 driver->attach_adapter(adap);
199 }
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201out_unlock:
202 up(&core_lists);
203 return res;
204}
205
206
207int i2c_del_adapter(struct i2c_adapter *adap)
208{
209 struct list_head *item, *_n;
210 struct i2c_adapter *adap_from_list;
211 struct i2c_driver *driver;
212 struct i2c_client *client;
213 int res = 0;
214
215 down(&core_lists);
216
217 /* First make sure that this adapter was ever added */
218 list_for_each_entry(adap_from_list, &adapters, list) {
219 if (adap_from_list == adap)
220 break;
221 }
222 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200223 pr_debug("i2c-core: attempting to delete unregistered "
224 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 res = -EINVAL;
226 goto out_unlock;
227 }
228
229 list_for_each(item,&drivers) {
230 driver = list_entry(item, struct i2c_driver, list);
231 if (driver->detach_adapter)
232 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200233 dev_err(&adap->dev, "detach_adapter failed "
234 "for driver [%s]\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 goto out_unlock;
236 }
237 }
238
239 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200240 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 list_for_each_safe(item, _n, &adap->clients) {
242 client = list_entry(item, struct i2c_client, list);
243
244 /* detaching devices is unconditional of the set notify
245 * flag, as _all_ clients that reside on the adapter
246 * must be deleted, as this would cause invalid states.
247 */
248 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200249 dev_err(&adap->dev, "detach_client failed for client "
250 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 client->addr);
252 goto out_unlock;
253 }
254 }
255
256 /* clean up the sysfs representation */
257 init_completion(&adap->dev_released);
258 init_completion(&adap->class_dev_released);
259 class_device_unregister(&adap->class_dev);
260 device_remove_file(&adap->dev, &dev_attr_name);
261 device_unregister(&adap->dev);
262 list_del(&adap->list);
263
264 /* wait for sysfs to drop all references */
265 wait_for_completion(&adap->dev_released);
266 wait_for_completion(&adap->class_dev_released);
267
268 /* free dynamically allocated bus id */
269 idr_remove(&i2c_adapter_idr, adap->nr);
270
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200271 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 out_unlock:
274 up(&core_lists);
275 return res;
276}
277
278
279/* -----
280 * What follows is the "upwards" interface: commands for talking to clients,
281 * which implement the functions to access the physical information of the
282 * chips.
283 */
284
285int i2c_add_driver(struct i2c_driver *driver)
286{
287 struct list_head *item;
288 struct i2c_adapter *adapter;
289 int res = 0;
290
291 down(&core_lists);
292
293 /* add the driver to the list of i2c drivers in the driver core */
294 driver->driver.name = driver->name;
295 driver->driver.bus = &i2c_bus_type;
296 driver->driver.probe = i2c_device_probe;
297 driver->driver.remove = i2c_device_remove;
298
299 res = driver_register(&driver->driver);
300 if (res)
301 goto out_unlock;
302
303 list_add_tail(&driver->list,&drivers);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200304 pr_debug("i2c-core: driver [%s] registered\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* now look for instances of driver on our adapters */
307 if (driver->flags & I2C_DF_NOTIFY) {
308 list_for_each(item,&adapters) {
309 adapter = list_entry(item, struct i2c_adapter, list);
310 driver->attach_adapter(adapter);
311 }
312 }
313
314 out_unlock:
315 up(&core_lists);
316 return res;
317}
318
319int i2c_del_driver(struct i2c_driver *driver)
320{
321 struct list_head *item1, *item2, *_n;
322 struct i2c_client *client;
323 struct i2c_adapter *adap;
324
325 int res = 0;
326
327 down(&core_lists);
328
329 /* Have a look at each adapter, if clients of this driver are still
330 * attached. If so, detach them to be able to kill the driver
331 * afterwards.
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200332 *
333 * Removing clients does not depend on the notify flag, else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * invalid operation might (will!) result, when using stale client
335 * pointers.
336 */
337 list_for_each(item1,&adapters) {
338 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (driver->detach_adapter) {
340 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200341 dev_err(&adap->dev, "detach_adapter failed "
342 "for driver [%s]\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto out_unlock;
344 }
345 } else {
346 list_for_each_safe(item2, _n, &adap->clients) {
347 client = list_entry(item2, struct i2c_client, list);
348 if (client->driver != driver)
349 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200350 dev_dbg(&adap->dev, "detaching client [%s] "
351 "at 0x%02x\n", client->name,
352 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200354 dev_err(&adap->dev, "detach_client "
355 "failed for client [%s] at "
356 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 client->addr);
358 goto out_unlock;
359 }
360 }
361 }
362 }
363
364 driver_unregister(&driver->driver);
365 list_del(&driver->list);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200366 pr_debug("i2c-core: driver [%s] unregistered\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 out_unlock:
369 up(&core_lists);
370 return 0;
371}
372
373static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
374{
375 struct list_head *item;
376 struct i2c_client *client;
377
378 list_for_each(item,&adapter->clients) {
379 client = list_entry(item, struct i2c_client, list);
380 if (client->addr == addr)
381 return -EBUSY;
382 }
383 return 0;
384}
385
386int i2c_check_addr(struct i2c_adapter *adapter, int addr)
387{
388 int rval;
389
390 down(&adapter->clist_lock);
391 rval = __i2c_check_addr(adapter, addr);
392 up(&adapter->clist_lock);
393
394 return rval;
395}
396
397int i2c_attach_client(struct i2c_client *client)
398{
399 struct i2c_adapter *adapter = client->adapter;
400
401 down(&adapter->clist_lock);
402 if (__i2c_check_addr(client->adapter, client->addr)) {
403 up(&adapter->clist_lock);
404 return -EBUSY;
405 }
406 list_add_tail(&client->list,&adapter->clients);
407 up(&adapter->clist_lock);
408
409 if (adapter->client_register) {
410 if (adapter->client_register(client)) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200411 dev_dbg(&adapter->dev, "client_register "
412 "failed for client [%s] at 0x%02x\n",
413 client->name, client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 }
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (client->flags & I2C_CLIENT_ALLOW_USE)
418 client->usage_count = 0;
419
420 client->dev.parent = &client->adapter->dev;
421 client->dev.driver = &client->driver->driver;
422 client->dev.bus = &i2c_bus_type;
423 client->dev.release = &i2c_client_release;
424
425 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
426 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200427 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
428 client->name, client->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 device_register(&client->dev);
430 device_create_file(&client->dev, &dev_attr_client_name);
431
432 return 0;
433}
434
435
436int i2c_detach_client(struct i2c_client *client)
437{
438 struct i2c_adapter *adapter = client->adapter;
439 int res = 0;
440
Jean Delvare7bef5592005-07-27 22:14:49 +0200441 if ((client->flags & I2C_CLIENT_ALLOW_USE)
442 && (client->usage_count > 0)) {
443 dev_warn(&client->dev, "Client [%s] still busy, "
444 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (adapter->client_unregister) {
449 res = adapter->client_unregister(client);
450 if (res) {
451 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700452 "client_unregister [%s] failed, "
453 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto out;
455 }
456 }
457
458 down(&adapter->clist_lock);
459 list_del(&client->list);
460 init_completion(&client->released);
461 device_remove_file(&client->dev, &dev_attr_client_name);
462 device_unregister(&client->dev);
463 up(&adapter->clist_lock);
464 wait_for_completion(&client->released);
465
466 out:
467 return res;
468}
469
470static int i2c_inc_use_client(struct i2c_client *client)
471{
472
473 if (!try_module_get(client->driver->owner))
474 return -ENODEV;
475 if (!try_module_get(client->adapter->owner)) {
476 module_put(client->driver->owner);
477 return -ENODEV;
478 }
479
480 return 0;
481}
482
483static void i2c_dec_use_client(struct i2c_client *client)
484{
485 module_put(client->driver->owner);
486 module_put(client->adapter->owner);
487}
488
489int i2c_use_client(struct i2c_client *client)
490{
491 int ret;
492
493 ret = i2c_inc_use_client(client);
494 if (ret)
495 return ret;
496
497 if (client->flags & I2C_CLIENT_ALLOW_USE) {
498 if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
499 client->usage_count++;
500 else if (client->usage_count > 0)
501 goto busy;
502 else
503 client->usage_count++;
504 }
505
506 return 0;
507 busy:
508 i2c_dec_use_client(client);
509 return -EBUSY;
510}
511
512int i2c_release_client(struct i2c_client *client)
513{
514 if(client->flags & I2C_CLIENT_ALLOW_USE) {
515 if(client->usage_count>0)
516 client->usage_count--;
517 else {
518 pr_debug("i2c-core: %s used one too many times\n",
519 __FUNCTION__);
520 return -EPERM;
521 }
522 }
523
524 i2c_dec_use_client(client);
525
526 return 0;
527}
528
529void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
530{
531 struct list_head *item;
532 struct i2c_client *client;
533
534 down(&adap->clist_lock);
535 list_for_each(item,&adap->clients) {
536 client = list_entry(item, struct i2c_client, list);
537 if (!try_module_get(client->driver->owner))
538 continue;
539 if (NULL != client->driver->command) {
540 up(&adap->clist_lock);
541 client->driver->command(client,cmd,arg);
542 down(&adap->clist_lock);
543 }
544 module_put(client->driver->owner);
545 }
546 up(&adap->clist_lock);
547}
548
549static int __init i2c_init(void)
550{
551 int retval;
552
553 retval = bus_register(&i2c_bus_type);
554 if (retval)
555 return retval;
556 retval = driver_register(&i2c_adapter_driver);
557 if (retval)
558 return retval;
559 return class_register(&i2c_adapter_class);
560}
561
562static void __exit i2c_exit(void)
563{
564 class_unregister(&i2c_adapter_class);
565 driver_unregister(&i2c_adapter_driver);
566 bus_unregister(&i2c_bus_type);
567}
568
569subsys_initcall(i2c_init);
570module_exit(i2c_exit);
571
572/* ----------------------------------------------------
573 * the functional interface to the i2c busses.
574 * ----------------------------------------------------
575 */
576
577int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
578{
579 int ret;
580
581 if (adap->algo->master_xfer) {
582#ifdef DEBUG
583 for (ret = 0; ret < num; ret++) {
584 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
585 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
586 'R' : 'W', msgs[ret].addr, msgs[ret].len);
587 }
588#endif
589
590 down(&adap->bus_lock);
591 ret = adap->algo->master_xfer(adap,msgs,num);
592 up(&adap->bus_lock);
593
594 return ret;
595 } else {
596 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
597 return -ENOSYS;
598 }
599}
600
601int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
602{
603 int ret;
604 struct i2c_adapter *adap=client->adapter;
605 struct i2c_msg msg;
606
Jean Delvare815f55f2005-05-07 22:58:46 +0200607 msg.addr = client->addr;
608 msg.flags = client->flags & I2C_M_TEN;
609 msg.len = count;
610 msg.buf = (char *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Jean Delvare815f55f2005-05-07 22:58:46 +0200612 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Jean Delvare815f55f2005-05-07 22:58:46 +0200614 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
615 transmitted, else error code. */
616 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
620{
621 struct i2c_adapter *adap=client->adapter;
622 struct i2c_msg msg;
623 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jean Delvare815f55f2005-05-07 22:58:46 +0200625 msg.addr = client->addr;
626 msg.flags = client->flags & I2C_M_TEN;
627 msg.flags |= I2C_M_RD;
628 msg.len = count;
629 msg.buf = buf;
630
631 ret = i2c_transfer(adap, &msg, 1);
632
633 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
634 transmitted, else error code. */
635 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638
639int i2c_control(struct i2c_client *client,
640 unsigned int cmd, unsigned long arg)
641{
642 int ret = 0;
643 struct i2c_adapter *adap = client->adapter;
644
645 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
646 switch (cmd) {
647 case I2C_RETRIES:
648 adap->retries = arg;
649 break;
650 case I2C_TIMEOUT:
651 adap->timeout = arg;
652 break;
653 default:
654 if (adap->algo->algo_control!=NULL)
655 ret = adap->algo->algo_control(adap,cmd,arg);
656 }
657 return ret;
658}
659
660/* ----------------------------------------------------
661 * the i2c address scanning function
662 * Will not work for 10-bit addresses!
663 * ----------------------------------------------------
664 */
665int i2c_probe(struct i2c_adapter *adapter,
666 struct i2c_client_address_data *address_data,
667 int (*found_proc) (struct i2c_adapter *, int, int))
668{
669 int addr,i,found,err;
670 int adap_id = i2c_adapter_id(adapter);
671
672 /* Forget it if we can't probe using SMBUS_QUICK */
673 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
674 return -1;
675
676 for (addr = 0x00; addr <= 0x7f; addr++) {
677
678 /* Skip if already in use */
679 if (i2c_check_addr(adapter,addr))
680 continue;
681
682 /* If it is in one of the force entries, we don't do any detection
683 at all */
684 found = 0;
685
686 for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) {
687 if (((adap_id == address_data->force[i]) ||
688 (address_data->force[i] == ANY_I2C_BUS)) &&
689 (addr == address_data->force[i+1])) {
690 dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n",
691 adap_id, addr);
692 if ((err = found_proc(adapter,addr,0)))
693 return err;
694 found = 1;
695 }
696 }
697 if (found)
698 continue;
699
700 /* If this address is in one of the ignores, we can forget about
701 it right now */
702 for (i = 0;
703 !found && (address_data->ignore[i] != I2C_CLIENT_END);
704 i += 2) {
705 if (((adap_id == address_data->ignore[i]) ||
706 ((address_data->ignore[i] == ANY_I2C_BUS))) &&
707 (addr == address_data->ignore[i+1])) {
708 dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
709 "addr %04x\n", adap_id ,addr);
710 found = 1;
711 }
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (found)
714 continue;
715
716 /* Now, we will do a detection, but only if it is in the normal or
717 probe entries */
718 for (i = 0;
719 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
720 i += 1) {
721 if (addr == address_data->normal_i2c[i]) {
722 found = 1;
723 dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
724 "addr %02x\n", adap_id, addr);
725 }
726 }
727
728 for (i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 !found && (address_data->probe[i] != I2C_CLIENT_END);
730 i += 2) {
731 if (((adap_id == address_data->probe[i]) ||
732 ((address_data->probe[i] == ANY_I2C_BUS))) &&
733 (addr == address_data->probe[i+1])) {
734 found = 1;
735 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
736 "addr %04x\n", adap_id,addr);
737 }
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!found)
740 continue;
741
742 /* OK, so we really should examine this address. First check
743 whether there is some client here at all! */
744 if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
745 if ((err = found_proc(adapter,addr,-1)))
746 return err;
747 }
748 return 0;
749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751struct i2c_adapter* i2c_get_adapter(int id)
752{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct i2c_adapter *adapter;
754
755 down(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400756 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
757 if (adapter && !try_module_get(adapter->owner))
758 adapter = NULL;
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 up(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400761 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764void i2c_put_adapter(struct i2c_adapter *adap)
765{
766 module_put(adap->owner);
767}
768
769/* The SMBus parts */
770
771#define POLY (0x1070U << 3)
772static u8
773crc8(u16 data)
774{
775 int i;
776
777 for(i = 0; i < 8; i++) {
778 if (data & 0x8000)
779 data = data ^ POLY;
780 data = data << 1;
781 }
782 return (u8)(data >> 8);
783}
784
785/* CRC over count bytes in the first array plus the bytes in the rest
786 array if it is non-null. rest[0] is the (length of rest) - 1
787 and is included. */
788static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
789{
790 int i;
791
792 for(i = 0; i < count; i++)
793 crc = crc8((crc ^ first[i]) << 8);
794 if(rest != NULL)
795 for(i = 0; i <= rest[0]; i++)
796 crc = crc8((crc ^ rest[i]) << 8);
797 return crc;
798}
799
800static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
801{
802 return i2c_smbus_partial_pec(0, count, first, rest);
803}
804
805/* Returns new "size" (transaction type)
806 Note that we convert byte to byte_data and byte_data to word_data
807 rather than invent new xxx_PEC transactions. */
808static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
809 union i2c_smbus_data *data)
810{
811 u8 buf[3];
812
813 buf[0] = addr << 1;
814 buf[1] = command;
815 switch(size) {
816 case I2C_SMBUS_BYTE:
817 data->byte = i2c_smbus_pec(2, buf, NULL);
818 size = I2C_SMBUS_BYTE_DATA;
819 break;
820 case I2C_SMBUS_BYTE_DATA:
821 buf[2] = data->byte;
822 data->word = buf[2] ||
823 (i2c_smbus_pec(3, buf, NULL) << 8);
824 size = I2C_SMBUS_WORD_DATA;
825 break;
826 case I2C_SMBUS_WORD_DATA:
827 /* unsupported */
828 break;
829 case I2C_SMBUS_BLOCK_DATA:
830 data->block[data->block[0] + 1] =
831 i2c_smbus_pec(2, buf, data->block);
832 size = I2C_SMBUS_BLOCK_DATA_PEC;
833 break;
834 }
835 return size;
836}
837
838static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
839 union i2c_smbus_data *data)
840{
841 u8 buf[3], rpec, cpec;
842
843 buf[1] = command;
844 switch(size) {
845 case I2C_SMBUS_BYTE_DATA:
846 buf[0] = (addr << 1) | 1;
847 cpec = i2c_smbus_pec(2, buf, NULL);
848 rpec = data->byte;
849 break;
850 case I2C_SMBUS_WORD_DATA:
851 buf[0] = (addr << 1) | 1;
852 buf[2] = data->word & 0xff;
853 cpec = i2c_smbus_pec(3, buf, NULL);
854 rpec = data->word >> 8;
855 break;
856 case I2C_SMBUS_WORD_DATA_PEC:
857 /* unsupported */
858 cpec = rpec = 0;
859 break;
860 case I2C_SMBUS_PROC_CALL_PEC:
861 /* unsupported */
862 cpec = rpec = 0;
863 break;
864 case I2C_SMBUS_BLOCK_DATA_PEC:
865 buf[0] = (addr << 1);
866 buf[2] = (addr << 1) | 1;
867 cpec = i2c_smbus_pec(3, buf, data->block);
868 rpec = data->block[data->block[0] + 1];
869 break;
870 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
871 buf[0] = (addr << 1) | 1;
872 rpec = i2c_smbus_partial_pec(partial, 1,
873 buf, data->block);
874 cpec = data->block[data->block[0] + 1];
875 break;
876 default:
877 cpec = rpec = 0;
878 break;
879 }
880 if (rpec != cpec) {
881 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
882 rpec, cpec);
883 return -1;
884 }
885 return 0;
886}
887
888s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
889{
890 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
891 value,0,I2C_SMBUS_QUICK,NULL);
892}
893
894s32 i2c_smbus_read_byte(struct i2c_client *client)
895{
896 union i2c_smbus_data data;
897 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
898 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
899 return -1;
900 else
901 return 0x0FF & data.byte;
902}
903
904s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
905{
906 union i2c_smbus_data data; /* only for PEC */
907 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
908 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
909}
910
911s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
912{
913 union i2c_smbus_data data;
914 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
915 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
916 return -1;
917 else
918 return 0x0FF & data.byte;
919}
920
921s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
922{
923 union i2c_smbus_data data;
924 data.byte = value;
925 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
926 I2C_SMBUS_WRITE,command,
927 I2C_SMBUS_BYTE_DATA,&data);
928}
929
930s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
931{
932 union i2c_smbus_data data;
933 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
934 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
935 return -1;
936 else
937 return 0x0FFFF & data.word;
938}
939
940s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
941{
942 union i2c_smbus_data data;
943 data.word = value;
944 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
945 I2C_SMBUS_WRITE,command,
946 I2C_SMBUS_WORD_DATA,&data);
947}
948
949s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
950 u8 length, u8 *values)
951{
952 union i2c_smbus_data data;
953 int i;
954 if (length > I2C_SMBUS_BLOCK_MAX)
955 length = I2C_SMBUS_BLOCK_MAX;
956 for (i = 1; i <= length; i++)
957 data.block[i] = values[i-1];
958 data.block[0] = length;
959 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
960 I2C_SMBUS_WRITE,command,
961 I2C_SMBUS_BLOCK_DATA,&data);
962}
963
964/* Returns the number of read bytes */
965s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
966{
967 union i2c_smbus_data data;
968 int i;
969 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
970 I2C_SMBUS_READ,command,
971 I2C_SMBUS_I2C_BLOCK_DATA,&data))
972 return -1;
973 else {
974 for (i = 1; i <= data.block[0]; i++)
975 values[i-1] = data.block[i];
976 return data.block[0];
977 }
978}
979
980/* Simulate a SMBus command using the i2c protocol
981 No checking of parameters is done! */
982static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
983 unsigned short flags,
984 char read_write, u8 command, int size,
985 union i2c_smbus_data * data)
986{
987 /* So we need to generate a series of msgs. In the case of writing, we
988 need to use only one message; when reading, we need two. We initialize
989 most things with sane defaults, to keep the code below somewhat
990 simpler. */
991 unsigned char msgbuf0[34];
992 unsigned char msgbuf1[34];
993 int num = read_write == I2C_SMBUS_READ?2:1;
994 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
995 { addr, flags | I2C_M_RD, 0, msgbuf1 }
996 };
997 int i;
998
999 msgbuf0[0] = command;
1000 switch(size) {
1001 case I2C_SMBUS_QUICK:
1002 msg[0].len = 0;
1003 /* Special case: The read/write field is used as data */
1004 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1005 num = 1;
1006 break;
1007 case I2C_SMBUS_BYTE:
1008 if (read_write == I2C_SMBUS_READ) {
1009 /* Special case: only a read! */
1010 msg[0].flags = I2C_M_RD | flags;
1011 num = 1;
1012 }
1013 break;
1014 case I2C_SMBUS_BYTE_DATA:
1015 if (read_write == I2C_SMBUS_READ)
1016 msg[1].len = 1;
1017 else {
1018 msg[0].len = 2;
1019 msgbuf0[1] = data->byte;
1020 }
1021 break;
1022 case I2C_SMBUS_WORD_DATA:
1023 if (read_write == I2C_SMBUS_READ)
1024 msg[1].len = 2;
1025 else {
1026 msg[0].len=3;
1027 msgbuf0[1] = data->word & 0xff;
1028 msgbuf0[2] = (data->word >> 8) & 0xff;
1029 }
1030 break;
1031 case I2C_SMBUS_PROC_CALL:
1032 num = 2; /* Special case */
1033 read_write = I2C_SMBUS_READ;
1034 msg[0].len = 3;
1035 msg[1].len = 2;
1036 msgbuf0[1] = data->word & 0xff;
1037 msgbuf0[2] = (data->word >> 8) & 0xff;
1038 break;
1039 case I2C_SMBUS_BLOCK_DATA:
1040 case I2C_SMBUS_BLOCK_DATA_PEC:
1041 if (read_write == I2C_SMBUS_READ) {
1042 dev_err(&adapter->dev, "Block read not supported "
1043 "under I2C emulation!\n");
1044 return -1;
1045 } else {
1046 msg[0].len = data->block[0] + 2;
1047 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1048 dev_err(&adapter->dev, "smbus_access called with "
1049 "invalid block write size (%d)\n",
1050 data->block[0]);
1051 return -1;
1052 }
1053 if(size == I2C_SMBUS_BLOCK_DATA_PEC)
1054 (msg[0].len)++;
1055 for (i = 1; i <= msg[0].len; i++)
1056 msgbuf0[i] = data->block[i-1];
1057 }
1058 break;
1059 case I2C_SMBUS_BLOCK_PROC_CALL:
1060 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
1061 dev_dbg(&adapter->dev, "Block process call not supported "
1062 "under I2C emulation!\n");
1063 return -1;
1064 case I2C_SMBUS_I2C_BLOCK_DATA:
1065 if (read_write == I2C_SMBUS_READ) {
1066 msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
1067 } else {
1068 msg[0].len = data->block[0] + 1;
1069 if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
1070 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1071 "invalid block write size (%d)\n",
1072 data->block[0]);
1073 return -1;
1074 }
1075 for (i = 1; i <= data->block[0]; i++)
1076 msgbuf0[i] = data->block[i];
1077 }
1078 break;
1079 default:
1080 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1081 size);
1082 return -1;
1083 }
1084
1085 if (i2c_transfer(adapter, msg, num) < 0)
1086 return -1;
1087
1088 if (read_write == I2C_SMBUS_READ)
1089 switch(size) {
1090 case I2C_SMBUS_BYTE:
1091 data->byte = msgbuf0[0];
1092 break;
1093 case I2C_SMBUS_BYTE_DATA:
1094 data->byte = msgbuf1[0];
1095 break;
1096 case I2C_SMBUS_WORD_DATA:
1097 case I2C_SMBUS_PROC_CALL:
1098 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1099 break;
1100 case I2C_SMBUS_I2C_BLOCK_DATA:
1101 /* fixed at 32 for now */
1102 data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
1103 for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
1104 data->block[i+1] = msgbuf1[i];
1105 break;
1106 }
1107 return 0;
1108}
1109
1110
1111s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1112 char read_write, u8 command, int size,
1113 union i2c_smbus_data * data)
1114{
1115 s32 res;
1116 int swpec = 0;
1117 u8 partial = 0;
1118
1119 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1120 if((flags & I2C_CLIENT_PEC) &&
1121 !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
1122 swpec = 1;
1123 if(read_write == I2C_SMBUS_READ &&
1124 size == I2C_SMBUS_BLOCK_DATA)
1125 size = I2C_SMBUS_BLOCK_DATA_PEC;
1126 else if(size == I2C_SMBUS_PROC_CALL)
1127 size = I2C_SMBUS_PROC_CALL_PEC;
1128 else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
1129 i2c_smbus_add_pec(addr, command,
1130 I2C_SMBUS_BLOCK_DATA, data);
1131 partial = data->block[data->block[0] + 1];
1132 size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
1133 } else if(read_write == I2C_SMBUS_WRITE &&
1134 size != I2C_SMBUS_QUICK &&
1135 size != I2C_SMBUS_I2C_BLOCK_DATA)
1136 size = i2c_smbus_add_pec(addr, command, size, data);
1137 }
1138
1139 if (adapter->algo->smbus_xfer) {
1140 down(&adapter->bus_lock);
1141 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1142 command,size,data);
1143 up(&adapter->bus_lock);
1144 } else
1145 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1146 command,size,data);
1147
1148 if(res >= 0 && swpec &&
1149 size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
1150 (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
1151 size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
1152 if(i2c_smbus_check_pec(addr, command, size, partial, data))
1153 return -1;
1154 }
1155 return res;
1156}
1157
1158
Jean Delvareefde7232005-07-20 23:03:50 +02001159/* Next four are needed by i2c-isa */
1160EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1161EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1162EXPORT_SYMBOL_GPL(i2c_adapter_class);
1163EXPORT_SYMBOL_GPL(i2c_bus_type);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165EXPORT_SYMBOL(i2c_add_adapter);
1166EXPORT_SYMBOL(i2c_del_adapter);
1167EXPORT_SYMBOL(i2c_add_driver);
1168EXPORT_SYMBOL(i2c_del_driver);
1169EXPORT_SYMBOL(i2c_attach_client);
1170EXPORT_SYMBOL(i2c_detach_client);
1171EXPORT_SYMBOL(i2c_use_client);
1172EXPORT_SYMBOL(i2c_release_client);
1173EXPORT_SYMBOL(i2c_clients_command);
1174EXPORT_SYMBOL(i2c_check_addr);
1175
1176EXPORT_SYMBOL(i2c_master_send);
1177EXPORT_SYMBOL(i2c_master_recv);
1178EXPORT_SYMBOL(i2c_control);
1179EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180EXPORT_SYMBOL(i2c_get_adapter);
1181EXPORT_SYMBOL(i2c_put_adapter);
1182EXPORT_SYMBOL(i2c_probe);
1183
1184EXPORT_SYMBOL(i2c_smbus_xfer);
1185EXPORT_SYMBOL(i2c_smbus_write_quick);
1186EXPORT_SYMBOL(i2c_smbus_read_byte);
1187EXPORT_SYMBOL(i2c_smbus_write_byte);
1188EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1189EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1190EXPORT_SYMBOL(i2c_smbus_read_word_data);
1191EXPORT_SYMBOL(i2c_smbus_write_word_data);
1192EXPORT_SYMBOL(i2c_smbus_write_block_data);
1193EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1194
1195MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1196MODULE_DESCRIPTION("I2C-Bus main module");
1197MODULE_LICENSE("GPL");