5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 * Copyright (C) 2000 Chris R. Baugher <baugher@enteract.com>
8 * Copyright (C) 2001,2002 Frank Mori Hess <fmhess@users.sourceforge.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
23 * Description: DAS16 compatible boards
24 * Author: Sam Moore, Warren Jasper, ds, Chris Baugher, Frank Hess, Roman Fietze
25 * Devices: (Keithley Metrabyte) DAS-16 [das-16]
26 * (Keithley Metrabyte) DAS-16G [das-16g]
27 * (Keithley Metrabyte) DAS-16F [das-16f]
28 * (Keithley Metrabyte) DAS-1201 [das-1201]
29 * (Keithley Metrabyte) DAS-1202 [das-1202]
30 * (Keithley Metrabyte) DAS-1401 [das-1401]
31 * (Keithley Metrabyte) DAS-1402 [das-1402]
32 * (Keithley Metrabyte) DAS-1601 [das-1601]
33 * (Keithley Metrabyte) DAS-1602 [das-1602]
34 * (ComputerBoards) PC104-DAS16/JR [pc104-das16jr]
35 * (ComputerBoards) PC104-DAS16JR/16 [pc104-das16jr/16]
36 * (ComputerBoards) CIO-DAS16 [cio-das16]
37 * (ComputerBoards) CIO-DAS16F [cio-das16/f]
38 * (ComputerBoards) CIO-DAS16/JR [cio-das16/jr]
39 * (ComputerBoards) CIO-DAS16JR/16 [cio-das16jr/16]
40 * (ComputerBoards) CIO-DAS1401/12 [cio-das1401/12]
41 * (ComputerBoards) CIO-DAS1402/12 [cio-das1402/12]
42 * (ComputerBoards) CIO-DAS1402/16 [cio-das1402/16]
43 * (ComputerBoards) CIO-DAS1601/12 [cio-das1601/12]
44 * (ComputerBoards) CIO-DAS1602/12 [cio-das1602/12]
45 * (ComputerBoards) CIO-DAS1602/16 [cio-das1602/16]
46 * (ComputerBoards) CIO-DAS16/330 [cio-das16/330]
50 * A rewrite of the das16 and das1600 drivers.
53 * [0] - base io address
54 * [1] - irq (does nothing, irq is not used anymore)
55 * [2] - dma channel (optional, required for comedi_command support)
56 * [3] - master clock speed in MHz (optional, 1 or 10, ignored if
57 * board can probe clock, defaults to 1)
58 * [4] - analog input range lowest voltage in microvolts (optional,
59 * only useful if your board does not have software
61 * [5] - analog input range highest voltage in microvolts (optional,
62 * only useful if board does not have software programmable
64 * [6] - analog output range lowest voltage in microvolts (optional)
65 * [7] - analog output range highest voltage in microvolts (optional)
67 * Passing a zero for an option is the same as leaving it unspecified.
71 * Testing and debugging help provided by Daniel Koch.
75 * 4919.PDF (das1400, 1600)
77 * 4923.PDF (das1200, 1400, 1600)
79 * Computer boards manuals also available from their website
80 * www.measurementcomputing.com
83 #include <linux/module.h>
84 #include <linux/delay.h>
85 #include <linux/pci.h>
86 #include <linux/interrupt.h>
90 #include "../comedidev.h"
94 #include "comedi_fc.h"
96 #define DAS16_DMA_SIZE 0xff00 /* size in bytes of allocated dma buffer */
101 #define DAS16_TRIG_REG 0x00
102 #define DAS16_AI_LSB_REG 0x00
103 #define DAS16_AI_MSB_REG 0x01
104 #define DAS16_MUX_REG 0x02
105 #define DAS16_DIO_REG 0x03
106 #define DAS16_AO_LSB_REG(x) ((x) ? 0x06 : 0x04)
107 #define DAS16_AO_MSB_REG(x) ((x) ? 0x07 : 0x05)
108 #define DAS16_STATUS_REG 0x08
109 #define DAS16_STATUS_BUSY (1 << 7)
110 #define DAS16_STATUS_UNIPOLAR (1 << 6)
111 #define DAS16_STATUS_MUXBIT (1 << 5)
112 #define DAS16_STATUS_INT (1 << 4)
113 #define DAS16_CTRL_REG 0x09
114 #define DAS16_CTRL_INTE (1 << 7)
115 #define DAS16_CTRL_IRQ(x) (((x) & 0x7) << 4)
116 #define DAS16_CTRL_DMAE (1 << 2)
117 #define DAS16_CTRL_PACING_MASK (3 << 0)
118 #define DAS16_CTRL_INT_PACER (3 << 0)
119 #define DAS16_CTRL_EXT_PACER (2 << 0)
120 #define DAS16_CTRL_SOFT_PACER (0 << 0)
121 #define DAS16_PACER_REG 0x0a
122 #define DAS16_PACER_BURST_LEN(x) (((x) & 0xf) << 4)
123 #define DAS16_PACER_CTR0 (1 << 1)
124 #define DAS16_PACER_TRIG0 (1 << 0)
125 #define DAS16_GAIN_REG 0x0b
126 #define DAS16_TIMER_BASE_REG 0x0c /* to 0x0f */
128 #define DAS1600_CONV_REG 0x404
129 #define DAS1600_CONV_DISABLE (1 << 6)
130 #define DAS1600_BURST_REG 0x405
131 #define DAS1600_BURST_VAL (1 << 6)
132 #define DAS1600_ENABLE_REG 0x406
133 #define DAS1600_ENABLE_VAL (1 << 6)
134 #define DAS1600_STATUS_REG 0x407
135 #define DAS1600_STATUS_BME (1 << 6)
136 #define DAS1600_STATUS_ME (1 << 5)
137 #define DAS1600_STATUS_CD (1 << 4)
138 #define DAS1600_STATUS_WS (1 << 1)
139 #define DAS1600_STATUS_CLK_10MHZ (1 << 0)
141 static const struct comedi_lrange range_das1x01_bip = {
150 static const struct comedi_lrange range_das1x01_unip = {
159 static const struct comedi_lrange range_das1x02_bip = {
168 static const struct comedi_lrange range_das1x02_unip = {
177 static const struct comedi_lrange range_das16jr = {
191 static const struct comedi_lrange range_das16jr_16 = {
204 static const int das16jr_gainlist[] = { 8, 0, 1, 2, 3, 4, 5, 6, 7 };
205 static const int das16jr_16_gainlist[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
206 static const int das1600_gainlist[] = { 0, 1, 2, 3 };
215 static const int *const das16_gainlists[] = {
223 static const struct comedi_lrange *const das16_ai_uni_lranges[] = {
231 static const struct comedi_lrange *const das16_ai_bip_lranges[] = {
241 unsigned int ai_maxdata;
242 unsigned int ai_speed; /* max conversion speed in nanosec */
244 unsigned int has_ao:1;
245 unsigned int has_8255:1;
247 unsigned int i8255_offset;
253 static const struct das16_board das16_boards[] = {
256 .ai_maxdata = 0x0fff,
258 .ai_pg = das16_pg_none,
261 .i8255_offset = 0x10,
266 .ai_maxdata = 0x0fff,
268 .ai_pg = das16_pg_none,
271 .i8255_offset = 0x10,
276 .ai_maxdata = 0x0fff,
278 .ai_pg = das16_pg_none,
281 .i8255_offset = 0x10,
286 .ai_maxdata = 0x0fff,
288 .ai_pg = das16_pg_none,
291 .i8255_offset = 0x10,
295 .name = "cio-das16/f",
296 .ai_maxdata = 0x0fff,
298 .ai_pg = das16_pg_none,
301 .i8255_offset = 0x10,
305 .name = "cio-das16/jr",
306 .ai_maxdata = 0x0fff,
308 .ai_pg = das16_pg_16jr,
312 .name = "pc104-das16jr",
313 .ai_maxdata = 0x0fff,
315 .ai_pg = das16_pg_16jr,
319 .name = "cio-das16jr/16",
320 .ai_maxdata = 0xffff,
322 .ai_pg = das16_pg_16jr_16,
326 .name = "pc104-das16jr/16",
327 .ai_maxdata = 0xffff,
329 .ai_pg = das16_pg_16jr_16,
334 .ai_maxdata = 0x0fff,
336 .ai_pg = das16_pg_none,
338 .i8255_offset = 0x400,
343 .ai_maxdata = 0x0fff,
345 .ai_pg = das16_pg_none,
347 .i8255_offset = 0x400,
352 .ai_maxdata = 0x0fff,
354 .ai_pg = das16_pg_1601,
359 .ai_maxdata = 0x0fff,
361 .ai_pg = das16_pg_1602,
366 .ai_maxdata = 0x0fff,
368 .ai_pg = das16_pg_1601,
371 .i8255_offset = 0x400,
376 .ai_maxdata = 0x0fff,
378 .ai_pg = das16_pg_1602,
381 .i8255_offset = 0x400,
385 .name = "cio-das1401/12",
386 .ai_maxdata = 0x0fff,
388 .ai_pg = das16_pg_1601,
392 .name = "cio-das1402/12",
393 .ai_maxdata = 0x0fff,
395 .ai_pg = das16_pg_1602,
399 .name = "cio-das1402/16",
400 .ai_maxdata = 0xffff,
402 .ai_pg = das16_pg_1602,
406 .name = "cio-das1601/12",
407 .ai_maxdata = 0x0fff,
409 .ai_pg = das16_pg_1601,
412 .i8255_offset = 0x400,
416 .name = "cio-das1602/12",
417 .ai_maxdata = 0x0fff,
419 .ai_pg = das16_pg_1602,
422 .i8255_offset = 0x400,
426 .name = "cio-das1602/16",
427 .ai_maxdata = 0xffff,
429 .ai_pg = das16_pg_1602,
432 .i8255_offset = 0x400,
436 .name = "cio-das16/330",
437 .ai_maxdata = 0x0fff,
439 .ai_pg = das16_pg_16jr,
445 /* Period for timer interrupt in jiffies. It's a function
446 * to deal with possibility of dynamic HZ patches */
447 static inline int timer_period(void)
452 struct das16_private_struct {
453 unsigned int clockbase;
454 unsigned int ctrl_reg;
455 unsigned long adc_byte_count;
456 unsigned int divisor1;
457 unsigned int divisor2;
458 unsigned int dma_chan;
459 uint16_t *dma_buffer[2];
460 dma_addr_t dma_buffer_addr[2];
461 unsigned int current_buffer;
462 unsigned int dma_transfer_size;
463 struct comedi_lrange *user_ai_range_table;
464 struct comedi_lrange *user_ao_range_table;
465 struct timer_list timer;
467 unsigned long extra_iobase;
468 unsigned int can_burst:1;
471 static void das16_ai_enable(struct comedi_device *dev,
472 unsigned int mode, unsigned int src)
474 struct das16_private_struct *devpriv = dev->private;
476 devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE |
478 DAS16_CTRL_PACING_MASK);
479 devpriv->ctrl_reg |= mode;
482 devpriv->ctrl_reg |= DAS16_CTRL_EXT_PACER;
484 devpriv->ctrl_reg |= DAS16_CTRL_INT_PACER;
485 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
488 static void das16_ai_disable(struct comedi_device *dev)
490 struct das16_private_struct *devpriv = dev->private;
492 /* disable interrupts, dma and pacer clocked conversions */
493 devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE |
495 DAS16_CTRL_PACING_MASK);
496 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
499 /* the pc104-das16jr (at least) has problems if the dma
500 transfer is interrupted in the middle of transferring
501 a 16 bit sample, so this function takes care to get
502 an even transfer count after disabling dma
505 static int disable_dma_on_even(struct comedi_device *dev)
507 struct das16_private_struct *devpriv = dev->private;
510 static const int disable_limit = 100;
511 static const int enable_timeout = 100;
513 disable_dma(devpriv->dma_chan);
514 residue = get_dma_residue(devpriv->dma_chan);
515 for (i = 0; i < disable_limit && (residue % 2); ++i) {
517 enable_dma(devpriv->dma_chan);
518 for (j = 0; j < enable_timeout; ++j) {
521 new_residue = get_dma_residue(devpriv->dma_chan);
522 if (new_residue != residue)
525 disable_dma(devpriv->dma_chan);
526 residue = get_dma_residue(devpriv->dma_chan);
528 if (i == disable_limit) {
529 dev_err(dev->class_dev,
530 "failed to get an even dma transfer, could be trouble\n");
535 static void das16_interrupt(struct comedi_device *dev)
537 struct das16_private_struct *devpriv = dev->private;
538 struct comedi_subdevice *s = dev->read_subdev;
539 struct comedi_async *async = s->async;
540 struct comedi_cmd *cmd = &async->cmd;
541 unsigned long spin_flags;
542 unsigned long dma_flags;
543 int num_bytes, residue;
546 spin_lock_irqsave(&dev->spinlock, spin_flags);
547 if (!(devpriv->ctrl_reg & DAS16_CTRL_DMAE)) {
548 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
552 dma_flags = claim_dma_lock();
553 clear_dma_ff(devpriv->dma_chan);
554 residue = disable_dma_on_even(dev);
556 /* figure out how many points to read */
557 if (residue > devpriv->dma_transfer_size) {
558 dev_err(dev->class_dev, "residue > transfer size!\n");
559 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
562 num_bytes = devpriv->dma_transfer_size - residue;
564 if (cmd->stop_src == TRIG_COUNT &&
565 num_bytes >= devpriv->adc_byte_count) {
566 num_bytes = devpriv->adc_byte_count;
567 async->events |= COMEDI_CB_EOA;
570 buffer_index = devpriv->current_buffer;
571 devpriv->current_buffer = (devpriv->current_buffer + 1) % 2;
572 devpriv->adc_byte_count -= num_bytes;
575 if ((async->events & COMEDI_CB_EOA) == 0) {
576 set_dma_addr(devpriv->dma_chan,
577 devpriv->dma_buffer_addr[devpriv->current_buffer]);
578 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
579 enable_dma(devpriv->dma_chan);
581 release_dma_lock(dma_flags);
583 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
585 cfc_write_array_to_buffer(s,
586 devpriv->dma_buffer[buffer_index], num_bytes);
588 cfc_handle_events(dev, s);
591 static void das16_timer_interrupt(unsigned long arg)
593 struct comedi_device *dev = (struct comedi_device *)arg;
594 struct das16_private_struct *devpriv = dev->private;
596 das16_interrupt(dev);
598 if (devpriv->timer_running)
599 mod_timer(&devpriv->timer, jiffies + timer_period());
602 static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
603 struct comedi_cmd *cmd)
605 const struct das16_board *board = comedi_board(dev);
606 struct das16_private_struct *devpriv = dev->private;
608 int gain, start_chan, i;
611 /* Step 1 : check if triggers are trivially valid */
613 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
616 if (devpriv->can_burst)
617 mask |= TRIG_TIMER | TRIG_EXT;
618 err |= cfc_check_trigger_src(&cmd->scan_begin_src, mask);
620 tmp = cmd->convert_src;
621 mask = TRIG_TIMER | TRIG_EXT;
622 if (devpriv->can_burst)
624 err |= cfc_check_trigger_src(&cmd->convert_src, mask);
626 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
627 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
632 /* Step 2a : make sure trigger sources are unique */
634 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
635 err |= cfc_check_trigger_is_unique(cmd->convert_src);
636 err |= cfc_check_trigger_is_unique(cmd->stop_src);
638 /* Step 2b : and mutually compatible */
640 /* make sure scan_begin_src and convert_src dont conflict */
641 if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
643 if (cmd->scan_begin_src != TRIG_FOLLOW && cmd->convert_src != TRIG_NOW)
649 /* Step 3: check if arguments are trivially valid */
651 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
653 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
654 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
656 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
658 /* check against maximum frequency */
659 if (cmd->scan_begin_src == TRIG_TIMER)
660 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
661 board->ai_speed * cmd->chanlist_len);
663 if (cmd->convert_src == TRIG_TIMER)
664 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
667 if (cmd->stop_src == TRIG_NONE)
668 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
673 /* step 4: fix up arguments */
674 if (cmd->scan_begin_src == TRIG_TIMER) {
675 unsigned int tmp = cmd->scan_begin_arg;
676 /* set divisors, correct timing arguments */
677 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
680 &cmd->scan_begin_arg,
681 cmd->flags & TRIG_ROUND_MASK);
682 err += (tmp != cmd->scan_begin_arg);
684 if (cmd->convert_src == TRIG_TIMER) {
685 unsigned int tmp = cmd->convert_arg;
686 /* set divisors, correct timing arguments */
687 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
691 cmd->flags & TRIG_ROUND_MASK);
692 err += (tmp != cmd->convert_arg);
697 /* check channel/gain list against card's limitations */
699 gain = CR_RANGE(cmd->chanlist[0]);
700 start_chan = CR_CHAN(cmd->chanlist[0]);
701 for (i = 1; i < cmd->chanlist_len; i++) {
702 if (CR_CHAN(cmd->chanlist[i]) !=
703 (start_chan + i) % s->n_chan) {
704 dev_err(dev->class_dev,
705 "entries in chanlist must be consecutive channels, counting upwards\n");
708 if (CR_RANGE(cmd->chanlist[i]) != gain) {
709 dev_err(dev->class_dev,
710 "entries in chanlist must all have the same gain\n");
721 static unsigned int das16_set_pacer(struct comedi_device *dev, unsigned int ns,
724 struct das16_private_struct *devpriv = dev->private;
725 unsigned long timer_base = dev->iobase + DAS16_TIMER_BASE_REG;
727 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
731 rounding_flags & TRIG_ROUND_MASK);
733 /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
734 i8254_load(timer_base, 0, 1, devpriv->divisor1, 2);
735 i8254_load(timer_base, 0, 2, devpriv->divisor2, 2);
740 static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s)
742 const struct das16_board *board = comedi_board(dev);
743 struct das16_private_struct *devpriv = dev->private;
744 struct comedi_async *async = s->async;
745 struct comedi_cmd *cmd = &async->cmd;
750 if (cmd->flags & TRIG_RT) {
751 dev_err(dev->class_dev,
752 "isa dma transfers cannot be performed with TRIG_RT, aborting\n");
756 devpriv->adc_byte_count =
757 cmd->stop_arg * cmd->chanlist_len * sizeof(uint16_t);
759 if (devpriv->can_burst)
760 outb(DAS1600_CONV_DISABLE, dev->iobase + DAS1600_CONV_REG);
762 /* set scan limits */
763 byte = CR_CHAN(cmd->chanlist[0]);
764 byte |= CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]) << 4;
765 outb(byte, dev->iobase + DAS16_MUX_REG);
767 /* set gain (this is also burst rate register but according to
768 * computer boards manual, burst rate does nothing, even on
770 if (board->ai_pg != das16_pg_none) {
771 range = CR_RANGE(cmd->chanlist[0]);
772 outb((das16_gainlists[board->ai_pg])[range],
773 dev->iobase + DAS16_GAIN_REG);
776 /* set counter mode and counts */
778 das16_set_pacer(dev, cmd->convert_arg,
779 cmd->flags & TRIG_ROUND_MASK);
781 /* enable counters */
783 if (devpriv->can_burst) {
784 if (cmd->convert_src == TRIG_NOW) {
785 outb(DAS1600_BURST_VAL,
786 dev->iobase + DAS1600_BURST_REG);
787 /* set burst length */
788 byte |= DAS16_PACER_BURST_LEN(cmd->chanlist_len - 1);
790 outb(0, dev->iobase + DAS1600_BURST_REG);
793 outb(byte, dev->iobase + DAS16_PACER_REG);
795 /* set up dma transfer */
796 flags = claim_dma_lock();
797 disable_dma(devpriv->dma_chan);
798 /* clear flip-flop to make sure 2-byte registers for
799 * count and address get set correctly */
800 clear_dma_ff(devpriv->dma_chan);
801 devpriv->current_buffer = 0;
802 set_dma_addr(devpriv->dma_chan,
803 devpriv->dma_buffer_addr[devpriv->current_buffer]);
804 devpriv->dma_transfer_size = DAS16_DMA_SIZE;
805 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
806 enable_dma(devpriv->dma_chan);
807 release_dma_lock(flags);
809 /* set up interrupt */
810 devpriv->timer_running = 1;
811 devpriv->timer.expires = jiffies + timer_period();
812 add_timer(&devpriv->timer);
814 das16_ai_enable(dev, DAS16_CTRL_DMAE, cmd->convert_src);
816 if (devpriv->can_burst)
817 outb(0, dev->iobase + DAS1600_CONV_REG);
822 static int das16_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
824 struct das16_private_struct *devpriv = dev->private;
827 spin_lock_irqsave(&dev->spinlock, flags);
829 das16_ai_disable(dev);
830 disable_dma(devpriv->dma_chan);
832 /* disable SW timer */
833 if (devpriv->timer_running) {
834 devpriv->timer_running = 0;
835 del_timer(&devpriv->timer);
838 if (devpriv->can_burst)
839 outb(0, dev->iobase + DAS1600_BURST_REG);
841 spin_unlock_irqrestore(&dev->spinlock, flags);
846 static void das16_ai_munge(struct comedi_device *dev,
847 struct comedi_subdevice *s, void *array,
848 unsigned int num_bytes,
849 unsigned int start_chan_index)
851 unsigned int i, num_samples = num_bytes / sizeof(short);
854 for (i = 0; i < num_samples; i++) {
855 data[i] = le16_to_cpu(data[i]);
856 if (s->maxdata == 0x0fff)
858 data[i] &= s->maxdata;
862 static int das16_ai_wait_for_conv(struct comedi_device *dev,
863 unsigned int timeout)
868 for (i = 0; i < timeout; i++) {
869 status = inb(dev->iobase + DAS16_STATUS_REG);
870 if (!(status & DAS16_STATUS_BUSY))
876 static int das16_ai_insn_read(struct comedi_device *dev,
877 struct comedi_subdevice *s,
878 struct comedi_insn *insn,
881 const struct das16_board *board = comedi_board(dev);
882 unsigned int chan = CR_CHAN(insn->chanspec);
883 unsigned int range = CR_RANGE(insn->chanspec);
888 das16_ai_disable(dev);
890 /* set multiplexer */
891 outb(chan | (chan << 4), dev->iobase + DAS16_MUX_REG);
894 if (board->ai_pg != das16_pg_none) {
895 outb((das16_gainlists[board->ai_pg])[range],
896 dev->iobase + DAS16_GAIN_REG);
899 for (i = 0; i < insn->n; i++) {
900 /* trigger conversion */
901 outb_p(0, dev->iobase + DAS16_TRIG_REG);
903 ret = das16_ai_wait_for_conv(dev, 1000);
907 val = inb(dev->iobase + DAS16_AI_MSB_REG) << 8;
908 val |= inb(dev->iobase + DAS16_AI_LSB_REG);
909 if (s->maxdata == 0x0fff)
919 static int das16_ao_insn_write(struct comedi_device *dev,
920 struct comedi_subdevice *s,
921 struct comedi_insn *insn,
924 unsigned int chan = CR_CHAN(insn->chanspec);
928 for (i = 0; i < insn->n; i++) {
932 outb(val & 0xff, dev->iobase + DAS16_AO_LSB_REG(chan));
933 outb((val >> 8) & 0xff, dev->iobase + DAS16_AO_MSB_REG(chan));
939 static int das16_di_insn_bits(struct comedi_device *dev,
940 struct comedi_subdevice *s,
941 struct comedi_insn *insn,
944 data[1] = inb(dev->iobase + DAS16_DIO_REG) & 0xf;
949 static int das16_do_insn_bits(struct comedi_device *dev,
950 struct comedi_subdevice *s,
951 struct comedi_insn *insn,
954 unsigned int mask = data[0];
955 unsigned int bits = data[1];
959 s->state |= (bits & mask);
961 outb(s->state, dev->iobase + DAS16_DIO_REG);
969 static int das16_probe(struct comedi_device *dev, struct comedi_devconfig *it)
971 const struct das16_board *board = comedi_board(dev);
974 /* diobits indicates boards */
975 diobits = inb(dev->iobase + DAS16_DIO_REG) & 0xf0;
976 if (board->id != diobits) {
977 dev_err(dev->class_dev,
978 "requested board's id bits are incorrect (0x%x != 0x%x)\n",
986 static void das16_reset(struct comedi_device *dev)
988 outb(0, dev->iobase + DAS16_STATUS_REG);
989 outb(0, dev->iobase + DAS16_CTRL_REG);
990 outb(0, dev->iobase + DAS16_PACER_REG);
991 outb(0, dev->iobase + DAS16_TIMER_BASE_REG + i8254_control_reg);
994 static int das16_attach(struct comedi_device *dev, struct comedi_devconfig *it)
996 const struct das16_board *board = comedi_board(dev);
997 struct das16_private_struct *devpriv;
998 struct comedi_subdevice *s;
999 struct comedi_lrange *lrange;
1000 struct comedi_krange *krange;
1001 unsigned int dma_chan = it->options[2];
1002 unsigned int status;
1005 /* check that clock setting is valid */
1006 if (it->options[3]) {
1007 if (it->options[3] != 0 &&
1008 it->options[3] != 1 && it->options[3] != 10) {
1009 dev_err(dev->class_dev,
1010 "Invalid option. Master clock must be set to 1 or 10 (MHz)\n");
1015 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1019 if (board->size < 0x400) {
1020 ret = comedi_request_region(dev, it->options[0], board->size);
1024 ret = comedi_request_region(dev, it->options[0], 0x10);
1027 /* Request an additional region for the 8255 */
1028 ret = __comedi_request_region(dev, dev->iobase + 0x400,
1029 board->size & 0x3ff);
1032 devpriv->extra_iobase = dev->iobase + 0x400;
1033 devpriv->can_burst = 1;
1036 /* probe id bits to make sure they are consistent */
1037 if (das16_probe(dev, it))
1040 /* get master clock speed */
1041 if (devpriv->can_burst) {
1042 status = inb(dev->iobase + DAS1600_STATUS_REG);
1044 if (status & DAS1600_STATUS_CLK_10MHZ)
1045 devpriv->clockbase = 100;
1047 devpriv->clockbase = 1000;
1050 devpriv->clockbase = 1000 / it->options[3];
1052 devpriv->clockbase = 1000; /* 1 MHz default */
1055 /* initialize dma */
1056 if (dma_chan == 1 || dma_chan == 3) {
1057 unsigned long flags;
1060 if (request_dma(dma_chan, dev->board_name)) {
1061 dev_err(dev->class_dev,
1062 "failed to request dma channel %i\n",
1066 devpriv->dma_chan = dma_chan;
1068 /* allocate dma buffers */
1069 for (i = 0; i < 2; i++) {
1072 p = pci_alloc_consistent(NULL, DAS16_DMA_SIZE,
1073 &devpriv->dma_buffer_addr[i]);
1076 devpriv->dma_buffer[i] = p;
1079 flags = claim_dma_lock();
1080 disable_dma(devpriv->dma_chan);
1081 set_dma_mode(devpriv->dma_chan, DMA_MODE_READ);
1082 release_dma_lock(flags);
1084 init_timer(&devpriv->timer);
1085 devpriv->timer.function = das16_timer_interrupt;
1086 devpriv->timer.data = (unsigned long)dev;
1089 /* get any user-defined input range */
1090 if (board->ai_pg == das16_pg_none &&
1091 (it->options[4] || it->options[5])) {
1092 /* allocate single-range range table */
1093 lrange = kzalloc(sizeof(*lrange) + sizeof(*krange), GFP_KERNEL);
1097 /* initialize ai range */
1098 devpriv->user_ai_range_table = lrange;
1100 krange = devpriv->user_ai_range_table->range;
1101 krange->min = it->options[4];
1102 krange->max = it->options[5];
1103 krange->flags = UNIT_volt;
1106 /* get any user-defined output range */
1107 if (it->options[6] || it->options[7]) {
1108 /* allocate single-range range table */
1109 lrange = kzalloc(sizeof(*lrange) + sizeof(*krange), GFP_KERNEL);
1113 /* initialize ao range */
1114 devpriv->user_ao_range_table = lrange;
1116 krange = devpriv->user_ao_range_table->range;
1117 krange->min = it->options[6];
1118 krange->max = it->options[7];
1119 krange->flags = UNIT_volt;
1122 ret = comedi_alloc_subdevices(dev, 4 + board->has_8255);
1126 status = inb(dev->iobase + DAS16_STATUS_REG);
1128 /* Analog Input subdevice */
1129 s = &dev->subdevices[0];
1130 s->type = COMEDI_SUBD_AI;
1131 s->subdev_flags = SDF_READABLE;
1132 if (status & DAS16_STATUS_MUXBIT) {
1133 s->subdev_flags |= SDF_GROUND;
1136 s->subdev_flags |= SDF_DIFF;
1139 s->len_chanlist = s->n_chan;
1140 s->maxdata = board->ai_maxdata;
1141 if (devpriv->user_ai_range_table) { /* user defined ai range */
1142 s->range_table = devpriv->user_ai_range_table;
1143 } else if (status & DAS16_STATUS_UNIPOLAR) {
1144 s->range_table = das16_ai_uni_lranges[board->ai_pg];
1146 s->range_table = das16_ai_bip_lranges[board->ai_pg];
1148 s->insn_read = das16_ai_insn_read;
1149 if (devpriv->dma_chan) {
1150 dev->read_subdev = s;
1151 s->subdev_flags |= SDF_CMD_READ;
1152 s->do_cmdtest = das16_cmd_test;
1153 s->do_cmd = das16_cmd_exec;
1154 s->cancel = das16_cancel;
1155 s->munge = das16_ai_munge;
1158 /* Analog Output subdevice */
1159 s = &dev->subdevices[1];
1160 if (board->has_ao) {
1161 s->type = COMEDI_SUBD_AO;
1162 s->subdev_flags = SDF_WRITABLE;
1164 s->maxdata = 0x0fff;
1165 s->range_table = devpriv->user_ao_range_table;
1166 s->insn_write = das16_ao_insn_write;
1168 s->type = COMEDI_SUBD_UNUSED;
1171 /* Digital Input subdevice */
1172 s = &dev->subdevices[2];
1173 s->type = COMEDI_SUBD_DI;
1174 s->subdev_flags = SDF_READABLE;
1177 s->range_table = &range_digital;
1178 s->insn_bits = das16_di_insn_bits;
1180 /* Digital Output subdevice */
1181 s = &dev->subdevices[3];
1182 s->type = COMEDI_SUBD_DO;
1183 s->subdev_flags = SDF_WRITABLE;
1186 s->range_table = &range_digital;
1187 s->insn_bits = das16_do_insn_bits;
1189 /* initialize digital output lines */
1190 outb(s->state, dev->iobase + DAS16_DIO_REG);
1192 /* 8255 Digital I/O subdevice */
1193 if (board->has_8255) {
1194 s = &dev->subdevices[4];
1195 ret = subdev_8255_init(dev, s, NULL,
1196 dev->iobase + board->i8255_offset);
1202 /* set the interrupt level */
1203 devpriv->ctrl_reg = DAS16_CTRL_IRQ(dev->irq);
1204 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
1206 if (devpriv->can_burst) {
1207 outb(DAS1600_ENABLE_VAL, dev->iobase + DAS1600_ENABLE_REG);
1208 outb(0, dev->iobase + DAS1600_CONV_REG);
1209 outb(0, dev->iobase + DAS1600_BURST_REG);
1215 static void das16_detach(struct comedi_device *dev)
1217 const struct das16_board *board = comedi_board(dev);
1218 struct das16_private_struct *devpriv = dev->private;
1225 for (i = 0; i < 2; i++) {
1226 if (devpriv->dma_buffer[i])
1227 pci_free_consistent(NULL, DAS16_DMA_SIZE,
1228 devpriv->dma_buffer[i],
1230 dma_buffer_addr[i]);
1232 if (devpriv->dma_chan)
1233 free_dma(devpriv->dma_chan);
1234 kfree(devpriv->user_ai_range_table);
1235 kfree(devpriv->user_ao_range_table);
1237 if (devpriv->extra_iobase)
1238 release_region(devpriv->extra_iobase,
1239 board->size & 0x3ff);
1242 comedi_legacy_detach(dev);
1245 static struct comedi_driver das16_driver = {
1246 .driver_name = "das16",
1247 .module = THIS_MODULE,
1248 .attach = das16_attach,
1249 .detach = das16_detach,
1250 .board_name = &das16_boards[0].name,
1251 .num_names = ARRAY_SIZE(das16_boards),
1252 .offset = sizeof(das16_boards[0]),
1254 module_comedi_driver(das16_driver);
1256 MODULE_AUTHOR("Comedi http://www.comedi.org");
1257 MODULE_DESCRIPTION("Comedi driver for DAS16 compatible boards");
1258 MODULE_LICENSE("GPL");