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 void i2c_adapter_dev_release(struct device *dev)
112 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
113 complete(&adap->dev_released);
116 struct device_driver i2c_adapter_driver = {
117 .owner = THIS_MODULE,
118 .name = "i2c_adapter",
119 .bus = &i2c_bus_type,
122 /* ------------------------------------------------------------------------- */
124 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
126 struct class i2c_adapter_class = {
127 .owner = THIS_MODULE,
128 .name = "i2c-adapter",
131 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
133 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
134 return sprintf(buf, "%s\n", adap->name);
136 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
139 static void i2c_client_release(struct device *dev)
141 struct i2c_client *client = to_i2c_client(dev);
142 complete(&client->released);
145 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
147 struct i2c_client *client = to_i2c_client(dev);
148 return sprintf(buf, "%s\n", client->name);
152 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
153 * an i2c adapter attribute (above).
155 static struct device_attribute dev_attr_client_name =
156 __ATTR(name, S_IRUGO, &show_client_name, NULL);
159 /* ---------------------------------------------------
160 * registering functions
161 * ---------------------------------------------------
165 * i2c_add_adapter is called from within the algorithm layer,
166 * when a new hw adapter registers. A new device is register to be
167 * available for clients.
169 int i2c_add_adapter(struct i2c_adapter *adap)
172 struct list_head *item;
173 struct i2c_driver *driver;
175 mutex_lock(&core_lists);
177 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
182 res = idr_get_new(&i2c_adapter_idr, adap, &id);
189 adap->nr = id & MAX_ID_MASK;
190 mutex_init(&adap->bus_lock);
191 mutex_init(&adap->clist_lock);
192 list_add_tail(&adap->list,&adapters);
193 INIT_LIST_HEAD(&adap->clients);
195 /* Add the adapter to the driver core.
196 * If the parent pointer is not set up,
197 * we add this adapter to the host bus.
199 if (adap->dev.parent == NULL) {
200 adap->dev.parent = &platform_bus;
201 pr_debug("I2C adapter driver [%s] forgot to specify "
202 "physical device\n", adap->name);
204 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
205 adap->dev.driver = &i2c_adapter_driver;
206 adap->dev.release = &i2c_adapter_dev_release;
207 adap->dev.class = &i2c_adapter_class;
208 res = device_register(&adap->dev);
211 res = device_create_file(&adap->dev, &dev_attr_name);
215 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
217 /* inform drivers of new adapters */
218 list_for_each(item,&drivers) {
219 driver = list_entry(item, struct i2c_driver, list);
220 if (driver->attach_adapter)
221 /* We ignore the return code; if it fails, too bad */
222 driver->attach_adapter(adap);
226 mutex_unlock(&core_lists);
230 init_completion(&adap->dev_released); /* Needed? */
231 device_unregister(&adap->dev);
232 wait_for_completion(&adap->dev_released);
234 list_del(&adap->list);
235 idr_remove(&i2c_adapter_idr, adap->nr);
240 int i2c_del_adapter(struct i2c_adapter *adap)
242 struct list_head *item, *_n;
243 struct i2c_adapter *adap_from_list;
244 struct i2c_driver *driver;
245 struct i2c_client *client;
248 mutex_lock(&core_lists);
250 /* First make sure that this adapter was ever added */
251 list_for_each_entry(adap_from_list, &adapters, list) {
252 if (adap_from_list == adap)
255 if (adap_from_list != adap) {
256 pr_debug("i2c-core: attempting to delete unregistered "
257 "adapter [%s]\n", adap->name);
262 list_for_each(item,&drivers) {
263 driver = list_entry(item, struct i2c_driver, list);
264 if (driver->detach_adapter)
265 if ((res = driver->detach_adapter(adap))) {
266 dev_err(&adap->dev, "detach_adapter failed "
268 driver->driver.name);
273 /* detach any active clients. This must be done first, because
274 * it can fail; in which case we give up. */
275 list_for_each_safe(item, _n, &adap->clients) {
276 client = list_entry(item, struct i2c_client, list);
278 if ((res=client->driver->detach_client(client))) {
279 dev_err(&adap->dev, "detach_client failed for client "
280 "[%s] at address 0x%02x\n", client->name,
286 /* clean up the sysfs representation */
287 init_completion(&adap->dev_released);
288 device_remove_file(&adap->dev, &dev_attr_name);
289 device_unregister(&adap->dev);
290 list_del(&adap->list);
292 /* wait for sysfs to drop all references */
293 wait_for_completion(&adap->dev_released);
295 /* free dynamically allocated bus id */
296 idr_remove(&i2c_adapter_idr, adap->nr);
298 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
301 mutex_unlock(&core_lists);
307 * What follows is the "upwards" interface: commands for talking to clients,
308 * which implement the functions to access the physical information of the
312 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
314 struct list_head *item;
315 struct i2c_adapter *adapter;
318 /* add the driver to the list of i2c drivers in the driver core */
319 driver->driver.owner = owner;
320 driver->driver.bus = &i2c_bus_type;
322 res = driver_register(&driver->driver);
326 mutex_lock(&core_lists);
328 list_add_tail(&driver->list,&drivers);
329 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
331 /* now look for instances of driver on our adapters */
332 if (driver->attach_adapter) {
333 list_for_each(item,&adapters) {
334 adapter = list_entry(item, struct i2c_adapter, list);
335 driver->attach_adapter(adapter);
339 mutex_unlock(&core_lists);
342 EXPORT_SYMBOL(i2c_register_driver);
344 int i2c_del_driver(struct i2c_driver *driver)
346 struct list_head *item1, *item2, *_n;
347 struct i2c_client *client;
348 struct i2c_adapter *adap;
352 mutex_lock(&core_lists);
354 /* Have a look at each adapter, if clients of this driver are still
355 * attached. If so, detach them to be able to kill the driver
358 list_for_each(item1,&adapters) {
359 adap = list_entry(item1, struct i2c_adapter, list);
360 if (driver->detach_adapter) {
361 if ((res = driver->detach_adapter(adap))) {
362 dev_err(&adap->dev, "detach_adapter failed "
364 driver->driver.name);
368 list_for_each_safe(item2, _n, &adap->clients) {
369 client = list_entry(item2, struct i2c_client, list);
370 if (client->driver != driver)
372 dev_dbg(&adap->dev, "detaching client [%s] "
373 "at 0x%02x\n", client->name,
375 if ((res = driver->detach_client(client))) {
376 dev_err(&adap->dev, "detach_client "
377 "failed for client [%s] at "
378 "0x%02x\n", client->name,
386 driver_unregister(&driver->driver);
387 list_del(&driver->list);
388 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
391 mutex_unlock(&core_lists);
395 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
397 struct list_head *item;
398 struct i2c_client *client;
400 list_for_each(item,&adapter->clients) {
401 client = list_entry(item, struct i2c_client, list);
402 if (client->addr == addr)
408 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
412 mutex_lock(&adapter->clist_lock);
413 rval = __i2c_check_addr(adapter, addr);
414 mutex_unlock(&adapter->clist_lock);
419 int i2c_attach_client(struct i2c_client *client)
421 struct i2c_adapter *adapter = client->adapter;
424 mutex_lock(&adapter->clist_lock);
425 if (__i2c_check_addr(client->adapter, client->addr)) {
429 list_add_tail(&client->list,&adapter->clients);
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);
448 mutex_unlock(&adapter->clist_lock);
450 if (adapter->client_register) {
451 if (adapter->client_register(client)) {
452 dev_dbg(&adapter->dev, "client_register "
453 "failed for client [%s] at 0x%02x\n",
454 client->name, client->addr);
461 init_completion(&client->released); /* Needed? */
462 device_unregister(&client->dev);
463 wait_for_completion(&client->released);
465 list_del(&client->list);
466 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
467 "(%d)\n", client->name, client->addr, res);
469 mutex_unlock(&adapter->clist_lock);
474 int i2c_detach_client(struct i2c_client *client)
476 struct i2c_adapter *adapter = client->adapter;
479 if (client->usage_count > 0) {
480 dev_warn(&client->dev, "Client [%s] still busy, "
481 "can't detach\n", client->name);
485 if (adapter->client_unregister) {
486 res = adapter->client_unregister(client);
488 dev_err(&client->dev,
489 "client_unregister [%s] failed, "
490 "client not detached\n", client->name);
495 mutex_lock(&adapter->clist_lock);
496 list_del(&client->list);
497 init_completion(&client->released);
498 device_remove_file(&client->dev, &dev_attr_client_name);
499 device_unregister(&client->dev);
500 mutex_unlock(&adapter->clist_lock);
501 wait_for_completion(&client->released);
507 static int i2c_inc_use_client(struct i2c_client *client)
510 if (!try_module_get(client->driver->driver.owner))
512 if (!try_module_get(client->adapter->owner)) {
513 module_put(client->driver->driver.owner);
520 static void i2c_dec_use_client(struct i2c_client *client)
522 module_put(client->driver->driver.owner);
523 module_put(client->adapter->owner);
526 int i2c_use_client(struct i2c_client *client)
530 ret = i2c_inc_use_client(client);
534 client->usage_count++;
539 int i2c_release_client(struct i2c_client *client)
541 if (!client->usage_count) {
542 pr_debug("i2c-core: %s used one too many times\n",
547 client->usage_count--;
548 i2c_dec_use_client(client);
553 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
555 struct list_head *item;
556 struct i2c_client *client;
558 mutex_lock(&adap->clist_lock);
559 list_for_each(item,&adap->clients) {
560 client = list_entry(item, struct i2c_client, list);
561 if (!try_module_get(client->driver->driver.owner))
563 if (NULL != client->driver->command) {
564 mutex_unlock(&adap->clist_lock);
565 client->driver->command(client,cmd,arg);
566 mutex_lock(&adap->clist_lock);
568 module_put(client->driver->driver.owner);
570 mutex_unlock(&adap->clist_lock);
573 static int __init i2c_init(void)
577 retval = bus_register(&i2c_bus_type);
580 retval = driver_register(&i2c_adapter_driver);
583 return class_register(&i2c_adapter_class);
586 static void __exit i2c_exit(void)
588 class_unregister(&i2c_adapter_class);
589 driver_unregister(&i2c_adapter_driver);
590 bus_unregister(&i2c_bus_type);
593 subsys_initcall(i2c_init);
594 module_exit(i2c_exit);
596 /* ----------------------------------------------------
597 * the functional interface to the i2c busses.
598 * ----------------------------------------------------
601 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
605 if (adap->algo->master_xfer) {
607 for (ret = 0; ret < num; ret++) {
608 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
609 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
610 'R' : 'W', msgs[ret].addr, msgs[ret].len);
614 mutex_lock_nested(&adap->bus_lock, adap->level);
615 ret = adap->algo->master_xfer(adap,msgs,num);
616 mutex_unlock(&adap->bus_lock);
620 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
625 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
628 struct i2c_adapter *adap=client->adapter;
631 msg.addr = client->addr;
632 msg.flags = client->flags & I2C_M_TEN;
634 msg.buf = (char *)buf;
636 ret = i2c_transfer(adap, &msg, 1);
638 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
639 transmitted, else error code. */
640 return (ret == 1) ? count : ret;
643 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
645 struct i2c_adapter *adap=client->adapter;
649 msg.addr = client->addr;
650 msg.flags = client->flags & I2C_M_TEN;
651 msg.flags |= I2C_M_RD;
655 ret = i2c_transfer(adap, &msg, 1);
657 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
658 transmitted, else error code. */
659 return (ret == 1) ? count : ret;
663 int i2c_control(struct i2c_client *client,
664 unsigned int cmd, unsigned long arg)
667 struct i2c_adapter *adap = client->adapter;
669 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
678 if (adap->algo->algo_control!=NULL)
679 ret = adap->algo->algo_control(adap,cmd,arg);
684 /* ----------------------------------------------------
685 * the i2c address scanning function
686 * Will not work for 10-bit addresses!
687 * ----------------------------------------------------
689 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
690 int (*found_proc) (struct i2c_adapter *, int, int))
694 /* Make sure the address is valid */
695 if (addr < 0x03 || addr > 0x77) {
696 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
701 /* Skip if already in use */
702 if (i2c_check_addr(adapter, addr))
705 /* Make sure there is something at this address, unless forced */
707 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
708 I2C_SMBUS_QUICK, NULL) < 0)
711 /* prevent 24RF08 corruption */
712 if ((addr & ~0x0f) == 0x50)
713 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
714 I2C_SMBUS_QUICK, NULL);
717 /* Finally call the custom detection function */
718 err = found_proc(adapter, addr, kind);
719 /* -ENODEV can be returned if there is a chip at the given address
720 but it isn't supported by this chip driver. We catch it here as
721 this isn't an error. */
726 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
731 int i2c_probe(struct i2c_adapter *adapter,
732 struct i2c_client_address_data *address_data,
733 int (*found_proc) (struct i2c_adapter *, int, int))
736 int adap_id = i2c_adapter_id(adapter);
738 /* Force entries are done first, and are not affected by ignore
740 if (address_data->forces) {
741 unsigned short **forces = address_data->forces;
744 for (kind = 0; forces[kind]; kind++) {
745 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
747 if (forces[kind][i] == adap_id
748 || forces[kind][i] == ANY_I2C_BUS) {
749 dev_dbg(&adapter->dev, "found force "
750 "parameter for adapter %d, "
751 "addr 0x%02x, kind %d\n",
752 adap_id, forces[kind][i + 1],
754 err = i2c_probe_address(adapter,
764 /* Stop here if we can't use SMBUS_QUICK */
765 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
766 if (address_data->probe[0] == I2C_CLIENT_END
767 && address_data->normal_i2c[0] == I2C_CLIENT_END)
770 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
771 "can't probe for chips\n");
775 /* Probe entries are done second, and are not affected by ignore
777 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
778 if (address_data->probe[i] == adap_id
779 || address_data->probe[i] == ANY_I2C_BUS) {
780 dev_dbg(&adapter->dev, "found probe parameter for "
781 "adapter %d, addr 0x%02x\n", adap_id,
782 address_data->probe[i + 1]);
783 err = i2c_probe_address(adapter,
784 address_data->probe[i + 1],
791 /* Normal entries are done last, unless shadowed by an ignore entry */
792 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
796 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
798 if ((address_data->ignore[j] == adap_id ||
799 address_data->ignore[j] == ANY_I2C_BUS)
800 && address_data->ignore[j + 1]
801 == address_data->normal_i2c[i]) {
802 dev_dbg(&adapter->dev, "found ignore "
803 "parameter for adapter %d, "
804 "addr 0x%02x\n", adap_id,
805 address_data->ignore[j + 1]);
813 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
814 "addr 0x%02x\n", adap_id,
815 address_data->normal_i2c[i]);
816 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
825 struct i2c_adapter* i2c_get_adapter(int id)
827 struct i2c_adapter *adapter;
829 mutex_lock(&core_lists);
830 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
831 if (adapter && !try_module_get(adapter->owner))
834 mutex_unlock(&core_lists);
838 void i2c_put_adapter(struct i2c_adapter *adap)
840 module_put(adap->owner);
843 /* The SMBus parts */
845 #define POLY (0x1070U << 3)
851 for(i = 0; i < 8; i++) {
856 return (u8)(data >> 8);
859 /* Incremental CRC8 over count bytes in the array pointed to by p */
860 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
864 for(i = 0; i < count; i++)
865 crc = crc8((crc ^ p[i]) << 8);
869 /* Assume a 7-bit address, which is reasonable for SMBus */
870 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
872 /* The address will be sent first */
873 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
874 pec = i2c_smbus_pec(pec, &addr, 1);
876 /* The data buffer follows */
877 return i2c_smbus_pec(pec, msg->buf, msg->len);
880 /* Used for write only transactions */
881 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
883 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
887 /* Return <0 on CRC error
888 If there was a write before this read (most cases) we need to take the
889 partial CRC from the write part into account.
890 Note that this function does modify the message (we need to decrease the
891 message length to hide the CRC byte from the caller). */
892 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
894 u8 rpec = msg->buf[--msg->len];
895 cpec = i2c_smbus_msg_pec(cpec, msg);
898 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
905 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
907 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
908 value,0,I2C_SMBUS_QUICK,NULL);
911 s32 i2c_smbus_read_byte(struct i2c_client *client)
913 union i2c_smbus_data data;
914 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
915 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
921 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
923 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
924 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
927 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
929 union i2c_smbus_data data;
930 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
931 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
937 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
939 union i2c_smbus_data data;
941 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
942 I2C_SMBUS_WRITE,command,
943 I2C_SMBUS_BYTE_DATA,&data);
946 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
948 union i2c_smbus_data data;
949 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
950 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
956 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
958 union i2c_smbus_data data;
960 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
961 I2C_SMBUS_WRITE,command,
962 I2C_SMBUS_WORD_DATA,&data);
965 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
966 u8 length, const u8 *values)
968 union i2c_smbus_data data;
970 if (length > I2C_SMBUS_BLOCK_MAX)
971 length = I2C_SMBUS_BLOCK_MAX;
972 data.block[0] = length;
973 memcpy(&data.block[1], values, length);
974 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
975 I2C_SMBUS_WRITE,command,
976 I2C_SMBUS_BLOCK_DATA,&data);
979 /* Returns the number of read bytes */
980 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
982 union i2c_smbus_data data;
984 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
985 I2C_SMBUS_READ,command,
986 I2C_SMBUS_I2C_BLOCK_DATA,&data))
989 memcpy(values, &data.block[1], data.block[0]);
990 return data.block[0];
993 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
994 u8 length, const u8 *values)
996 union i2c_smbus_data data;
998 if (length > I2C_SMBUS_BLOCK_MAX)
999 length = I2C_SMBUS_BLOCK_MAX;
1000 data.block[0] = length;
1001 memcpy(data.block + 1, values, length);
1002 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1003 I2C_SMBUS_WRITE, command,
1004 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1007 /* Simulate a SMBus command using the i2c protocol
1008 No checking of parameters is done! */
1009 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1010 unsigned short flags,
1011 char read_write, u8 command, int size,
1012 union i2c_smbus_data * data)
1014 /* So we need to generate a series of msgs. In the case of writing, we
1015 need to use only one message; when reading, we need two. We initialize
1016 most things with sane defaults, to keep the code below somewhat
1018 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1019 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1020 int num = read_write == I2C_SMBUS_READ?2:1;
1021 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1022 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1027 msgbuf0[0] = command;
1029 case I2C_SMBUS_QUICK:
1031 /* Special case: The read/write field is used as data */
1032 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1035 case I2C_SMBUS_BYTE:
1036 if (read_write == I2C_SMBUS_READ) {
1037 /* Special case: only a read! */
1038 msg[0].flags = I2C_M_RD | flags;
1042 case I2C_SMBUS_BYTE_DATA:
1043 if (read_write == I2C_SMBUS_READ)
1047 msgbuf0[1] = data->byte;
1050 case I2C_SMBUS_WORD_DATA:
1051 if (read_write == I2C_SMBUS_READ)
1055 msgbuf0[1] = data->word & 0xff;
1056 msgbuf0[2] = data->word >> 8;
1059 case I2C_SMBUS_PROC_CALL:
1060 num = 2; /* Special case */
1061 read_write = I2C_SMBUS_READ;
1064 msgbuf0[1] = data->word & 0xff;
1065 msgbuf0[2] = data->word >> 8;
1067 case I2C_SMBUS_BLOCK_DATA:
1068 if (read_write == I2C_SMBUS_READ) {
1069 dev_err(&adapter->dev, "Block read not supported "
1070 "under I2C emulation!\n");
1073 msg[0].len = data->block[0] + 2;
1074 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1075 dev_err(&adapter->dev, "smbus_access called with "
1076 "invalid block write size (%d)\n",
1080 for (i = 1; i < msg[0].len; i++)
1081 msgbuf0[i] = data->block[i-1];
1084 case I2C_SMBUS_BLOCK_PROC_CALL:
1085 dev_dbg(&adapter->dev, "Block process call not supported "
1086 "under I2C emulation!\n");
1088 case I2C_SMBUS_I2C_BLOCK_DATA:
1089 if (read_write == I2C_SMBUS_READ) {
1090 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1092 msg[0].len = data->block[0] + 1;
1093 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1094 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1095 "invalid block write size (%d)\n",
1099 for (i = 1; i <= data->block[0]; i++)
1100 msgbuf0[i] = data->block[i];
1104 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1109 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1110 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1112 /* Compute PEC if first message is a write */
1113 if (!(msg[0].flags & I2C_M_RD)) {
1114 if (num == 1) /* Write only */
1115 i2c_smbus_add_pec(&msg[0]);
1116 else /* Write followed by read */
1117 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1119 /* Ask for PEC if last message is a read */
1120 if (msg[num-1].flags & I2C_M_RD)
1124 if (i2c_transfer(adapter, msg, num) < 0)
1127 /* Check PEC if last message is a read */
1128 if (i && (msg[num-1].flags & I2C_M_RD)) {
1129 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1133 if (read_write == I2C_SMBUS_READ)
1135 case I2C_SMBUS_BYTE:
1136 data->byte = msgbuf0[0];
1138 case I2C_SMBUS_BYTE_DATA:
1139 data->byte = msgbuf1[0];
1141 case I2C_SMBUS_WORD_DATA:
1142 case I2C_SMBUS_PROC_CALL:
1143 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1145 case I2C_SMBUS_I2C_BLOCK_DATA:
1146 /* fixed at 32 for now */
1147 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1148 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1149 data->block[i+1] = msgbuf1[i];
1156 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1157 char read_write, u8 command, int size,
1158 union i2c_smbus_data * data)
1162 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1164 if (adapter->algo->smbus_xfer) {
1165 mutex_lock(&adapter->bus_lock);
1166 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1168 mutex_unlock(&adapter->bus_lock);
1170 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1177 /* Next four are needed by i2c-isa */
1178 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1179 EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1180 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1181 EXPORT_SYMBOL_GPL(i2c_bus_type);
1183 EXPORT_SYMBOL(i2c_add_adapter);
1184 EXPORT_SYMBOL(i2c_del_adapter);
1185 EXPORT_SYMBOL(i2c_del_driver);
1186 EXPORT_SYMBOL(i2c_attach_client);
1187 EXPORT_SYMBOL(i2c_detach_client);
1188 EXPORT_SYMBOL(i2c_use_client);
1189 EXPORT_SYMBOL(i2c_release_client);
1190 EXPORT_SYMBOL(i2c_clients_command);
1191 EXPORT_SYMBOL(i2c_check_addr);
1193 EXPORT_SYMBOL(i2c_master_send);
1194 EXPORT_SYMBOL(i2c_master_recv);
1195 EXPORT_SYMBOL(i2c_control);
1196 EXPORT_SYMBOL(i2c_transfer);
1197 EXPORT_SYMBOL(i2c_get_adapter);
1198 EXPORT_SYMBOL(i2c_put_adapter);
1199 EXPORT_SYMBOL(i2c_probe);
1201 EXPORT_SYMBOL(i2c_smbus_xfer);
1202 EXPORT_SYMBOL(i2c_smbus_write_quick);
1203 EXPORT_SYMBOL(i2c_smbus_read_byte);
1204 EXPORT_SYMBOL(i2c_smbus_write_byte);
1205 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1206 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1207 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1208 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1209 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1210 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1211 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1213 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1214 MODULE_DESCRIPTION("I2C-Bus main module");
1215 MODULE_LICENSE("GPL");