]> rtime.felk.cvut.cz Git - mf6xx.git/blob - src/uio/mf624/kernel/mf624.c
1c88cf97cb29526d1567201607b09aa9733560a5
[mf6xx.git] / src / uio / mf624 / kernel / mf624.c
1 /*
2  * UIO driver fo Humusoft MF624 DAQ card.
3  * Copyright (C) 2011 Rostislav Lisovy <lisovy@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/io.h>
26 #include <linux/kernel.h>
27 #include <linux/uio_driver.h>
28
29 #define PCI_VENDOR_ID_HUMUSOFT          0x186c
30 #define PCI_DEVICE_ID_MF624             0x0624
31 #define PCI_SUBVENDOR_ID_HUMUSOFT       0x186c
32 #define PCI_SUBDEVICE_DEVICE            0x0624
33
34 /* BAR0 Interrupt control/status register */
35 #define INTCSR                          0x4C
36 #define INTCSR_ADINT_ENABLE             (1 << 0)
37 #define INTCSR_CTR4INT_ENABLE           (1 << 3)
38 #define INTCSR_PCIINT_ENABLE            (1 << 6)
39 #define INTCSR_ADINT_STATUS             (1 << 2)
40 #define INTCSR_CTR4INT_STATUS           (1 << 5)
41
42 enum mf624_interrupt_source {ADC, CTR4, ALL};
43
44 void mf624_disable_interrupt(enum mf624_interrupt_source source,
45                              struct uio_info *info)
46 {
47         void __iomem *INTCSR_reg = info->mem[0].internal_addr + INTCSR;
48
49         switch (source) {
50         case ADC:
51                 iowrite32(ioread32(INTCSR_reg)
52                         & ~(INTCSR_ADINT_ENABLE | INTCSR_PCIINT_ENABLE),
53                         INTCSR_reg);
54                 break;
55
56         case CTR4:
57                 iowrite32(ioread32(INTCSR_reg)
58                         & ~(INTCSR_CTR4INT_ENABLE | INTCSR_PCIINT_ENABLE),
59                         INTCSR_reg);
60                 break;
61
62         case ALL:
63         default:
64                 iowrite32(ioread32(INTCSR_reg)
65                         & ~(INTCSR_ADINT_ENABLE | INTCSR_CTR4INT_ENABLE
66                             | INTCSR_PCIINT_ENABLE),
67                         INTCSR_reg);
68                 break;
69         }
70 }
71
72 void mf624_enable_interrupt(enum mf624_interrupt_source source,
73                             struct uio_info *info)
74 {
75         void __iomem *INTCSR_reg = info->mem[0].internal_addr + INTCSR;
76
77         switch (source) {
78         case ADC:
79                 iowrite32(ioread32(INTCSR_reg)
80                         | INTCSR_ADINT_ENABLE | INTCSR_PCIINT_ENABLE,
81                         INTCSR_reg);
82                 break;
83
84         case CTR4:
85                 iowrite32(ioread32(INTCSR_reg)
86                         | INTCSR_CTR4INT_ENABLE | INTCSR_PCIINT_ENABLE,
87                         INTCSR_reg);
88                 break;
89
90         case ALL:
91         default:
92                 iowrite32(ioread32(INTCSR_reg)
93                         | INTCSR_ADINT_ENABLE | INTCSR_CTR4INT_ENABLE
94                         | INTCSR_PCIINT_ENABLE,
95                         INTCSR_reg);
96                 break;
97         }
98 }
99
100 static irqreturn_t mf624_irq_handler(int irq, struct uio_info *info)
101 {
102         void __iomem *INTCSR_reg = info->mem[0].internal_addr + INTCSR;
103
104         if ((ioread32(INTCSR_reg) & INTCSR_ADINT_ENABLE)
105             && (ioread32(INTCSR_reg) & INTCSR_ADINT_STATUS)) {
106                 mf624_disable_interrupt(ADC, info);
107                 return IRQ_HANDLED;
108         }
109
110         if ((ioread32(INTCSR_reg) & INTCSR_CTR4INT_ENABLE)
111             && (ioread32(INTCSR_reg) & INTCSR_CTR4INT_STATUS)) {
112                 mf624_disable_interrupt(CTR4, info);
113                 return IRQ_HANDLED;
114         }
115
116         return IRQ_NONE;
117 }
118
119 static int mf624_irqcontrol(struct uio_info *info, s32 irq_on)
120 {
121         if (irq_on == 0)
122                 mf624_disable_interrupt(ALL, info);
123         else if (irq_on == 1)
124                 mf624_enable_interrupt(ALL, info);
125
126         return 0;
127 }
128
129 static int mf624_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
130 {
131         struct uio_info *info;
132
133         info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
134         if (!info)
135                 return -ENOMEM;
136
137         if (pci_enable_device(dev))
138                 goto out_free;
139
140         if (pci_request_regions(dev, "mf624"))
141                 goto out_disable;
142
143         info->name = "mf624";
144         info->version = "0.0.1";
145
146         /* Note: Datasheet says device uses BAR0, BAR1, BAR2 -- do not trust it */
147
148         /* BAR0 */
149         info->mem[0].name = "PCI chipset, interrupts, status "
150                         "bits, special functions";
151         info->mem[0].addr = pci_resource_start(dev, 0);
152         if (!info->mem[0].addr)
153                 goto out_release;
154         info->mem[0].size = pci_resource_len(dev, 0);
155         info->mem[0].memtype = UIO_MEM_PHYS;
156         info->mem[0].internal_addr = pci_ioremap_bar(dev, 0);
157         if (!info->mem[0].internal_addr)
158                 goto out_release;
159
160         /* BAR2 */
161         info->mem[1].name = "ADC, DAC, DIO";
162         info->mem[1].addr = pci_resource_start(dev, 2);
163         if (!info->mem[1].addr)
164                 goto out_unmap0;
165         info->mem[1].size = pci_resource_len(dev, 2);
166         info->mem[1].memtype = UIO_MEM_PHYS;
167         info->mem[1].internal_addr = pci_ioremap_bar(dev, 2);
168         if (!info->mem[1].internal_addr)
169                 goto out_unmap0;
170
171         /* BAR4 */
172         info->mem[2].name = "Counter/timer chip";
173         info->mem[2].addr = pci_resource_start(dev, 4);
174         if (!info->mem[2].addr)
175                 goto out_unmap1;
176         info->mem[2].size = pci_resource_len(dev, 4);
177         info->mem[2].memtype = UIO_MEM_PHYS;
178         info->mem[2].internal_addr = pci_ioremap_bar(dev, 4);
179         if (!info->mem[2].internal_addr)
180                 goto out_unmap1;
181
182         info->irq = dev->irq;
183         info->irq_flags = IRQF_SHARED;
184         info->handler = mf624_irq_handler;
185
186         info->irqcontrol = mf624_irqcontrol;
187
188         if (uio_register_device(&dev->dev, info))
189                 goto out_unmap2;
190
191         pci_set_drvdata(dev, info);
192
193         return 0;
194
195 out_unmap2:
196         iounmap(info->mem[2].internal_addr);
197 out_unmap1:
198         iounmap(info->mem[1].internal_addr);
199 out_unmap0:
200         iounmap(info->mem[0].internal_addr);
201
202 out_release:
203         pci_release_regions(dev);
204
205 out_disable:
206         pci_disable_device(dev);
207
208 out_free:
209         kfree(info);
210         return -ENODEV;
211 }
212
213 static void mf624_pci_remove(struct pci_dev *dev)
214 {
215         struct uio_info *info = pci_get_drvdata(dev);
216
217         mf624_disable_interrupt(ALL, info);
218
219         uio_unregister_device(info);
220         pci_release_regions(dev);
221         pci_disable_device(dev);
222         pci_set_drvdata(dev, NULL);
223
224         iounmap(info->mem[0].internal_addr);
225         iounmap(info->mem[1].internal_addr);
226         iounmap(info->mem[2].internal_addr);
227
228         kfree(info);
229 }
230
231 static DEFINE_PCI_DEVICE_TABLE(mf624_pci_id) = {
232         { PCI_DEVICE(PCI_VENDOR_ID_HUMUSOFT, PCI_DEVICE_ID_MF624) },
233         { 0, }
234 };
235
236 static struct pci_driver mf624_pci_driver = {
237         .name = "mf624",
238         .id_table = mf624_pci_id,
239         .probe = mf624_pci_probe,
240         .remove = mf624_pci_remove,
241 };
242 MODULE_DEVICE_TABLE(pci, mf624_pci_id);
243
244 module_pci_driver(mf624_pci_driver);
245 MODULE_LICENSE("GPL v2");
246 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.com>");