summaryrefslogtreecommitdiffstats
path: root/drivers/i2c/i2c-adap-ite.c
blob: c6a35dafcf32b18e43a8d8b7cdd0df4a33c69f56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
   -------------------------------------------------------------------------
   i2c-adap-ite.c i2c-hw access for the IIC peripheral on the ITE MIPS system
   -------------------------------------------------------------------------
   Hai-Pao Fan, MontaVista Software, Inc.
   hpfan@mvista.com or source@mvista.com

   Copyright 2001 MontaVista Software Inc.

   ----------------------------------------------------------------------------
   This file was highly leveraged from i2c-elektor.c, which was created
   by Simon G. Vogl and Hans Berglund:

 
     Copyright (C) 1995-97 Simon G. Vogl
                   1998-99 Hans Berglund

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.		     */
/* ------------------------------------------------------------------------- */

/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
   Frodo Looijaard <frodol@dds.nl> */

#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/malloc.h>
#include <linux/version.h>
#include <linux/init.h>
#include <asm/irq.h>
#include <asm/io.h>

#include <linux/i2c.h>
#include <linux/i2c-algo-ite.h>
#include <linux/i2c-adap-ite.h>
#include "i2c-ite.h"

#define DEFAULT_BASE  0x14014030
#define ITE_IIC_IO_SIZE	0x40
#define DEFAULT_IRQ   0
#define DEFAULT_CLOCK 0x1b0e	/* default 16MHz/(27+14) = 400KHz */
#define DEFAULT_OWN   0x55

static int base  = 0;
static int irq   = 0;
static int clock = 0;
static int own   = 0;

static int i2c_debug=0;
static struct iic_ite gpi;
#if (LINUX_VERSION_CODE < 0x020301)
static struct wait_queue *iic_wait = NULL;
#else
static wait_queue_head_t iic_wait;
#endif
static int iic_pending;

/* ----- global defines -----------------------------------------------	*/
#define DEB(x)	if (i2c_debug>=1) x
#define DEB2(x) if (i2c_debug>=2) x
#define DEB3(x) if (i2c_debug>=3) x
#define DEBE(x)	x	/* error messages 				*/


/* ----- local functions ----------------------------------------------	*/

static void iic_ite_setiic(void *data, int ctl, short val)
{
        unsigned long j = jiffies + 10;

	DEB3(printk(" Write 0x%02x to 0x%x\n",(unsigned short)val, ctl&0xff));
	DEB3({while (jiffies < j) schedule();}) 
	outw(val,ctl);
}

static short iic_ite_getiic(void *data, int ctl)
{
	short val;

	val = inw(ctl);
	DEB3(printk("Read 0x%02x from 0x%x\n",(unsigned short)val, ctl&0xff));  
	return (val);
}

/* Return our slave address.  This is the address
 * put on the I2C bus when another master on the bus wants to address us
 * as a slave
 */
static int iic_ite_getown(void *data)
{
	return (gpi.iic_own);
}


static int iic_ite_getclock(void *data)
{
	return (gpi.iic_clock);
}


#if 0
static void iic_ite_sleep(unsigned long timeout)
{
	schedule_timeout( timeout * HZ);
}
#endif


/* Put this process to sleep.  We will wake up when the
 * IIC controller interrupts.
 */
static void iic_ite_waitforpin(void) {

   int timeout = 2;

   /* If interrupts are enabled (which they are), then put the process to
    * sleep.  This process will be awakened by two events -- either the
    * the IIC peripheral interrupts or the timeout expires. 
    * If interrupts are not enabled then delay for a reasonable amount 
    * of time and return.
    */
   if (gpi.iic_irq > 0) {
	cli();
	if (iic_pending == 0) {
		interruptible_sleep_on_timeout(&iic_wait, timeout*HZ );
	} else
		iic_pending = 0;
	sti();
   } else {
      udelay(100);
   }
}


static void iic_ite_handler(int this_irq, void *dev_id, struct pt_regs *regs) 
{
	
   iic_pending = 1;

   DEB2(printk("iic_ite_handler: in interrupt handler\n"));
   wake_up_interruptible(&iic_wait);
}


/* Lock the region of memory where I/O registers exist.  Request our
 * interrupt line and register its associated handler.
 */
