2 -------------------------------------------------------------------------
3 i2c-algo-ite.c i2c driver algorithms for ITE adapters
5 Hai-Pao Fan, MontaVista Software, Inc.
6 hpfan@mvista.com or source@mvista.com
8 Copyright 2000 MontaVista Software Inc.
10 ---------------------------------------------------------------------------
11 This file was highly leveraged from i2c-algo-pcf.c, which was created
12 by Simon G. Vogl and Hans Berglund:
15 Copyright (C) 1995-1997 Simon G. Vogl
16 1998-2000 Hans Berglund
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /* ------------------------------------------------------------------------- */
33 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
34 Frodo Looijaard <frodol@dds.nl> ,and also from Martin Bailey
35 <mbailey@littlefeet-inc.com> */
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/version.h>
42 #include <linux/init.h>
43 #include <asm/uaccess.h>
44 #include <linux/ioport.h>
45 #include <linux/errno.h>
46 #include <linux/sched.h>
48 #include <linux/i2c.h>
49 #include <linux/i2c-algo-ite.h>
52 #define PM_DSR IT8172_PCI_IO_BASE + IT_PM_DSR
53 #define PM_IBSR IT8172_PCI_IO_BASE + IT_PM_DSR + 0x04
54 #define GPIO_CCR IT8172_PCI_IO_BASE + IT_GPCCR
56 /* ----- global defines ----------------------------------------------- */
57 #define DEB(x) if (i2c_debug>=1) x
58 #define DEB2(x) if (i2c_debug>=2) x
59 #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
60 #define DEBPROTO(x) if (i2c_debug>=9) x;
61 /* debug the protocol by showing transferred bits */
62 #define DEF_TIMEOUT 16
64 /* debugging - slow down transfer to have a look at the data .. */
65 /* I use this with two leds&resistors, each one connected to sda,scl */
66 /* respectively. This makes sure that the algorithm works. Some chips */
67 /* might not like this, as they have an internal timeout of some mils */
69 #define SLO_IO jif=jiffies;while(jiffies<=jif+i2c_table[minor].veryslow)\
70 if (need_resched) schedule();
74 /* ----- global variables --------------------------------------------- */
82 static int i2c_debug=1;
83 static int iic_test=0; /* see if the line-setting functions work */
84 static int iic_scan=0; /* have a look at what's hanging 'round */
86 /* --- setting states on the bus with the right timing: --------------- */
88 #define get_clock(adap) adap->getclock(adap->data)
89 #define iic_outw(adap, reg, val) adap->setiic(adap->data, reg, val)
90 #define iic_inw(adap, reg) adap->getiic(adap->data, reg)
93 /* --- other auxiliary functions -------------------------------------- */
95 static void iic_start(struct i2c_algo_iic_data *adap)
97 iic_outw(adap,ITE_I2CHCR,ITE_CMD);
100 static void iic_stop(struct i2c_algo_iic_data *adap)
102 iic_outw(adap,ITE_I2CHCR,0);
103 iic_outw(adap,ITE_I2CHSR,ITE_I2CHSR_TDI);
106 static void iic_reset(struct i2c_algo_iic_data *adap)
108 iic_outw(adap, PM_IBSR, iic_inw(adap, PM_IBSR) | 0x80);
112 static int wait_for_bb(struct i2c_algo_iic_data *adap)
114 int timeout = DEF_TIMEOUT;
117 status = iic_inw(adap, ITE_I2CHSR);
119 while (timeout-- && (status & ITE_I2CHSR_HB)) {
120 udelay(1000); /* How much is this? */
121 status = iic_inw(adap, ITE_I2CHSR);
125 printk(KERN_ERR "Timeout, host is busy\n");
132 * Puts this process to sleep for a period equal to timeout
134 static inline void iic_sleep(unsigned long timeout)
136 schedule_timeout( timeout * HZ);
139 /* After we issue a transaction on the IIC bus, this function
140 * is called. It puts this process to sleep until we get an interrupt from
141 * from the controller telling us that the transaction we requested in complete.
143 static int wait_for_pin(struct i2c_algo_iic_data *adap, short *status) {
145 int timeout = DEF_TIMEOUT;
147 timeout = wait_for_bb(adap);
149 DEB2(printk("Timeout waiting for host not busy\n");)
152 timeout = DEF_TIMEOUT;
154 *status = iic_inw(adap, ITE_I2CHSR);
156 while (timeout-- && !(*status & ITE_I2CHSR_TDI)) {
158 *status = iic_inw(adap, ITE_I2CHSR);
167 static int wait_for_fe(struct i2c_algo_iic_data *adap, short *status)
169 int timeout = DEF_TIMEOUT;
171 *status = iic_inw(adap, ITE_I2CFSR);
173 while (timeout-- && (*status & ITE_I2CFSR_FE)) {
175 iic_inw(adap, ITE_I2CFSR);
184 static int iic_init (struct i2c_algo_iic_data *adap)
188 /* Clear bit 7 to set I2C to normal operation mode */
189 i=iic_inw(adap, PM_DSR)& 0xff7f;
190 iic_outw(adap, PM_DSR, i);
192 /* set IT_GPCCR port C bit 2&3 as function 2 */
193 i = iic_inw(adap, GPIO_CCR) & 0xfc0f;
194 iic_outw(adap,GPIO_CCR,i);
196 /* Clear slave address/sub-address */
197 iic_outw(adap,ITE_I2CSAR, 0);
198 iic_outw(adap,ITE_I2CSSAR, 0);
200 /* Set clock counter register */
201 iic_outw(adap,ITE_I2CCKCNT, get_clock(adap));
203 /* Set START/reSTART/STOP time registers */
204 iic_outw(adap,ITE_I2CSHDR, 0x0a);
205 iic_outw(adap,ITE_I2CRSUR, 0x0a);
206 iic_outw(adap,ITE_I2CPSUR, 0x0a);
208 /* Enable interrupts on completing the current transaction */
209 iic_outw(adap,ITE_I2CHCR, ITE_I2CHCR_IE | ITE_I2CHCR_HCE);
211 /* Clear transfer count */
212 iic_outw(adap,ITE_I2CFBCR, 0x0);
214 DEB2(printk("iic_init: Initialized IIC on ITE 0x%x\n",
215 iic_inw(adap, ITE_I2CHSR)));
221 * Sanity check for the adapter hardware - check the reaction of
222 * the bus lines only if it seems to be idle.
224 static int test_bus(struct i2c_algo_iic_data *adap, char *name) {
228 if (adap->getscl==NULL) {
229 printk("test_bus: Warning: Adapter can't read from clock line - skipping test.\n");
233 printk("test_bus: Adapter: %s scl: %d sda: %d -- testing...\n",
234 name,getscl(adap),getsda(adap));
236 printk("test_bus: %s seems to be busy.\n",adap->name);
240 printk("test_bus:1 scl: %d sda: %d \n",getscl(adap),
242 if ( 0 != getsda(adap) ) {
243 printk("test_bus: %s SDA stuck high!\n",name);
247 if ( 0 == getscl(adap) ) {
248 printk("test_bus: %s SCL unexpected low while pulling SDA low!\n",
253 printk("test_bus:2 scl: %d sda: %d \n",getscl(adap),
255 if ( 0 == getsda(adap) ) {
256 printk("test_bus: %s SDA stuck low!\n",name);
260 if ( 0 == getscl(adap) ) {
261 printk("test_bus: %s SCL unexpected low while SDA high!\n",
266 printk("test_bus:3 scl: %d sda: %d \n",getscl(adap),
268 if ( 0 != getscl(adap) ) {
273 if ( 0 == getsda(adap) ) {
274 printk("test_bus: %s SDA unexpected low while pulling SCL low!\n",
279 printk("test_bus:4 scl: %d sda: %d \n",getscl(adap),
281 if ( 0 == getscl(adap) ) {
282 printk("test_bus: %s SCL stuck low!\n",name);
286 if ( 0 == getsda(adap) ) {
287 printk("test_bus: %s SDA unexpected low while SCL high!\n",
291 printk("test_bus: %s passed test.\n",name);
301 /* ----- Utility functions
305 /* Verify the device we want to talk to on the IIC bus really exists. */
306 static inline int try_address(struct i2c_algo_iic_data *adap,
307 unsigned int addr, int retries)
312 for (i=0;i<retries;i++) {
313 iic_outw(adap, ITE_I2CSAR, addr);
315 if (wait_for_pin(adap, &status) == 0) {
316 if ((status & ITE_I2CHSR_DNE) == 0) {
318 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
320 break; /* success! */
324 udelay(adap->udelay);
326 DEB2(if (i) printk("try_address: needed %d retries for 0x%x\n",i,
332 static int iic_sendbytes(struct i2c_adapter *i2c_adap,const char *buf,
335 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
336 int wrcount=0, timeout;
338 int loops, remainder, i, j;
344 iic_outw(adap, ITE_I2CSSAR, (unsigned short)buf[wrcount++]);
349 loops = count / 32; /* 32-byte FIFO */
350 remainder = count % 32;
353 for(i=0; i<loops; i++) {
355 iic_outw(adap, ITE_I2CFBCR, 32);
356 for(j=0; j<32/2; j++) {
357 tmp.byte[1] = buf[wrcount++];
358 tmp.byte[0] = buf[wrcount++];
359 iic_outw(adap, ITE_I2CFDR, tmp.word);
362 /* status FIFO overrun */
363 iic_inw(adap, ITE_I2CFSR);
364 iic_inw(adap, ITE_I2CFBCR);
366 iic_outw(adap, ITE_I2CHCR, ITE_WRITE); /* Issue WRITE command */
368 /* Wait for transmission to complete */
369 timeout = wait_for_pin(adap, &status);
372 printk("iic_sendbytes: %s write timeout.\n", i2c_adap->name);
373 return -EREMOTEIO; /* got a better one ?? */
375 if (status & ITE_I2CHSR_DB) {
377 printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap->name);
378 return -EREMOTEIO; /* got a better one ?? */
383 iic_outw(adap, ITE_I2CFBCR, remainder);
384 for(i=0; i<remainder/2; i++) {
385 tmp.byte[1] = buf[wrcount++];
386 tmp.byte[0] = buf[wrcount++];
387 iic_outw(adap, ITE_I2CFDR, tmp.word);
390 /* status FIFO overrun */
391 iic_inw(adap, ITE_I2CFSR);
392 iic_inw(adap, ITE_I2CFBCR);
394 iic_outw(adap, ITE_I2CHCR, ITE_WRITE); /* Issue WRITE command */
396 timeout = wait_for_pin(adap, &status);
399 printk("iic_sendbytes: %s write timeout.\n", i2c_adap->name);
400 return -EREMOTEIO; /* got a better one ?? */
403 if (status & ITE_I2CHSR_DB) {
405 printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap->name);
406 return -EREMOTEIO; /* got a better one ?? */
415 static int iic_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count,
418 int rdcount=0, i, timeout;
420 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
421 int loops, remainder, j;
427 loops = count / 32; /* 32-byte FIFO */
428 remainder = count % 32;
431 for(i=0; i<loops; i++) {
432 iic_outw(adap, ITE_I2CFBCR, 32);
434 iic_outw(adap, ITE_I2CHCR, ITE_SREAD);
436 iic_outw(adap, ITE_I2CHCR, ITE_READ); /* Issue READ command */
438 timeout = wait_for_pin(adap, &status);
441 printk("iic_readbytes: %s read timeout.\n", i2c_adap->name);
445 if (status & ITE_I2CHSR_DB) {
447 printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);
452 timeout = wait_for_fe(adap, &status);
455 printk("iic_readbytes: %s FIFO is empty\n", i2c_adap->name);
459 for(j=0; j<32/2; j++) {
460 tmp.word = iic_inw(adap, ITE_I2CFDR);
461 buf[rdcount++] = tmp.byte[1];
462 buf[rdcount++] = tmp.byte[0];
465 /* status FIFO underrun */
466 iic_inw(adap, ITE_I2CFSR);
473 remainder=(remainder+1)/2 * 2;
474 iic_outw(adap, ITE_I2CFBCR, remainder);
476 iic_outw(adap, ITE_I2CHCR, ITE_SREAD);
478 iic_outw(adap, ITE_I2CHCR, ITE_READ); /* Issue READ command */
480 timeout = wait_for_pin(adap, &status);
483 printk("iic_readbytes: %s read timeout.\n", i2c_adap->name);
487 if (status & ITE_I2CHSR_DB) {
489 printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);
493 timeout = wait_for_fe(adap, &status);
496 printk("iic_readbytes: %s FIFO is empty\n", i2c_adap->name);
500 for(i=0; i<(remainder+1)/2; i++) {
501 tmp.word = iic_inw(adap, ITE_I2CFDR);
502 buf[rdcount++] = tmp.byte[1];
503 buf[rdcount++] = tmp.byte[0];
506 /* status FIFO underrun */
507 iic_inw(adap, ITE_I2CFSR);
516 /* This function implements combined transactions. Combined
517 * transactions consist of combinations of reading and writing blocks of data.
518 * Each transfer (i.e. a read or a write) is separated by a repeated start
522 static int iic_combined_transaction(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
525 struct i2c_msg *pmsg;
528 DEB2(printk("Beginning combined transaction\n"));
530 for(i=0; i<(num-1); i++) {
532 if(pmsg->flags & I2C_M_RD) {
533 DEB2(printk(" This one is a read\n"));
534 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);
536 else if(!(pmsg->flags & I2C_M_RD)) {
537 DEB2(printk("This one is a write\n"));
538 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);
541 /* Last read or write segment needs to be terminated with a stop */
544 if(pmsg->flags & I2C_M_RD) {
545 DEB2(printk("Doing the last read\n"));
546 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
548 else if(!(pmsg->flags & I2C_M_RD)) {
549 DEB2(printk("Doing the last write\n"));
550 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
558 /* Whenever we initiate a transaction, the first byte clocked
559 * onto the bus after the start condition is the address (7 bit) of the
560 * device we want to talk to. This function manipulates the address specified
561 * so that it makes sense to the hardware when written to the IIC peripheral.
563 * Note: 10 bit addresses are not supported in this driver, although they are
564 * supported by the hardware. This functionality needs to be implemented.
566 static inline int iic_doAddress(struct i2c_algo_iic_data *adap,
567 struct i2c_msg *msg, int retries)
569 unsigned short flags = msg->flags;
573 /* Ten bit addresses not supported right now */
574 if ( (flags & I2C_M_TEN) ) {
576 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
577 DEB2(printk("addr0: %d\n",addr));
578 ret = try_address(adap, addr, retries);
580 printk("iic_doAddress: died at extended address code.\n");
583 iic_outw(adap,msg->addr & 0x7f);
585 printk("iic_doAddress: died at 2nd address code.\n");
588 if ( flags & I2C_M_RD ) {
591 ret = try_address(adap, addr, retries);
593 printk("iic_doAddress: died at extended address code.\n");
600 addr = ( msg->addr << 1 );
603 if (flags & I2C_M_RD )
605 if (flags & I2C_M_REV_DIR_ADDR )
609 if (iic_inw(adap, ITE_I2CSAR) != addr) {
610 iic_outw(adap, ITE_I2CSAR, addr);
611 ret = try_address(adap, addr, retries);
613 printk("iic_doAddress: died at address code.\n");
624 /* Description: Prepares the controller for a transaction (clearing status
625 * registers, data buffers, etc), and then calls either iic_readbytes or
626 * iic_sendbytes to do the actual transaction.
628 * still to be done: Before we issue a transaction, we should
629 * verify that the bus is not busy or in some unknown state.
631 static int iic_xfer(struct i2c_adapter *i2c_adap,
632 struct i2c_msg msgs[],
635 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
636 struct i2c_msg *pmsg;
643 DEB2(printk("iic_xfer: read/write length is 0\n");)
646 if(!(pmsg->flags & I2C_M_RD) && (!(pmsg->len)%2) ) {
647 DEB2(printk("iic_xfer: write buffer length is not odd\n");)
651 /* Wait for any pending transfers to complete */
652 timeout = wait_for_bb(adap);
654 DEB2(printk("iic_xfer: Timeout waiting for host not busy\n");)
659 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
662 ret = iic_doAddress(adap, pmsg, i2c_adap->retries);
667 /* Combined transaction (read and write) */
669 DEB2(printk("iic_xfer: Call combined transaction\n"));
670 ret = iic_combined_transaction(i2c_adap, msgs, num);
674 DEB3(printk("iic_xfer: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
675 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
677 if(pmsg->flags & I2C_M_RD) /* Read */
678 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, 0);
681 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len);
684 if (ret != pmsg->len)
685 DEB3(printk("iic_xfer: error or fail on read/write %d bytes.\n",ret));
687 DEB3(printk("iic_xfer: read/write %d bytes.\n",ret));
693 /* Implements device specific ioctls. Higher level ioctls can
694 * be found in i2c-core.c and are typical of any i2c controller (specifying
695 * slave address, timeouts, etc). These ioctls take advantage of any hardware
696 * features built into the controller for which this algorithm-adapter set
697 * was written. These ioctls allow you to take control of the data and clock
698 * lines and set the either high or low,
699 * similar to a GPIO pin.
701 static int algo_control(struct i2c_adapter *adapter,
702 unsigned int cmd, unsigned long arg)
705 struct i2c_algo_iic_data *adap = adapter->algo_data;
706 struct i2c_iic_msg s_msg;
710 if (cmd == I2C_SREAD) {
711 if(copy_from_user(&s_msg, (struct i2c_iic_msg *)arg,
712 sizeof(struct i2c_iic_msg)))
714 buf = kmalloc(s_msg.len, GFP_KERNEL);
719 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
722 iic_outw(adap, ITE_I2CSAR,s_msg.addr<<1);
723 iic_outw(adap, ITE_I2CSSAR,s_msg.waddr & 0xff);
725 ret = iic_readbytes(adapter, buf, s_msg.len, 1);
727 if(copy_to_user( s_msg.buf, buf, s_msg.len) )
736 static u32 iic_func(struct i2c_adapter *adap)
738 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
739 I2C_FUNC_PROTOCOL_MANGLING;
742 /* -----exported algorithm data: ------------------------------------- */
744 static struct i2c_algorithm iic_algo = {
747 iic_xfer, /* master_xfer */
748 NULL, /* smbus_xfer */
749 NULL, /* slave_xmit */
750 NULL, /* slave_recv */
751 algo_control, /* ioctl */
752 iic_func, /* functionality */
757 * registering functions to load algorithms at runtime
759 int i2c_iic_add_bus(struct i2c_adapter *adap)
763 struct i2c_algo_iic_data *iic_adap = adap->algo_data;
766 int ret = test_bus(iic_adap, adap->name);
771 DEB2(printk("i2c-algo-ite: hw routines for %s registered.\n",
774 /* register new adapter to i2c module... */
776 adap->id |= iic_algo.id;
777 adap->algo = &iic_algo;
779 adap->timeout = 100; /* default values, should */
780 adap->retries = 3; /* be replaced by defines */
787 i2c_add_adapter(adap);
791 /* By default scanning the bus is turned off. */
793 printk(KERN_INFO " i2c-algo-ite: scanning bus %s.\n",
795 for (i = 0x00; i < 0xff; i+=2) {
796 iic_outw(iic_adap, ITE_I2CSAR, i);
798 if ( (wait_for_pin(iic_adap, &status) == 0) &&
799 ((status & ITE_I2CHSR_DNE) == 0) ) {
800 printk(KERN_INFO "\n(%02x)\n",i>>1);
802 printk(KERN_INFO ".");
805 udelay(iic_adap->udelay);
812 int i2c_iic_del_bus(struct i2c_adapter *adap)
815 if ((res = i2c_del_adapter(adap)) < 0)
817 DEB2(printk("i2c-algo-ite: adapter unregistered: %s\n",adap->name));
826 int __init i2c_algo_iic_init (void)
828 printk(KERN_INFO "ITE iic (i2c) algorithm module\n");
833 void i2c_algo_iic_exit(void)
839 EXPORT_SYMBOL(i2c_iic_add_bus);
840 EXPORT_SYMBOL(i2c_iic_del_bus);
842 /* The MODULE_* macros resolve to nothing if MODULES is not defined
843 * when this file is compiled.
845 MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
846 MODULE_DESCRIPTION("ITE iic algorithm");
847 MODULE_LICENSE("GPL");
849 MODULE_PARM(iic_test, "i");
850 MODULE_PARM(iic_scan, "i");
851 MODULE_PARM(i2c_debug,"i");
853 MODULE_PARM_DESC(iic_test, "Test if the I2C bus is available");
854 MODULE_PARM_DESC(iic_scan, "Scan for active chips on the bus");
855 MODULE_PARM_DESC(i2c_debug,
856 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 iic-protocol");
859 /* This function resolves to init_module (the function invoked when a module
860 * is loaded via insmod) when this file is compiled with MODULES defined.
861 * Otherwise (i.e. if you want this driver statically linked to the kernel),
862 * a pointer to this function is stored in a table and called
863 * during the intialization of the kernel (in do_basic_setup in /init/main.c)
865 * All this functionality is complements of the macros defined in linux/init.h
867 module_init(i2c_algo_iic_init);
870 /* If MODULES is defined when this file is compiled, then this function will
871 * resolved to cleanup_module.
873 module_exit(i2c_algo_iic_exit);