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 <linux/completion.h>
36 #include <asm/uaccess.h>
39 static LIST_HEAD(adapters);
40 static LIST_HEAD(drivers);
41 static DEFINE_MUTEX(core_lists);
42 static DEFINE_IDR(i2c_adapter_idr);
44 /* match always succeeds, as we want the probe() to tell if we really accept this match */
45 static int i2c_device_match(struct device *dev, struct device_driver *drv)
50 static int i2c_bus_suspend(struct device * dev, pm_message_t state)
54 if (dev->driver && dev->driver->suspend)
55 rc = dev->driver->suspend(dev, state);
59 static int i2c_bus_resume(struct device * dev)
63 if (dev->driver && dev->driver->resume)
64 rc = dev->driver->resume(dev);
68 static int i2c_device_probe(struct device *dev)
73 static int i2c_device_remove(struct device *dev)
78 struct bus_type i2c_bus_type = {
80 .match = i2c_device_match,
81 .probe = i2c_device_probe,
82 .remove = i2c_device_remove,
83 .suspend = i2c_bus_suspend,
84 .resume = i2c_bus_resume,
87 void i2c_adapter_dev_release(struct device *dev)
89 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
90 complete(&adap->dev_released);
93 struct device_driver i2c_adapter_driver = {
95 .name = "i2c_adapter",
99 /* ------------------------------------------------------------------------- */
101 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
103 static void i2c_adapter_class_dev_release(struct class_device *dev)
105 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
106 complete(&adap->class_dev_released);
109 static ssize_t i2c_adapter_show_name(struct class_device *cdev, char *buf)
111 struct i2c_adapter *adap = class_dev_to_i2c_adapter(cdev);
112 return sprintf(buf, "%s\n", adap->name);
115 static struct class_device_attribute i2c_adapter_attrs[] = {
116 __ATTR(name, S_IRUGO, i2c_adapter_show_name, NULL),
120 struct class i2c_adapter_class = {
121 .owner = THIS_MODULE,
122 .name = "i2c-adapter",
123 .class_dev_attrs = i2c_adapter_attrs,
124 .release = &i2c_adapter_class_dev_release,
127 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
129 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
130 return sprintf(buf, "%s\n", adap->name);
132 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
135 static void i2c_client_release(struct device *dev)
137 struct i2c_client *client = to_i2c_client(dev);
138 complete(&client->released);
141 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
143 struct i2c_client *client = to_i2c_client(dev);
144 return sprintf(buf, "%s\n", client->name);
148 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
149 * an i2c adapter attribute (above).
151 static struct device_attribute dev_attr_client_name =
152 __ATTR(name, S_IRUGO, &show_client_name, NULL);
155 /* ---------------------------------------------------
156 * registering functions
157 * ---------------------------------------------------
161 * i2c_add_adapter is called from within the algorithm layer,
162 * when a new hw adapter registers. A new device is register to be
163 * available for clients.
165 int i2c_add_adapter(struct i2c_adapter *adap)
168 struct list_head *item;
169 struct i2c_driver *driver;
171 mutex_lock(&core_lists);
173 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
178 res = idr_get_new(&i2c_adapter_idr, adap, &id);
185 adap->nr = id & MAX_ID_MASK;
186 mutex_init(&adap->bus_lock);
187 mutex_init(&adap->clist_lock);
188 list_add_tail(&adap->list,&adapters);
189 INIT_LIST_HEAD(&adap->clients);
191 /* Add the adapter to the driver core.
192 * If the parent pointer is not set up,
193 * we add this adapter to the host bus.
195 if (adap->dev.parent == NULL) {
196 adap->dev.parent = &platform_bus;
197 printk(KERN_WARNING "**WARNING** I2C adapter driver [%s] "
198 "forgot to specify physical device; fix it!\n",
201 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
202 adap->dev.driver = &i2c_adapter_driver;
203 adap->dev.release = &i2c_adapter_dev_release;
204 res = device_register(&adap->dev);
207 res = device_create_file(&adap->dev, &dev_attr_name);
211 /* Add this adapter to the i2c_adapter class */
212 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
213 adap->class_dev.dev = &adap->dev;
214 adap->class_dev.class = &i2c_adapter_class;
215 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
216 res = class_device_register(&adap->class_dev);
218 goto out_remove_name;
220 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
222 /* inform drivers of new adapters */
223 list_for_each(item,&drivers) {
224 driver = list_entry(item, struct i2c_driver, list);
225 if (driver->attach_adapter)
226 /* We ignore the return code; if it fails, too bad */
227 driver->attach_adapter(adap);
231 mutex_unlock(&core_lists);
235 device_remove_file(&adap->dev, &dev_attr_name);
237 init_completion(&adap->dev_released); /* Needed? */
238 device_unregister(&adap->dev);
239 wait_for_completion(&adap->dev_released);
241 list_del(&adap->list);
242 idr_remove(&i2c_adapter_idr, adap->nr);
247 int i2c_del_adapter(struct i2c_adapter *adap)
249 struct list_head *item, *_n;
250 struct i2c_adapter *adap_from_list;
251 struct i2c_driver *driver;
252 struct i2c_client *client;
255 mutex_lock(&core_lists);
257 /* First make sure that this adapter was ever added */
258 list_for_each_entry(adap_from_list, &adapters, list) {
259 if (adap_from_list == adap)
262 if (adap_from_list != adap) {
263 pr_debug("i2c-core: attempting to delete unregistered "
264 "adapter [%s]\n", adap->name);
269 list_for_each(item,&drivers) {
270 driver = list_entry(item, struct i2c_driver, list);
271 if (driver->detach_adapter)
272 if ((res = driver->detach_adapter(adap))) {
273 dev_err(&adap->dev, "detach_adapter failed "
275 driver->driver.name);
280 /* detach any active clients. This must be done first, because
281 * it can fail; in which case we give up. */
282 list_for_each_safe(item, _n, &adap->clients) {
283 client = list_entry(item, struct i2c_client, list);
285 if ((res=client->driver->detach_client(client))) {
286 dev_err(&adap->dev, "detach_client failed for client "
287 "[%s] at address 0x%02x\n", client->name,
293 /* clean up the sysfs representation */
294 init_completion(&adap->dev_released);
295 init_completion(&adap->class_dev_released);
296 class_device_unregister(&adap->class_dev);
297 device_remove_file(&adap->dev, &dev_attr_name);
298 device_unregister(&adap->dev);
299 list_del(&adap->list);
301 /* wait for sysfs to drop all references */
302 wait_for_completion(&adap->dev_released);
303 wait_for_completion(&adap->class_dev_released);
305 /* free dynamically allocated bus id */
306 idr_remove(&i2c_adapter_idr, adap->nr);
308 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
311 mutex_unlock(&core_lists);
317 * What follows is the "upwards" interface: commands for talking to clients,
318 * which implement the functions to access the physical information of the
322 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
324 struct list_head *item;
325 struct i2c_adapter *adapter;
328 /* add the driver to the list of i2c drivers in the driver core */
329 driver->driver.owner = owner;
330 driver->driver.bus = &i2c_bus_type;
332 res = driver_register(&driver->driver);
336 mutex_lock(&core_lists);
338 list_add_tail(&driver->list,&drivers);
339 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
341 /* now look for instances of driver on our adapters */
342 if (driver->attach_adapter) {
343 list_for_each(item,&adapters) {
344 adapter = list_entry(item, struct i2c_adapter, list);
345 driver->attach_adapter(adapter);
349 mutex_unlock(&core_lists);
352 EXPORT_SYMBOL(i2c_register_driver);
354 int i2c_del_driver(struct i2c_driver *driver)
356 struct list_head *item1, *item2, *_n;
357 struct i2c_client *client;
358 struct i2c_adapter *adap;
362 mutex_lock(&core_lists);
364 /* Have a look at each adapter, if clients of this driver are still
365 * attached. If so, detach them to be able to kill the driver
368 list_for_each(item1,&adapters) {
369 adap = list_entry(item1, struct i2c_adapter, list);
370 if (driver->detach_adapter) {
371 if ((res = driver->detach_adapter(adap))) {
372 dev_err(&adap->dev, "detach_adapter failed "
374 driver->driver.name);
378 list_for_each_safe(item2, _n, &adap->clients) {
379 client = list_entry(item2, struct i2c_client, list);
380 if (client->driver != driver)
382 dev_dbg(&adap->dev, "detaching client [%s] "
383 "at 0x%02x\n", client->name,
385 if ((res = driver->detach_client(client))) {
386 dev_err(&adap->dev, "detach_client "
387 "failed for client [%s] at "
388 "0x%02x\n", client->name,
396 driver_unregister(&driver->driver);
397 list_del(&driver->list);
398 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
401 mutex_unlock(&core_lists);
405 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
407 struct list_head *item;
408 struct i2c_client *client;
410 list_for_each(item,&adapter->clients) {
411 client = list_entry(item, struct i2c_client, list);
412 if (client->addr == addr)
418 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
422 mutex_lock(&adapter->clist_lock);
423 rval = __i2c_check_addr(adapter, addr);
424 mutex_unlock(&adapter->clist_lock);
429 int i2c_attach_client(struct i2c_client *client)
431 struct i2c_adapter *adapter = client->adapter;
434 mutex_lock(&adapter->clist_lock);
435 if (__i2c_check_addr(client->adapter, client->addr)) {
439 list_add_tail(&client->list,&adapter->clients);
441 client->usage_count = 0;
443 client->dev.parent = &client->adapter->dev;
444 client->dev.driver = &client->driver->driver;
445 client->dev.bus = &i2c_bus_type;
446 client->dev.release = &i2c_client_release;
448 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
449 "%d-%04x", i2c_adapter_id(adapter), client->addr);
450 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
451 client->name, client->dev.bus_id);
452 res = device_register(&client->dev);
455 res = device_create_file(&client->dev, &dev_attr_client_name);
458 mutex_unlock(&adapter->clist_lock);
460 if (adapter->client_register) {
461 if (adapter->client_register(client)) {
462 dev_dbg(&adapter->dev, "client_register "
463 "failed for client [%s] at 0x%02x\n",
464 client->name, client->addr);
471 init_completion(&client->released); /* Needed? */
472 device_unregister(&client->dev);
473 wait_for_completion(&client->released);
475 list_del(&client->list);
476 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
477 "(%d)\n", client->name, client->addr, res);
479 mutex_unlock(&adapter->clist_lock);
484 int i2c_detach_client(struct i2c_client *client)
486 struct i2c_adapter *adapter = client->adapter;
489 if (client->usage_count > 0) {
490 dev_warn(&client->dev, "Client [%s] still busy, "
491 "can't detach\n", client->name);
495 if (adapter->client_unregister) {
496 res = adapter->client_unregister(client);
498 dev_err(&client->dev,
499 "client_unregister [%s] failed, "
500 "client not detached\n", client->name);
505 mutex_lock(&adapter->clist_lock);
506 list_del(&client->list);
507 init_completion(&client->released);
508 device_remove_file(&client->dev, &dev_attr_client_name);
509 device_unregister(&client->dev);
510 mutex_unlock(&adapter->clist_lock);
511 wait_for_completion(&client->released);
517 static int i2c_inc_use_client(struct i2c_client *client)
520 if (!try_module_get(client->driver->driver.owner))
522 if (!try_module_get(client->adapter->owner)) {
523 module_put(client->driver->driver.owner);
530 static void i2c_dec_use_client(struct i2c_client *client)
532 module_put(client->driver->driver.owner);
533 module_put(client->adapter->owner);
536 int i2c_use_client(struct i2c_client *client)
540 ret = i2c_inc_use_client(client);
544 client->usage_count++;
549 int i2c_release_client(struct i2c_client *client)
551 if (!client->usage_count) {
552 pr_debug("i2c-core: %s used one too many times\n",
557 client->usage_count--;
558 i2c_dec_use_client(client);
563 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
565 struct list_head *item;
566 struct i2c_client *client;
568 mutex_lock(&adap->clist_lock);
569 list_for_each(item,&adap->clients) {
570 client = list_entry(item, struct i2c_client, list);
571 if (!try_module_get(client->driver->driver.owner))
573 if (NULL != client->driver->command) {
574 mutex_unlock(&adap->clist_lock);
575 client->driver->command(client,cmd,arg);
576 mutex_lock(&adap->clist_lock);
578 module_put(client->driver->driver.owner);
580 mutex_unlock(&adap->clist_lock);
583 static int __init i2c_init(void)
587 retval = bus_register(&i2c_bus_type);
590 retval = driver_register(&i2c_adapter_driver);
593 return class_register(&i2c_adapter_class);
596 static void __exit i2c_exit(void)
598 class_unregister(&i2c_adapter_class);
599 driver_unregister(&i2c_adapter_driver);
600 bus_unregister(&i2c_bus_type);
603 subsys_initcall(i2c_init);
604 module_exit(i2c_exit);
606 /* ----------------------------------------------------
607 * the functional interface to the i2c busses.
608 * ----------------------------------------------------
611 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
615 if (adap->algo->master_xfer) {
617 for (ret = 0; ret < num; ret++) {
618 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
619 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
620 'R' : 'W', msgs[ret].addr, msgs[ret].len);
624 mutex_lock_nested(&adap->bus_lock, adap->level);
625 ret = adap->algo->master_xfer(adap,msgs,num);
626 mutex_unlock(&adap->bus_lock);
630 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
635 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
638 struct i2c_adapter *adap=client->adapter;
641 msg.addr = client->addr;
642 msg.flags = client->flags & I2C_M_TEN;
644 msg.buf = (char *)buf;
646 ret = i2c_transfer(adap, &msg, 1);
648 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
649 transmitted, else error code. */
650 return (ret == 1) ? count : ret;
653 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
655 struct i2c_adapter *adap=client->adapter;
659 msg.addr = client->addr;
660 msg.flags = client->flags & I2C_M_TEN;
661 msg.flags |= I2C_M_RD;
665 ret = i2c_transfer(adap, &msg, 1);
667 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
668 transmitted, else error code. */
669 return (ret == 1) ? count : ret;
673 int i2c_control(struct i2c_client *client,
674 unsigned int cmd, unsigned long arg)
677 struct i2c_adapter *adap = client->adapter;
679 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
688 if (adap->algo->algo_control!=NULL)
689 ret = adap->algo->algo_control(adap,cmd,arg);
694 /* ----------------------------------------------------
695 * the i2c address scanning function
696 * Will not work for 10-bit addresses!
697 * ----------------------------------------------------
699 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
700 int (*found_proc) (struct i2c_adapter *, int, int))
704 /* Make sure the address is valid */
705 if (addr < 0x03 || addr > 0x77) {
706 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
711 /* Skip if already in use */
712 if (i2c_check_addr(adapter, addr))
715 /* Make sure there is something at this address, unless forced */
717 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
718 I2C_SMBUS_QUICK, NULL) < 0)
721 /* prevent 24RF08 corruption */
722 if ((addr & ~0x0f) == 0x50)
723 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
724 I2C_SMBUS_QUICK, NULL);
727 /* Finally call the custom detection function */
728 err = found_proc(adapter, addr, kind);
729 /* -ENODEV can be returned if there is a chip at the given address
730 but it isn't supported by this chip driver. We catch it here as
731 this isn't an error. */
736 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
741 int i2c_probe(struct i2c_adapter *adapter,
742 struct i2c_client_address_data *address_data,
743 int (*found_proc) (struct i2c_adapter *, int, int))
746 int adap_id = i2c_adapter_id(adapter);
748 /* Force entries are done first, and are not affected by ignore
750 if (address_data->forces) {
751 unsigned short **forces = address_data->forces;
754 for (kind = 0; forces[kind]; kind++) {
755 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
757 if (forces[kind][i] == adap_id
758 || forces[kind][i] == ANY_I2C_BUS) {
759 dev_dbg(&adapter->dev, "found force "
760 "parameter for adapter %d, "
761 "addr 0x%02x, kind %d\n",
762 adap_id, forces[kind][i + 1],
764 err = i2c_probe_address(adapter,
774 /* Stop here if we can't use SMBUS_QUICK */
775 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
776 if (address_data->probe[0] == I2C_CLIENT_END
777 && address_data->normal_i2c[0] == I2C_CLIENT_END)
780 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
781 "can't probe for chips\n");
785 /* Probe entries are done second, and are not affected by ignore
787 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
788 if (address_data->probe[i] == adap_id
789 || address_data->probe[i] == ANY_I2C_BUS) {
790 dev_dbg(&adapter->dev, "found probe parameter for "
791 "adapter %d, addr 0x%02x\n", adap_id,
792 address_data->probe[i + 1]);
793 err = i2c_probe_address(adapter,
794 address_data->probe[i + 1],
801 /* Normal entries are done last, unless shadowed by an ignore entry */
802 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
806 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
808 if ((address_data->ignore[j] == adap_id ||
809 address_data->ignore[j] == ANY_I2C_BUS)
810 && address_data->ignore[j + 1]
811 == address_data->normal_i2c[i]) {
812 dev_dbg(&adapter->dev, "found ignore "
813 "parameter for adapter %d, "
814 "addr 0x%02x\n", adap_id,
815 address_data->ignore[j + 1]);
823 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
824 "addr 0x%02x\n", adap_id,
825 address_data->normal_i2c[i]);
826 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
835 struct i2c_adapter* i2c_get_adapter(int id)
837 struct i2c_adapter *adapter;
839 mutex_lock(&core_lists);
840 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
841 if (adapter && !try_module_get(adapter->owner))
844 mutex_unlock(&core_lists);
848 void i2c_put_adapter(struct i2c_adapter *adap)
850 module_put(adap->owner);
853 /* The SMBus parts */
855 #define POLY (0x1070U << 3)
861 for(i = 0; i < 8; i++) {
866 return (u8)(data >> 8);
869 /* Incremental CRC8 over count bytes in the array pointed to by p */
870 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
874 for(i = 0; i < count; i++)
875 crc = crc8((crc ^ p[i]) << 8);
879 /* Assume a 7-bit address, which is reasonable for SMBus */
880 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
882 /* The address will be sent first */
883 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
884 pec = i2c_smbus_pec(pec, &addr, 1);
886 /* The data buffer follows */
887 return i2c_smbus_pec(pec, msg->buf, msg->len);
890 /* Used for write only transactions */
891 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
893 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
897 /* Return <0 on CRC error
898 If there was a write before this read (most cases) we need to take the
899 partial CRC from the write part into account.
900 Note that this function does modify the message (we need to decrease the
901 message length to hide the CRC byte from the caller). */
902 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
904 u8 rpec = msg->buf[--msg->len];
905 cpec = i2c_smbus_msg_pec(cpec, msg);
908 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
915 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
917 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
918 value,0,I2C_SMBUS_QUICK,NULL);
921 s32 i2c_smbus_read_byte(struct i2c_client *client)
923 union i2c_smbus_data data;
924 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
925 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
931 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
933 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
934 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
937 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
939 union i2c_smbus_data data;
940 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
941 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
947 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
949 union i2c_smbus_data data;
951 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
952 I2C_SMBUS_WRITE,command,
953 I2C_SMBUS_BYTE_DATA,&data);
956 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
958 union i2c_smbus_data data;
959 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
960 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
966 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
968 union i2c_smbus_data data;
970 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
971 I2C_SMBUS_WRITE,command,
972 I2C_SMBUS_WORD_DATA,&data);
975 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
976 u8 length, const u8 *values)
978 union i2c_smbus_data data;
980 if (length > I2C_SMBUS_BLOCK_MAX)
981 length = I2C_SMBUS_BLOCK_MAX;
982 data.block[0] = length;
983 memcpy(&data.block[1], values, length);
984 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
985 I2C_SMBUS_WRITE,command,
986 I2C_SMBUS_BLOCK_DATA,&data);
989 /* Returns the number of read bytes */
990 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
992 union i2c_smbus_data data;
994 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
995 I2C_SMBUS_READ,command,
996 I2C_SMBUS_I2C_BLOCK_DATA,&data))
999 memcpy(values, &data.block[1], data.block[0]);
1000 return data.block[0];
1003 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1004 u8 length, const u8 *values)
1006 union i2c_smbus_data data;
1008 if (length > I2C_SMBUS_BLOCK_MAX)
1009 length = I2C_SMBUS_BLOCK_MAX;
1010 data.block[0] = length;
1011 memcpy(data.block + 1, values, length);
1012 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1013 I2C_SMBUS_WRITE, command,
1014 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1017 /* Simulate a SMBus command using the i2c protocol
1018 No checking of parameters is done! */
1019 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1020 unsigned short flags,
1021 char read_write, u8 command, int size,
1022 union i2c_smbus_data * data)
1024 /* So we need to generate a series of msgs. In the case of writing, we
1025 need to use only one message; when reading, we need two. We initialize
1026 most things with sane defaults, to keep the code below somewhat
1028 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1029 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1030 int num = read_write == I2C_SMBUS_READ?2:1;
1031 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1032 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1037 msgbuf0[0] = command;
1039 case I2C_SMBUS_QUICK:
1041 /* Special case: The read/write field is used as data */
1042 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1045 case I2C_SMBUS_BYTE:
1046 if (read_write == I2C_SMBUS_READ) {
1047 /* Special case: only a read! */
1048 msg[0].flags = I2C_M_RD | flags;
1052 case I2C_SMBUS_BYTE_DATA:
1053 if (read_write == I2C_SMBUS_READ)
1057 msgbuf0[1] = data->byte;
1060 case I2C_SMBUS_WORD_DATA:
1061 if (read_write == I2C_SMBUS_READ)
1065 msgbuf0[1] = data->word & 0xff;
1066 msgbuf0[2] = data->word >> 8;
1069 case I2C_SMBUS_PROC_CALL:
1070 num = 2; /* Special case */
1071 read_write = I2C_SMBUS_READ;
1074 msgbuf0[1] = data->word & 0xff;
1075 msgbuf0[2] = data->word >> 8;
1077 case I2C_SMBUS_BLOCK_DATA:
1078 if (read_write == I2C_SMBUS_READ) {
1079 dev_err(&adapter->dev, "Block read not supported "
1080 "under I2C emulation!\n");
1083 msg[0].len = data->block[0] + 2;
1084 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1085 dev_err(&adapter->dev, "smbus_access called with "
1086 "invalid block write size (%d)\n",
1090 for (i = 1; i < msg[0].len; i++)
1091 msgbuf0[i] = data->block[i-1];
1094 case I2C_SMBUS_BLOCK_PROC_CALL:
1095 dev_dbg(&adapter->dev, "Block process call not supported "
1096 "under I2C emulation!\n");
1098 case I2C_SMBUS_I2C_BLOCK_DATA:
1099 if (read_write == I2C_SMBUS_READ) {
1100 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1102 msg[0].len = data->block[0] + 1;
1103 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1104 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1105 "invalid block write size (%d)\n",
1109 for (i = 1; i <= data->block[0]; i++)
1110 msgbuf0[i] = data->block[i];
1114 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1119 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1120 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1122 /* Compute PEC if first message is a write */
1123 if (!(msg[0].flags & I2C_M_RD)) {
1124 if (num == 1) /* Write only */
1125 i2c_smbus_add_pec(&msg[0]);
1126 else /* Write followed by read */
1127 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1129 /* Ask for PEC if last message is a read */
1130 if (msg[num-1].flags & I2C_M_RD)
1134 if (i2c_transfer(adapter, msg, num) < 0)
1137 /* Check PEC if last message is a read */
1138 if (i && (msg[num-1].flags & I2C_M_RD)) {
1139 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1143 if (read_write == I2C_SMBUS_READ)
1145 case I2C_SMBUS_BYTE:
1146 data->byte = msgbuf0[0];
1148 case I2C_SMBUS_BYTE_DATA:
1149 data->byte = msgbuf1[0];
1151 case I2C_SMBUS_WORD_DATA:
1152 case I2C_SMBUS_PROC_CALL:
1153 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1155 case I2C_SMBUS_I2C_BLOCK_DATA:
1156 /* fixed at 32 for now */
1157 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1158 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1159 data->block[i+1] = msgbuf1[i];
1166 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1167 char read_write, u8 command, int size,
1168 union i2c_smbus_data * data)
1172 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1174 if (adapter->algo->smbus_xfer) {
1175 mutex_lock(&adapter->bus_lock);
1176 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1178 mutex_unlock(&adapter->bus_lock);
1180 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1187 /* Next four are needed by i2c-isa */
1188 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1189 EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1190 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1191 EXPORT_SYMBOL_GPL(i2c_bus_type);
1193 EXPORT_SYMBOL(i2c_add_adapter);
1194 EXPORT_SYMBOL(i2c_del_adapter);
1195 EXPORT_SYMBOL(i2c_del_driver);
1196 EXPORT_SYMBOL(i2c_attach_client);
1197 EXPORT_SYMBOL(i2c_detach_client);
1198 EXPORT_SYMBOL(i2c_use_client);
1199 EXPORT_SYMBOL(i2c_release_client);
1200 EXPORT_SYMBOL(i2c_clients_command);
1201 EXPORT_SYMBOL(i2c_check_addr);
1203 EXPORT_SYMBOL(i2c_master_send);
1204 EXPORT_SYMBOL(i2c_master_recv);
1205 EXPORT_SYMBOL(i2c_control);
1206 EXPORT_SYMBOL(i2c_transfer);
1207 EXPORT_SYMBOL(i2c_get_adapter);
1208 EXPORT_SYMBOL(i2c_put_adapter);
1209 EXPORT_SYMBOL(i2c_probe);
1211 EXPORT_SYMBOL(i2c_smbus_xfer);
1212 EXPORT_SYMBOL(i2c_smbus_write_quick);
1213 EXPORT_SYMBOL(i2c_smbus_read_byte);
1214 EXPORT_SYMBOL(i2c_smbus_write_byte);
1215 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1216 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1217 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1218 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1219 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1220 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1221 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1223 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1224 MODULE_DESCRIPTION("I2C-Bus main module");
1225 MODULE_LICENSE("GPL");