ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / i2c / busses / i2c-ite.c
1 /*
2    -------------------------------------------------------------------------
3    i2c-adap-ite.c i2c-hw access for the IIC peripheral on the ITE MIPS system
4    -------------------------------------------------------------------------
5    Hai-Pao Fan, MontaVista Software, Inc.
6    hpfan@mvista.com or source@mvista.com
7
8    Copyright 2001 MontaVista Software Inc.
9
10    ----------------------------------------------------------------------------
11    This file was highly leveraged from i2c-elektor.c, which was created
12    by Simon G. Vogl and Hans Berglund:
13
14  
15      Copyright (C) 1995-97 Simon G. Vogl
16                    1998-99 Hans Berglund
17
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.
22
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.
27
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 /* ------------------------------------------------------------------------- */
32
33 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
34    Frodo Looijaard <frodol@dds.nl> */
35
36 #include <linux/config.h>
37 #include <linux/kernel.h>
38 #include <linux/ioport.h>
39 #include <linux/module.h>
40 #include <linux/delay.h>
41 #include <linux/slab.h>
42 #include <linux/init.h>
43 #include <asm/irq.h>
44 #include <asm/io.h>
45
46 #include <linux/i2c.h>
47 #include <linux/i2c-algo-ite.h>
48 #include <linux/i2c-adap-ite.h>
49 #include "../i2c-ite.h"
50
51 #define DEFAULT_BASE  0x14014030
52 #define ITE_IIC_IO_SIZE 0x40
53 #define DEFAULT_IRQ   0
54 #define DEFAULT_CLOCK 0x1b0e    /* default 16MHz/(27+14) = 400KHz */
55 #define DEFAULT_OWN   0x55
56
57 static int base  = 0;
58 static int irq   = 0;
59 static int clock = 0;
60 static int own   = 0;
61
62 static struct iic_ite gpi;
63 static wait_queue_head_t iic_wait;
64 static int iic_pending;
65
66 /* ----- local functions ---------------------------------------------- */
67
68 static void iic_ite_setiic(void *data, int ctl, short val)
69 {
70         unsigned long j = jiffies + 10;
71
72         pr_debug(" Write 0x%02x to 0x%x\n",(unsigned short)val, ctl&0xff);
73 #ifdef DEBUG
74         while (time_before(jiffies, j))
75                 schedule();
76 #endif
77         outw(val,ctl);
78 }
79
80 static short iic_ite_getiic(void *data, int ctl)
81 {
82         short val;
83
84         val = inw(ctl);
85         pr_debug("Read 0x%02x from 0x%x\n",(unsigned short)val, ctl&0xff);
86         return (val);
87 }
88
89 /* Return our slave address.  This is the address
90  * put on the I2C bus when another master on the bus wants to address us
91  * as a slave
92  */
93 static int iic_ite_getown(void *data)
94 {
95         return (gpi.iic_own);
96 }
97
98
99 static int iic_ite_getclock(void *data)
100 {
101         return (gpi.iic_clock);
102 }
103
104
105 #if 0
106 static void iic_ite_sleep(unsigned long timeout)
107 {
108         schedule_timeout( timeout * HZ);
109 }
110 #endif
111
112
113 /* Put this process to sleep.  We will wake up when the
114  * IIC controller interrupts.
115  */
116 static void iic_ite_waitforpin(void) {
117
118    int timeout = 2;
119
120    /* If interrupts are enabled (which they are), then put the process to
121     * sleep.  This process will be awakened by two events -- either the
122     * the IIC peripheral interrupts or the timeout expires. 
123     * If interrupts are not enabled then delay for a reasonable amount 
124     * of time and return.
125     */
126    if (gpi.iic_irq > 0) {
127         cli();
128         if (iic_pending == 0) {
129                 interruptible_sleep_on_timeout(&iic_wait, timeout*HZ );
130         } else
131                 iic_pending = 0;
132         sti();
133    } else {
134       udelay(100);
135    }
136 }
137
138
139 static void iic_ite_handler(int this_irq, void *dev_id, struct pt_regs *regs) 
140 {
141         
142    iic_pending = 1;
143
144    wake_up_interruptible(&iic_wait);
145 }
146
147
148 /* Lock the region of memory where I/O registers exist.  Request our
149  * interrupt line and register its associated handler.
150  */
151 static int iic_hw_resrc_init(void)
152 {
153         if (!request_region(gpi.iic_base, ITE_IIC_IO_SIZE, "i2c"))
154                 return -ENODEV;
155   
156         if (gpi.iic_irq <= 0)
157                 return 0;
158
159         if (request_irq(gpi.iic_irq, iic_ite_handler, 0, "ITE IIC", 0) < 0)
160                 gpi.iic_irq = 0;
161         else
162                 enable_irq(gpi.iic_irq);
163
164         return 0;
165 }
166
167
168 static void iic_ite_release(void)
169 {
170         if (gpi.iic_irq > 0) {
171                 disable_irq(gpi.iic_irq);
172                 free_irq(gpi.iic_irq, 0);
173         }
174         release_region(gpi.iic_base , 2);
175 }
176
177 /* ------------------------------------------------------------------------
178  * Encapsulate the above functions in the correct operations structure.
179  * This is only done when more than one hardware adapter is supported.
180  */
181 static struct i2c_algo_iic_data iic_ite_data = {
182         NULL,
183         iic_ite_setiic,
184         iic_ite_getiic,
185         iic_ite_getown,
186         iic_ite_getclock,
187         iic_ite_waitforpin,
188         80, 80, 100,            /*      waits, timeout */
189 };
190
191 static struct i2c_adapter iic_ite_ops = {
192         .owner          = THIS_MODULE,
193         .id             = I2C_HW_I_IIC,
194         .algo_data      = &iic_ite_data,
195         .dev            = {
196                 .name   = "ITE IIC adapter",
197         },
198 };
199
200 /* Called when the module is loaded.  This function starts the
201  * cascade of calls up through the hierarchy of i2c modules (i.e. up to the
202  *  algorithm layer and into to the core layer)
203  */
204 static int __init iic_ite_init(void) 
205 {
206
207         struct iic_ite *piic = &gpi;
208
209         printk(KERN_INFO "Initialize ITE IIC adapter module\n");
210         if (base == 0)
211                 piic->iic_base = DEFAULT_BASE;
212         else
213                 piic->iic_base = base;
214
215         if (irq == 0)
216                 piic->iic_irq = DEFAULT_IRQ;
217         else
218                 piic->iic_irq = irq;
219
220         if (clock == 0)
221                 piic->iic_clock = DEFAULT_CLOCK;
222         else
223                 piic->iic_clock = clock;
224
225         if (own == 0)
226                 piic->iic_own = DEFAULT_OWN;
227         else
228                 piic->iic_own = own;
229
230         iic_ite_data.data = (void *)piic;
231         init_waitqueue_head(&iic_wait);
232         if (iic_hw_resrc_init() == 0) {
233                 if (i2c_iic_add_bus(&iic_ite_ops) < 0)
234                         return -ENODEV;
235         } else {
236                 return -ENODEV;
237         }
238         printk(KERN_INFO " found device at %#x irq %d.\n", 
239                 piic->iic_base, piic->iic_irq);
240         return 0;
241 }
242
243
244 static void iic_ite_exit(void)
245 {
246         i2c_iic_del_bus(&iic_ite_ops);
247         iic_ite_release();
248 }
249
250 /* If modules is NOT defined when this file is compiled, then the MODULE_*
251  * macros will resolve to nothing
252  */
253 MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
254 MODULE_DESCRIPTION("I2C-Bus adapter routines for ITE IIC bus adapter");
255 MODULE_LICENSE("GPL");
256
257 MODULE_PARM(base, "i");
258 MODULE_PARM(irq, "i");
259 MODULE_PARM(clock, "i");
260 MODULE_PARM(own, "i");
261
262
263 /* Called when module is loaded or when kernel is initialized.
264  * If MODULES is defined when this file is compiled, then this function will
265  * resolve to init_module (the function called when insmod is invoked for a
266  * module).  Otherwise, this function is called early in the boot, when the
267  * kernel is intialized.  Check out /include/init.h to see how this works.
268  */
269 module_init(iic_ite_init);
270
271 /* Resolves to module_cleanup when MODULES is defined. */
272 module_exit(iic_ite_exit);