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
27 * instead of 4 digital ports
31 * The state of this driver is only a starting point for a complete
32 * COMEDI-driver. The final driver should support all features of the
33 * carrier board and modules.
35 * The test configuration:
37 * kernel 2.2.14 with RTAI v1.2 and patch-2.2.14rthal2
44 Description: Intelligent Instruments PCI-20001C carrier board
45 Author: Markus Kempf <kempf@matsci.uni-sb.de>
46 Devices: [Intelligent Instrumentation] PCI-20001C (ii_pci20kc)
49 Supports the PCI-20001 C-2a Carrier board, and could probably support
50 the other carrier boards with small modifications. Modules supported
52 PCI-20006 M-2 16-bit analog output module
53 PCI-20341 M-1A 16-bit analog input module
58 2 first option for module 1
59 3 second option for module 1
60 4 first option for module 2
61 5 second option for module 2
62 6 first option for module 3
63 7 second option for module 3
65 options for PCI-20006M:
66 first: Analog output channel 0 range configuration
67 0 bipolar 10 (-10V -- +10V)
68 1 unipolar 10 (0V -- +10V)
69 2 bipolar 5 (-5V -- 5V)
70 second: Analog output channel 1 range configuration
72 options for PCI-20341M:
73 first: Analog input gain configuration
80 #include "../comedidev.h"
82 #define PCI20000_ID 0x1d
83 #define PCI20341_ID 0x77
84 #define PCI20006_ID 0xe3
85 #define PCI20xxx_EMPTY_ID 0xff
87 #define PCI20000_OFFSET 0x100
88 #define PCI20000_MODULES 3
90 #define PCI20000_DIO_0 0x80
91 #define PCI20000_DIO_1 0x81
92 #define PCI20000_DIO_2 0xc0
93 #define PCI20000_DIO_3 0xc1
94 #define PCI20000_DIO_CONTROL_01 0x83 /* port 0, 1 control */
95 #define PCI20000_DIO_CONTROL_23 0xc3 /* port 2, 3 control */
96 #define PCI20000_DIO_BUFFER 0x82 /* buffer direction & enable */
97 #define PCI20000_DIO_EOC 0xef /* even port, control output */
98 #define PCI20000_DIO_OOC 0xfd /* odd port, control output */
99 #define PCI20000_DIO_EIC 0x90 /* even port, control input */
100 #define PCI20000_DIO_OIC 0x82 /* odd port, control input */
101 #define DIO_CAND 0x12 /* and bit 1 & 4 of control */
102 #define DIO_BE 0x01 /* buffer: port enable */
103 #define DIO_BO 0x04 /* buffer: output */
104 #define DIO_BI 0x05 /* buffer: input */
105 #define DIO_PS_0 0x00 /* buffer: port shift 0 */
106 #define DIO_PS_1 0x01 /* buffer: port shift 1 */
107 #define DIO_PS_2 0x04 /* buffer: port shift 2 */
108 #define DIO_PS_3 0x05 /* buffer: port shift 3 */
110 #define PCI20006_LCHAN0 0x0d
111 #define PCI20006_STROBE0 0x0b
112 #define PCI20006_LCHAN1 0x15
113 #define PCI20006_STROBE1 0x13
115 #define PCI20341_INIT 0x04
116 #define PCI20341_REPMODE 0x00 /* single shot mode */
117 #define PCI20341_PACER 0x00 /* Hardware Pacer disabled */
118 #define PCI20341_CHAN_NR 0x04 /* number of input channels */
119 #define PCI20341_CONFIG_REG 0x10
120 #define PCI20341_MOD_STATUS 0x01
121 #define PCI20341_OPT_REG 0x11
122 #define PCI20341_SET_TIME_REG 0x15
123 #define PCI20341_LCHAN_ADDR_REG 0x13
124 #define PCI20341_CHAN_LIST 0x80
125 #define PCI20341_CC_RESET 0x1b
126 #define PCI20341_CHAN_RESET 0x19
127 #define PCI20341_SOFT_PACER 0x04
128 #define PCI20341_STATUS_REG 0x12
129 #define PCI20341_LDATA 0x02
130 #define PCI20341_DAISY_CHAIN 0x20 /* On-board inputs only */
131 #define PCI20341_MUX 0x04 /* Enable on-board MUX */
132 #define PCI20341_SCANLIST 0x80 /* Channel/Gain Scan List */
134 union pci20xxx_subdev_private {
135 void __iomem *iobase;
137 void __iomem *iobase;
138 const struct comedi_lrange *ao_range_list[2];
139 /* range of channels of ao module */
140 unsigned int last_data[2];
143 void __iomem *iobase;
150 struct pci20xxx_private {
152 void __iomem *ioaddr;
153 union pci20xxx_subdev_private subdev_private[PCI20000_MODULES];
156 #define CHAN (CR_CHAN(it->chanlist[0]))
158 static int pci20006_init(struct comedi_device *dev, struct comedi_subdevice *s,
160 static int pci20341_init(struct comedi_device *dev, struct comedi_subdevice *s,
162 static int pci20xxx_dio_init(struct comedi_device *dev,
163 struct comedi_subdevice *s);
166 options[0] Board base address
168 options[2] first option for module 1
169 options[3] second option for module 1
170 options[4] first option for module 2
171 options[5] second option for module 2
172 options[6] first option for module 3
173 options[7] second option for module 3
175 options for PCI-20341M:
176 first Analog input gain configuration
182 options for PCI-20006M:
183 first Analog output channel 0 range configuration
184 0 == bipolar 10 (-10V -- +10V)
185 1 == unipolar 10V (0V -- +10V)
186 2 == bipolar 5V (-5V -- +5V)
187 second Analog output channel 1 range configuration
188 0 == bipolar 10 (-10V -- +10V)
189 1 == unipolar 10V (0V -- +10V)
190 2 == bipolar 5V (-5V -- +5V)
192 static int pci20xxx_attach(struct comedi_device *dev,
193 struct comedi_devconfig *it)
195 struct pci20xxx_private *devpriv;
199 struct comedi_subdevice *s;
200 union pci20xxx_subdev_private *sdp;
202 ret = comedi_alloc_subdevices(dev, 1 + PCI20000_MODULES);
206 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
209 dev->private = devpriv;
211 devpriv->ioaddr = (void __iomem *)(unsigned long)it->options[0];
213 /* Check PCI-20001 C-2A Carrier Board ID */
214 if ((readb(devpriv->ioaddr) & PCI20000_ID) != PCI20000_ID) {
215 dev_warn(dev->class_dev,
216 "PCI-20001 C-2A Carrier Board at base=0x%p not found !\n",
220 dev_info(dev->class_dev, "PCI-20001 C-2A at base=0x%p\n",
223 for (i = 0; i < PCI20000_MODULES; i++) {
224 s = &dev->subdevices[i];
225 id = readb(devpriv->ioaddr + (i + 1) * PCI20000_OFFSET);
226 s->private = devpriv->subdev_private + i;
230 sdp->pci20006.iobase =
231 devpriv->ioaddr + (i + 1) * PCI20000_OFFSET;
232 pci20006_init(dev, s, it->options[2 * i + 2],
233 it->options[2 * i + 3]);
234 dev_info(dev->class_dev,
235 "PCI-20006 module in slot %d\n", i + 1);
238 sdp->pci20341.iobase =
239 devpriv->ioaddr + (i + 1) * PCI20000_OFFSET;
240 pci20341_init(dev, s, it->options[2 * i + 2],
241 it->options[2 * i + 3]);
242 dev_info(dev->class_dev,
243 "PCI-20341 module in slot %d\n", i + 1);
246 dev_warn(dev->class_dev,
247 "unknown module code 0x%02x in slot %d: module disabled\n",
248 id, i); /* XXX this looks like a bug! i + 1 ?? */
250 case PCI20xxx_EMPTY_ID:
251 s->type = COMEDI_SUBD_UNUSED;
256 /* initialize struct pci20xxx_private */
257 pci20xxx_dio_init(dev, &dev->subdevices[PCI20000_MODULES]);
262 static void pci20xxx_detach(struct comedi_device *dev)
264 /* Nothing to cleanup */
269 static int pci20006_insn_read(struct comedi_device *dev,
270 struct comedi_subdevice *s,
271 struct comedi_insn *insn, unsigned int *data);
272 static int pci20006_insn_write(struct comedi_device *dev,
273 struct comedi_subdevice *s,
274 struct comedi_insn *insn, unsigned int *data);
276 static const struct comedi_lrange *pci20006_range_list[] = {
282 static int pci20006_init(struct comedi_device *dev, struct comedi_subdevice *s,
285 union pci20xxx_subdev_private *sdp = s->private;
287 if (opt0 < 0 || opt0 > 2)
289 if (opt1 < 0 || opt1 > 2)
292 sdp->pci20006.ao_range_list[0] = pci20006_range_list[opt0];
293 sdp->pci20006.ao_range_list[1] = pci20006_range_list[opt1];
296 s->type = COMEDI_SUBD_AO;
297 s->subdev_flags = SDF_WRITABLE;
300 s->insn_read = pci20006_insn_read;
301 s->insn_write = pci20006_insn_write;
303 s->range_table_list = sdp->pci20006.ao_range_list;
307 static int pci20006_insn_read(struct comedi_device *dev,
308 struct comedi_subdevice *s,
309 struct comedi_insn *insn, unsigned int *data)
311 union pci20xxx_subdev_private *sdp = s->private;
313 data[0] = sdp->pci20006.last_data[CR_CHAN(insn->chanspec)];
318 static int pci20006_insn_write(struct comedi_device *dev,
319 struct comedi_subdevice *s,
320 struct comedi_insn *insn, unsigned int *data)
322 union pci20xxx_subdev_private *sdp = s->private;
324 unsigned int boarddata;
326 sdp->pci20006.last_data[CR_CHAN(insn->chanspec)] = data[0];
327 boarddata = (((unsigned int)data[0] + 0x8000) & 0xffff);
328 /* comedi-data -> board-data */
329 lo = (boarddata & 0xff);
330 hi = ((boarddata >> 8) & 0xff);
332 switch (CR_CHAN(insn->chanspec)) {
334 writeb(lo, sdp->iobase + PCI20006_LCHAN0);
335 writeb(hi, sdp->iobase + PCI20006_LCHAN0 + 1);
336 writeb(0x00, sdp->iobase + PCI20006_STROBE0);
339 writeb(lo, sdp->iobase + PCI20006_LCHAN1);
340 writeb(hi, sdp->iobase + PCI20006_LCHAN1 + 1);
341 writeb(0x00, sdp->iobase + PCI20006_STROBE1);
344 dev_warn(dev->class_dev, "ao channel Error!\n");
353 static int pci20341_insn_read(struct comedi_device *dev,
354 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 = {
365 static const struct comedi_lrange range_bipolar0_05 = {
370 static const struct comedi_lrange range_bipolar0_025 = {
375 static const struct comedi_lrange *const pci20341_ranges[] = {
382 static int pci20341_init(struct comedi_device *dev, struct comedi_subdevice *s,
385 union pci20xxx_subdev_private *sdp = s->private;
388 /* options handling */
389 if (opt0 < 0 || opt0 > 3)
391 sdp->pci20341.timebase = pci20341_timebase[opt0];
392 sdp->pci20341.settling_time = pci20341_settling_time[opt0];
395 s->type = COMEDI_SUBD_AI;
396 s->subdev_flags = SDF_READABLE;
397 s->n_chan = PCI20341_CHAN_NR;
398 s->len_chanlist = PCI20341_SCANLIST;
399 s->insn_read = pci20341_insn_read;
401 s->range_table = pci20341_ranges[opt0];
403 /* depends on gain, trigger, repetition mode */
404 option = sdp->pci20341.timebase | PCI20341_REPMODE;
406 /* initialize Module */
407 writeb(PCI20341_INIT, sdp->iobase + PCI20341_CONFIG_REG);
409 writeb(PCI20341_PACER, sdp->iobase + PCI20341_MOD_STATUS);
410 /* option register */
411 writeb(option, sdp->iobase + PCI20341_OPT_REG);
412 /* settling time counter */
413 writeb(sdp->pci20341.settling_time,
414 sdp->iobase + PCI20341_SET_TIME_REG);
415 /* trigger not implemented */
419 static int pci20341_insn_read(struct comedi_device *dev,
420 struct comedi_subdevice *s,
421 struct comedi_insn *insn, unsigned int *data)
423 union pci20xxx_subdev_private *sdp = s->private;
424 unsigned int i = 0, j = 0;
426 unsigned char eoc; /* end of conversion */
427 unsigned int clb; /* channel list byte */
428 unsigned int boarddata;
430 /* write number of input channels */
431 writeb(1, sdp->iobase + PCI20341_LCHAN_ADDR_REG);
432 clb = PCI20341_DAISY_CHAIN | PCI20341_MUX | (sdp->pci20341.ai_gain << 3)
433 | CR_CHAN(insn->chanspec);
434 writeb(clb, sdp->iobase + PCI20341_CHAN_LIST);
436 /* reset settling time counter and trigger delay counter */
437 writeb(0x00, sdp->iobase + PCI20341_CC_RESET);
439 writeb(0x00, sdp->iobase + PCI20341_CHAN_RESET);
443 for (i = 0; i < insn->n; i++) {
444 /* data polling isn't the niciest way to get the data, I know,
445 * but there are only 6 cycles (mean) and it is easier than
446 * the whole interrupt stuff
450 readb(sdp->iobase + PCI20341_SOFT_PACER);
452 eoc = readb(sdp->iobase + PCI20341_STATUS_REG);
453 /* poll Interrupt Flag */
454 while ((eoc < 0x80) && j < 100) {
456 eoc = readb(sdp->iobase + PCI20341_STATUS_REG);
459 dev_warn(dev->class_dev,
460 "AI interrupt channel %i polling exit !\n", i);
463 lo = readb(sdp->iobase + PCI20341_LDATA);
464 hi = readb(sdp->iobase + PCI20341_LDATA + 1);
465 boarddata = lo + 0x100 * hi;
467 /* board-data -> comedi-data */
468 data[i] = (short)((boarddata + 0x8000) & 0xffff);
476 static void pci20xxx_dio_config(struct comedi_device *dev,
477 struct comedi_subdevice *s);
478 static int pci20xxx_dio_insn_bits(struct comedi_device *dev,
479 struct comedi_subdevice *s,
480 struct comedi_insn *insn, unsigned int *data);
481 static int pci20xxx_dio_insn_config(struct comedi_device *dev,
482 struct comedi_subdevice *s,
483 struct comedi_insn *insn,
486 /* initialize struct pci20xxx_private */
487 static int pci20xxx_dio_init(struct comedi_device *dev,
488 struct comedi_subdevice *s)
491 s->type = COMEDI_SUBD_DIO;
492 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
494 s->insn_bits = pci20xxx_dio_insn_bits;
495 s->insn_config = pci20xxx_dio_insn_config;
497 s->len_chanlist = 32;
498 s->range_table = &range_digital;
501 /* digital I/O lines default to input on board reset. */
502 pci20xxx_dio_config(dev, s);
507 static int pci20xxx_dio_insn_config(struct comedi_device *dev,
508 struct comedi_subdevice *s,
509 struct comedi_insn *insn,
514 mask = 1 << CR_CHAN(insn->chanspec);
515 if (mask & 0x000000ff)
517 else if (mask & 0x0000ff00)
519 else if (mask & 0x00ff0000)
527 pci20xxx_dio_config(dev, s);
532 static int pci20xxx_dio_insn_bits(struct comedi_device *dev,
533 struct comedi_subdevice *s,
534 struct comedi_insn *insn, unsigned int *data)
536 struct pci20xxx_private *devpriv = dev->private;
537 unsigned int mask = data[0];
540 s->state |= (mask & data[1]);
543 if (mask & 0x000000ff)
544 writeb((s->state >> 0) & 0xff,
545 devpriv->ioaddr + PCI20000_DIO_0);
546 if (mask & 0x0000ff00)
547 writeb((s->state >> 8) & 0xff,
548 devpriv->ioaddr + PCI20000_DIO_1);
549 if (mask & 0x00ff0000)
550 writeb((s->state >> 16) & 0xff,
551 devpriv->ioaddr + PCI20000_DIO_2);
552 if (mask & 0xff000000)
553 writeb((s->state >> 24) & 0xff,
554 devpriv->ioaddr + PCI20000_DIO_3);
556 data[1] = readb(devpriv->ioaddr + PCI20000_DIO_0);
557 data[1] |= readb(devpriv->ioaddr + PCI20000_DIO_1) << 8;
558 data[1] |= readb(devpriv->ioaddr + PCI20000_DIO_2) << 16;
559 data[1] |= readb(devpriv->ioaddr + PCI20000_DIO_3) << 24;
564 static void pci20xxx_dio_config(struct comedi_device *dev,
565 struct comedi_subdevice *s)
567 struct pci20xxx_private *devpriv = dev->private;
568 unsigned char control_01;
569 unsigned char control_23;
570 unsigned char buffer;
572 control_01 = readb(devpriv->ioaddr + PCI20000_DIO_CONTROL_01);
573 control_23 = readb(devpriv->ioaddr + PCI20000_DIO_CONTROL_23);
574 buffer = readb(devpriv->ioaddr + PCI20000_DIO_BUFFER);
576 if (s->io_bits & 0x000000ff) {
578 control_01 &= PCI20000_DIO_EOC;
579 buffer = (buffer & (~(DIO_BE << DIO_PS_0))) | (DIO_BO <<
583 control_01 = (control_01 & DIO_CAND) | PCI20000_DIO_EIC;
584 buffer = (buffer & (~(DIO_BI << DIO_PS_0)));
586 if (s->io_bits & 0x0000ff00) {
588 control_01 &= PCI20000_DIO_OOC;
589 buffer = (buffer & (~(DIO_BE << DIO_PS_1))) | (DIO_BO <<
593 control_01 = (control_01 & DIO_CAND) | PCI20000_DIO_OIC;
594 buffer = (buffer & (~(DIO_BI << DIO_PS_1)));
596 if (s->io_bits & 0x00ff0000) {
598 control_23 &= PCI20000_DIO_EOC;
599 buffer = (buffer & (~(DIO_BE << DIO_PS_2))) | (DIO_BO <<
603 control_23 = (control_23 & DIO_CAND) | PCI20000_DIO_EIC;
604 buffer = (buffer & (~(DIO_BI << DIO_PS_2)));
606 if (s->io_bits & 0xff000000) {
608 control_23 &= PCI20000_DIO_OOC;
609 buffer = (buffer & (~(DIO_BE << DIO_PS_3))) | (DIO_BO <<
613 control_23 = (control_23 & DIO_CAND) | PCI20000_DIO_OIC;
614 buffer = (buffer & (~(DIO_BI << DIO_PS_3)));
616 writeb(control_01, devpriv->ioaddr + PCI20000_DIO_CONTROL_01);
617 writeb(control_23, devpriv->ioaddr + PCI20000_DIO_CONTROL_23);
618 writeb(buffer, devpriv->ioaddr + PCI20000_DIO_BUFFER);
622 static void pci20xxx_do(struct comedi_device *dev, struct comedi_subdevice *s)
624 struct pci20xxx_private *devpriv = dev->private;
626 /* XXX if the channel is configured for input, does this
628 /* XXX it would be a good idea to only update the registers
629 that _need_ to be updated. This requires changes to
631 writeb((s->state >> 0) & 0xff, devpriv->ioaddr + PCI20000_DIO_0);
632 writeb((s->state >> 8) & 0xff, devpriv->ioaddr + PCI20000_DIO_1);
633 writeb((s->state >> 16) & 0xff, devpriv->ioaddr + PCI20000_DIO_2);
634 writeb((s->state >> 24) & 0xff, devpriv->ioaddr + PCI20000_DIO_3);
637 static unsigned int pci20xxx_di(struct comedi_device *dev,
638 struct comedi_subdevice *s)
640 struct pci20xxx_private *devpriv = dev->private;
643 /* XXX same note as above */
644 bits = readb(devpriv->ioaddr + PCI20000_DIO_0);
645 bits |= readb(devpriv->ioaddr + PCI20000_DIO_1) << 8;
646 bits |= readb(devpriv->ioaddr + PCI20000_DIO_2) << 16;
647 bits |= readb(devpriv->ioaddr + PCI20000_DIO_3) << 24;
653 static struct comedi_driver pci20xxx_driver = {
654 .driver_name = "ii_pci20kc",
655 .module = THIS_MODULE,
656 .attach = pci20xxx_attach,
657 .detach = pci20xxx_detach,
659 module_comedi_driver(pci20xxx_driver);
661 MODULE_AUTHOR("Comedi http://www.comedi.org");
662 MODULE_DESCRIPTION("Comedi low-level driver");
663 MODULE_LICENSE("GPL");