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);
55 static int i2c_bus_resume(struct device * dev)
59 if (dev->driver && dev->driver->resume)
60 rc = dev->driver->resume(dev);
64 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 void i2c_adapter_dev_release(struct device *dev)
83 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
84 complete(&adap->dev_released);
87 struct device_driver i2c_adapter_driver = {
89 .name = "i2c_adapter",
91 .probe = i2c_device_probe,
92 .remove = i2c_device_remove,
95 static void i2c_adapter_class_dev_release(struct class_device *dev)
97 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
98 complete(&adap->class_dev_released);
101 struct class i2c_adapter_class = {
102 .owner = THIS_MODULE,
103 .name = "i2c-adapter",
104 .release = &i2c_adapter_class_dev_release,
107 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
109 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
110 return sprintf(buf, "%s\n", adap->name);
112 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
115 static void i2c_client_release(struct device *dev)
117 struct i2c_client *client = to_i2c_client(dev);
118 complete(&client->released);
121 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
123 struct i2c_client *client = to_i2c_client(dev);
124 return sprintf(buf, "%s\n", client->name);
128 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
129 * different type of a device. So beware if the DEVICE_ATTR() macro ever
130 * changes, this definition will also have to change.
132 static struct device_attribute dev_attr_client_name = {
133 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
134 .show = &show_client_name,
138 /* ---------------------------------------------------
139 * registering functions
140 * ---------------------------------------------------
144 * i2c_add_adapter is called from within the algorithm layer,
145 * when a new hw adapter registers. A new device is register to be
146 * available for clients.
148 int i2c_add_adapter(struct i2c_adapter *adap)
151 struct list_head *item;
152 struct i2c_driver *driver;
156 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
161 res = idr_get_new(&i2c_adapter_idr, adap, &id);
168 adap->nr = id & MAX_ID_MASK;
169 init_MUTEX(&adap->bus_lock);
170 init_MUTEX(&adap->clist_lock);
171 list_add_tail(&adap->list,&adapters);
172 INIT_LIST_HEAD(&adap->clients);
174 /* Add the adapter to the driver core.
175 * If the parent pointer is not set up,
176 * we add this adapter to the host bus.
178 if (adap->dev.parent == NULL)
179 adap->dev.parent = &platform_bus;
180 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
181 adap->dev.driver = &i2c_adapter_driver;
182 adap->dev.release = &i2c_adapter_dev_release;
183 device_register(&adap->dev);
184 device_create_file(&adap->dev, &dev_attr_name);
186 /* Add this adapter to the i2c_adapter class */
187 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
188 adap->class_dev.dev = &adap->dev;
189 adap->class_dev.class = &i2c_adapter_class;
190 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
191 class_device_register(&adap->class_dev);
193 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
195 /* inform drivers of new adapters */
196 list_for_each(item,&drivers) {
197 driver = list_entry(item, struct i2c_driver, list);
198 if (driver->flags & I2C_DF_NOTIFY)
199 /* We ignore the return code; if it fails, too bad */
200 driver->attach_adapter(adap);
209 int i2c_del_adapter(struct i2c_adapter *adap)
211 struct list_head *item, *_n;
212 struct i2c_adapter *adap_from_list;
213 struct i2c_driver *driver;
214 struct i2c_client *client;
219 /* First make sure that this adapter was ever added */
220 list_for_each_entry(adap_from_list, &adapters, list) {
221 if (adap_from_list == adap)
224 if (adap_from_list != adap) {
225 pr_debug("i2c-core: attempting to delete unregistered "
226 "adapter [%s]\n", adap->name);
231 list_for_each(item,&drivers) {
232 driver = list_entry(item, struct i2c_driver, list);
233 if (driver->detach_adapter)
234 if ((res = driver->detach_adapter(adap))) {
235 dev_err(&adap->dev, "detach_adapter failed "
236 "for driver [%s]\n", driver->name);
241 /* detach any active clients. This must be done first, because
242 * it can fail; in which case we give up. */
243 list_for_each_safe(item, _n, &adap->clients) {
244 client = list_entry(item, struct i2c_client, list);
246 /* detaching devices is unconditional of the set notify
247 * flag, as _all_ clients that reside on the adapter
248 * must be deleted, as this would cause invalid states.
250 if ((res=client->driver->detach_client(client))) {
251 dev_err(&adap->dev, "detach_client failed for client "
252 "[%s] at address 0x%02x\n", client->name,
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 [%s] unregistered\n", adap->name);
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.owner = driver->owner;
297 driver->driver.name = driver->name;
298 driver->driver.bus = &i2c_bus_type;
299 driver->driver.probe = i2c_device_probe;
300 driver->driver.remove = i2c_device_remove;
302 res = driver_register(&driver->driver);
306 list_add_tail(&driver->list,&drivers);
307 pr_debug("i2c-core: driver [%s] registered\n", driver->name);
309 /* now look for instances of driver on our adapters */
310 if (driver->flags & I2C_DF_NOTIFY) {
311 list_for_each(item,&adapters) {
312 adapter = list_entry(item, struct i2c_adapter, list);
313 driver->attach_adapter(adapter);
322 int i2c_del_driver(struct i2c_driver *driver)
324 struct list_head *item1, *item2, *_n;
325 struct i2c_client *client;
326 struct i2c_adapter *adap;
332 /* Have a look at each adapter, if clients of this driver are still
333 * attached. If so, detach them to be able to kill the driver
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 if (driver->detach_adapter) {
343 if ((res = driver->detach_adapter(adap))) {
344 dev_err(&adap->dev, "detach_adapter failed "
345 "for driver [%s]\n", driver->name);
349 list_for_each_safe(item2, _n, &adap->clients) {
350 client = list_entry(item2, struct i2c_client, list);
351 if (client->driver != driver)
353 dev_dbg(&adap->dev, "detaching client [%s] "
354 "at 0x%02x\n", client->name,
356 if ((res = driver->detach_client(client))) {
357 dev_err(&adap->dev, "detach_client "
358 "failed for client [%s] at "
359 "0x%02x\n", client->name,
367 driver_unregister(&driver->driver);
368 list_del(&driver->list);
369 pr_debug("i2c-core: driver [%s] unregistered\n", driver->name);
376 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
378 struct list_head *item;
379 struct i2c_client *client;
381 list_for_each(item,&adapter->clients) {
382 client = list_entry(item, struct i2c_client, list);
383 if (client->addr == addr)
389 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
393 down(&adapter->clist_lock);
394 rval = __i2c_check_addr(adapter, addr);
395 up(&adapter->clist_lock);
400 int i2c_attach_client(struct i2c_client *client)
402 struct i2c_adapter *adapter = client->adapter;
404 down(&adapter->clist_lock);
405 if (__i2c_check_addr(client->adapter, client->addr)) {
406 up(&adapter->clist_lock);
409 list_add_tail(&client->list,&adapter->clients);
410 up(&adapter->clist_lock);
412 if (adapter->client_register) {
413 if (adapter->client_register(client)) {
414 dev_dbg(&adapter->dev, "client_register "
415 "failed for client [%s] at 0x%02x\n",
416 client->name, client->addr);
420 if (client->flags & I2C_CLIENT_ALLOW_USE)
421 client->usage_count = 0;
423 client->dev.parent = &client->adapter->dev;
424 client->dev.driver = &client->driver->driver;
425 client->dev.bus = &i2c_bus_type;
426 client->dev.release = &i2c_client_release;
428 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
429 "%d-%04x", i2c_adapter_id(adapter), client->addr);
430 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
431 client->name, client->dev.bus_id);
432 device_register(&client->dev);
433 device_create_file(&client->dev, &dev_attr_client_name);
439 int i2c_detach_client(struct i2c_client *client)
441 struct i2c_adapter *adapter = client->adapter;
444 if ((client->flags & I2C_CLIENT_ALLOW_USE)
445 && (client->usage_count > 0)) {
446 dev_warn(&client->dev, "Client [%s] still busy, "
447 "can't detach\n", client->name);
451 if (adapter->client_unregister) {
452 res = adapter->client_unregister(client);
454 dev_err(&client->dev,
455 "client_unregister [%s] failed, "
456 "client not detached\n", client->name);
461 down(&adapter->clist_lock);
462 list_del(&client->list);
463 init_completion(&client->released);
464 device_remove_file(&client->dev, &dev_attr_client_name);
465 device_unregister(&client->dev);
466 up(&adapter->clist_lock);
467 wait_for_completion(&client->released);
473 static int i2c_inc_use_client(struct i2c_client *client)
476 if (!try_module_get(client->driver->owner))
478 if (!try_module_get(client->adapter->owner)) {
479 module_put(client->driver->owner);
486 static void i2c_dec_use_client(struct i2c_client *client)
488 module_put(client->driver->owner);
489 module_put(client->adapter->owner);
492 int i2c_use_client(struct i2c_client *client)
496 ret = i2c_inc_use_client(client);
500 if (client->flags & I2C_CLIENT_ALLOW_USE) {
501 if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
502 client->usage_count++;
503 else if (client->usage_count > 0)
506 client->usage_count++;
511 i2c_dec_use_client(client);
515 int i2c_release_client(struct i2c_client *client)
517 if(client->flags & I2C_CLIENT_ALLOW_USE) {
518 if(client->usage_count>0)
519 client->usage_count--;
521 pr_debug("i2c-core: %s used one too many times\n",
527 i2c_dec_use_client(client);
532 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
534 struct list_head *item;
535 struct i2c_client *client;
537 down(&adap->clist_lock);
538 list_for_each(item,&adap->clients) {
539 client = list_entry(item, struct i2c_client, list);
540 if (!try_module_get(client->driver->owner))
542 if (NULL != client->driver->command) {
543 up(&adap->clist_lock);
544 client->driver->command(client,cmd,arg);
545 down(&adap->clist_lock);
547 module_put(client->driver->owner);
549 up(&adap->clist_lock);
552 static int __init i2c_init(void)
556 retval = bus_register(&i2c_bus_type);
559 retval = driver_register(&i2c_adapter_driver);
562 return class_register(&i2c_adapter_class);
565 static void __exit i2c_exit(void)
567 class_unregister(&i2c_adapter_class);
568 driver_unregister(&i2c_adapter_driver);
569 bus_unregister(&i2c_bus_type);
572 subsys_initcall(i2c_init);
573 module_exit(i2c_exit);
575 /* ----------------------------------------------------
576 * the functional interface to the i2c busses.
577 * ----------------------------------------------------
580 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
584 if (adap->algo->master_xfer) {
586 for (ret = 0; ret < num; ret++) {
587 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
588 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
589 'R' : 'W', msgs[ret].addr, msgs[ret].len);
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 msg.addr = client->addr;
611 msg.flags = client->flags & I2C_M_TEN;
613 msg.buf = (char *)buf;
615 ret = i2c_transfer(adap, &msg, 1);
617 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
618 transmitted, else error code. */
619 return (ret == 1) ? count : ret;
622 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
624 struct i2c_adapter *adap=client->adapter;
628 msg.addr = client->addr;
629 msg.flags = client->flags & I2C_M_TEN;
630 msg.flags |= I2C_M_RD;
634 ret = i2c_transfer(adap, &msg, 1);
636 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
637 transmitted, else error code. */
638 return (ret == 1) ? count : ret;
642 int i2c_control(struct i2c_client *client,
643 unsigned int cmd, unsigned long arg)
646 struct i2c_adapter *adap = client->adapter;
648 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
657 if (adap->algo->algo_control!=NULL)
658 ret = adap->algo->algo_control(adap,cmd,arg);
663 /* ----------------------------------------------------
664 * the i2c address scanning function
665 * Will not work for 10-bit addresses!
666 * ----------------------------------------------------
668 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
669 int (*found_proc) (struct i2c_adapter *, int, int))
673 /* Make sure the address is valid */
674 if (addr < 0x03 || addr > 0x77) {
675 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
680 /* Skip if already in use */
681 if (i2c_check_addr(adapter, addr))
684 /* Make sure there is something at this address, unless forced */
686 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
687 I2C_SMBUS_QUICK, NULL) < 0)
690 /* prevent 24RF08 corruption */
691 if ((addr & ~0x0f) == 0x50)
692 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
693 I2C_SMBUS_QUICK, NULL);
696 /* Finally call the custom detection function */
697 err = found_proc(adapter, addr, kind);
699 /* -ENODEV can be returned if there is a chip at the given address
700 but it isn't supported by this chip driver. We catch it here as
701 this isn't an error. */
702 return (err == -ENODEV) ? 0 : err;
705 int i2c_probe(struct i2c_adapter *adapter,
706 struct i2c_client_address_data *address_data,
707 int (*found_proc) (struct i2c_adapter *, int, int))
710 int adap_id = i2c_adapter_id(adapter);
712 /* Force entries are done first, and are not affected by ignore
714 if (address_data->forces) {
715 unsigned short **forces = address_data->forces;
718 for (kind = 0; forces[kind]; kind++) {
719 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
721 if (forces[kind][i] == adap_id
722 || forces[kind][i] == ANY_I2C_BUS) {
723 dev_dbg(&adapter->dev, "found force "
724 "parameter for adapter %d, "
725 "addr 0x%02x, kind %d\n",
726 adap_id, forces[kind][i + 1],
728 err = i2c_probe_address(adapter,
738 /* Stop here if we can't use SMBUS_QUICK */
739 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
740 if (address_data->probe[0] == I2C_CLIENT_END
741 && address_data->normal_i2c[0] == I2C_CLIENT_END)
744 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
745 "can't probe for chips\n");
749 /* Probe entries are done second, and are not affected by ignore
751 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
752 if (address_data->probe[i] == adap_id
753 || address_data->probe[i] == ANY_I2C_BUS) {
754 dev_dbg(&adapter->dev, "found probe parameter for "
755 "adapter %d, addr 0x%02x\n", adap_id,
756 address_data->probe[i + 1]);
757 err = i2c_probe_address(adapter,
758 address_data->probe[i + 1],
765 /* Normal entries are done last, unless shadowed by an ignore entry */
766 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
770 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
772 if ((address_data->ignore[j] == adap_id ||
773 address_data->ignore[j] == ANY_I2C_BUS)
774 && address_data->ignore[j + 1]
775 == address_data->normal_i2c[i]) {
776 dev_dbg(&adapter->dev, "found ignore "
777 "parameter for adapter %d, "
778 "addr 0x%02x\n", adap_id,
779 address_data->ignore[j + 1]);
787 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
788 "addr 0x%02x\n", adap_id,
789 address_data->normal_i2c[i]);
790 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
799 struct i2c_adapter* i2c_get_adapter(int id)
801 struct i2c_adapter *adapter;
804 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
805 if (adapter && !try_module_get(adapter->owner))
812 void i2c_put_adapter(struct i2c_adapter *adap)
814 module_put(adap->owner);
817 /* The SMBus parts */
819 #define POLY (0x1070U << 3)
825 for(i = 0; i < 8; i++) {
830 return (u8)(data >> 8);
833 /* CRC over count bytes in the first array plus the bytes in the rest
834 array if it is non-null. rest[0] is the (length of rest) - 1
836 static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
840 for(i = 0; i < count; i++)
841 crc = crc8((crc ^ first[i]) << 8);
843 for(i = 0; i <= rest[0]; i++)
844 crc = crc8((crc ^ rest[i]) << 8);
848 static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
850 return i2c_smbus_partial_pec(0, count, first, rest);
853 /* Returns new "size" (transaction type)
854 Note that we convert byte to byte_data and byte_data to word_data
855 rather than invent new xxx_PEC transactions. */
856 static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
857 union i2c_smbus_data *data)
865 data->byte = i2c_smbus_pec(2, buf, NULL);
866 size = I2C_SMBUS_BYTE_DATA;
868 case I2C_SMBUS_BYTE_DATA:
870 data->word = buf[2] |
871 (i2c_smbus_pec(3, buf, NULL) << 8);
872 size = I2C_SMBUS_WORD_DATA;
874 case I2C_SMBUS_WORD_DATA:
877 case I2C_SMBUS_BLOCK_DATA:
878 data->block[data->block[0] + 1] =
879 i2c_smbus_pec(2, buf, data->block);
880 size = I2C_SMBUS_BLOCK_DATA_PEC;
886 static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
887 union i2c_smbus_data *data)
889 u8 buf[3], rpec, cpec;
893 case I2C_SMBUS_BYTE_DATA:
894 buf[0] = (addr << 1) | 1;
895 cpec = i2c_smbus_pec(2, buf, NULL);
898 case I2C_SMBUS_WORD_DATA:
899 buf[0] = (addr << 1) | 1;
900 buf[2] = data->word & 0xff;
901 cpec = i2c_smbus_pec(3, buf, NULL);
902 rpec = data->word >> 8;
904 case I2C_SMBUS_WORD_DATA_PEC:
908 case I2C_SMBUS_PROC_CALL_PEC:
912 case I2C_SMBUS_BLOCK_DATA_PEC:
913 buf[0] = (addr << 1);
914 buf[2] = (addr << 1) | 1;
915 cpec = i2c_smbus_pec(3, buf, data->block);
916 rpec = data->block[data->block[0] + 1];
918 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
919 buf[0] = (addr << 1) | 1;
920 rpec = i2c_smbus_partial_pec(partial, 1,
922 cpec = data->block[data->block[0] + 1];
929 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
936 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
938 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
939 value,0,I2C_SMBUS_QUICK,NULL);
942 s32 i2c_smbus_read_byte(struct i2c_client *client)
944 union i2c_smbus_data data;
945 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
946 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
949 return 0x0FF & data.byte;
952 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
954 union i2c_smbus_data data; /* only for PEC */
955 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
956 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
959 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
961 union i2c_smbus_data data;
962 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
963 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
966 return 0x0FF & data.byte;
969 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
971 union i2c_smbus_data data;
973 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
974 I2C_SMBUS_WRITE,command,
975 I2C_SMBUS_BYTE_DATA,&data);
978 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
980 union i2c_smbus_data data;
981 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
982 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
985 return 0x0FFFF & data.word;
988 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
990 union i2c_smbus_data data;
992 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
993 I2C_SMBUS_WRITE,command,
994 I2C_SMBUS_WORD_DATA,&data);
997 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
998 u8 length, u8 *values)
1000 union i2c_smbus_data data;
1002 if (length > I2C_SMBUS_BLOCK_MAX)
1003 length = I2C_SMBUS_BLOCK_MAX;
1004 for (i = 1; i <= length; i++)
1005 data.block[i] = values[i-1];
1006 data.block[0] = length;
1007 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1008 I2C_SMBUS_WRITE,command,
1009 I2C_SMBUS_BLOCK_DATA,&data);
1012 /* Returns the number of read bytes */
1013 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1015 union i2c_smbus_data data;
1017 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1018 I2C_SMBUS_READ,command,
1019 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1022 for (i = 1; i <= data.block[0]; i++)
1023 values[i-1] = data.block[i];
1024 return data.block[0];
1028 /* Simulate a SMBus command using the i2c protocol
1029 No checking of parameters is done! */
1030 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1031 unsigned short flags,
1032 char read_write, u8 command, int size,
1033 union i2c_smbus_data * data)
1035 /* So we need to generate a series of msgs. In the case of writing, we
1036 need to use only one message; when reading, we need two. We initialize
1037 most things with sane defaults, to keep the code below somewhat
1039 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1040 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1041 int num = read_write == I2C_SMBUS_READ?2:1;
1042 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1043 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1047 msgbuf0[0] = command;
1049 case I2C_SMBUS_QUICK:
1051 /* Special case: The read/write field is used as data */
1052 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1055 case I2C_SMBUS_BYTE:
1056 if (read_write == I2C_SMBUS_READ) {
1057 /* Special case: only a read! */
1058 msg[0].flags = I2C_M_RD | flags;
1062 case I2C_SMBUS_BYTE_DATA:
1063 if (read_write == I2C_SMBUS_READ)
1067 msgbuf0[1] = data->byte;
1070 case I2C_SMBUS_WORD_DATA:
1071 if (read_write == I2C_SMBUS_READ)
1075 msgbuf0[1] = data->word & 0xff;
1076 msgbuf0[2] = (data->word >> 8) & 0xff;
1079 case I2C_SMBUS_PROC_CALL:
1080 num = 2; /* Special case */
1081 read_write = I2C_SMBUS_READ;
1084 msgbuf0[1] = data->word & 0xff;
1085 msgbuf0[2] = (data->word >> 8) & 0xff;
1087 case I2C_SMBUS_BLOCK_DATA:
1088 case I2C_SMBUS_BLOCK_DATA_PEC:
1089 if (read_write == I2C_SMBUS_READ) {
1090 dev_err(&adapter->dev, "Block read not supported "
1091 "under I2C emulation!\n");
1094 msg[0].len = data->block[0] + 2;
1095 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1096 dev_err(&adapter->dev, "smbus_access called with "
1097 "invalid block write size (%d)\n",
1101 if(size == I2C_SMBUS_BLOCK_DATA_PEC)
1103 for (i = 1; i < msg[0].len; i++)
1104 msgbuf0[i] = data->block[i-1];
1107 case I2C_SMBUS_BLOCK_PROC_CALL:
1108 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
1109 dev_dbg(&adapter->dev, "Block process call not supported "
1110 "under I2C emulation!\n");
1112 case I2C_SMBUS_I2C_BLOCK_DATA:
1113 if (read_write == I2C_SMBUS_READ) {
1114 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1116 msg[0].len = data->block[0] + 1;
1117 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1118 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1119 "invalid block write size (%d)\n",
1123 for (i = 1; i <= data->block[0]; i++)
1124 msgbuf0[i] = data->block[i];
1128 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1133 if (i2c_transfer(adapter, msg, num) < 0)
1136 if (read_write == I2C_SMBUS_READ)
1138 case I2C_SMBUS_BYTE:
1139 data->byte = msgbuf0[0];
1141 case I2C_SMBUS_BYTE_DATA:
1142 data->byte = msgbuf1[0];
1144 case I2C_SMBUS_WORD_DATA:
1145 case I2C_SMBUS_PROC_CALL:
1146 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1148 case I2C_SMBUS_I2C_BLOCK_DATA:
1149 /* fixed at 32 for now */
1150 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1151 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1152 data->block[i+1] = msgbuf1[i];
1159 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1160 char read_write, u8 command, int size,
1161 union i2c_smbus_data * data)
1167 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1168 if((flags & I2C_CLIENT_PEC) &&
1169 !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
1171 if(read_write == I2C_SMBUS_READ &&
1172 size == I2C_SMBUS_BLOCK_DATA)
1173 size = I2C_SMBUS_BLOCK_DATA_PEC;
1174 else if(size == I2C_SMBUS_PROC_CALL)
1175 size = I2C_SMBUS_PROC_CALL_PEC;
1176 else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
1177 i2c_smbus_add_pec(addr, command,
1178 I2C_SMBUS_BLOCK_DATA, data);
1179 partial = data->block[data->block[0] + 1];
1180 size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
1181 } else if(read_write == I2C_SMBUS_WRITE &&
1182 size != I2C_SMBUS_QUICK &&
1183 size != I2C_SMBUS_I2C_BLOCK_DATA)
1184 size = i2c_smbus_add_pec(addr, command, size, data);
1187 if (adapter->algo->smbus_xfer) {
1188 down(&adapter->bus_lock);
1189 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1191 up(&adapter->bus_lock);
1193 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1196 if(res >= 0 && swpec &&
1197 size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
1198 (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
1199 size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
1200 if(i2c_smbus_check_pec(addr, command, size, partial, data))
1207 /* Next four are needed by i2c-isa */
1208 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1209 EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1210 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1211 EXPORT_SYMBOL_GPL(i2c_bus_type);
1213 EXPORT_SYMBOL(i2c_add_adapter);
1214 EXPORT_SYMBOL(i2c_del_adapter);
1215 EXPORT_SYMBOL(i2c_add_driver);
1216 EXPORT_SYMBOL(i2c_del_driver);
1217 EXPORT_SYMBOL(i2c_attach_client);
1218 EXPORT_SYMBOL(i2c_detach_client);
1219 EXPORT_SYMBOL(i2c_use_client);
1220 EXPORT_SYMBOL(i2c_release_client);
1221 EXPORT_SYMBOL(i2c_clients_command);
1222 EXPORT_SYMBOL(i2c_check_addr);
1224 EXPORT_SYMBOL(i2c_master_send);
1225 EXPORT_SYMBOL(i2c_master_recv);
1226 EXPORT_SYMBOL(i2c_control);
1227 EXPORT_SYMBOL(i2c_transfer);
1228 EXPORT_SYMBOL(i2c_get_adapter);
1229 EXPORT_SYMBOL(i2c_put_adapter);
1230 EXPORT_SYMBOL(i2c_probe);
1232 EXPORT_SYMBOL(i2c_smbus_xfer);
1233 EXPORT_SYMBOL(i2c_smbus_write_quick);
1234 EXPORT_SYMBOL(i2c_smbus_read_byte);
1235 EXPORT_SYMBOL(i2c_smbus_write_byte);
1236 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1237 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1238 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1239 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1240 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1241 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1243 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1244 MODULE_DESCRIPTION("I2C-Bus main module");
1245 MODULE_LICENSE("GPL");