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> and
23 Jean Delvare <khali@linux-fr.org> */
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 <linux/platform_device.h>
34 #include <linux/mutex.h>
35 #include <asm/uaccess.h>
38 static LIST_HEAD(adapters);
39 static LIST_HEAD(drivers);
40 static DEFINE_MUTEX(core_lists);
41 static DEFINE_IDR(i2c_adapter_idr);
43 /* match always succeeds, as we want the probe() to tell if we really accept this match */
44 static int i2c_device_match(struct device *dev, struct device_driver *drv)
49 static int i2c_bus_suspend(struct device * dev, pm_message_t state)
53 if (dev->driver && dev->driver->suspend)
54 rc = dev->driver->suspend(dev, state);
58 static int i2c_bus_resume(struct device * dev)
62 if (dev->driver && dev->driver->resume)
63 rc = dev->driver->resume(dev);
67 static int i2c_device_probe(struct device *dev)
72 static int i2c_device_remove(struct device *dev)
77 struct bus_type i2c_bus_type = {
79 .match = i2c_device_match,
80 .probe = i2c_device_probe,
81 .remove = i2c_device_remove,
82 .suspend = i2c_bus_suspend,
83 .resume = i2c_bus_resume,
86 void i2c_adapter_dev_release(struct device *dev)
88 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
89 complete(&adap->dev_released);
92 struct device_driver i2c_adapter_driver = {
94 .name = "i2c_adapter",
98 /* ------------------------------------------------------------------------- */
100 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
102 static void i2c_adapter_class_dev_release(struct class_device *dev)
104 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
105 complete(&adap->class_dev_released);
108 static ssize_t i2c_adapter_show_name(struct class_device *cdev, char *buf)
110 struct i2c_adapter *adap = class_dev_to_i2c_adapter(cdev);
111 return sprintf(buf, "%s\n", adap->name);
114 static struct class_device_attribute i2c_adapter_attrs[] = {
115 __ATTR(name, S_IRUGO, i2c_adapter_show_name, NULL),
119 struct class i2c_adapter_class = {
120 .owner = THIS_MODULE,
121 .name = "i2c-adapter",
122 .class_dev_attrs = i2c_adapter_attrs,
123 .release = &i2c_adapter_class_dev_release,
126 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
128 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
129 return sprintf(buf, "%s\n", adap->name);
131 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
134 static void i2c_client_release(struct device *dev)
136 struct i2c_client *client = to_i2c_client(dev);
137 complete(&client->released);
140 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
142 struct i2c_client *client = to_i2c_client(dev);
143 return sprintf(buf, "%s\n", client->name);
147 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
148 * an i2c adapter attribute (above).
150 static struct device_attribute dev_attr_client_name =
151 __ATTR(name, S_IRUGO, &show_client_name, NULL);
154 /* ---------------------------------------------------
155 * registering functions
156 * ---------------------------------------------------
160 * i2c_add_adapter is called from within the algorithm layer,
161 * when a new hw adapter registers. A new device is register to be
162 * available for clients.
164 int i2c_add_adapter(struct i2c_adapter *adap)
167 struct list_head *item;
168 struct i2c_driver *driver;
170 mutex_lock(&core_lists);
172 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
177 res = idr_get_new(&i2c_adapter_idr, adap, &id);
184 adap->nr = id & MAX_ID_MASK;
185 mutex_init(&adap->bus_lock);
186 mutex_init(&adap->clist_lock);
187 list_add_tail(&adap->list,&adapters);
188 INIT_LIST_HEAD(&adap->clients);
190 /* Add the adapter to the driver core.
191 * If the parent pointer is not set up,
192 * we add this adapter to the host bus.
194 if (adap->dev.parent == NULL) {
195 adap->dev.parent = &platform_bus;
196 printk(KERN_WARNING "**WARNING** I2C adapter driver [%s] "
197 "forgot to specify physical device; fix it!\n",
200 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
201 adap->dev.driver = &i2c_adapter_driver;
202 adap->dev.release = &i2c_adapter_dev_release;
203 res = device_register(&adap->dev);
206 res = device_create_file(&adap->dev, &dev_attr_name);
210 /* Add this adapter to the i2c_adapter class */
211 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
212 adap->class_dev.dev = &adap->dev;
213 adap->class_dev.class = &i2c_adapter_class;
214 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
215 res = class_device_register(&adap->class_dev);
217 goto out_remove_name;
219 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
221 /* inform drivers of new adapters */
222 list_for_each(item,&drivers) {
223 driver = list_entry(item, struct i2c_driver, list);
224 if (driver->attach_adapter)
225 /* We ignore the return code; if it fails, too bad */
226 driver->attach_adapter(adap);
230 mutex_unlock(&core_lists);
234 device_remove_file(&adap->dev, &dev_attr_name);
236 init_completion(&adap->dev_released); /* Needed? */
237 device_unregister(&adap->dev);
238 wait_for_completion(&adap->dev_released);
240 list_del(&adap->list);
241 idr_remove(&i2c_adapter_idr, adap->nr);
246 int i2c_del_adapter(struct i2c_adapter *adap)
248 struct list_head *item, *_n;
249 struct i2c_adapter *adap_from_list;
250 struct i2c_driver *driver;
251 struct i2c_client *client;
254 mutex_lock(&core_lists);
256 /* First make sure that this adapter was ever added */
257 list_for_each_entry(adap_from_list, &adapters, list) {
258 if (adap_from_list == adap)
261 if (adap_from_list != adap) {
262 pr_debug("i2c-core: attempting to delete unregistered "
263 "adapter [%s]\n", adap->name);
268 list_for_each(item,&drivers) {
269 driver = list_entry(item, struct i2c_driver, list);
270 if (driver->detach_adapter)
271 if ((res = driver->detach_adapter(adap))) {
272 dev_err(&adap->dev, "detach_adapter failed "
274 driver->driver.name);
279 /* detach any active clients. This must be done first, because
280 * it can fail; in which case we give up. */
281 list_for_each_safe(item, _n, &adap->clients) {
282 client = list_entry(item, struct i2c_client, list);
284 if ((res=client->driver->detach_client(client))) {
285 dev_err(&adap->dev, "detach_client failed for client "
286 "[%s] at address 0x%02x\n", client->name,
292 /* clean up the sysfs representation */
293 init_completion(&adap->dev_released);
294 init_completion(&adap->class_dev_released);
295 class_device_unregister(&adap->class_dev);
296 device_remove_file(&adap->dev, &dev_attr_name);
297 device_unregister(&adap->dev);
298 list_del(&adap->list);
300 /* wait for sysfs to drop all references */
301 wait_for_completion(&adap->dev_released);
302 wait_for_completion(&adap->class_dev_released);
304 /* free dynamically allocated bus id */
305 idr_remove(&i2c_adapter_idr, adap->nr);
307 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
310 mutex_unlock(&core_lists);
316 * What follows is the "upwards" interface: commands for talking to clients,
317 * which implement the functions to access the physical information of the
321 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
323 struct list_head *item;
324 struct i2c_adapter *adapter;
327 /* add the driver to the list of i2c drivers in the driver core */
328 driver->driver.owner = owner;
329 driver->driver.bus = &i2c_bus_type;
331 res = driver_register(&driver->driver);
335 mutex_lock(&core_lists);
337 list_add_tail(&driver->list,&drivers);
338 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
340 /* now look for instances of driver on our adapters */
341 if (driver->attach_adapter) {
342 list_for_each(item,&adapters) {
343 adapter = list_entry(item, struct i2c_adapter, list);
344 driver->attach_adapter(adapter);
348 mutex_unlock(&core_lists);
351 EXPORT_SYMBOL(i2c_register_driver);
353 int i2c_del_driver(struct i2c_driver *driver)
355 struct list_head *item1, *item2, *_n;
356 struct i2c_client *client;
357 struct i2c_adapter *adap;
361 mutex_lock(&core_lists);
363 /* Have a look at each adapter, if clients of this driver are still
364 * attached. If so, detach them to be able to kill the driver
367 list_for_each(item1,&adapters) {
368 adap = list_entry(item1, struct i2c_adapter, list);
369 if (driver->detach_adapter) {
370 if ((res = driver->detach_adapter(adap))) {
371 dev_err(&adap->dev, "detach_adapter failed "
373 driver->driver.name);
377 list_for_each_safe(item2, _n, &adap->clients) {
378 client = list_entry(item2, struct i2c_client, list);
379 if (client->driver != driver)
381 dev_dbg(&adap->dev, "detaching client [%s] "
382 "at 0x%02x\n", client->name,
384 if ((res = driver->detach_client(client))) {
385 dev_err(&adap->dev, "detach_client "
386 "failed for client [%s] at "
387 "0x%02x\n", client->name,
395 driver_unregister(&driver->driver);
396 list_del(&driver->list);
397 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
400 mutex_unlock(&core_lists);
404 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
406 struct list_head *item;
407 struct i2c_client *client;
409 list_for_each(item,&adapter->clients) {
410 client = list_entry(item, struct i2c_client, list);
411 if (client->addr == addr)
417 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
421 mutex_lock(&adapter->clist_lock);
422 rval = __i2c_check_addr(adapter, addr);
423 mutex_unlock(&adapter->clist_lock);
428 int i2c_attach_client(struct i2c_client *client)
430 struct i2c_adapter *adapter = client->adapter;
433 mutex_lock(&adapter->clist_lock);
434 if (__i2c_check_addr(client->adapter, client->addr)) {
438 list_add_tail(&client->list,&adapter->clients);
440 client->usage_count = 0;
442 client->dev.parent = &client->adapter->dev;
443 client->dev.driver = &client->driver->driver;
444 client->dev.bus = &i2c_bus_type;
445 client->dev.release = &i2c_client_release;
447 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
448 "%d-%04x", i2c_adapter_id(adapter), client->addr);
449 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
450 client->name, client->dev.bus_id);
451 res = device_register(&client->dev);
454 res = device_create_file(&client->dev, &dev_attr_client_name);
457 mutex_unlock(&adapter->clist_lock);
459 if (adapter->client_register) {
460 if (adapter->client_register(client)) {
461 dev_dbg(&adapter->dev, "client_register "
462 "failed for client [%s] at 0x%02x\n",
463 client->name, client->addr);
470 init_completion(&client->released); /* Needed? */
471 device_unregister(&client->dev);
472 wait_for_completion(&client->released);
474 list_del(&client->list);
475 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
476 "(%d)\n", client->name, client->addr, res);
478 mutex_unlock(&adapter->clist_lock);
483 int i2c_detach_client(struct i2c_client *client)
485 struct i2c_adapter *adapter = client->adapter;
488 if (client->usage_count > 0) {
489 dev_warn(&client->dev, "Client [%s] still busy, "
490 "can't detach\n", client->name);
494 if (adapter->client_unregister) {
495 res = adapter->client_unregister(client);
497 dev_err(&client->dev,
498 "client_unregister [%s] failed, "
499 "client not detached\n", client->name);
504 mutex_lock(&adapter->clist_lock);
505 list_del(&client->list);
506 init_completion(&client->released);
507 device_remove_file(&client->dev, &dev_attr_client_name);
508 device_unregister(&client->dev);
509 mutex_unlock(&adapter->clist_lock);
510 wait_for_completion(&client->released);
516 static int i2c_inc_use_client(struct i2c_client *client)
519 if (!try_module_get(client->driver->driver.owner))
521 if (!try_module_get(client->adapter->owner)) {
522 module_put(client->driver->driver.owner);
529 static void i2c_dec_use_client(struct i2c_client *client)
531 module_put(client->driver->driver.owner);
532 module_put(client->adapter->owner);
535 int i2c_use_client(struct i2c_client *client)
539 ret = i2c_inc_use_client(client);
543 client->usage_count++;
548 int i2c_release_client(struct i2c_client *client)
550 if (!client->usage_count) {
551 pr_debug("i2c-core: %s used one too many times\n",
556 client->usage_count--;
557 i2c_dec_use_client(client);
562 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
564 struct list_head *item;
565 struct i2c_client *client;
567 mutex_lock(&adap->clist_lock);
568 list_for_each(item,&adap->clients) {
569 client = list_entry(item, struct i2c_client, list);
570 if (!try_module_get(client->driver->driver.owner))
572 if (NULL != client->driver->command) {
573 mutex_unlock(&adap->clist_lock);
574 client->driver->command(client,cmd,arg);
575 mutex_lock(&adap->clist_lock);
577 module_put(client->driver->driver.owner);
579 mutex_unlock(&adap->clist_lock);
582 static int __init i2c_init(void)
586 retval = bus_register(&i2c_bus_type);
589 retval = driver_register(&i2c_adapter_driver);
592 return class_register(&i2c_adapter_class);
595 static void __exit i2c_exit(void)
597 class_unregister(&i2c_adapter_class);
598 driver_unregister(&i2c_adapter_driver);
599 bus_unregister(&i2c_bus_type);
602 subsys_initcall(i2c_init);
603 module_exit(i2c_exit);
605 /* ----------------------------------------------------
606 * the functional interface to the i2c busses.
607 * ----------------------------------------------------
610 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
614 if (adap->algo->master_xfer) {
616 for (ret = 0; ret < num; ret++) {
617 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
618 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
619 'R' : 'W', msgs[ret].addr, msgs[ret].len);
623 mutex_lock_nested(&adap->bus_lock, adap->level);
624 ret = adap->algo->master_xfer(adap,msgs,num);
625 mutex_unlock(&adap->bus_lock);
629 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
634 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
637 struct i2c_adapter *adap=client->adapter;
640 msg.addr = client->addr;
641 msg.flags = client->flags & I2C_M_TEN;
643 msg.buf = (char *)buf;
645 ret = i2c_transfer(adap, &msg, 1);
647 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
648 transmitted, else error code. */
649 return (ret == 1) ? count : ret;
652 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
654 struct i2c_adapter *adap=client->adapter;
658 msg.addr = client->addr;
659 msg.flags = client->flags & I2C_M_TEN;
660 msg.flags |= I2C_M_RD;
664 ret = i2c_transfer(adap, &msg, 1);
666 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
667 transmitted, else error code. */
668 return (ret == 1) ? count : ret;
672 int i2c_control(struct i2c_client *client,
673 unsigned int cmd, unsigned long arg)
676 struct i2c_adapter *adap = client->adapter;
678 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
687 if (adap->algo->algo_control!=NULL)
688 ret = adap->algo->algo_control(adap,cmd,arg);
693 /* ----------------------------------------------------
694 * the i2c address scanning function
695 * Will not work for 10-bit addresses!
696 * ----------------------------------------------------
698 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
699 int (*found_proc) (struct i2c_adapter *, int, int))
703 /* Make sure the address is valid */
704 if (addr < 0x03 || addr > 0x77) {
705 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
710 /* Skip if already in use */
711 if (i2c_check_addr(adapter, addr))
714 /* Make sure there is something at this address, unless forced */
716 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
717 I2C_SMBUS_QUICK, NULL) < 0)
720 /* prevent 24RF08 corruption */
721 if ((addr & ~0x0f) == 0x50)
722 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
723 I2C_SMBUS_QUICK, NULL);
726 /* Finally call the custom detection function */
727 err = found_proc(adapter, addr, kind);
728 /* -ENODEV can be returned if there is a chip at the given address
729 but it isn't supported by this chip driver. We catch it here as
730 this isn't an error. */
735 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
740 int i2c_probe(struct i2c_adapter *adapter,
741 struct i2c_client_address_data *address_data,
742 int (*found_proc) (struct i2c_adapter *, int, int))
745 int adap_id = i2c_adapter_id(adapter);
747 /* Force entries are done first, and are not affected by ignore
749 if (address_data->forces) {
750 unsigned short **forces = address_data->forces;
753 for (kind = 0; forces[kind]; kind++) {
754 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
756 if (forces[kind][i] == adap_id
757 || forces[kind][i] == ANY_I2C_BUS) {
758 dev_dbg(&adapter->dev, "found force "
759 "parameter for adapter %d, "
760 "addr 0x%02x, kind %d\n",
761 adap_id, forces[kind][i + 1],
763 err = i2c_probe_address(adapter,
773 /* Stop here if we can't use SMBUS_QUICK */
774 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
775 if (address_data->probe[0] == I2C_CLIENT_END
776 && address_data->normal_i2c[0] == I2C_CLIENT_END)
779 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
780 "can't probe for chips\n");
784 /* Probe entries are done second, and are not affected by ignore
786 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
787 if (address_data->probe[i] == adap_id
788 || address_data->probe[i] == ANY_I2C_BUS) {
789 dev_dbg(&adapter->dev, "found probe parameter for "
790 "adapter %d, addr 0x%02x\n", adap_id,
791 address_data->probe[i + 1]);
792 err = i2c_probe_address(adapter,
793 address_data->probe[i + 1],
800 /* Normal entries are done last, unless shadowed by an ignore entry */
801 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
805 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
807 if ((address_data->ignore[j] == adap_id ||
808 address_data->ignore[j] == ANY_I2C_BUS)
809 && address_data->ignore[j + 1]
810 == address_data->normal_i2c[i]) {
811 dev_dbg(&adapter->dev, "found ignore "
812 "parameter for adapter %d, "
813 "addr 0x%02x\n", adap_id,
814 address_data->ignore[j + 1]);
822 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
823 "addr 0x%02x\n", adap_id,
824 address_data->normal_i2c[i]);
825 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
834 struct i2c_adapter* i2c_get_adapter(int id)
836 struct i2c_adapter *adapter;
838 mutex_lock(&core_lists);
839 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
840 if (adapter && !try_module_get(adapter->owner))
843 mutex_unlock(&core_lists);
847 void i2c_put_adapter(struct i2c_adapter *adap)
849 module_put(adap->owner);
852 /* The SMBus parts */
854 #define POLY (0x1070U << 3)
860 for(i = 0; i < 8; i++) {
865 return (u8)(data >> 8);
868 /* Incremental CRC8 over count bytes in the array pointed to by p */
869 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
873 for(i = 0; i < count; i++)
874 crc = crc8((crc ^ p[i]) << 8);
878 /* Assume a 7-bit address, which is reasonable for SMBus */
879 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
881 /* The address will be sent first */
882 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
883 pec = i2c_smbus_pec(pec, &addr, 1);
885 /* The data buffer follows */
886 return i2c_smbus_pec(pec, msg->buf, msg->len);
889 /* Used for write only transactions */
890 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
892 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
896 /* Return <0 on CRC error
897 If there was a write before this read (most cases) we need to take the
898 partial CRC from the write part into account.
899 Note that this function does modify the message (we need to decrease the
900 message length to hide the CRC byte from the caller). */
901 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
903 u8 rpec = msg->buf[--msg->len];
904 cpec = i2c_smbus_msg_pec(cpec, msg);
907 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
914 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
916 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
917 value,0,I2C_SMBUS_QUICK,NULL);
920 s32 i2c_smbus_read_byte(struct i2c_client *client)
922 union i2c_smbus_data data;
923 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
924 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
930 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
932 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
933 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
936 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
938 union i2c_smbus_data data;
939 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
940 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
946 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
948 union i2c_smbus_data data;
950 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
951 I2C_SMBUS_WRITE,command,
952 I2C_SMBUS_BYTE_DATA,&data);
955 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
957 union i2c_smbus_data data;
958 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
959 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
965 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
967 union i2c_smbus_data data;
969 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
970 I2C_SMBUS_WRITE,command,
971 I2C_SMBUS_WORD_DATA,&data);
974 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
975 u8 length, const u8 *values)
977 union i2c_smbus_data data;
979 if (length > I2C_SMBUS_BLOCK_MAX)
980 length = I2C_SMBUS_BLOCK_MAX;
981 data.block[0] = length;
982 memcpy(&data.block[1], values, length);
983 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
984 I2C_SMBUS_WRITE,command,
985 I2C_SMBUS_BLOCK_DATA,&data);
988 /* Returns the number of read bytes */
989 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
991 union i2c_smbus_data data;
993 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
994 I2C_SMBUS_READ,command,
995 I2C_SMBUS_I2C_BLOCK_DATA,&data))
998 memcpy(values, &data.block[1], data.block[0]);
999 return data.block[0];
1002 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1003 u8 length, const u8 *values)
1005 union i2c_smbus_data data;
1007 if (length > I2C_SMBUS_BLOCK_MAX)
1008 length = I2C_SMBUS_BLOCK_MAX;
1009 data.block[0] = length;
1010 memcpy(data.block + 1, values, length);
1011 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1012 I2C_SMBUS_WRITE, command,
1013 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1016 /* Simulate a SMBus command using the i2c protocol
1017 No checking of parameters is done! */
1018 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1019 unsigned short flags,
1020 char read_write, u8 command, int size,
1021 union i2c_smbus_data * data)
1023 /* So we need to generate a series of msgs. In the case of writing, we
1024 need to use only one message; when reading, we need two. We initialize
1025 most things with sane defaults, to keep the code below somewhat
1027 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1028 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1029 int num = read_write == I2C_SMBUS_READ?2:1;
1030 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1031 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1036 msgbuf0[0] = command;
1038 case I2C_SMBUS_QUICK:
1040 /* Special case: The read/write field is used as data */
1041 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1044 case I2C_SMBUS_BYTE:
1045 if (read_write == I2C_SMBUS_READ) {
1046 /* Special case: only a read! */
1047 msg[0].flags = I2C_M_RD | flags;
1051 case I2C_SMBUS_BYTE_DATA:
1052 if (read_write == I2C_SMBUS_READ)
1056 msgbuf0[1] = data->byte;
1059 case I2C_SMBUS_WORD_DATA:
1060 if (read_write == I2C_SMBUS_READ)
1064 msgbuf0[1] = data->word & 0xff;
1065 msgbuf0[2] = data->word >> 8;
1068 case I2C_SMBUS_PROC_CALL:
1069 num = 2; /* Special case */
1070 read_write = I2C_SMBUS_READ;
1073 msgbuf0[1] = data->word & 0xff;
1074 msgbuf0[2] = data->word >> 8;
1076 case I2C_SMBUS_BLOCK_DATA:
1077 if (read_write == I2C_SMBUS_READ) {
1078 dev_err(&adapter->dev, "Block read not supported "
1079 "under I2C emulation!\n");
1082 msg[0].len = data->block[0] + 2;
1083 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1084 dev_err(&adapter->dev, "smbus_access called with "
1085 "invalid block write size (%d)\n",
1089 for (i = 1; i < msg[0].len; i++)
1090 msgbuf0[i] = data->block[i-1];
1093 case I2C_SMBUS_BLOCK_PROC_CALL:
1094 dev_dbg(&adapter->dev, "Block process call not supported "
1095 "under I2C emulation!\n");
1097 case I2C_SMBUS_I2C_BLOCK_DATA:
1098 if (read_write == I2C_SMBUS_READ) {
1099 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1101 msg[0].len = data->block[0] + 1;
1102 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1103 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1104 "invalid block write size (%d)\n",
1108 for (i = 1; i <= data->block[0]; i++)
1109 msgbuf0[i] = data->block[i];
1113 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1118 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1119 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1121 /* Compute PEC if first message is a write */
1122 if (!(msg[0].flags & I2C_M_RD)) {
1123 if (num == 1) /* Write only */
1124 i2c_smbus_add_pec(&msg[0]);
1125 else /* Write followed by read */
1126 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1128 /* Ask for PEC if last message is a read */
1129 if (msg[num-1].flags & I2C_M_RD)
1133 if (i2c_transfer(adapter, msg, num) < 0)
1136 /* Check PEC if last message is a read */
1137 if (i && (msg[num-1].flags & I2C_M_RD)) {
1138 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1142 if (read_write == I2C_SMBUS_READ)
1144 case I2C_SMBUS_BYTE:
1145 data->byte = msgbuf0[0];
1147 case I2C_SMBUS_BYTE_DATA:
1148 data->byte = msgbuf1[0];
1150 case I2C_SMBUS_WORD_DATA:
1151 case I2C_SMBUS_PROC_CALL:
1152 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1154 case I2C_SMBUS_I2C_BLOCK_DATA:
1155 /* fixed at 32 for now */
1156 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1157 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1158 data->block[i+1] = msgbuf1[i];
1165 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1166 char read_write, u8 command, int size,
1167 union i2c_smbus_data * data)
1171 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1173 if (adapter->algo->smbus_xfer) {
1174 mutex_lock(&adapter->bus_lock);
1175 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1177 mutex_unlock(&adapter->bus_lock);
1179 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1186 /* Next four are needed by i2c-isa */
1187 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1188 EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1189 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1190 EXPORT_SYMBOL_GPL(i2c_bus_type);
1192 EXPORT_SYMBOL(i2c_add_adapter);
1193 EXPORT_SYMBOL(i2c_del_adapter);
1194 EXPORT_SYMBOL(i2c_del_driver);
1195 EXPORT_SYMBOL(i2c_attach_client);
1196 EXPORT_SYMBOL(i2c_detach_client);
1197 EXPORT_SYMBOL(i2c_use_client);
1198 EXPORT_SYMBOL(i2c_release_client);
1199 EXPORT_SYMBOL(i2c_clients_command);
1200 EXPORT_SYMBOL(i2c_check_addr);
1202 EXPORT_SYMBOL(i2c_master_send);
1203 EXPORT_SYMBOL(i2c_master_recv);
1204 EXPORT_SYMBOL(i2c_control);
1205 EXPORT_SYMBOL(i2c_transfer);
1206 EXPORT_SYMBOL(i2c_get_adapter);
1207 EXPORT_SYMBOL(i2c_put_adapter);
1208 EXPORT_SYMBOL(i2c_probe);
1210 EXPORT_SYMBOL(i2c_smbus_xfer);
1211 EXPORT_SYMBOL(i2c_smbus_write_quick);
1212 EXPORT_SYMBOL(i2c_smbus_read_byte);
1213 EXPORT_SYMBOL(i2c_smbus_write_byte);
1214 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1215 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1216 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1217 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1218 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1219 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1220 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1222 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1223 MODULE_DESCRIPTION("I2C-Bus main module");
1224 MODULE_LICENSE("GPL");