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
8 Copyright 2001 MontaVista Software Inc.
10 ----------------------------------------------------------------------------
11 This file was highly leveraged from i2c-elektor.c, which was created
12 by Simon G. Vogl and Hans Berglund:
15 Copyright (C) 1995-97 Simon G. Vogl
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 even
34 Frodo Looijaard <frodol@dds.nl> */
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 <linux/wait.h>
46 #include <linux/i2c.h>
47 #include <linux/i2c-algo-ite.h>
48 #include <linux/i2c-adap-ite.h>
49 #include "../i2c-ite.h"
51 #define DEFAULT_BASE 0x14014030
52 #define ITE_IIC_IO_SIZE 0x40
54 #define DEFAULT_CLOCK 0x1b0e /* default 16MHz/(27+14) = 400KHz */
55 #define DEFAULT_OWN 0x55
62 static struct iic_ite gpi
;
63 static wait_queue_head_t iic_wait
;
64 static int iic_pending
;
65 static spinlock_t lock
;
67 /* ----- local functions ---------------------------------------------- */
69 static void iic_ite_setiic(void *data
, int ctl
, short val
)
71 unsigned long j
= jiffies
+ 10;
73 pr_debug(" Write 0x%02x to 0x%x\n",(unsigned short)val
, ctl
&0xff);
75 while (time_before(jiffies
, j
))
81 static short iic_ite_getiic(void *data
, int ctl
)
86 pr_debug("Read 0x%02x from 0x%x\n",(unsigned short)val
, ctl
&0xff);
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
94 static int iic_ite_getown(void *data
)
100 static int iic_ite_getclock(void *data
)
102 return (gpi
.iic_clock
);
106 /* Put this process to sleep. We will wake up when the
107 * IIC controller interrupts.
109 static void iic_ite_waitforpin(void) {
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.
120 if (gpi
.iic_irq
> 0) {
121 spin_lock_irqsave(&lock
, flags
);
122 if (iic_pending
== 0) {
123 spin_unlock_irqrestore(&lock
, flags
);
124 prepare_to_wait(&iic_wait
, &wait
, TASK_INTERRUPTIBLE
);
125 if (schedule_timeout(timeout
*HZ
)) {
126 spin_lock_irqsave(&lock
, flags
);
127 if (iic_pending
== 1) {
130 spin_unlock_irqrestore(&lock
, flags
);
132 finish_wait(&iic_wait
, &wait
);
135 spin_unlock_irqrestore(&lock
, flags
);
143 static irqreturn_t
iic_ite_handler(int this_irq
, void *dev_id
,
144 struct pt_regs
*regs
)
150 wake_up_interruptible(&iic_wait
);
156 /* Lock the region of memory where I/O registers exist. Request our
157 * interrupt line and register its associated handler.
159 static int iic_hw_resrc_init(void)
161 if (!request_region(gpi
.iic_base
, ITE_IIC_IO_SIZE
, "i2c"))
164 if (gpi
.iic_irq
<= 0)
167 if (request_irq(gpi
.iic_irq
, iic_ite_handler
, 0, "ITE IIC", 0) < 0)
170 enable_irq(gpi
.iic_irq
);
176 static void iic_ite_release(void)
178 if (gpi
.iic_irq
> 0) {
179 disable_irq(gpi
.iic_irq
);
180 free_irq(gpi
.iic_irq
, 0);
182 release_region(gpi
.iic_base
, 2);
185 /* ------------------------------------------------------------------------
186 * Encapsulate the above functions in the correct operations structure.
187 * This is only done when more than one hardware adapter is supported.
189 static struct i2c_algo_iic_data iic_ite_data
= {
196 80, 80, 100, /* waits, timeout */
199 static struct i2c_adapter iic_ite_ops
= {
200 .owner
= THIS_MODULE
,
202 .algo_data
= &iic_ite_data
,
204 .name
= "ITE IIC adapter",
208 /* Called when the module is loaded. This function starts the
209 * cascade of calls up through the hierarchy of i2c modules (i.e. up to the
210 * algorithm layer and into to the core layer)
212 static int __init
iic_ite_init(void)
215 struct iic_ite
*piic
= &gpi
;
217 printk(KERN_INFO
"Initialize ITE IIC adapter module\n");
219 piic
->iic_base
= DEFAULT_BASE
;
221 piic
->iic_base
= base
;
224 piic
->iic_irq
= DEFAULT_IRQ
;
229 piic
->iic_clock
= DEFAULT_CLOCK
;
231 piic
->iic_clock
= clock
;
234 piic
->iic_own
= DEFAULT_OWN
;
238 iic_ite_data
.data
= (void *)piic
;
239 init_waitqueue_head(&iic_wait
);
240 spin_lock_init(&lock
);
241 if (iic_hw_resrc_init() == 0) {
242 if (i2c_iic_add_bus(&iic_ite_ops
) < 0)
247 printk(KERN_INFO
" found device at %#x irq %d.\n",
248 piic
->iic_base
, piic
->iic_irq
);
253 static void iic_ite_exit(void)
255 i2c_iic_del_bus(&iic_ite_ops
);
259 /* If modules is NOT defined when this file is compiled, then the MODULE_*
260 * macros will resolve to nothing
262 MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
263 MODULE_DESCRIPTION("I2C-Bus adapter routines for ITE IIC bus adapter");
264 MODULE_LICENSE("GPL");
266 module_param(base
, int, 0);
267 module_param(irq
, int, 0);
268 module_param(clock
, int, 0);
269 module_param(own
, int, 0);
272 /* Called when module is loaded or when kernel is initialized.
273 * If MODULES is defined when this file is compiled, then this function will
274 * resolve to init_module (the function called when insmod is invoked for a
275 * module). Otherwise, this function is called early in the boot, when the
276 * kernel is intialized. Check out /include/init.h to see how this works.
278 module_init(iic_ite_init
);
280 /* Resolves to module_cleanup when MODULES is defined. */
281 module_exit(iic_ite_exit
);