2 * comedi/drivers/ii_pci20kc.c
3 * Driver for Intelligent Instruments PCI-20001C carrier board
6 * Copyright (C) 2000 Markus Kempf <kempf@matsci.uni-sb.de>
7 * with suggestions from David Schleef
10 * Linux device driver for COMEDI
11 * Intelligent Instrumentation
12 * PCI-20001 C-2A Carrier Board
13 * PCI-20341 M-1A 16-Bit analog input module
17 * PCI-20006 M-2 16-Bit analog output module
18 * - ranges (-10V - +10V) (0V - +10V) (-5V - +5V)
21 * only ONE PCI-20341 module possible
22 * only ONE PCI-20006 module possible
23 * no extern trigger implemented
25 * NOT WORKING (but soon) only 4 on-board differential channels supported
26 * NOT WORKING (but soon) only ONE di-port and ONE do-port supported instead of 4 digital ports
30 * The state of this driver is only a starting point for a complete
31 * COMEDI-driver. The final driver should support all features of the
32 * carrier board and modules.
34 * The test configuration:
36 * kernel 2.2.14 with RTAI v1.2 and patch-2.2.14rthal2
43 Description: Intelligent Instruments PCI-20001C carrier board
44 Author: Markus Kempf <kempf@matsci.uni-sb.de>
45 Devices: [Intelligent Instrumentation] PCI-20001C (ii_pci20kc)
48 Supports the PCI-20001 C-2a Carrier board, and could probably support
49 the other carrier boards with small modifications. Modules supported
51 PCI-20006 M-2 16-bit analog output module
52 PCI-20341 M-1A 16-bit analog input module
57 2 first option for module 1
58 3 second option for module 1
59 4 first option for module 2
60 5 second option for module 2
61 6 first option for module 3
62 7 second option for module 3
64 options for PCI-20006M:
65 first: Analog output channel 0 range configuration
66 0 bipolar 10 (-10V -- +10V)
67 1 unipolar 10 (0V -- +10V)
68 2 bipolar 5 (-5V -- 5V)
69 second: Analog output channel 1 range configuration
71 options for PCI-20341M:
72 first: Analog input gain configuration
79 /* XXX needs to use ioremap() for compatibility with 2.4 kernels. Should also
80 * check_mem_region() etc. - fmhess */
82 #include "../comedidev.h"
84 #define PCI20000_ID 0x1d
85 #define PCI20341_ID 0x77
86 #define PCI20006_ID 0xe3
87 #define PCI20xxx_EMPTY_ID 0xff
89 #define PCI20000_OFFSET 0x100
90 #define PCI20000_MODULES 3
92 #define PCI20000_DIO_0 0x80
93 #define PCI20000_DIO_1 0x81
94 #define PCI20000_DIO_2 0xc0
95 #define PCI20000_DIO_3 0xc1
96 #define PCI20000_DIO_CONTROL_01 0x83 /* port 0, 1 control */
97 #define PCI20000_DIO_CONTROL_23 0xc3 /* port 2, 3 control */
98 #define PCI20000_DIO_BUFFER 0x82 /* buffer direction and enable */
99 #define PCI20000_DIO_EOC 0xef /* even port, control output */
100 #define PCI20000_DIO_OOC 0xfd /* odd port, control output */
101 #define PCI20000_DIO_EIC 0x90 /* even port, control input */
102 #define PCI20000_DIO_OIC 0x82 /* odd port, control input */
103 #define DIO_CAND 0x12 /* and bit 1, bit 4 of control */
104 #define DIO_BE 0x01 /* buffer: port enable */
105 #define DIO_BO 0x04 /* buffer: output */
106 #define DIO_BI 0x05 /* buffer: input */
107 #define DIO_PS_0 0x00 /* buffer: port shift 0 */
108 #define DIO_PS_1 0x01 /* buffer: port shift 1 */
109 #define DIO_PS_2 0x04 /* buffer: port shift 2 */
110 #define DIO_PS_3 0x05 /* buffer: port shift 3 */
112 #define PCI20006_LCHAN0 0x0d
113 #define PCI20006_STROBE0 0x0b
114 #define PCI20006_LCHAN1 0x15
115 #define PCI20006_STROBE1 0x13
117 #define PCI20341_INIT 0x04
118 #define PCI20341_REPMODE 0x00 /* single shot mode */
119 #define PCI20341_PACER 0x00 /* Hardware Pacer disabled */
120 #define PCI20341_CHAN_NR 0x04 /* number of input channels */
121 #define PCI20341_CONFIG_REG 0x10
122 #define PCI20341_MOD_STATUS 0x01
123 #define PCI20341_OPT_REG 0x11
124 #define PCI20341_SET_TIME_REG 0x15
125 #define PCI20341_LCHAN_ADDR_REG 0x13
126 #define PCI20341_CHAN_LIST 0x80
127 #define PCI20341_CC_RESET 0x1b
128 #define PCI20341_CHAN_RESET 0x19
129 #define PCI20341_SOFT_PACER 0x04
130 #define PCI20341_STATUS_REG 0x12
131 #define PCI20341_LDATA 0x02
132 #define PCI20341_DAISY_CHAIN 0x20 /* On-board inputs only */
133 #define PCI20341_MUX 0x04 /* Enable on-board MUX */
134 #define PCI20341_SCANLIST 0x80 /* Channel/Gain Scan List */
136 union pci20xxx_subdev_private {
140 const struct comedi_lrange *ao_range_list[2]; /* range of channels of ao module */
141 unsigned int last_data[2];
151 struct pci20xxx_private {
154 union pci20xxx_subdev_private subdev_private[PCI20000_MODULES];
158 #define devpriv ((struct pci20xxx_private *)dev->private)
159 #define CHAN (CR_CHAN(it->chanlist[0]))
161 static int pci20xxx_attach(struct comedi_device * dev, struct comedi_devconfig * it);
162 static int pci20xxx_detach(struct comedi_device * dev);
164 static struct comedi_driver driver_pci20xxx = {
165 driver_name:"ii_pci20kc",
167 attach:pci20xxx_attach,
168 detach:pci20xxx_detach,
171 static int pci20006_init(struct comedi_device * dev, struct comedi_subdevice * s,
173 static int pci20341_init(struct comedi_device * dev, struct comedi_subdevice * s,
175 static int pci20xxx_dio_init(struct comedi_device * dev, struct comedi_subdevice * s);
178 options[0] Board base address
180 options[2] first option for module 1
181 options[3] second option for module 1
182 options[4] first option for module 2
183 options[5] second option for module 2
184 options[6] first option for module 3
185 options[7] second option for module 3
187 options for PCI-20341M:
188 first Analog input gain configuration
194 options for PCI-20006M:
195 first Analog output channel 0 range configuration
196 0 == bipolar 10 (-10V -- +10V)
197 1 == unipolar 10V (0V -- +10V)
198 2 == bipolar 5V (-5V -- +5V)
199 second Analog output channel 1 range configuration
200 0 == bipolar 10 (-10V -- +10V)
201 1 == unipolar 10V (0V -- +10V)
202 2 == bipolar 5V (-5V -- +5V)
204 static int pci20xxx_attach(struct comedi_device * dev, struct comedi_devconfig * it)
209 struct comedi_subdevice *s;
210 union pci20xxx_subdev_private *sdp;
212 if ((ret = alloc_subdevices(dev, 1 + PCI20000_MODULES)) < 0)
214 if ((ret = alloc_private(dev, sizeof(struct pci20xxx_private))) < 0)
217 devpriv->ioaddr = (void *)(unsigned long)it->options[0];
218 dev->board_name = "pci20kc";
220 /* Check PCI-20001 C-2A Carrier Board ID */
221 if ((readb(devpriv->ioaddr) & PCI20000_ID) != PCI20000_ID) {
222 printk("comedi%d: ii_pci20kc", dev->minor);
223 printk(" PCI-20001 C-2A Carrier Board at base=0x%p not found !\n", devpriv->ioaddr);
226 printk("comedi%d:\n", dev->minor);
227 printk("ii_pci20kc: PCI-20001 C-2A at base=0x%p\n", devpriv->ioaddr);
229 for (i = 0; i < PCI20000_MODULES; i++) {
230 s = dev->subdevices + i;
231 id = readb(devpriv->ioaddr + (i + 1) * PCI20000_OFFSET);
232 s->private = devpriv->subdev_private + i;
236 sdp->pci20006.iobase =
237 devpriv->ioaddr + (i + 1) * PCI20000_OFFSET;
238 pci20006_init(dev, s, it->options[2 * i + 2],
239 it->options[2 * i + 3]);
240 printk("comedi%d: ii_pci20kc", dev->minor);
241 printk(" PCI-20006 module in slot %d \n", i + 1);
244 sdp->pci20341.iobase =
245 devpriv->ioaddr + (i + 1) * PCI20000_OFFSET;
246 pci20341_init(dev, s, it->options[2 * i + 2],
247 it->options[2 * i + 3]);
248 printk("comedi%d: ii_pci20kc", dev->minor);
249 printk(" PCI-20341 module in slot %d \n", i + 1);
252 printk("ii_pci20kc: unknown module code 0x%02x in slot %d: module disabled\n", id, i);
254 case PCI20xxx_EMPTY_ID:
255 s->type = COMEDI_SUBD_UNUSED;
260 /* initialize struct pci20xxx_private */
261 pci20xxx_dio_init(dev, dev->subdevices + PCI20000_MODULES);
266 static int pci20xxx_detach(struct comedi_device * dev)
268 printk("comedi%d: pci20xxx: remove\n", dev->minor);
275 static int pci20006_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
276 struct comedi_insn * insn, unsigned int * data);
277 static int pci20006_insn_write(struct comedi_device * dev, struct comedi_subdevice * s,
278 struct comedi_insn * insn, unsigned int * data);
280 static const struct comedi_lrange *pci20006_range_list[] = {
286 static int pci20006_init(struct comedi_device * dev, struct comedi_subdevice * s,
289 union pci20xxx_subdev_private *sdp = s->private;
291 if (opt0 < 0 || opt0 > 2)
293 if (opt1 < 0 || opt1 > 2)
296 sdp->pci20006.ao_range_list[0] = pci20006_range_list[opt0];
297 sdp->pci20006.ao_range_list[1] = pci20006_range_list[opt1];
300 s->type = COMEDI_SUBD_AO;
301 s->subdev_flags = SDF_WRITABLE;
304 s->insn_read = pci20006_insn_read;
305 s->insn_write = pci20006_insn_write;
307 s->range_table_list = sdp->pci20006.ao_range_list;
311 static int pci20006_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
312 struct comedi_insn * insn, unsigned int * data)
314 union pci20xxx_subdev_private *sdp = s->private;
316 data[0] = sdp->pci20006.last_data[CR_CHAN(insn->chanspec)];
321 static int pci20006_insn_write(struct comedi_device * dev, struct comedi_subdevice * s,
322 struct comedi_insn * insn, unsigned int * data)
324 union pci20xxx_subdev_private *sdp = s->private;
326 unsigned int boarddata;
328 sdp->pci20006.last_data[CR_CHAN(insn->chanspec)] = data[0];
329 boarddata = (((unsigned int)data[0] + 0x8000) & 0xffff); /* comedi-data -> board-data */
330 lo = (boarddata & 0xff);
331 hi = ((boarddata >> 8) & 0xff);
333 switch (CR_CHAN(insn->chanspec)) {
335 writeb(lo, sdp->iobase + PCI20006_LCHAN0);
336 writeb(hi, sdp->iobase + PCI20006_LCHAN0 + 1);
337 writeb(0x00, sdp->iobase + PCI20006_STROBE0);
340 writeb(lo, sdp->iobase + PCI20006_LCHAN1);
341 writeb(hi, sdp->iobase + PCI20006_LCHAN1 + 1);
342 writeb(0x00, sdp->iobase + PCI20006_STROBE1);
345 printk(" comedi%d: pci20xxx: ao channel Error!\n", dev->minor);
354 static int pci20341_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
355 struct comedi_insn * insn, unsigned int * data);
357 static const int pci20341_timebase[] = { 0x00, 0x00, 0x00, 0x04 };
358 static const int pci20341_settling_time[] = { 0x58, 0x58, 0x93, 0x99 };
360 static const struct comedi_lrange range_bipolar0_5 = { 1, {BIP_RANGE(0.5)} };
361 static const struct comedi_lrange range_bipolar0_05 = { 1, {BIP_RANGE(0.05)} };
362 static const struct comedi_lrange range_bipolar0_025 = { 1, {BIP_RANGE(0.025)} };
364 static const struct comedi_lrange *const pci20341_ranges[] = {
371 static int pci20341_init(struct comedi_device * dev, struct comedi_subdevice * s,
374 union pci20xxx_subdev_private *sdp = s->private;
377 /* options handling */
378 if (opt0 < 0 || opt0 > 3)
380 sdp->pci20341.timebase = pci20341_timebase[opt0];
381 sdp->pci20341.settling_time = pci20341_settling_time[opt0];
384 s->type = COMEDI_SUBD_AI;
385 s->subdev_flags = SDF_READABLE;
386 s->n_chan = PCI20341_CHAN_NR;
387 s->len_chanlist = PCI20341_SCANLIST;
388 s->insn_read = pci20341_insn_read;
390 s->range_table = pci20341_ranges[opt0];
392 option = sdp->pci20341.timebase | PCI20341_REPMODE; /* depends on gain, trigger, repetition mode */
394 writeb(PCI20341_INIT, sdp->iobase + PCI20341_CONFIG_REG); /* initialize Module */
395 writeb(PCI20341_PACER, sdp->iobase + PCI20341_MOD_STATUS); /* set Pacer */
396 writeb(option, sdp->iobase + PCI20341_OPT_REG); /* option register */
397 writeb(sdp->pci20341.settling_time, sdp->iobase + PCI20341_SET_TIME_REG); /* settling time counter */
398 /* trigger not implemented */
402 static int pci20341_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
403 struct comedi_insn * insn, unsigned int * data)
405 union pci20xxx_subdev_private *sdp = s->private;
406 unsigned int i = 0, j = 0;
408 unsigned char eoc; /* end of conversion */
409 unsigned int clb; /* channel list byte */
410 unsigned int boarddata;
412 writeb(1, sdp->iobase + PCI20341_LCHAN_ADDR_REG); /* write number of input channels */
413 clb = PCI20341_DAISY_CHAIN | PCI20341_MUX | (sdp->pci20341.ai_gain << 3)
414 | CR_CHAN(insn->chanspec);
415 writeb(clb, sdp->iobase + PCI20341_CHAN_LIST);
416 writeb(0x00, sdp->iobase + PCI20341_CC_RESET); /* reset settling time counter and trigger delay counter */
417 writeb(0x00, sdp->iobase + PCI20341_CHAN_RESET);
421 for (i = 0; i < insn->n; i++) {
422 /* data polling isn't the niciest way to get the data, I know,
423 * but there are only 6 cycles (mean) and it is easier than
424 * the whole interrupt stuff
427 readb(sdp->iobase + PCI20341_SOFT_PACER); /* generate Pacer */
428 eoc = readb(sdp->iobase + PCI20341_STATUS_REG);
429 while ((eoc < 0x80) && j < 100) { /* poll Interrupt Flag */
431 eoc = readb(sdp->iobase + PCI20341_STATUS_REG);
434 printk("comedi%d: pci20xxx: AI interrupt channel %i polling exit !\n", dev->minor, i);
437 lo = readb(sdp->iobase + PCI20341_LDATA);
438 hi = readb(sdp->iobase + PCI20341_LDATA + 1);
439 boarddata = lo + 0x100 * hi;
440 data[i] = (short) ((boarddata + 0x8000) & 0xffff); /* board-data -> comedi-data */
448 static void pci20xxx_dio_config(struct comedi_device * dev, struct comedi_subdevice * s);
449 static int pci20xxx_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
450 struct comedi_insn * insn, unsigned int * data);
451 static int pci20xxx_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
452 struct comedi_insn * insn, unsigned int * data);
454 /* initialize struct pci20xxx_private */
455 static int pci20xxx_dio_init(struct comedi_device * dev, struct comedi_subdevice * s)
458 s->type = COMEDI_SUBD_DIO;
459 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
461 s->insn_bits = pci20xxx_dio_insn_bits;
462 s->insn_config = pci20xxx_dio_insn_config;
464 s->len_chanlist = 32;
465 s->range_table = &range_digital;
468 /* digital I/O lines default to input on board reset. */
469 pci20xxx_dio_config(dev, s);
474 static int pci20xxx_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
475 struct comedi_insn * insn, unsigned int * data)
479 mask = 1 << CR_CHAN(insn->chanspec);
480 if (mask & 0x000000ff) {
482 } else if (mask & 0x0000ff00) {
484 } else if (mask & 0x00ff0000) {
494 pci20xxx_dio_config(dev, s);
499 static int pci20xxx_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
500 struct comedi_insn * insn, unsigned int * data)
502 unsigned int mask = data[0];
505 s->state |= (mask & data[1]);
508 if (mask & 0x000000ff)
509 writeb((s->state >> 0) & 0xff,
510 devpriv->ioaddr + PCI20000_DIO_0);
511 if (mask & 0x0000ff00)
512 writeb((s->state >> 8) & 0xff,
513 devpriv->ioaddr + PCI20000_DIO_1);
514 if (mask & 0x00ff0000)
515 writeb((s->state >> 16) & 0xff,
516 devpriv->ioaddr + PCI20000_DIO_2);
517 if (mask & 0xff000000)
518 writeb((s->state >> 24) & 0xff,
519 devpriv->ioaddr + PCI20000_DIO_3);
521 data[1] = readb(devpriv->ioaddr + PCI20000_DIO_0);
522 data[1] |= readb(devpriv->ioaddr + PCI20000_DIO_1) << 8;
523 data[1] |= readb(devpriv->ioaddr + PCI20000_DIO_2) << 16;
524 data[1] |= readb(devpriv->ioaddr + PCI20000_DIO_3) << 24;
529 static void pci20xxx_dio_config(struct comedi_device * dev, struct comedi_subdevice * s)
531 unsigned char control_01;
532 unsigned char control_23;
533 unsigned char buffer;
535 control_01 = readb(devpriv->ioaddr + PCI20000_DIO_CONTROL_01);
536 control_23 = readb(devpriv->ioaddr + PCI20000_DIO_CONTROL_23);
537 buffer = readb(devpriv->ioaddr + PCI20000_DIO_BUFFER);
539 if (s->io_bits & 0x000000ff) {
541 control_01 &= PCI20000_DIO_EOC;
542 buffer = (buffer & (~(DIO_BE << DIO_PS_0))) | (DIO_BO <<
546 control_01 = (control_01 & DIO_CAND) | PCI20000_DIO_EIC;
547 buffer = (buffer & (~(DIO_BI << DIO_PS_0)));
549 if (s->io_bits & 0x0000ff00) {
551 control_01 &= PCI20000_DIO_OOC;
552 buffer = (buffer & (~(DIO_BE << DIO_PS_1))) | (DIO_BO <<
556 control_01 = (control_01 & DIO_CAND) | PCI20000_DIO_OIC;
557 buffer = (buffer & (~(DIO_BI << DIO_PS_1)));
559 if (s->io_bits & 0x00ff0000) {
561 control_23 &= PCI20000_DIO_EOC;
562 buffer = (buffer & (~(DIO_BE << DIO_PS_2))) | (DIO_BO <<
566 control_23 = (control_23 & DIO_CAND) | PCI20000_DIO_EIC;
567 buffer = (buffer & (~(DIO_BI << DIO_PS_2)));
569 if (s->io_bits & 0xff000000) {
571 control_23 &= PCI20000_DIO_OOC;
572 buffer = (buffer & (~(DIO_BE << DIO_PS_3))) | (DIO_BO <<
576 control_23 = (control_23 & DIO_CAND) | PCI20000_DIO_OIC;
577 buffer = (buffer & (~(DIO_BI << DIO_PS_3)));
579 writeb(control_01, devpriv->ioaddr + PCI20000_DIO_CONTROL_01);
580 writeb(control_23, devpriv->ioaddr + PCI20000_DIO_CONTROL_23);
581 writeb(buffer, devpriv->ioaddr + PCI20000_DIO_BUFFER);
585 static void pci20xxx_do(struct comedi_device * dev, struct comedi_subdevice * s)
587 /* XXX if the channel is configured for input, does this
589 /* XXX it would be a good idea to only update the registers
590 that _need_ to be updated. This requires changes to
592 writeb((s->state >> 0) & 0xff, devpriv->ioaddr + PCI20000_DIO_0);
593 writeb((s->state >> 8) & 0xff, devpriv->ioaddr + PCI20000_DIO_1);
594 writeb((s->state >> 16) & 0xff, devpriv->ioaddr + PCI20000_DIO_2);
595 writeb((s->state >> 24) & 0xff, devpriv->ioaddr + PCI20000_DIO_3);
598 static unsigned int pci20xxx_di(struct comedi_device * dev, struct comedi_subdevice * s)
600 /* XXX same note as above */
603 bits = readb(devpriv->ioaddr + PCI20000_DIO_0);
604 bits |= readb(devpriv->ioaddr + PCI20000_DIO_1) << 8;
605 bits |= readb(devpriv->ioaddr + PCI20000_DIO_2) << 16;
606 bits |= readb(devpriv->ioaddr + PCI20000_DIO_3) << 24;
612 COMEDI_INITCLEANUP(driver_pci20xxx);