2 * comedi/drivers/mf6x4.c
3 * Driver for Humusoft MF634 and MF624 Data acquisition cards
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 * Description: Humusoft MF634 and MF624 Data acquisition card driver
21 * Devices: [Humusoft] MF634 (mf634), MF624 (mf624)
22 * Author: Rostislav Lisovy <lisovy@gmail.com>
25 * Configuration Options: none
28 #include <linux/module.h>
29 #include <linux/delay.h>
31 #include "../comedi_pci.h"
33 /* Registers present in BAR0 memory region */
34 #define MF624_GPIOC_REG 0x54
36 #define MF6X4_GPIOC_EOLC BIT(17) /* End Of Last Conversion */
37 #define MF6X4_GPIOC_LDAC BIT(23) /* Load DACs */
38 #define MF6X4_GPIOC_DACEN BIT(26)
41 #define MF6X4_ADDATA_REG 0x00
42 #define MF6X4_ADCTRL_REG 0x00
43 #define MF6X4_ADCTRL_CHAN(x) BIT(chan)
44 #define MF6X4_DIN_REG 0x10
45 #define MF6X4_DIN_MASK 0xff
46 #define MF6X4_DOUT_REG 0x10
47 #define MF6X4_ADSTART_REG 0x20
48 #define MF6X4_DAC_REG(x) (0x20 + ((x) * 2))
51 #define MF634_GPIOC_REG 0x68
60 /* We need to keep track of the order of BARs used by the cards */
61 unsigned int bar_nums[3];
64 static const struct mf6x4_board mf6x4_boards[] = {
67 .bar_nums = {0, 2, 3},
71 .bar_nums = {0, 2, 4},
75 struct mf6x4_private {
77 * Documentation for both MF634 and MF624 describes registers
78 * present in BAR0, 1 and 2 regions.
79 * The real (i.e. in HW) BAR numbers are different for MF624
80 * and MF634 yet we will call them 0, 1, 2
82 void __iomem *bar0_mem;
83 void __iomem *bar2_mem;
86 * This configuration register has the same function and fields
87 * for both cards however it lies in different BARs on different
88 * offsets -- this variable makes the access easier
90 void __iomem *gpioc_reg;
93 static int mf6x4_di_insn_bits(struct comedi_device *dev,
94 struct comedi_subdevice *s,
95 struct comedi_insn *insn,
98 data[1] = ioread16(dev->mmio + MF6X4_DIN_REG) & MF6X4_DIN_MASK;
103 static int mf6x4_do_insn_bits(struct comedi_device *dev,
104 struct comedi_subdevice *s,
105 struct comedi_insn *insn,
108 if (comedi_dio_update_state(s, data))
109 iowrite16(s->state, dev->mmio + MF6X4_DOUT_REG);
116 static int mf6x4_ai_eoc(struct comedi_device *dev,
117 struct comedi_subdevice *s,
118 struct comedi_insn *insn,
119 unsigned long context)
121 struct mf6x4_private *devpriv = dev->private;
124 /* EOLC goes low at end of conversion. */
125 status = ioread32(devpriv->gpioc_reg);
126 if ((status & MF6X4_GPIOC_EOLC) == 0)
131 static int mf6x4_ai_insn_read(struct comedi_device *dev,
132 struct comedi_subdevice *s,
133 struct comedi_insn *insn,
136 unsigned int chan = CR_CHAN(insn->chanspec);
141 /* Set the ADC channel number in the scan list */
142 iowrite16(MF6X4_ADCTRL_CHAN(chan), dev->mmio + MF6X4_ADCTRL_REG);
144 for (i = 0; i < insn->n; i++) {
145 /* Trigger ADC conversion by reading ADSTART */
146 ioread16(dev->mmio + MF6X4_ADSTART_REG);
148 ret = comedi_timeout(dev, s, insn, mf6x4_ai_eoc, 0);
152 /* Read the actual value */
153 d = ioread16(dev->mmio + MF6X4_ADDATA_REG);
155 /* munge the 2's complement data to offset binary */
156 data[i] = comedi_offset_munge(s, d);
159 iowrite16(0x0, dev->mmio + MF6X4_ADCTRL_REG);
164 static int mf6x4_ao_insn_write(struct comedi_device *dev,
165 struct comedi_subdevice *s,
166 struct comedi_insn *insn,
169 struct mf6x4_private *devpriv = dev->private;
170 unsigned int chan = CR_CHAN(insn->chanspec);
171 unsigned int val = s->readback[chan];
175 /* Enable instantaneous update of converters outputs + Enable DACs */
176 gpioc = ioread32(devpriv->gpioc_reg);
177 iowrite32((gpioc & ~MF6X4_GPIOC_LDAC) | MF6X4_GPIOC_DACEN,
180 for (i = 0; i < insn->n; i++) {
182 iowrite16(val, dev->mmio + MF6X4_DAC_REG(chan));
184 s->readback[chan] = val;
189 static int mf6x4_auto_attach(struct comedi_device *dev, unsigned long context)
191 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
192 const struct mf6x4_board *board = NULL;
193 struct mf6x4_private *devpriv;
194 struct comedi_subdevice *s;
197 if (context < ARRAY_SIZE(mf6x4_boards))
198 board = &mf6x4_boards[context];
202 dev->board_ptr = board;
203 dev->board_name = board->name;
205 ret = comedi_pci_enable(dev);
209 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
213 devpriv->bar0_mem = pci_ioremap_bar(pcidev, board->bar_nums[0]);
214 if (!devpriv->bar0_mem)
217 dev->mmio = pci_ioremap_bar(pcidev, board->bar_nums[1]);
221 devpriv->bar2_mem = pci_ioremap_bar(pcidev, board->bar_nums[2]);
222 if (!devpriv->bar2_mem)
225 if (board == &mf6x4_boards[BOARD_MF634])
226 devpriv->gpioc_reg = devpriv->bar2_mem + MF634_GPIOC_REG;
228 devpriv->gpioc_reg = devpriv->bar0_mem + MF624_GPIOC_REG;
230 ret = comedi_alloc_subdevices(dev, 4);
234 /* Analog Input subdevice */
235 s = &dev->subdevices[0];
236 s->type = COMEDI_SUBD_AI;
237 s->subdev_flags = SDF_READABLE | SDF_GROUND;
240 s->range_table = &range_bipolar10;
241 s->insn_read = mf6x4_ai_insn_read;
243 /* Analog Output subdevice */
244 s = &dev->subdevices[1];
245 s->type = COMEDI_SUBD_AO;
246 s->subdev_flags = SDF_WRITABLE;
249 s->range_table = &range_bipolar10;
250 s->insn_write = mf6x4_ao_insn_write;
252 ret = comedi_alloc_subdev_readback(s);
256 /* Digital Input subdevice */
257 s = &dev->subdevices[2];
258 s->type = COMEDI_SUBD_DI;
259 s->subdev_flags = SDF_READABLE;
262 s->range_table = &range_digital;
263 s->insn_bits = mf6x4_di_insn_bits;
265 /* Digital Output subdevice */
266 s = &dev->subdevices[3];
267 s->type = COMEDI_SUBD_DO;
268 s->subdev_flags = SDF_WRITABLE;
271 s->range_table = &range_digital;
272 s->insn_bits = mf6x4_do_insn_bits;
277 static void mf6x4_detach(struct comedi_device *dev)
279 struct mf6x4_private *devpriv = dev->private;
282 if (devpriv->bar0_mem)
283 iounmap(devpriv->bar0_mem);
284 if (devpriv->bar2_mem)
285 iounmap(devpriv->bar2_mem);
287 comedi_pci_detach(dev);
290 static struct comedi_driver mf6x4_driver = {
291 .driver_name = "mf6x4",
292 .module = THIS_MODULE,
293 .auto_attach = mf6x4_auto_attach,
294 .detach = mf6x4_detach,
297 static int mf6x4_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
299 return comedi_pci_auto_config(dev, &mf6x4_driver, id->driver_data);
302 static const struct pci_device_id mf6x4_pci_table[] = {
303 { PCI_VDEVICE(HUMUSOFT, 0x0634), BOARD_MF634 },
304 { PCI_VDEVICE(HUMUSOFT, 0x0624), BOARD_MF624 },
307 MODULE_DEVICE_TABLE(pci, mf6x4_pci_table);
309 static struct pci_driver mf6x4_pci_driver = {
311 .id_table = mf6x4_pci_table,
312 .probe = mf6x4_pci_probe,
313 .remove = comedi_pci_auto_unconfig,
316 module_comedi_pci_driver(mf6x4_driver, mf6x4_pci_driver);
318 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.com>");
319 MODULE_DESCRIPTION("Comedi MF634 and MF624 DAQ cards driver");
320 MODULE_LICENSE("GPL");