ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / vr41xx / common / vrc4173.c
1 /*
2  * FILE NAME
3  *      drivers/char/vrc4173.c
4  * 
5  * BRIEF MODULE DESCRIPTION
6  *      NEC VRC4173 driver for NEC VR4122/VR4131.
7  *
8  * Author: Yoichi Yuasa
9  *         yyuasa@mvista.com or source@mvista.com
10  *
11  * Copyright 2001,2002 MontaVista Software Inc.
12  *
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.
17  *
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.
21  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
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.
32  */
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/interrupt.h>
36 #include <linux/irq.h>
37 #include <linux/pci.h>
38 #include <linux/types.h>
39
40 #include <asm/vr41xx/vr41xx.h>
41 #include <asm/vr41xx/vrc4173.h>
42
43 MODULE_DESCRIPTION("NEC VRC4173 driver for NEC VR4122/4131");
44 MODULE_AUTHOR("Yoichi Yuasa <yyuasa@mvista.com>");
45 MODULE_LICENSE("GPL");
46
47 #define VRC4173_CMUCLKMSK       0x040
48 #define VRC4173_CMUSRST         0x042
49
50 #define VRC4173_SELECTREG       0x09e
51
52 #define VRC4173_SYSINT1REG      0x060
53 #define VRC4173_MSYSINT1REG     0x06c
54
55 static struct pci_device_id vrc4173_table[] = {
56         {PCI_VENDOR_ID_NEC, PCI_DEVICE_ID_NEC_VRC4173, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
57         {0, }
58 };
59
60 unsigned long vrc4173_io_offset = 0;
61
62 EXPORT_SYMBOL(vrc4173_io_offset);
63
64 static u16 vrc4173_cmuclkmsk;
65 static int vrc4173_initialized;
66
67 void vrc4173_clock_supply(u16 mask)
68 {
69         if (vrc4173_initialized) {
70                 vrc4173_cmuclkmsk |= mask;
71                 vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
72         }
73 }
74
75 void vrc4173_clock_mask(u16 mask)
76 {
77         if (vrc4173_initialized) {
78                 vrc4173_cmuclkmsk &= ~mask;
79                 vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
80         }
81 }
82
83 static inline void vrc4173_cmu_init(void)
84 {
85         vrc4173_cmuclkmsk = vrc4173_inw(VRC4173_CMUCLKMSK);
86 }
87
88 EXPORT_SYMBOL(vrc4173_clock_supply);
89 EXPORT_SYMBOL(vrc4173_clock_mask);
90
91 void vrc4173_select_function(int func)
92 {
93         u16 val;
94
95         if (vrc4173_initialized) {
96                 val = vrc4173_inw(VRC4173_SELECTREG);
97                 switch(func) {
98                 case PS2CH1_SELECT:
99                         val |= 0x0004;
100                         break;
101                 case PS2CH2_SELECT:
102                         val |= 0x0002;
103                         break;
104                 case TOUCHPANEL_SELECT:
105                         val &= 0x0007;
106                         break;
107                 case KIU8_SELECT:
108                         val &= 0x000e;
109                         break;
110                 case KIU10_SELECT:
111                         val &= 0x000c;
112                         break;
113                 case KIU12_SELECT:
114                         val &= 0x0008;
115                         break;
116                 case GPIO_SELECT:
117                         val |= 0x0008;
118                         break;
119                 }
120                 vrc4173_outw(val, VRC4173_SELECTREG);
121         }
122 }
123
124 EXPORT_SYMBOL(vrc4173_select_function);
125
126 static void enable_vrc4173_irq(unsigned int irq)
127 {
128         u16 val;
129
130         val = vrc4173_inw(VRC4173_MSYSINT1REG);
131         val |= (u16)1 << (irq - VRC4173_IRQ_BASE);
132         vrc4173_outw(val, VRC4173_MSYSINT1REG);
133 }
134
135 static void disable_vrc4173_irq(unsigned int irq)
136 {
137         u16 val;
138
139         val = vrc4173_inw(VRC4173_MSYSINT1REG);
140         val &= ~((u16)1 << (irq - VRC4173_IRQ_BASE));
141         vrc4173_outw(val, VRC4173_MSYSINT1REG);
142 }
143
144 static unsigned int startup_vrc4173_irq(unsigned int irq)
145 {
146         enable_vrc4173_irq(irq);
147         return 0; /* never anything pending */
148 }
149
150 #define shutdown_vrc4173_irq    disable_vrc4173_irq
151 #define ack_vrc4173_irq         disable_vrc4173_irq
152
153 static void end_vrc4173_irq(unsigned int irq)
154 {
155         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
156                 enable_vrc4173_irq(irq);
157 }
158
159 static struct hw_interrupt_type vrc4173_irq_type = {
160         "VRC4173",
161         startup_vrc4173_irq,
162         shutdown_vrc4173_irq,
163         enable_vrc4173_irq,
164         disable_vrc4173_irq,
165         ack_vrc4173_irq,
166         end_vrc4173_irq,
167         NULL
168 };
169
170 static int vrc4173_get_irq_number(int irq)
171 {
172         u16 status, mask;
173         int i;
174
175         status = vrc4173_inw(VRC4173_SYSINT1REG);
176         mask = vrc4173_inw(VRC4173_MSYSINT1REG);
177
178         status &= mask;
179         if (status) {
180                 for (i = 0; i < 16; i++)
181                         if (status & (0x0001 << i))
182                                 return VRC4173_IRQ_BASE + i;
183         }
184
185         return -EINVAL;
186 }
187
188 static inline void vrc4173_icu_init(int cascade_irq)
189 {
190         int i;
191
192         if (cascade_irq < GIU_IRQ(0) || cascade_irq > GIU_IRQ(15))
193                 return;
194         
195         vrc4173_outw(0, VRC4173_MSYSINT1REG);
196
197         vr41xx_set_irq_trigger(GIU_IRQ_TO_PIN(cascade_irq), TRIGGER_LEVEL, SIGNAL_THROUGH);
198         vr41xx_set_irq_level(GIU_IRQ_TO_PIN(cascade_irq), LEVEL_LOW);
199
200         for (i = VRC4173_IRQ_BASE; i <= VRC4173_IRQ_LAST; i++)
201                 irq_desc[i].handler = &vrc4173_irq_type;
202 }
203
204 static int __devinit vrc4173_probe(struct pci_dev *pdev,
205                                    const struct pci_device_id *ent)
206 {
207         unsigned long start, flags;
208         int err;
209
210         if ((err = pci_enable_device(pdev)) < 0) {
211                 printk(KERN_ERR "vrc4173: failed to enable device -- err=%d\n", err);
212                 return err;
213         }
214
215         pci_set_master(pdev);
216
217         start = pci_resource_start(pdev, 0);
218         if (!start) {
219                 printk(KERN_ERR "vrc4173:No PCI I/O resources, aborting\n");
220                 return -ENODEV;
221         }
222
223         if (!start || (((flags = pci_resource_flags(pdev, 0)) & IORESOURCE_IO) == 0)) {
224                 printk(KERN_ERR "vrc4173: No PCI I/O resources, aborting\n");
225                 return -ENODEV;
226         }
227
228         if ((err = pci_request_regions(pdev, "NEC VRC4173")) < 0) {
229                 printk(KERN_ERR "vrc4173: PCI resources are busy, aborting\n");
230                 return err;
231         }
232
233         set_vrc4173_io_offset(start);
234
235         vrc4173_cmu_init();
236
237         vrc4173_icu_init(pdev->irq);
238
239         if ((err = vr41xx_cascade_irq(pdev->irq, vrc4173_get_irq_number)) < 0) {
240                 printk(KERN_ERR
241                        "vrc4173: IRQ resource %d is busy, aborting\n", pdev->irq);
242                 return err;
243         }
244
245         printk(KERN_INFO
246                "NEC VRC4173 at 0x%#08lx, IRQ is cascaded to %d\n", start, pdev->irq);
247
248         return 0;
249 }
250
251 static struct pci_driver vrc4173_driver = {
252         .name           = "NEC VRC4173",
253         .probe          = vrc4173_probe,
254         .remove         = NULL,
255         .id_table       = vrc4173_table,
256 };
257
258 static int __devinit vrc4173_init(void)
259 {
260         int err;
261
262         if ((err = pci_module_init(&vrc4173_driver)) < 0)
263                 return err;
264
265         vrc4173_initialized = 1;
266
267         return 0;
268 }
269
270 static void __devexit vrc4173_exit(void)
271 {
272         vrc4173_initialized = 0;
273
274         pci_unregister_driver(&vrc4173_driver);
275 }
276
277 module_init(vrc4173_init);
278 module_exit(vrc4173_exit);