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/init.h>
42 #include <asm/uaccess.h>
43 #include <linux/ioport.h>
44 #include <linux/errno.h>
45 #include <linux/sched.h>
47 #include <linux/i2c.h>
48 #include <linux/i2c-algo-ite.h>
51 #define PM_DSR IT8172_PCI_IO_BASE + IT_PM_DSR
52 #define PM_IBSR IT8172_PCI_IO_BASE + IT_PM_DSR + 0x04
53 #define GPIO_CCR IT8172_PCI_IO_BASE + IT_GPCCR
55 /* ----- global defines ----------------------------------------------- */
56 #define DEB(x) if (i2c_debug>=1) x
57 #define DEB2(x) if (i2c_debug>=2) x
58 #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
59 #define DEBPROTO(x) if (i2c_debug>=9) x;
60 /* debug the protocol by showing transferred bits */
61 #define DEF_TIMEOUT 16
63 /* debugging - slow down transfer to have a look at the data .. */
64 /* I use this with two leds&resistors, each one connected to sda,scl */
65 /* respectively. This makes sure that the algorithm works. Some chips */
66 /* might not like this, as they have an internal timeout of some mils */
68 #define SLO_IO jif=jiffies;while(jiffies<=jif+i2c_table[minor].veryslow)\
69 if (need_resched) schedule();
73 /* ----- global variables --------------------------------------------- */
81 static int i2c_debug=1;
82 static int iic_test=0; /* see if the line-setting functions work */
83 static int iic_scan=0; /* have a look at what's hanging 'round */
85 /* --- setting states on the bus with the right timing: --------------- */
87 #define get_clock(adap) adap->getclock(adap->data)
88 #define iic_outw(adap, reg, val) adap->setiic(adap->data, reg, val)
89 #define iic_inw(adap, reg) adap->getiic(adap->data, reg)
92 /* --- other auxiliary functions -------------------------------------- */
94 static void iic_start(struct i2c_algo_iic_data *adap)
96 iic_outw(adap,ITE_I2CHCR,ITE_CMD);
99 static void iic_stop(struct i2c_algo_iic_data *adap)
101 iic_outw(adap,ITE_I2CHCR,0);
102 iic_outw(adap,ITE_I2CHSR,ITE_I2CHSR_TDI);
105 static void iic_reset(struct i2c_algo_iic_data *adap)
107 iic_outw(adap, PM_IBSR, iic_inw(adap, PM_IBSR) | 0x80);
111 static int wait_for_bb(struct i2c_algo_iic_data *adap)
113 int timeout = DEF_TIMEOUT;
116 status = iic_inw(adap, ITE_I2CHSR);
118 while (timeout-- && (status & ITE_I2CHSR_HB)) {
119 udelay(1000); /* How much is this? */
120 status = iic_inw(adap, ITE_I2CHSR);
124 printk(KERN_ERR "Timeout, host is busy\n");
131 * Puts this process to sleep for a period equal to timeout
133 static inline void iic_sleep(unsigned long timeout)
135 schedule_timeout( timeout * HZ);
138 /* After we issue a transaction on the IIC bus, this function
139 * is called. It puts this process to sleep until we get an interrupt from
140 * from the controller telling us that the transaction we requested in complete.
142 static int wait_for_pin(struct i2c_algo_iic_data *adap, short *status) {
144 int timeout = DEF_TIMEOUT;
146 timeout = wait_for_bb(adap);
148 DEB2(printk("Timeout waiting for host not busy\n");)
151 timeout = DEF_TIMEOUT;
153 *status = iic_inw(adap, ITE_I2CHSR);
155 while (timeout-- && !(*status & ITE_I2CHSR_TDI)) {
157 *status = iic_inw(adap, ITE_I2CHSR);
166 static int wait_for_fe(struct i2c_algo_iic_data *adap, short *status)
168 int timeout = DEF_TIMEOUT;
170 *status = iic_inw(adap, ITE_I2CFSR);
172 while (timeout-- && (*status & ITE_I2CFSR_FE)) {
174 iic_inw(adap, ITE_I2CFSR);
183 static int iic_init (struct i2c_algo_iic_data *adap)
187 /* Clear bit 7 to set I2C to normal operation mode */
188 i=iic_inw(adap, PM_DSR)& 0xff7f;
189 iic_outw(adap, PM_DSR, i);
191 /* set IT_GPCCR port C bit 2&3 as function 2 */
192 i = iic_inw(adap, GPIO_CCR) & 0xfc0f;
193 iic_outw(adap,GPIO_CCR,i);
195 /* Clear slave address/sub-address */
196 iic_outw(adap,ITE_I2CSAR, 0);
197 iic_outw(adap,ITE_I2CSSAR, 0);
199 /* Set clock counter register */
200 iic_outw(adap,ITE_I2CCKCNT, get_clock(adap));
202 /* Set START/reSTART/STOP time registers */
203 iic_outw(adap,ITE_I2CSHDR, 0x0a);
204 iic_outw(adap,ITE_I2CRSUR, 0x0a);
205 iic_outw(adap,ITE_I2CPSUR, 0x0a);
207 /* Enable interrupts on completing the current transaction */
208 iic_outw(adap,ITE_I2CHCR, ITE_I2CHCR_IE | ITE_I2CHCR_HCE);
210 /* Clear transfer count */
211 iic_outw(adap,ITE_I2CFBCR, 0x0);
213 DEB2(printk("iic_init: Initialized IIC on ITE 0x%x\n",
214 iic_inw(adap, ITE_I2CHSR)));
220 * Sanity check for the adapter hardware - check the reaction of
221 * the bus lines only if it seems to be idle.
223 static int test_bus(struct i2c_algo_iic_data *adap, char *name) {
227 if (adap->getscl==NULL) {
228 printk("test_bus: Warning: Adapter can't read from clock line - skipping test.\n");
232 printk("test_bus: Adapter: %s scl: %d sda: %d -- testing...\n",
233 name,getscl(adap),getsda(adap));
235 printk("test_bus: %s seems to be busy.\n",adap->name);
239 printk("test_bus:1 scl: %d sda: %d \n",getscl(adap),
241 if ( 0 != getsda(adap) ) {
242 printk("test_bus: %s SDA stuck high!\n",name);
246 if ( 0 == getscl(adap) ) {
247 printk("test_bus: %s SCL unexpected low while pulling SDA low!\n",
252 printk("test_bus:2 scl: %d sda: %d \n",getscl(adap),
254 if ( 0 == getsda(adap) ) {
255 printk("test_bus: %s SDA stuck low!\n",name);
259 if ( 0 == getscl(adap) ) {
260 printk("test_bus: %s SCL unexpected low while SDA high!\n",
265 printk("test_bus:3 scl: %d sda: %d \n",getscl(adap),
267 if ( 0 != getscl(adap) ) {
272 if ( 0 == getsda(adap) ) {
273 printk("test_bus: %s SDA unexpected low while pulling SCL low!\n",
278 printk("test_bus:4 scl: %d sda: %d \n",getscl(adap),
280 if ( 0 == getscl(adap) ) {
281 printk("test_bus: %s SCL stuck low!\n",name);
285 if ( 0 == getsda(adap) ) {
286 printk("test_bus: %s SDA unexpected low while SCL high!\n",
290 printk("test_bus: %s passed test.\n",name);
300 /* ----- Utility functions
304 /* Verify the device we want to talk to on the IIC bus really exists. */
305 static inline int try_address(struct i2c_algo_iic_data *adap,
306 unsigned int addr, int retries)
311 for (i=0;i<retries;i++) {
312 iic_outw(adap, ITE_I2CSAR, addr);
314 if (wait_for_pin(adap, &status) == 0) {
315 if ((status & ITE_I2CHSR_DNE) == 0) {
317 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
319 break; /* success! */
323 udelay(adap->udelay);
325 DEB2(if (i) printk("try_address: needed %d retries for 0x%x\n",i,
331 static int iic_sendbytes(struct i2c_adapter *i2c_adap,const char *buf,
334 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
335 int wrcount=0, timeout;
337 int loops, remainder, i, j;
343 iic_outw(adap, ITE_I2CSSAR, (unsigned short)buf[wrcount++]);
348 loops = count / 32; /* 32-byte FIFO */
349 remainder = count % 32;
352 for(i=0; i<loops; i++) {
354 iic_outw(adap, ITE_I2CFBCR, 32);
355 for(j=0; j<32/2; j++) {
356 tmp.byte[1] = buf[wrcount++];
357 tmp.byte[0] = buf[wrcount++];
358 iic_outw(adap, ITE_I2CFDR, tmp.word);
361 /* status FIFO overrun */
362 iic_inw(adap, ITE_I2CFSR);
363 iic_inw(adap, ITE_I2CFBCR);
365 iic_outw(adap, ITE_I2CHCR, ITE_WRITE); /* Issue WRITE command */
367 /* Wait for transmission to complete */
368 timeout = wait_for_pin(adap, &status);
371 printk("iic_sendbytes: %s write timeout.\n", i2c_adap->name);
372 return -EREMOTEIO; /* got a better one ?? */
374 if (status & ITE_I2CHSR_DB) {
376 printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap->name);
377 return -EREMOTEIO; /* got a better one ?? */
382 iic_outw(adap, ITE_I2CFBCR, remainder);
383 for(i=0; i<remainder/2; i++) {
384 tmp.byte[1] = buf[wrcount++];
385 tmp.byte[0] = buf[wrcount++];
386 iic_outw(adap, ITE_I2CFDR, tmp.word);
389 /* status FIFO overrun */
390 iic_inw(adap, ITE_I2CFSR);
391 iic_inw(adap, ITE_I2CFBCR);
393 iic_outw(adap, ITE_I2CHCR, ITE_WRITE); /* Issue WRITE command */
395 timeout = wait_for_pin(adap, &status);
398 printk("iic_sendbytes: %s write timeout.\n", i2c_adap->name);
399 return -EREMOTEIO; /* got a better one ?? */
402 if (status & ITE_I2CHSR_DB) {
404 printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap->name);
405 return -EREMOTEIO; /* got a better one ?? */
414 static int iic_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count,
417 int rdcount=0, i, timeout;
419 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
420 int loops, remainder, j;
426 loops = count / 32; /* 32-byte FIFO */
427 remainder = count % 32;
430 for(i=0; i<loops; i++) {
431 iic_outw(adap, ITE_I2CFBCR, 32);
433 iic_outw(adap, ITE_I2CHCR, ITE_SREAD);
435 iic_outw(adap, ITE_I2CHCR, ITE_READ); /* Issue READ command */
437 timeout = wait_for_pin(adap, &status);
440 printk("iic_readbytes: %s read timeout.\n", i2c_adap->name);
444 if (status & ITE_I2CHSR_DB) {
446 printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);
451 timeout = wait_for_fe(adap, &status);
454 printk("iic_readbytes: %s FIFO is empty\n", i2c_adap->name);
458 for(j=0; j<32/2; j++) {
459 tmp.word = iic_inw(adap, ITE_I2CFDR);
460 buf[rdcount++] = tmp.byte[1];
461 buf[rdcount++] = tmp.byte[0];
464 /* status FIFO underrun */
465 iic_inw(adap, ITE_I2CFSR);
472 remainder=(remainder+1)/2 * 2;
473 iic_outw(adap, ITE_I2CFBCR, remainder);
475 iic_outw(adap, ITE_I2CHCR, ITE_SREAD);
477 iic_outw(adap, ITE_I2CHCR, ITE_READ); /* Issue READ command */
479 timeout = wait_for_pin(adap, &status);
482 printk("iic_readbytes: %s read timeout.\n", i2c_adap->name);
486 if (status & ITE_I2CHSR_DB) {
488 printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);
492 timeout = wait_for_fe(adap, &status);
495 printk("iic_readbytes: %s FIFO is empty\n", i2c_adap->name);
499 for(i=0; i<(remainder+1)/2; i++) {
500 tmp.word = iic_inw(adap, ITE_I2CFDR);
501 buf[rdcount++] = tmp.byte[1];
502 buf[rdcount++] = tmp.byte[0];
505 /* status FIFO underrun */
506 iic_inw(adap, ITE_I2CFSR);
515 /* This function implements combined transactions. Combined
516 * transactions consist of combinations of reading and writing blocks of data.
517 * Each transfer (i.e. a read or a write) is separated by a repeated start
521 static int iic_combined_transaction(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
524 struct i2c_msg *pmsg;
527 DEB2(printk("Beginning combined transaction\n"));
529 for(i=0; i<(num-1); i++) {
531 if(pmsg->flags & I2C_M_RD) {
532 DEB2(printk(" This one is a read\n"));
533 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);
535 else if(!(pmsg->flags & I2C_M_RD)) {
536 DEB2(printk("This one is a write\n"));
537 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);
540 /* Last read or write segment needs to be terminated with a stop */
543 if(pmsg->flags & I2C_M_RD) {
544 DEB2(printk("Doing the last read\n"));
545 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
547 else if(!(pmsg->flags & I2C_M_RD)) {
548 DEB2(printk("Doing the last write\n"));
549 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
557 /* Whenever we initiate a transaction, the first byte clocked
558 * onto the bus after the start condition is the address (7 bit) of the
559 * device we want to talk to. This function manipulates the address specified
560 * so that it makes sense to the hardware when written to the IIC peripheral.
562 * Note: 10 bit addresses are not supported in this driver, although they are
563 * supported by the hardware. This functionality needs to be implemented.
565 static inline int iic_doAddress(struct i2c_algo_iic_data *adap,
566 struct i2c_msg *msg, int retries)
568 unsigned short flags = msg->flags;
572 /* Ten bit addresses not supported right now */
573 if ( (flags & I2C_M_TEN) ) {
575 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
576 DEB2(printk("addr0: %d\n",addr));
577 ret = try_address(adap, addr, retries);
579 printk("iic_doAddress: died at extended address code.\n");
582 iic_outw(adap,msg->addr & 0x7f);
584 printk("iic_doAddress: died at 2nd address code.\n");
587 if ( flags & I2C_M_RD ) {
590 ret = try_address(adap, addr, retries);
592 printk("iic_doAddress: died at extended address code.\n");
599 addr = ( msg->addr << 1 );
602 if (flags & I2C_M_RD )
604 if (flags & I2C_M_REV_DIR_ADDR )
608 if (iic_inw(adap, ITE_I2CSAR) != addr) {
609 iic_outw(adap, ITE_I2CSAR, addr);
610 ret = try_address(adap, addr, retries);
612 printk("iic_doAddress: died at address code.\n");
623 /* Description: Prepares the controller for a transaction (clearing status
624 * registers, data buffers, etc), and then calls either iic_readbytes or
625 * iic_sendbytes to do the actual transaction.
627 * still to be done: Before we issue a transaction, we should
628 * verify that the bus is not busy or in some unknown state.
630 static int iic_xfer(struct i2c_adapter *i2c_adap,
631 struct i2c_msg msgs[],
634 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
635 struct i2c_msg *pmsg;
642 DEB2(printk("iic_xfer: read/write length is 0\n");)
645 if(!(pmsg->flags & I2C_M_RD) && (!(pmsg->len)%2) ) {
646 DEB2(printk("iic_xfer: write buffer length is not odd\n");)
650 /* Wait for any pending transfers to complete */
651 timeout = wait_for_bb(adap);
653 DEB2(printk("iic_xfer: Timeout waiting for host not busy\n");)
658 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
661 ret = iic_doAddress(adap, pmsg, i2c_adap->retries);
666 /* Combined transaction (read and write) */
668 DEB2(printk("iic_xfer: Call combined transaction\n"));
669 ret = iic_combined_transaction(i2c_adap, msgs, num);
673 DEB3(printk("iic_xfer: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
674 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
676 if(pmsg->flags & I2C_M_RD) /* Read */
677 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, 0);
680 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len);
683 if (ret != pmsg->len)
684 DEB3(printk("iic_xfer: error or fail on read/write %d bytes.\n",ret));
686 DEB3(printk("iic_xfer: read/write %d bytes.\n",ret));
692 /* Implements device specific ioctls. Higher level ioctls can
693 * be found in i2c-core.c and are typical of any i2c controller (specifying
694 * slave address, timeouts, etc). These ioctls take advantage of any hardware
695 * features built into the controller for which this algorithm-adapter set
696 * was written. These ioctls allow you to take control of the data and clock
697 * lines and set the either high or low,
698 * similar to a GPIO pin.
700 static int algo_control(struct i2c_adapter *adapter,
701 unsigned int cmd, unsigned long arg)
704 struct i2c_algo_iic_data *adap = adapter->algo_data;
705 struct i2c_iic_msg s_msg;
709 if (cmd == I2C_SREAD) {
710 if(copy_from_user(&s_msg, (struct i2c_iic_msg *)arg,
711 sizeof(struct i2c_iic_msg)))
713 buf = kmalloc(s_msg.len, GFP_KERNEL);
718 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
721 iic_outw(adap, ITE_I2CSAR,s_msg.addr<<1);
722 iic_outw(adap, ITE_I2CSSAR,s_msg.waddr & 0xff);
724 ret = iic_readbytes(adapter, buf, s_msg.len, 1);
726 if(copy_to_user( s_msg.buf, buf, s_msg.len) )
735 static u32 iic_func(struct i2c_adapter *adap)
737 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
738 I2C_FUNC_PROTOCOL_MANGLING;
741 /* -----exported algorithm data: ------------------------------------- */
743 static struct i2c_algorithm iic_algo = {
746 iic_xfer, /* master_xfer */
747 NULL, /* smbus_xfer */
748 NULL, /* slave_xmit */
749 NULL, /* slave_recv */
750 algo_control, /* ioctl */
751 iic_func, /* functionality */
756 * registering functions to load algorithms at runtime
758 int i2c_iic_add_bus(struct i2c_adapter *adap)
762 struct i2c_algo_iic_data *iic_adap = adap->algo_data;
765 int ret = test_bus(iic_adap, adap->name);
770 DEB2(printk("i2c-algo-ite: hw routines for %s registered.\n",
773 /* register new adapter to i2c module... */
775 adap->id |= iic_algo.id;
776 adap->algo = &iic_algo;
778 adap->timeout = 100; /* default values, should */
779 adap->retries = 3; /* be replaced by defines */
789 /* By default scanning the bus is turned off. */
791 printk(KERN_INFO " i2c-algo-ite: scanning bus %s.\n",
793 for (i = 0x00; i < 0xff; i+=2) {
794 iic_outw(iic_adap, ITE_I2CSAR, i);
796 if ( (wait_for_pin(iic_adap, &status) == 0) &&
797 ((status & ITE_I2CHSR_DNE) == 0) ) {
798 printk(KERN_INFO "\n(%02x)\n",i>>1);
800 printk(KERN_INFO ".");
803 udelay(iic_adap->udelay);
807 return i2c_add_adapter(adap);
811 int i2c_iic_del_bus(struct i2c_adapter *adap)
814 if ((res = i2c_del_adapter(adap)) < 0)
816 DEB2(printk("i2c-algo-ite: adapter unregistered: %s\n",adap->name));
825 int __init i2c_algo_iic_init (void)
827 printk(KERN_INFO "ITE iic (i2c) algorithm module\n");
832 void i2c_algo_iic_exit(void)
838 EXPORT_SYMBOL(i2c_iic_add_bus);
839 EXPORT_SYMBOL(i2c_iic_del_bus);
841 /* The MODULE_* macros resolve to nothing if MODULES is not defined
842 * when this file is compiled.
844 MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
845 MODULE_DESCRIPTION("ITE iic algorithm");
846 MODULE_LICENSE("GPL");
848 MODULE_PARM(iic_test, "i");
849 MODULE_PARM(iic_scan, "i");
850 MODULE_PARM(i2c_debug,"i");
852 MODULE_PARM_DESC(iic_test, "Test if the I2C bus is available");
853 MODULE_PARM_DESC(iic_scan, "Scan for active chips on the bus");
854 MODULE_PARM_DESC(i2c_debug,
855 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 iic-protocol");
858 /* This function resolves to init_module (the function invoked when a module
859 * is loaded via insmod) when this file is compiled with MODULES defined.
860 * Otherwise (i.e. if you want this driver statically linked to the kernel),
861 * a pointer to this function is stored in a table and called
862 * during the intialization of the kernel (in do_basic_setup in /init/main.c)
864 * All this functionality is complements of the macros defined in linux/init.h
866 module_init(i2c_algo_iic_init);
869 /* If MODULES is defined when this file is compiled, then this function will
870 * resolved to cleanup_module.
872 module_exit(i2c_algo_iic_exit);