1 // SPDX-License-Identifier: GPL-2.0
4 * Driver for Intelligent Instruments PCI-20001C carrier board and modules.
6 * Copyright (C) 2000 Markus Kempf <kempf@matsci.uni-sb.de>
7 * with suggestions from David Schleef 16.06.2000
12 * Description: Intelligent Instruments PCI-20001C carrier board
13 * Devices: [Intelligent Instrumentation] PCI-20001C (ii_pci20kc)
14 * Author: Markus Kempf <kempf@matsci.uni-sb.de>
17 * Supports the PCI-20001C-1a and PCI-20001C-2a carrier boards. The
18 * -2a version has 32 on-board DIO channels. Three add-on modules
19 * can be added to the carrier board for additional functionality.
21 * Supported add-on modules:
22 * PCI-20006M-1 1 channel, 16-bit analog output module
23 * PCI-20006M-2 2 channel, 16-bit analog output module
24 * PCI-20341M-1A 4 channel, 16-bit analog input module
27 * 0 Board base address
31 #include <linux/module.h>
33 #include <linux/comedi/comedidev.h>
38 #define II20K_SIZE 0x400
39 #define II20K_MOD_OFFSET 0x100
40 #define II20K_ID_REG 0x00
41 #define II20K_ID_MOD1_EMPTY BIT(7)
42 #define II20K_ID_MOD2_EMPTY BIT(6)
43 #define II20K_ID_MOD3_EMPTY BIT(5)
44 #define II20K_ID_MASK 0x1f
45 #define II20K_ID_PCI20001C_1A 0x1b /* no on-board DIO */
46 #define II20K_ID_PCI20001C_2A 0x1d /* on-board DIO */
47 #define II20K_MOD_STATUS_REG 0x40
48 #define II20K_MOD_STATUS_IRQ_MOD1 BIT(7)
49 #define II20K_MOD_STATUS_IRQ_MOD2 BIT(6)
50 #define II20K_MOD_STATUS_IRQ_MOD3 BIT(5)
51 #define II20K_DIO0_REG 0x80
52 #define II20K_DIO1_REG 0x81
53 #define II20K_DIR_ENA_REG 0x82
54 #define II20K_DIR_DIO3_OUT BIT(7)
55 #define II20K_DIR_DIO2_OUT BIT(6)
56 #define II20K_BUF_DISAB_DIO3 BIT(5)
57 #define II20K_BUF_DISAB_DIO2 BIT(4)
58 #define II20K_DIR_DIO1_OUT BIT(3)
59 #define II20K_DIR_DIO0_OUT BIT(2)
60 #define II20K_BUF_DISAB_DIO1 BIT(1)
61 #define II20K_BUF_DISAB_DIO0 BIT(0)
62 #define II20K_CTRL01_REG 0x83
63 #define II20K_CTRL01_SET BIT(7)
64 #define II20K_CTRL01_DIO0_IN BIT(4)
65 #define II20K_CTRL01_DIO1_IN BIT(1)
66 #define II20K_DIO2_REG 0xc0
67 #define II20K_DIO3_REG 0xc1
68 #define II20K_CTRL23_REG 0xc3
69 #define II20K_CTRL23_SET BIT(7)
70 #define II20K_CTRL23_DIO2_IN BIT(4)
71 #define II20K_CTRL23_DIO3_IN BIT(1)
73 #define II20K_ID_PCI20006M_1 0xe2 /* 1 AO channels */
74 #define II20K_ID_PCI20006M_2 0xe3 /* 2 AO channels */
75 #define II20K_AO_STRB_REG(x) (0x0b + ((x) * 0x08))
76 #define II20K_AO_LSB_REG(x) (0x0d + ((x) * 0x08))
77 #define II20K_AO_MSB_REG(x) (0x0e + ((x) * 0x08))
78 #define II20K_AO_STRB_BOTH_REG 0x1b
80 #define II20K_ID_PCI20341M_1 0x77 /* 4 AI channels */
81 #define II20K_AI_STATUS_CMD_REG 0x01
82 #define II20K_AI_STATUS_CMD_BUSY BIT(7)
83 #define II20K_AI_STATUS_CMD_HW_ENA BIT(1)
84 #define II20K_AI_STATUS_CMD_EXT_START BIT(0)
85 #define II20K_AI_LSB_REG 0x02
86 #define II20K_AI_MSB_REG 0x03
87 #define II20K_AI_PACER_RESET_REG 0x04
88 #define II20K_AI_16BIT_DATA_REG 0x06
89 #define II20K_AI_CONF_REG 0x10
90 #define II20K_AI_CONF_ENA BIT(2)
91 #define II20K_AI_OPT_REG 0x11
92 #define II20K_AI_OPT_TRIG_ENA BIT(5)
93 #define II20K_AI_OPT_TRIG_INV BIT(4)
94 #define II20K_AI_OPT_TIMEBASE(x) (((x) & 0x3) << 1)
95 #define II20K_AI_OPT_BURST_MODE BIT(0)
96 #define II20K_AI_STATUS_REG 0x12
97 #define II20K_AI_STATUS_INT BIT(7)
98 #define II20K_AI_STATUS_TRIG BIT(6)
99 #define II20K_AI_STATUS_TRIG_ENA BIT(5)
100 #define II20K_AI_STATUS_PACER_ERR BIT(2)
101 #define II20K_AI_STATUS_DATA_ERR BIT(1)
102 #define II20K_AI_STATUS_SET_TIME_ERR BIT(0)
103 #define II20K_AI_LAST_CHAN_ADDR_REG 0x13
104 #define II20K_AI_CUR_ADDR_REG 0x14
105 #define II20K_AI_SET_TIME_REG 0x15
106 #define II20K_AI_DELAY_LSB_REG 0x16
107 #define II20K_AI_DELAY_MSB_REG 0x17
108 #define II20K_AI_CHAN_ADV_REG 0x18
109 #define II20K_AI_CHAN_RESET_REG 0x19
110 #define II20K_AI_START_TRIG_REG 0x1a
111 #define II20K_AI_COUNT_RESET_REG 0x1b
112 #define II20K_AI_CHANLIST_REG 0x80
113 #define II20K_AI_CHANLIST_ONBOARD_ONLY BIT(5)
114 #define II20K_AI_CHANLIST_GAIN(x) (((x) & 0x3) << 3)
115 #define II20K_AI_CHANLIST_MUX_ENA BIT(2)
116 #define II20K_AI_CHANLIST_CHAN(x) (((x) & 0x3) << 0)
117 #define II20K_AI_CHANLIST_LEN 0x80
119 /* the AO range is set by jumpers on the 20006M module */
120 static const struct comedi_lrange ii20k_ao_ranges = {
122 BIP_RANGE(5), /* Chan 0 - W1/W3 in Chan 1 - W2/W4 in */
123 UNI_RANGE(10), /* Chan 0 - W1/W3 out Chan 1 - W2/W4 in */
124 BIP_RANGE(10) /* Chan 0 - W1/W3 in Chan 1 - W2/W4 out */
128 static const struct comedi_lrange ii20k_ai_ranges = {
130 BIP_RANGE(5), /* gain 1 */
131 BIP_RANGE(0.5), /* gain 10 */
132 BIP_RANGE(0.05), /* gain 100 */
133 BIP_RANGE(0.025) /* gain 200 */
137 static void __iomem *ii20k_module_iobase(struct comedi_device *dev,
138 struct comedi_subdevice *s)
140 return dev->mmio + (s->index + 1) * II20K_MOD_OFFSET;
143 static int ii20k_ao_insn_write(struct comedi_device *dev,
144 struct comedi_subdevice *s,
145 struct comedi_insn *insn,
148 void __iomem *iobase = ii20k_module_iobase(dev, s);
149 unsigned int chan = CR_CHAN(insn->chanspec);
152 for (i = 0; i < insn->n; i++) {
153 unsigned int val = data[i];
155 s->readback[chan] = val;
157 /* munge the offset binary data to 2's complement */
158 val = comedi_offset_munge(s, val);
160 writeb(val & 0xff, iobase + II20K_AO_LSB_REG(chan));
161 writeb((val >> 8) & 0xff, iobase + II20K_AO_MSB_REG(chan));
162 writeb(0x00, iobase + II20K_AO_STRB_REG(chan));
168 static int ii20k_ai_eoc(struct comedi_device *dev,
169 struct comedi_subdevice *s,
170 struct comedi_insn *insn,
171 unsigned long context)
173 void __iomem *iobase = ii20k_module_iobase(dev, s);
174 unsigned char status;
176 status = readb(iobase + II20K_AI_STATUS_REG);
177 if ((status & II20K_AI_STATUS_INT) == 0)
182 static void ii20k_ai_setup(struct comedi_device *dev,
183 struct comedi_subdevice *s,
184 unsigned int chanspec)
186 void __iomem *iobase = ii20k_module_iobase(dev, s);
187 unsigned int chan = CR_CHAN(chanspec);
188 unsigned int range = CR_RANGE(chanspec);
191 /* initialize module */
192 writeb(II20K_AI_CONF_ENA, iobase + II20K_AI_CONF_REG);
194 /* software conversion */
195 writeb(0, iobase + II20K_AI_STATUS_CMD_REG);
197 /* set the time base for the settling time counter based on the gain */
198 val = (range < 3) ? II20K_AI_OPT_TIMEBASE(0) : II20K_AI_OPT_TIMEBASE(2);
199 writeb(val, iobase + II20K_AI_OPT_REG);
201 /* set the settling time counter based on the gain */
202 val = (range < 2) ? 0x58 : (range < 3) ? 0x93 : 0x99;
203 writeb(val, iobase + II20K_AI_SET_TIME_REG);
205 /* set number of input channels */
206 writeb(1, iobase + II20K_AI_LAST_CHAN_ADDR_REG);
208 /* set the channel list byte */
209 val = II20K_AI_CHANLIST_ONBOARD_ONLY |
210 II20K_AI_CHANLIST_MUX_ENA |
211 II20K_AI_CHANLIST_GAIN(range) |
212 II20K_AI_CHANLIST_CHAN(chan);
213 writeb(val, iobase + II20K_AI_CHANLIST_REG);
215 /* reset settling time counter and trigger delay counter */
216 writeb(0, iobase + II20K_AI_COUNT_RESET_REG);
218 /* reset channel scanner */
219 writeb(0, iobase + II20K_AI_CHAN_RESET_REG);
222 static int ii20k_ai_insn_read(struct comedi_device *dev,
223 struct comedi_subdevice *s,
224 struct comedi_insn *insn,
227 void __iomem *iobase = ii20k_module_iobase(dev, s);
231 ii20k_ai_setup(dev, s, insn->chanspec);
233 for (i = 0; i < insn->n; i++) {
236 /* generate a software start convert signal */
237 readb(iobase + II20K_AI_PACER_RESET_REG);
239 ret = comedi_timeout(dev, s, insn, ii20k_ai_eoc, 0);
243 val = readb(iobase + II20K_AI_LSB_REG);
244 val |= (readb(iobase + II20K_AI_MSB_REG) << 8);
246 /* munge the 2's complement data to offset binary */
247 data[i] = comedi_offset_munge(s, val);
253 static void ii20k_dio_config(struct comedi_device *dev,
254 struct comedi_subdevice *s)
256 unsigned char ctrl01 = 0;
257 unsigned char ctrl23 = 0;
258 unsigned char dir_ena = 0;
260 /* port 0 - channels 0-7 */
261 if (s->io_bits & 0x000000ff) {
263 ctrl01 &= ~II20K_CTRL01_DIO0_IN;
264 dir_ena &= ~II20K_BUF_DISAB_DIO0;
265 dir_ena |= II20K_DIR_DIO0_OUT;
268 ctrl01 |= II20K_CTRL01_DIO0_IN;
269 dir_ena &= ~II20K_DIR_DIO0_OUT;
272 /* port 1 - channels 8-15 */
273 if (s->io_bits & 0x0000ff00) {
275 ctrl01 &= ~II20K_CTRL01_DIO1_IN;
276 dir_ena &= ~II20K_BUF_DISAB_DIO1;
277 dir_ena |= II20K_DIR_DIO1_OUT;
280 ctrl01 |= II20K_CTRL01_DIO1_IN;
281 dir_ena &= ~II20K_DIR_DIO1_OUT;
284 /* port 2 - channels 16-23 */
285 if (s->io_bits & 0x00ff0000) {
287 ctrl23 &= ~II20K_CTRL23_DIO2_IN;
288 dir_ena &= ~II20K_BUF_DISAB_DIO2;
289 dir_ena |= II20K_DIR_DIO2_OUT;
292 ctrl23 |= II20K_CTRL23_DIO2_IN;
293 dir_ena &= ~II20K_DIR_DIO2_OUT;
296 /* port 3 - channels 24-31 */
297 if (s->io_bits & 0xff000000) {
299 ctrl23 &= ~II20K_CTRL23_DIO3_IN;
300 dir_ena &= ~II20K_BUF_DISAB_DIO3;
301 dir_ena |= II20K_DIR_DIO3_OUT;
304 ctrl23 |= II20K_CTRL23_DIO3_IN;
305 dir_ena &= ~II20K_DIR_DIO3_OUT;
308 ctrl23 |= II20K_CTRL01_SET;
309 ctrl23 |= II20K_CTRL23_SET;
311 /* order is important */
312 writeb(ctrl01, dev->mmio + II20K_CTRL01_REG);
313 writeb(ctrl23, dev->mmio + II20K_CTRL23_REG);
314 writeb(dir_ena, dev->mmio + II20K_DIR_ENA_REG);
317 static int ii20k_dio_insn_config(struct comedi_device *dev,
318 struct comedi_subdevice *s,
319 struct comedi_insn *insn,
322 unsigned int chan = CR_CHAN(insn->chanspec);
335 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
339 ii20k_dio_config(dev, s);
344 static int ii20k_dio_insn_bits(struct comedi_device *dev,
345 struct comedi_subdevice *s,
346 struct comedi_insn *insn,
351 mask = comedi_dio_update_state(s, data);
353 if (mask & 0x000000ff)
354 writeb((s->state >> 0) & 0xff,
355 dev->mmio + II20K_DIO0_REG);
356 if (mask & 0x0000ff00)
357 writeb((s->state >> 8) & 0xff,
358 dev->mmio + II20K_DIO1_REG);
359 if (mask & 0x00ff0000)
360 writeb((s->state >> 16) & 0xff,
361 dev->mmio + II20K_DIO2_REG);
362 if (mask & 0xff000000)
363 writeb((s->state >> 24) & 0xff,
364 dev->mmio + II20K_DIO3_REG);
367 data[1] = readb(dev->mmio + II20K_DIO0_REG);
368 data[1] |= readb(dev->mmio + II20K_DIO1_REG) << 8;
369 data[1] |= readb(dev->mmio + II20K_DIO2_REG) << 16;
370 data[1] |= readb(dev->mmio + II20K_DIO3_REG) << 24;
375 static int ii20k_init_module(struct comedi_device *dev,
376 struct comedi_subdevice *s)
378 void __iomem *iobase = ii20k_module_iobase(dev, s);
382 id = readb(iobase + II20K_ID_REG);
384 case II20K_ID_PCI20006M_1:
385 case II20K_ID_PCI20006M_2:
386 /* Analog Output subdevice */
387 s->type = COMEDI_SUBD_AO;
388 s->subdev_flags = SDF_WRITABLE;
389 s->n_chan = (id == II20K_ID_PCI20006M_2) ? 2 : 1;
391 s->range_table = &ii20k_ao_ranges;
392 s->insn_write = ii20k_ao_insn_write;
394 ret = comedi_alloc_subdev_readback(s);
398 case II20K_ID_PCI20341M_1:
399 /* Analog Input subdevice */
400 s->type = COMEDI_SUBD_AI;
401 s->subdev_flags = SDF_READABLE | SDF_DIFF;
404 s->range_table = &ii20k_ai_ranges;
405 s->insn_read = ii20k_ai_insn_read;
408 s->type = COMEDI_SUBD_UNUSED;
415 static int ii20k_attach(struct comedi_device *dev,
416 struct comedi_devconfig *it)
418 struct comedi_subdevice *s;
419 unsigned int membase;
424 membase = it->options[0];
425 if (!membase || (membase & ~(0x100000 - II20K_SIZE))) {
426 dev_warn(dev->class_dev,
427 "%s: invalid memory address specified\n",
432 if (!request_mem_region(membase, II20K_SIZE, dev->board_name)) {
433 dev_warn(dev->class_dev, "%s: I/O mem conflict (%#x,%u)\n",
434 dev->board_name, membase, II20K_SIZE);
437 dev->iobase = membase; /* actually, a memory address */
439 dev->mmio = ioremap(membase, II20K_SIZE);
443 id = readb(dev->mmio + II20K_ID_REG);
444 switch (id & II20K_ID_MASK) {
445 case II20K_ID_PCI20001C_1A:
448 case II20K_ID_PCI20001C_2A:
455 ret = comedi_alloc_subdevices(dev, 4);
459 s = &dev->subdevices[0];
460 if (id & II20K_ID_MOD1_EMPTY) {
461 s->type = COMEDI_SUBD_UNUSED;
463 ret = ii20k_init_module(dev, s);
468 s = &dev->subdevices[1];
469 if (id & II20K_ID_MOD2_EMPTY) {
470 s->type = COMEDI_SUBD_UNUSED;
472 ret = ii20k_init_module(dev, s);
477 s = &dev->subdevices[2];
478 if (id & II20K_ID_MOD3_EMPTY) {
479 s->type = COMEDI_SUBD_UNUSED;
481 ret = ii20k_init_module(dev, s);
486 /* Digital I/O subdevice */
487 s = &dev->subdevices[3];
489 s->type = COMEDI_SUBD_DIO;
490 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
493 s->range_table = &range_digital;
494 s->insn_bits = ii20k_dio_insn_bits;
495 s->insn_config = ii20k_dio_insn_config;
497 /* default all channels to input */
498 ii20k_dio_config(dev, s);
500 s->type = COMEDI_SUBD_UNUSED;
506 static void ii20k_detach(struct comedi_device *dev)
510 if (dev->iobase) /* actually, a memory address */
511 release_mem_region(dev->iobase, II20K_SIZE);
514 static struct comedi_driver ii20k_driver = {
515 .driver_name = "ii_pci20kc",
516 .module = THIS_MODULE,
517 .attach = ii20k_attach,
518 .detach = ii20k_detach,
520 module_comedi_driver(ii20k_driver);
522 MODULE_AUTHOR("Comedi https://www.comedi.org");
523 MODULE_DESCRIPTION("Comedi driver for Intelligent Instruments PCI-20001C");
524 MODULE_LICENSE("GPL");