2 comedi/drivers/cb_pcimdas.c
3 Comedi driver for Computer Boards PCIM-DAS1602/16
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: Measurement Computing PCI Migration series boards
26 Devices: [ComputerBoards] PCIM-DAS1602/16 (cb_pcimdas)
27 Author: Richard Bytheway
28 Updated: Wed, 13 Nov 2002 12:34:56 +0000
31 Written to support the PCIM-DAS1602/16 on a 2.4 series kernel.
33 Configuration Options:
37 Developed from cb_pcidas and skel by Richard Bytheway (mocelet@sucs.org).
38 Only supports DIO, AO and simple AI in it's present form.
39 No interrupts, multi channel or FIFO AI, although the card looks like it could support this.
40 See http://www.mccdaq.com/PDFs/Manuals/pcim-das1602-16.pdf for more details.
43 #include "../comedidev.h"
45 #include <linux/delay.h>
46 #include <linux/interrupt.h>
51 /* #define CBPCIMDAS_DEBUG */
52 #undef CBPCIMDAS_DEBUG
54 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
56 /* Registers for the PCIM-DAS1602/16 */
58 /* sizes of io regions (bytes) */
66 /* AI and Counter Constants */
68 #define MAIN_CONN_DIO 1
70 #define ADC_CONV_STAT 3
75 #define CLK8254_1_DATA 8
76 #define CLK8254_2_DATA 9
77 #define CLK8254_3_DATA 10
78 #define CLK8254_CONTROL 11
79 #define USER_COUNTER 12
80 #define RESID_COUNT_H 13
81 #define RESID_COUNT_L 14
83 /* Board description */
84 struct cb_pcimdas_board
{
86 unsigned short device_id
;
87 int ai_se_chans
; /* Inputs in single-ended mode */
88 int ai_diff_chans
; /* Inputs in differential mode */
89 int ai_bits
; /* analog input resolution */
90 int ai_speed
; /* fastest conversion period in ns */
91 int ao_nchan
; /* number of analog out channels */
92 int ao_bits
; /* analogue output resolution */
93 int has_ao_fifo
; /* analog output has fifo */
94 int ao_scan_speed
; /* analog output speed for 1602 series (for a scan, not conversion) */
95 int fifo_size
; /* number of samples fifo can hold */
96 int dio_bits
; /* number of dio bits */
97 int has_dio
; /* has DIO */
98 const struct comedi_lrange
*ranges
;
101 static const struct cb_pcimdas_board cb_pcimdas_boards
[] = {
103 .name
= "PCIM-DAS1602/16",
108 .ai_speed
= 10000, /* ?? */
111 .has_ao_fifo
= 0, /* ?? */
112 .ao_scan_speed
= 10000,
117 /* .ranges = &cb_pcimdas_ranges, */
122 * this structure is for data unique to this hardware driver. If
123 * several hardware drivers keep similar information in this structure,
124 * feel free to suggest moving the variable to the struct comedi_device
127 struct cb_pcimdas_private
{
131 /* Used for AO readback */
132 unsigned int ao_readback
[2];
136 * "instructions" read/write data in "one-shot" or "software-triggered"
139 static int cb_pcimdas_ai_rinsn(struct comedi_device
*dev
,
140 struct comedi_subdevice
*s
,
141 struct comedi_insn
*insn
, unsigned int *data
)
143 const struct cb_pcimdas_board
*thisboard
= comedi_board(dev
);
144 struct cb_pcimdas_private
*devpriv
= dev
->private;
148 int chan
= CR_CHAN(insn
->chanspec
);
149 unsigned short chanlims
;
152 /* only support sw initiated reads from a single channel */
154 /* check channel number */
155 if ((inb(devpriv
->BADR3
+ 2) & 0x20) == 0) /* differential mode */
156 maxchans
= thisboard
->ai_diff_chans
;
158 maxchans
= thisboard
->ai_se_chans
;
160 if (chan
> (maxchans
- 1))
161 return -ETIMEDOUT
; /* *** Wrong error code. Fixme. */
163 /* configure for sw initiated read */
164 d
= inb(devpriv
->BADR3
+ 5);
165 if ((d
& 0x03) > 0) { /* only reset if needed. */
167 outb(d
, devpriv
->BADR3
+ 5);
169 outb(0x01, devpriv
->BADR3
+ 6); /* set bursting off, conversions on */
170 outb(0x00, devpriv
->BADR3
+ 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
173 * write channel limits to multiplexer, set Low (bits 0-3) and
174 * High (bits 4-7) channels to chan.
176 chanlims
= chan
| (chan
<< 4);
177 outb(chanlims
, devpriv
->BADR3
+ 0);
179 /* convert n samples */
180 for (n
= 0; n
< insn
->n
; n
++) {
181 /* trigger conversion */
182 outw(0, dev
->iobase
+ 0);
184 #define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
185 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
187 /* wait for conversion to end */
188 for (i
= 0; i
< TIMEOUT
; i
++) {
189 busy
= inb(devpriv
->BADR3
+ 2) & 0x80;
198 d
= inw(dev
->iobase
+ 0);
200 /* mangle the data as necessary */
201 /* d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed. */
206 /* return the number of samples read/written */
210 static int cb_pcimdas_ao_winsn(struct comedi_device
*dev
,
211 struct comedi_subdevice
*s
,
212 struct comedi_insn
*insn
, unsigned int *data
)
214 struct cb_pcimdas_private
*devpriv
= dev
->private;
216 int chan
= CR_CHAN(insn
->chanspec
);
218 /* Writing a list of values to an AO channel is probably not
219 * very useful, but that's how the interface is defined. */
220 for (i
= 0; i
< insn
->n
; i
++) {
223 outw(data
[i
] & 0x0FFF, dev
->iobase
+ DAC0_OFFSET
);
226 outw(data
[i
] & 0x0FFF, dev
->iobase
+ DAC1_OFFSET
);
231 devpriv
->ao_readback
[chan
] = data
[i
];
234 /* return the number of samples read/written */
238 /* AO subdevices should have a read insn as well as a write insn.
239 * Usually this means copying a value stored in devpriv. */
240 static int cb_pcimdas_ao_rinsn(struct comedi_device
*dev
,
241 struct comedi_subdevice
*s
,
242 struct comedi_insn
*insn
, unsigned int *data
)
244 struct cb_pcimdas_private
*devpriv
= dev
->private;
246 int chan
= CR_CHAN(insn
->chanspec
);
248 for (i
= 0; i
< insn
->n
; i
++)
249 data
[i
] = devpriv
->ao_readback
[chan
];
254 static const void *cb_pcimdas_find_boardinfo(struct comedi_device
*dev
,
255 struct pci_dev
*pcidev
)
257 const struct cb_pcimdas_board
*thisboard
;
260 for (i
= 0; i
< ARRAY_SIZE(cb_pcimdas_boards
); i
++) {
261 thisboard
= &cb_pcimdas_boards
[i
];
262 if (thisboard
->device_id
== pcidev
->device
)
268 static int cb_pcimdas_attach_pci(struct comedi_device
*dev
,
269 struct pci_dev
*pcidev
)
271 const struct cb_pcimdas_board
*thisboard
;
272 struct cb_pcimdas_private
*devpriv
;
273 struct comedi_subdevice
*s
;
274 unsigned long iobase_8255
;
277 comedi_set_hw_dev(dev
, &pcidev
->dev
);
279 thisboard
= cb_pcimdas_find_boardinfo(dev
, pcidev
);
282 dev
->board_ptr
= thisboard
;
283 dev
->board_name
= thisboard
->name
;
285 ret
= alloc_private(dev
, sizeof(*devpriv
));
288 devpriv
= dev
->private;
290 /* Warn about non-tested features */
291 switch (thisboard
->device_id
) {
295 dev_dbg(dev
->class_dev
, "THIS CARD IS UNSUPPORTED.\n");
296 dev_dbg(dev
->class_dev
,
297 "PLEASE REPORT USAGE TO <mocelet@sucs.org>\n");
300 ret
= comedi_pci_enable(pcidev
, dev
->board_name
);
304 dev
->iobase
= pci_resource_start(pcidev
, 2);
305 devpriv
->BADR3
= pci_resource_start(pcidev
, 3);
306 iobase_8255
= pci_resource_start(pcidev
, 4);
308 /* Dont support IRQ yet */
310 /* if(request_irq(pcidev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */
312 /* printk(" unable to allocate irq %u\n", pcidev->irq); */
313 /* return -EINVAL; */
315 /* dev->irq = pcidev->irq; */
317 ret
= comedi_alloc_subdevices(dev
, 3);
321 s
= dev
->subdevices
+ 0;
322 /* dev->read_subdev=s; */
323 /* analog input subdevice */
324 s
->type
= COMEDI_SUBD_AI
;
325 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
326 s
->n_chan
= thisboard
->ai_se_chans
;
327 s
->maxdata
= (1 << thisboard
->ai_bits
) - 1;
328 s
->range_table
= &range_unknown
;
329 s
->len_chanlist
= 1; /* This is the maximum chanlist length that */
330 /* the board can handle */
331 s
->insn_read
= cb_pcimdas_ai_rinsn
;
333 s
= dev
->subdevices
+ 1;
334 /* analog output subdevice */
335 s
->type
= COMEDI_SUBD_AO
;
336 s
->subdev_flags
= SDF_WRITABLE
;
337 s
->n_chan
= thisboard
->ao_nchan
;
338 s
->maxdata
= 1 << thisboard
->ao_bits
;
339 /* ranges are hardware settable, but not software readable. */
340 s
->range_table
= &range_unknown
;
341 s
->insn_write
= &cb_pcimdas_ao_winsn
;
342 s
->insn_read
= &cb_pcimdas_ao_rinsn
;
344 s
= dev
->subdevices
+ 2;
345 /* digital i/o subdevice */
346 if (thisboard
->has_dio
)
347 subdev_8255_init(dev
, s
, NULL
, iobase_8255
);
349 s
->type
= COMEDI_SUBD_UNUSED
;
351 dev_info(dev
->class_dev
, "%s attached\n", dev
->board_name
);
356 static void cb_pcimdas_detach(struct comedi_device
*dev
)
358 struct pci_dev
*pcidev
= comedi_to_pci_dev(dev
);
361 free_irq(dev
->irq
, dev
);
364 comedi_pci_disable(pcidev
);
368 static struct comedi_driver cb_pcimdas_driver
= {
369 .driver_name
= "cb_pcimdas",
370 .module
= THIS_MODULE
,
371 .attach_pci
= cb_pcimdas_attach_pci
,
372 .detach
= cb_pcimdas_detach
,
375 static int __devinit
cb_pcimdas_pci_probe(struct pci_dev
*dev
,
376 const struct pci_device_id
*ent
)
378 return comedi_pci_auto_config(dev
, &cb_pcimdas_driver
);
381 static void __devexit
cb_pcimdas_pci_remove(struct pci_dev
*dev
)
383 comedi_pci_auto_unconfig(dev
);
386 static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table
) = {
387 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTERBOARDS
, 0x0056) },
390 MODULE_DEVICE_TABLE(pci
, cb_pcimdas_pci_table
);
392 static struct pci_driver cb_pcimdas_pci_driver
= {
393 .name
= "cb_pcimdas",
394 .id_table
= cb_pcimdas_pci_table
,
395 .probe
= cb_pcimdas_pci_probe
,
396 .remove
= __devexit_p(cb_pcimdas_pci_remove
),
398 module_comedi_pci_driver(cb_pcimdas_driver
, cb_pcimdas_pci_driver
);
400 MODULE_AUTHOR("Comedi http://www.comedi.org");
401 MODULE_DESCRIPTION("Comedi low-level driver");
402 MODULE_LICENSE("GPL");