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>
49 #include "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 #define DEB2(x) if (i2c_debug>=2) x
56 #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
57 #define DEF_TIMEOUT 16
63 static int iic_test
; /* see if the line-setting functions work */
65 /* --- setting states on the bus with the right timing: --------------- */
67 #define get_clock(adap) adap->getclock(adap->data)
68 #define iic_outw(adap, reg, val) adap->setiic(adap->data, reg, val)
69 #define iic_inw(adap, reg) adap->getiic(adap->data, reg)
72 /* --- other auxiliary functions -------------------------------------- */
74 static void iic_start(struct i2c_algo_iic_data
*adap
)
76 iic_outw(adap
,ITE_I2CHCR
,ITE_CMD
);
79 static void iic_stop(struct i2c_algo_iic_data
*adap
)
81 iic_outw(adap
,ITE_I2CHCR
,0);
82 iic_outw(adap
,ITE_I2CHSR
,ITE_I2CHSR_TDI
);
85 static void iic_reset(struct i2c_algo_iic_data
*adap
)
87 iic_outw(adap
, PM_IBSR
, iic_inw(adap
, PM_IBSR
) | 0x80);
91 static int wait_for_bb(struct i2c_algo_iic_data
*adap
)
93 int timeout
= DEF_TIMEOUT
;
96 status
= iic_inw(adap
, ITE_I2CHSR
);
98 while (timeout
-- && (status
& ITE_I2CHSR_HB
)) {
99 udelay(1000); /* How much is this? */
100 status
= iic_inw(adap
, ITE_I2CHSR
);
104 printk(KERN_ERR
"Timeout, host is busy\n");
110 /* After we issue a transaction on the IIC bus, this function
111 * is called. It puts this process to sleep until we get an interrupt from
112 * from the controller telling us that the transaction we requested in complete.
114 static int wait_for_pin(struct i2c_algo_iic_data
*adap
, short *status
) {
116 int timeout
= DEF_TIMEOUT
;
118 timeout
= wait_for_bb(adap
);
120 DEB2(printk("Timeout waiting for host not busy\n");)
123 timeout
= DEF_TIMEOUT
;
125 *status
= iic_inw(adap
, ITE_I2CHSR
);
127 while (timeout
-- && !(*status
& ITE_I2CHSR_TDI
)) {
129 *status
= iic_inw(adap
, ITE_I2CHSR
);
138 static int wait_for_fe(struct i2c_algo_iic_data
*adap
, short *status
)
140 int timeout
= DEF_TIMEOUT
;
142 *status
= iic_inw(adap
, ITE_I2CFSR
);
144 while (timeout
-- && (*status
& ITE_I2CFSR_FE
)) {
146 iic_inw(adap
, ITE_I2CFSR
);
155 static int iic_init (struct i2c_algo_iic_data
*adap
)
159 /* Clear bit 7 to set I2C to normal operation mode */
160 i
=iic_inw(adap
, PM_DSR
)& 0xff7f;
161 iic_outw(adap
, PM_DSR
, i
);
163 /* set IT_GPCCR port C bit 2&3 as function 2 */
164 i
= iic_inw(adap
, GPIO_CCR
) & 0xfc0f;
165 iic_outw(adap
,GPIO_CCR
,i
);
167 /* Clear slave address/sub-address */
168 iic_outw(adap
,ITE_I2CSAR
, 0);
169 iic_outw(adap
,ITE_I2CSSAR
, 0);
171 /* Set clock counter register */
172 iic_outw(adap
,ITE_I2CCKCNT
, get_clock(adap
));
174 /* Set START/reSTART/STOP time registers */
175 iic_outw(adap
,ITE_I2CSHDR
, 0x0a);
176 iic_outw(adap
,ITE_I2CRSUR
, 0x0a);
177 iic_outw(adap
,ITE_I2CPSUR
, 0x0a);
179 /* Enable interrupts on completing the current transaction */
180 iic_outw(adap
,ITE_I2CHCR
, ITE_I2CHCR_IE
| ITE_I2CHCR_HCE
);
182 /* Clear transfer count */
183 iic_outw(adap
,ITE_I2CFBCR
, 0x0);
185 DEB2(printk("iic_init: Initialized IIC on ITE 0x%x\n",
186 iic_inw(adap
, ITE_I2CHSR
)));
192 * Sanity check for the adapter hardware - check the reaction of
193 * the bus lines only if it seems to be idle.
195 static int test_bus(struct i2c_algo_iic_data
*adap
, char *name
) {
199 if (adap
->getscl
==NULL
) {
200 printk("test_bus: Warning: Adapter can't read from clock line - skipping test.\n");
204 printk("test_bus: Adapter: %s scl: %d sda: %d -- testing...\n",
205 name
,getscl(adap
),getsda(adap
));
207 printk("test_bus: %s seems to be busy.\n",adap
->name
);
211 printk("test_bus:1 scl: %d sda: %d\n", getscl(adap
),
213 if ( 0 != getsda(adap
) ) {
214 printk("test_bus: %s SDA stuck high!\n",name
);
218 if ( 0 == getscl(adap
) ) {
219 printk("test_bus: %s SCL unexpected low while pulling SDA low!\n",
224 printk("test_bus:2 scl: %d sda: %d\n", getscl(adap
),
226 if ( 0 == getsda(adap
) ) {
227 printk("test_bus: %s SDA stuck low!\n",name
);
231 if ( 0 == getscl(adap
) ) {
232 printk("test_bus: %s SCL unexpected low while SDA high!\n",
237 printk("test_bus:3 scl: %d sda: %d\n", getscl(adap
),
239 if ( 0 != getscl(adap
) ) {
244 if ( 0 == getsda(adap
) ) {
245 printk("test_bus: %s SDA unexpected low while pulling SCL low!\n",
250 printk("test_bus:4 scl: %d sda: %d\n", getscl(adap
),
252 if ( 0 == getscl(adap
) ) {
253 printk("test_bus: %s SCL stuck low!\n",name
);
257 if ( 0 == getsda(adap
) ) {
258 printk("test_bus: %s SDA unexpected low while SCL high!\n",
262 printk("test_bus: %s passed test.\n",name
);
272 /* ----- Utility functions
276 /* Verify the device we want to talk to on the IIC bus really exists. */
277 static inline int try_address(struct i2c_algo_iic_data
*adap
,
278 unsigned int addr
, int retries
)
283 for (i
=0;i
<retries
;i
++) {
284 iic_outw(adap
, ITE_I2CSAR
, addr
);
286 if (wait_for_pin(adap
, &status
) == 0) {
287 if ((status
& ITE_I2CHSR_DNE
) == 0) {
289 iic_outw(adap
, ITE_I2CFCR
, ITE_I2CFCR_FLUSH
);
291 break; /* success! */
295 udelay(adap
->udelay
);
297 DEB2(if (i
) printk("try_address: needed %d retries for 0x%x\n",i
,
303 static int iic_sendbytes(struct i2c_adapter
*i2c_adap
,const char *buf
,
306 struct i2c_algo_iic_data
*adap
= i2c_adap
->algo_data
;
307 int wrcount
=0, timeout
;
309 int loops
, remainder
, i
, j
;
315 iic_outw(adap
, ITE_I2CSSAR
, (unsigned short)buf
[wrcount
++]);
320 loops
= count
/ 32; /* 32-byte FIFO */
321 remainder
= count
% 32;
324 for(i
=0; i
<loops
; i
++) {
326 iic_outw(adap
, ITE_I2CFBCR
, 32);
327 for(j
=0; j
<32/2; j
++) {
328 tmp
.byte
[1] = buf
[wrcount
++];
329 tmp
.byte
[0] = buf
[wrcount
++];
330 iic_outw(adap
, ITE_I2CFDR
, tmp
.word
);
333 /* status FIFO overrun */
334 iic_inw(adap
, ITE_I2CFSR
);
335 iic_inw(adap
, ITE_I2CFBCR
);
337 iic_outw(adap
, ITE_I2CHCR
, ITE_WRITE
); /* Issue WRITE command */
339 /* Wait for transmission to complete */
340 timeout
= wait_for_pin(adap
, &status
);
343 printk("iic_sendbytes: %s write timeout.\n", i2c_adap
->name
);
344 return -EREMOTEIO
; /* got a better one ?? */
346 if (status
& ITE_I2CHSR_DB
) {
348 printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap
->name
);
349 return -EREMOTEIO
; /* got a better one ?? */
354 iic_outw(adap
, ITE_I2CFBCR
, remainder
);
355 for(i
=0; i
<remainder
/2; i
++) {
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 timeout
= wait_for_pin(adap
, &status
);
370 printk("iic_sendbytes: %s write timeout.\n", i2c_adap
->name
);
371 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 ?? */
386 static int iic_readbytes(struct i2c_adapter
*i2c_adap
, char *buf
, int count
,
389 int rdcount
=0, i
, timeout
;
391 struct i2c_algo_iic_data
*adap
= i2c_adap
->algo_data
;
392 int loops
, remainder
, j
;
398 loops
= count
/ 32; /* 32-byte FIFO */
399 remainder
= count
% 32;
402 for(i
=0; i
<loops
; i
++) {
403 iic_outw(adap
, ITE_I2CFBCR
, 32);
405 iic_outw(adap
, ITE_I2CHCR
, ITE_SREAD
);
407 iic_outw(adap
, ITE_I2CHCR
, ITE_READ
); /* Issue READ command */
409 timeout
= wait_for_pin(adap
, &status
);
412 printk("iic_readbytes: %s read timeout.\n", i2c_adap
->name
);
416 if (status
& ITE_I2CHSR_DB
) {
418 printk("iic_readbytes: %s read error - no ack.\n", i2c_adap
->name
);
423 timeout
= wait_for_fe(adap
, &status
);
426 printk("iic_readbytes: %s FIFO is empty\n", i2c_adap
->name
);
430 for(j
=0; j
<32/2; j
++) {
431 tmp
.word
= iic_inw(adap
, ITE_I2CFDR
);
432 buf
[rdcount
++] = tmp
.byte
[1];
433 buf
[rdcount
++] = tmp
.byte
[0];
436 /* status FIFO underrun */
437 iic_inw(adap
, ITE_I2CFSR
);
444 remainder
=(remainder
+1)/2 * 2;
445 iic_outw(adap
, ITE_I2CFBCR
, remainder
);
447 iic_outw(adap
, ITE_I2CHCR
, ITE_SREAD
);
449 iic_outw(adap
, ITE_I2CHCR
, ITE_READ
); /* Issue READ command */
451 timeout
= wait_for_pin(adap
, &status
);
454 printk("iic_readbytes: %s read timeout.\n", i2c_adap
->name
);
458 if (status
& ITE_I2CHSR_DB
) {
460 printk("iic_readbytes: %s read error - no ack.\n", i2c_adap
->name
);
464 timeout
= wait_for_fe(adap
, &status
);
467 printk("iic_readbytes: %s FIFO is empty\n", i2c_adap
->name
);
471 for(i
=0; i
<(remainder
+1)/2; i
++) {
472 tmp
.word
= iic_inw(adap
, ITE_I2CFDR
);
473 buf
[rdcount
++] = tmp
.byte
[1];
474 buf
[rdcount
++] = tmp
.byte
[0];
477 /* status FIFO underrun */
478 iic_inw(adap
, ITE_I2CFSR
);
487 /* This function implements combined transactions. Combined
488 * transactions consist of combinations of reading and writing blocks of data.
489 * Each transfer (i.e. a read or a write) is separated by a repeated start
493 static int iic_combined_transaction(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
, int num
)
496 struct i2c_msg
*pmsg
;
499 DEB2(printk("Beginning combined transaction\n"));
501 for(i
=0; i
<(num
-1); i
++) {
503 if(pmsg
->flags
& I2C_M_RD
) {
504 DEB2(printk(" This one is a read\n"));
505 ret
= iic_readbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
, IIC_COMBINED_XFER
);
507 else if(!(pmsg
->flags
& I2C_M_RD
)) {
508 DEB2(printk("This one is a write\n"));
509 ret
= iic_sendbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
, IIC_COMBINED_XFER
);
512 /* Last read or write segment needs to be terminated with a stop */
515 if(pmsg
->flags
& I2C_M_RD
) {
516 DEB2(printk("Doing the last read\n"));
517 ret
= iic_readbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
, IIC_SINGLE_XFER
);
519 else if(!(pmsg
->flags
& I2C_M_RD
)) {
520 DEB2(printk("Doing the last write\n"));
521 ret
= iic_sendbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
, IIC_SINGLE_XFER
);
529 /* Whenever we initiate a transaction, the first byte clocked
530 * onto the bus after the start condition is the address (7 bit) of the
531 * device we want to talk to. This function manipulates the address specified
532 * so that it makes sense to the hardware when written to the IIC peripheral.
534 * Note: 10 bit addresses are not supported in this driver, although they are
535 * supported by the hardware. This functionality needs to be implemented.
537 static inline int iic_doAddress(struct i2c_algo_iic_data
*adap
,
538 struct i2c_msg
*msg
, int retries
)
540 unsigned short flags
= msg
->flags
;
544 /* Ten bit addresses not supported right now */
545 if ( (flags
& I2C_M_TEN
) ) {
547 addr
= 0xf0 | (( msg
->addr
>> 7) & 0x03);
548 DEB2(printk("addr0: %d\n",addr
));
549 ret
= try_address(adap
, addr
, retries
);
551 printk("iic_doAddress: died at extended address code.\n");
554 iic_outw(adap
,msg
->addr
& 0x7f);
556 printk("iic_doAddress: died at 2nd address code.\n");
559 if ( flags
& I2C_M_RD
) {
562 ret
= try_address(adap
, addr
, retries
);
564 printk("iic_doAddress: died at extended address code.\n");
571 addr
= ( msg
->addr
<< 1 );
574 if (flags
& I2C_M_RD
)
576 if (flags
& I2C_M_REV_DIR_ADDR
)
580 if (iic_inw(adap
, ITE_I2CSAR
) != addr
) {
581 iic_outw(adap
, ITE_I2CSAR
, addr
);
582 ret
= try_address(adap
, addr
, retries
);
584 printk("iic_doAddress: died at address code.\n");
595 /* Description: Prepares the controller for a transaction (clearing status
596 * registers, data buffers, etc), and then calls either iic_readbytes or
597 * iic_sendbytes to do the actual transaction.
599 * still to be done: Before we issue a transaction, we should
600 * verify that the bus is not busy or in some unknown state.
602 static int iic_xfer(struct i2c_adapter
*i2c_adap
,
603 struct i2c_msg
*msgs
,
606 struct i2c_algo_iic_data
*adap
= i2c_adap
->algo_data
;
607 struct i2c_msg
*pmsg
;
614 DEB2(printk("iic_xfer: read/write length is 0\n");)
617 if(!(pmsg
->flags
& I2C_M_RD
) && (!(pmsg
->len
)%2) ) {
618 DEB2(printk("iic_xfer: write buffer length is not odd\n");)
622 /* Wait for any pending transfers to complete */
623 timeout
= wait_for_bb(adap
);
625 DEB2(printk("iic_xfer: Timeout waiting for host not busy\n");)
630 iic_outw(adap
, ITE_I2CFCR
, ITE_I2CFCR_FLUSH
);
633 ret
= iic_doAddress(adap
, pmsg
, i2c_adap
->retries
);
638 /* Combined transaction (read and write) */
640 DEB2(printk("iic_xfer: Call combined transaction\n"));
641 ret
= iic_combined_transaction(i2c_adap
, msgs
, num
);
645 DEB3(printk("iic_xfer: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
646 i
, msgs
[i
].addr
, msgs
[i
].flags
, msgs
[i
].len
);)
648 if(pmsg
->flags
& I2C_M_RD
) /* Read */
649 ret
= iic_readbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
, 0);
652 ret
= iic_sendbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
);
655 if (ret
!= pmsg
->len
)
656 DEB3(printk("iic_xfer: error or fail on read/write %d bytes.\n",ret
));
658 DEB3(printk("iic_xfer: read/write %d bytes.\n",ret
));
664 /* Implements device specific ioctls. Higher level ioctls can
665 * be found in i2c-core.c and are typical of any i2c controller (specifying
666 * slave address, timeouts, etc). These ioctls take advantage of any hardware
667 * features built into the controller for which this algorithm-adapter set
668 * was written. These ioctls allow you to take control of the data and clock
669 * lines and set the either high or low,
670 * similar to a GPIO pin.
672 static int algo_control(struct i2c_adapter
*adapter
,
673 unsigned int cmd
, unsigned long arg
)
676 struct i2c_algo_iic_data
*adap
= adapter
->algo_data
;
677 struct i2c_iic_msg s_msg
;
681 if (cmd
== I2C_SREAD
) {
682 if(copy_from_user(&s_msg
, (struct i2c_iic_msg
*)arg
,
683 sizeof(struct i2c_iic_msg
)))
685 buf
= kmalloc(s_msg
.len
, GFP_KERNEL
);
690 iic_outw(adap
, ITE_I2CFCR
, ITE_I2CFCR_FLUSH
);
693 iic_outw(adap
, ITE_I2CSAR
,s_msg
.addr
<<1);
694 iic_outw(adap
, ITE_I2CSSAR
,s_msg
.waddr
& 0xff);
696 ret
= iic_readbytes(adapter
, buf
, s_msg
.len
, 1);
698 if(copy_to_user( s_msg
.buf
, buf
, s_msg
.len
) )
707 static u32
iic_func(struct i2c_adapter
*adap
)
709 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_10BIT_ADDR
|
710 I2C_FUNC_PROTOCOL_MANGLING
;
713 /* -----exported algorithm data: ------------------------------------- */
715 static struct i2c_algorithm iic_algo
= {
716 .master_xfer
= iic_xfer
,
717 .algo_control
= algo_control
, /* ioctl */
718 .functionality
= iic_func
,
723 * registering functions to load algorithms at runtime
725 int i2c_iic_add_bus(struct i2c_adapter
*adap
)
727 struct i2c_algo_iic_data
*iic_adap
= adap
->algo_data
;
730 int ret
= test_bus(iic_adap
, adap
->name
);
735 DEB2(printk("i2c-algo-ite: hw routines for %s registered.\n",
738 /* register new adapter to i2c module... */
739 adap
->algo
= &iic_algo
;
741 adap
->timeout
= 100; /* default values, should */
742 adap
->retries
= 3; /* be replaced by defines */
745 i2c_add_adapter(adap
);
752 int i2c_iic_del_bus(struct i2c_adapter
*adap
)
755 if ((res
= i2c_del_adapter(adap
)) < 0)
757 DEB2(printk("i2c-algo-ite: adapter unregistered: %s\n",adap
->name
));
763 int __init
i2c_algo_iic_init (void)
765 printk(KERN_INFO
"ITE iic (i2c) algorithm module\n");
770 void i2c_algo_iic_exit(void)
776 EXPORT_SYMBOL(i2c_iic_add_bus
);
777 EXPORT_SYMBOL(i2c_iic_del_bus
);
779 /* The MODULE_* macros resolve to nothing if MODULES is not defined
780 * when this file is compiled.
782 MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
783 MODULE_DESCRIPTION("ITE iic algorithm");
784 MODULE_LICENSE("GPL");
786 module_param(iic_test
, bool, 0);
787 module_param(i2c_debug
, int, S_IRUGO
| S_IWUSR
);
789 MODULE_PARM_DESC(iic_test
, "Test if the I2C bus is available");
790 MODULE_PARM_DESC(i2c_debug
,
791 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 iic-protocol");
794 /* This function resolves to init_module (the function invoked when a module
795 * is loaded via insmod) when this file is compiled with MODULES defined.
796 * Otherwise (i.e. if you want this driver statically linked to the kernel),
797 * a pointer to this function is stored in a table and called
798 * during the initialization of the kernel (in do_basic_setup in /init/main.c)
800 * All this functionality is complements of the macros defined in linux/init.h
802 module_init(i2c_algo_iic_init
);
805 /* If MODULES is defined when this file is compiled, then this function will
806 * resolved to cleanup_module.
808 module_exit(i2c_algo_iic_exit
);