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/slab.h>
85 #include <linux/delay.h>
86 #include <linux/pci.h>
87 #include <linux/interrupt.h>
91 #include "../comedidev.h"
95 #include "comedi_fc.h"
97 #define DAS16_DMA_SIZE 0xff00 /* size in bytes of allocated dma buffer */
102 #define DAS16_TRIG_REG 0x00
103 #define DAS16_AI_LSB_REG 0x00
104 #define DAS16_AI_MSB_REG 0x01
105 #define DAS16_MUX_REG 0x02
106 #define DAS16_DIO_REG 0x03
107 #define DAS16_AO_LSB_REG(x) ((x) ? 0x06 : 0x04)
108 #define DAS16_AO_MSB_REG(x) ((x) ? 0x07 : 0x05)
109 #define DAS16_STATUS_REG 0x08
110 #define DAS16_STATUS_BUSY (1 << 7)
111 #define DAS16_STATUS_UNIPOLAR (1 << 6)
112 #define DAS16_STATUS_MUXBIT (1 << 5)
113 #define DAS16_STATUS_INT (1 << 4)
114 #define DAS16_CTRL_REG 0x09
115 #define DAS16_CTRL_INTE (1 << 7)
116 #define DAS16_CTRL_IRQ(x) (((x) & 0x7) << 4)
117 #define DAS16_CTRL_DMAE (1 << 2)
118 #define DAS16_CTRL_PACING_MASK (3 << 0)
119 #define DAS16_CTRL_INT_PACER (3 << 0)
120 #define DAS16_CTRL_EXT_PACER (2 << 0)
121 #define DAS16_CTRL_SOFT_PACER (0 << 0)
122 #define DAS16_PACER_REG 0x0a
123 #define DAS16_PACER_BURST_LEN(x) (((x) & 0xf) << 4)
124 #define DAS16_PACER_CTR0 (1 << 1)
125 #define DAS16_PACER_TRIG0 (1 << 0)
126 #define DAS16_GAIN_REG 0x0b
127 #define DAS16_TIMER_BASE_REG 0x0c /* to 0x0f */
129 #define DAS1600_CONV_REG 0x404
130 #define DAS1600_CONV_DISABLE (1 << 6)
131 #define DAS1600_BURST_REG 0x405
132 #define DAS1600_BURST_VAL (1 << 6)
133 #define DAS1600_ENABLE_REG 0x406
134 #define DAS1600_ENABLE_VAL (1 << 6)
135 #define DAS1600_STATUS_REG 0x407
136 #define DAS1600_STATUS_BME (1 << 6)
137 #define DAS1600_STATUS_ME (1 << 5)
138 #define DAS1600_STATUS_CD (1 << 4)
139 #define DAS1600_STATUS_WS (1 << 1)
140 #define DAS1600_STATUS_CLK_10MHZ (1 << 0)
142 static const struct comedi_lrange range_das1x01_bip = {
151 static const struct comedi_lrange range_das1x01_unip = {
160 static const struct comedi_lrange range_das1x02_bip = {
169 static const struct comedi_lrange range_das1x02_unip = {
178 static const struct comedi_lrange range_das16jr = {
192 static const struct comedi_lrange range_das16jr_16 = {
205 static const int das16jr_gainlist[] = { 8, 0, 1, 2, 3, 4, 5, 6, 7 };
206 static const int das16jr_16_gainlist[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
207 static const int das1600_gainlist[] = { 0, 1, 2, 3 };
216 static const int *const das16_gainlists[] = {
224 static const struct comedi_lrange *const das16_ai_uni_lranges[] = {
232 static const struct comedi_lrange *const das16_ai_bip_lranges[] = {
242 unsigned int ai_maxdata;
243 unsigned int ai_speed; /* max conversion speed in nanosec */
245 unsigned int has_ao:1;
246 unsigned int has_8255:1;
248 unsigned int i8255_offset;
254 static const struct das16_board das16_boards[] = {
257 .ai_maxdata = 0x0fff,
259 .ai_pg = das16_pg_none,
262 .i8255_offset = 0x10,
267 .ai_maxdata = 0x0fff,
269 .ai_pg = das16_pg_none,
272 .i8255_offset = 0x10,
277 .ai_maxdata = 0x0fff,
279 .ai_pg = das16_pg_none,
282 .i8255_offset = 0x10,
287 .ai_maxdata = 0x0fff,
289 .ai_pg = das16_pg_none,
292 .i8255_offset = 0x10,
296 .name = "cio-das16/f",
297 .ai_maxdata = 0x0fff,
299 .ai_pg = das16_pg_none,
302 .i8255_offset = 0x10,
306 .name = "cio-das16/jr",
307 .ai_maxdata = 0x0fff,
309 .ai_pg = das16_pg_16jr,
313 .name = "pc104-das16jr",
314 .ai_maxdata = 0x0fff,
316 .ai_pg = das16_pg_16jr,
320 .name = "cio-das16jr/16",
321 .ai_maxdata = 0xffff,
323 .ai_pg = das16_pg_16jr_16,
327 .name = "pc104-das16jr/16",
328 .ai_maxdata = 0xffff,
330 .ai_pg = das16_pg_16jr_16,
335 .ai_maxdata = 0x0fff,
337 .ai_pg = das16_pg_none,
339 .i8255_offset = 0x400,
344 .ai_maxdata = 0x0fff,
346 .ai_pg = das16_pg_none,
348 .i8255_offset = 0x400,
353 .ai_maxdata = 0x0fff,
355 .ai_pg = das16_pg_1601,
360 .ai_maxdata = 0x0fff,
362 .ai_pg = das16_pg_1602,
367 .ai_maxdata = 0x0fff,
369 .ai_pg = das16_pg_1601,
372 .i8255_offset = 0x400,
377 .ai_maxdata = 0x0fff,
379 .ai_pg = das16_pg_1602,
382 .i8255_offset = 0x400,
386 .name = "cio-das1401/12",
387 .ai_maxdata = 0x0fff,
389 .ai_pg = das16_pg_1601,
393 .name = "cio-das1402/12",
394 .ai_maxdata = 0x0fff,
396 .ai_pg = das16_pg_1602,
400 .name = "cio-das1402/16",
401 .ai_maxdata = 0xffff,
403 .ai_pg = das16_pg_1602,
407 .name = "cio-das1601/12",
408 .ai_maxdata = 0x0fff,
410 .ai_pg = das16_pg_1601,
413 .i8255_offset = 0x400,
417 .name = "cio-das1602/12",
418 .ai_maxdata = 0x0fff,
420 .ai_pg = das16_pg_1602,
423 .i8255_offset = 0x400,
427 .name = "cio-das1602/16",
428 .ai_maxdata = 0xffff,
430 .ai_pg = das16_pg_1602,
433 .i8255_offset = 0x400,
437 .name = "cio-das16/330",
438 .ai_maxdata = 0x0fff,
440 .ai_pg = das16_pg_16jr,
446 /* Period for timer interrupt in jiffies. It's a function
447 * to deal with possibility of dynamic HZ patches */
448 static inline int timer_period(void)
453 struct das16_private_struct {
454 unsigned int clockbase;
455 unsigned int ctrl_reg;
456 unsigned long adc_byte_count;
457 unsigned int divisor1;
458 unsigned int divisor2;
459 unsigned int dma_chan;
460 uint16_t *dma_buffer[2];
461 dma_addr_t dma_buffer_addr[2];
462 unsigned int current_buffer;
463 unsigned int dma_transfer_size;
464 struct comedi_lrange *user_ai_range_table;
465 struct comedi_lrange *user_ao_range_table;
466 struct timer_list timer;
468 unsigned long extra_iobase;
469 unsigned int can_burst:1;
472 static void das16_ai_enable(struct comedi_device *dev,
473 unsigned int mode, unsigned int src)
475 struct das16_private_struct *devpriv = dev->private;
477 devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE |
479 DAS16_CTRL_PACING_MASK);
480 devpriv->ctrl_reg |= mode;
483 devpriv->ctrl_reg |= DAS16_CTRL_EXT_PACER;
485 devpriv->ctrl_reg |= DAS16_CTRL_INT_PACER;
486 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
489 static void das16_ai_disable(struct comedi_device *dev)
491 struct das16_private_struct *devpriv = dev->private;
493 /* disable interrupts, dma and pacer clocked conversions */
494 devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE |
496 DAS16_CTRL_PACING_MASK);
497 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
500 /* the pc104-das16jr (at least) has problems if the dma
501 transfer is interrupted in the middle of transferring
502 a 16 bit sample, so this function takes care to get
503 an even transfer count after disabling dma
506 static int disable_dma_on_even(struct comedi_device *dev)
508 struct das16_private_struct *devpriv = dev->private;
511 static const int disable_limit = 100;
512 static const int enable_timeout = 100;
514 disable_dma(devpriv->dma_chan);
515 residue = get_dma_residue(devpriv->dma_chan);
516 for (i = 0; i < disable_limit && (residue % 2); ++i) {
518 enable_dma(devpriv->dma_chan);
519 for (j = 0; j < enable_timeout; ++j) {
522 new_residue = get_dma_residue(devpriv->dma_chan);
523 if (new_residue != residue)
526 disable_dma(devpriv->dma_chan);
527 residue = get_dma_residue(devpriv->dma_chan);
529 if (i == disable_limit) {
530 dev_err(dev->class_dev,
531 "failed to get an even dma transfer, could be trouble\n");
536 static void das16_interrupt(struct comedi_device *dev)
538 struct das16_private_struct *devpriv = dev->private;
539 struct comedi_subdevice *s = dev->read_subdev;
540 struct comedi_async *async = s->async;
541 struct comedi_cmd *cmd = &async->cmd;
542 unsigned long spin_flags;
543 unsigned long dma_flags;
544 int num_bytes, residue;
547 spin_lock_irqsave(&dev->spinlock, spin_flags);
548 if (!(devpriv->ctrl_reg & DAS16_CTRL_DMAE)) {
549 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
553 dma_flags = claim_dma_lock();
554 clear_dma_ff(devpriv->dma_chan);
555 residue = disable_dma_on_even(dev);
557 /* figure out how many points to read */
558 if (residue > devpriv->dma_transfer_size) {
559 dev_err(dev->class_dev, "residue > transfer size!\n");
560 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
563 num_bytes = devpriv->dma_transfer_size - residue;
565 if (cmd->stop_src == TRIG_COUNT &&
566 num_bytes >= devpriv->adc_byte_count) {
567 num_bytes = devpriv->adc_byte_count;
568 async->events |= COMEDI_CB_EOA;
571 buffer_index = devpriv->current_buffer;
572 devpriv->current_buffer = (devpriv->current_buffer + 1) % 2;
573 devpriv->adc_byte_count -= num_bytes;
576 if ((async->events & COMEDI_CB_EOA) == 0) {
577 set_dma_addr(devpriv->dma_chan,
578 devpriv->dma_buffer_addr[devpriv->current_buffer]);
579 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
580 enable_dma(devpriv->dma_chan);
582 release_dma_lock(dma_flags);
584 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
586 cfc_write_array_to_buffer(s,
587 devpriv->dma_buffer[buffer_index], num_bytes);
589 cfc_handle_events(dev, s);
592 static void das16_timer_interrupt(unsigned long arg)
594 struct comedi_device *dev = (struct comedi_device *)arg;
595 struct das16_private_struct *devpriv = dev->private;
597 das16_interrupt(dev);
599 if (devpriv->timer_running)
600 mod_timer(&devpriv->timer, jiffies + timer_period());
603 static int das16_ai_check_chanlist(struct comedi_device *dev,
604 struct comedi_subdevice *s,
605 struct comedi_cmd *cmd)
607 unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
608 unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
611 for (i = 1; i < cmd->chanlist_len; i++) {
612 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
613 unsigned int range = CR_RANGE(cmd->chanlist[i]);
615 if (chan != ((chan0 + i) % s->n_chan)) {
616 dev_dbg(dev->class_dev,
617 "entries in chanlist must be consecutive channels, counting upwards\n");
621 if (range != range0) {
622 dev_dbg(dev->class_dev,
623 "entries in chanlist must all have the same gain\n");
631 static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
632 struct comedi_cmd *cmd)
634 const struct das16_board *board = comedi_board(dev);
635 struct das16_private_struct *devpriv = dev->private;
637 unsigned int trig_mask;
640 /* Step 1 : check if triggers are trivially valid */
642 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
644 trig_mask = TRIG_FOLLOW;
645 if (devpriv->can_burst)
646 trig_mask |= TRIG_TIMER | TRIG_EXT;
647 err |= cfc_check_trigger_src(&cmd->scan_begin_src, trig_mask);
649 trig_mask = TRIG_TIMER | TRIG_EXT;
650 if (devpriv->can_burst)
651 trig_mask |= TRIG_NOW;
652 err |= cfc_check_trigger_src(&cmd->convert_src, trig_mask);
654 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
655 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
660 /* Step 2a : make sure trigger sources are unique */
662 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
663 err |= cfc_check_trigger_is_unique(cmd->convert_src);
664 err |= cfc_check_trigger_is_unique(cmd->stop_src);
666 /* Step 2b : and mutually compatible */
668 /* make sure scan_begin_src and convert_src dont conflict */
669 if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
671 if (cmd->scan_begin_src != TRIG_FOLLOW && cmd->convert_src != TRIG_NOW)
677 /* Step 3: check if arguments are trivially valid */
679 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
681 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
682 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
684 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
686 /* check against maximum frequency */
687 if (cmd->scan_begin_src == TRIG_TIMER)
688 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
689 board->ai_speed * cmd->chanlist_len);
691 if (cmd->convert_src == TRIG_TIMER)
692 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
695 if (cmd->stop_src == TRIG_NONE)
696 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
701 /* step 4: fix up arguments */
702 if (cmd->scan_begin_src == TRIG_TIMER) {
703 arg = cmd->scan_begin_arg;
704 i8253_cascade_ns_to_timer(devpriv->clockbase,
708 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
710 if (cmd->convert_src == TRIG_TIMER) {
711 arg = cmd->convert_arg;
712 i8253_cascade_ns_to_timer(devpriv->clockbase,
716 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, arg);
721 /* Step 5: check channel list if it exists */
722 if (cmd->chanlist && cmd->chanlist_len > 0)
723 err |= das16_ai_check_chanlist(dev, s, cmd);
731 static unsigned int das16_set_pacer(struct comedi_device *dev, unsigned int ns,
734 struct das16_private_struct *devpriv = dev->private;
735 unsigned long timer_base = dev->iobase + DAS16_TIMER_BASE_REG;
737 i8253_cascade_ns_to_timer(devpriv->clockbase,
738 &devpriv->divisor1, &devpriv->divisor2,
739 &ns, rounding_flags);
741 i8254_set_mode(timer_base, 0, 1, I8254_MODE2 | I8254_BINARY);
742 i8254_set_mode(timer_base, 0, 2, I8254_MODE2 | I8254_BINARY);
743 i8254_write(timer_base, 0, 1, devpriv->divisor1);
744 i8254_write(timer_base, 0, 2, devpriv->divisor2);
749 static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s)
751 const struct das16_board *board = comedi_board(dev);
752 struct das16_private_struct *devpriv = dev->private;
753 struct comedi_async *async = s->async;
754 struct comedi_cmd *cmd = &async->cmd;
759 if (cmd->flags & TRIG_RT) {
760 dev_err(dev->class_dev,
761 "isa dma transfers cannot be performed with TRIG_RT, aborting\n");
765 devpriv->adc_byte_count = cmd->stop_arg * cfc_bytes_per_scan(s);
767 if (devpriv->can_burst)
768 outb(DAS1600_CONV_DISABLE, dev->iobase + DAS1600_CONV_REG);
770 /* set scan limits */
771 byte = CR_CHAN(cmd->chanlist[0]);
772 byte |= CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]) << 4;
773 outb(byte, dev->iobase + DAS16_MUX_REG);
775 /* set gain (this is also burst rate register but according to
776 * computer boards manual, burst rate does nothing, even on
778 if (board->ai_pg != das16_pg_none) {
779 range = CR_RANGE(cmd->chanlist[0]);
780 outb((das16_gainlists[board->ai_pg])[range],
781 dev->iobase + DAS16_GAIN_REG);
784 /* set counter mode and counts */
786 das16_set_pacer(dev, cmd->convert_arg,
787 cmd->flags & TRIG_ROUND_MASK);
789 /* enable counters */
791 if (devpriv->can_burst) {
792 if (cmd->convert_src == TRIG_NOW) {
793 outb(DAS1600_BURST_VAL,
794 dev->iobase + DAS1600_BURST_REG);
795 /* set burst length */
796 byte |= DAS16_PACER_BURST_LEN(cmd->chanlist_len - 1);
798 outb(0, dev->iobase + DAS1600_BURST_REG);
801 outb(byte, dev->iobase + DAS16_PACER_REG);
803 /* set up dma transfer */
804 flags = claim_dma_lock();
805 disable_dma(devpriv->dma_chan);
806 /* clear flip-flop to make sure 2-byte registers for
807 * count and address get set correctly */
808 clear_dma_ff(devpriv->dma_chan);
809 devpriv->current_buffer = 0;
810 set_dma_addr(devpriv->dma_chan,
811 devpriv->dma_buffer_addr[devpriv->current_buffer]);
812 devpriv->dma_transfer_size = DAS16_DMA_SIZE;
813 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
814 enable_dma(devpriv->dma_chan);
815 release_dma_lock(flags);
817 /* set up interrupt */
818 devpriv->timer_running = 1;
819 devpriv->timer.expires = jiffies + timer_period();
820 add_timer(&devpriv->timer);
822 das16_ai_enable(dev, DAS16_CTRL_DMAE, cmd->convert_src);
824 if (devpriv->can_burst)
825 outb(0, dev->iobase + DAS1600_CONV_REG);
830 static int das16_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
832 struct das16_private_struct *devpriv = dev->private;
835 spin_lock_irqsave(&dev->spinlock, flags);
837 das16_ai_disable(dev);
838 disable_dma(devpriv->dma_chan);
840 /* disable SW timer */
841 if (devpriv->timer_running) {
842 devpriv->timer_running = 0;
843 del_timer(&devpriv->timer);
846 if (devpriv->can_burst)
847 outb(0, dev->iobase + DAS1600_BURST_REG);
849 spin_unlock_irqrestore(&dev->spinlock, flags);
854 static void das16_ai_munge(struct comedi_device *dev,
855 struct comedi_subdevice *s, void *array,
856 unsigned int num_bytes,
857 unsigned int start_chan_index)
859 unsigned int i, num_samples = num_bytes / sizeof(short);
860 unsigned short *data = array;
862 for (i = 0; i < num_samples; i++) {
863 data[i] = le16_to_cpu(data[i]);
864 if (s->maxdata == 0x0fff)
866 data[i] &= s->maxdata;
870 static int das16_ai_eoc(struct comedi_device *dev,
871 struct comedi_subdevice *s,
872 struct comedi_insn *insn,
873 unsigned long context)
877 status = inb(dev->iobase + DAS16_STATUS_REG);
878 if ((status & DAS16_STATUS_BUSY) == 0)
883 static int das16_ai_insn_read(struct comedi_device *dev,
884 struct comedi_subdevice *s,
885 struct comedi_insn *insn,
888 const struct das16_board *board = comedi_board(dev);
889 unsigned int chan = CR_CHAN(insn->chanspec);
890 unsigned int range = CR_RANGE(insn->chanspec);
895 das16_ai_disable(dev);
897 /* set multiplexer */
898 outb(chan | (chan << 4), dev->iobase + DAS16_MUX_REG);
901 if (board->ai_pg != das16_pg_none) {
902 outb((das16_gainlists[board->ai_pg])[range],
903 dev->iobase + DAS16_GAIN_REG);
906 for (i = 0; i < insn->n; i++) {
907 /* trigger conversion */
908 outb_p(0, dev->iobase + DAS16_TRIG_REG);
910 ret = comedi_timeout(dev, s, insn, das16_ai_eoc, 0);
914 val = inb(dev->iobase + DAS16_AI_MSB_REG) << 8;
915 val |= inb(dev->iobase + DAS16_AI_LSB_REG);
916 if (s->maxdata == 0x0fff)
926 static int das16_ao_insn_write(struct comedi_device *dev,
927 struct comedi_subdevice *s,
928 struct comedi_insn *insn,
931 unsigned int chan = CR_CHAN(insn->chanspec);
935 for (i = 0; i < insn->n; i++) {
939 outb(val & 0xff, dev->iobase + DAS16_AO_LSB_REG(chan));
940 outb((val >> 8) & 0xff, dev->iobase + DAS16_AO_MSB_REG(chan));
946 static int das16_di_insn_bits(struct comedi_device *dev,
947 struct comedi_subdevice *s,
948 struct comedi_insn *insn,
951 data[1] = inb(dev->iobase + DAS16_DIO_REG) & 0xf;
956 static int das16_do_insn_bits(struct comedi_device *dev,
957 struct comedi_subdevice *s,
958 struct comedi_insn *insn,
961 if (comedi_dio_update_state(s, data))
962 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 = I8254_OSC_BASE_10MHZ;
1047 devpriv->clockbase = I8254_OSC_BASE_1MHZ;
1050 devpriv->clockbase = I8254_OSC_BASE_1MHZ /
1053 devpriv->clockbase = I8254_OSC_BASE_1MHZ;
1056 /* initialize dma */
1057 if (dma_chan == 1 || dma_chan == 3) {
1058 unsigned long flags;
1061 if (request_dma(dma_chan, dev->board_name)) {
1062 dev_err(dev->class_dev,
1063 "failed to request dma channel %i\n",
1067 devpriv->dma_chan = dma_chan;
1069 /* allocate dma buffers */
1070 for (i = 0; i < 2; i++) {
1073 p = pci_alloc_consistent(NULL, DAS16_DMA_SIZE,
1074 &devpriv->dma_buffer_addr[i]);
1077 devpriv->dma_buffer[i] = p;
1080 flags = claim_dma_lock();
1081 disable_dma(devpriv->dma_chan);
1082 set_dma_mode(devpriv->dma_chan, DMA_MODE_READ);
1083 release_dma_lock(flags);
1085 init_timer(&devpriv->timer);
1086 devpriv->timer.function = das16_timer_interrupt;
1087 devpriv->timer.data = (unsigned long)dev;
1090 /* get any user-defined input range */
1091 if (board->ai_pg == das16_pg_none &&
1092 (it->options[4] || it->options[5])) {
1093 /* allocate single-range range table */
1094 lrange = kzalloc(sizeof(*lrange) + sizeof(*krange), GFP_KERNEL);
1098 /* initialize ai range */
1099 devpriv->user_ai_range_table = lrange;
1101 krange = devpriv->user_ai_range_table->range;
1102 krange->min = it->options[4];
1103 krange->max = it->options[5];
1104 krange->flags = UNIT_volt;
1107 /* get any user-defined output range */
1108 if (it->options[6] || it->options[7]) {
1109 /* allocate single-range range table */
1110 lrange = kzalloc(sizeof(*lrange) + sizeof(*krange), GFP_KERNEL);
1114 /* initialize ao range */
1115 devpriv->user_ao_range_table = lrange;
1117 krange = devpriv->user_ao_range_table->range;
1118 krange->min = it->options[6];
1119 krange->max = it->options[7];
1120 krange->flags = UNIT_volt;
1123 ret = comedi_alloc_subdevices(dev, 4 + board->has_8255);
1127 status = inb(dev->iobase + DAS16_STATUS_REG);
1129 /* Analog Input subdevice */
1130 s = &dev->subdevices[0];
1131 s->type = COMEDI_SUBD_AI;
1132 s->subdev_flags = SDF_READABLE;
1133 if (status & DAS16_STATUS_MUXBIT) {
1134 s->subdev_flags |= SDF_GROUND;
1137 s->subdev_flags |= SDF_DIFF;
1140 s->len_chanlist = s->n_chan;
1141 s->maxdata = board->ai_maxdata;
1142 if (devpriv->user_ai_range_table) { /* user defined ai range */
1143 s->range_table = devpriv->user_ai_range_table;
1144 } else if (status & DAS16_STATUS_UNIPOLAR) {
1145 s->range_table = das16_ai_uni_lranges[board->ai_pg];
1147 s->range_table = das16_ai_bip_lranges[board->ai_pg];
1149 s->insn_read = das16_ai_insn_read;
1150 if (devpriv->dma_chan) {
1151 dev->read_subdev = s;
1152 s->subdev_flags |= SDF_CMD_READ;
1153 s->do_cmdtest = das16_cmd_test;
1154 s->do_cmd = das16_cmd_exec;
1155 s->cancel = das16_cancel;
1156 s->munge = das16_ai_munge;
1159 /* Analog Output subdevice */
1160 s = &dev->subdevices[1];
1161 if (board->has_ao) {
1162 s->type = COMEDI_SUBD_AO;
1163 s->subdev_flags = SDF_WRITABLE;
1165 s->maxdata = 0x0fff;
1166 s->range_table = devpriv->user_ao_range_table;
1167 s->insn_write = das16_ao_insn_write;
1169 s->type = COMEDI_SUBD_UNUSED;
1172 /* Digital Input subdevice */
1173 s = &dev->subdevices[2];
1174 s->type = COMEDI_SUBD_DI;
1175 s->subdev_flags = SDF_READABLE;
1178 s->range_table = &range_digital;
1179 s->insn_bits = das16_di_insn_bits;
1181 /* Digital Output subdevice */
1182 s = &dev->subdevices[3];
1183 s->type = COMEDI_SUBD_DO;
1184 s->subdev_flags = SDF_WRITABLE;
1187 s->range_table = &range_digital;
1188 s->insn_bits = das16_do_insn_bits;
1190 /* initialize digital output lines */
1191 outb(s->state, dev->iobase + DAS16_DIO_REG);
1193 /* 8255 Digital I/O subdevice */
1194 if (board->has_8255) {
1195 s = &dev->subdevices[4];
1196 ret = subdev_8255_init(dev, s, NULL,
1197 dev->iobase + board->i8255_offset);
1203 /* set the interrupt level */
1204 devpriv->ctrl_reg = DAS16_CTRL_IRQ(dev->irq);
1205 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
1207 if (devpriv->can_burst) {
1208 outb(DAS1600_ENABLE_VAL, dev->iobase + DAS1600_ENABLE_REG);
1209 outb(0, dev->iobase + DAS1600_CONV_REG);
1210 outb(0, dev->iobase + DAS1600_BURST_REG);
1216 static void das16_detach(struct comedi_device *dev)
1218 const struct das16_board *board = comedi_board(dev);
1219 struct das16_private_struct *devpriv = dev->private;
1226 for (i = 0; i < 2; i++) {
1227 if (devpriv->dma_buffer[i])
1228 pci_free_consistent(NULL, DAS16_DMA_SIZE,
1229 devpriv->dma_buffer[i],
1231 dma_buffer_addr[i]);
1233 if (devpriv->dma_chan)
1234 free_dma(devpriv->dma_chan);
1235 kfree(devpriv->user_ai_range_table);
1236 kfree(devpriv->user_ao_range_table);
1238 if (devpriv->extra_iobase)
1239 release_region(devpriv->extra_iobase,
1240 board->size & 0x3ff);
1243 comedi_legacy_detach(dev);
1246 static struct comedi_driver das16_driver = {
1247 .driver_name = "das16",
1248 .module = THIS_MODULE,
1249 .attach = das16_attach,
1250 .detach = das16_detach,
1251 .board_name = &das16_boards[0].name,
1252 .num_names = ARRAY_SIZE(das16_boards),
1253 .offset = sizeof(das16_boards[0]),
1255 module_comedi_driver(das16_driver);
1257 MODULE_AUTHOR("Comedi http://www.comedi.org");
1258 MODULE_DESCRIPTION("Comedi driver for DAS16 compatible boards");
1259 MODULE_LICENSE("GPL");