vserver 1.9.5.x5
[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;
58 static int irq;
59 static int clock;
60 static int own;
61
62 static struct iic_ite gpi;
63 static wait_queue_head_t iic_wait;
64 static int iic_pending;
65 static spinlock_t lock;
66
67 /* ----- local functions ---------------------------------------------- */
68
69 static void iic_ite_setiic(void *data, int ctl, short val)
70 {
71         unsigned long j = jiffies + 10;
72
73         pr_debug(" Write 0x%02x to 0x%x\n",(unsigned short)val, ctl&0xff);
74 #ifdef DEBUG
75         while (time_before(jiffies, j))
76                 schedule();
77 #endif
78         outw(val,ctl);
79 }
80
81 static short iic_ite_getiic(void *data, int ctl)
82 {
83         short val;
84
85         val = inw(ctl);
86         pr_debug("Read 0x%02x from 0x%x\n",(unsigned short)val, ctl&0xff);
87         return (val);
88 }
89
90 /* Return our slave address.  This is the address
91  * put on the I2C bus when another master on the bus wants to address us
92  * as a slave
93  */
94 static int iic_ite_getown(void *data)
95 {
96         return (gpi.iic_own);
97 }
98
99
100 static int iic_ite_getclock(void *data)
101 {
102         return (gpi.iic_clock);
103 }
104
105
106 /* Put this process to sleep.  We will wake up when the
107  * IIC controller interrupts.
108  */
109 static void iic_ite_waitforpin(void) {
110
111    int timeout = 2;
112    long flags;
113
114    /* If interrupts are enabled (which they are), then put the process to
115     * sleep.  This process will be awakened by two events -- either the
116     * the IIC peripheral interrupts or the timeout expires. 
117     * If interrupts are not enabled then delay for a reasonable amount 
118     * of time and return.
119     */
120    if (gpi.iic_irq > 0) {
121         spin_lock_irqsave(&lock, flags);
122         if (iic_pending == 0) {
123                 spin_unlock_irqrestore(&lock, flags);
124                 if (interruptible_sleep_on_timeout(&iic_wait, timeout*HZ)) {
125                         spin_lock_irqsave(&lock, flags);
126                         if (iic_pending == 1) {
127                                 iic_pending = 0;
128                         }
129                         spin_unlock_irqrestore(&lock, flags);
130                 }
131         } else {
132                 iic_pending = 0;
133                 spin_unlock_irqrestore(&lock, flags);
134         }
135    } else {
136       udelay(100);
137    }
138 }
139
140
141 static irqreturn_t iic_ite_handler(int this_irq, void *dev_id,
142                                                         struct pt_regs *regs)
143 {
144         spin_lock(&lock);
145         iic_pending = 1;
146         spin_unlock(&lock);
147
148         wake_up_interruptible(&iic_wait);
149
150         return IRQ_HANDLED;
151 }
152
153
154 /* Lock the region of memory where I/O registers exist.  Request our
155  * interrupt line and register its associated handler.
156  */
157 static int iic_hw_resrc_init(void)
158 {
159         if (!request_region(gpi.iic_base, ITE_IIC_IO_SIZE, "i2c"))
160                 return -ENODEV;
161   
162         if (gpi.iic_irq <= 0)
163                 return 0;
164
165         if (request_irq(gpi.iic_irq, iic_ite_handler, 0, "ITE IIC", 0) < 0)
166                 gpi.iic_irq = 0;
167         else
168                 enable_irq(gpi.iic_irq);
169
170         return 0;
171 }
172
173
174 static void iic_ite_release(void)
175 {
176         if (gpi.iic_irq > 0) {
177                 disable_irq(gpi.iic_irq);
178                 free_irq(gpi.iic_irq, 0);
179         }
180         release_region(gpi.iic_base , 2);
181 }
182
183 /* ------------------------------------------------------------------------
184  * Encapsulate the above functions in the correct operations structure.
185  * This is only done when more than one hardware adapter is supported.
186  */
187 static struct i2c_algo_iic_data iic_ite_data = {
188         NULL,
189         iic_ite_setiic,
190         iic_ite_getiic,
191         iic_ite_getown,
192         iic_ite_getclock,
193         iic_ite_waitforpin,
194         80, 80, 100,            /*      waits, timeout */
195 };
196
197 static struct i2c_adapter iic_ite_ops = {
198         .owner          = THIS_MODULE,
199         .id             = I2C_HW_I_IIC,
200         .algo_data      = &iic_ite_data,
201         .dev            = {
202                 .name   = "ITE IIC adapter",
203         },
204 };
205
206 /* Called when the module is loaded.  This function starts the
207  * cascade of calls up through the hierarchy of i2c modules (i.e. up to the
208  *  algorithm layer and into to the core layer)
209  */
210 static int __init iic_ite_init(void) 
211 {
212
213         struct iic_ite *piic = &gpi;
214
215         printk(KERN_INFO "Initialize ITE IIC adapter module\n");
216         if (base == 0)
217                 piic->iic_base = DEFAULT_BASE;
218         else
219                 piic->iic_base = base;
220
221         if (irq == 0)
222                 piic->iic_irq = DEFAULT_IRQ;
223         else
224                 piic->iic_irq = irq;
225
226         if (clock == 0)
227                 piic->iic_clock = DEFAULT_CLOCK;
228         else
229                 piic->iic_clock = clock;
230
231         if (own == 0)
232                 piic->iic_own = DEFAULT_OWN;
233         else
234                 piic->iic_own = own;
235
236         iic_ite_data.data = (void *)piic;
237         init_waitqueue_head(&iic_wait);
238         spin_lock_init(&lock);
239         if (iic_hw_resrc_init() == 0) {
240                 if (i2c_iic_add_bus(&iic_ite_ops) < 0)
241                         return -ENODEV;
242         } else {
243                 return -ENODEV;
244         }
245         printk(KERN_INFO " found device at %#x irq %d.\n", 
246                 piic->iic_base, piic->iic_irq);
247         return 0;
248 }
249
250
251 static void iic_ite_exit(void)
252 {
253         i2c_iic_del_bus(&iic_ite_ops);
254         iic_ite_release();
255 }
256
257 /* If modules is NOT defined when this file is compiled, then the MODULE_*
258  * macros will resolve to nothing
259  */
260 MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
261 MODULE_DESCRIPTION("I2C-Bus adapter routines for ITE IIC bus adapter");
262 MODULE_LICENSE("GPL");
263
264 module_param(base, int, 0);
265 module_param(irq, int, 0);
266 module_param(clock, int, 0);
267 module_param(own, int, 0);
268
269
270 /* Called when module is loaded or when kernel is initialized.
271  * If MODULES is defined when this file is compiled, then this function will
272  * resolve to init_module (the function called when insmod is invoked for a
273  * module).  Otherwise, this function is called early in the boot, when the
274  * kernel is intialized.  Check out /include/init.h to see how this works.
275  */
276 module_init(iic_ite_init);
277
278 /* Resolves to module_cleanup when MODULES is defined. */
279 module_exit(iic_ite_exit);