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 <asm/uaccess.h>
37 static LIST_HEAD(adapters);
38 static LIST_HEAD(drivers);
39 static DECLARE_MUTEX(core_lists);
40 static DEFINE_IDR(i2c_adapter_idr);
42 /* match always succeeds, as we want the probe() to tell if we really accept this match */
43 static int i2c_device_match(struct device *dev, struct device_driver *drv)
48 static int i2c_bus_suspend(struct device * dev, pm_message_t state)
52 if (dev->driver && dev->driver->suspend)
53 rc = dev->driver->suspend(dev, state);
57 static int i2c_bus_resume(struct device * dev)
61 if (dev->driver && dev->driver->resume)
62 rc = dev->driver->resume(dev);
66 struct bus_type i2c_bus_type = {
68 .match = i2c_device_match,
69 .suspend = i2c_bus_suspend,
70 .resume = i2c_bus_resume,
73 static int i2c_device_probe(struct device *dev)
78 static int i2c_device_remove(struct device *dev)
83 void i2c_adapter_dev_release(struct device *dev)
85 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
86 complete(&adap->dev_released);
89 struct device_driver i2c_adapter_driver = {
91 .name = "i2c_adapter",
93 .probe = i2c_device_probe,
94 .remove = i2c_device_remove,
97 static void i2c_adapter_class_dev_release(struct class_device *dev)
99 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
100 complete(&adap->class_dev_released);
103 struct class i2c_adapter_class = {
104 .owner = THIS_MODULE,
105 .name = "i2c-adapter",
106 .release = &i2c_adapter_class_dev_release,
109 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
111 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
112 return sprintf(buf, "%s\n", adap->name);
114 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
117 static void i2c_client_release(struct device *dev)
119 struct i2c_client *client = to_i2c_client(dev);
120 complete(&client->released);
123 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
125 struct i2c_client *client = to_i2c_client(dev);
126 return sprintf(buf, "%s\n", client->name);
130 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
131 * different type of a device. So beware if the DEVICE_ATTR() macro ever
132 * changes, this definition will also have to change.
134 static struct device_attribute dev_attr_client_name = {
135 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
136 .show = &show_client_name,
140 /* ---------------------------------------------------
141 * registering functions
142 * ---------------------------------------------------
146 * i2c_add_adapter is called from within the algorithm layer,
147 * when a new hw adapter registers. A new device is register to be
148 * available for clients.
150 int i2c_add_adapter(struct i2c_adapter *adap)
153 struct list_head *item;
154 struct i2c_driver *driver;
158 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
163 res = idr_get_new(&i2c_adapter_idr, adap, &id);
170 adap->nr = id & MAX_ID_MASK;
171 init_MUTEX(&adap->bus_lock);
172 init_MUTEX(&adap->clist_lock);
173 list_add_tail(&adap->list,&adapters);
174 INIT_LIST_HEAD(&adap->clients);
176 /* Add the adapter to the driver core.
177 * If the parent pointer is not set up,
178 * we add this adapter to the host bus.
180 if (adap->dev.parent == NULL)
181 adap->dev.parent = &platform_bus;
182 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
183 adap->dev.driver = &i2c_adapter_driver;
184 adap->dev.release = &i2c_adapter_dev_release;
185 device_register(&adap->dev);
186 device_create_file(&adap->dev, &dev_attr_name);
188 /* Add this adapter to the i2c_adapter class */
189 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
190 adap->class_dev.dev = &adap->dev;
191 adap->class_dev.class = &i2c_adapter_class;
192 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
193 class_device_register(&adap->class_dev);
195 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
197 /* inform drivers of new adapters */
198 list_for_each(item,&drivers) {
199 driver = list_entry(item, struct i2c_driver, list);
200 if (driver->attach_adapter)
201 /* We ignore the return code; if it fails, too bad */
202 driver->attach_adapter(adap);
211 int i2c_del_adapter(struct i2c_adapter *adap)
213 struct list_head *item, *_n;
214 struct i2c_adapter *adap_from_list;
215 struct i2c_driver *driver;
216 struct i2c_client *client;
221 /* First make sure that this adapter was ever added */
222 list_for_each_entry(adap_from_list, &adapters, list) {
223 if (adap_from_list == adap)
226 if (adap_from_list != adap) {
227 pr_debug("i2c-core: attempting to delete unregistered "
228 "adapter [%s]\n", adap->name);
233 list_for_each(item,&drivers) {
234 driver = list_entry(item, struct i2c_driver, list);
235 if (driver->detach_adapter)
236 if ((res = driver->detach_adapter(adap))) {
237 dev_err(&adap->dev, "detach_adapter failed "
238 "for driver [%s]\n", driver->name);
243 /* detach any active clients. This must be done first, because
244 * it can fail; in which case we give up. */
245 list_for_each_safe(item, _n, &adap->clients) {
246 client = list_entry(item, struct i2c_client, list);
248 /* detaching devices is unconditional of the set notify
249 * flag, as _all_ clients that reside on the adapter
250 * must be deleted, as this would cause invalid states.
252 if ((res=client->driver->detach_client(client))) {
253 dev_err(&adap->dev, "detach_client failed for client "
254 "[%s] at address 0x%02x\n", client->name,
260 /* clean up the sysfs representation */
261 init_completion(&adap->dev_released);
262 init_completion(&adap->class_dev_released);
263 class_device_unregister(&adap->class_dev);
264 device_remove_file(&adap->dev, &dev_attr_name);
265 device_unregister(&adap->dev);
266 list_del(&adap->list);
268 /* wait for sysfs to drop all references */
269 wait_for_completion(&adap->dev_released);
270 wait_for_completion(&adap->class_dev_released);
272 /* free dynamically allocated bus id */
273 idr_remove(&i2c_adapter_idr, adap->nr);
275 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
284 * What follows is the "upwards" interface: commands for talking to clients,
285 * which implement the functions to access the physical information of the
289 int i2c_add_driver(struct i2c_driver *driver)
291 struct list_head *item;
292 struct i2c_adapter *adapter;
297 /* add the driver to the list of i2c drivers in the driver core */
298 driver->driver.owner = driver->owner;
299 driver->driver.name = driver->name;
300 driver->driver.bus = &i2c_bus_type;
301 driver->driver.probe = i2c_device_probe;
302 driver->driver.remove = i2c_device_remove;
304 res = driver_register(&driver->driver);
308 list_add_tail(&driver->list,&drivers);
309 pr_debug("i2c-core: driver [%s] registered\n", driver->name);
311 /* now look for instances of driver on our adapters */
312 if (driver->attach_adapter) {
313 list_for_each(item,&adapters) {
314 adapter = list_entry(item, struct i2c_adapter, list);
315 driver->attach_adapter(adapter);
324 int i2c_del_driver(struct i2c_driver *driver)
326 struct list_head *item1, *item2, *_n;
327 struct i2c_client *client;
328 struct i2c_adapter *adap;
334 /* Have a look at each adapter, if clients of this driver are still
335 * attached. If so, detach them to be able to kill the driver
338 * Removing clients does not depend on the notify flag, else
339 * invalid operation might (will!) result, when using stale client
342 list_for_each(item1,&adapters) {
343 adap = list_entry(item1, struct i2c_adapter, list);
344 if (driver->detach_adapter) {
345 if ((res = driver->detach_adapter(adap))) {
346 dev_err(&adap->dev, "detach_adapter failed "
347 "for driver [%s]\n", driver->name);
351 list_for_each_safe(item2, _n, &adap->clients) {
352 client = list_entry(item2, struct i2c_client, list);
353 if (client->driver != driver)
355 dev_dbg(&adap->dev, "detaching client [%s] "
356 "at 0x%02x\n", client->name,
358 if ((res = driver->detach_client(client))) {
359 dev_err(&adap->dev, "detach_client "
360 "failed for client [%s] at "
361 "0x%02x\n", client->name,
369 driver_unregister(&driver->driver);
370 list_del(&driver->list);
371 pr_debug("i2c-core: driver [%s] unregistered\n", driver->name);
378 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
380 struct list_head *item;
381 struct i2c_client *client;
383 list_for_each(item,&adapter->clients) {
384 client = list_entry(item, struct i2c_client, list);
385 if (client->addr == addr)
391 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
395 down(&adapter->clist_lock);
396 rval = __i2c_check_addr(adapter, addr);
397 up(&adapter->clist_lock);
402 int i2c_attach_client(struct i2c_client *client)
404 struct i2c_adapter *adapter = client->adapter;
406 down(&adapter->clist_lock);
407 if (__i2c_check_addr(client->adapter, client->addr)) {
408 up(&adapter->clist_lock);
411 list_add_tail(&client->list,&adapter->clients);
412 up(&adapter->clist_lock);
414 if (adapter->client_register) {
415 if (adapter->client_register(client)) {
416 dev_dbg(&adapter->dev, "client_register "
417 "failed for client [%s] at 0x%02x\n",
418 client->name, client->addr);
422 client->usage_count = 0;
424 client->dev.parent = &client->adapter->dev;
425 client->dev.driver = &client->driver->driver;
426 client->dev.bus = &i2c_bus_type;
427 client->dev.release = &i2c_client_release;
429 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
430 "%d-%04x", i2c_adapter_id(adapter), client->addr);
431 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
432 client->name, client->dev.bus_id);
433 device_register(&client->dev);
434 device_create_file(&client->dev, &dev_attr_client_name);
440 int i2c_detach_client(struct i2c_client *client)
442 struct i2c_adapter *adapter = client->adapter;
445 if (client->usage_count > 0) {
446 dev_warn(&client->dev, "Client [%s] still busy, "
447 "can't detach\n", client->name);
451 if (adapter->client_unregister) {
452 res = adapter->client_unregister(client);
454 dev_err(&client->dev,
455 "client_unregister [%s] failed, "
456 "client not detached\n", client->name);
461 down(&adapter->clist_lock);
462 list_del(&client->list);
463 init_completion(&client->released);
464 device_remove_file(&client->dev, &dev_attr_client_name);
465 device_unregister(&client->dev);
466 up(&adapter->clist_lock);
467 wait_for_completion(&client->released);
473 static int i2c_inc_use_client(struct i2c_client *client)
476 if (!try_module_get(client->driver->owner))
478 if (!try_module_get(client->adapter->owner)) {
479 module_put(client->driver->owner);
486 static void i2c_dec_use_client(struct i2c_client *client)
488 module_put(client->driver->owner);
489 module_put(client->adapter->owner);
492 int i2c_use_client(struct i2c_client *client)
496 ret = i2c_inc_use_client(client);
500 if (client->usage_count > 0)
502 client->usage_count++;
506 i2c_dec_use_client(client);
510 int i2c_release_client(struct i2c_client *client)
512 if (!client->usage_count) {
513 pr_debug("i2c-core: %s used one too many times\n",
518 client->usage_count--;
519 i2c_dec_use_client(client);
524 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
526 struct list_head *item;
527 struct i2c_client *client;
529 down(&adap->clist_lock);
530 list_for_each(item,&adap->clients) {
531 client = list_entry(item, struct i2c_client, list);
532 if (!try_module_get(client->driver->owner))
534 if (NULL != client->driver->command) {
535 up(&adap->clist_lock);
536 client->driver->command(client,cmd,arg);
537 down(&adap->clist_lock);
539 module_put(client->driver->owner);
541 up(&adap->clist_lock);
544 static int __init i2c_init(void)
548 retval = bus_register(&i2c_bus_type);
551 retval = driver_register(&i2c_adapter_driver);
554 return class_register(&i2c_adapter_class);
557 static void __exit i2c_exit(void)
559 class_unregister(&i2c_adapter_class);
560 driver_unregister(&i2c_adapter_driver);
561 bus_unregister(&i2c_bus_type);
564 subsys_initcall(i2c_init);
565 module_exit(i2c_exit);
567 /* ----------------------------------------------------
568 * the functional interface to the i2c busses.
569 * ----------------------------------------------------
572 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
576 if (adap->algo->master_xfer) {
578 for (ret = 0; ret < num; ret++) {
579 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
580 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
581 'R' : 'W', msgs[ret].addr, msgs[ret].len);
585 down(&adap->bus_lock);
586 ret = adap->algo->master_xfer(adap,msgs,num);
591 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
596 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
599 struct i2c_adapter *adap=client->adapter;
602 msg.addr = client->addr;
603 msg.flags = client->flags & I2C_M_TEN;
605 msg.buf = (char *)buf;
607 ret = i2c_transfer(adap, &msg, 1);
609 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
610 transmitted, else error code. */
611 return (ret == 1) ? count : ret;
614 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
616 struct i2c_adapter *adap=client->adapter;
620 msg.addr = client->addr;
621 msg.flags = client->flags & I2C_M_TEN;
622 msg.flags |= I2C_M_RD;
626 ret = i2c_transfer(adap, &msg, 1);
628 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
629 transmitted, else error code. */
630 return (ret == 1) ? count : ret;
634 int i2c_control(struct i2c_client *client,
635 unsigned int cmd, unsigned long arg)
638 struct i2c_adapter *adap = client->adapter;
640 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
649 if (adap->algo->algo_control!=NULL)
650 ret = adap->algo->algo_control(adap,cmd,arg);
655 /* ----------------------------------------------------
656 * the i2c address scanning function
657 * Will not work for 10-bit addresses!
658 * ----------------------------------------------------
660 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
661 int (*found_proc) (struct i2c_adapter *, int, int))
665 /* Make sure the address is valid */
666 if (addr < 0x03 || addr > 0x77) {
667 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
672 /* Skip if already in use */
673 if (i2c_check_addr(adapter, addr))
676 /* Make sure there is something at this address, unless forced */
678 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
679 I2C_SMBUS_QUICK, NULL) < 0)
682 /* prevent 24RF08 corruption */
683 if ((addr & ~0x0f) == 0x50)
684 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
685 I2C_SMBUS_QUICK, NULL);
688 /* Finally call the custom detection function */
689 err = found_proc(adapter, addr, kind);
691 /* -ENODEV can be returned if there is a chip at the given address
692 but it isn't supported by this chip driver. We catch it here as
693 this isn't an error. */
694 return (err == -ENODEV) ? 0 : err;
697 int i2c_probe(struct i2c_adapter *adapter,
698 struct i2c_client_address_data *address_data,
699 int (*found_proc) (struct i2c_adapter *, int, int))
702 int adap_id = i2c_adapter_id(adapter);
704 /* Force entries are done first, and are not affected by ignore
706 if (address_data->forces) {
707 unsigned short **forces = address_data->forces;
710 for (kind = 0; forces[kind]; kind++) {
711 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
713 if (forces[kind][i] == adap_id
714 || forces[kind][i] == ANY_I2C_BUS) {
715 dev_dbg(&adapter->dev, "found force "
716 "parameter for adapter %d, "
717 "addr 0x%02x, kind %d\n",
718 adap_id, forces[kind][i + 1],
720 err = i2c_probe_address(adapter,
730 /* Stop here if we can't use SMBUS_QUICK */
731 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
732 if (address_data->probe[0] == I2C_CLIENT_END
733 && address_data->normal_i2c[0] == I2C_CLIENT_END)
736 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
737 "can't probe for chips\n");
741 /* Probe entries are done second, and are not affected by ignore
743 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
744 if (address_data->probe[i] == adap_id
745 || address_data->probe[i] == ANY_I2C_BUS) {
746 dev_dbg(&adapter->dev, "found probe parameter for "
747 "adapter %d, addr 0x%02x\n", adap_id,
748 address_data->probe[i + 1]);
749 err = i2c_probe_address(adapter,
750 address_data->probe[i + 1],
757 /* Normal entries are done last, unless shadowed by an ignore entry */
758 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
762 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
764 if ((address_data->ignore[j] == adap_id ||
765 address_data->ignore[j] == ANY_I2C_BUS)
766 && address_data->ignore[j + 1]
767 == address_data->normal_i2c[i]) {
768 dev_dbg(&adapter->dev, "found ignore "
769 "parameter for adapter %d, "
770 "addr 0x%02x\n", adap_id,
771 address_data->ignore[j + 1]);
779 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
780 "addr 0x%02x\n", adap_id,
781 address_data->normal_i2c[i]);
782 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
791 struct i2c_adapter* i2c_get_adapter(int id)
793 struct i2c_adapter *adapter;
796 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
797 if (adapter && !try_module_get(adapter->owner))
804 void i2c_put_adapter(struct i2c_adapter *adap)
806 module_put(adap->owner);
809 /* The SMBus parts */
811 #define POLY (0x1070U << 3)
817 for(i = 0; i < 8; i++) {
822 return (u8)(data >> 8);
825 /* Incremental CRC8 over count bytes in the array pointed to by p */
826 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
830 for(i = 0; i < count; i++)
831 crc = crc8((crc ^ p[i]) << 8);
835 /* Assume a 7-bit address, which is reasonable for SMBus */
836 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
838 /* The address will be sent first */
839 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
840 pec = i2c_smbus_pec(pec, &addr, 1);
842 /* The data buffer follows */
843 return i2c_smbus_pec(pec, msg->buf, msg->len);
846 /* Used for write only transactions */
847 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
849 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
853 /* Return <0 on CRC error
854 If there was a write before this read (most cases) we need to take the
855 partial CRC from the write part into account.
856 Note that this function does modify the message (we need to decrease the
857 message length to hide the CRC byte from the caller). */
858 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
860 u8 rpec = msg->buf[--msg->len];
861 cpec = i2c_smbus_msg_pec(cpec, msg);
864 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
871 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
873 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
874 value,0,I2C_SMBUS_QUICK,NULL);
877 s32 i2c_smbus_read_byte(struct i2c_client *client)
879 union i2c_smbus_data data;
880 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
881 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
884 return 0x0FF & data.byte;
887 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
889 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
890 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
893 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
895 union i2c_smbus_data data;
896 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
897 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
900 return 0x0FF & data.byte;
903 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
905 union i2c_smbus_data data;
907 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
908 I2C_SMBUS_WRITE,command,
909 I2C_SMBUS_BYTE_DATA,&data);
912 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
914 union i2c_smbus_data data;
915 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
916 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
919 return 0x0FFFF & data.word;
922 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
924 union i2c_smbus_data data;
926 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
927 I2C_SMBUS_WRITE,command,
928 I2C_SMBUS_WORD_DATA,&data);
931 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
932 u8 length, u8 *values)
934 union i2c_smbus_data data;
936 if (length > I2C_SMBUS_BLOCK_MAX)
937 length = I2C_SMBUS_BLOCK_MAX;
938 for (i = 1; i <= length; i++)
939 data.block[i] = values[i-1];
940 data.block[0] = length;
941 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
942 I2C_SMBUS_WRITE,command,
943 I2C_SMBUS_BLOCK_DATA,&data);
946 /* Returns the number of read bytes */
947 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
949 union i2c_smbus_data data;
951 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
952 I2C_SMBUS_READ,command,
953 I2C_SMBUS_I2C_BLOCK_DATA,&data))
956 for (i = 1; i <= data.block[0]; i++)
957 values[i-1] = data.block[i];
958 return data.block[0];
962 /* Simulate a SMBus command using the i2c protocol
963 No checking of parameters is done! */
964 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
965 unsigned short flags,
966 char read_write, u8 command, int size,
967 union i2c_smbus_data * data)
969 /* So we need to generate a series of msgs. In the case of writing, we
970 need to use only one message; when reading, we need two. We initialize
971 most things with sane defaults, to keep the code below somewhat
973 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
974 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
975 int num = read_write == I2C_SMBUS_READ?2:1;
976 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
977 { addr, flags | I2C_M_RD, 0, msgbuf1 }
982 msgbuf0[0] = command;
984 case I2C_SMBUS_QUICK:
986 /* Special case: The read/write field is used as data */
987 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
991 if (read_write == I2C_SMBUS_READ) {
992 /* Special case: only a read! */
993 msg[0].flags = I2C_M_RD | flags;
997 case I2C_SMBUS_BYTE_DATA:
998 if (read_write == I2C_SMBUS_READ)
1002 msgbuf0[1] = data->byte;
1005 case I2C_SMBUS_WORD_DATA:
1006 if (read_write == I2C_SMBUS_READ)
1010 msgbuf0[1] = data->word & 0xff;
1011 msgbuf0[2] = (data->word >> 8) & 0xff;
1014 case I2C_SMBUS_PROC_CALL:
1015 num = 2; /* Special case */
1016 read_write = I2C_SMBUS_READ;
1019 msgbuf0[1] = data->word & 0xff;
1020 msgbuf0[2] = (data->word >> 8) & 0xff;
1022 case I2C_SMBUS_BLOCK_DATA:
1023 if (read_write == I2C_SMBUS_READ) {
1024 dev_err(&adapter->dev, "Block read not supported "
1025 "under I2C emulation!\n");
1028 msg[0].len = data->block[0] + 2;
1029 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1030 dev_err(&adapter->dev, "smbus_access called with "
1031 "invalid block write size (%d)\n",
1035 for (i = 1; i < msg[0].len; i++)
1036 msgbuf0[i] = data->block[i-1];
1039 case I2C_SMBUS_BLOCK_PROC_CALL:
1040 dev_dbg(&adapter->dev, "Block process call not supported "
1041 "under I2C emulation!\n");
1043 case I2C_SMBUS_I2C_BLOCK_DATA:
1044 if (read_write == I2C_SMBUS_READ) {
1045 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1047 msg[0].len = data->block[0] + 1;
1048 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1049 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1050 "invalid block write size (%d)\n",
1054 for (i = 1; i <= data->block[0]; i++)
1055 msgbuf0[i] = data->block[i];
1059 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1064 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1065 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1067 /* Compute PEC if first message is a write */
1068 if (!(msg[0].flags & I2C_M_RD)) {
1069 if (num == 1) /* Write only */
1070 i2c_smbus_add_pec(&msg[0]);
1071 else /* Write followed by read */
1072 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1074 /* Ask for PEC if last message is a read */
1075 if (msg[num-1].flags & I2C_M_RD)
1079 if (i2c_transfer(adapter, msg, num) < 0)
1082 /* Check PEC if last message is a read */
1083 if (i && (msg[num-1].flags & I2C_M_RD)) {
1084 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1088 if (read_write == I2C_SMBUS_READ)
1090 case I2C_SMBUS_BYTE:
1091 data->byte = msgbuf0[0];
1093 case I2C_SMBUS_BYTE_DATA:
1094 data->byte = msgbuf1[0];
1096 case I2C_SMBUS_WORD_DATA:
1097 case I2C_SMBUS_PROC_CALL:
1098 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1100 case I2C_SMBUS_I2C_BLOCK_DATA:
1101 /* fixed at 32 for now */
1102 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1103 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1104 data->block[i+1] = msgbuf1[i];
1111 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1112 char read_write, u8 command, int size,
1113 union i2c_smbus_data * data)
1117 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1119 if (adapter->algo->smbus_xfer) {
1120 down(&adapter->bus_lock);
1121 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1123 up(&adapter->bus_lock);
1125 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1132 /* Next four are needed by i2c-isa */
1133 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1134 EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1135 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1136 EXPORT_SYMBOL_GPL(i2c_bus_type);
1138 EXPORT_SYMBOL(i2c_add_adapter);
1139 EXPORT_SYMBOL(i2c_del_adapter);
1140 EXPORT_SYMBOL(i2c_add_driver);
1141 EXPORT_SYMBOL(i2c_del_driver);
1142 EXPORT_SYMBOL(i2c_attach_client);
1143 EXPORT_SYMBOL(i2c_detach_client);
1144 EXPORT_SYMBOL(i2c_use_client);
1145 EXPORT_SYMBOL(i2c_release_client);
1146 EXPORT_SYMBOL(i2c_clients_command);
1147 EXPORT_SYMBOL(i2c_check_addr);
1149 EXPORT_SYMBOL(i2c_master_send);
1150 EXPORT_SYMBOL(i2c_master_recv);
1151 EXPORT_SYMBOL(i2c_control);
1152 EXPORT_SYMBOL(i2c_transfer);
1153 EXPORT_SYMBOL(i2c_get_adapter);
1154 EXPORT_SYMBOL(i2c_put_adapter);
1155 EXPORT_SYMBOL(i2c_probe);
1157 EXPORT_SYMBOL(i2c_smbus_xfer);
1158 EXPORT_SYMBOL(i2c_smbus_write_quick);
1159 EXPORT_SYMBOL(i2c_smbus_read_byte);
1160 EXPORT_SYMBOL(i2c_smbus_write_byte);
1161 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1162 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1163 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1164 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1165 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1166 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1168 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1169 MODULE_DESCRIPTION("I2C-Bus main module");
1170 MODULE_LICENSE("GPL");