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/config.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/init.h>
31 #include <linux/idr.h>
32 #include <linux/seq_file.h>
33 #include <asm/uaccess.h>
36 static LIST_HEAD(adapters);
37 static LIST_HEAD(drivers);
38 static DECLARE_MUTEX(core_lists);
39 static DEFINE_IDR(i2c_adapter_idr);
41 int i2c_device_probe(struct device *dev)
46 int i2c_device_remove(struct device *dev)
51 static void i2c_adapter_dev_release(struct device *dev)
53 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
54 complete(&adap->dev_released);
57 static struct device_driver i2c_adapter_driver = {
58 .name = "i2c_adapter",
60 .probe = i2c_device_probe,
61 .remove = i2c_device_remove,
64 static void i2c_adapter_class_dev_release(struct class_device *dev)
66 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
67 complete(&adap->class_dev_released);
70 static struct class i2c_adapter_class = {
71 .name = "i2c-adapter",
72 .release = &i2c_adapter_class_dev_release,
75 static ssize_t show_adapter_name(struct device *dev, char *buf)
77 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
78 return sprintf(buf, "%s\n", adap->name);
80 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
83 static void i2c_client_release(struct device *dev)
85 struct i2c_client *client = to_i2c_client(dev);
86 complete(&client->released);
89 static ssize_t show_client_name(struct device *dev, char *buf)
91 struct i2c_client *client = to_i2c_client(dev);
92 return sprintf(buf, "%s\n", client->name);
96 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
97 * different type of a device. So beware if the DEVICE_ATTR() macro ever
98 * changes, this definition will also have to change.
100 static struct device_attribute dev_attr_client_name = {
101 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
102 .show = &show_client_name,
106 /* ---------------------------------------------------
107 * registering functions
108 * ---------------------------------------------------
112 * i2c_add_adapter is called from within the algorithm layer,
113 * when a new hw adapter registers. A new device is register to be
114 * available for clients.
116 int i2c_add_adapter(struct i2c_adapter *adap)
119 struct list_head *item;
120 struct i2c_driver *driver;
124 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
129 res = idr_get_new(&i2c_adapter_idr, NULL, &id);
136 adap->nr = id & MAX_ID_MASK;
137 init_MUTEX(&adap->bus_lock);
138 init_MUTEX(&adap->clist_lock);
139 list_add_tail(&adap->list,&adapters);
140 INIT_LIST_HEAD(&adap->clients);
142 /* Add the adapter to the driver core.
143 * If the parent pointer is not set up,
144 * we add this adapter to the host bus.
146 if (adap->dev.parent == NULL)
147 adap->dev.parent = &platform_bus;
148 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
149 adap->dev.driver = &i2c_adapter_driver;
150 adap->dev.release = &i2c_adapter_dev_release;
151 device_register(&adap->dev);
152 device_create_file(&adap->dev, &dev_attr_name);
154 /* Add this adapter to the i2c_adapter class */
155 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
156 adap->class_dev.dev = &adap->dev;
157 adap->class_dev.class = &i2c_adapter_class;
158 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
159 class_device_register(&adap->class_dev);
161 /* inform drivers of new adapters */
162 list_for_each(item,&drivers) {
163 driver = list_entry(item, struct i2c_driver, list);
164 if (driver->flags & I2C_DF_NOTIFY)
165 /* We ignore the return code; if it fails, too bad */
166 driver->attach_adapter(adap);
169 dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);
177 int i2c_del_adapter(struct i2c_adapter *adap)
179 struct list_head *item, *_n;
180 struct i2c_adapter *adap_from_list;
181 struct i2c_driver *driver;
182 struct i2c_client *client;
187 /* First make sure that this adapter was ever added */
188 list_for_each_entry(adap_from_list, &adapters, list) {
189 if (adap_from_list == adap)
192 if (adap_from_list != adap) {
193 pr_debug("I2C: Attempting to delete an unregistered "
199 list_for_each(item,&drivers) {
200 driver = list_entry(item, struct i2c_driver, list);
201 if (driver->detach_adapter)
202 if ((res = driver->detach_adapter(adap))) {
203 dev_warn(&adap->dev, "can't detach adapter "
204 "while detaching driver %s: driver not "
205 "detached!", driver->name);
210 /* detach any active clients. This must be done first, because
211 * it can fail; in which case we give upp. */
212 list_for_each_safe(item, _n, &adap->clients) {
213 client = list_entry(item, struct i2c_client, list);
215 /* detaching devices is unconditional of the set notify
216 * flag, as _all_ clients that reside on the adapter
217 * must be deleted, as this would cause invalid states.
219 if ((res=client->driver->detach_client(client))) {
220 dev_err(&adap->dev, "adapter not "
221 "unregistered, because client at "
222 "address %02x can't be detached. ",
228 /* clean up the sysfs representation */
229 init_completion(&adap->dev_released);
230 init_completion(&adap->class_dev_released);
231 class_device_unregister(&adap->class_dev);
232 device_remove_file(&adap->dev, &dev_attr_name);
233 device_unregister(&adap->dev);
234 list_del(&adap->list);
236 /* wait for sysfs to drop all references */
237 wait_for_completion(&adap->dev_released);
238 wait_for_completion(&adap->class_dev_released);
240 /* free dynamically allocated bus id */
241 idr_remove(&i2c_adapter_idr, adap->nr);
243 dev_dbg(&adap->dev, "adapter unregistered\n");
252 * What follows is the "upwards" interface: commands for talking to clients,
253 * which implement the functions to access the physical information of the
257 int i2c_add_driver(struct i2c_driver *driver)
259 struct list_head *item;
260 struct i2c_adapter *adapter;
265 /* add the driver to the list of i2c drivers in the driver core */
266 driver->driver.name = driver->name;
267 driver->driver.bus = &i2c_bus_type;
268 driver->driver.probe = i2c_device_probe;
269 driver->driver.remove = i2c_device_remove;
271 res = driver_register(&driver->driver);
275 list_add_tail(&driver->list,&drivers);
276 pr_debug("i2c-core: driver %s registered.\n", driver->name);
278 /* now look for instances of driver on our adapters */
279 if (driver->flags & I2C_DF_NOTIFY) {
280 list_for_each(item,&adapters) {
281 adapter = list_entry(item, struct i2c_adapter, list);
282 driver->attach_adapter(adapter);
291 int i2c_del_driver(struct i2c_driver *driver)
293 struct list_head *item1, *item2, *_n;
294 struct i2c_client *client;
295 struct i2c_adapter *adap;
301 /* Have a look at each adapter, if clients of this driver are still
302 * attached. If so, detach them to be able to kill the driver
305 pr_debug("i2c-core: unregister_driver - looking for clients.\n");
306 /* removing clients does not depend on the notify flag, else
307 * invalid operation might (will!) result, when using stale client
310 list_for_each(item1,&adapters) {
311 adap = list_entry(item1, struct i2c_adapter, list);
312 dev_dbg(&adap->dev, "examining adapter\n");
313 if (driver->detach_adapter) {
314 if ((res = driver->detach_adapter(adap))) {
315 dev_warn(&adap->dev, "while unregistering "
316 "dummy driver %s, adapter could "
317 "not be detached properly; driver "
318 "not unloaded!",driver->name);
322 list_for_each_safe(item2, _n, &adap->clients) {
323 client = list_entry(item2, struct i2c_client, list);
324 if (client->driver != driver)
326 pr_debug("i2c-core.o: detaching client %s:\n", client->name);
327 if ((res = driver->detach_client(client))) {
328 dev_err(&adap->dev, "while "
329 "unregistering driver "
330 "`%s', the client at "
333 "be detached; driver "
343 driver_unregister(&driver->driver);
344 list_del(&driver->list);
345 pr_debug("i2c-core: driver unregistered: %s\n", driver->name);
352 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
354 struct list_head *item;
355 struct i2c_client *client;
357 list_for_each(item,&adapter->clients) {
358 client = list_entry(item, struct i2c_client, list);
359 if (client->addr == addr)
365 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
369 down(&adapter->clist_lock);
370 rval = __i2c_check_addr(adapter, addr);
371 up(&adapter->clist_lock);
376 int i2c_attach_client(struct i2c_client *client)
378 struct i2c_adapter *adapter = client->adapter;
380 down(&adapter->clist_lock);
381 if (__i2c_check_addr(client->adapter, client->addr)) {
382 up(&adapter->clist_lock);
385 list_add_tail(&client->list,&adapter->clients);
386 up(&adapter->clist_lock);
388 if (adapter->client_register) {
389 if (adapter->client_register(client)) {
390 dev_warn(&adapter->dev, "warning: client_register "
391 "seems to have failed for client %02x\n",
396 dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",
399 if (client->flags & I2C_CLIENT_ALLOW_USE)
400 client->usage_count = 0;
402 client->dev.parent = &client->adapter->dev;
403 client->dev.driver = &client->driver->driver;
404 client->dev.bus = &i2c_bus_type;
405 client->dev.release = &i2c_client_release;
407 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
408 "%d-%04x", i2c_adapter_id(adapter), client->addr);
409 pr_debug("registering %s\n", client->dev.bus_id);
410 device_register(&client->dev);
411 device_create_file(&client->dev, &dev_attr_client_name);
417 int i2c_detach_client(struct i2c_client *client)
419 struct i2c_adapter *adapter = client->adapter;
422 if ((client->flags & I2C_CLIENT_ALLOW_USE) && (client->usage_count > 0))
425 if (adapter->client_unregister) {
426 res = adapter->client_unregister(client);
428 dev_err(&client->dev,
429 "client_unregister [%s] failed, "
430 "client not detached", client->name);
435 down(&adapter->clist_lock);
436 list_del(&client->list);
437 init_completion(&client->released);
438 device_remove_file(&client->dev, &dev_attr_client_name);
439 device_unregister(&client->dev);
440 up(&adapter->clist_lock);
441 wait_for_completion(&client->released);
447 static int i2c_inc_use_client(struct i2c_client *client)
450 if (!try_module_get(client->driver->owner))
452 if (!try_module_get(client->adapter->owner)) {
453 module_put(client->driver->owner);
460 static void i2c_dec_use_client(struct i2c_client *client)
462 module_put(client->driver->owner);
463 module_put(client->adapter->owner);
466 int i2c_use_client(struct i2c_client *client)
470 ret = i2c_inc_use_client(client);
474 if (client->flags & I2C_CLIENT_ALLOW_USE) {
475 if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
476 client->usage_count++;
477 else if (client->usage_count > 0)
480 client->usage_count++;
485 i2c_dec_use_client(client);
489 int i2c_release_client(struct i2c_client *client)
491 if(client->flags & I2C_CLIENT_ALLOW_USE) {
492 if(client->usage_count>0)
493 client->usage_count--;
495 pr_debug("i2c-core: %s used one too many times\n",
501 i2c_dec_use_client(client);
506 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
508 struct list_head *item;
509 struct i2c_client *client;
511 down(&adap->clist_lock);
512 list_for_each(item,&adap->clients) {
513 client = list_entry(item, struct i2c_client, list);
514 if (!try_module_get(client->driver->owner))
516 if (NULL != client->driver->command) {
517 up(&adap->clist_lock);
518 client->driver->command(client,cmd,arg);
519 down(&adap->clist_lock);
521 module_put(client->driver->owner);
523 up(&adap->clist_lock);
527 /* match always succeeds, as we want the probe() to tell if we really accept this match */
528 static int i2c_device_match(struct device *dev, struct device_driver *drv)
533 static int i2c_bus_suspend(struct device * dev, u32 state)
537 if (dev->driver && dev->driver->suspend)
538 rc = dev->driver->suspend(dev,state,0);
542 static int i2c_bus_resume(struct device * dev)
546 if (dev->driver && dev->driver->resume)
547 rc = dev->driver->resume(dev,0);
551 struct bus_type i2c_bus_type = {
553 .match = i2c_device_match,
554 .suspend = i2c_bus_suspend,
555 .resume = i2c_bus_resume,
558 static int __init i2c_init(void)
562 retval = bus_register(&i2c_bus_type);
565 retval = driver_register(&i2c_adapter_driver);
568 return class_register(&i2c_adapter_class);
571 static void __exit i2c_exit(void)
573 class_unregister(&i2c_adapter_class);
574 driver_unregister(&i2c_adapter_driver);
575 bus_unregister(&i2c_bus_type);
578 subsys_initcall(i2c_init);
579 module_exit(i2c_exit);
581 /* ----------------------------------------------------
582 * the functional interface to the i2c busses.
583 * ----------------------------------------------------
586 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg msgs[],int num)
590 if (adap->algo->master_xfer) {
591 dev_dbg(&adap->dev, "master_xfer: with %d msgs.\n", num);
593 down(&adap->bus_lock);
594 ret = adap->algo->master_xfer(adap,msgs,num);
599 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
604 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
607 struct i2c_adapter *adap=client->adapter;
610 if (client->adapter->algo->master_xfer) {
611 msg.addr = client->addr;
612 msg.flags = client->flags & I2C_M_TEN;
614 msg.buf = (char *)buf;
616 dev_dbg(&client->adapter->dev, "master_send: writing %d bytes.\n",
619 down(&adap->bus_lock);
620 ret = adap->algo->master_xfer(adap,&msg,1);
623 /* if everything went ok (i.e. 1 msg transmitted), return #bytes
624 * transmitted, else error code.
626 return (ret == 1 )? count : ret;
628 dev_err(&client->adapter->dev, "I2C level transfers not supported\n");
633 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
635 struct i2c_adapter *adap=client->adapter;
638 if (client->adapter->algo->master_xfer) {
639 msg.addr = client->addr;
640 msg.flags = client->flags & I2C_M_TEN;
641 msg.flags |= I2C_M_RD;
645 dev_dbg(&client->adapter->dev, "master_recv: reading %d bytes.\n",
648 down(&adap->bus_lock);
649 ret = adap->algo->master_xfer(adap,&msg,1);
652 dev_dbg(&client->adapter->dev, "master_recv: return:%d (count:%d, addr:0x%02x)\n",
653 ret, count, client->addr);
655 /* if everything went ok (i.e. 1 msg transmitted), return #bytes
656 * transmitted, else error code.
658 return (ret == 1 )? count : ret;
660 dev_err(&client->adapter->dev, "I2C level transfers not supported\n");
666 int i2c_control(struct i2c_client *client,
667 unsigned int cmd, unsigned long arg)
670 struct i2c_adapter *adap = client->adapter;
672 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
681 if (adap->algo->algo_control!=NULL)
682 ret = adap->algo->algo_control(adap,cmd,arg);
687 /* ----------------------------------------------------
688 * the i2c address scanning function
689 * Will not work for 10-bit addresses!
690 * ----------------------------------------------------
692 int i2c_probe(struct i2c_adapter *adapter,
693 struct i2c_client_address_data *address_data,
694 int (*found_proc) (struct i2c_adapter *, int, int))
696 int addr,i,found,err;
697 int adap_id = i2c_adapter_id(adapter);
699 /* Forget it if we can't probe using SMBUS_QUICK */
700 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
703 for (addr = 0x00; addr <= 0x7f; addr++) {
705 /* Skip if already in use */
706 if (i2c_check_addr(adapter,addr))
709 /* If it is in one of the force entries, we don't do any detection
713 for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 3) {
714 if (((adap_id == address_data->force[i]) ||
715 (address_data->force[i] == ANY_I2C_BUS)) &&
716 (addr == address_data->force[i+1])) {
717 dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n",
719 if ((err = found_proc(adapter,addr,0)))
727 /* If this address is in one of the ignores, we can forget about
730 !found && (address_data->ignore[i] != I2C_CLIENT_END);
732 if (((adap_id == address_data->ignore[i]) ||
733 ((address_data->ignore[i] == ANY_I2C_BUS))) &&
734 (addr == address_data->ignore[i+1])) {
735 dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
736 "addr %04x\n", adap_id ,addr);
741 !found && (address_data->ignore_range[i] != I2C_CLIENT_END);
743 if (((adap_id == address_data->ignore_range[i]) ||
744 ((address_data->ignore_range[i]==ANY_I2C_BUS))) &&
745 (addr >= address_data->ignore_range[i+1]) &&
746 (addr <= address_data->ignore_range[i+2])) {
747 dev_dbg(&adapter->dev, "found ignore_range parameter for adapter %d, "
748 "addr %04x\n", adap_id,addr);
755 /* Now, we will do a detection, but only if it is in the normal or
758 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
760 if (addr == address_data->normal_i2c[i]) {
762 dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
763 "addr %02x\n", adap_id, addr);
768 !found && (address_data->normal_i2c_range[i] != I2C_CLIENT_END);
770 if ((addr >= address_data->normal_i2c_range[i]) &&
771 (addr <= address_data->normal_i2c_range[i+1])) {
773 dev_dbg(&adapter->dev, "found normal i2c_range entry for adapter %d, "
774 "addr %04x\n", adap_id,addr);
779 !found && (address_data->probe[i] != I2C_CLIENT_END);
781 if (((adap_id == address_data->probe[i]) ||
782 ((address_data->probe[i] == ANY_I2C_BUS))) &&
783 (addr == address_data->probe[i+1])) {
785 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
786 "addr %04x\n", adap_id,addr);
790 !found && (address_data->probe_range[i] != I2C_CLIENT_END);
792 if (((adap_id == address_data->probe_range[i]) ||
793 (address_data->probe_range[i] == ANY_I2C_BUS)) &&
794 (addr >= address_data->probe_range[i+1]) &&
795 (addr <= address_data->probe_range[i+2])) {
797 dev_dbg(&adapter->dev, "found probe_range parameter for adapter %d, "
798 "addr %04x\n", adap_id,addr);
804 /* OK, so we really should examine this address. First check
805 whether there is some client here at all! */
806 if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
807 if ((err = found_proc(adapter,addr,-1)))
814 * return id number for a specific adapter
816 int i2c_adapter_id(struct i2c_adapter *adap)
821 struct i2c_adapter* i2c_get_adapter(int id)
823 struct list_head *item;
824 struct i2c_adapter *adapter;
827 list_for_each(item,&adapters) {
828 adapter = list_entry(item, struct i2c_adapter, list);
829 if (id == adapter->nr &&
830 try_module_get(adapter->owner)) {
839 void i2c_put_adapter(struct i2c_adapter *adap)
841 module_put(adap->owner);
844 /* The SMBus parts */
846 #define POLY (0x1070U << 3)
852 for(i = 0; i < 8; i++) {
857 return (u8)(data >> 8);
860 /* CRC over count bytes in the first array plus the bytes in the rest
861 array if it is non-null. rest[0] is the (length of rest) - 1
863 u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
867 for(i = 0; i < count; i++)
868 crc = crc8((crc ^ first[i]) << 8);
870 for(i = 0; i <= rest[0]; i++)
871 crc = crc8((crc ^ rest[i]) << 8);
875 u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
877 return i2c_smbus_partial_pec(0, count, first, rest);
880 /* Returns new "size" (transaction type)
881 Note that we convert byte to byte_data and byte_data to word_data
882 rather than invent new xxx_PEC transactions. */
883 int i2c_smbus_add_pec(u16 addr, u8 command, int size,
884 union i2c_smbus_data *data)
892 data->byte = i2c_smbus_pec(2, buf, NULL);
893 size = I2C_SMBUS_BYTE_DATA;
895 case I2C_SMBUS_BYTE_DATA:
897 data->word = buf[2] ||
898 (i2c_smbus_pec(3, buf, NULL) << 8);
899 size = I2C_SMBUS_WORD_DATA;
901 case I2C_SMBUS_WORD_DATA:
904 case I2C_SMBUS_BLOCK_DATA:
905 data->block[data->block[0] + 1] =
906 i2c_smbus_pec(2, buf, data->block);
907 size = I2C_SMBUS_BLOCK_DATA_PEC;
913 int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
914 union i2c_smbus_data *data)
916 u8 buf[3], rpec, cpec;
920 case I2C_SMBUS_BYTE_DATA:
921 buf[0] = (addr << 1) | 1;
922 cpec = i2c_smbus_pec(2, buf, NULL);
925 case I2C_SMBUS_WORD_DATA:
926 buf[0] = (addr << 1) | 1;
927 buf[2] = data->word & 0xff;
928 cpec = i2c_smbus_pec(3, buf, NULL);
929 rpec = data->word >> 8;
931 case I2C_SMBUS_WORD_DATA_PEC:
935 case I2C_SMBUS_PROC_CALL_PEC:
939 case I2C_SMBUS_BLOCK_DATA_PEC:
940 buf[0] = (addr << 1);
941 buf[2] = (addr << 1) | 1;
942 cpec = i2c_smbus_pec(3, buf, data->block);
943 rpec = data->block[data->block[0] + 1];
945 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
946 buf[0] = (addr << 1) | 1;
947 rpec = i2c_smbus_partial_pec(partial, 1,
949 cpec = data->block[data->block[0] + 1];
956 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
963 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
965 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
966 value,0,I2C_SMBUS_QUICK,NULL);
969 s32 i2c_smbus_read_byte(struct i2c_client *client)
971 union i2c_smbus_data data;
972 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
973 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
976 return 0x0FF & data.byte;
979 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
981 union i2c_smbus_data data; /* only for PEC */
982 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
983 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
986 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
988 union i2c_smbus_data data;
989 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
990 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
993 return 0x0FF & data.byte;
996 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
998 union i2c_smbus_data data;
1000 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1001 I2C_SMBUS_WRITE,command,
1002 I2C_SMBUS_BYTE_DATA,&data);
1005 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1007 union i2c_smbus_data data;
1008 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1009 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1012 return 0x0FFFF & data.word;
1015 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1017 union i2c_smbus_data data;
1019 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1020 I2C_SMBUS_WRITE,command,
1021 I2C_SMBUS_WORD_DATA,&data);
1024 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1025 u8 length, u8 *values)
1027 union i2c_smbus_data data;
1029 if (length > I2C_SMBUS_BLOCK_MAX)
1030 length = I2C_SMBUS_BLOCK_MAX;
1031 for (i = 1; i <= length; i++)
1032 data.block[i] = values[i-1];
1033 data.block[0] = length;
1034 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1035 I2C_SMBUS_WRITE,command,
1036 I2C_SMBUS_BLOCK_DATA,&data);
1039 /* Returns the number of read bytes */
1040 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1042 union i2c_smbus_data data;
1044 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1045 I2C_SMBUS_READ,command,
1046 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1049 for (i = 1; i <= data.block[0]; i++)
1050 values[i-1] = data.block[i];
1051 return data.block[0];
1055 /* Simulate a SMBus command using the i2c protocol
1056 No checking of parameters is done! */
1057 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1058 unsigned short flags,
1059 char read_write, u8 command, int size,
1060 union i2c_smbus_data * data)
1062 /* So we need to generate a series of msgs. In the case of writing, we
1063 need to use only one message; when reading, we need two. We initialize
1064 most things with sane defaults, to keep the code below somewhat
1066 unsigned char msgbuf0[34];
1067 unsigned char msgbuf1[34];
1068 int num = read_write == I2C_SMBUS_READ?2:1;
1069 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1070 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1074 msgbuf0[0] = command;
1076 case I2C_SMBUS_QUICK:
1078 /* Special case: The read/write field is used as data */
1079 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1082 case I2C_SMBUS_BYTE:
1083 if (read_write == I2C_SMBUS_READ) {
1084 /* Special case: only a read! */
1085 msg[0].flags = I2C_M_RD | flags;
1089 case I2C_SMBUS_BYTE_DATA:
1090 if (read_write == I2C_SMBUS_READ)
1094 msgbuf0[1] = data->byte;
1097 case I2C_SMBUS_WORD_DATA:
1098 if (read_write == I2C_SMBUS_READ)
1102 msgbuf0[1] = data->word & 0xff;
1103 msgbuf0[2] = (data->word >> 8) & 0xff;
1106 case I2C_SMBUS_PROC_CALL:
1107 num = 2; /* Special case */
1108 read_write = I2C_SMBUS_READ;
1111 msgbuf0[1] = data->word & 0xff;
1112 msgbuf0[2] = (data->word >> 8) & 0xff;
1114 case I2C_SMBUS_BLOCK_DATA:
1115 case I2C_SMBUS_BLOCK_DATA_PEC:
1116 if (read_write == I2C_SMBUS_READ) {
1117 dev_err(&adapter->dev, "Block read not supported "
1118 "under I2C emulation!\n");
1121 msg[0].len = data->block[0] + 2;
1122 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1123 dev_err(&adapter->dev, "smbus_access called with "
1124 "invalid block write size (%d)\n",
1128 if(size == I2C_SMBUS_BLOCK_DATA_PEC)
1130 for (i = 1; i <= msg[0].len; i++)
1131 msgbuf0[i] = data->block[i-1];
1134 case I2C_SMBUS_BLOCK_PROC_CALL:
1135 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
1136 dev_dbg(&adapter->dev, "Block process call not supported "
1137 "under I2C emulation!\n");
1139 case I2C_SMBUS_I2C_BLOCK_DATA:
1140 if (read_write == I2C_SMBUS_READ) {
1141 msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
1143 msg[0].len = data->block[0] + 1;
1144 if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
1145 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1146 "invalid block write size (%d)\n",
1150 for (i = 1; i <= data->block[0]; i++)
1151 msgbuf0[i] = data->block[i];
1155 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1160 if (i2c_transfer(adapter, msg, num) < 0)
1163 if (read_write == I2C_SMBUS_READ)
1165 case I2C_SMBUS_BYTE:
1166 data->byte = msgbuf0[0];
1168 case I2C_SMBUS_BYTE_DATA:
1169 data->byte = msgbuf1[0];
1171 case I2C_SMBUS_WORD_DATA:
1172 case I2C_SMBUS_PROC_CALL:
1173 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1175 case I2C_SMBUS_I2C_BLOCK_DATA:
1176 /* fixed at 32 for now */
1177 data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
1178 for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
1179 data->block[i+1] = msgbuf1[i];
1186 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1187 char read_write, u8 command, int size,
1188 union i2c_smbus_data * data)
1194 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1195 if((flags & I2C_CLIENT_PEC) &&
1196 !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
1198 if(read_write == I2C_SMBUS_READ &&
1199 size == I2C_SMBUS_BLOCK_DATA)
1200 size = I2C_SMBUS_BLOCK_DATA_PEC;
1201 else if(size == I2C_SMBUS_PROC_CALL)
1202 size = I2C_SMBUS_PROC_CALL_PEC;
1203 else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
1204 i2c_smbus_add_pec(addr, command,
1205 I2C_SMBUS_BLOCK_DATA, data);
1206 partial = data->block[data->block[0] + 1];
1207 size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
1208 } else if(read_write == I2C_SMBUS_WRITE &&
1209 size != I2C_SMBUS_QUICK &&
1210 size != I2C_SMBUS_I2C_BLOCK_DATA)
1211 size = i2c_smbus_add_pec(addr, command, size, data);
1214 if (adapter->algo->smbus_xfer) {
1215 down(&adapter->bus_lock);
1216 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1218 up(&adapter->bus_lock);
1220 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1223 if(res >= 0 && swpec &&
1224 size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
1225 (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
1226 size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
1227 if(i2c_smbus_check_pec(addr, command, size, partial, data))
1234 /* You should always define `functionality'; the 'else' is just for
1235 backward compatibility. */
1236 u32 i2c_get_functionality (struct i2c_adapter *adap)
1238 if (adap->algo->functionality)
1239 return adap->algo->functionality(adap);
1244 int i2c_check_functionality (struct i2c_adapter *adap, u32 func)
1246 u32 adap_func = i2c_get_functionality (adap);
1247 return (func & adap_func) == func;
1250 EXPORT_SYMBOL(i2c_add_adapter);
1251 EXPORT_SYMBOL(i2c_del_adapter);
1252 EXPORT_SYMBOL(i2c_add_driver);
1253 EXPORT_SYMBOL(i2c_del_driver);
1254 EXPORT_SYMBOL(i2c_attach_client);
1255 EXPORT_SYMBOL(i2c_detach_client);
1256 EXPORT_SYMBOL(i2c_use_client);
1257 EXPORT_SYMBOL(i2c_release_client);
1258 EXPORT_SYMBOL(i2c_clients_command);
1259 EXPORT_SYMBOL(i2c_check_addr);
1261 EXPORT_SYMBOL(i2c_master_send);
1262 EXPORT_SYMBOL(i2c_master_recv);
1263 EXPORT_SYMBOL(i2c_control);
1264 EXPORT_SYMBOL(i2c_transfer);
1265 EXPORT_SYMBOL(i2c_adapter_id);
1266 EXPORT_SYMBOL(i2c_get_adapter);
1267 EXPORT_SYMBOL(i2c_put_adapter);
1268 EXPORT_SYMBOL(i2c_probe);
1270 EXPORT_SYMBOL(i2c_smbus_xfer);
1271 EXPORT_SYMBOL(i2c_smbus_write_quick);
1272 EXPORT_SYMBOL(i2c_smbus_read_byte);
1273 EXPORT_SYMBOL(i2c_smbus_write_byte);
1274 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1275 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1276 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1277 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1278 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1279 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1281 EXPORT_SYMBOL(i2c_get_functionality);
1282 EXPORT_SYMBOL(i2c_check_functionality);
1284 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1285 MODULE_DESCRIPTION("I2C-Bus main module");
1286 MODULE_LICENSE("GPL");