static int iic_hw_resrc_init(void)
{
  	if (check_region(gpi.iic_base, ITE_IIC_IO_SIZE) < 0 ) {
   	   return -ENODEV;
  	} else {
  	   request_region(gpi.iic_base, ITE_IIC_IO_SIZE, 
		"i2c (i2c bus adapter)");
  	}
	if (gpi.iic_irq > 0) {
	   if (request_irq(gpi.iic_irq, iic_ite_handler, 0, "ITE IIC", 0) < 0) {
	      gpi.iic_irq = 0;
	   } else
	      DEB3(printk("Enabled IIC IRQ %d\n", gpi.iic_irq));
	      enable_irq(gpi.iic_irq);
	}
	return 0;
}


static void iic_ite_release(void)
{
	if (gpi.iic_irq > 0) {
		disable_irq(gpi.iic_irq);
		free_irq(gpi.iic_irq, 0);
	}
	release_region(gpi.iic_base , 2);
}


static int iic_ite_reg(struct i2c_client *client)
{
	return 0;
}


static int iic_ite_unreg(struct i2c_client *client)
{
	return 0;
}


static void iic_ite_inc_use(struct i2c_adapter *adap)
{
#ifdef MODULE
	MOD_INC_USE_COUNT;
#endif
}


static void iic_ite_dec_use(struct i2c_adapter *adap)
{
#ifdef MODULE
	MOD_DEC_USE_COUNT;
#endif
}


/* ------------------------------------------------------------------------
 * Encapsulate the above functions in the correct operations structure.
 * This is only done when more than one hardware adapter is supported.
 */
static struct i2c_algo_iic_data iic_ite_data = {
	NULL,
	iic_ite_setiic,
	iic_ite_getiic,
	iic_ite_getown,
	iic_ite_getclock,
	iic_ite_waitforpin,
	80, 80, 100,		/*	waits, timeout */
};

static struct i2c_adapter iic_ite_ops = {
	"ITE IIC adapter",
	I2C_HW_I_IIC,
	NULL,
	&iic_ite_data,
	iic_ite_inc_use,
	iic_ite_dec_use,
	iic_ite_reg,
	iic_ite_unreg,
};

/* Called when the module is loaded.  This function starts the
 * cascade of calls up through the heirarchy of i2c modules (i.e. up to the
 *  algorithm layer and into to the core layer)
 */
static int __init iic_ite_init(void) 
{

	struct iic_ite *piic = &gpi;

	printk(KERN_INFO "Initialize ITE IIC adapter module\n");
	if (base == 0)
		piic->iic_base = DEFAULT_BASE;
	else
		piic->iic_base = base;

	if (irq == 0)
		piic->iic_irq = DEFAULT_IRQ;
	else
		piic->iic_irq = irq;

	if (clock == 0)
		piic->iic_clock = DEFAULT_CLOCK;
	else
		piic->iic_clock = clock;

	if (own == 0)
		piic->iic_own = DEFAULT_OWN;
	else
		piic->iic_own = own;

	iic_ite_data.data = (void *)piic;
#if (LINUX_VERSION_CODE >= 0x020301)
	init_waitqueue_head(&iic_wait);
#endif
	if (iic_hw_resrc_init() == 0) {
		if (i2c_iic_add_bus(&iic_ite_ops) < 0)
			return -ENODEV;
	} else {
		return -ENODEV;
	}
	printk(KERN_INFO " found device at %#x irq %d.\n", 
		piic->iic_base, piic->iic_irq);
	return 0;
}


static void iic_ite_exit(void)
{
	i2c_iic_del_bus(&iic_ite_ops);
        iic_ite_release();
}

EXPORT_NO_SYMBOLS;

/* If modules is NOT defined when this file is compiled, then the MODULE_*
 * macros will resolve to nothing
 */
MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
MODULE_DESCRIPTION("I2C-Bus adapter routines for ITE IIC bus adapter");

MODULE_PARM(base, "i");
MODULE_PARM(irq, "i");
MODULE_PARM(clock, "i");
MODULE_PARM(own, "i");
MODULE_PARM(i2c_debug,"i");


/* Called when module is loaded or when kernel is intialized.
 * If MODULES is defined when this file is compiled, then this function will
 * resolve to init_module (the function called when insmod is invoked for a
 * module).  Otherwise, this function is called early in the boot, when the
 * kernel is intialized.  Check out /include/init.h to see how this works.
 */
module_init(iic_ite_init);

/* Resolves to module_cleanup when MODULES is defined. */
module_exit(iic_ite_exit);