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 static void i2c_adapter_class_dev_release(struct class_device *dev)
100 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
101 complete(&adap->class_dev_released);
104 struct class i2c_adapter_class = {
105 .owner = THIS_MODULE,
106 .name = "i2c-adapter",
107 .release = &i2c_adapter_class_dev_release,
110 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
112 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
113 return sprintf(buf, "%s\n", adap->name);
115 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
118 static void i2c_client_release(struct device *dev)
120 struct i2c_client *client = to_i2c_client(dev);
121 complete(&client->released);
124 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
126 struct i2c_client *client = to_i2c_client(dev);
127 return sprintf(buf, "%s\n", client->name);
131 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
132 * different type of a device. So beware if the DEVICE_ATTR() macro ever
133 * changes, this definition will also have to change.
135 static struct device_attribute dev_attr_client_name = {
136 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
137 .show = &show_client_name,
141 /* ---------------------------------------------------
142 * registering functions
143 * ---------------------------------------------------
147 * i2c_add_adapter is called from within the algorithm layer,
148 * when a new hw adapter registers. A new device is register to be
149 * available for clients.
151 int i2c_add_adapter(struct i2c_adapter *adap)
154 struct list_head *item;
155 struct i2c_driver *driver;
157 mutex_lock(&core_lists);
159 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
164 res = idr_get_new(&i2c_adapter_idr, adap, &id);
171 adap->nr = id & MAX_ID_MASK;
172 mutex_init(&adap->bus_lock);
173 mutex_init(&adap->clist_lock);
174 list_add_tail(&adap->list,&adapters);
175 INIT_LIST_HEAD(&adap->clients);
177 /* Add the adapter to the driver core.
178 * If the parent pointer is not set up,
179 * we add this adapter to the host bus.
181 if (adap->dev.parent == NULL)
182 adap->dev.parent = &platform_bus;
183 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
184 adap->dev.driver = &i2c_adapter_driver;
185 adap->dev.release = &i2c_adapter_dev_release;
186 res = device_register(&adap->dev);
189 res = device_create_file(&adap->dev, &dev_attr_name);
193 /* Add this adapter to the i2c_adapter class */
194 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
195 adap->class_dev.dev = &adap->dev;
196 adap->class_dev.class = &i2c_adapter_class;
197 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
198 res = class_device_register(&adap->class_dev);
200 goto out_remove_name;
202 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
204 /* inform drivers of new adapters */
205 list_for_each(item,&drivers) {
206 driver = list_entry(item, struct i2c_driver, list);
207 if (driver->attach_adapter)
208 /* We ignore the return code; if it fails, too bad */
209 driver->attach_adapter(adap);
213 mutex_unlock(&core_lists);
217 device_remove_file(&adap->dev, &dev_attr_name);
219 init_completion(&adap->dev_released); /* Needed? */
220 device_unregister(&adap->dev);
221 wait_for_completion(&adap->dev_released);
223 list_del(&adap->list);
224 idr_remove(&i2c_adapter_idr, adap->nr);
229 int i2c_del_adapter(struct i2c_adapter *adap)
231 struct list_head *item, *_n;
232 struct i2c_adapter *adap_from_list;
233 struct i2c_driver *driver;
234 struct i2c_client *client;
237 mutex_lock(&core_lists);
239 /* First make sure that this adapter was ever added */
240 list_for_each_entry(adap_from_list, &adapters, list) {
241 if (adap_from_list == adap)
244 if (adap_from_list != adap) {
245 pr_debug("i2c-core: attempting to delete unregistered "
246 "adapter [%s]\n", adap->name);
251 list_for_each(item,&drivers) {
252 driver = list_entry(item, struct i2c_driver, list);
253 if (driver->detach_adapter)
254 if ((res = driver->detach_adapter(adap))) {
255 dev_err(&adap->dev, "detach_adapter failed "
257 driver->driver.name);
262 /* detach any active clients. This must be done first, because
263 * it can fail; in which case we give up. */
264 list_for_each_safe(item, _n, &adap->clients) {
265 client = list_entry(item, struct i2c_client, list);
267 if ((res=client->driver->detach_client(client))) {
268 dev_err(&adap->dev, "detach_client failed for client "
269 "[%s] at address 0x%02x\n", client->name,
275 /* clean up the sysfs representation */
276 init_completion(&adap->dev_released);
277 init_completion(&adap->class_dev_released);
278 class_device_unregister(&adap->class_dev);
279 device_remove_file(&adap->dev, &dev_attr_name);
280 device_unregister(&adap->dev);
281 list_del(&adap->list);
283 /* wait for sysfs to drop all references */
284 wait_for_completion(&adap->dev_released);
285 wait_for_completion(&adap->class_dev_released);
287 /* free dynamically allocated bus id */
288 idr_remove(&i2c_adapter_idr, adap->nr);
290 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
293 mutex_unlock(&core_lists);
299 * What follows is the "upwards" interface: commands for talking to clients,
300 * which implement the functions to access the physical information of the
304 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
306 struct list_head *item;
307 struct i2c_adapter *adapter;
310 /* add the driver to the list of i2c drivers in the driver core */
311 driver->driver.owner = owner;
312 driver->driver.bus = &i2c_bus_type;
314 res = driver_register(&driver->driver);
318 mutex_lock(&core_lists);
320 list_add_tail(&driver->list,&drivers);
321 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
323 /* now look for instances of driver on our adapters */
324 if (driver->attach_adapter) {
325 list_for_each(item,&adapters) {
326 adapter = list_entry(item, struct i2c_adapter, list);
327 driver->attach_adapter(adapter);
331 mutex_unlock(&core_lists);
334 EXPORT_SYMBOL(i2c_register_driver);
336 int i2c_del_driver(struct i2c_driver *driver)
338 struct list_head *item1, *item2, *_n;
339 struct i2c_client *client;
340 struct i2c_adapter *adap;
344 mutex_lock(&core_lists);
346 /* Have a look at each adapter, if clients of this driver are still
347 * attached. If so, detach them to be able to kill the driver
350 list_for_each(item1,&adapters) {
351 adap = list_entry(item1, struct i2c_adapter, list);
352 if (driver->detach_adapter) {
353 if ((res = driver->detach_adapter(adap))) {
354 dev_err(&adap->dev, "detach_adapter failed "
356 driver->driver.name);
360 list_for_each_safe(item2, _n, &adap->clients) {
361 client = list_entry(item2, struct i2c_client, list);
362 if (client->driver != driver)
364 dev_dbg(&adap->dev, "detaching client [%s] "
365 "at 0x%02x\n", client->name,
367 if ((res = driver->detach_client(client))) {
368 dev_err(&adap->dev, "detach_client "
369 "failed for client [%s] at "
370 "0x%02x\n", client->name,
378 driver_unregister(&driver->driver);
379 list_del(&driver->list);
380 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
383 mutex_unlock(&core_lists);
387 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
389 struct list_head *item;
390 struct i2c_client *client;
392 list_for_each(item,&adapter->clients) {
393 client = list_entry(item, struct i2c_client, list);
394 if (client->addr == addr)
400 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
404 mutex_lock(&adapter->clist_lock);
405 rval = __i2c_check_addr(adapter, addr);
406 mutex_unlock(&adapter->clist_lock);
411 int i2c_attach_client(struct i2c_client *client)
413 struct i2c_adapter *adapter = client->adapter;
416 mutex_lock(&adapter->clist_lock);
417 if (__i2c_check_addr(client->adapter, client->addr)) {
421 list_add_tail(&client->list,&adapter->clients);
423 if (adapter->client_register) {
424 if (adapter->client_register(client)) {
425 dev_dbg(&adapter->dev, "client_register "
426 "failed for client [%s] at 0x%02x\n",
427 client->name, client->addr);
431 client->usage_count = 0;
433 client->dev.parent = &client->adapter->dev;
434 client->dev.driver = &client->driver->driver;
435 client->dev.bus = &i2c_bus_type;
436 client->dev.release = &i2c_client_release;
438 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
439 "%d-%04x", i2c_adapter_id(adapter), client->addr);
440 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
441 client->name, client->dev.bus_id);
442 res = device_register(&client->dev);
445 res = device_create_file(&client->dev, &dev_attr_client_name);
450 mutex_unlock(&adapter->clist_lock);
454 init_completion(&client->released); /* Needed? */
455 device_unregister(&client->dev);
456 wait_for_completion(&client->released);
458 list_del(&client->list);
459 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
460 "(%d)\n", client->name, client->addr, res);
465 int i2c_detach_client(struct i2c_client *client)
467 struct i2c_adapter *adapter = client->adapter;
470 if (client->usage_count > 0) {
471 dev_warn(&client->dev, "Client [%s] still busy, "
472 "can't detach\n", client->name);
476 if (adapter->client_unregister) {
477 res = adapter->client_unregister(client);
479 dev_err(&client->dev,
480 "client_unregister [%s] failed, "
481 "client not detached\n", client->name);
486 mutex_lock(&adapter->clist_lock);
487 list_del(&client->list);
488 init_completion(&client->released);
489 device_remove_file(&client->dev, &dev_attr_client_name);
490 device_unregister(&client->dev);
491 mutex_unlock(&adapter->clist_lock);
492 wait_for_completion(&client->released);
498 static int i2c_inc_use_client(struct i2c_client *client)
501 if (!try_module_get(client->driver->driver.owner))
503 if (!try_module_get(client->adapter->owner)) {
504 module_put(client->driver->driver.owner);
511 static void i2c_dec_use_client(struct i2c_client *client)
513 module_put(client->driver->driver.owner);
514 module_put(client->adapter->owner);
517 int i2c_use_client(struct i2c_client *client)
521 ret = i2c_inc_use_client(client);
525 client->usage_count++;
530 int i2c_release_client(struct i2c_client *client)
532 if (!client->usage_count) {
533 pr_debug("i2c-core: %s used one too many times\n",
538 client->usage_count--;
539 i2c_dec_use_client(client);
544 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
546 struct list_head *item;
547 struct i2c_client *client;
549 mutex_lock(&adap->clist_lock);
550 list_for_each(item,&adap->clients) {
551 client = list_entry(item, struct i2c_client, list);
552 if (!try_module_get(client->driver->driver.owner))
554 if (NULL != client->driver->command) {
555 mutex_unlock(&adap->clist_lock);
556 client->driver->command(client,cmd,arg);
557 mutex_lock(&adap->clist_lock);
559 module_put(client->driver->driver.owner);
561 mutex_unlock(&adap->clist_lock);
564 static int __init i2c_init(void)
568 retval = bus_register(&i2c_bus_type);
571 retval = driver_register(&i2c_adapter_driver);
574 return class_register(&i2c_adapter_class);
577 static void __exit i2c_exit(void)
579 class_unregister(&i2c_adapter_class);
580 driver_unregister(&i2c_adapter_driver);
581 bus_unregister(&i2c_bus_type);
584 subsys_initcall(i2c_init);
585 module_exit(i2c_exit);
587 /* ----------------------------------------------------
588 * the functional interface to the i2c busses.
589 * ----------------------------------------------------
592 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
596 if (adap->algo->master_xfer) {
598 for (ret = 0; ret < num; ret++) {
599 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
600 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
601 'R' : 'W', msgs[ret].addr, msgs[ret].len);
605 mutex_lock(&adap->bus_lock);
606 ret = adap->algo->master_xfer(adap,msgs,num);
607 mutex_unlock(&adap->bus_lock);
611 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
616 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
619 struct i2c_adapter *adap=client->adapter;
622 msg.addr = client->addr;
623 msg.flags = client->flags & I2C_M_TEN;
625 msg.buf = (char *)buf;
627 ret = i2c_transfer(adap, &msg, 1);
629 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
630 transmitted, else error code. */
631 return (ret == 1) ? count : ret;
634 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
636 struct i2c_adapter *adap=client->adapter;
640 msg.addr = client->addr;
641 msg.flags = client->flags & I2C_M_TEN;
642 msg.flags |= I2C_M_RD;
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;
654 int i2c_control(struct i2c_client *client,
655 unsigned int cmd, unsigned long arg)
658 struct i2c_adapter *adap = client->adapter;
660 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
669 if (adap->algo->algo_control!=NULL)
670 ret = adap->algo->algo_control(adap,cmd,arg);
675 /* ----------------------------------------------------
676 * the i2c address scanning function
677 * Will not work for 10-bit addresses!
678 * ----------------------------------------------------
680 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
681 int (*found_proc) (struct i2c_adapter *, int, int))
685 /* Make sure the address is valid */
686 if (addr < 0x03 || addr > 0x77) {
687 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
692 /* Skip if already in use */
693 if (i2c_check_addr(adapter, addr))
696 /* Make sure there is something at this address, unless forced */
698 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
699 I2C_SMBUS_QUICK, NULL) < 0)
702 /* prevent 24RF08 corruption */
703 if ((addr & ~0x0f) == 0x50)
704 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
705 I2C_SMBUS_QUICK, NULL);
708 /* Finally call the custom detection function */
709 err = found_proc(adapter, addr, kind);
710 /* -ENODEV can be returned if there is a chip at the given address
711 but it isn't supported by this chip driver. We catch it here as
712 this isn't an error. */
717 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
722 int i2c_probe(struct i2c_adapter *adapter,
723 struct i2c_client_address_data *address_data,
724 int (*found_proc) (struct i2c_adapter *, int, int))
727 int adap_id = i2c_adapter_id(adapter);
729 /* Force entries are done first, and are not affected by ignore
731 if (address_data->forces) {
732 unsigned short **forces = address_data->forces;
735 for (kind = 0; forces[kind]; kind++) {
736 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
738 if (forces[kind][i] == adap_id
739 || forces[kind][i] == ANY_I2C_BUS) {
740 dev_dbg(&adapter->dev, "found force "
741 "parameter for adapter %d, "
742 "addr 0x%02x, kind %d\n",
743 adap_id, forces[kind][i + 1],
745 err = i2c_probe_address(adapter,
755 /* Stop here if we can't use SMBUS_QUICK */
756 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
757 if (address_data->probe[0] == I2C_CLIENT_END
758 && address_data->normal_i2c[0] == I2C_CLIENT_END)
761 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
762 "can't probe for chips\n");
766 /* Probe entries are done second, and are not affected by ignore
768 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
769 if (address_data->probe[i] == adap_id
770 || address_data->probe[i] == ANY_I2C_BUS) {
771 dev_dbg(&adapter->dev, "found probe parameter for "
772 "adapter %d, addr 0x%02x\n", adap_id,
773 address_data->probe[i + 1]);
774 err = i2c_probe_address(adapter,
775 address_data->probe[i + 1],
782 /* Normal entries are done last, unless shadowed by an ignore entry */
783 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
787 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
789 if ((address_data->ignore[j] == adap_id ||
790 address_data->ignore[j] == ANY_I2C_BUS)
791 && address_data->ignore[j + 1]
792 == address_data->normal_i2c[i]) {
793 dev_dbg(&adapter->dev, "found ignore "
794 "parameter for adapter %d, "
795 "addr 0x%02x\n", adap_id,
796 address_data->ignore[j + 1]);
804 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
805 "addr 0x%02x\n", adap_id,
806 address_data->normal_i2c[i]);
807 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
816 struct i2c_adapter* i2c_get_adapter(int id)
818 struct i2c_adapter *adapter;
820 mutex_lock(&core_lists);
821 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
822 if (adapter && !try_module_get(adapter->owner))
825 mutex_unlock(&core_lists);
829 void i2c_put_adapter(struct i2c_adapter *adap)
831 module_put(adap->owner);
834 /* The SMBus parts */
836 #define POLY (0x1070U << 3)
842 for(i = 0; i < 8; i++) {
847 return (u8)(data >> 8);
850 /* Incremental CRC8 over count bytes in the array pointed to by p */
851 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
855 for(i = 0; i < count; i++)
856 crc = crc8((crc ^ p[i]) << 8);
860 /* Assume a 7-bit address, which is reasonable for SMBus */
861 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
863 /* The address will be sent first */
864 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
865 pec = i2c_smbus_pec(pec, &addr, 1);
867 /* The data buffer follows */
868 return i2c_smbus_pec(pec, msg->buf, msg->len);
871 /* Used for write only transactions */
872 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
874 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
878 /* Return <0 on CRC error
879 If there was a write before this read (most cases) we need to take the
880 partial CRC from the write part into account.
881 Note that this function does modify the message (we need to decrease the
882 message length to hide the CRC byte from the caller). */
883 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
885 u8 rpec = msg->buf[--msg->len];
886 cpec = i2c_smbus_msg_pec(cpec, msg);
889 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
896 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
898 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
899 value,0,I2C_SMBUS_QUICK,NULL);
902 s32 i2c_smbus_read_byte(struct i2c_client *client)
904 union i2c_smbus_data data;
905 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
906 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
912 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
914 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
915 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
918 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
920 union i2c_smbus_data data;
921 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
922 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
928 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
930 union i2c_smbus_data data;
932 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
933 I2C_SMBUS_WRITE,command,
934 I2C_SMBUS_BYTE_DATA,&data);
937 s32 i2c_smbus_read_word_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_WORD_DATA, &data))
947 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 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_WORD_DATA,&data);
956 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
957 u8 length, const u8 *values)
959 union i2c_smbus_data data;
961 if (length > I2C_SMBUS_BLOCK_MAX)
962 length = I2C_SMBUS_BLOCK_MAX;
963 data.block[0] = length;
964 memcpy(&data.block[1], values, length);
965 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
966 I2C_SMBUS_WRITE,command,
967 I2C_SMBUS_BLOCK_DATA,&data);
970 /* Returns the number of read bytes */
971 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
973 union i2c_smbus_data data;
975 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
976 I2C_SMBUS_READ,command,
977 I2C_SMBUS_I2C_BLOCK_DATA,&data))
980 memcpy(values, &data.block[1], data.block[0]);
981 return data.block[0];
984 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
985 u8 length, const u8 *values)
987 union i2c_smbus_data data;
989 if (length > I2C_SMBUS_BLOCK_MAX)
990 length = I2C_SMBUS_BLOCK_MAX;
991 data.block[0] = length;
992 memcpy(data.block + 1, values, length);
993 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
994 I2C_SMBUS_WRITE, command,
995 I2C_SMBUS_I2C_BLOCK_DATA, &data);
998 /* Simulate a SMBus command using the i2c protocol
999 No checking of parameters is done! */
1000 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1001 unsigned short flags,
1002 char read_write, u8 command, int size,
1003 union i2c_smbus_data * data)
1005 /* So we need to generate a series of msgs. In the case of writing, we
1006 need to use only one message; when reading, we need two. We initialize
1007 most things with sane defaults, to keep the code below somewhat
1009 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1010 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1011 int num = read_write == I2C_SMBUS_READ?2:1;
1012 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1013 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1018 msgbuf0[0] = command;
1020 case I2C_SMBUS_QUICK:
1022 /* Special case: The read/write field is used as data */
1023 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1026 case I2C_SMBUS_BYTE:
1027 if (read_write == I2C_SMBUS_READ) {
1028 /* Special case: only a read! */
1029 msg[0].flags = I2C_M_RD | flags;
1033 case I2C_SMBUS_BYTE_DATA:
1034 if (read_write == I2C_SMBUS_READ)
1038 msgbuf0[1] = data->byte;
1041 case I2C_SMBUS_WORD_DATA:
1042 if (read_write == I2C_SMBUS_READ)
1046 msgbuf0[1] = data->word & 0xff;
1047 msgbuf0[2] = data->word >> 8;
1050 case I2C_SMBUS_PROC_CALL:
1051 num = 2; /* Special case */
1052 read_write = I2C_SMBUS_READ;
1055 msgbuf0[1] = data->word & 0xff;
1056 msgbuf0[2] = data->word >> 8;
1058 case I2C_SMBUS_BLOCK_DATA:
1059 if (read_write == I2C_SMBUS_READ) {
1060 dev_err(&adapter->dev, "Block read not supported "
1061 "under I2C emulation!\n");
1064 msg[0].len = data->block[0] + 2;
1065 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1066 dev_err(&adapter->dev, "smbus_access called with "
1067 "invalid block write size (%d)\n",
1071 for (i = 1; i < msg[0].len; i++)
1072 msgbuf0[i] = data->block[i-1];
1075 case I2C_SMBUS_BLOCK_PROC_CALL:
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_BLOCK_MAX;
1083 msg[0].len = data->block[0] + 1;
1084 if (msg[0].len > I2C_SMBUS_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 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1101 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1103 /* Compute PEC if first message is a write */
1104 if (!(msg[0].flags & I2C_M_RD)) {
1105 if (num == 1) /* Write only */
1106 i2c_smbus_add_pec(&msg[0]);
1107 else /* Write followed by read */
1108 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1110 /* Ask for PEC if last message is a read */
1111 if (msg[num-1].flags & I2C_M_RD)
1115 if (i2c_transfer(adapter, msg, num) < 0)
1118 /* Check PEC if last message is a read */
1119 if (i && (msg[num-1].flags & I2C_M_RD)) {
1120 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1124 if (read_write == I2C_SMBUS_READ)
1126 case I2C_SMBUS_BYTE:
1127 data->byte = msgbuf0[0];
1129 case I2C_SMBUS_BYTE_DATA:
1130 data->byte = msgbuf1[0];
1132 case I2C_SMBUS_WORD_DATA:
1133 case I2C_SMBUS_PROC_CALL:
1134 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1136 case I2C_SMBUS_I2C_BLOCK_DATA:
1137 /* fixed at 32 for now */
1138 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1139 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1140 data->block[i+1] = msgbuf1[i];
1147 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1148 char read_write, u8 command, int size,
1149 union i2c_smbus_data * data)
1153 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1155 if (adapter->algo->smbus_xfer) {
1156 mutex_lock(&adapter->bus_lock);
1157 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1159 mutex_unlock(&adapter->bus_lock);
1161 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1168 /* Next four are needed by i2c-isa */
1169 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1170 EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1171 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1172 EXPORT_SYMBOL_GPL(i2c_bus_type);
1174 EXPORT_SYMBOL(i2c_add_adapter);
1175 EXPORT_SYMBOL(i2c_del_adapter);
1176 EXPORT_SYMBOL(i2c_del_driver);
1177 EXPORT_SYMBOL(i2c_attach_client);
1178 EXPORT_SYMBOL(i2c_detach_client);
1179 EXPORT_SYMBOL(i2c_use_client);
1180 EXPORT_SYMBOL(i2c_release_client);
1181 EXPORT_SYMBOL(i2c_clients_command);
1182 EXPORT_SYMBOL(i2c_check_addr);
1184 EXPORT_SYMBOL(i2c_master_send);
1185 EXPORT_SYMBOL(i2c_master_recv);
1186 EXPORT_SYMBOL(i2c_control);
1187 EXPORT_SYMBOL(i2c_transfer);
1188 EXPORT_SYMBOL(i2c_get_adapter);
1189 EXPORT_SYMBOL(i2c_put_adapter);
1190 EXPORT_SYMBOL(i2c_probe);
1192 EXPORT_SYMBOL(i2c_smbus_xfer);
1193 EXPORT_SYMBOL(i2c_smbus_write_quick);
1194 EXPORT_SYMBOL(i2c_smbus_read_byte);
1195 EXPORT_SYMBOL(i2c_smbus_write_byte);
1196 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1197 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1198 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1199 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1200 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1201 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1202 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1204 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1205 MODULE_DESCRIPTION("I2C-Bus main module");
1206 MODULE_LICENSE("GPL");