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.
33 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/kernel.h>
36 #include <linux/miscdevice.h>
37 #include <linux/init.h>
38 #include <linux/ioport.h>
39 #include <asm/uaccess.h>
40 #include <asm/addrspace.h>
41 #include <asm/it8172/it8172_int.h>
42 #include <linux/sched.h>
43 #include <linux/ite_gpio.h>
45 #define ite_gpio_base 0x14013800
47 #define ITE_GPADR (*(volatile __u8 *)(0x14013800 + KSEG1))
48 #define ITE_GPBDR (*(volatile __u8 *)(0x14013808 + KSEG1))
49 #define ITE_GPCDR (*(volatile __u8 *)(0x14013810 + KSEG1))
50 #define ITE_GPACR (*(volatile __u16 *)(0x14013802 + KSEG1))
51 #define ITE_GPBCR (*(volatile __u16 *)(0x1401380a + KSEG1))
52 #define ITE_GPCCR (*(volatile __u16 *)(0x14013812 + KSEG1))
53 #define ITE_GPAICR (*(volatile __u16 *)(0x14013804 + KSEG1))
54 #define ITE_GPBICR (*(volatile __u16 *)(0x1401380c + KSEG1))
55 #define ITE_GPCICR (*(volatile __u16 *)(0x14013814 + KSEG1))
56 #define ITE_GPAISR (*(volatile __u8 *)(0x14013806 + KSEG1))
57 #define ITE_GPBISR (*(volatile __u8 *)(0x1401380e + KSEG1))
58 #define ITE_GPCISR (*(volatile __u8 *)(0x14013816 + KSEG1))
59 #define ITE_GCR (*(volatile __u8 *)(0x14013818 + KSEG1))
61 #define MAX_GPIO_LINE 21
62 static int ite_gpio_irq
=IT8172_GPIO_IRQ
;
64 static long ite_irq_counter
[MAX_GPIO_LINE
];
65 wait_queue_head_t ite_gpio_wait
[MAX_GPIO_LINE
];
66 static int ite_gpio_irq_pending
[MAX_GPIO_LINE
];
68 static int ite_gpio_debug
=0;
69 #define DEB(x) if (ite_gpio_debug>=1) x
71 int ite_gpio_in(__u32 device
, __u32 mask
, volatile __u32
*data
)
73 DEB(printk("ite_gpio_in mask=0x%x\n",mask
));
77 ITE_GPACR
= (__u16
)mask
; /* 0xffff */
81 ITE_GPBCR
= (__u16
)mask
; /* 0xffff */
85 ITE_GPCCR
= (__u16
)mask
; /* 0x03ff */
96 int ite_gpio_out(__u32 device
, __u32 mask
, __u32 data
)
100 ITE_GPACR
= (__u16
)mask
; /* 0x5555 */
101 ITE_GPADR
= (__u8
)data
;
104 ITE_GPBCR
= (__u16
)mask
; /* 0x5555 */
105 ITE_GPBDR
= (__u8
)data
;
108 ITE_GPCCR
= (__u16
)mask
; /* 0x0155 */
109 ITE_GPCDR
= (__u8
)data
;
118 int ite_gpio_int_ctrl(__u32 device
, __u32 mask
, __u32 data
)
122 ITE_GPAICR
= (ITE_GPAICR
& ~mask
) | (data
& mask
);
125 ITE_GPBICR
= (ITE_GPBICR
& ~mask
) | (data
& mask
);
128 ITE_GPCICR
= (ITE_GPCICR
& ~mask
) | (data
& mask
);
137 int ite_gpio_in_status(__u32 device
, __u32 mask
, volatile __u32
*data
)
141 if ((MAX_GPIO_LINE
> *data
) && (*data
>= 0))
142 ret
=ite_gpio_irq_pending
[*data
];
144 DEB(printk("ite_gpio_in_status %d ret=%d\n",*data
, ret
));
148 *data
= ITE_GPAISR
& mask
;
151 *data
= ITE_GPBISR
& mask
;
154 *data
= ITE_GPCISR
& mask
;
163 int ite_gpio_out_status(__u32 device
, __u32 mask
, __u32 data
)
167 ITE_GPAISR
= (ITE_GPAISR
& ~mask
) | (data
& mask
);
170 ITE_GPBISR
= (ITE_GPBISR
& ~mask
) | (data
& mask
);
173 ITE_GPCISR
= (ITE_GPCISR
& ~mask
) | (data
& mask
);
182 int ite_gpio_gen_ctrl(__u32 device
, __u32 mask
, __u32 data
)
184 ITE_GCR
= (ITE_GCR
& ~mask
) | (data
& mask
);
189 int ite_gpio_int_wait (__u32 device
, __u32 mask
, __u32 data
)
199 line
= (data
& mask
) <<8;
202 line
= (data
& mask
) <<16;
205 for (i
=MAX_GPIO_LINE
-1; i
>= 0; i
--) {
206 if ( (line
) & (1 << i
))
210 DEB(printk("wait device=0x%d mask=0x%x data=0x%x index %d\n",
211 device
, mask
, data
, i
));
216 if (ite_gpio_irq_pending
[i
]==1)
221 ite_gpio_irq_pending
[i
] = 1;
222 ret
= interruptible_sleep_on_timeout(&ite_gpio_wait
[i
], 3*HZ
);
223 restore_flags (flags
);
224 ite_gpio_irq_pending
[i
] = 0;
229 EXPORT_SYMBOL(ite_gpio_in
);
230 EXPORT_SYMBOL(ite_gpio_out
);
231 EXPORT_SYMBOL(ite_gpio_int_ctrl
);
232 EXPORT_SYMBOL(ite_gpio_in_status
);
233 EXPORT_SYMBOL(ite_gpio_out_status
);
234 EXPORT_SYMBOL(ite_gpio_gen_ctrl
);
235 EXPORT_SYMBOL(ite_gpio_int_wait
);
237 static int ite_gpio_open(struct inode
*inode
, struct file
*file
)
243 static int ite_gpio_release(struct inode
*inode
, struct file
*file
)
249 static int ite_gpio_ioctl(struct inode
*inode
, struct file
*file
,
250 unsigned int cmd
, unsigned long arg
)
252 static struct ite_gpio_ioctl_data ioctl_data
;
254 if (copy_from_user(&ioctl_data
, (struct ite_gpio_ioctl_data
*)arg
,
257 if ((ioctl_data
.device
< ITE_GPIO_PORTA
) ||
258 (ioctl_data
.device
> ITE_GPIO_PORTC
) )
263 if (ite_gpio_in(ioctl_data
.device
, ioctl_data
.mask
,
267 if (copy_to_user((struct ite_gpio_ioctl_data
*)arg
,
268 &ioctl_data
, sizeof(ioctl_data
)))
273 return ite_gpio_out(ioctl_data
.device
,
274 ioctl_data
.mask
, ioctl_data
.data
);
277 case ITE_GPIO_INT_CTRL
:
278 return ite_gpio_int_ctrl(ioctl_data
.device
,
279 ioctl_data
.mask
, ioctl_data
.data
);
282 case ITE_GPIO_IN_STATUS
:
283 if (ite_gpio_in_status(ioctl_data
.device
, ioctl_data
.mask
,
286 if (copy_to_user((struct ite_gpio_ioctl_data
*)arg
,
287 &ioctl_data
, sizeof(ioctl_data
)))
291 case ITE_GPIO_OUT_STATUS
:
292 return ite_gpio_out_status(ioctl_data
.device
,
293 ioctl_data
.mask
, ioctl_data
.data
);
296 case ITE_GPIO_GEN_CTRL
:
297 return ite_gpio_gen_ctrl(ioctl_data
.device
,
298 ioctl_data
.mask
, ioctl_data
.data
);
301 case ITE_GPIO_INT_WAIT
:
302 return ite_gpio_int_wait(ioctl_data
.device
,
303 ioctl_data
.mask
, ioctl_data
.data
);
314 static void ite_gpio_irq_handler(int this_irq
, void *dev_id
,
315 struct pt_regs
*regs
)
319 line
= ITE_GPCISR
& 0x1f;
320 for (i
=4; i
>=0; i
--) {
321 if ( line
& (1 << i
)) {
322 ++ite_irq_counter
[i
+16];
323 ite_gpio_irq_pending
[i
+16] = 2;
324 wake_up_interruptible(&ite_gpio_wait
[i
+16]);
326 DEB(printk("interrupt 0x%x %d\n", &ite_gpio_wait
[i
+16], i
+16));
328 ITE_GPCISR
= ITE_GPCISR
& (1<<i
);
333 for (i
=7; i
>= 0; i
--) {
334 if ( line
& (1 << i
)) {
335 ++ite_irq_counter
[i
+8];
336 ite_gpio_irq_pending
[i
+8] = 2;
337 wake_up_interruptible(&ite_gpio_wait
[i
+8]);
339 DEB(printk("interrupt 0x%x %d\n",ITE_GPBISR
, i
+8));
341 ITE_GPBISR
= ITE_GPBISR
& (1<<i
);
346 for (i
=7; i
>= 0; i
--) {
347 if ( line
& (1 << i
)) {
348 ++ite_irq_counter
[i
];
349 ite_gpio_irq_pending
[i
] = 2;
350 wake_up_interruptible(&ite_gpio_wait
[i
]);
352 DEB(printk("interrupt 0x%x %d\n",ITE_GPAISR
, i
));
354 ITE_GPAISR
= ITE_GPAISR
& (1<<i
);
360 static const struct file_operations ite_gpio_fops
= {
361 .owner
= THIS_MODULE
,
362 .ioctl
= ite_gpio_ioctl
,
363 .open
= ite_gpio_open
,
364 .release
= ite_gpio_release
,
367 static struct miscdevice ite_gpio_miscdev
= {
373 int __init
ite_gpio_init(void)
377 if (misc_register(&ite_gpio_miscdev
))
380 if (!request_region(ite_gpio_base
, 0x1c, "ITE GPIO"))
382 misc_deregister(&ite_gpio_miscdev
);
386 /* initialize registers */
395 for (i
= 0; i
< MAX_GPIO_LINE
; i
++) {
396 ite_gpio_irq_pending
[i
]=0;
397 init_waitqueue_head(&ite_gpio_wait
[i
]);
400 if (request_irq(ite_gpio_irq
, ite_gpio_irq_handler
, IRQF_SHARED
, "gpio", 0) < 0) {
401 misc_deregister(&ite_gpio_miscdev
);
402 release_region(ite_gpio_base
, 0x1c);
406 printk("GPIO at 0x%x (irq = %d)\n", ite_gpio_base
, ite_gpio_irq
);
411 static void __exit
ite_gpio_exit(void)
413 misc_deregister(&ite_gpio_miscdev
);
416 module_init(ite_gpio_init
);
417 module_exit(ite_gpio_exit
);
419 MODULE_LICENSE("GPL");