Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

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