blob: 20f92e355f56a1463a63bfba4c359bff7596b494 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762struct i2c_adapter* i2c_get_adapter(int id)
763{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct i2c_adapter *adapter;
765
766 down(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400767 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
768 if (adapter && !try_module_get(adapter->owner))
769 adapter = NULL;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 up(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400772 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775void i2c_put_adapter(struct i2c_adapter *adap)
776{
777 module_put(adap->owner);
778}
779
780/* The SMBus parts */
781
782#define POLY (0x1070U << 3)
783static u8
784crc8(u16 data)
785{
786 int i;
787
788 for(i = 0; i < 8; i++) {
789 if (data & 0x8000)
790 data = data ^ POLY;
791 data = data << 1;
792 }
793 return (u8)(data >> 8);
794}
795
796/* CRC over count bytes in the first array plus the bytes in the rest
797 array if it is non-null. rest[0] is the (length of rest) - 1
798 and is included. */
799static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
800{
801 int i;
802
803 for(i = 0; i < count; i++)
804 crc = crc8((crc ^ first[i]) << 8);
805 if(rest != NULL)
806 for(i = 0; i <= rest[0]; i++)
807 crc = crc8((crc ^ rest[i]) << 8);
808 return crc;
809}
810
811static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
812{
813 return i2c_smbus_partial_pec(0, count, first, rest);
814}
815
816/* Returns new "size" (transaction type)
817 Note that we convert byte to byte_data and byte_data to word_data
818 rather than invent new xxx_PEC transactions. */
819static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
820 union i2c_smbus_data *data)
821{
822 u8 buf[3];
823
824 buf[0] = addr << 1;
825 buf[1] = command;
826 switch(size) {
827 case I2C_SMBUS_BYTE:
828 data->byte = i2c_smbus_pec(2, buf, NULL);
829 size = I2C_SMBUS_BYTE_DATA;
830 break;
831 case I2C_SMBUS_BYTE_DATA:
832 buf[2] = data->byte;
833 data->word = buf[2] ||
834 (i2c_smbus_pec(3, buf, NULL) << 8);
835 size = I2C_SMBUS_WORD_DATA;
836 break;
837 case I2C_SMBUS_WORD_DATA:
838 /* unsupported */
839 break;
840 case I2C_SMBUS_BLOCK_DATA:
841 data->block[data->block[0] + 1] =
842 i2c_smbus_pec(2, buf, data->block);
843 size = I2C_SMBUS_BLOCK_DATA_PEC;
844 break;
845 }
846 return size;
847}
848
849static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
850 union i2c_smbus_data *data)
851{
852 u8 buf[3], rpec, cpec;
853
854 buf[1] = command;
855 switch(size) {
856 case I2C_SMBUS_BYTE_DATA:
857 buf[0] = (addr << 1) | 1;
858 cpec = i2c_smbus_pec(2, buf, NULL);
859 rpec = data->byte;
860 break;
861 case I2C_SMBUS_WORD_DATA:
862 buf[0] = (addr << 1) | 1;
863 buf[2] = data->word & 0xff;
864 cpec = i2c_smbus_pec(3, buf, NULL);
865 rpec = data->word >> 8;
866 break;
867 case I2C_SMBUS_WORD_DATA_PEC:
868 /* unsupported */
869 cpec = rpec = 0;
870 break;
871 case I2C_SMBUS_PROC_CALL_PEC:
872 /* unsupported */
873 cpec = rpec = 0;
874 break;
875 case I2C_SMBUS_BLOCK_DATA_PEC:
876 buf[0] = (addr << 1);
877 buf[2] = (addr << 1) | 1;
878 cpec = i2c_smbus_pec(3, buf, data->block);
879 rpec = data->block[data->block[0] + 1];
880 break;
881 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
882 buf[0] = (addr << 1) | 1;
883 rpec = i2c_smbus_partial_pec(partial, 1,
884 buf, data->block);
885 cpec = data->block[data->block[0] + 1];
886 break;
887 default:
888 cpec = rpec = 0;
889 break;
890 }
891 if (rpec != cpec) {
892 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
893 rpec, cpec);
894 return -1;
895 }
896 return 0;
897}
898
899s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
900{
901 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
902 value,0,I2C_SMBUS_QUICK,NULL);
903}
904
905s32 i2c_smbus_read_byte(struct i2c_client *client)
906{
907 union i2c_smbus_data data;
908 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
909 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
910 return -1;
911 else
912 return 0x0FF & data.byte;
913}
914
915s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
916{
917 union i2c_smbus_data data; /* only for PEC */
918 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
919 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
920}
921
922s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
923{
924 union i2c_smbus_data data;
925 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
926 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
927 return -1;
928 else
929 return 0x0FF & data.byte;
930}
931
932s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
933{
934 union i2c_smbus_data data;
935 data.byte = value;
936 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
937 I2C_SMBUS_WRITE,command,
938 I2C_SMBUS_BYTE_DATA,&data);
939}
940
941s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
942{
943 union i2c_smbus_data data;
944 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
945 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
946 return -1;
947 else
948 return 0x0FFFF & data.word;
949}
950
951s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
952{
953 union i2c_smbus_data data;
954 data.word = value;
955 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
956 I2C_SMBUS_WRITE,command,
957 I2C_SMBUS_WORD_DATA,&data);
958}
959
960s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
961 u8 length, u8 *values)
962{
963 union i2c_smbus_data data;
964 int i;
965 if (length > I2C_SMBUS_BLOCK_MAX)
966 length = I2C_SMBUS_BLOCK_MAX;
967 for (i = 1; i <= length; i++)
968 data.block[i] = values[i-1];
969 data.block[0] = length;
970 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
971 I2C_SMBUS_WRITE,command,
972 I2C_SMBUS_BLOCK_DATA,&data);
973}
974
975/* Returns the number of read bytes */
976s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
977{
978 union i2c_smbus_data data;
979 int i;
980 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
981 I2C_SMBUS_READ,command,
982 I2C_SMBUS_I2C_BLOCK_DATA,&data))
983 return -1;
984 else {
985 for (i = 1; i <= data.block[0]; i++)
986 values[i-1] = data.block[i];
987 return data.block[0];
988 }
989}
990
991/* Simulate a SMBus command using the i2c protocol
992 No checking of parameters is done! */
993static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
994 unsigned short flags,
995 char read_write, u8 command, int size,
996 union i2c_smbus_data * data)
997{
998 /* So we need to generate a series of msgs. In the case of writing, we
999 need to use only one message; when reading, we need two. We initialize
1000 most things with sane defaults, to keep the code below somewhat
1001 simpler. */
1002 unsigned char msgbuf0[34];
1003 unsigned char msgbuf1[34];
1004 int num = read_write == I2C_SMBUS_READ?2:1;
1005 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1006 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1007 };
1008 int i;
1009
1010 msgbuf0[0] = command;
1011 switch(size) {
1012 case I2C_SMBUS_QUICK:
1013 msg[0].len = 0;
1014 /* Special case: The read/write field is used as data */
1015 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1016 num = 1;
1017 break;
1018 case I2C_SMBUS_BYTE:
1019 if (read_write == I2C_SMBUS_READ) {
1020 /* Special case: only a read! */
1021 msg[0].flags = I2C_M_RD | flags;
1022 num = 1;
1023 }
1024 break;
1025 case I2C_SMBUS_BYTE_DATA:
1026 if (read_write == I2C_SMBUS_READ)
1027 msg[1].len = 1;
1028 else {
1029 msg[0].len = 2;
1030 msgbuf0[1] = data->byte;
1031 }
1032 break;
1033 case I2C_SMBUS_WORD_DATA:
1034 if (read_write == I2C_SMBUS_READ)
1035 msg[1].len = 2;
1036 else {
1037 msg[0].len=3;
1038 msgbuf0[1] = data->word & 0xff;
1039 msgbuf0[2] = (data->word >> 8) & 0xff;
1040 }
1041 break;
1042 case I2C_SMBUS_PROC_CALL:
1043 num = 2; /* Special case */
1044 read_write = I2C_SMBUS_READ;
1045 msg[0].len = 3;
1046 msg[1].len = 2;
1047 msgbuf0[1] = data->word & 0xff;
1048 msgbuf0[2] = (data->word >> 8) & 0xff;
1049 break;
1050 case I2C_SMBUS_BLOCK_DATA:
1051 case I2C_SMBUS_BLOCK_DATA_PEC:
1052 if (read_write == I2C_SMBUS_READ) {
1053 dev_err(&adapter->dev, "Block read not supported "
1054 "under I2C emulation!\n");
1055 return -1;
1056 } else {
1057 msg[0].len = data->block[0] + 2;
1058 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1059 dev_err(&adapter->dev, "smbus_access called with "
1060 "invalid block write size (%d)\n",
1061 data->block[0]);
1062 return -1;
1063 }
1064 if(size == I2C_SMBUS_BLOCK_DATA_PEC)
1065 (msg[0].len)++;
1066 for (i = 1; i <= msg[0].len; i++)
1067 msgbuf0[i] = data->block[i-1];
1068 }
1069 break;
1070 case I2C_SMBUS_BLOCK_PROC_CALL:
1071 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
1072 dev_dbg(&adapter->dev, "Block process call not supported "
1073 "under I2C emulation!\n");
1074 return -1;
1075 case I2C_SMBUS_I2C_BLOCK_DATA:
1076 if (read_write == I2C_SMBUS_READ) {
1077 msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
1078 } else {
1079 msg[0].len = data->block[0] + 1;
1080 if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
1081 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1082 "invalid block write size (%d)\n",
1083 data->block[0]);
1084 return -1;
1085 }
1086 for (i = 1; i <= data->block[0]; i++)
1087 msgbuf0[i] = data->block[i];
1088 }
1089 break;
1090 default:
1091 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1092 size);
1093 return -1;
1094 }
1095
1096 if (i2c_transfer(adapter, msg, num) < 0)
1097 return -1;
1098
1099 if (read_write == I2C_SMBUS_READ)
1100 switch(size) {
1101 case I2C_SMBUS_BYTE:
1102 data->byte = msgbuf0[0];
1103 break;
1104 case I2C_SMBUS_BYTE_DATA:
1105 data->byte = msgbuf1[0];
1106 break;
1107 case I2C_SMBUS_WORD_DATA:
1108 case I2C_SMBUS_PROC_CALL:
1109 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1110 break;
1111 case I2C_SMBUS_I2C_BLOCK_DATA:
1112 /* fixed at 32 for now */
1113 data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
1114 for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
1115 data->block[i+1] = msgbuf1[i];
1116 break;
1117 }
1118 return 0;
1119}
1120
1121
1122s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1123 char read_write, u8 command, int size,
1124 union i2c_smbus_data * data)
1125{
1126 s32 res;
1127 int swpec = 0;
1128 u8 partial = 0;
1129
1130 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1131 if((flags & I2C_CLIENT_PEC) &&
1132 !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
1133 swpec = 1;
1134 if(read_write == I2C_SMBUS_READ &&
1135 size == I2C_SMBUS_BLOCK_DATA)
1136 size = I2C_SMBUS_BLOCK_DATA_PEC;
1137 else if(size == I2C_SMBUS_PROC_CALL)
1138 size = I2C_SMBUS_PROC_CALL_PEC;
1139 else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
1140 i2c_smbus_add_pec(addr, command,
1141 I2C_SMBUS_BLOCK_DATA, data);
1142 partial = data->block[data->block[0] + 1];
1143 size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
1144 } else if(read_write == I2C_SMBUS_WRITE &&
1145 size != I2C_SMBUS_QUICK &&
1146 size != I2C_SMBUS_I2C_BLOCK_DATA)
1147 size = i2c_smbus_add_pec(addr, command, size, data);
1148 }
1149
1150 if (adapter->algo->smbus_xfer) {
1151 down(&adapter->bus_lock);
1152 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1153 command,size,data);
1154 up(&adapter->bus_lock);
1155 } else
1156 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1157 command,size,data);
1158
1159 if(res >= 0 && swpec &&
1160 size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
1161 (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
1162 size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
1163 if(i2c_smbus_check_pec(addr, command, size, partial, data))
1164 return -1;
1165 }
1166 return res;
1167}
1168
1169
Jean Delvareefde7232005-07-20 23:03:50 +02001170/* Next four are needed by i2c-isa */
1171EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1172EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1173EXPORT_SYMBOL_GPL(i2c_adapter_class);
1174EXPORT_SYMBOL_GPL(i2c_bus_type);
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176EXPORT_SYMBOL(i2c_add_adapter);
1177EXPORT_SYMBOL(i2c_del_adapter);
1178EXPORT_SYMBOL(i2c_add_driver);
1179EXPORT_SYMBOL(i2c_del_driver);
1180EXPORT_SYMBOL(i2c_attach_client);
1181EXPORT_SYMBOL(i2c_detach_client);
1182EXPORT_SYMBOL(i2c_use_client);
1183EXPORT_SYMBOL(i2c_release_client);
1184EXPORT_SYMBOL(i2c_clients_command);
1185EXPORT_SYMBOL(i2c_check_addr);
1186
1187EXPORT_SYMBOL(i2c_master_send);
1188EXPORT_SYMBOL(i2c_master_recv);
1189EXPORT_SYMBOL(i2c_control);
1190EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191EXPORT_SYMBOL(i2c_get_adapter);
1192EXPORT_SYMBOL(i2c_put_adapter);
1193EXPORT_SYMBOL(i2c_probe);
1194
1195EXPORT_SYMBOL(i2c_smbus_xfer);
1196EXPORT_SYMBOL(i2c_smbus_write_quick);
1197EXPORT_SYMBOL(i2c_smbus_read_byte);
1198EXPORT_SYMBOL(i2c_smbus_write_byte);
1199EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1200EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1201EXPORT_SYMBOL(i2c_smbus_read_word_data);
1202EXPORT_SYMBOL(i2c_smbus_write_word_data);
1203EXPORT_SYMBOL(i2c_smbus_write_block_data);
1204EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1205
1206MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1207MODULE_DESCRIPTION("I2C-Bus main module");
1208MODULE_LICENSE("GPL");