]> rtime.felk.cvut.cz Git - mf6xx.git/blob - src/uio/mf624/kernel/mf624.c
uio: mf624: A bit of refactoring
[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) & ~(INTCSR_ADINT_ENABLE),
52                         INTCSR_reg);
53                 break;
54
55         case CTR4:
56                 iowrite32(ioread32(INTCSR_reg) & ~(INTCSR_CTR4INT_ENABLE),
57                         INTCSR_reg);
58                 break;
59
60         case ALL:
61         default:
62                 iowrite32(ioread32(INTCSR_reg)
63                         & ~(INTCSR_ADINT_ENABLE | INTCSR_CTR4INT_ENABLE),
64                         INTCSR_reg);
65                 break;
66         }
67 }
68
69 void mf624_enable_interrupt(enum mf624_interrupt_source source,
70                             struct uio_info *info)
71 {
72         void __iomem *INTCSR_reg = info->mem[0].internal_addr + INTCSR;
73
74         switch (source) {
75         case ADC:
76                 iowrite32(ioread32(INTCSR_reg) | (INTCSR_ADINT_ENABLE),
77                         INTCSR_reg);
78                 break;
79
80         case CTR4:
81                 iowrite32(ioread32(INTCSR_reg) | (INTCSR_CTR4INT_ENABLE),
82                         INTCSR_reg);
83                 break;
84
85         case ALL:
86         default:
87                 iowrite32(ioread32(INTCSR_reg)
88                         | (INTCSR_ADINT_ENABLE | INTCSR_CTR4INT_ENABLE),
89                         INTCSR_reg);
90                 break;
91         }
92 }
93
94 static irqreturn_t mf624_irq_handler(int irq, struct uio_info *info)
95 {
96         u8 __iomem *INTCSR_reg = (u8 *)info->mem[0].internal_addr + INTCSR;
97
98         if ((ioread32(INTCSR_reg) & INTCSR_ADINT_ENABLE)
99             && (ioread32(INTCSR_reg) & INTCSR_ADINT_STATUS)) {
100                 mf624_disable_interrupt(ADC, info);
101                 return IRQ_HANDLED;
102         }
103
104         if ((ioread32(INTCSR_reg) & INTCSR_CTR4INT_ENABLE)
105             && (ioread32(INTCSR_reg) & INTCSR_CTR4INT_STATUS)) {
106                 mf624_disable_interrupt(CTR4, info);
107                 return IRQ_HANDLED;
108         }
109
110         return IRQ_NONE;
111 }
112
113 static int mf624_irqcontrol(struct uio_info *info, s32 irq_on)
114 {
115         if (irq_on == 0)
116                 mf624_disable_interrupt(ALL, info);
117         else if (irq_on == 1)
118                 mf624_enable_interrupt(ALL, info);
119
120         return 0;
121 }
122
123 static int mf624_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
124 {
125         struct uio_info *info;
126
127         info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
128         if (!info)
129                 return -ENOMEM;
130
131         if (pci_enable_device(dev))
132                 goto out_free;
133
134         if (pci_request_regions(dev, "mf624"))
135                 goto out_disable;
136
137         info->name = "mf624";
138         info->version = "0.0.1";
139
140         /* BAR0 */
141         info->mem[0].name = "PCI chipset, interrupts, status "
142                         "bits, special functions";
143         info->mem[0].addr = pci_resource_start(dev, 0);
144         if (!info->mem[0].addr)
145                 goto out_release;
146         info->mem[0].size = pci_resource_len(dev, 0);
147         info->mem[0].memtype = UIO_MEM_PHYS;
148         info->mem[0].internal_addr = pci_ioremap_bar(dev, 0);
149         if (!info->mem[0].internal_addr)
150                 goto out_release;
151
152         /* BAR2 */
153         info->mem[1].name = "ADC, DAC, DIO";
154         info->mem[1].addr = pci_resource_start(dev, 2);
155         if (!info->mem[1].addr)
156                 goto out_unmap0;
157         info->mem[1].size = pci_resource_len(dev, 2);
158         info->mem[1].memtype = UIO_MEM_PHYS;
159         info->mem[1].internal_addr = pci_ioremap_bar(dev, 2);
160         if (!info->mem[1].internal_addr)
161                 goto out_unmap0;
162
163         /* BAR4 */
164         info->mem[2].name = "Counter/timer chip";
165         info->mem[2].addr = pci_resource_start(dev, 4);
166         if (!info->mem[2].addr)
167                 goto out_unmap1;
168         info->mem[2].size = pci_resource_len(dev, 4);
169         info->mem[2].memtype = UIO_MEM_PHYS;
170         info->mem[2].internal_addr = pci_ioremap_bar(dev, 4);
171         if (!info->mem[2].internal_addr)
172                 goto out_unmap1;
173
174         info->irq = dev->irq;
175         info->irq_flags = IRQF_SHARED;
176         info->handler = mf624_irq_handler;
177
178         info->irqcontrol = mf624_irqcontrol;
179
180         if (uio_register_device(&dev->dev, info))
181                 goto out_unmap2;
182
183         pci_set_drvdata(dev, info);
184
185         return 0;
186
187 out_unmap2:
188         iounmap(info->mem[2].internal_addr);
189 out_unmap1:
190         iounmap(info->mem[1].internal_addr);
191 out_unmap0:
192         iounmap(info->mem[0].internal_addr);
193
194 out_release:
195         pci_release_regions(dev);
196
197 out_disable:
198         pci_disable_device(dev);
199
200 out_free:
201         kfree(info);
202         return -ENODEV;
203 }
204
205 static void mf624_pci_remove(struct pci_dev *dev)
206 {
207         struct uio_info *info = pci_get_drvdata(dev);
208
209         mf624_disable_interrupt(ALL, info);
210
211         uio_unregister_device(info);
212         pci_release_regions(dev);
213         pci_disable_device(dev);
214         pci_set_drvdata(dev, NULL);
215
216         iounmap(info->mem[0].internal_addr);
217         iounmap(info->mem[1].internal_addr);
218         iounmap(info->mem[2].internal_addr);
219
220         kfree(info);
221 }
222
223 static DEFINE_PCI_DEVICE_TABLE(mf624_pci_id) = {
224         { PCI_DEVICE(PCI_VENDOR_ID_HUMUSOFT, PCI_DEVICE_ID_MF624) },
225         { 0, }
226 };
227
228 static struct pci_driver mf624_pci_driver = {
229         .name = "mf624",
230         .id_table = mf624_pci_id,
231         .probe = mf624_pci_probe,
232         .remove = mf624_pci_remove,
233 };
234 MODULE_DEVICE_TABLE(pci, mf624_pci_id);
235
236 module_pci_driver(mf624_pci_driver);
237 MODULE_LICENSE("GPL v2");
238 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.com>");