Merge 'staging-next' to Linus's tree
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / comedi / drivers / cb_pcimdas.c
blob3d53df000cf11ee4f77601b8b0695dc28dd4afcf
1 /*
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.
24 Driver: cb_pcimdas
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
29 Status: experimental
31 Written to support the PCIM-DAS1602/16 on a 2.4 series kernel.
33 Configuration Options:
34 [0] - PCI bus number
35 [1] - PCI slot number
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>
48 #include "comedi_pci.h"
49 #include "plx9052.h"
50 #include "8255.h"
52 /* #define CBPCIMDAS_DEBUG */
53 #undef CBPCIMDAS_DEBUG
55 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
57 /* Registers for the PCIM-DAS1602/16 */
59 /* sizes of io regions (bytes) */
60 #define BADR0_SIZE 2 /* ?? */
61 #define BADR1_SIZE 4
62 #define BADR2_SIZE 6
63 #define BADR3_SIZE 16
64 #define BADR4_SIZE 4
66 /* DAC Offsets */
67 #define ADC_TRIG 0
68 #define DAC0_OFFSET 2
69 #define DAC1_OFFSET 4
71 /* AI and Counter Constants */
72 #define MUX_LIMITS 0
73 #define MAIN_CONN_DIO 1
74 #define ADC_STAT 2
75 #define ADC_CONV_STAT 3
76 #define ADC_INT 4
77 #define ADC_PACER 5
78 #define BURST_MODE 6
79 #define PROG_GAIN 7
80 #define CLK8254_1_DATA 8
81 #define CLK8254_2_DATA 9
82 #define CLK8254_3_DATA 10
83 #define CLK8254_CONTROL 11
84 #define USER_COUNTER 12
85 #define RESID_COUNT_H 13
86 #define RESID_COUNT_L 14
88 /* Board description */
89 struct cb_pcimdas_board {
90 const char *name;
91 unsigned short device_id;
92 int ai_se_chans; /* Inputs in single-ended mode */
93 int ai_diff_chans; /* Inputs in differential mode */
94 int ai_bits; /* analog input resolution */
95 int ai_speed; /* fastest conversion period in ns */
96 int ao_nchan; /* number of analog out channels */
97 int ao_bits; /* analogue output resolution */
98 int has_ao_fifo; /* analog output has fifo */
99 int ao_scan_speed; /* analog output speed for 1602 series (for a scan, not conversion) */
100 int fifo_size; /* number of samples fifo can hold */
101 int dio_bits; /* number of dio bits */
102 int has_dio; /* has DIO */
103 const struct comedi_lrange *ranges;
106 static const struct cb_pcimdas_board cb_pcimdas_boards[] = {
108 .name = "PCIM-DAS1602/16",
109 .device_id = 0x56,
110 .ai_se_chans = 16,
111 .ai_diff_chans = 8,
112 .ai_bits = 16,
113 .ai_speed = 10000, /* ?? */
114 .ao_nchan = 2,
115 .ao_bits = 12,
116 .has_ao_fifo = 0, /* ?? */
117 .ao_scan_speed = 10000,
118 /* ?? */
119 .fifo_size = 1024,
120 .dio_bits = 24,
121 .has_dio = 1,
122 /* .ranges = &cb_pcimdas_ranges, */
126 /* This is used by modprobe to translate PCI IDs to drivers. Should
127 * only be used for PCI and ISA-PnP devices */
128 static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table) = {
129 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTERBOARDS, 0x0056) },
130 { 0 }
133 MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
135 #define N_BOARDS 1 /* Max number of boards supported */
138 * Useful for shorthand access to the particular board structure
140 #define thisboard ((const struct cb_pcimdas_board *)dev->board_ptr)
142 /* this structure is for data unique to this hardware driver. If
143 several hardware drivers keep similar information in this structure,
144 feel free to suggest moving the variable to the struct comedi_device struct. */
145 struct cb_pcimdas_private {
146 int data;
148 /* would be useful for a PCI device */
149 struct pci_dev *pci_dev;
151 /* base addresses */
152 unsigned long BADR0;
153 unsigned long BADR1;
154 unsigned long BADR2;
155 unsigned long BADR3;
156 unsigned long BADR4;
158 /* Used for AO readback */
159 unsigned int ao_readback[2];
161 /* Used for DIO */
162 unsigned short int port_a; /* copy of BADR4+0 */
163 unsigned short int port_b; /* copy of BADR4+1 */
164 unsigned short int port_c; /* copy of BADR4+2 */
165 unsigned short int dio_mode; /* copy of BADR4+3 */
170 * most drivers define the following macro to make it easy to
171 * access the private structure.
173 #define devpriv ((struct cb_pcimdas_private *)dev->private)
176 * The struct comedi_driver structure tells the Comedi core module
177 * which functions to call to configure/deconfigure (attach/detach)
178 * the board, and also about the kernel module that contains
179 * the device code.
181 static int cb_pcimdas_attach(struct comedi_device *dev,
182 struct comedi_devconfig *it);
183 static int cb_pcimdas_detach(struct comedi_device *dev);
184 static struct comedi_driver driver_cb_pcimdas = {
185 .driver_name = "cb_pcimdas",
186 .module = THIS_MODULE,
187 .attach = cb_pcimdas_attach,
188 .detach = cb_pcimdas_detach,
191 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
192 struct comedi_subdevice *s,
193 struct comedi_insn *insn, unsigned int *data);
194 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
195 struct comedi_subdevice *s,
196 struct comedi_insn *insn, unsigned int *data);
197 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
198 struct comedi_subdevice *s,
199 struct comedi_insn *insn, unsigned int *data);
202 * Attach is called by the Comedi core to configure the driver
203 * for a particular board. If you specified a board_name array
204 * in the driver structure, dev->board_ptr contains that
205 * address.
207 static int cb_pcimdas_attach(struct comedi_device *dev,
208 struct comedi_devconfig *it)
210 struct comedi_subdevice *s;
211 struct pci_dev *pcidev = NULL;
212 int index;
213 /* int i; */
215 printk("comedi%d: cb_pcimdas: ", dev->minor);
218 * Allocate the private structure area.
220 if (alloc_private(dev, sizeof(struct cb_pcimdas_private)) < 0)
221 return -ENOMEM;
224 * Probe the device to determine what device in the series it is.
226 printk("\n");
228 for_each_pci_dev(pcidev) {
229 /* is it not a computer boards card? */
230 if (pcidev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS)
231 continue;
232 /* loop through cards supported by this driver */
233 for (index = 0; index < N_BOARDS; index++) {
234 if (cb_pcimdas_boards[index].device_id !=
235 pcidev->device)
236 continue;
237 /* was a particular bus/slot requested? */
238 if (it->options[0] || it->options[1]) {
239 /* are we on the wrong bus/slot? */
240 if (pcidev->bus->number != it->options[0] ||
241 PCI_SLOT(pcidev->devfn) != it->options[1]) {
242 continue;
245 devpriv->pci_dev = pcidev;
246 dev->board_ptr = cb_pcimdas_boards + index;
247 goto found;
251 printk("No supported ComputerBoards/MeasurementComputing card found on "
252 "requested position\n");
253 return -EIO;
255 found:
257 printk("Found %s on bus %i, slot %i\n", cb_pcimdas_boards[index].name,
258 pcidev->bus->number, PCI_SLOT(pcidev->devfn));
260 /* Warn about non-tested features */
261 switch (thisboard->device_id) {
262 case 0x56:
263 break;
264 default:
265 printk("THIS CARD IS UNSUPPORTED.\n"
266 "PLEASE REPORT USAGE TO <mocelet@sucs.org>\n");
269 if (comedi_pci_enable(pcidev, "cb_pcimdas")) {
270 printk(" Failed to enable PCI device and request regions\n");
271 return -EIO;
274 devpriv->BADR0 = pci_resource_start(devpriv->pci_dev, 0);
275 devpriv->BADR1 = pci_resource_start(devpriv->pci_dev, 1);
276 devpriv->BADR2 = pci_resource_start(devpriv->pci_dev, 2);
277 devpriv->BADR3 = pci_resource_start(devpriv->pci_dev, 3);
278 devpriv->BADR4 = pci_resource_start(devpriv->pci_dev, 4);
280 #ifdef CBPCIMDAS_DEBUG
281 printk("devpriv->BADR0 = 0x%lx\n", devpriv->BADR0);
282 printk("devpriv->BADR1 = 0x%lx\n", devpriv->BADR1);
283 printk("devpriv->BADR2 = 0x%lx\n", devpriv->BADR2);
284 printk("devpriv->BADR3 = 0x%lx\n", devpriv->BADR3);
285 printk("devpriv->BADR4 = 0x%lx\n", devpriv->BADR4);
286 #endif
288 /* Dont support IRQ yet */
289 /* get irq */
290 /* if(request_irq(devpriv->pci_dev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */
291 /* { */
292 /* printk(" unable to allocate irq %u\n", devpriv->pci_dev->irq); */
293 /* return -EINVAL; */
294 /* } */
295 /* dev->irq = devpriv->pci_dev->irq; */
297 /* Initialize dev->board_name */
298 dev->board_name = thisboard->name;
301 * Allocate the subdevice structures. alloc_subdevice() is a
302 * convenient macro defined in comedidev.h.
304 if (alloc_subdevices(dev, 3) < 0)
305 return -ENOMEM;
307 s = dev->subdevices + 0;
308 /* dev->read_subdev=s; */
309 /* analog input subdevice */
310 s->type = COMEDI_SUBD_AI;
311 s->subdev_flags = SDF_READABLE | SDF_GROUND;
312 s->n_chan = thisboard->ai_se_chans;
313 s->maxdata = (1 << thisboard->ai_bits) - 1;
314 s->range_table = &range_unknown;
315 s->len_chanlist = 1; /* This is the maximum chanlist length that */
316 /* the board can handle */
317 s->insn_read = cb_pcimdas_ai_rinsn;
319 s = dev->subdevices + 1;
320 /* analog output subdevice */
321 s->type = COMEDI_SUBD_AO;
322 s->subdev_flags = SDF_WRITABLE;
323 s->n_chan = thisboard->ao_nchan;
324 s->maxdata = 1 << thisboard->ao_bits;
325 s->range_table = &range_unknown; /* ranges are hardware settable, but not software readable. */
326 s->insn_write = &cb_pcimdas_ao_winsn;
327 s->insn_read = &cb_pcimdas_ao_rinsn;
329 s = dev->subdevices + 2;
330 /* digital i/o subdevice */
331 if (thisboard->has_dio)
332 subdev_8255_init(dev, s, NULL, devpriv->BADR4);
333 else
334 s->type = COMEDI_SUBD_UNUSED;
336 printk("attached\n");
338 return 1;
342 * _detach is called to deconfigure a device. It should deallocate
343 * resources.
344 * This function is also called when _attach() fails, so it should be
345 * careful not to release resources that were not necessarily
346 * allocated by _attach(). dev->private and dev->subdevices are
347 * deallocated automatically by the core.
349 static int cb_pcimdas_detach(struct comedi_device *dev)
351 #ifdef CBPCIMDAS_DEBUG
352 if (devpriv) {
353 printk("devpriv->BADR0 = 0x%lx\n", devpriv->BADR0);
354 printk("devpriv->BADR1 = 0x%lx\n", devpriv->BADR1);
355 printk("devpriv->BADR2 = 0x%lx\n", devpriv->BADR2);
356 printk("devpriv->BADR3 = 0x%lx\n", devpriv->BADR3);
357 printk("devpriv->BADR4 = 0x%lx\n", devpriv->BADR4);
359 #endif
360 printk("comedi%d: cb_pcimdas: remove\n", dev->minor);
361 if (dev->irq)
362 free_irq(dev->irq, dev);
363 if (devpriv) {
364 if (devpriv->pci_dev) {
365 if (devpriv->BADR0)
366 comedi_pci_disable(devpriv->pci_dev);
367 pci_dev_put(devpriv->pci_dev);
371 return 0;
375 * "instructions" read/write data in "one-shot" or "software-triggered"
376 * mode.
378 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
379 struct comedi_subdevice *s,
380 struct comedi_insn *insn, unsigned int *data)
382 int n, i;
383 unsigned int d;
384 unsigned int busy;
385 int chan = CR_CHAN(insn->chanspec);
386 unsigned short chanlims;
387 int maxchans;
389 /* only support sw initiated reads from a single channel */
391 /* check channel number */
392 if ((inb(devpriv->BADR3 + 2) & 0x20) == 0) /* differential mode */
393 maxchans = thisboard->ai_diff_chans;
394 else
395 maxchans = thisboard->ai_se_chans;
397 if (chan > (maxchans - 1))
398 return -ETIMEDOUT; /* *** Wrong error code. Fixme. */
400 /* configure for sw initiated read */
401 d = inb(devpriv->BADR3 + 5);
402 if ((d & 0x03) > 0) { /* only reset if needed. */
403 d = d & 0xfd;
404 outb(d, devpriv->BADR3 + 5);
406 outb(0x01, devpriv->BADR3 + 6); /* set bursting off, conversions on */
407 outb(0x00, devpriv->BADR3 + 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
409 /* write channel limits to multiplexer, set Low (bits 0-3) and High (bits 4-7) channels to chan. */
410 chanlims = chan | (chan << 4);
411 outb(chanlims, devpriv->BADR3 + 0);
413 /* convert n samples */
414 for (n = 0; n < insn->n; n++) {
415 /* trigger conversion */
416 outw(0, devpriv->BADR2 + 0);
418 #define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
419 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
421 /* wait for conversion to end */
422 for (i = 0; i < TIMEOUT; i++) {
423 busy = inb(devpriv->BADR3 + 2) & 0x80;
424 if (!busy)
425 break;
427 if (i == TIMEOUT) {
428 printk("timeout\n");
429 return -ETIMEDOUT;
431 /* read data */
432 d = inw(devpriv->BADR2 + 0);
434 /* mangle the data as necessary */
435 /* d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed. */
437 data[n] = d;
440 /* return the number of samples read/written */
441 return n;
444 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
445 struct comedi_subdevice *s,
446 struct comedi_insn *insn, unsigned int *data)
448 int i;
449 int chan = CR_CHAN(insn->chanspec);
451 /* Writing a list of values to an AO channel is probably not
452 * very useful, but that's how the interface is defined. */
453 for (i = 0; i < insn->n; i++) {
454 switch (chan) {
455 case 0:
456 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC0_OFFSET);
457 break;
458 case 1:
459 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC1_OFFSET);
460 break;
461 default:
462 return -1;
464 devpriv->ao_readback[chan] = data[i];
467 /* return the number of samples read/written */
468 return i;
471 /* AO subdevices should have a read insn as well as a write insn.
472 * Usually this means copying a value stored in devpriv. */
473 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
474 struct comedi_subdevice *s,
475 struct comedi_insn *insn, unsigned int *data)
477 int i;
478 int chan = CR_CHAN(insn->chanspec);
480 for (i = 0; i < insn->n; i++)
481 data[i] = devpriv->ao_readback[chan];
483 return i;
487 * A convenient macro that defines init_module() and cleanup_module(),
488 * as necessary.
490 static int __devinit driver_cb_pcimdas_pci_probe(struct pci_dev *dev,
491 const struct pci_device_id
492 *ent)
494 return comedi_pci_auto_config(dev, driver_cb_pcimdas.driver_name);
497 static void __devexit driver_cb_pcimdas_pci_remove(struct pci_dev *dev)
499 comedi_pci_auto_unconfig(dev);
502 static struct pci_driver driver_cb_pcimdas_pci_driver = {
503 .id_table = cb_pcimdas_pci_table,
504 .probe = &driver_cb_pcimdas_pci_probe,
505 .remove = __devexit_p(&driver_cb_pcimdas_pci_remove)
508 static int __init driver_cb_pcimdas_init_module(void)
510 int retval;
512 retval = comedi_driver_register(&driver_cb_pcimdas);
513 if (retval < 0)
514 return retval;
516 driver_cb_pcimdas_pci_driver.name =
517 (char *)driver_cb_pcimdas.driver_name;
518 return pci_register_driver(&driver_cb_pcimdas_pci_driver);
521 static void __exit driver_cb_pcimdas_cleanup_module(void)
523 pci_unregister_driver(&driver_cb_pcimdas_pci_driver);
524 comedi_driver_unregister(&driver_cb_pcimdas);
527 module_init(driver_cb_pcimdas_init_module);
528 module_exit(driver_cb_pcimdas_cleanup_module);
530 MODULE_AUTHOR("Comedi http://www.comedi.org");
531 MODULE_DESCRIPTION("Comedi low-level driver");
532 MODULE_LICENSE("GPL");