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
));
79 ITE_GPACR
= (__u16
)mask
; /* 0xffff */
83 ITE_GPBCR
= (__u16
)mask
; /* 0xffff */
87 ITE_GPCCR
= (__u16
)mask
; /* 0x03ff */
98 int ite_gpio_out(__u32 device
, __u32 mask
, __u32 data
)
102 ITE_GPACR
= (__u16
)mask
; /* 0x5555 */
103 ITE_GPADR
= (__u8
)data
;
106 ITE_GPBCR
= (__u16
)mask
; /* 0x5555 */
107 ITE_GPBDR
= (__u8
)data
;
110 ITE_GPCCR
= (__u16
)mask
; /* 0x0155 */
111 ITE_GPCDR
= (__u8
)data
;
120 int ite_gpio_int_ctrl(__u32 device
, __u32 mask
, __u32 data
)
124 ITE_GPAICR
= (ITE_GPAICR
& ~mask
) | (data
& mask
);
127 ITE_GPBICR
= (ITE_GPBICR
& ~mask
) | (data
& mask
);
130 ITE_GPCICR
= (ITE_GPCICR
& ~mask
) | (data
& mask
);
139 int ite_gpio_in_status(__u32 device
, __u32 mask
, volatile __u32
*data
)
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
));
150 *data
= ITE_GPAISR
& mask
;
153 *data
= ITE_GPBISR
& mask
;
156 *data
= ITE_GPCISR
& mask
;
165 int ite_gpio_out_status(__u32 device
, __u32 mask
, __u32 data
)
169 ITE_GPAISR
= (ITE_GPAISR
& ~mask
) | (data
& mask
);
172 ITE_GPBISR
= (ITE_GPBISR
& ~mask
) | (data
& mask
);
175 ITE_GPCISR
= (ITE_GPCISR
& ~mask
) | (data
& mask
);
184 int ite_gpio_gen_ctrl(__u32 device
, __u32 mask
, __u32 data
)
186 ITE_GCR
= (ITE_GCR
& ~mask
) | (data
& mask
);
191 int ite_gpio_int_wait (__u32 device
, __u32 mask
, __u32 data
)
201 line
= (data
& mask
) <<8;
204 line
= (data
& mask
) <<16;
207 for (i
=MAX_GPIO_LINE
-1; i
>= 0; i
--) {
208 if ( (line
) & (1 << i
))
212 DEB(printk("wait device=0x%d mask=0x%x data=0x%x index %d\n",
213 device
, mask
, data
, i
));
218 if (ite_gpio_irq_pending
[i
]==1)
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;
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
)
249 static int ite_gpio_release(struct inode
*inode
, struct file
*file
)
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
,
264 if ((ioctl_data
.device
< ITE_GPIO_PORTA
) ||
265 (ioctl_data
.device
> ITE_GPIO_PORTC
) )
270 if (ite_gpio_in(ioctl_data
.device
, ioctl_data
.mask
,
274 if (copy_to_user((struct ite_gpio_ioctl_data
*)arg
,
275 &ioctl_data
, sizeof(ioctl_data
)))
280 return ite_gpio_out(ioctl_data
.device
,
281 ioctl_data
.mask
, ioctl_data
.data
);
284 case ITE_GPIO_INT_CTRL
:
285 return ite_gpio_int_ctrl(ioctl_data
.device
,
286 ioctl_data
.mask
, ioctl_data
.data
);
289 case ITE_GPIO_IN_STATUS
:
290 if (ite_gpio_in_status(ioctl_data
.device
, ioctl_data
.mask
,
293 if (copy_to_user((struct ite_gpio_ioctl_data
*)arg
,
294 &ioctl_data
, sizeof(ioctl_data
)))
298 case ITE_GPIO_OUT_STATUS
:
299 return ite_gpio_out_status(ioctl_data
.device
,
300 ioctl_data
.mask
, ioctl_data
.data
);
303 case ITE_GPIO_GEN_CTRL
:
304 return ite_gpio_gen_ctrl(ioctl_data
.device
,
305 ioctl_data
.mask
, ioctl_data
.data
);
308 case ITE_GPIO_INT_WAIT
:
309 return ite_gpio_int_wait(ioctl_data
.device
,
310 ioctl_data
.mask
, ioctl_data
.data
);
320 static void ite_gpio_irq_handler(int this_irq
, void *dev_id
, struct pt_regs
*regs
)
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
);
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
);
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
);
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
=
381 int __init
ite_gpio_init(void)
385 if (misc_register(&ite_gpio_miscdev
))
388 if (!request_region(ite_gpio_base
, 0x1c, "ITE GPIO"))
390 misc_deregister(&ite_gpio_miscdev
);
394 /* initialize registers */
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);
414 printk("GPIO at 0x%x (irq = %d)\n", ite_gpio_base
, ite_gpio_irq
);
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");