2 comedi/drivers/icp_multi.c
4 COMEDI - Linux Control and Measurement Device Interface
5 Copyright (C) 1997-2002 David A. Schleef <ds@schleef.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
20 Description: Inova ICP_MULTI
21 Author: Anne Smorthit <anne.smorthit@sfwte.ch>
22 Devices: [Inova] ICP_MULTI (icp_multi)
25 The driver works for analog input and output and digital input and output.
26 It does not work with interrupts or with the counters. Currently no support
29 It has 16 single-ended or 8 differential Analogue Input channels with 12-bit
30 resolution. Ranges : 5V, 10V, +/-5V, +/-10V, 0..20mA and 4..20mA. Input
31 ranges can be individually programmed for each channel. Voltage or current
32 measurement is selected by jumper.
34 There are 4 x 12-bit Analogue Outputs. Ranges : 5V, 10V, +/-5V, +/-10V
36 16 x Digital Inputs, 24V
38 8 x Digital Outputs, 24V, 1A
42 Configuration options: not applicable, uses PCI auto config
45 #include <linux/pci.h>
46 #include <linux/delay.h>
47 #include <linux/interrupt.h>
49 #include "../comedidev.h"
51 #define PCI_DEVICE_ID_ICP_MULTI 0x8000
53 #define ICP_MULTI_ADC_CSR 0 /* R/W: ADC command/status register */
54 #define ICP_MULTI_AI 2 /* R: Analogue input data */
55 #define ICP_MULTI_DAC_CSR 4 /* R/W: DAC command/status register */
56 #define ICP_MULTI_AO 6 /* R/W: Analogue output data */
57 #define ICP_MULTI_DI 8 /* R/W: Digital inouts */
58 #define ICP_MULTI_DO 0x0A /* R/W: Digital outputs */
59 #define ICP_MULTI_INT_EN 0x0C /* R/W: Interrupt enable register */
60 #define ICP_MULTI_INT_STAT 0x0E /* R/W: Interrupt status register */
61 #define ICP_MULTI_CNTR0 0x10 /* R/W: Counter 0 */
62 #define ICP_MULTI_CNTR1 0x12 /* R/W: counter 1 */
63 #define ICP_MULTI_CNTR2 0x14 /* R/W: Counter 2 */
64 #define ICP_MULTI_CNTR3 0x16 /* R/W: Counter 3 */
66 /* Define bits from ADC command/status register */
67 #define ADC_ST 0x0001 /* Start ADC */
68 #define ADC_BSY 0x0001 /* ADC busy */
69 #define ADC_BI 0x0010 /* Bipolar input range 1 = bipolar */
70 #define ADC_RA 0x0020 /* Input range 0 = 5V, 1 = 10V */
71 #define ADC_DI 0x0040 /* Differential input mode 1 = differential */
73 /* Define bits from DAC command/status register */
74 #define DAC_ST 0x0001 /* Start DAC */
75 #define DAC_BSY 0x0001 /* DAC busy */
76 #define DAC_BI 0x0010 /* Bipolar input range 1 = bipolar */
77 #define DAC_RA 0x0020 /* Input range 0 = 5V, 1 = 10V */
79 /* Define bits from interrupt enable/status registers */
80 #define ADC_READY 0x0001 /* A/d conversion ready interrupt */
81 #define DAC_READY 0x0002 /* D/a conversion ready interrupt */
82 #define DOUT_ERROR 0x0004 /* Digital output error interrupt */
83 #define DIN_STATUS 0x0008 /* Digital input status change interrupt */
84 #define CIE0 0x0010 /* Counter 0 overrun interrupt */
85 #define CIE1 0x0020 /* Counter 1 overrun interrupt */
86 #define CIE2 0x0040 /* Counter 2 overrun interrupt */
87 #define CIE3 0x0080 /* Counter 3 overrun interrupt */
89 /* Useful definitions */
90 #define Status_IRQ 0x00ff /* All interrupts */
92 /* Define analogue range */
93 static const struct comedi_lrange range_analog = { 4, {
101 static const char range_codes_analog[] = { 0x00, 0x20, 0x10, 0x30 };
104 ==============================================================================
105 Data & Structure declarations
106 ==============================================================================
109 struct icp_multi_private {
110 char valid; /* card is usable */
111 void __iomem *io_addr; /* Pointer to mapped io address */
112 unsigned int AdcCmdStatus; /* ADC Command/Status register */
113 unsigned int DacCmdStatus; /* DAC Command/Status register */
114 unsigned int IntEnable; /* Interrupt Enable register */
115 unsigned int IntStatus; /* Interrupt Status register */
116 unsigned int act_chanlist[32]; /* list of scanned channel */
117 unsigned char act_chanlist_len; /* len of scanlist */
118 unsigned char act_chanlist_pos; /* actual position in MUX list */
119 unsigned int *ai_chanlist; /* actaul chanlist */
120 short *ai_data; /* data buffer */
121 short ao_data[4]; /* data output buffer */
122 short di_data; /* Digital input data */
123 unsigned int do_data; /* Remember digital output data */
126 static void setup_channel_list(struct comedi_device *dev,
127 struct comedi_subdevice *s,
128 unsigned int *chanlist, unsigned int n_chan)
130 struct icp_multi_private *devpriv = dev->private;
131 unsigned int i, range, chanprog;
134 devpriv->act_chanlist_len = n_chan;
135 devpriv->act_chanlist_pos = 0;
137 for (i = 0; i < n_chan; i++) {
139 chanprog = CR_CHAN(chanlist[i]);
141 /* Determine if it is a differential channel (Bit 15 = 1) */
142 if (CR_AREF(chanlist[i]) == AREF_DIFF) {
150 /* Clear channel, range and input mode bits
151 * in A/D command/status register */
152 devpriv->AdcCmdStatus &= 0xf00f;
154 /* Set channel number and differential mode status bit */
156 /* Set channel number, bits 9-11 & mode, bit 6 */
157 devpriv->AdcCmdStatus |= (chanprog << 9);
158 devpriv->AdcCmdStatus |= ADC_DI;
160 /* Set channel number, bits 8-11 */
161 devpriv->AdcCmdStatus |= (chanprog << 8);
163 /* Get range for current channel */
164 range = range_codes_analog[CR_RANGE(chanlist[i])];
165 /* Set range. bits 4-5 */
166 devpriv->AdcCmdStatus |= range;
168 /* Output channel, range, mode to ICP Multi */
169 writew(devpriv->AdcCmdStatus,
170 devpriv->io_addr + ICP_MULTI_ADC_CSR);
174 static int icp_multi_insn_read_ai(struct comedi_device *dev,
175 struct comedi_subdevice *s,
176 struct comedi_insn *insn, unsigned int *data)
178 struct icp_multi_private *devpriv = dev->private;
181 /* Disable A/D conversion ready interrupt */
182 devpriv->IntEnable &= ~ADC_READY;
183 writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
185 /* Clear interrupt status */
186 devpriv->IntStatus |= ADC_READY;
187 writew(devpriv->IntStatus, devpriv->io_addr + ICP_MULTI_INT_STAT);
189 /* Set up appropriate channel, mode and range data, for specified ch */
190 setup_channel_list(dev, s, &insn->chanspec, 1);
192 for (n = 0; n < insn->n; n++) {
193 /* Set start ADC bit */
194 devpriv->AdcCmdStatus |= ADC_ST;
195 writew(devpriv->AdcCmdStatus,
196 devpriv->io_addr + ICP_MULTI_ADC_CSR);
197 devpriv->AdcCmdStatus &= ~ADC_ST;
201 /* Wait for conversion to complete, or get fed up waiting */
204 if (!(readw(devpriv->io_addr +
205 ICP_MULTI_ADC_CSR) & ADC_BSY))
211 /* If we reach here, a timeout has occurred */
212 comedi_error(dev, "A/D insn timeout");
214 /* Disable interrupt */
215 devpriv->IntEnable &= ~ADC_READY;
216 writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
218 /* Clear interrupt status */
219 devpriv->IntStatus |= ADC_READY;
220 writew(devpriv->IntStatus,
221 devpriv->io_addr + ICP_MULTI_INT_STAT);
223 /* Clear data received */
230 (readw(devpriv->io_addr + ICP_MULTI_AI) >> 4) & 0x0fff;
233 /* Disable interrupt */
234 devpriv->IntEnable &= ~ADC_READY;
235 writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
237 /* Clear interrupt status */
238 devpriv->IntStatus |= ADC_READY;
239 writew(devpriv->IntStatus, devpriv->io_addr + ICP_MULTI_INT_STAT);
244 static int icp_multi_insn_write_ao(struct comedi_device *dev,
245 struct comedi_subdevice *s,
246 struct comedi_insn *insn, unsigned int *data)
248 struct icp_multi_private *devpriv = dev->private;
249 int n, chan, range, timeout;
251 /* Disable D/A conversion ready interrupt */
252 devpriv->IntEnable &= ~DAC_READY;
253 writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
255 /* Clear interrupt status */
256 devpriv->IntStatus |= DAC_READY;
257 writew(devpriv->IntStatus, devpriv->io_addr + ICP_MULTI_INT_STAT);
259 /* Get channel number and range */
260 chan = CR_CHAN(insn->chanspec);
261 range = CR_RANGE(insn->chanspec);
263 /* Set up range and channel data */
264 /* Bit 4 = 1 : Bipolar */
266 /* Bit 5 = 1 : 10V */
267 /* Bits 8-9 : Channel number */
268 devpriv->DacCmdStatus &= 0xfccf;
269 devpriv->DacCmdStatus |= range_codes_analog[range];
270 devpriv->DacCmdStatus |= (chan << 8);
272 writew(devpriv->DacCmdStatus, devpriv->io_addr + ICP_MULTI_DAC_CSR);
274 for (n = 0; n < insn->n; n++) {
275 /* Wait for analogue output data register to be
276 * ready for new data, or get fed up waiting */
279 if (!(readw(devpriv->io_addr +
280 ICP_MULTI_DAC_CSR) & DAC_BSY))
286 /* If we reach here, a timeout has occurred */
287 comedi_error(dev, "D/A insn timeout");
289 /* Disable interrupt */
290 devpriv->IntEnable &= ~DAC_READY;
291 writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
293 /* Clear interrupt status */
294 devpriv->IntStatus |= DAC_READY;
295 writew(devpriv->IntStatus,
296 devpriv->io_addr + ICP_MULTI_INT_STAT);
298 /* Clear data received */
299 devpriv->ao_data[chan] = 0;
304 /* Write data to analogue output data register */
305 writew(data[n], devpriv->io_addr + ICP_MULTI_AO);
307 /* Set DAC_ST bit to write the data to selected channel */
308 devpriv->DacCmdStatus |= DAC_ST;
309 writew(devpriv->DacCmdStatus,
310 devpriv->io_addr + ICP_MULTI_DAC_CSR);
311 devpriv->DacCmdStatus &= ~DAC_ST;
313 /* Save analogue output data */
314 devpriv->ao_data[chan] = data[n];
320 static int icp_multi_insn_read_ao(struct comedi_device *dev,
321 struct comedi_subdevice *s,
322 struct comedi_insn *insn, unsigned int *data)
324 struct icp_multi_private *devpriv = dev->private;
327 /* Get channel number */
328 chan = CR_CHAN(insn->chanspec);
330 /* Read analogue outputs */
331 for (n = 0; n < insn->n; n++)
332 data[n] = devpriv->ao_data[chan];
337 static int icp_multi_insn_bits_di(struct comedi_device *dev,
338 struct comedi_subdevice *s,
339 struct comedi_insn *insn, unsigned int *data)
341 struct icp_multi_private *devpriv = dev->private;
343 data[1] = readw(devpriv->io_addr + ICP_MULTI_DI);
348 static int icp_multi_insn_bits_do(struct comedi_device *dev,
349 struct comedi_subdevice *s,
350 struct comedi_insn *insn, unsigned int *data)
352 struct icp_multi_private *devpriv = dev->private;
355 s->state &= ~data[0];
356 s->state |= (data[0] & data[1]);
358 printk(KERN_DEBUG "Digital outputs = %4x \n", s->state);
360 writew(s->state, devpriv->io_addr + ICP_MULTI_DO);
363 data[1] = readw(devpriv->io_addr + ICP_MULTI_DI);
368 static int icp_multi_insn_read_ctr(struct comedi_device *dev,
369 struct comedi_subdevice *s,
370 struct comedi_insn *insn, unsigned int *data)
375 static int icp_multi_insn_write_ctr(struct comedi_device *dev,
376 struct comedi_subdevice *s,
377 struct comedi_insn *insn,
383 static irqreturn_t interrupt_service_icp_multi(int irq, void *d)
385 struct comedi_device *dev = d;
386 struct icp_multi_private *devpriv = dev->private;
389 /* Is this interrupt from our board? */
390 int_no = readw(devpriv->io_addr + ICP_MULTI_INT_STAT) & Status_IRQ;
395 /* Determine which interrupt is active & handle it */
422 static int check_channel_list(struct comedi_device *dev,
423 struct comedi_subdevice *s,
424 unsigned int *chanlist, unsigned int n_chan)
428 /* Check that we at least have one channel to check */
430 comedi_error(dev, "range/channel list is empty!");
433 /* Check all channels */
434 for (i = 0; i < n_chan; i++) {
435 /* Check that channel number is < maximum */
436 if (CR_AREF(chanlist[i]) == AREF_DIFF) {
437 if (CR_CHAN(chanlist[i]) > (s->nchan / 2)) {
439 "Incorrect differential ai ch-nr");
443 if (CR_CHAN(chanlist[i]) > s->n_chan) {
445 "Incorrect ai channel number");
454 static int icp_multi_reset(struct comedi_device *dev)
456 struct icp_multi_private *devpriv = dev->private;
459 /* Clear INT enables and requests */
460 writew(0, devpriv->io_addr + ICP_MULTI_INT_EN);
461 writew(0x00ff, devpriv->io_addr + ICP_MULTI_INT_STAT);
463 /* Set DACs to 0..5V range and 0V output */
464 for (i = 0; i < 4; i++) {
465 devpriv->DacCmdStatus &= 0xfcce;
467 /* Set channel number */
468 devpriv->DacCmdStatus |= (i << 8);
471 writew(0, devpriv->io_addr + ICP_MULTI_AO);
473 /* Set start conversion bit */
474 devpriv->DacCmdStatus |= DAC_ST;
476 /* Output to command / status register */
477 writew(devpriv->DacCmdStatus,
478 devpriv->io_addr + ICP_MULTI_DAC_CSR);
480 /* Delay to allow DAC time to recover */
484 /* Digital outputs to 0 */
485 writew(0, devpriv->io_addr + ICP_MULTI_DO);
490 static int icp_multi_auto_attach(struct comedi_device *dev,
491 unsigned long context_unused)
493 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
494 struct icp_multi_private *devpriv;
495 struct comedi_subdevice *s;
498 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
501 dev->private = devpriv;
503 ret = comedi_pci_enable(dev);
507 devpriv->io_addr = pci_ioremap_bar(pcidev, 2);
508 if (!devpriv->io_addr)
511 ret = comedi_alloc_subdevices(dev, 5);
515 icp_multi_reset(dev);
518 ret = request_irq(pcidev->irq, interrupt_service_icp_multi,
519 IRQF_SHARED, dev->board_name, dev);
521 dev->irq = pcidev->irq;
524 s = &dev->subdevices[0];
525 dev->read_subdev = s;
526 s->type = COMEDI_SUBD_AI;
527 s->subdev_flags = SDF_READABLE | SDF_COMMON | SDF_GROUND | SDF_DIFF;
530 s->len_chanlist = 16;
531 s->range_table = &range_analog;
532 s->insn_read = icp_multi_insn_read_ai;
534 s = &dev->subdevices[1];
535 s->type = COMEDI_SUBD_AO;
536 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
540 s->range_table = &range_analog;
541 s->insn_write = icp_multi_insn_write_ao;
542 s->insn_read = icp_multi_insn_read_ao;
544 s = &dev->subdevices[2];
545 s->type = COMEDI_SUBD_DI;
546 s->subdev_flags = SDF_READABLE;
549 s->len_chanlist = 16;
550 s->range_table = &range_digital;
552 s->insn_bits = icp_multi_insn_bits_di;
554 s = &dev->subdevices[3];
555 s->type = COMEDI_SUBD_DO;
556 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
560 s->range_table = &range_digital;
563 s->insn_bits = icp_multi_insn_bits_do;
565 s = &dev->subdevices[4];
566 s->type = COMEDI_SUBD_COUNTER;
567 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
572 s->insn_read = icp_multi_insn_read_ctr;
573 s->insn_write = icp_multi_insn_write_ctr;
577 dev_info(dev->class_dev, "%s attached, irq %sabled\n",
578 dev->board_name, dev->irq ? "en" : "dis");
583 static void icp_multi_detach(struct comedi_device *dev)
585 struct icp_multi_private *devpriv = dev->private;
589 icp_multi_reset(dev);
591 free_irq(dev->irq, dev);
592 if (devpriv && devpriv->io_addr)
593 iounmap(devpriv->io_addr);
594 comedi_pci_disable(dev);
597 static struct comedi_driver icp_multi_driver = {
598 .driver_name = "icp_multi",
599 .module = THIS_MODULE,
600 .auto_attach = icp_multi_auto_attach,
601 .detach = icp_multi_detach,
604 static int icp_multi_pci_probe(struct pci_dev *dev,
605 const struct pci_device_id *id)
607 return comedi_pci_auto_config(dev, &icp_multi_driver, id->driver_data);
610 static DEFINE_PCI_DEVICE_TABLE(icp_multi_pci_table) = {
611 { PCI_DEVICE(PCI_VENDOR_ID_ICP, PCI_DEVICE_ID_ICP_MULTI) },
614 MODULE_DEVICE_TABLE(pci, icp_multi_pci_table);
616 static struct pci_driver icp_multi_pci_driver = {
618 .id_table = icp_multi_pci_table,
619 .probe = icp_multi_pci_probe,
620 .remove = comedi_pci_auto_unconfig,
622 module_comedi_pci_driver(icp_multi_driver, icp_multi_pci_driver);
624 MODULE_AUTHOR("Comedi http://www.comedi.org");
625 MODULE_DESCRIPTION("Comedi low-level driver");
626 MODULE_LICENSE("GPL");