blob: b0bceb2fb8b1110b677a45ffb1dc964a2811eb37 [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
191 /* inform drivers of new adapters */
192 list_for_each(item,&drivers) {
193 driver = list_entry(item, struct i2c_driver, list);
194 if (driver->flags & I2C_DF_NOTIFY)
195 /* We ignore the return code; if it fails, too bad */
196 driver->attach_adapter(adap);
197 }
198
199 dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);
200
201out_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) {
223 pr_debug("I2C: Attempting to delete an unregistered "
224 "adapter\n");
225 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))) {
233 dev_warn(&adap->dev, "can't detach adapter "
Jean Delvare86749e82005-07-29 12:15:29 -0700234 "while detaching driver %s: driver "
235 "not detached!\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto out_unlock;
237 }
238 }
239
240 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200241 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 list_for_each_safe(item, _n, &adap->clients) {
243 client = list_entry(item, struct i2c_client, list);
244
245 /* detaching devices is unconditional of the set notify
246 * flag, as _all_ clients that reside on the adapter
247 * must be deleted, as this would cause invalid states.
248 */
249 if ((res=client->driver->detach_client(client))) {
250 dev_err(&adap->dev, "adapter not "
251 "unregistered, because client at "
252 "address %02x can't be detached. ",
253 client->addr);
254 goto out_unlock;
255 }
256 }
257
258 /* clean up the sysfs representation */
259 init_completion(&adap->dev_released);
260 init_completion(&adap->class_dev_released);
261 class_device_unregister(&adap->class_dev);
262 device_remove_file(&adap->dev, &dev_attr_name);
263 device_unregister(&adap->dev);
264 list_del(&adap->list);
265
266 /* wait for sysfs to drop all references */
267 wait_for_completion(&adap->dev_released);
268 wait_for_completion(&adap->class_dev_released);
269
270 /* free dynamically allocated bus id */
271 idr_remove(&i2c_adapter_idr, adap->nr);
272
273 dev_dbg(&adap->dev, "adapter unregistered\n");
274
275 out_unlock:
276 up(&core_lists);
277 return res;
278}
279
280
281/* -----
282 * What follows is the "upwards" interface: commands for talking to clients,
283 * which implement the functions to access the physical information of the
284 * chips.
285 */
286
287int i2c_add_driver(struct i2c_driver *driver)
288{
289 struct list_head *item;
290 struct i2c_adapter *adapter;
291 int res = 0;
292
293 down(&core_lists);
294
295 /* add the driver to the list of i2c drivers in the driver core */
296 driver->driver.name = driver->name;
297 driver->driver.bus = &i2c_bus_type;
298 driver->driver.probe = i2c_device_probe;
299 driver->driver.remove = i2c_device_remove;
300
301 res = driver_register(&driver->driver);
302 if (res)
303 goto out_unlock;
304
305 list_add_tail(&driver->list,&drivers);
306 pr_debug("i2c-core: driver %s registered.\n", driver->name);
307
308 /* now look for instances of driver on our adapters */
309 if (driver->flags & I2C_DF_NOTIFY) {
310 list_for_each(item,&adapters) {
311 adapter = list_entry(item, struct i2c_adapter, list);
312 driver->attach_adapter(adapter);
313 }
314 }
315
316 out_unlock:
317 up(&core_lists);
318 return res;
319}
320
321int i2c_del_driver(struct i2c_driver *driver)
322{
323 struct list_head *item1, *item2, *_n;
324 struct i2c_client *client;
325 struct i2c_adapter *adap;
326
327 int res = 0;
328
329 down(&core_lists);
330
331 /* Have a look at each adapter, if clients of this driver are still
332 * attached. If so, detach them to be able to kill the driver
333 * afterwards.
334 */
335 pr_debug("i2c-core: unregister_driver - looking for clients.\n");
336 /* removing clients does not depend on the notify flag, else
337 * invalid operation might (will!) result, when using stale client
338 * pointers.
339 */
340 list_for_each(item1,&adapters) {
341 adap = list_entry(item1, struct i2c_adapter, list);
342 dev_dbg(&adap->dev, "examining adapter\n");
343 if (driver->detach_adapter) {
344 if ((res = driver->detach_adapter(adap))) {
345 dev_warn(&adap->dev, "while unregistering "
346 "dummy driver %s, adapter could "
347 "not be detached properly; driver "
348 "not unloaded!",driver->name);
349 goto out_unlock;
350 }
351 } else {
352 list_for_each_safe(item2, _n, &adap->clients) {
353 client = list_entry(item2, struct i2c_client, list);
354 if (client->driver != driver)
355 continue;
356 pr_debug("i2c-core.o: detaching client %s:\n", client->name);
357 if ((res = driver->detach_client(client))) {
358 dev_err(&adap->dev, "while "
359 "unregistering driver "
360 "`%s', the client at "
361 "address %02x of "
362 "adapter could not "
363 "be detached; driver "
364 "not unloaded!",
365 driver->name,
366 client->addr);
367 goto out_unlock;
368 }
369 }
370 }
371 }
372
373 driver_unregister(&driver->driver);
374 list_del(&driver->list);
375 pr_debug("i2c-core: driver unregistered: %s\n", driver->name);
376
377 out_unlock:
378 up(&core_lists);
379 return 0;
380}
381
382static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
383{
384 struct list_head *item;
385 struct i2c_client *client;
386
387 list_for_each(item,&adapter->clients) {
388 client = list_entry(item, struct i2c_client, list);
389 if (client->addr == addr)
390 return -EBUSY;
391 }
392 return 0;
393}
394
395int i2c_check_addr(struct i2c_adapter *adapter, int addr)
396{
397 int rval;
398
399 down(&adapter->clist_lock);
400 rval = __i2c_check_addr(adapter, addr);
401 up(&adapter->clist_lock);
402
403 return rval;
404}
405
406int i2c_attach_client(struct i2c_client *client)
407{
408 struct i2c_adapter *adapter = client->adapter;
409
410 down(&adapter->clist_lock);
411 if (__i2c_check_addr(client->adapter, client->addr)) {
412 up(&adapter->clist_lock);
413 return -EBUSY;
414 }
415 list_add_tail(&client->list,&adapter->clients);
416 up(&adapter->clist_lock);
417
418 if (adapter->client_register) {
419 if (adapter->client_register(client)) {
420 dev_warn(&adapter->dev, "warning: client_register "
421 "seems to have failed for client %02x\n",
422 client->addr);
423 }
424 }
425
426 dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",
427 client->name);
428
429 if (client->flags & I2C_CLIENT_ALLOW_USE)
430 client->usage_count = 0;
431
432 client->dev.parent = &client->adapter->dev;
433 client->dev.driver = &client->driver->driver;
434 client->dev.bus = &i2c_bus_type;
435 client->dev.release = &i2c_client_release;
436
437 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
438 "%d-%04x", i2c_adapter_id(adapter), client->addr);
439 pr_debug("registering %s\n", client->dev.bus_id);
440 device_register(&client->dev);
441 device_create_file(&client->dev, &dev_attr_client_name);
442
443 return 0;
444}
445
446
447int i2c_detach_client(struct i2c_client *client)
448{
449 struct i2c_adapter *adapter = client->adapter;
450 int res = 0;
451
Jean Delvare7bef5592005-07-27 22:14:49 +0200452 if ((client->flags & I2C_CLIENT_ALLOW_USE)
453 && (client->usage_count > 0)) {
454 dev_warn(&client->dev, "Client [%s] still busy, "
455 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (adapter->client_unregister) {
460 res = adapter->client_unregister(client);
461 if (res) {
462 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700463 "client_unregister [%s] failed, "
464 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto out;
466 }
467 }
468
469 down(&adapter->clist_lock);
470 list_del(&client->list);
471 init_completion(&client->released);
472 device_remove_file(&client->dev, &dev_attr_client_name);
473 device_unregister(&client->dev);
474 up(&adapter->clist_lock);
475 wait_for_completion(&client->released);
476
477 out:
478 return res;
479}
480
481static int i2c_inc_use_client(struct i2c_client *client)
482{
483
484 if (!try_module_get(client->driver->owner))
485 return -ENODEV;
486 if (!try_module_get(client->adapter->owner)) {
487 module_put(client->driver->owner);
488 return -ENODEV;
489 }
490
491 return 0;
492}
493
494static void i2c_dec_use_client(struct i2c_client *client)
495{
496 module_put(client->driver->owner);
497 module_put(client->adapter->owner);
498}
499
500int i2c_use_client(struct i2c_client *client)
501{
502 int ret;
503
504 ret = i2c_inc_use_client(client);
505 if (ret)
506 return ret;
507
508 if (client->flags & I2C_CLIENT_ALLOW_USE) {
509 if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
510 client->usage_count++;
511 else if (client->usage_count > 0)
512 goto busy;
513 else
514 client->usage_count++;
515 }
516
517 return 0;
518 busy:
519 i2c_dec_use_client(client);
520 return -EBUSY;
521}
522
523int i2c_release_client(struct i2c_client *client)
524{
525 if(client->flags & I2C_CLIENT_ALLOW_USE) {
526 if(client->usage_count>0)
527 client->usage_count--;
528 else {
529 pr_debug("i2c-core: %s used one too many times\n",
530 __FUNCTION__);
531 return -EPERM;
532 }
533 }
534
535 i2c_dec_use_client(client);
536
537 return 0;
538}
539
540void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
541{
542 struct list_head *item;
543 struct i2c_client *client;
544
545 down(&adap->clist_lock);
546 list_for_each(item,&adap->clients) {
547 client = list_entry(item, struct i2c_client, list);
548 if (!try_module_get(client->driver->owner))
549 continue;
550 if (NULL != client->driver->command) {
551 up(&adap->clist_lock);
552 client->driver->command(client,cmd,arg);
553 down(&adap->clist_lock);
554 }
555 module_put(client->driver->owner);
556 }
557 up(&adap->clist_lock);
558}
559
560static int __init i2c_init(void)
561{
562 int retval;
563
564 retval = bus_register(&i2c_bus_type);
565 if (retval)
566 return retval;
567 retval = driver_register(&i2c_adapter_driver);
568 if (retval)
569 return retval;
570 return class_register(&i2c_adapter_class);
571}
572
573static void __exit i2c_exit(void)
574{
575 class_unregister(&i2c_adapter_class);
576 driver_unregister(&i2c_adapter_driver);
577 bus_unregister(&i2c_bus_type);
578}
579
580subsys_initcall(i2c_init);
581module_exit(i2c_exit);
582
583/* ----------------------------------------------------
584 * the functional interface to the i2c busses.
585 * ----------------------------------------------------
586 */
587
588int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
589{
590 int ret;
591
592 if (adap->algo->master_xfer) {
593#ifdef DEBUG
594 for (ret = 0; ret < num; ret++) {
595 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
596 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
597 'R' : 'W', msgs[ret].addr, msgs[ret].len);
598 }
599#endif
600
601 down(&adap->bus_lock);
602 ret = adap->algo->master_xfer(adap,msgs,num);
603 up(&adap->bus_lock);
604
605 return ret;
606 } else {
607 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
608 return -ENOSYS;
609 }
610}
611
612int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
613{
614 int ret;
615 struct i2c_adapter *adap=client->adapter;
616 struct i2c_msg msg;
617
Jean Delvare815f55f2005-05-07 22:58:46 +0200618 msg.addr = client->addr;
619 msg.flags = client->flags & I2C_M_TEN;
620 msg.len = count;
621 msg.buf = (char *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Jean Delvare815f55f2005-05-07 22:58:46 +0200623 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jean Delvare815f55f2005-05-07 22:58:46 +0200625 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
626 transmitted, else error code. */
627 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
631{
632 struct i2c_adapter *adap=client->adapter;
633 struct i2c_msg msg;
634 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Jean Delvare815f55f2005-05-07 22:58:46 +0200636 msg.addr = client->addr;
637 msg.flags = client->flags & I2C_M_TEN;
638 msg.flags |= I2C_M_RD;
639 msg.len = count;
640 msg.buf = buf;
641
642 ret = i2c_transfer(adap, &msg, 1);
643
644 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
645 transmitted, else error code. */
646 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649
650int i2c_control(struct i2c_client *client,
651 unsigned int cmd, unsigned long arg)
652{
653 int ret = 0;
654 struct i2c_adapter *adap = client->adapter;
655
656 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
657 switch (cmd) {
658 case I2C_RETRIES:
659 adap->retries = arg;
660 break;
661 case I2C_TIMEOUT:
662 adap->timeout = arg;
663 break;
664 default:
665 if (adap->algo->algo_control!=NULL)
666 ret = adap->algo->algo_control(adap,cmd,arg);
667 }
668 return ret;
669}
670
671/* ----------------------------------------------------
672 * the i2c address scanning function
673 * Will not work for 10-bit addresses!
674 * ----------------------------------------------------
675 */
676int i2c_probe(struct i2c_adapter *adapter,
677 struct i2c_client_address_data *address_data,
678 int (*found_proc) (struct i2c_adapter *, int, int))
679{
680 int addr,i,found,err;
681 int adap_id = i2c_adapter_id(adapter);
682
683 /* Forget it if we can't probe using SMBUS_QUICK */
684 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
685 return -1;
686
687 for (addr = 0x00; addr <= 0x7f; addr++) {
688
689 /* Skip if already in use */
690 if (i2c_check_addr(adapter,addr))
691 continue;
692
693 /* If it is in one of the force entries, we don't do any detection
694 at all */
695 found = 0;
696
697 for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) {
698 if (((adap_id == address_data->force[i]) ||
699 (address_data->force[i] == ANY_I2C_BUS)) &&
700 (addr == address_data->force[i+1])) {
701 dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n",
702 adap_id, addr);
703 if ((err = found_proc(adapter,addr,0)))
704 return err;
705 found = 1;
706 }
707 }
708 if (found)
709 continue;
710
711 /* If this address is in one of the ignores, we can forget about
712 it right now */
713 for (i = 0;
714 !found && (address_data->ignore[i] != I2C_CLIENT_END);
715 i += 2) {
716 if (((adap_id == address_data->ignore[i]) ||
717 ((address_data->ignore[i] == ANY_I2C_BUS))) &&
718 (addr == address_data->ignore[i+1])) {
719 dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
720 "addr %04x\n", adap_id ,addr);
721 found = 1;
722 }
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (found)
725 continue;
726
727 /* Now, we will do a detection, but only if it is in the normal or
728 probe entries */
729 for (i = 0;
730 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
731 i += 1) {
732 if (addr == address_data->normal_i2c[i]) {
733 found = 1;
734 dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
735 "addr %02x\n", adap_id, addr);
736 }
737 }
738
739 for (i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 !found && (address_data->probe[i] != I2C_CLIENT_END);
741 i += 2) {
742 if (((adap_id == address_data->probe[i]) ||
743 ((address_data->probe[i] == ANY_I2C_BUS))) &&
744 (addr == address_data->probe[i+1])) {
745 found = 1;
746 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
747 "addr %04x\n", adap_id,addr);
748 }
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!found)
751 continue;
752
753 /* OK, so we really should examine this address. First check
754 whether there is some client here at all! */
755 if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
756 if ((err = found_proc(adapter,addr,-1)))
757 return err;
758 }
759 return 0;
760}
761
762/*
763 * return id number for a specific adapter
764 */
765int i2c_adapter_id(struct i2c_adapter *adap)
766{
767 return adap->nr;
768}
769
770struct i2c_adapter* i2c_get_adapter(int id)
771{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct i2c_adapter *adapter;
773
774 down(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400775 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
776 if (adapter && !try_module_get(adapter->owner))
777 adapter = NULL;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 up(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400780 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783void i2c_put_adapter(struct i2c_adapter *adap)
784{
785 module_put(adap->owner);
786}
787
788/* The SMBus parts */
789
790#define POLY (0x1070U << 3)
791static u8
792crc8(u16 data)
793{
794 int i;
795
796 for(i = 0; i < 8; i++) {
797 if (data & 0x8000)
798 data = data ^ POLY;
799 data = data << 1;
800 }
801 return (u8)(data >> 8);
802}
803
804/* CRC over count bytes in the first array plus the bytes in the rest
805 array if it is non-null. rest[0] is the (length of rest) - 1
806 and is included. */
807static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
808{
809 int i;
810
811 for(i = 0; i < count; i++)
812 crc = crc8((crc ^ first[i]) << 8);
813 if(rest != NULL)
814 for(i = 0; i <= rest[0]; i++)
815 crc = crc8((crc ^ rest[i]) << 8);
816 return crc;
817}
818
819static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
820{
821 return i2c_smbus_partial_pec(0, count, first, rest);
822}
823
824/* Returns new "size" (transaction type)
825 Note that we convert byte to byte_data and byte_data to word_data
826 rather than invent new xxx_PEC transactions. */
827static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
828 union i2c_smbus_data *data)
829{
830 u8 buf[3];
831
832 buf[0] = addr << 1;
833 buf[1] = command;
834 switch(size) {
835 case I2C_SMBUS_BYTE:
836 data->byte = i2c_smbus_pec(2, buf, NULL);
837 size = I2C_SMBUS_BYTE_DATA;
838 break;
839 case I2C_SMBUS_BYTE_DATA:
840 buf[2] = data->byte;
841 data->word = buf[2] ||
842 (i2c_smbus_pec(3, buf, NULL) << 8);
843 size = I2C_SMBUS_WORD_DATA;
844 break;
845 case I2C_SMBUS_WORD_DATA:
846 /* unsupported */
847 break;
848 case I2C_SMBUS_BLOCK_DATA:
849 data->block[data->block[0] + 1] =
850 i2c_smbus_pec(2, buf, data->block);
851 size = I2C_SMBUS_BLOCK_DATA_PEC;
852 break;
853 }
854 return size;
855}
856
857static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
858 union i2c_smbus_data *data)
859{
860 u8 buf[3], rpec, cpec;
861
862 buf[1] = command;
863 switch(size) {
864 case I2C_SMBUS_BYTE_DATA:
865 buf[0] = (addr << 1) | 1;
866 cpec = i2c_smbus_pec(2, buf, NULL);
867 rpec = data->byte;
868 break;
869 case I2C_SMBUS_WORD_DATA:
870 buf[0] = (addr << 1) | 1;
871 buf[2] = data->word & 0xff;
872 cpec = i2c_smbus_pec(3, buf, NULL);
873 rpec = data->word >> 8;
874 break;
875 case I2C_SMBUS_WORD_DATA_PEC:
876 /* unsupported */
877 cpec = rpec = 0;
878 break;
879 case I2C_SMBUS_PROC_CALL_PEC:
880 /* unsupported */
881 cpec = rpec = 0;
882 break;
883 case I2C_SMBUS_BLOCK_DATA_PEC:
884 buf[0] = (addr << 1);
885 buf[2] = (addr << 1) | 1;
886 cpec = i2c_smbus_pec(3, buf, data->block);
887 rpec = data->block[data->block[0] + 1];
888 break;
889 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
890 buf[0] = (addr << 1) | 1;
891 rpec = i2c_smbus_partial_pec(partial, 1,
892 buf, data->block);
893 cpec = data->block[data->block[0] + 1];
894 break;
895 default:
896 cpec = rpec = 0;
897 break;
898 }
899 if (rpec != cpec) {
900 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
901 rpec, cpec);
902 return -1;
903 }
904 return 0;
905}
906
907s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
908{
909 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
910 value,0,I2C_SMBUS_QUICK,NULL);
911}
912
913s32 i2c_smbus_read_byte(struct i2c_client *client)
914{
915 union i2c_smbus_data data;
916 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
917 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
918 return -1;
919 else
920 return 0x0FF & data.byte;
921}
922
923s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
924{
925 union i2c_smbus_data data; /* only for PEC */
926 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
927 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
928}
929
930s32 i2c_smbus_read_byte_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_BYTE_DATA,&data))
935 return -1;
936 else
937 return 0x0FF & data.byte;
938}
939
940s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
941{
942 union i2c_smbus_data data;
943 data.byte = value;
944 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
945 I2C_SMBUS_WRITE,command,
946 I2C_SMBUS_BYTE_DATA,&data);
947}
948
949s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
950{
951 union i2c_smbus_data data;
952 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
953 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
954 return -1;
955 else
956 return 0x0FFFF & data.word;
957}
958
959s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
960{
961 union i2c_smbus_data data;
962 data.word = value;
963 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
964 I2C_SMBUS_WRITE,command,
965 I2C_SMBUS_WORD_DATA,&data);
966}
967
968s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
969 u8 length, u8 *values)
970{
971 union i2c_smbus_data data;
972 int i;
973 if (length > I2C_SMBUS_BLOCK_MAX)
974 length = I2C_SMBUS_BLOCK_MAX;
975 for (i = 1; i <= length; i++)
976 data.block[i] = values[i-1];
977 data.block[0] = length;
978 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
979 I2C_SMBUS_WRITE,command,
980 I2C_SMBUS_BLOCK_DATA,&data);
981}
982
983/* Returns the number of read bytes */
984s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
985{
986 union i2c_smbus_data data;
987 int i;
988 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
989 I2C_SMBUS_READ,command,
990 I2C_SMBUS_I2C_BLOCK_DATA,&data))
991 return -1;
992 else {
993 for (i = 1; i <= data.block[0]; i++)
994 values[i-1] = data.block[i];
995 return data.block[0];
996 }
997}
998
999/* Simulate a SMBus command using the i2c protocol
1000 No checking of parameters is done! */
1001static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1002 unsigned short flags,
1003 char read_write, u8 command, int size,
1004 union i2c_smbus_data * data)
1005{
1006 /* So we need to generate a series of msgs. In the case of writing, we
1007 need to use only one message; when reading, we need two. We initialize
1008 most things with sane defaults, to keep the code below somewhat
1009 simpler. */
1010 unsigned char msgbuf0[34];
1011 unsigned char msgbuf1[34];
1012 int num = read_write == I2C_SMBUS_READ?2:1;
1013 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1014 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1015 };
1016 int i;
1017
1018 msgbuf0[0] = command;
1019 switch(size) {
1020 case I2C_SMBUS_QUICK:
1021 msg[0].len = 0;
1022 /* Special case: The read/write field is used as data */
1023 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1024 num = 1;
1025 break;
1026 case I2C_SMBUS_BYTE:
1027 if (read_write == I2C_SMBUS_READ) {
1028 /* Special case: only a read! */
1029 msg[0].flags = I2C_M_RD | flags;
1030 num = 1;
1031 }
1032 break;
1033 case I2C_SMBUS_BYTE_DATA:
1034 if (read_write == I2C_SMBUS_READ)
1035 msg[1].len = 1;
1036 else {
1037 msg[0].len = 2;
1038 msgbuf0[1] = data->byte;
1039 }
1040 break;
1041 case I2C_SMBUS_WORD_DATA:
1042 if (read_write == I2C_SMBUS_READ)
1043 msg[1].len = 2;
1044 else {
1045 msg[0].len=3;
1046 msgbuf0[1] = data->word & 0xff;
1047 msgbuf0[2] = (data->word >> 8) & 0xff;
1048 }
1049 break;
1050 case I2C_SMBUS_PROC_CALL:
1051 num = 2; /* Special case */
1052 read_write = I2C_SMBUS_READ;
1053 msg[0].len = 3;
1054 msg[1].len = 2;
1055 msgbuf0[1] = data->word & 0xff;
1056 msgbuf0[2] = (data->word >> 8) & 0xff;
1057 break;
1058 case I2C_SMBUS_BLOCK_DATA:
1059 case I2C_SMBUS_BLOCK_DATA_PEC:
1060 if (read_write == I2C_SMBUS_READ) {
1061 dev_err(&adapter->dev, "Block read not supported "
1062 "under I2C emulation!\n");
1063 return -1;
1064 } else {
1065 msg[0].len = data->block[0] + 2;
1066 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1067 dev_err(&adapter->dev, "smbus_access called with "
1068 "invalid block write size (%d)\n",
1069 data->block[0]);
1070 return -1;
1071 }
1072 if(size == I2C_SMBUS_BLOCK_DATA_PEC)
1073 (msg[0].len)++;
1074 for (i = 1; i <= msg[0].len; i++)
1075 msgbuf0[i] = data->block[i-1];
1076 }
1077 break;
1078 case I2C_SMBUS_BLOCK_PROC_CALL:
1079 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
1080 dev_dbg(&adapter->dev, "Block process call not supported "
1081 "under I2C emulation!\n");
1082 return -1;
1083 case I2C_SMBUS_I2C_BLOCK_DATA:
1084 if (read_write == I2C_SMBUS_READ) {
1085 msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
1086 } else {
1087 msg[0].len = data->block[0] + 1;
1088 if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
1089 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1090 "invalid block write size (%d)\n",
1091 data->block[0]);
1092 return -1;
1093 }
1094 for (i = 1; i <= data->block[0]; i++)
1095 msgbuf0[i] = data->block[i];
1096 }
1097 break;
1098 default:
1099 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1100 size);
1101 return -1;
1102 }
1103
1104 if (i2c_transfer(adapter, msg, num) < 0)
1105 return -1;
1106
1107 if (read_write == I2C_SMBUS_READ)
1108 switch(size) {
1109 case I2C_SMBUS_BYTE:
1110 data->byte = msgbuf0[0];
1111 break;
1112 case I2C_SMBUS_BYTE_DATA:
1113 data->byte = msgbuf1[0];
1114 break;
1115 case I2C_SMBUS_WORD_DATA:
1116 case I2C_SMBUS_PROC_CALL:
1117 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1118 break;
1119 case I2C_SMBUS_I2C_BLOCK_DATA:
1120 /* fixed at 32 for now */
1121 data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
1122 for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
1123 data->block[i+1] = msgbuf1[i];
1124 break;
1125 }
1126 return 0;
1127}
1128
1129
1130s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1131 char read_write, u8 command, int size,
1132 union i2c_smbus_data * data)
1133{
1134 s32 res;
1135 int swpec = 0;
1136 u8 partial = 0;
1137
1138 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1139 if((flags & I2C_CLIENT_PEC) &&
1140 !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
1141 swpec = 1;
1142 if(read_write == I2C_SMBUS_READ &&
1143 size == I2C_SMBUS_BLOCK_DATA)
1144 size = I2C_SMBUS_BLOCK_DATA_PEC;
1145 else if(size == I2C_SMBUS_PROC_CALL)
1146 size = I2C_SMBUS_PROC_CALL_PEC;
1147 else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
1148 i2c_smbus_add_pec(addr, command,
1149 I2C_SMBUS_BLOCK_DATA, data);
1150 partial = data->block[data->block[0] + 1];
1151 size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
1152 } else if(read_write == I2C_SMBUS_WRITE &&
1153 size != I2C_SMBUS_QUICK &&
1154 size != I2C_SMBUS_I2C_BLOCK_DATA)
1155 size = i2c_smbus_add_pec(addr, command, size, data);
1156 }
1157
1158 if (adapter->algo->smbus_xfer) {
1159 down(&adapter->bus_lock);
1160 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1161 command,size,data);
1162 up(&adapter->bus_lock);
1163 } else
1164 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1165 command,size,data);
1166
1167 if(res >= 0 && swpec &&
1168 size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
1169 (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
1170 size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
1171 if(i2c_smbus_check_pec(addr, command, size, partial, data))
1172 return -1;
1173 }
1174 return res;
1175}
1176
1177
Jean Delvareefde7232005-07-20 23:03:50 +02001178/* Next four are needed by i2c-isa */
1179EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1180EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1181EXPORT_SYMBOL_GPL(i2c_adapter_class);
1182EXPORT_SYMBOL_GPL(i2c_bus_type);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184EXPORT_SYMBOL(i2c_add_adapter);
1185EXPORT_SYMBOL(i2c_del_adapter);
1186EXPORT_SYMBOL(i2c_add_driver);
1187EXPORT_SYMBOL(i2c_del_driver);
1188EXPORT_SYMBOL(i2c_attach_client);
1189EXPORT_SYMBOL(i2c_detach_client);
1190EXPORT_SYMBOL(i2c_use_client);
1191EXPORT_SYMBOL(i2c_release_client);
1192EXPORT_SYMBOL(i2c_clients_command);
1193EXPORT_SYMBOL(i2c_check_addr);
1194
1195EXPORT_SYMBOL(i2c_master_send);
1196EXPORT_SYMBOL(i2c_master_recv);
1197EXPORT_SYMBOL(i2c_control);
1198EXPORT_SYMBOL(i2c_transfer);
1199EXPORT_SYMBOL(i2c_adapter_id);
1200EXPORT_SYMBOL(i2c_get_adapter);
1201EXPORT_SYMBOL(i2c_put_adapter);
1202EXPORT_SYMBOL(i2c_probe);
1203
1204EXPORT_SYMBOL(i2c_smbus_xfer);
1205EXPORT_SYMBOL(i2c_smbus_write_quick);
1206EXPORT_SYMBOL(i2c_smbus_read_byte);
1207EXPORT_SYMBOL(i2c_smbus_write_byte);
1208EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1209EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1210EXPORT_SYMBOL(i2c_smbus_read_word_data);
1211EXPORT_SYMBOL(i2c_smbus_write_word_data);
1212EXPORT_SYMBOL(i2c_smbus_write_block_data);
1213EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1214
1215MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1216MODULE_DESCRIPTION("I2C-Bus main module");
1217MODULE_LICENSE("GPL");