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)
46 static int i2c_bus_suspend(struct device * dev, pm_message_t state)
50 if (dev->driver && dev->driver->suspend)
51 rc = dev->driver->suspend(dev,state,0);
55 static int i2c_bus_resume(struct device * dev)
59 if (dev->driver && dev->driver->resume)
60 rc = dev->driver->resume(dev,0);
64 static struct bus_type i2c_bus_type = {
66 .match = i2c_device_match,
67 .suspend = i2c_bus_suspend,
68 .resume = i2c_bus_resume,
71 static int i2c_device_probe(struct device *dev)
76 static int i2c_device_remove(struct device *dev)
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",
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 * ---------------------------------------------------
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)
149 struct list_head *item;
150 struct i2c_driver *driver;
154 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
159 res = idr_get_new(&i2c_adapter_idr, adap, &id);
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);
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;
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)
222 if (adap_from_list != adap) {
223 pr_debug("I2C: Attempting to delete an unregistered "
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 "
235 "not detached!\n", driver->name);
240 /* detach any active clients. This must be done first, because
241 * it can fail; in which case we give up. */
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. ",
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");
282 * What follows is the "upwards" interface: commands for talking to clients,
283 * which implement the functions to access the physical information of the
287 int i2c_add_driver(struct i2c_driver *driver)
289 struct list_head *item;
290 struct i2c_adapter *adapter;
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);
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);
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;
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
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
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);
352 list_for_each_safe(item2, _n, &adap->clients) {
353 client = list_entry(item2, struct i2c_client, list);
354 if (client->driver != driver)
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 "
363 "be detached; driver "
373 driver_unregister(&driver->driver);
374 list_del(&driver->list);
375 pr_debug("i2c-core: driver unregistered: %s\n", driver->name);
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)
395 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
399 down(&adapter->clist_lock);
400 rval = __i2c_check_addr(adapter, addr);
401 up(&adapter->clist_lock);
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);
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",
426 dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",
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);
447 int i2c_detach_client(struct i2c_client *client)
449 struct i2c_adapter *adapter = client->adapter;
452 if ((client->flags & I2C_CLIENT_ALLOW_USE) && (client->usage_count > 0))
455 if (adapter->client_unregister) {
456 res = adapter->client_unregister(client);
458 dev_err(&client->dev,
459 "client_unregister [%s] failed, "
460 "client not detached\n", client->name);
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);
477 static int i2c_inc_use_client(struct i2c_client *client)
480 if (!try_module_get(client->driver->owner))
482 if (!try_module_get(client->adapter->owner)) {
483 module_put(client->driver->owner);
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)
500 ret = i2c_inc_use_client(client);
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)
510 client->usage_count++;
515 i2c_dec_use_client(client);
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--;
525 pr_debug("i2c-core: %s used one too many times\n",
531 i2c_dec_use_client(client);
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))
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)
560 retval = bus_register(&i2c_bus_type);
563 retval = driver_register(&i2c_adapter_driver);
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)
588 if (adap->algo->master_xfer) {
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);
597 down(&adap->bus_lock);
598 ret = adap->algo->master_xfer(adap,msgs,num);
603 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
608 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
611 struct i2c_adapter *adap=client->adapter;
614 msg.addr = client->addr;
615 msg.flags = client->flags & I2C_M_TEN;
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;
632 msg.addr = client->addr;
633 msg.flags = client->flags & I2C_M_TEN;
634 msg.flags |= I2C_M_RD;
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)
650 struct i2c_adapter *adap = client->adapter;
652 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
661 if (adap->algo->algo_control!=NULL)
662 ret = adap->algo->algo_control(adap,cmd,arg);
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))
683 for (addr = 0x00; addr <= 0x7f; addr++) {
685 /* Skip if already in use */
686 if (i2c_check_addr(adapter,addr))
689 /* If it is in one of the force entries, we don't do any detection
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",
699 if ((err = found_proc(adapter,addr,0)))
707 /* If this address is in one of the ignores, we can forget about
710 !found && (address_data->ignore[i] != I2C_CLIENT_END);
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);
723 /* Now, we will do a detection, but only if it is in the normal or
726 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
728 if (addr == address_data->normal_i2c[i]) {
730 dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
731 "addr %02x\n", adap_id, addr);
736 !found && (address_data->probe[i] != I2C_CLIENT_END);
738 if (((adap_id == address_data->probe[i]) ||
739 ((address_data->probe[i] == ANY_I2C_BUS))) &&
740 (addr == address_data->probe[i+1])) {
742 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
743 "addr %04x\n", adap_id,addr);
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)))
759 * return id number for a specific adapter
761 int i2c_adapter_id(struct i2c_adapter *adap)
766 struct i2c_adapter* i2c_get_adapter(int id)
768 struct i2c_adapter *adapter;
771 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
772 if (adapter && !try_module_get(adapter->owner))
779 void i2c_put_adapter(struct i2c_adapter *adap)
781 module_put(adap->owner);
784 /* The SMBus parts */
786 #define POLY (0x1070U << 3)
792 for(i = 0; i < 8; i++) {
797 return (u8)(data >> 8);
800 /* CRC over count bytes in the first array plus the bytes in the rest
801 array if it is non-null. rest[0] is the (length of rest) - 1
803 static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
807 for(i = 0; i < count; i++)
808 crc = crc8((crc ^ first[i]) << 8);
810 for(i = 0; i <= rest[0]; i++)
811 crc = crc8((crc ^ rest[i]) << 8);
815 static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
817 return i2c_smbus_partial_pec(0, count, first, rest);
820 /* Returns new "size" (transaction type)
821 Note that we convert byte to byte_data and byte_data to word_data
822 rather than invent new xxx_PEC transactions. */
823 static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
824 union i2c_smbus_data *data)
832 data->byte = i2c_smbus_pec(2, buf, NULL);
833 size = I2C_SMBUS_BYTE_DATA;
835 case I2C_SMBUS_BYTE_DATA:
837 data->word = buf[2] ||
838 (i2c_smbus_pec(3, buf, NULL) << 8);
839 size = I2C_SMBUS_WORD_DATA;
841 case I2C_SMBUS_WORD_DATA:
844 case I2C_SMBUS_BLOCK_DATA:
845 data->block[data->block[0] + 1] =
846 i2c_smbus_pec(2, buf, data->block);
847 size = I2C_SMBUS_BLOCK_DATA_PEC;
853 static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
854 union i2c_smbus_data *data)
856 u8 buf[3], rpec, cpec;
860 case I2C_SMBUS_BYTE_DATA:
861 buf[0] = (addr << 1) | 1;
862 cpec = i2c_smbus_pec(2, buf, NULL);
865 case I2C_SMBUS_WORD_DATA:
866 buf[0] = (addr << 1) | 1;
867 buf[2] = data->word & 0xff;
868 cpec = i2c_smbus_pec(3, buf, NULL);
869 rpec = data->word >> 8;
871 case I2C_SMBUS_WORD_DATA_PEC:
875 case I2C_SMBUS_PROC_CALL_PEC:
879 case I2C_SMBUS_BLOCK_DATA_PEC:
880 buf[0] = (addr << 1);
881 buf[2] = (addr << 1) | 1;
882 cpec = i2c_smbus_pec(3, buf, data->block);
883 rpec = data->block[data->block[0] + 1];
885 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
886 buf[0] = (addr << 1) | 1;
887 rpec = i2c_smbus_partial_pec(partial, 1,
889 cpec = data->block[data->block[0] + 1];
896 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
903 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
905 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
906 value,0,I2C_SMBUS_QUICK,NULL);
909 s32 i2c_smbus_read_byte(struct i2c_client *client)
911 union i2c_smbus_data data;
912 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
913 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
916 return 0x0FF & data.byte;
919 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
921 union i2c_smbus_data data; /* only for PEC */
922 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
923 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
926 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
928 union i2c_smbus_data data;
929 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
930 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
933 return 0x0FF & data.byte;
936 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
938 union i2c_smbus_data data;
940 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
941 I2C_SMBUS_WRITE,command,
942 I2C_SMBUS_BYTE_DATA,&data);
945 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
947 union i2c_smbus_data data;
948 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
949 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
952 return 0x0FFFF & data.word;
955 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
957 union i2c_smbus_data data;
959 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
960 I2C_SMBUS_WRITE,command,
961 I2C_SMBUS_WORD_DATA,&data);
964 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
965 u8 length, u8 *values)
967 union i2c_smbus_data data;
969 if (length > I2C_SMBUS_BLOCK_MAX)
970 length = I2C_SMBUS_BLOCK_MAX;
971 for (i = 1; i <= length; i++)
972 data.block[i] = values[i-1];
973 data.block[0] = length;
974 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
975 I2C_SMBUS_WRITE,command,
976 I2C_SMBUS_BLOCK_DATA,&data);
979 /* Returns the number of read bytes */
980 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
982 union i2c_smbus_data data;
984 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
985 I2C_SMBUS_READ,command,
986 I2C_SMBUS_I2C_BLOCK_DATA,&data))
989 for (i = 1; i <= data.block[0]; i++)
990 values[i-1] = data.block[i];
991 return data.block[0];
995 /* Simulate a SMBus command using the i2c protocol
996 No checking of parameters is done! */
997 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
998 unsigned short flags,
999 char read_write, u8 command, int size,
1000 union i2c_smbus_data * data)
1002 /* So we need to generate a series of msgs. In the case of writing, we
1003 need to use only one message; when reading, we need two. We initialize
1004 most things with sane defaults, to keep the code below somewhat
1006 unsigned char msgbuf0[34];
1007 unsigned char msgbuf1[34];
1008 int num = read_write == I2C_SMBUS_READ?2:1;
1009 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1010 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1014 msgbuf0[0] = command;
1016 case I2C_SMBUS_QUICK:
1018 /* Special case: The read/write field is used as data */
1019 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1022 case I2C_SMBUS_BYTE:
1023 if (read_write == I2C_SMBUS_READ) {
1024 /* Special case: only a read! */
1025 msg[0].flags = I2C_M_RD | flags;
1029 case I2C_SMBUS_BYTE_DATA:
1030 if (read_write == I2C_SMBUS_READ)
1034 msgbuf0[1] = data->byte;
1037 case I2C_SMBUS_WORD_DATA:
1038 if (read_write == I2C_SMBUS_READ)
1042 msgbuf0[1] = data->word & 0xff;
1043 msgbuf0[2] = (data->word >> 8) & 0xff;
1046 case I2C_SMBUS_PROC_CALL:
1047 num = 2; /* Special case */
1048 read_write = I2C_SMBUS_READ;
1051 msgbuf0[1] = data->word & 0xff;
1052 msgbuf0[2] = (data->word >> 8) & 0xff;
1054 case I2C_SMBUS_BLOCK_DATA:
1055 case I2C_SMBUS_BLOCK_DATA_PEC:
1056 if (read_write == I2C_SMBUS_READ) {
1057 dev_err(&adapter->dev, "Block read not supported "
1058 "under I2C emulation!\n");
1061 msg[0].len = data->block[0] + 2;
1062 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1063 dev_err(&adapter->dev, "smbus_access called with "
1064 "invalid block write size (%d)\n",
1068 if(size == I2C_SMBUS_BLOCK_DATA_PEC)
1070 for (i = 1; i <= msg[0].len; i++)
1071 msgbuf0[i] = data->block[i-1];
1074 case I2C_SMBUS_BLOCK_PROC_CALL:
1075 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
1076 dev_dbg(&adapter->dev, "Block process call not supported "
1077 "under I2C emulation!\n");
1079 case I2C_SMBUS_I2C_BLOCK_DATA:
1080 if (read_write == I2C_SMBUS_READ) {
1081 msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
1083 msg[0].len = data->block[0] + 1;
1084 if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
1085 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1086 "invalid block write size (%d)\n",
1090 for (i = 1; i <= data->block[0]; i++)
1091 msgbuf0[i] = data->block[i];
1095 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1100 if (i2c_transfer(adapter, msg, num) < 0)
1103 if (read_write == I2C_SMBUS_READ)
1105 case I2C_SMBUS_BYTE:
1106 data->byte = msgbuf0[0];
1108 case I2C_SMBUS_BYTE_DATA:
1109 data->byte = msgbuf1[0];
1111 case I2C_SMBUS_WORD_DATA:
1112 case I2C_SMBUS_PROC_CALL:
1113 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1115 case I2C_SMBUS_I2C_BLOCK_DATA:
1116 /* fixed at 32 for now */
1117 data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
1118 for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
1119 data->block[i+1] = msgbuf1[i];
1126 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1127 char read_write, u8 command, int size,
1128 union i2c_smbus_data * data)
1134 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1135 if((flags & I2C_CLIENT_PEC) &&
1136 !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
1138 if(read_write == I2C_SMBUS_READ &&
1139 size == I2C_SMBUS_BLOCK_DATA)
1140 size = I2C_SMBUS_BLOCK_DATA_PEC;
1141 else if(size == I2C_SMBUS_PROC_CALL)
1142 size = I2C_SMBUS_PROC_CALL_PEC;
1143 else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
1144 i2c_smbus_add_pec(addr, command,
1145 I2C_SMBUS_BLOCK_DATA, data);
1146 partial = data->block[data->block[0] + 1];
1147 size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
1148 } else if(read_write == I2C_SMBUS_WRITE &&
1149 size != I2C_SMBUS_QUICK &&
1150 size != I2C_SMBUS_I2C_BLOCK_DATA)
1151 size = i2c_smbus_add_pec(addr, command, size, data);
1154 if (adapter->algo->smbus_xfer) {
1155 down(&adapter->bus_lock);
1156 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1158 up(&adapter->bus_lock);
1160 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1163 if(res >= 0 && swpec &&
1164 size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
1165 (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
1166 size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
1167 if(i2c_smbus_check_pec(addr, command, size, partial, data))
1174 EXPORT_SYMBOL(i2c_add_adapter);
1175 EXPORT_SYMBOL(i2c_del_adapter);
1176 EXPORT_SYMBOL(i2c_add_driver);
1177 EXPORT_SYMBOL(i2c_del_driver);
1178 EXPORT_SYMBOL(i2c_attach_client);
1179 EXPORT_SYMBOL(i2c_detach_client);
1180 EXPORT_SYMBOL(i2c_use_client);
1181 EXPORT_SYMBOL(i2c_release_client);
1182 EXPORT_SYMBOL(i2c_clients_command);
1183 EXPORT_SYMBOL(i2c_check_addr);
1185 EXPORT_SYMBOL(i2c_master_send);
1186 EXPORT_SYMBOL(i2c_master_recv);
1187 EXPORT_SYMBOL(i2c_control);
1188 EXPORT_SYMBOL(i2c_transfer);
1189 EXPORT_SYMBOL(i2c_adapter_id);
1190 EXPORT_SYMBOL(i2c_get_adapter);
1191 EXPORT_SYMBOL(i2c_put_adapter);
1192 EXPORT_SYMBOL(i2c_probe);
1194 EXPORT_SYMBOL(i2c_smbus_xfer);
1195 EXPORT_SYMBOL(i2c_smbus_write_quick);
1196 EXPORT_SYMBOL(i2c_smbus_read_byte);
1197 EXPORT_SYMBOL(i2c_smbus_write_byte);
1198 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1199 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1200 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1201 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1202 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1203 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1205 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1206 MODULE_DESCRIPTION("I2C-Bus main module");
1207 MODULE_LICENSE("GPL");