2 comedi/drivers/ni_atmio16d.c
3 Hardware driver for National Instruments AT-MIO16D board
4 Copyright (C) 2000 Chris R. Baugher <baugher@enteract.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 Description: National Instruments AT-MIO-16D
24 Author: Chris R. Baugher <baugher@enteract.com>
26 Devices: [National Instruments] AT-MIO-16 (atmio16), AT-MIO-16D (atmio16d)
29 * I must give credit here to Michal Dobes <dobes@tesnet.cz> who
30 * wrote the driver for Advantec's pcl812 boards. I used the interrupt
31 * handling code from his driver as an example for this one.
38 #include "../comedidev.h"
40 #include <linux/ioport.h>
44 /* Configuration and Status Registers */
45 #define COM_REG_1 0x00 /* wo 16 */
46 #define STAT_REG 0x00 /* ro 16 */
47 #define COM_REG_2 0x02 /* wo 16 */
48 /* Event Strobe Registers */
49 #define START_CONVERT_REG 0x08 /* wo 16 */
50 #define START_DAQ_REG 0x0A /* wo 16 */
51 #define AD_CLEAR_REG 0x0C /* wo 16 */
52 #define EXT_STROBE_REG 0x0E /* wo 16 */
53 /* Analog Output Registers */
54 #define DAC0_REG 0x10 /* wo 16 */
55 #define DAC1_REG 0x12 /* wo 16 */
56 #define INT2CLR_REG 0x14 /* wo 16 */
57 /* Analog Input Registers */
58 #define MUX_CNTR_REG 0x04 /* wo 16 */
59 #define MUX_GAIN_REG 0x06 /* wo 16 */
60 #define AD_FIFO_REG 0x16 /* ro 16 */
61 #define DMA_TC_INT_CLR_REG 0x16 /* wo 16 */
62 /* AM9513A Counter/Timer Registers */
63 #define AM9513A_DATA_REG 0x18 /* rw 16 */
64 #define AM9513A_COM_REG 0x1A /* wo 16 */
65 #define AM9513A_STAT_REG 0x1A /* ro 16 */
66 /* MIO-16 Digital I/O Registers */
67 #define MIO_16_DIG_IN_REG 0x1C /* ro 16 */
68 #define MIO_16_DIG_OUT_REG 0x1C /* wo 16 */
69 /* RTSI Switch Registers */
70 #define RTSI_SW_SHIFT_REG 0x1E /* wo 8 */
71 #define RTSI_SW_STROBE_REG 0x1F /* wo 8 */
72 /* DIO-24 Registers */
73 #define DIO_24_PORTA_REG 0x00 /* rw 8 */
74 #define DIO_24_PORTB_REG 0x01 /* rw 8 */
75 #define DIO_24_PORTC_REG 0x02 /* rw 8 */
76 #define DIO_24_CNFG_REG 0x03 /* wo 8 */
78 /* Command Register bits */
79 #define COMREG1_2SCADC 0x0001
80 #define COMREG1_1632CNT 0x0002
81 #define COMREG1_SCANEN 0x0008
82 #define COMREG1_DAQEN 0x0010
83 #define COMREG1_DMAEN 0x0020
84 #define COMREG1_CONVINTEN 0x0080
85 #define COMREG2_SCN2 0x0010
86 #define COMREG2_INTEN 0x0080
87 #define COMREG2_DOUTEN0 0x0100
88 #define COMREG2_DOUTEN1 0x0200
89 /* Status Register bits */
90 #define STAT_AD_OVERRUN 0x0100
91 #define STAT_AD_OVERFLOW 0x0200
92 #define STAT_AD_DAQPROG 0x0800
93 #define STAT_AD_CONVAVAIL 0x2000
94 #define STAT_AD_DAQSTOPINT 0x4000
95 /* AM9513A Counter/Timer defines */
96 #define CLOCK_1_MHZ 0x8B25
97 #define CLOCK_100_KHZ 0x8C25
98 #define CLOCK_10_KHZ 0x8D25
99 #define CLOCK_1_KHZ 0x8E25
100 #define CLOCK_100_HZ 0x8F25
101 /* Other miscellaneous defines */
102 #define ATMIO16D_SIZE 32 /* bus address range */
103 #define devpriv ((struct atmio16d_private *)dev->private)
104 #define ATMIO16D_TIMEOUT 10
106 struct atmio16_board_t {
112 static const struct atmio16_board_t atmio16_boards[] = {
123 #define n_atmio16_boards sizeof(atmio16_boards)/sizeof(atmio16_boards[0])
125 #define boardtype ((const struct atmio16_board_t *)dev->board_ptr)
127 /* function prototypes */
128 static int atmio16d_attach(struct comedi_device * dev, struct comedi_devconfig * it);
129 static int atmio16d_detach(struct comedi_device * dev);
130 static irqreturn_t atmio16d_interrupt(int irq, void *d);
131 static int atmio16d_ai_cmdtest(struct comedi_device * dev, struct comedi_subdevice * s,
132 struct comedi_cmd * cmd);
133 static int atmio16d_ai_cmd(struct comedi_device * dev, struct comedi_subdevice * s);
134 static int atmio16d_ai_cancel(struct comedi_device * dev, struct comedi_subdevice * s);
135 static void reset_counters(struct comedi_device * dev);
136 static void reset_atmio16d(struct comedi_device * dev);
138 /* main driver struct */
139 static struct comedi_driver driver_atmio16d = {
140 driver_name:"atmio16",
142 attach:atmio16d_attach,
143 detach:atmio16d_detach,
144 board_name:&atmio16_boards[0].name,
145 num_names:n_atmio16_boards,
146 offset:sizeof(struct atmio16_board_t),
149 COMEDI_INITCLEANUP(driver_atmio16d);
152 static const struct comedi_lrange range_atmio16d_ai_10_bipolar = { 4, {
160 static const struct comedi_lrange range_atmio16d_ai_5_bipolar = { 4, {
168 static const struct comedi_lrange range_atmio16d_ai_unipolar = { 4, {
176 /* private data struct */
177 struct atmio16d_private {
178 enum { adc_diff, adc_singleended } adc_mux;
179 enum { adc_bipolar10, adc_bipolar5, adc_unipolar10 } adc_range;
180 enum { adc_2comp, adc_straight } adc_coding;
181 enum { dac_bipolar, dac_unipolar } dac0_range, dac1_range;
182 enum { dac_internal, dac_external } dac0_reference, dac1_reference;
183 enum { dac_2comp, dac_straight } dac0_coding, dac1_coding;
184 const struct comedi_lrange *ao_range_type_list[2];
185 unsigned int ao_readback[2];
186 unsigned int com_reg_1_state; /* current state of command register 1 */
187 unsigned int com_reg_2_state; /* current state of command register 2 */
190 static void reset_counters(struct comedi_device *dev)
193 outw(0xFFC2, dev->iobase + AM9513A_COM_REG);
194 outw(0xFF02, dev->iobase + AM9513A_COM_REG);
195 outw(0x4, dev->iobase + AM9513A_DATA_REG);
196 outw(0xFF0A, dev->iobase + AM9513A_COM_REG);
197 outw(0x3, dev->iobase + AM9513A_DATA_REG);
198 outw(0xFF42, dev->iobase + AM9513A_COM_REG);
199 outw(0xFF42, dev->iobase + AM9513A_COM_REG);
201 outw(0xFFC4, dev->iobase + AM9513A_COM_REG);
202 outw(0xFF03, dev->iobase + AM9513A_COM_REG);
203 outw(0x4, dev->iobase + AM9513A_DATA_REG);
204 outw(0xFF0B, dev->iobase + AM9513A_COM_REG);
205 outw(0x3, dev->iobase + AM9513A_DATA_REG);
206 outw(0xFF44, dev->iobase + AM9513A_COM_REG);
207 outw(0xFF44, dev->iobase + AM9513A_COM_REG);
209 outw(0xFFC8, dev->iobase + AM9513A_COM_REG);
210 outw(0xFF04, dev->iobase + AM9513A_COM_REG);
211 outw(0x4, dev->iobase + AM9513A_DATA_REG);
212 outw(0xFF0C, dev->iobase + AM9513A_COM_REG);
213 outw(0x3, dev->iobase + AM9513A_DATA_REG);
214 outw(0xFF48, dev->iobase + AM9513A_COM_REG);
215 outw(0xFF48, dev->iobase + AM9513A_COM_REG);
217 outw(0xFFD0, dev->iobase + AM9513A_COM_REG);
218 outw(0xFF05, dev->iobase + AM9513A_COM_REG);
219 outw(0x4, dev->iobase + AM9513A_DATA_REG);
220 outw(0xFF0D, dev->iobase + AM9513A_COM_REG);
221 outw(0x3, dev->iobase + AM9513A_DATA_REG);
222 outw(0xFF50, dev->iobase + AM9513A_COM_REG);
223 outw(0xFF50, dev->iobase + AM9513A_COM_REG);
225 outw(0, dev->iobase + AD_CLEAR_REG);
228 static void reset_atmio16d(struct comedi_device *dev)
232 /* now we need to initialize the board */
233 outw(0, dev->iobase + COM_REG_1);
234 outw(0, dev->iobase + COM_REG_2);
235 outw(0, dev->iobase + MUX_GAIN_REG);
236 /* init AM9513A timer */
237 outw(0xFFFF, dev->iobase + AM9513A_COM_REG);
238 outw(0xFFEF, dev->iobase + AM9513A_COM_REG);
239 outw(0xFF17, dev->iobase + AM9513A_COM_REG);
240 outw(0xF000, dev->iobase + AM9513A_DATA_REG);
241 for (i = 1; i <= 5; ++i) {
242 outw(0xFF00 + i, dev->iobase + AM9513A_COM_REG);
243 outw(0x0004, dev->iobase + AM9513A_DATA_REG);
244 outw(0xFF08 + i, dev->iobase + AM9513A_COM_REG);
245 outw(0x3, dev->iobase + AM9513A_DATA_REG);
247 outw(0xFF5F, dev->iobase + AM9513A_COM_REG);
248 /* timer init done */
249 outw(0, dev->iobase + AD_CLEAR_REG);
250 outw(0, dev->iobase + INT2CLR_REG);
251 /* select straight binary mode for Analog Input */
252 devpriv->com_reg_1_state |= 1;
253 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
254 devpriv->adc_coding = adc_straight;
255 /* zero the analog outputs */
256 outw(2048, dev->iobase + DAC0_REG);
257 outw(2048, dev->iobase + DAC1_REG);
260 static irqreturn_t atmio16d_interrupt(int irq, void *d)
262 struct comedi_device *dev = d;
263 struct comedi_subdevice *s = dev->subdevices + 0;
265 /* printk("atmio16d_interrupt!\n"); */
267 comedi_buf_put(s->async, inw(dev->iobase + AD_FIFO_REG));
269 comedi_event(dev, s);
273 static int atmio16d_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
274 struct comedi_cmd *cmd)
278 printk("atmio16d_ai_cmdtest\n");
280 /* make sure triggers are valid */
281 tmp = cmd->start_src;
282 cmd->start_src &= TRIG_NOW;
283 if (!cmd->start_src || tmp != cmd->start_src)
286 tmp = cmd->scan_begin_src;
287 cmd->scan_begin_src &= TRIG_FOLLOW | TRIG_TIMER;
288 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
291 tmp = cmd->convert_src;
292 cmd->convert_src &= TRIG_TIMER;
293 if (!cmd->convert_src || tmp != cmd->convert_src)
296 tmp = cmd->scan_end_src;
297 cmd->scan_end_src &= TRIG_COUNT;
298 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
302 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
303 if (!cmd->stop_src || tmp != cmd->stop_src)
309 /* step 2: make sure trigger sources are unique and mutually compatible */
310 /* note that mutual compatiblity is not an issue here */
311 if (cmd->scan_begin_src != TRIG_FOLLOW &&
312 cmd->scan_begin_src != TRIG_EXT &&
313 cmd->scan_begin_src != TRIG_TIMER)
315 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
321 /* step 3: make sure arguments are trivially compatible */
323 if (cmd->start_arg != 0) {
327 if (cmd->scan_begin_src == TRIG_FOLLOW) {
328 /* internal trigger */
329 if (cmd->scan_begin_arg != 0) {
330 cmd->scan_begin_arg = 0;
335 /* external trigger */
336 /* should be level/edge, hi/lo specification here */
337 if (cmd->scan_begin_arg != 0) {
338 cmd->scan_begin_arg = 0;
344 if (cmd->convert_arg < 10000) {
345 cmd->convert_arg = 10000;
349 if (cmd->convert_arg > SLOWEST_TIMER) {
350 cmd->convert_arg = SLOWEST_TIMER;
354 if (cmd->scan_end_arg != cmd->chanlist_len) {
355 cmd->scan_end_arg = cmd->chanlist_len;
358 if (cmd->stop_src == TRIG_COUNT) {
359 /* any count is allowed */
362 if (cmd->stop_arg != 0) {
374 static int atmio16d_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
376 struct comedi_cmd *cmd = &s->async->cmd;
377 unsigned int timer, base_clock;
378 unsigned int sample_count, tmp, chan, gain;
381 printk("atmio16d_ai_cmd\n");
383 /* This is slowly becoming a working command interface. *
384 * It is still uber-experimental */
387 s->async->cur_chan = 0;
389 /* check if scanning multiple channels */
390 if (cmd->chanlist_len < 2) {
391 devpriv->com_reg_1_state &= ~COMREG1_SCANEN;
392 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
394 devpriv->com_reg_1_state |= COMREG1_SCANEN;
395 devpriv->com_reg_2_state |= COMREG2_SCN2;
396 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
397 outw(devpriv->com_reg_2_state, dev->iobase + COM_REG_2);
400 /* Setup the Mux-Gain Counter */
401 for (i = 0; i < cmd->chanlist_len; ++i) {
402 chan = CR_CHAN(cmd->chanlist[i]);
403 gain = CR_RANGE(cmd->chanlist[i]);
404 outw(i, dev->iobase + MUX_CNTR_REG);
405 tmp = chan | (gain << 6);
406 if (i == cmd->scan_end_arg - 1)
407 tmp |= 0x0010; /* set LASTONE bit */
408 outw(tmp, dev->iobase + MUX_GAIN_REG);
411 /* Now program the sample interval timer */
412 /* Figure out which clock to use then get an
413 * appropriate timer value */
414 if (cmd->convert_arg < 65536000) {
415 base_clock = CLOCK_1_MHZ;
416 timer = cmd->convert_arg / 1000;
417 } else if (cmd->convert_arg < 655360000) {
418 base_clock = CLOCK_100_KHZ;
419 timer = cmd->convert_arg / 10000;
420 } else if (cmd->convert_arg <= 0xffffffff /* 6553600000 */) {
421 base_clock = CLOCK_10_KHZ;
422 timer = cmd->convert_arg / 100000;
423 } else if (cmd->convert_arg <= 0xffffffff /* 65536000000 */) {
424 base_clock = CLOCK_1_KHZ;
425 timer = cmd->convert_arg / 1000000;
427 outw(0xFF03, dev->iobase + AM9513A_COM_REG);
428 outw(base_clock, dev->iobase + AM9513A_DATA_REG);
429 outw(0xFF0B, dev->iobase + AM9513A_COM_REG);
430 outw(0x2, dev->iobase + AM9513A_DATA_REG);
431 outw(0xFF44, dev->iobase + AM9513A_COM_REG);
432 outw(0xFFF3, dev->iobase + AM9513A_COM_REG);
433 outw(timer, dev->iobase + AM9513A_DATA_REG);
434 outw(0xFF24, dev->iobase + AM9513A_COM_REG);
436 /* Now figure out how many samples to get */
437 /* and program the sample counter */
438 sample_count = cmd->stop_arg * cmd->scan_end_arg;
439 outw(0xFF04, dev->iobase + AM9513A_COM_REG);
440 outw(0x1025, dev->iobase + AM9513A_DATA_REG);
441 outw(0xFF0C, dev->iobase + AM9513A_COM_REG);
442 if (sample_count < 65536) {
443 /* use only Counter 4 */
444 outw(sample_count, dev->iobase + AM9513A_DATA_REG);
445 outw(0xFF48, dev->iobase + AM9513A_COM_REG);
446 outw(0xFFF4, dev->iobase + AM9513A_COM_REG);
447 outw(0xFF28, dev->iobase + AM9513A_COM_REG);
448 devpriv->com_reg_1_state &= ~COMREG1_1632CNT;
449 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
451 /* Counter 4 and 5 are needed */
452 if ((tmp = sample_count & 0xFFFF)) {
453 outw(tmp - 1, dev->iobase + AM9513A_DATA_REG);
455 outw(0xFFFF, dev->iobase + AM9513A_DATA_REG);
457 outw(0xFF48, dev->iobase + AM9513A_COM_REG);
458 outw(0, dev->iobase + AM9513A_DATA_REG);
459 outw(0xFF28, dev->iobase + AM9513A_COM_REG);
460 outw(0xFF05, dev->iobase + AM9513A_COM_REG);
461 outw(0x25, dev->iobase + AM9513A_DATA_REG);
462 outw(0xFF0D, dev->iobase + AM9513A_COM_REG);
463 tmp = sample_count & 0xFFFF;
464 if ((tmp == 0) || (tmp == 1)) {
465 outw((sample_count >> 16) & 0xFFFF,
466 dev->iobase + AM9513A_DATA_REG);
468 outw(((sample_count >> 16) & 0xFFFF) + 1,
469 dev->iobase + AM9513A_DATA_REG);
471 outw(0xFF70, dev->iobase + AM9513A_COM_REG);
472 devpriv->com_reg_1_state |= COMREG1_1632CNT;
473 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
476 /* Program the scan interval timer ONLY IF SCANNING IS ENABLED */
477 /* Figure out which clock to use then get an
478 * appropriate timer value */
479 if (cmd->chanlist_len > 1) {
480 if (cmd->scan_begin_arg < 65536000) {
481 base_clock = CLOCK_1_MHZ;
482 timer = cmd->scan_begin_arg / 1000;
483 } else if (cmd->scan_begin_arg < 655360000) {
484 base_clock = CLOCK_100_KHZ;
485 timer = cmd->scan_begin_arg / 10000;
486 } else if (cmd->scan_begin_arg < 0xffffffff /* 6553600000 */) {
487 base_clock = CLOCK_10_KHZ;
488 timer = cmd->scan_begin_arg / 100000;
489 } else if (cmd->scan_begin_arg < 0xffffffff /* 65536000000 */) {
490 base_clock = CLOCK_1_KHZ;
491 timer = cmd->scan_begin_arg / 1000000;
493 outw(0xFF02, dev->iobase + AM9513A_COM_REG);
494 outw(base_clock, dev->iobase + AM9513A_DATA_REG);
495 outw(0xFF0A, dev->iobase + AM9513A_COM_REG);
496 outw(0x2, dev->iobase + AM9513A_DATA_REG);
497 outw(0xFF42, dev->iobase + AM9513A_COM_REG);
498 outw(0xFFF2, dev->iobase + AM9513A_COM_REG);
499 outw(timer, dev->iobase + AM9513A_DATA_REG);
500 outw(0xFF22, dev->iobase + AM9513A_COM_REG);
503 /* Clear the A/D FIFO and reset the MUX counter */
504 outw(0, dev->iobase + AD_CLEAR_REG);
505 outw(0, dev->iobase + MUX_CNTR_REG);
506 outw(0, dev->iobase + INT2CLR_REG);
507 /* enable this acquisition operation */
508 devpriv->com_reg_1_state |= COMREG1_DAQEN;
509 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
510 /* enable interrupts for conversion completion */
511 devpriv->com_reg_1_state |= COMREG1_CONVINTEN;
512 devpriv->com_reg_2_state |= COMREG2_INTEN;
513 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
514 outw(devpriv->com_reg_2_state, dev->iobase + COM_REG_2);
515 /* apply a trigger. this starts the counters! */
516 outw(0, dev->iobase + START_DAQ_REG);
521 /* This will cancel a running acquisition operation */
522 static int atmio16d_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
529 /* Mode 0 is used to get a single conversion on demand */
530 static int atmio16d_ai_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
531 struct comedi_insn *insn, unsigned int *data)
539 printk("atmio16d_ai_insn_read\n");
541 chan = CR_CHAN(insn->chanspec);
542 gain = CR_RANGE(insn->chanspec);
544 /* reset the Analog input circuitry */
545 /* outw( 0, dev->iobase+AD_CLEAR_REG ); */
546 /* reset the Analog Input MUX Counter to 0 */
547 /* outw( 0, dev->iobase+MUX_CNTR_REG ); */
549 /* set the Input MUX gain */
550 outw(chan | (gain << 6), dev->iobase + MUX_GAIN_REG);
552 for (i = 0; i < insn->n; i++) {
553 /* start the conversion */
554 outw(0, dev->iobase + START_CONVERT_REG);
555 /* wait for it to finish */
556 for (t = 0; t < ATMIO16D_TIMEOUT; t++) {
557 /* check conversion status */
558 status = inw(dev->iobase + STAT_REG);
560 printk("status=%x\n", status);
562 if (status & STAT_AD_CONVAVAIL) {
563 /* read the data now */
564 data[i] = inw(dev->iobase + AD_FIFO_REG);
565 /* change to two's complement if need be */
566 if (devpriv->adc_coding == adc_2comp) {
571 if (status & STAT_AD_OVERFLOW) {
572 printk("atmio16d: a/d FIFO overflow\n");
573 outw(0, dev->iobase + AD_CLEAR_REG);
578 /* end waiting, now check if it timed out */
579 if (t == ATMIO16D_TIMEOUT) {
580 rt_printk("atmio16d: timeout\n");
589 static int atmio16d_ao_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
590 struct comedi_insn *insn, unsigned int *data)
594 printk("atmio16d_ao_insn_read\n");
597 for (i = 0; i < insn->n; i++) {
598 data[i] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
604 static int atmio16d_ao_insn_write(struct comedi_device *dev, struct comedi_subdevice *s,
605 struct comedi_insn *insn, unsigned int *data)
611 printk("atmio16d_ao_insn_write\n");
614 chan = CR_CHAN(insn->chanspec);
616 for (i = 0; i < insn->n; i++) {
620 if (devpriv->dac0_coding == dac_2comp) {
623 outw(d, dev->iobase + DAC0_REG);
626 if (devpriv->dac1_coding == dac_2comp) {
629 outw(d, dev->iobase + DAC1_REG);
634 devpriv->ao_readback[chan] = data[i];
639 static int atmio16d_dio_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
640 struct comedi_insn *insn, unsigned int *data)
646 s->state &= ~data[0];
647 s->state |= (data[0] | data[1]);
648 outw(s->state, dev->iobase + MIO_16_DIG_OUT_REG);
650 data[1] = inw(dev->iobase + MIO_16_DIG_IN_REG);
655 static int atmio16d_dio_insn_config(struct comedi_device *dev, struct comedi_subdevice *s,
656 struct comedi_insn *insn, unsigned int *data)
661 for (i = 0; i < insn->n; i++) {
662 mask = (CR_CHAN(insn->chanspec) < 4) ? 0x0f : 0xf0;
667 devpriv->com_reg_2_state &= ~(COMREG2_DOUTEN0 | COMREG2_DOUTEN1);
668 if (s->io_bits & 0x0f)
669 devpriv->com_reg_2_state |= COMREG2_DOUTEN0;
670 if (s->io_bits & 0xf0)
671 devpriv->com_reg_2_state |= COMREG2_DOUTEN1;
672 outw(devpriv->com_reg_2_state, dev->iobase + COM_REG_2);
678 options[0] - I/O port
681 N == irq N {3,4,5,6,7,9,10,11,12,14,15}
684 N == irq N {3,4,5,6,7,9}
685 options[3] - DMA1 channel
688 options[4] - DMA2 channel
693 0=differential, 1=single
694 options[6] - a/d range
695 0=bipolar10, 1=bipolar5, 2=unipolar10
697 options[7] - dac0 range
698 0=bipolar, 1=unipolar
699 options[8] - dac0 reference
700 0=internal, 1=external
701 options[9] - dac0 coding
702 0=2's comp, 1=straight binary
704 options[10] - dac1 range
705 options[11] - dac1 reference
706 options[12] - dac1 coding
709 static int atmio16d_attach(struct comedi_device *dev, struct comedi_devconfig *it)
712 unsigned long iobase;
715 struct comedi_subdevice *s;
717 /* make sure the address range is free and allocate it */
718 iobase = it->options[0];
719 printk("comedi%d: atmio16d: 0x%04lx ", dev->minor, iobase);
720 if (!request_region(iobase, ATMIO16D_SIZE, "ni_atmio16d")) {
721 printk("I/O port conflict\n");
724 dev->iobase = iobase;
727 dev->board_name = boardtype->name;
729 if ((ret = alloc_subdevices(dev, 4)) < 0)
731 if ((ret = alloc_private(dev, sizeof(struct atmio16d_private))) < 0)
734 /* reset the atmio16d hardware */
737 /* check if our interrupt is available and get it */
738 irq = it->options[1];
740 if ((ret = comedi_request_irq(irq, atmio16d_interrupt,
741 0, "atmio16d", dev)) < 0) {
742 printk("failed to allocate irq %u\n", irq);
746 printk("( irq = %u )\n", irq);
748 printk("( no irq )");
751 /* set device options */
752 devpriv->adc_mux = it->options[5];
753 devpriv->adc_range = it->options[6];
755 devpriv->dac0_range = it->options[7];
756 devpriv->dac0_reference = it->options[8];
757 devpriv->dac0_coding = it->options[9];
758 devpriv->dac1_range = it->options[10];
759 devpriv->dac1_reference = it->options[11];
760 devpriv->dac1_coding = it->options[12];
762 /* setup sub-devices */
763 s = dev->subdevices + 0;
764 dev->read_subdev = s;
766 s->type = COMEDI_SUBD_AI;
767 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
768 s->n_chan = (devpriv->adc_mux ? 16 : 8);
769 s->len_chanlist = 16;
770 s->insn_read = atmio16d_ai_insn_read;
771 s->do_cmdtest = atmio16d_ai_cmdtest;
772 s->do_cmd = atmio16d_ai_cmd;
773 s->cancel = atmio16d_ai_cancel;
774 s->maxdata = 0xfff; /* 4095 decimal */
775 switch (devpriv->adc_range) {
777 s->range_table = &range_atmio16d_ai_10_bipolar;
780 s->range_table = &range_atmio16d_ai_5_bipolar;
783 s->range_table = &range_atmio16d_ai_unipolar;
789 s->type = COMEDI_SUBD_AO;
790 s->subdev_flags = SDF_WRITABLE;
792 s->insn_read = atmio16d_ao_insn_read;
793 s->insn_write = atmio16d_ao_insn_write;
794 s->maxdata = 0xfff; /* 4095 decimal */
795 s->range_table_list = devpriv->ao_range_type_list;
796 switch (devpriv->dac0_range) {
798 devpriv->ao_range_type_list[0] = &range_bipolar10;
801 devpriv->ao_range_type_list[0] = &range_unipolar10;
804 switch (devpriv->dac1_range) {
806 devpriv->ao_range_type_list[1] = &range_bipolar10;
809 devpriv->ao_range_type_list[1] = &range_unipolar10;
815 s->type = COMEDI_SUBD_DIO;
816 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
818 s->insn_bits = atmio16d_dio_insn_bits;
819 s->insn_config = atmio16d_dio_insn_config;
821 s->range_table = &range_digital;
825 if (boardtype->has_8255) {
826 subdev_8255_init(dev, s, NULL, dev->iobase);
828 s->type = COMEDI_SUBD_UNUSED;
831 /* don't yet know how to deal with counter/timers */
835 s->type = COMEDI_SUBD_TIMER;
844 static int atmio16d_detach(struct comedi_device *dev)
846 printk("comedi%d: atmio16d: remove\n", dev->minor);
848 if (dev->subdevices && boardtype->has_8255)
849 subdev_8255_cleanup(dev, dev->subdevices + 3);
852 comedi_free_irq(dev->irq, dev);
857 release_region(dev->iobase, ATMIO16D_SIZE);