1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * UIO driver fo Humusoft MF624 DAQ card.
4 * Copyright (C) 2011 Rostislav Lisovy <lisovy@gmail.com>,
5 * Czech Technical University in Prague
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/pci.h>
12 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/uio_driver.h>
17 #define PCI_VENDOR_ID_HUMUSOFT 0x186c
18 #define PCI_DEVICE_ID_MF624 0x0624
19 #define PCI_SUBVENDOR_ID_HUMUSOFT 0x186c
20 #define PCI_SUBDEVICE_DEVICE 0x0624
22 /* BAR0 Interrupt control/status register */
24 #define INTCSR_ADINT_ENABLE (1 << 0)
25 #define INTCSR_CTR4INT_ENABLE (1 << 3)
26 #define INTCSR_PCIINT_ENABLE (1 << 6)
27 #define INTCSR_ADINT_STATUS (1 << 2)
28 #define INTCSR_CTR4INT_STATUS (1 << 5)
30 enum mf624_interrupt_source
{ADC
, CTR4
, ALL
};
32 static void mf624_disable_interrupt(enum mf624_interrupt_source source
,
33 struct uio_info
*info
)
35 void __iomem
*INTCSR_reg
= info
->mem
[0].internal_addr
+ INTCSR
;
39 iowrite32(ioread32(INTCSR_reg
)
40 & ~(INTCSR_ADINT_ENABLE
| INTCSR_PCIINT_ENABLE
),
45 iowrite32(ioread32(INTCSR_reg
)
46 & ~(INTCSR_CTR4INT_ENABLE
| INTCSR_PCIINT_ENABLE
),
52 iowrite32(ioread32(INTCSR_reg
)
53 & ~(INTCSR_ADINT_ENABLE
| INTCSR_CTR4INT_ENABLE
54 | INTCSR_PCIINT_ENABLE
),
60 static void mf624_enable_interrupt(enum mf624_interrupt_source source
,
61 struct uio_info
*info
)
63 void __iomem
*INTCSR_reg
= info
->mem
[0].internal_addr
+ INTCSR
;
67 iowrite32(ioread32(INTCSR_reg
)
68 | INTCSR_ADINT_ENABLE
| INTCSR_PCIINT_ENABLE
,
73 iowrite32(ioread32(INTCSR_reg
)
74 | INTCSR_CTR4INT_ENABLE
| INTCSR_PCIINT_ENABLE
,
80 iowrite32(ioread32(INTCSR_reg
)
81 | INTCSR_ADINT_ENABLE
| INTCSR_CTR4INT_ENABLE
82 | INTCSR_PCIINT_ENABLE
,
88 static irqreturn_t
mf624_irq_handler(int irq
, struct uio_info
*info
)
90 void __iomem
*INTCSR_reg
= info
->mem
[0].internal_addr
+ INTCSR
;
92 if ((ioread32(INTCSR_reg
) & INTCSR_ADINT_ENABLE
)
93 && (ioread32(INTCSR_reg
) & INTCSR_ADINT_STATUS
)) {
94 mf624_disable_interrupt(ADC
, info
);
98 if ((ioread32(INTCSR_reg
) & INTCSR_CTR4INT_ENABLE
)
99 && (ioread32(INTCSR_reg
) & INTCSR_CTR4INT_STATUS
)) {
100 mf624_disable_interrupt(CTR4
, info
);
107 static int mf624_irqcontrol(struct uio_info
*info
, s32 irq_on
)
110 mf624_disable_interrupt(ALL
, info
);
111 else if (irq_on
== 1)
112 mf624_enable_interrupt(ALL
, info
);
117 static int mf624_setup_mem(struct pci_dev
*dev
, int bar
, struct uio_mem
*mem
, const char *name
)
119 resource_size_t start
= pci_resource_start(dev
, bar
);
120 resource_size_t len
= pci_resource_len(dev
, bar
);
123 mem
->addr
= start
& PAGE_MASK
;
124 mem
->offs
= start
& ~PAGE_MASK
;
127 mem
->size
= ((start
& ~PAGE_MASK
) + len
+ PAGE_SIZE
- 1) & PAGE_MASK
;
128 mem
->memtype
= UIO_MEM_PHYS
;
129 mem
->internal_addr
= pci_ioremap_bar(dev
, bar
);
130 if (!mem
->internal_addr
)
135 static int mf624_pci_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
137 struct uio_info
*info
;
139 info
= devm_kzalloc(&dev
->dev
, sizeof(struct uio_info
), GFP_KERNEL
);
143 if (pci_enable_device(dev
))
146 if (pci_request_regions(dev
, "mf624"))
149 info
->name
= "mf624";
150 info
->version
= "0.0.1";
152 /* Note: Datasheet says device uses BAR0, BAR1, BAR2 -- do not trust it */
155 if (mf624_setup_mem(dev
, 0, &info
->mem
[0], "PCI chipset, interrupts, status "
156 "bits, special functions"))
159 if (mf624_setup_mem(dev
, 2, &info
->mem
[1], "ADC, DAC, DIO"))
163 if (mf624_setup_mem(dev
, 4, &info
->mem
[2], "Counter/timer chip"))
166 info
->irq
= dev
->irq
;
167 info
->irq_flags
= IRQF_SHARED
;
168 info
->handler
= mf624_irq_handler
;
170 info
->irqcontrol
= mf624_irqcontrol
;
172 if (uio_register_device(&dev
->dev
, info
))
175 pci_set_drvdata(dev
, info
);
180 iounmap(info
->mem
[2].internal_addr
);
182 iounmap(info
->mem
[1].internal_addr
);
184 iounmap(info
->mem
[0].internal_addr
);
187 pci_release_regions(dev
);
190 pci_disable_device(dev
);
195 static void mf624_pci_remove(struct pci_dev
*dev
)
197 struct uio_info
*info
= pci_get_drvdata(dev
);
199 mf624_disable_interrupt(ALL
, info
);
201 uio_unregister_device(info
);
202 pci_release_regions(dev
);
203 pci_disable_device(dev
);
205 iounmap(info
->mem
[0].internal_addr
);
206 iounmap(info
->mem
[1].internal_addr
);
207 iounmap(info
->mem
[2].internal_addr
);
210 static const struct pci_device_id mf624_pci_id
[] = {
211 { PCI_DEVICE(PCI_VENDOR_ID_HUMUSOFT
, PCI_DEVICE_ID_MF624
) },
215 static struct pci_driver mf624_pci_driver
= {
217 .id_table
= mf624_pci_id
,
218 .probe
= mf624_pci_probe
,
219 .remove
= mf624_pci_remove
,
221 MODULE_DEVICE_TABLE(pci
, mf624_pci_id
);
223 module_pci_driver(mf624_pci_driver
);
224 MODULE_LICENSE("GPL v2");
225 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.com>");