MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / ite_gpio.c
blob1a4dfef7e46db125f2626306e11bec97ba410b3e
1 /*
2 * FILE NAME ite_gpio.c
4 * BRIEF MODULE DESCRIPTION
5 * API for ITE GPIO device.
6 * Driver for ITE GPIO device.
8 * Author: MontaVista Software, Inc. <source@mvista.com>
9 * Hai-Pao Fan <haipao@mvista.com>
11 * Copyright 2001 MontaVista Software Inc.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
21 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/module.h>
35 #include <linux/config.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/miscdevice.h>
39 #include <linux/init.h>
40 #include <linux/ioport.h>
41 #include <asm/uaccess.h>
42 #include <asm/addrspace.h>
43 #include <asm/it8172/it8172_int.h>
44 #include <linux/sched.h>
45 #include <linux/ite_gpio.h>
47 #define ite_gpio_base 0x14013800
49 #define ITE_GPADR (*(volatile __u8 *)(0x14013800 + KSEG1))
50 #define ITE_GPBDR (*(volatile __u8 *)(0x14013808 + KSEG1))
51 #define ITE_GPCDR (*(volatile __u8 *)(0x14013810 + KSEG1))
52 #define ITE_GPACR (*(volatile __u16 *)(0x14013802 + KSEG1))
53 #define ITE_GPBCR (*(volatile __u16 *)(0x1401380a + KSEG1))
54 #define ITE_GPCCR (*(volatile __u16 *)(0x14013812 + KSEG1))
55 #define ITE_GPAICR (*(volatile __u16 *)(0x14013804 + KSEG1))
56 #define ITE_GPBICR (*(volatile __u16 *)(0x1401380c + KSEG1))
57 #define ITE_GPCICR (*(volatile __u16 *)(0x14013814 + KSEG1))
58 #define ITE_GPAISR (*(volatile __u8 *)(0x14013806 + KSEG1))
59 #define ITE_GPBISR (*(volatile __u8 *)(0x1401380e + KSEG1))
60 #define ITE_GPCISR (*(volatile __u8 *)(0x14013816 + KSEG1))
61 #define ITE_GCR (*(volatile __u8 *)(0x14013818 + KSEG1))
63 #define MAX_GPIO_LINE 21
64 static int ite_gpio_irq=IT8172_GPIO_IRQ;
66 static long ite_irq_counter[MAX_GPIO_LINE];
67 wait_queue_head_t ite_gpio_wait[MAX_GPIO_LINE];
68 static int ite_gpio_irq_pending[MAX_GPIO_LINE];
70 static int ite_gpio_debug=0;
71 #define DEB(x) if (ite_gpio_debug>=1) x
73 int ite_gpio_in(__u32 device, __u32 mask, volatile __u32 *data)
75 DEB(printk("ite_gpio_in mask=0x%x\n",mask));
77 switch (device) {
78 case ITE_GPIO_PORTA:
79 ITE_GPACR = (__u16)mask; /* 0xffff */
80 *data = ITE_GPADR;
81 break;
82 case ITE_GPIO_PORTB:
83 ITE_GPBCR = (__u16)mask; /* 0xffff */
84 *data = ITE_GPBDR;
85 break;
86 case ITE_GPIO_PORTC:
87 ITE_GPCCR = (__u16)mask; /* 0x03ff */
88 *data = ITE_GPCDR;
89 break;
90 default:
91 return -EFAULT;
94 return 0;
98 int ite_gpio_out(__u32 device, __u32 mask, __u32 data)
100 switch (device) {
101 case ITE_GPIO_PORTA:
102 ITE_GPACR = (__u16)mask; /* 0x5555 */
103 ITE_GPADR = (__u8)data;
104 break;
105 case ITE_GPIO_PORTB:
106 ITE_GPBCR = (__u16)mask; /* 0x5555 */
107 ITE_GPBDR = (__u8)data;
108 break;
109 case ITE_GPIO_PORTC:
110 ITE_GPCCR = (__u16)mask; /* 0x0155 */
111 ITE_GPCDR = (__u8)data;
112 break;
113 default:
114 return -EFAULT;
117 return 0;
120 int ite_gpio_int_ctrl(__u32 device, __u32 mask, __u32 data)
122 switch (device) {
123 case ITE_GPIO_PORTA:
124 ITE_GPAICR = (ITE_GPAICR & ~mask) | (data & mask);
125 break;
126 case ITE_GPIO_PORTB:
127 ITE_GPBICR = (ITE_GPBICR & ~mask) | (data & mask);
128 break;
129 case ITE_GPIO_PORTC:
130 ITE_GPCICR = (ITE_GPCICR & ~mask) | (data & mask);
131 break;
132 default:
133 return -EFAULT;
136 return 0;
139 int ite_gpio_in_status(__u32 device, __u32 mask, volatile __u32 *data)
141 int ret=-1;
143 if ((MAX_GPIO_LINE > *data) && (*data >= 0))
144 ret=ite_gpio_irq_pending[*data];
146 DEB(printk("ite_gpio_in_status %d ret=%d\n",*data, ret));
148 switch (device) {
149 case ITE_GPIO_PORTA:
150 *data = ITE_GPAISR & mask;
151 break;
152 case ITE_GPIO_PORTB:
153 *data = ITE_GPBISR & mask;
154 break;
155 case ITE_GPIO_PORTC:
156 *data = ITE_GPCISR & mask;
157 break;
158 default:
159 return -EFAULT;
162 return ret;
165 int ite_gpio_out_status(__u32 device, __u32 mask, __u32 data)
167 switch (device) {
168 case ITE_GPIO_PORTA:
169 ITE_GPAISR = (ITE_GPAISR & ~mask) | (data & mask);
170 break;
171 case ITE_GPIO_PORTB:
172 ITE_GPBISR = (ITE_GPBISR & ~mask) | (data & mask);
173 break;
174 case ITE_GPIO_PORTC:
175 ITE_GPCISR = (ITE_GPCISR & ~mask) | (data & mask);
176 break;
177 default:
178 return -EFAULT;
181 return 0;
184 int ite_gpio_gen_ctrl(__u32 device, __u32 mask, __u32 data)
186 ITE_GCR = (ITE_GCR & ~mask) | (data & mask);
188 return 0;
191 int ite_gpio_int_wait (__u32 device, __u32 mask, __u32 data)
193 int i,line=0, ret=0;
194 unsigned long flags;
196 switch (device) {
197 case ITE_GPIO_PORTA:
198 line = data & mask;
199 break;
200 case ITE_GPIO_PORTB:
201 line = (data & mask) <<8;
202 break;
203 case ITE_GPIO_PORTC:
204 line = (data & mask) <<16;
205 break;
207 for (i=MAX_GPIO_LINE-1; i >= 0; i--) {
208 if ( (line) & (1 << i))
209 break;
212 DEB(printk("wait device=0x%d mask=0x%x data=0x%x index %d\n",
213 device, mask, data, i));
215 if (line & ~(1<<i))
216 return -EFAULT;
218 if (ite_gpio_irq_pending[i]==1)
219 return -EFAULT;
221 save_flags (flags);
222 cli();
223 ite_gpio_irq_pending[i] = 1;
224 ret = interruptible_sleep_on_timeout(&ite_gpio_wait[i], 3*HZ);
225 restore_flags (flags);
226 ite_gpio_irq_pending[i] = 0;
228 return ret;
231 EXPORT_SYMBOL(ite_gpio_in);
232 EXPORT_SYMBOL(ite_gpio_out);
233 EXPORT_SYMBOL(ite_gpio_int_ctrl);
234 EXPORT_SYMBOL(ite_gpio_in_status);
235 EXPORT_SYMBOL(ite_gpio_out_status);
236 EXPORT_SYMBOL(ite_gpio_gen_ctrl);
237 EXPORT_SYMBOL(ite_gpio_int_wait);
239 static int ite_gpio_open(struct inode *inode, struct file *file)
241 unsigned int minor = iminor(inode);
242 if (minor != GPIO_MINOR)
243 return -ENODEV;
245 return 0;
249 static int ite_gpio_release(struct inode *inode, struct file *file)
251 return 0;
255 static int ite_gpio_ioctl(struct inode *inode, struct file *file,
256 unsigned int cmd, unsigned long arg)
259 static struct ite_gpio_ioctl_data ioctl_data;
261 if (copy_from_user(&ioctl_data, (struct ite_gpio_ioctl_data *)arg,
262 sizeof(ioctl_data)))
263 return -EFAULT;
264 if ((ioctl_data.device < ITE_GPIO_PORTA) ||
265 (ioctl_data.device > ITE_GPIO_PORTC) )
266 return -EFAULT;
268 switch(cmd) {
269 case ITE_GPIO_IN:
270 if (ite_gpio_in(ioctl_data.device, ioctl_data.mask,
271 &ioctl_data.data))
272 return -EFAULT;
274 if (copy_to_user((struct ite_gpio_ioctl_data *)arg,
275 &ioctl_data, sizeof(ioctl_data)))
276 return -EFAULT;
277 break;
279 case ITE_GPIO_OUT:
280 return ite_gpio_out(ioctl_data.device,
281 ioctl_data.mask, ioctl_data.data);
282 break;
284 case ITE_GPIO_INT_CTRL:
285 return ite_gpio_int_ctrl(ioctl_data.device,
286 ioctl_data.mask, ioctl_data.data);
287 break;
289 case ITE_GPIO_IN_STATUS:
290 if (ite_gpio_in_status(ioctl_data.device, ioctl_data.mask,
291 &ioctl_data.data))
292 return -EFAULT;
293 if (copy_to_user((struct ite_gpio_ioctl_data *)arg,
294 &ioctl_data, sizeof(ioctl_data)))
295 return -EFAULT;
296 break;
298 case ITE_GPIO_OUT_STATUS:
299 return ite_gpio_out_status(ioctl_data.device,
300 ioctl_data.mask, ioctl_data.data);
301 break;
303 case ITE_GPIO_GEN_CTRL:
304 return ite_gpio_gen_ctrl(ioctl_data.device,
305 ioctl_data.mask, ioctl_data.data);
306 break;
308 case ITE_GPIO_INT_WAIT:
309 return ite_gpio_int_wait(ioctl_data.device,
310 ioctl_data.mask, ioctl_data.data);
311 break;
313 default:
314 return -ENOIOCTLCMD;
317 return 0;
320 static void ite_gpio_irq_handler(int this_irq, void *dev_id, struct pt_regs *regs)
322 int i,line;
324 line = ITE_GPCISR & 0x1f;
325 for (i=4; i >=0; i--) {
326 if ( line & (1 << i)) {
327 ++ite_irq_counter[i+16];
328 ite_gpio_irq_pending[i+16] = 2;
329 wake_up_interruptible(&ite_gpio_wait[i+16]);
331 DEB(printk("interrupt 0x%x %d\n", &ite_gpio_wait[i+16], i+16));
333 ITE_GPCISR = ITE_GPCISR & (1<<i);
334 return;
337 line = ITE_GPBISR;
338 for (i=7; i >= 0; i--) {
339 if ( line & (1 << i)) {
340 ++ite_irq_counter[i+8];
341 ite_gpio_irq_pending[i+8] = 2;
342 wake_up_interruptible(&ite_gpio_wait[i+8]);
344 DEB(printk("interrupt 0x%x %d\n",ITE_GPBISR, i+8));
346 ITE_GPBISR = ITE_GPBISR & (1<<i);
347 return;
350 line = ITE_GPAISR;
351 for (i=7; i >= 0; i--) {
352 if ( line & (1 << i)) {
353 ++ite_irq_counter[i];
354 ite_gpio_irq_pending[i] = 2;
355 wake_up_interruptible(&ite_gpio_wait[i]);
357 DEB(printk("interrupt 0x%x %d\n",ITE_GPAISR, i));
359 ITE_GPAISR = ITE_GPAISR & (1<<i);
360 return;
365 static struct file_operations ite_gpio_fops =
367 .owner = THIS_MODULE,
368 .ioctl = ite_gpio_ioctl,
369 .open = ite_gpio_open,
370 .release = ite_gpio_release,
373 /* GPIO_MINOR in include/linux/miscdevice.h */
374 static struct miscdevice ite_gpio_miscdev =
376 GPIO_MINOR,
377 "ite_gpio",
378 &ite_gpio_fops
381 int __init ite_gpio_init(void)
383 int i;
385 if (misc_register(&ite_gpio_miscdev))
386 return -ENODEV;
388 if (!request_region(ite_gpio_base, 0x1c, "ITE GPIO"))
390 misc_deregister(&ite_gpio_miscdev);
391 return -EIO;
394 /* initialize registers */
395 ITE_GPACR = 0xffff;
396 ITE_GPBCR = 0xffff;
397 ITE_GPCCR = 0xffff;
398 ITE_GPAICR = 0x00ff;
399 ITE_GPBICR = 0x00ff;
400 ITE_GPCICR = 0x00ff;
401 ITE_GCR = 0;
403 for (i = 0; i < MAX_GPIO_LINE; i++) {
404 ite_gpio_irq_pending[i]=0;
405 init_waitqueue_head(&ite_gpio_wait[i]);
408 if (request_irq(ite_gpio_irq, ite_gpio_irq_handler, SA_SHIRQ, "gpio", 0) < 0) {
409 misc_deregister(&ite_gpio_miscdev);
410 release_region(ite_gpio_base, 0x1c);
411 return 0;
414 printk("GPIO at 0x%x (irq = %d)\n", ite_gpio_base, ite_gpio_irq);
416 return 0;
419 void __exit ite_gpio_exit(void)
421 misc_deregister(&ite_gpio_miscdev);
424 module_init(ite_gpio_init);
425 module_exit(ite_gpio_exit);
427 MODULE_LICENSE("GPL");