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);
45 /* ------------------------------------------------------------------------- */
47 /* match always succeeds, as we want the probe() to tell if we really accept this match */
48 static int i2c_device_match(struct device *dev, struct device_driver *drv)
53 static int i2c_device_probe(struct device *dev)
58 static int i2c_device_remove(struct device *dev)
63 static void i2c_device_shutdown(struct device *dev)
65 struct i2c_driver *driver;
69 driver = to_i2c_driver(dev->driver);
71 driver->shutdown(to_i2c_client(dev));
74 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
76 struct i2c_driver *driver;
80 driver = to_i2c_driver(dev->driver);
83 return driver->suspend(to_i2c_client(dev), mesg);
86 static int i2c_device_resume(struct device * dev)
88 struct i2c_driver *driver;
92 driver = to_i2c_driver(dev->driver);
95 return driver->resume(to_i2c_client(dev));
98 struct bus_type i2c_bus_type = {
100 .match = i2c_device_match,
101 .probe = i2c_device_probe,
102 .remove = i2c_device_remove,
103 .shutdown = i2c_device_shutdown,
104 .suspend = i2c_device_suspend,
105 .resume = i2c_device_resume,
108 /* ------------------------------------------------------------------------- */
110 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
112 void i2c_adapter_dev_release(struct device *dev)
114 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
115 complete(&adap->dev_released);
119 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
121 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
122 return sprintf(buf, "%s\n", adap->name);
125 static struct device_attribute i2c_adapter_attrs[] = {
126 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
130 struct class i2c_adapter_class = {
131 .owner = THIS_MODULE,
132 .name = "i2c-adapter",
133 .dev_attrs = i2c_adapter_attrs,
137 static void i2c_client_release(struct device *dev)
139 struct i2c_client *client = to_i2c_client(dev);
140 complete(&client->released);
143 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
145 struct i2c_client *client = to_i2c_client(dev);
146 return sprintf(buf, "%s\n", client->name);
150 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
151 * an i2c adapter attribute (above).
153 static struct device_attribute dev_attr_client_name =
154 __ATTR(name, S_IRUGO, &show_client_name, NULL);
157 /* ---------------------------------------------------
158 * registering functions
159 * ---------------------------------------------------
163 * i2c_add_adapter is called from within the algorithm layer,
164 * when a new hw adapter registers. A new device is register to be
165 * available for clients.
167 int i2c_add_adapter(struct i2c_adapter *adap)
170 struct list_head *item;
171 struct i2c_driver *driver;
173 mutex_lock(&core_lists);
175 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
180 res = idr_get_new(&i2c_adapter_idr, adap, &id);
187 adap->nr = id & MAX_ID_MASK;
188 mutex_init(&adap->bus_lock);
189 mutex_init(&adap->clist_lock);
190 list_add_tail(&adap->list,&adapters);
191 INIT_LIST_HEAD(&adap->clients);
193 /* Add the adapter to the driver core.
194 * If the parent pointer is not set up,
195 * we add this adapter to the host bus.
197 if (adap->dev.parent == NULL) {
198 adap->dev.parent = &platform_bus;
199 pr_debug("I2C adapter driver [%s] forgot to specify "
200 "physical device\n", adap->name);
202 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
203 adap->dev.release = &i2c_adapter_dev_release;
204 adap->dev.class = &i2c_adapter_class;
205 res = device_register(&adap->dev);
209 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
211 /* inform drivers of new adapters */
212 list_for_each(item,&drivers) {
213 driver = list_entry(item, struct i2c_driver, list);
214 if (driver->attach_adapter)
215 /* We ignore the return code; if it fails, too bad */
216 driver->attach_adapter(adap);
220 mutex_unlock(&core_lists);
224 list_del(&adap->list);
225 idr_remove(&i2c_adapter_idr, adap->nr);
230 int i2c_del_adapter(struct i2c_adapter *adap)
232 struct list_head *item, *_n;
233 struct i2c_adapter *adap_from_list;
234 struct i2c_driver *driver;
235 struct i2c_client *client;
238 mutex_lock(&core_lists);
240 /* First make sure that this adapter was ever added */
241 list_for_each_entry(adap_from_list, &adapters, list) {
242 if (adap_from_list == adap)
245 if (adap_from_list != adap) {
246 pr_debug("i2c-core: attempting to delete unregistered "
247 "adapter [%s]\n", adap->name);
252 list_for_each(item,&drivers) {
253 driver = list_entry(item, struct i2c_driver, list);
254 if (driver->detach_adapter)
255 if ((res = driver->detach_adapter(adap))) {
256 dev_err(&adap->dev, "detach_adapter failed "
258 driver->driver.name);
263 /* detach any active clients. This must be done first, because
264 * it can fail; in which case we give up. */
265 list_for_each_safe(item, _n, &adap->clients) {
266 client = list_entry(item, struct i2c_client, list);
268 if ((res=client->driver->detach_client(client))) {
269 dev_err(&adap->dev, "detach_client failed for client "
270 "[%s] at address 0x%02x\n", client->name,
276 /* clean up the sysfs representation */
277 init_completion(&adap->dev_released);
278 device_unregister(&adap->dev);
279 list_del(&adap->list);
281 /* wait for sysfs to drop all references */
282 wait_for_completion(&adap->dev_released);
284 /* free dynamically allocated bus id */
285 idr_remove(&i2c_adapter_idr, adap->nr);
287 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
290 mutex_unlock(&core_lists);
296 * What follows is the "upwards" interface: commands for talking to clients,
297 * which implement the functions to access the physical information of the
301 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
305 /* add the driver to the list of i2c drivers in the driver core */
306 driver->driver.owner = owner;
307 driver->driver.bus = &i2c_bus_type;
309 res = driver_register(&driver->driver);
313 mutex_lock(&core_lists);
315 list_add_tail(&driver->list,&drivers);
316 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
318 /* now look for instances of driver on our adapters */
319 if (driver->attach_adapter) {
320 struct i2c_adapter *adapter;
322 list_for_each_entry(adapter, &adapters, list) {
323 driver->attach_adapter(adapter);
327 mutex_unlock(&core_lists);
330 EXPORT_SYMBOL(i2c_register_driver);
332 int i2c_del_driver(struct i2c_driver *driver)
334 struct list_head *item1, *item2, *_n;
335 struct i2c_client *client;
336 struct i2c_adapter *adap;
340 mutex_lock(&core_lists);
342 /* Have a look at each adapter, if clients of this driver are still
343 * attached. If so, detach them to be able to kill the driver
346 list_for_each(item1,&adapters) {
347 adap = list_entry(item1, struct i2c_adapter, list);
348 if (driver->detach_adapter) {
349 if ((res = driver->detach_adapter(adap))) {
350 dev_err(&adap->dev, "detach_adapter failed "
352 driver->driver.name);
356 list_for_each_safe(item2, _n, &adap->clients) {
357 client = list_entry(item2, struct i2c_client, list);
358 if (client->driver != driver)
360 dev_dbg(&adap->dev, "detaching client [%s] "
361 "at 0x%02x\n", client->name,
363 if ((res = driver->detach_client(client))) {
364 dev_err(&adap->dev, "detach_client "
365 "failed for client [%s] at "
366 "0x%02x\n", client->name,
374 driver_unregister(&driver->driver);
375 list_del(&driver->list);
376 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
379 mutex_unlock(&core_lists);
383 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
385 struct list_head *item;
386 struct i2c_client *client;
388 list_for_each(item,&adapter->clients) {
389 client = list_entry(item, struct i2c_client, list);
390 if (client->addr == addr)
396 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
400 mutex_lock(&adapter->clist_lock);
401 rval = __i2c_check_addr(adapter, addr);
402 mutex_unlock(&adapter->clist_lock);
407 int i2c_attach_client(struct i2c_client *client)
409 struct i2c_adapter *adapter = client->adapter;
412 mutex_lock(&adapter->clist_lock);
413 if (__i2c_check_addr(client->adapter, client->addr)) {
417 list_add_tail(&client->list,&adapter->clients);
419 client->usage_count = 0;
421 client->dev.parent = &client->adapter->dev;
422 client->dev.driver = &client->driver->driver;
423 client->dev.bus = &i2c_bus_type;
424 client->dev.release = &i2c_client_release;
426 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
427 "%d-%04x", i2c_adapter_id(adapter), client->addr);
428 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
429 client->name, client->dev.bus_id);
430 res = device_register(&client->dev);
433 res = device_create_file(&client->dev, &dev_attr_client_name);
436 mutex_unlock(&adapter->clist_lock);
438 if (adapter->client_register) {
439 if (adapter->client_register(client)) {
440 dev_dbg(&adapter->dev, "client_register "
441 "failed for client [%s] at 0x%02x\n",
442 client->name, client->addr);
449 init_completion(&client->released); /* Needed? */
450 device_unregister(&client->dev);
451 wait_for_completion(&client->released);
453 list_del(&client->list);
454 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
455 "(%d)\n", client->name, client->addr, res);
457 mutex_unlock(&adapter->clist_lock);
462 int i2c_detach_client(struct i2c_client *client)
464 struct i2c_adapter *adapter = client->adapter;
467 if (client->usage_count > 0) {
468 dev_warn(&client->dev, "Client [%s] still busy, "
469 "can't detach\n", client->name);
473 if (adapter->client_unregister) {
474 res = adapter->client_unregister(client);
476 dev_err(&client->dev,
477 "client_unregister [%s] failed, "
478 "client not detached\n", client->name);
483 mutex_lock(&adapter->clist_lock);
484 list_del(&client->list);
485 init_completion(&client->released);
486 device_remove_file(&client->dev, &dev_attr_client_name);
487 device_unregister(&client->dev);
488 mutex_unlock(&adapter->clist_lock);
489 wait_for_completion(&client->released);
495 static int i2c_inc_use_client(struct i2c_client *client)
498 if (!try_module_get(client->driver->driver.owner))
500 if (!try_module_get(client->adapter->owner)) {
501 module_put(client->driver->driver.owner);
508 static void i2c_dec_use_client(struct i2c_client *client)
510 module_put(client->driver->driver.owner);
511 module_put(client->adapter->owner);
514 int i2c_use_client(struct i2c_client *client)
518 ret = i2c_inc_use_client(client);
522 client->usage_count++;
527 int i2c_release_client(struct i2c_client *client)
529 if (!client->usage_count) {
530 pr_debug("i2c-core: %s used one too many times\n",
535 client->usage_count--;
536 i2c_dec_use_client(client);
541 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
543 struct list_head *item;
544 struct i2c_client *client;
546 mutex_lock(&adap->clist_lock);
547 list_for_each(item,&adap->clients) {
548 client = list_entry(item, struct i2c_client, list);
549 if (!try_module_get(client->driver->driver.owner))
551 if (NULL != client->driver->command) {
552 mutex_unlock(&adap->clist_lock);
553 client->driver->command(client,cmd,arg);
554 mutex_lock(&adap->clist_lock);
556 module_put(client->driver->driver.owner);
558 mutex_unlock(&adap->clist_lock);
561 static int __init i2c_init(void)
565 retval = bus_register(&i2c_bus_type);
568 return class_register(&i2c_adapter_class);
571 static void __exit i2c_exit(void)
573 class_unregister(&i2c_adapter_class);
574 bus_unregister(&i2c_bus_type);
577 subsys_initcall(i2c_init);
578 module_exit(i2c_exit);
580 /* ----------------------------------------------------
581 * the functional interface to the i2c busses.
582 * ----------------------------------------------------
585 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
589 if (adap->algo->master_xfer) {
591 for (ret = 0; ret < num; ret++) {
592 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
593 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
594 'R' : 'W', msgs[ret].addr, msgs[ret].len);
598 mutex_lock_nested(&adap->bus_lock, adap->level);
599 ret = adap->algo->master_xfer(adap,msgs,num);
600 mutex_unlock(&adap->bus_lock);
604 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
609 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
612 struct i2c_adapter *adap=client->adapter;
615 msg.addr = client->addr;
616 msg.flags = client->flags & I2C_M_TEN;
618 msg.buf = (char *)buf;
620 ret = i2c_transfer(adap, &msg, 1);
622 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
623 transmitted, else error code. */
624 return (ret == 1) ? count : ret;
627 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
629 struct i2c_adapter *adap=client->adapter;
633 msg.addr = client->addr;
634 msg.flags = client->flags & I2C_M_TEN;
635 msg.flags |= I2C_M_RD;
639 ret = i2c_transfer(adap, &msg, 1);
641 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
642 transmitted, else error code. */
643 return (ret == 1) ? count : ret;
647 int i2c_control(struct i2c_client *client,
648 unsigned int cmd, unsigned long arg)
651 struct i2c_adapter *adap = client->adapter;
653 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
662 if (adap->algo->algo_control!=NULL)
663 ret = adap->algo->algo_control(adap,cmd,arg);
668 /* ----------------------------------------------------
669 * the i2c address scanning function
670 * Will not work for 10-bit addresses!
671 * ----------------------------------------------------
673 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
674 int (*found_proc) (struct i2c_adapter *, int, int))
678 /* Make sure the address is valid */
679 if (addr < 0x03 || addr > 0x77) {
680 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
685 /* Skip if already in use */
686 if (i2c_check_addr(adapter, addr))
689 /* Make sure there is something at this address, unless forced */
691 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
692 I2C_SMBUS_QUICK, NULL) < 0)
695 /* prevent 24RF08 corruption */
696 if ((addr & ~0x0f) == 0x50)
697 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
698 I2C_SMBUS_QUICK, NULL);
701 /* Finally call the custom detection function */
702 err = found_proc(adapter, addr, kind);
703 /* -ENODEV can be returned if there is a chip at the given address
704 but it isn't supported by this chip driver. We catch it here as
705 this isn't an error. */
710 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
715 int i2c_probe(struct i2c_adapter *adapter,
716 struct i2c_client_address_data *address_data,
717 int (*found_proc) (struct i2c_adapter *, int, int))
720 int adap_id = i2c_adapter_id(adapter);
722 /* Force entries are done first, and are not affected by ignore
724 if (address_data->forces) {
725 unsigned short **forces = address_data->forces;
728 for (kind = 0; forces[kind]; kind++) {
729 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
731 if (forces[kind][i] == adap_id
732 || forces[kind][i] == ANY_I2C_BUS) {
733 dev_dbg(&adapter->dev, "found force "
734 "parameter for adapter %d, "
735 "addr 0x%02x, kind %d\n",
736 adap_id, forces[kind][i + 1],
738 err = i2c_probe_address(adapter,
748 /* Stop here if we can't use SMBUS_QUICK */
749 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
750 if (address_data->probe[0] == I2C_CLIENT_END
751 && address_data->normal_i2c[0] == I2C_CLIENT_END)
754 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
755 "can't probe for chips\n");
759 /* Probe entries are done second, and are not affected by ignore
761 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
762 if (address_data->probe[i] == adap_id
763 || address_data->probe[i] == ANY_I2C_BUS) {
764 dev_dbg(&adapter->dev, "found probe parameter for "
765 "adapter %d, addr 0x%02x\n", adap_id,
766 address_data->probe[i + 1]);
767 err = i2c_probe_address(adapter,
768 address_data->probe[i + 1],
775 /* Normal entries are done last, unless shadowed by an ignore entry */
776 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
780 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
782 if ((address_data->ignore[j] == adap_id ||
783 address_data->ignore[j] == ANY_I2C_BUS)
784 && address_data->ignore[j + 1]
785 == address_data->normal_i2c[i]) {
786 dev_dbg(&adapter->dev, "found ignore "
787 "parameter for adapter %d, "
788 "addr 0x%02x\n", adap_id,
789 address_data->ignore[j + 1]);
797 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
798 "addr 0x%02x\n", adap_id,
799 address_data->normal_i2c[i]);
800 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
809 struct i2c_adapter* i2c_get_adapter(int id)
811 struct i2c_adapter *adapter;
813 mutex_lock(&core_lists);
814 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
815 if (adapter && !try_module_get(adapter->owner))
818 mutex_unlock(&core_lists);
822 void i2c_put_adapter(struct i2c_adapter *adap)
824 module_put(adap->owner);
827 /* The SMBus parts */
829 #define POLY (0x1070U << 3)
835 for(i = 0; i < 8; i++) {
840 return (u8)(data >> 8);
843 /* Incremental CRC8 over count bytes in the array pointed to by p */
844 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
848 for(i = 0; i < count; i++)
849 crc = crc8((crc ^ p[i]) << 8);
853 /* Assume a 7-bit address, which is reasonable for SMBus */
854 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
856 /* The address will be sent first */
857 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
858 pec = i2c_smbus_pec(pec, &addr, 1);
860 /* The data buffer follows */
861 return i2c_smbus_pec(pec, msg->buf, msg->len);
864 /* Used for write only transactions */
865 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
867 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
871 /* Return <0 on CRC error
872 If there was a write before this read (most cases) we need to take the
873 partial CRC from the write part into account.
874 Note that this function does modify the message (we need to decrease the
875 message length to hide the CRC byte from the caller). */
876 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
878 u8 rpec = msg->buf[--msg->len];
879 cpec = i2c_smbus_msg_pec(cpec, msg);
882 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
889 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
891 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
892 value,0,I2C_SMBUS_QUICK,NULL);
895 s32 i2c_smbus_read_byte(struct i2c_client *client)
897 union i2c_smbus_data data;
898 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
899 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
905 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
907 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
908 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
911 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
913 union i2c_smbus_data data;
914 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
915 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
921 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
923 union i2c_smbus_data data;
925 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
926 I2C_SMBUS_WRITE,command,
927 I2C_SMBUS_BYTE_DATA,&data);
930 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
932 union i2c_smbus_data data;
933 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
934 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
940 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
942 union i2c_smbus_data data;
944 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
945 I2C_SMBUS_WRITE,command,
946 I2C_SMBUS_WORD_DATA,&data);
949 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
950 u8 length, const u8 *values)
952 union i2c_smbus_data data;
954 if (length > I2C_SMBUS_BLOCK_MAX)
955 length = I2C_SMBUS_BLOCK_MAX;
956 data.block[0] = length;
957 memcpy(&data.block[1], values, length);
958 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
959 I2C_SMBUS_WRITE,command,
960 I2C_SMBUS_BLOCK_DATA,&data);
963 /* Returns the number of read bytes */
964 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
966 union i2c_smbus_data data;
968 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
969 I2C_SMBUS_READ,command,
970 I2C_SMBUS_I2C_BLOCK_DATA,&data))
973 memcpy(values, &data.block[1], data.block[0]);
974 return data.block[0];
977 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
978 u8 length, const u8 *values)
980 union i2c_smbus_data data;
982 if (length > I2C_SMBUS_BLOCK_MAX)
983 length = I2C_SMBUS_BLOCK_MAX;
984 data.block[0] = length;
985 memcpy(data.block + 1, values, length);
986 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
987 I2C_SMBUS_WRITE, command,
988 I2C_SMBUS_I2C_BLOCK_DATA, &data);
991 /* Simulate a SMBus command using the i2c protocol
992 No checking of parameters is done! */
993 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
994 unsigned short flags,
995 char read_write, u8 command, int size,
996 union i2c_smbus_data * data)
998 /* So we need to generate a series of msgs. In the case of writing, we
999 need to use only one message; when reading, we need two. We initialize
1000 most things with sane defaults, to keep the code below somewhat
1002 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1003 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1004 int num = read_write == I2C_SMBUS_READ?2:1;
1005 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1006 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1011 msgbuf0[0] = command;
1013 case I2C_SMBUS_QUICK:
1015 /* Special case: The read/write field is used as data */
1016 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1019 case I2C_SMBUS_BYTE:
1020 if (read_write == I2C_SMBUS_READ) {
1021 /* Special case: only a read! */
1022 msg[0].flags = I2C_M_RD | flags;
1026 case I2C_SMBUS_BYTE_DATA:
1027 if (read_write == I2C_SMBUS_READ)
1031 msgbuf0[1] = data->byte;
1034 case I2C_SMBUS_WORD_DATA:
1035 if (read_write == I2C_SMBUS_READ)
1039 msgbuf0[1] = data->word & 0xff;
1040 msgbuf0[2] = data->word >> 8;
1043 case I2C_SMBUS_PROC_CALL:
1044 num = 2; /* Special case */
1045 read_write = I2C_SMBUS_READ;
1048 msgbuf0[1] = data->word & 0xff;
1049 msgbuf0[2] = data->word >> 8;
1051 case I2C_SMBUS_BLOCK_DATA:
1052 if (read_write == I2C_SMBUS_READ) {
1053 dev_err(&adapter->dev, "Block read not supported "
1054 "under I2C emulation!\n");
1057 msg[0].len = data->block[0] + 2;
1058 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1059 dev_err(&adapter->dev, "smbus_access called with "
1060 "invalid block write size (%d)\n",
1064 for (i = 1; i < msg[0].len; i++)
1065 msgbuf0[i] = data->block[i-1];
1068 case I2C_SMBUS_BLOCK_PROC_CALL:
1069 dev_dbg(&adapter->dev, "Block process call not supported "
1070 "under I2C emulation!\n");
1072 case I2C_SMBUS_I2C_BLOCK_DATA:
1073 if (read_write == I2C_SMBUS_READ) {
1074 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1076 msg[0].len = data->block[0] + 1;
1077 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1078 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1079 "invalid block write size (%d)\n",
1083 for (i = 1; i <= data->block[0]; i++)
1084 msgbuf0[i] = data->block[i];
1088 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1093 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1094 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1096 /* Compute PEC if first message is a write */
1097 if (!(msg[0].flags & I2C_M_RD)) {
1098 if (num == 1) /* Write only */
1099 i2c_smbus_add_pec(&msg[0]);
1100 else /* Write followed by read */
1101 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1103 /* Ask for PEC if last message is a read */
1104 if (msg[num-1].flags & I2C_M_RD)
1108 if (i2c_transfer(adapter, msg, num) < 0)
1111 /* Check PEC if last message is a read */
1112 if (i && (msg[num-1].flags & I2C_M_RD)) {
1113 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1117 if (read_write == I2C_SMBUS_READ)
1119 case I2C_SMBUS_BYTE:
1120 data->byte = msgbuf0[0];
1122 case I2C_SMBUS_BYTE_DATA:
1123 data->byte = msgbuf1[0];
1125 case I2C_SMBUS_WORD_DATA:
1126 case I2C_SMBUS_PROC_CALL:
1127 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1129 case I2C_SMBUS_I2C_BLOCK_DATA:
1130 /* fixed at 32 for now */
1131 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1132 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1133 data->block[i+1] = msgbuf1[i];
1140 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1141 char read_write, u8 command, int size,
1142 union i2c_smbus_data * data)
1146 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1148 if (adapter->algo->smbus_xfer) {
1149 mutex_lock(&adapter->bus_lock);
1150 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1152 mutex_unlock(&adapter->bus_lock);
1154 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1161 /* Next three are needed by i2c-isa */
1162 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1163 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1164 EXPORT_SYMBOL_GPL(i2c_bus_type);
1166 EXPORT_SYMBOL(i2c_add_adapter);
1167 EXPORT_SYMBOL(i2c_del_adapter);
1168 EXPORT_SYMBOL(i2c_del_driver);
1169 EXPORT_SYMBOL(i2c_attach_client);
1170 EXPORT_SYMBOL(i2c_detach_client);
1171 EXPORT_SYMBOL(i2c_use_client);
1172 EXPORT_SYMBOL(i2c_release_client);
1173 EXPORT_SYMBOL(i2c_clients_command);
1174 EXPORT_SYMBOL(i2c_check_addr);
1176 EXPORT_SYMBOL(i2c_master_send);
1177 EXPORT_SYMBOL(i2c_master_recv);
1178 EXPORT_SYMBOL(i2c_control);
1179 EXPORT_SYMBOL(i2c_transfer);
1180 EXPORT_SYMBOL(i2c_get_adapter);
1181 EXPORT_SYMBOL(i2c_put_adapter);
1182 EXPORT_SYMBOL(i2c_probe);
1184 EXPORT_SYMBOL(i2c_smbus_xfer);
1185 EXPORT_SYMBOL(i2c_smbus_write_quick);
1186 EXPORT_SYMBOL(i2c_smbus_read_byte);
1187 EXPORT_SYMBOL(i2c_smbus_write_byte);
1188 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1189 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1190 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1191 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1192 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1193 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1194 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1196 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1197 MODULE_DESCRIPTION("I2C-Bus main module");
1198 MODULE_LICENSE("GPL");