[PATCH] I2C: Remove redundancy from i2c-core.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / i2c-core.c
blobf50342832d9540c6d6ee6b5730b6db7009688712
1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
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.
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.
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 /* ------------------------------------------------------------------------- */
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> */
24 #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>
35 static LIST_HEAD(adapters);
36 static LIST_HEAD(drivers);
37 static DECLARE_MUTEX(core_lists);
38 static DEFINE_IDR(i2c_adapter_idr);
40 /* match always succeeds, as we want the probe() to tell if we really accept this match */
41 static int i2c_device_match(struct device *dev, struct device_driver *drv)
43 return 1;
46 static int i2c_bus_suspend(struct device * dev, pm_message_t state)
48 int rc = 0;
50 if (dev->driver && dev->driver->suspend)
51 rc = dev->driver->suspend(dev,state,0);
52 return rc;
55 static int i2c_bus_resume(struct device * dev)
57 int rc = 0;
59 if (dev->driver && dev->driver->resume)
60 rc = dev->driver->resume(dev,0);
61 return rc;
64 static struct bus_type i2c_bus_type = {
65 .name = "i2c",
66 .match = i2c_device_match,
67 .suspend = i2c_bus_suspend,
68 .resume = i2c_bus_resume,
71 static int i2c_device_probe(struct device *dev)
73 return -ENODEV;
76 static int i2c_device_remove(struct device *dev)
78 return 0;
81 static void i2c_adapter_dev_release(struct device *dev)
83 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
84 complete(&adap->dev_released);
87 static struct device_driver i2c_adapter_driver = {
88 .name = "i2c_adapter",
89 .bus = &i2c_bus_type,
90 .probe = i2c_device_probe,
91 .remove = i2c_device_remove,
94 static void i2c_adapter_class_dev_release(struct class_device *dev)
96 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
97 complete(&adap->class_dev_released);
100 static struct class i2c_adapter_class = {
101 .name = "i2c-adapter",
102 .release = &i2c_adapter_class_dev_release,
105 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
107 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
108 return sprintf(buf, "%s\n", adap->name);
110 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
113 static void i2c_client_release(struct device *dev)
115 struct i2c_client *client = to_i2c_client(dev);
116 complete(&client->released);
119 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
121 struct i2c_client *client = to_i2c_client(dev);
122 return sprintf(buf, "%s\n", client->name);
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.
130 static struct device_attribute dev_attr_client_name = {
131 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
132 .show = &show_client_name,
136 /* ---------------------------------------------------
137 * registering functions
138 * ---------------------------------------------------
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.
146 int i2c_add_adapter(struct i2c_adapter *adap)
148 int id, res = 0;
149 struct list_head *item;
150 struct i2c_driver *driver;
152 down(&core_lists);
154 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
155 res = -ENOMEM;
156 goto out_unlock;
159 res = idr_get_new(&i2c_adapter_idr, NULL, &id);
160 if (res < 0) {
161 if (res == -EAGAIN)
162 res = -ENOMEM;
163 goto out_unlock;
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);
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.
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);
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);
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);
199 dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);
201 out_unlock:
202 up(&core_lists);
203 return res;
207 int i2c_del_adapter(struct i2c_adapter *adap)
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;
215 down(&core_lists);
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;
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;
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 "
234 "while detaching driver %s: driver not "
235 "detached!", driver->name);
236 goto out_unlock;
240 /* detach any active clients. This must be done first, because
241 * it can fail; in which case we give upp. */
242 list_for_each_safe(item, _n, &adap->clients) {
243 client = list_entry(item, struct i2c_client, list);
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.
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;
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);
266 /* wait for sysfs to drop all references */
267 wait_for_completion(&adap->dev_released);
268 wait_for_completion(&adap->class_dev_released);
270 /* free dynamically allocated bus id */
271 idr_remove(&i2c_adapter_idr, adap->nr);
273 dev_dbg(&adap->dev, "adapter unregistered\n");
275 out_unlock:
276 up(&core_lists);
277 return res;
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.
287 int i2c_add_driver(struct i2c_driver *driver)
289 struct list_head *item;
290 struct i2c_adapter *adapter;
291 int res = 0;
293 down(&core_lists);
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;
301 res = driver_register(&driver->driver);
302 if (res)
303 goto out_unlock;
305 list_add_tail(&driver->list,&drivers);
306 pr_debug("i2c-core: driver %s registered.\n", driver->name);
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);
316 out_unlock:
317 up(&core_lists);
318 return res;
321 int i2c_del_driver(struct i2c_driver *driver)
323 struct list_head *item1, *item2, *_n;
324 struct i2c_client *client;
325 struct i2c_adapter *adap;
327 int res = 0;
329 down(&core_lists);
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.
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.
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;
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;
373 driver_unregister(&driver->driver);
374 list_del(&driver->list);
375 pr_debug("i2c-core: driver unregistered: %s\n", driver->name);
377 out_unlock:
378 up(&core_lists);
379 return 0;
382 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
384 struct list_head *item;
385 struct i2c_client *client;
387 list_for_each(item,&adapter->clients) {
388 client = list_entry(item, struct i2c_client, list);
389 if (client->addr == addr)
390 return -EBUSY;
392 return 0;
395 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
397 int rval;
399 down(&adapter->clist_lock);
400 rval = __i2c_check_addr(adapter, addr);
401 up(&adapter->clist_lock);
403 return rval;
406 int i2c_attach_client(struct i2c_client *client)
408 struct i2c_adapter *adapter = client->adapter;
410 down(&adapter->clist_lock);
411 if (__i2c_check_addr(client->adapter, client->addr)) {
412 up(&adapter->clist_lock);
413 return -EBUSY;
415 list_add_tail(&client->list,&adapter->clients);
416 up(&adapter->clist_lock);
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);
426 dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",
427 client->name);
429 if (client->flags & I2C_CLIENT_ALLOW_USE)
430 client->usage_count = 0;
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;
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);
443 return 0;
447 int i2c_detach_client(struct i2c_client *client)
449 struct i2c_adapter *adapter = client->adapter;
450 int res = 0;
452 if ((client->flags & I2C_CLIENT_ALLOW_USE) && (client->usage_count > 0))
453 return -EBUSY;
455 if (adapter->client_unregister) {
456 res = adapter->client_unregister(client);
457 if (res) {
458 dev_err(&client->dev,
459 "client_unregister [%s] failed, "
460 "client not detached", client->name);
461 goto out;
465 down(&adapter->clist_lock);
466 list_del(&client->list);
467 init_completion(&client->released);
468 device_remove_file(&client->dev, &dev_attr_client_name);
469 device_unregister(&client->dev);
470 up(&adapter->clist_lock);
471 wait_for_completion(&client->released);
473 out:
474 return res;
477 static int i2c_inc_use_client(struct i2c_client *client)
480 if (!try_module_get(client->driver->owner))
481 return -ENODEV;
482 if (!try_module_get(client->adapter->owner)) {
483 module_put(client->driver->owner);
484 return -ENODEV;
487 return 0;
490 static void i2c_dec_use_client(struct i2c_client *client)
492 module_put(client->driver->owner);
493 module_put(client->adapter->owner);
496 int i2c_use_client(struct i2c_client *client)
498 int ret;
500 ret = i2c_inc_use_client(client);
501 if (ret)
502 return ret;
504 if (client->flags & I2C_CLIENT_ALLOW_USE) {
505 if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
506 client->usage_count++;
507 else if (client->usage_count > 0)
508 goto busy;
509 else
510 client->usage_count++;
513 return 0;
514 busy:
515 i2c_dec_use_client(client);
516 return -EBUSY;
519 int i2c_release_client(struct i2c_client *client)
521 if(client->flags & I2C_CLIENT_ALLOW_USE) {
522 if(client->usage_count>0)
523 client->usage_count--;
524 else {
525 pr_debug("i2c-core: %s used one too many times\n",
526 __FUNCTION__);
527 return -EPERM;
531 i2c_dec_use_client(client);
533 return 0;
536 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
538 struct list_head *item;
539 struct i2c_client *client;
541 down(&adap->clist_lock);
542 list_for_each(item,&adap->clients) {
543 client = list_entry(item, struct i2c_client, list);
544 if (!try_module_get(client->driver->owner))
545 continue;
546 if (NULL != client->driver->command) {
547 up(&adap->clist_lock);
548 client->driver->command(client,cmd,arg);
549 down(&adap->clist_lock);
551 module_put(client->driver->owner);
553 up(&adap->clist_lock);
556 static int __init i2c_init(void)
558 int retval;
560 retval = bus_register(&i2c_bus_type);
561 if (retval)
562 return retval;
563 retval = driver_register(&i2c_adapter_driver);
564 if (retval)
565 return retval;
566 return class_register(&i2c_adapter_class);
569 static void __exit i2c_exit(void)
571 class_unregister(&i2c_adapter_class);
572 driver_unregister(&i2c_adapter_driver);
573 bus_unregister(&i2c_bus_type);
576 subsys_initcall(i2c_init);
577 module_exit(i2c_exit);
579 /* ----------------------------------------------------
580 * the functional interface to the i2c busses.
581 * ----------------------------------------------------
584 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
586 int ret;
588 if (adap->algo->master_xfer) {
589 #ifdef DEBUG
590 for (ret = 0; ret < num; ret++) {
591 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
592 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
593 'R' : 'W', msgs[ret].addr, msgs[ret].len);
595 #endif
597 down(&adap->bus_lock);
598 ret = adap->algo->master_xfer(adap,msgs,num);
599 up(&adap->bus_lock);
601 return ret;
602 } else {
603 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
604 return -ENOSYS;
608 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
610 int ret;
611 struct i2c_adapter *adap=client->adapter;
612 struct i2c_msg msg;
614 msg.addr = client->addr;
615 msg.flags = client->flags & I2C_M_TEN;
616 msg.len = count;
617 msg.buf = (char *)buf;
619 ret = i2c_transfer(adap, &msg, 1);
621 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
622 transmitted, else error code. */
623 return (ret == 1) ? count : ret;
626 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
628 struct i2c_adapter *adap=client->adapter;
629 struct i2c_msg msg;
630 int ret;
632 msg.addr = client->addr;
633 msg.flags = client->flags & I2C_M_TEN;
634 msg.flags |= I2C_M_RD;
635 msg.len = count;
636 msg.buf = buf;
638 ret = i2c_transfer(adap, &msg, 1);
640 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
641 transmitted, else error code. */
642 return (ret == 1) ? count : ret;
646 int i2c_control(struct i2c_client *client,
647 unsigned int cmd, unsigned long arg)
649 int ret = 0;
650 struct i2c_adapter *adap = client->adapter;
652 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
653 switch (cmd) {
654 case I2C_RETRIES:
655 adap->retries = arg;
656 break;
657 case I2C_TIMEOUT:
658 adap->timeout = arg;
659 break;
660 default:
661 if (adap->algo->algo_control!=NULL)
662 ret = adap->algo->algo_control(adap,cmd,arg);
664 return ret;
667 /* ----------------------------------------------------
668 * the i2c address scanning function
669 * Will not work for 10-bit addresses!
670 * ----------------------------------------------------
672 int i2c_probe(struct i2c_adapter *adapter,
673 struct i2c_client_address_data *address_data,
674 int (*found_proc) (struct i2c_adapter *, int, int))
676 int addr,i,found,err;
677 int adap_id = i2c_adapter_id(adapter);
679 /* Forget it if we can't probe using SMBUS_QUICK */
680 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
681 return -1;
683 for (addr = 0x00; addr <= 0x7f; addr++) {
685 /* Skip if already in use */
686 if (i2c_check_addr(adapter,addr))
687 continue;
689 /* If it is in one of the force entries, we don't do any detection
690 at all */
691 found = 0;
693 for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) {
694 if (((adap_id == address_data->force[i]) ||
695 (address_data->force[i] == ANY_I2C_BUS)) &&
696 (addr == address_data->force[i+1])) {
697 dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n",
698 adap_id, addr);
699 if ((err = found_proc(adapter,addr,0)))
700 return err;
701 found = 1;
704 if (found)
705 continue;
707 /* If this address is in one of the ignores, we can forget about
708 it right now */
709 for (i = 0;
710 !found && (address_data->ignore[i] != I2C_CLIENT_END);
711 i += 2) {
712 if (((adap_id == address_data->ignore[i]) ||
713 ((address_data->ignore[i] == ANY_I2C_BUS))) &&
714 (addr == address_data->ignore[i+1])) {
715 dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
716 "addr %04x\n", adap_id ,addr);
717 found = 1;
720 if (found)
721 continue;
723 /* Now, we will do a detection, but only if it is in the normal or
724 probe entries */
725 for (i = 0;
726 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
727 i += 1) {
728 if (addr == address_data->normal_i2c[i]) {
729 found = 1;
730 dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
731 "addr %02x\n", adap_id, addr);
735 for (i = 0;
736 !found && (address_data->probe[i] != I2C_CLIENT_END);
737 i += 2) {
738 if (((adap_id == address_data->probe[i]) ||
739 ((address_data->probe[i] == ANY_I2C_BUS))) &&
740 (addr == address_data->probe[i+1])) {
741 found = 1;
742 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
743 "addr %04x\n", adap_id,addr);
746 if (!found)
747 continue;
749 /* OK, so we really should examine this address. First check
750 whether there is some client here at all! */
751 if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
752 if ((err = found_proc(adapter,addr,-1)))
753 return err;
755 return 0;
759 * return id number for a specific adapter
761 int i2c_adapter_id(struct i2c_adapter *adap)
763 return adap->nr;
766 struct i2c_adapter* i2c_get_adapter(int id)
768 struct list_head *item;
769 struct i2c_adapter *adapter;
771 down(&core_lists);
772 list_for_each(item,&adapters) {
773 adapter = list_entry(item, struct i2c_adapter, list);
774 if (id == adapter->nr &&
775 try_module_get(adapter->owner)) {
776 up(&core_lists);
777 return adapter;
780 up(&core_lists);
781 return NULL;
784 void i2c_put_adapter(struct i2c_adapter *adap)
786 module_put(adap->owner);
789 /* The SMBus parts */
791 #define POLY (0x1070U << 3)
792 static u8
793 crc8(u16 data)
795 int i;
797 for(i = 0; i < 8; i++) {
798 if (data & 0x8000)
799 data = data ^ POLY;
800 data = data << 1;
802 return (u8)(data >> 8);
805 /* CRC over count bytes in the first array plus the bytes in the rest
806 array if it is non-null. rest[0] is the (length of rest) - 1
807 and is included. */
808 static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
810 int i;
812 for(i = 0; i < count; i++)
813 crc = crc8((crc ^ first[i]) << 8);
814 if(rest != NULL)
815 for(i = 0; i <= rest[0]; i++)
816 crc = crc8((crc ^ rest[i]) << 8);
817 return crc;
820 static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
822 return i2c_smbus_partial_pec(0, count, first, rest);
825 /* Returns new "size" (transaction type)
826 Note that we convert byte to byte_data and byte_data to word_data
827 rather than invent new xxx_PEC transactions. */
828 static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
829 union i2c_smbus_data *data)
831 u8 buf[3];
833 buf[0] = addr << 1;
834 buf[1] = command;
835 switch(size) {
836 case I2C_SMBUS_BYTE:
837 data->byte = i2c_smbus_pec(2, buf, NULL);
838 size = I2C_SMBUS_BYTE_DATA;
839 break;
840 case I2C_SMBUS_BYTE_DATA:
841 buf[2] = data->byte;
842 data->word = buf[2] ||
843 (i2c_smbus_pec(3, buf, NULL) << 8);
844 size = I2C_SMBUS_WORD_DATA;
845 break;
846 case I2C_SMBUS_WORD_DATA:
847 /* unsupported */
848 break;
849 case I2C_SMBUS_BLOCK_DATA:
850 data->block[data->block[0] + 1] =
851 i2c_smbus_pec(2, buf, data->block);
852 size = I2C_SMBUS_BLOCK_DATA_PEC;
853 break;
855 return size;
858 static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
859 union i2c_smbus_data *data)
861 u8 buf[3], rpec, cpec;
863 buf[1] = command;
864 switch(size) {
865 case I2C_SMBUS_BYTE_DATA:
866 buf[0] = (addr << 1) | 1;
867 cpec = i2c_smbus_pec(2, buf, NULL);
868 rpec = data->byte;
869 break;
870 case I2C_SMBUS_WORD_DATA:
871 buf[0] = (addr << 1) | 1;
872 buf[2] = data->word & 0xff;
873 cpec = i2c_smbus_pec(3, buf, NULL);
874 rpec = data->word >> 8;
875 break;
876 case I2C_SMBUS_WORD_DATA_PEC:
877 /* unsupported */
878 cpec = rpec = 0;
879 break;
880 case I2C_SMBUS_PROC_CALL_PEC:
881 /* unsupported */
882 cpec = rpec = 0;
883 break;
884 case I2C_SMBUS_BLOCK_DATA_PEC:
885 buf[0] = (addr << 1);
886 buf[2] = (addr << 1) | 1;
887 cpec = i2c_smbus_pec(3, buf, data->block);
888 rpec = data->block[data->block[0] + 1];
889 break;
890 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
891 buf[0] = (addr << 1) | 1;
892 rpec = i2c_smbus_partial_pec(partial, 1,
893 buf, data->block);
894 cpec = data->block[data->block[0] + 1];
895 break;
896 default:
897 cpec = rpec = 0;
898 break;
900 if (rpec != cpec) {
901 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
902 rpec, cpec);
903 return -1;
905 return 0;
908 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
910 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
911 value,0,I2C_SMBUS_QUICK,NULL);
914 s32 i2c_smbus_read_byte(struct i2c_client *client)
916 union i2c_smbus_data data;
917 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
918 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
919 return -1;
920 else
921 return 0x0FF & data.byte;
924 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
926 union i2c_smbus_data data; /* only for PEC */
927 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
928 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
931 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
933 union i2c_smbus_data data;
934 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
935 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
936 return -1;
937 else
938 return 0x0FF & data.byte;
941 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
943 union i2c_smbus_data data;
944 data.byte = value;
945 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
946 I2C_SMBUS_WRITE,command,
947 I2C_SMBUS_BYTE_DATA,&data);
950 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
952 union i2c_smbus_data data;
953 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
954 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
955 return -1;
956 else
957 return 0x0FFFF & data.word;
960 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
962 union i2c_smbus_data data;
963 data.word = value;
964 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
965 I2C_SMBUS_WRITE,command,
966 I2C_SMBUS_WORD_DATA,&data);
969 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
970 u8 length, u8 *values)
972 union i2c_smbus_data data;
973 int i;
974 if (length > I2C_SMBUS_BLOCK_MAX)
975 length = I2C_SMBUS_BLOCK_MAX;
976 for (i = 1; i <= length; i++)
977 data.block[i] = values[i-1];
978 data.block[0] = length;
979 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
980 I2C_SMBUS_WRITE,command,
981 I2C_SMBUS_BLOCK_DATA,&data);
984 /* Returns the number of read bytes */
985 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
987 union i2c_smbus_data data;
988 int i;
989 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
990 I2C_SMBUS_READ,command,
991 I2C_SMBUS_I2C_BLOCK_DATA,&data))
992 return -1;
993 else {
994 for (i = 1; i <= data.block[0]; i++)
995 values[i-1] = data.block[i];
996 return data.block[0];
1000 /* Simulate a SMBus command using the i2c protocol
1001 No checking of parameters is done! */
1002 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1003 unsigned short flags,
1004 char read_write, u8 command, int size,
1005 union i2c_smbus_data * data)
1007 /* So we need to generate a series of msgs. In the case of writing, we
1008 need to use only one message; when reading, we need two. We initialize
1009 most things with sane defaults, to keep the code below somewhat
1010 simpler. */
1011 unsigned char msgbuf0[34];
1012 unsigned char msgbuf1[34];
1013 int num = read_write == I2C_SMBUS_READ?2:1;
1014 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1015 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1017 int i;
1019 msgbuf0[0] = command;
1020 switch(size) {
1021 case I2C_SMBUS_QUICK:
1022 msg[0].len = 0;
1023 /* Special case: The read/write field is used as data */
1024 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1025 num = 1;
1026 break;
1027 case I2C_SMBUS_BYTE:
1028 if (read_write == I2C_SMBUS_READ) {
1029 /* Special case: only a read! */
1030 msg[0].flags = I2C_M_RD | flags;
1031 num = 1;
1033 break;
1034 case I2C_SMBUS_BYTE_DATA:
1035 if (read_write == I2C_SMBUS_READ)
1036 msg[1].len = 1;
1037 else {
1038 msg[0].len = 2;
1039 msgbuf0[1] = data->byte;
1041 break;
1042 case I2C_SMBUS_WORD_DATA:
1043 if (read_write == I2C_SMBUS_READ)
1044 msg[1].len = 2;
1045 else {
1046 msg[0].len=3;
1047 msgbuf0[1] = data->word & 0xff;
1048 msgbuf0[2] = (data->word >> 8) & 0xff;
1050 break;
1051 case I2C_SMBUS_PROC_CALL:
1052 num = 2; /* Special case */
1053 read_write = I2C_SMBUS_READ;
1054 msg[0].len = 3;
1055 msg[1].len = 2;
1056 msgbuf0[1] = data->word & 0xff;
1057 msgbuf0[2] = (data->word >> 8) & 0xff;
1058 break;
1059 case I2C_SMBUS_BLOCK_DATA:
1060 case I2C_SMBUS_BLOCK_DATA_PEC:
1061 if (read_write == I2C_SMBUS_READ) {
1062 dev_err(&adapter->dev, "Block read not supported "
1063 "under I2C emulation!\n");
1064 return -1;
1065 } else {
1066 msg[0].len = data->block[0] + 2;
1067 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1068 dev_err(&adapter->dev, "smbus_access called with "
1069 "invalid block write size (%d)\n",
1070 data->block[0]);
1071 return -1;
1073 if(size == I2C_SMBUS_BLOCK_DATA_PEC)
1074 (msg[0].len)++;
1075 for (i = 1; i <= msg[0].len; i++)
1076 msgbuf0[i] = data->block[i-1];
1078 break;
1079 case I2C_SMBUS_BLOCK_PROC_CALL:
1080 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
1081 dev_dbg(&adapter->dev, "Block process call not supported "
1082 "under I2C emulation!\n");
1083 return -1;
1084 case I2C_SMBUS_I2C_BLOCK_DATA:
1085 if (read_write == I2C_SMBUS_READ) {
1086 msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
1087 } else {
1088 msg[0].len = data->block[0] + 1;
1089 if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
1090 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1091 "invalid block write size (%d)\n",
1092 data->block[0]);
1093 return -1;
1095 for (i = 1; i <= data->block[0]; i++)
1096 msgbuf0[i] = data->block[i];
1098 break;
1099 default:
1100 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1101 size);
1102 return -1;
1105 if (i2c_transfer(adapter, msg, num) < 0)
1106 return -1;
1108 if (read_write == I2C_SMBUS_READ)
1109 switch(size) {
1110 case I2C_SMBUS_BYTE:
1111 data->byte = msgbuf0[0];
1112 break;
1113 case I2C_SMBUS_BYTE_DATA:
1114 data->byte = msgbuf1[0];
1115 break;
1116 case I2C_SMBUS_WORD_DATA:
1117 case I2C_SMBUS_PROC_CALL:
1118 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1119 break;
1120 case I2C_SMBUS_I2C_BLOCK_DATA:
1121 /* fixed at 32 for now */
1122 data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
1123 for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
1124 data->block[i+1] = msgbuf1[i];
1125 break;
1127 return 0;
1131 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1132 char read_write, u8 command, int size,
1133 union i2c_smbus_data * data)
1135 s32 res;
1136 int swpec = 0;
1137 u8 partial = 0;
1139 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1140 if((flags & I2C_CLIENT_PEC) &&
1141 !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
1142 swpec = 1;
1143 if(read_write == I2C_SMBUS_READ &&
1144 size == I2C_SMBUS_BLOCK_DATA)
1145 size = I2C_SMBUS_BLOCK_DATA_PEC;
1146 else if(size == I2C_SMBUS_PROC_CALL)
1147 size = I2C_SMBUS_PROC_CALL_PEC;
1148 else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
1149 i2c_smbus_add_pec(addr, command,
1150 I2C_SMBUS_BLOCK_DATA, data);
1151 partial = data->block[data->block[0] + 1];
1152 size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
1153 } else if(read_write == I2C_SMBUS_WRITE &&
1154 size != I2C_SMBUS_QUICK &&
1155 size != I2C_SMBUS_I2C_BLOCK_DATA)
1156 size = i2c_smbus_add_pec(addr, command, size, data);
1159 if (adapter->algo->smbus_xfer) {
1160 down(&adapter->bus_lock);
1161 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1162 command,size,data);
1163 up(&adapter->bus_lock);
1164 } else
1165 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1166 command,size,data);
1168 if(res >= 0 && swpec &&
1169 size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
1170 (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
1171 size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
1172 if(i2c_smbus_check_pec(addr, command, size, partial, data))
1173 return -1;
1175 return res;
1179 EXPORT_SYMBOL(i2c_add_adapter);
1180 EXPORT_SYMBOL(i2c_del_adapter);
1181 EXPORT_SYMBOL(i2c_add_driver);
1182 EXPORT_SYMBOL(i2c_del_driver);
1183 EXPORT_SYMBOL(i2c_attach_client);
1184 EXPORT_SYMBOL(i2c_detach_client);
1185 EXPORT_SYMBOL(i2c_use_client);
1186 EXPORT_SYMBOL(i2c_release_client);
1187 EXPORT_SYMBOL(i2c_clients_command);
1188 EXPORT_SYMBOL(i2c_check_addr);
1190 EXPORT_SYMBOL(i2c_master_send);
1191 EXPORT_SYMBOL(i2c_master_recv);
1192 EXPORT_SYMBOL(i2c_control);
1193 EXPORT_SYMBOL(i2c_transfer);
1194 EXPORT_SYMBOL(i2c_adapter_id);
1195 EXPORT_SYMBOL(i2c_get_adapter);
1196 EXPORT_SYMBOL(i2c_put_adapter);
1197 EXPORT_SYMBOL(i2c_probe);
1199 EXPORT_SYMBOL(i2c_smbus_xfer);
1200 EXPORT_SYMBOL(i2c_smbus_write_quick);
1201 EXPORT_SYMBOL(i2c_smbus_read_byte);
1202 EXPORT_SYMBOL(i2c_smbus_write_byte);
1203 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1204 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1205 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1206 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1207 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1208 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1210 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1211 MODULE_DESCRIPTION("I2C-Bus main module");
1212 MODULE_LICENSE("GPL");