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), DAS-16G (das-16g),
26 * DAS-16F (das-16f), DAS-1201 (das-1201), DAS-1202 (das-1202),
27 * DAS-1401 (das-1401), DAS-1402 (das-1402), DAS-1601 (das-1601),
28 * DAS-1602 (das-1602),
29 * [ComputerBoards] PC104-DAS16/JR (pc104-das16jr),
30 * PC104-DAS16JR/16 (pc104-das16jr/16), CIO-DAS16 (cio-das16),
31 * CIO-DAS16F (cio-das16/f), CIO-DAS16/JR (cio-das16/jr),
32 * CIO-DAS16JR/16 (cio-das16jr/16), CIO-DAS1401/12 (cio-das1401/12),
33 * CIO-DAS1402/12 (cio-das1402/12), CIO-DAS1402/16 (cio-das1402/16),
34 * CIO-DAS1601/12 (cio-das1601/12), CIO-DAS1602/12 (cio-das1602/12),
35 * CIO-DAS1602/16 (cio-das1602/16), CIO-DAS16/330 (cio-das16/330)
39 * A rewrite of the das16 and das1600 drivers.
42 * [0] - base io address
43 * [1] - irq (does nothing, irq is not used anymore)
44 * [2] - dma channel (optional, required for comedi_command support)
45 * [3] - master clock speed in MHz (optional, 1 or 10, ignored if
46 * board can probe clock, defaults to 1)
47 * [4] - analog input range lowest voltage in microvolts (optional,
48 * only useful if your board does not have software
50 * [5] - analog input range highest voltage in microvolts (optional,
51 * only useful if board does not have software programmable
53 * [6] - analog output range lowest voltage in microvolts (optional)
54 * [7] - analog output range highest voltage in microvolts (optional)
56 * Passing a zero for an option is the same as leaving it unspecified.
60 * Testing and debugging help provided by Daniel Koch.
64 * 4919.PDF (das1400, 1600)
66 * 4923.PDF (das1200, 1400, 1600)
68 * Computer boards manuals also available from their website
69 * www.measurementcomputing.com
72 #include <linux/module.h>
73 #include <linux/slab.h>
74 #include <linux/interrupt.h>
76 #include "../comedidev.h"
78 #include "comedi_isadma.h"
79 #include "comedi_8254.h"
82 #define DAS16_DMA_SIZE 0xff00 /* size in bytes of allocated dma buffer */
87 #define DAS16_TRIG_REG 0x00
88 #define DAS16_AI_LSB_REG 0x00
89 #define DAS16_AI_MSB_REG 0x01
90 #define DAS16_MUX_REG 0x02
91 #define DAS16_DIO_REG 0x03
92 #define DAS16_AO_LSB_REG(x) ((x) ? 0x06 : 0x04)
93 #define DAS16_AO_MSB_REG(x) ((x) ? 0x07 : 0x05)
94 #define DAS16_STATUS_REG 0x08
95 #define DAS16_STATUS_BUSY BIT(7)
96 #define DAS16_STATUS_UNIPOLAR BIT(6)
97 #define DAS16_STATUS_MUXBIT BIT(5)
98 #define DAS16_STATUS_INT BIT(4)
99 #define DAS16_CTRL_REG 0x09
100 #define DAS16_CTRL_INTE BIT(7)
101 #define DAS16_CTRL_IRQ(x) (((x) & 0x7) << 4)
102 #define DAS16_CTRL_DMAE BIT(2)
103 #define DAS16_CTRL_PACING_MASK (3 << 0)
104 #define DAS16_CTRL_INT_PACER (3 << 0)
105 #define DAS16_CTRL_EXT_PACER (2 << 0)
106 #define DAS16_CTRL_SOFT_PACER (0 << 0)
107 #define DAS16_PACER_REG 0x0a
108 #define DAS16_PACER_BURST_LEN(x) (((x) & 0xf) << 4)
109 #define DAS16_PACER_CTR0 BIT(1)
110 #define DAS16_PACER_TRIG0 BIT(0)
111 #define DAS16_GAIN_REG 0x0b
112 #define DAS16_TIMER_BASE_REG 0x0c /* to 0x0f */
114 #define DAS1600_CONV_REG 0x404
115 #define DAS1600_CONV_DISABLE BIT(6)
116 #define DAS1600_BURST_REG 0x405
117 #define DAS1600_BURST_VAL BIT(6)
118 #define DAS1600_ENABLE_REG 0x406
119 #define DAS1600_ENABLE_VAL BIT(6)
120 #define DAS1600_STATUS_REG 0x407
121 #define DAS1600_STATUS_BME BIT(6)
122 #define DAS1600_STATUS_ME BIT(5)
123 #define DAS1600_STATUS_CD BIT(4)
124 #define DAS1600_STATUS_WS BIT(1)
125 #define DAS1600_STATUS_CLK_10MHZ BIT(0)
127 static const struct comedi_lrange range_das1x01_bip = {
136 static const struct comedi_lrange range_das1x01_unip = {
145 static const struct comedi_lrange range_das1x02_bip = {
154 static const struct comedi_lrange range_das1x02_unip = {
163 static const struct comedi_lrange range_das16jr = {
177 static const struct comedi_lrange range_das16jr_16 = {
190 static const int das16jr_gainlist[] = { 8, 0, 1, 2, 3, 4, 5, 6, 7 };
191 static const int das16jr_16_gainlist[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
192 static const int das1600_gainlist[] = { 0, 1, 2, 3 };
202 static const int *const das16_gainlists[] = {
210 static const struct comedi_lrange *const das16_ai_uni_lranges[] = {
218 static const struct comedi_lrange *const das16_ai_bip_lranges[] = {
228 unsigned int ai_maxdata;
229 unsigned int ai_speed; /* max conversion speed in nanosec */
231 unsigned int has_ao:1;
232 unsigned int has_8255:1;
234 unsigned int i8255_offset;
240 static const struct das16_board das16_boards[] = {
243 .ai_maxdata = 0x0fff,
245 .ai_pg = das16_pg_none,
248 .i8255_offset = 0x10,
253 .ai_maxdata = 0x0fff,
255 .ai_pg = das16_pg_none,
258 .i8255_offset = 0x10,
263 .ai_maxdata = 0x0fff,
265 .ai_pg = das16_pg_none,
268 .i8255_offset = 0x10,
273 .ai_maxdata = 0x0fff,
275 .ai_pg = das16_pg_none,
278 .i8255_offset = 0x10,
282 .name = "cio-das16/f",
283 .ai_maxdata = 0x0fff,
285 .ai_pg = das16_pg_none,
288 .i8255_offset = 0x10,
292 .name = "cio-das16/jr",
293 .ai_maxdata = 0x0fff,
295 .ai_pg = das16_pg_16jr,
299 .name = "pc104-das16jr",
300 .ai_maxdata = 0x0fff,
302 .ai_pg = das16_pg_16jr,
306 .name = "cio-das16jr/16",
307 .ai_maxdata = 0xffff,
309 .ai_pg = das16_pg_16jr_16,
313 .name = "pc104-das16jr/16",
314 .ai_maxdata = 0xffff,
316 .ai_pg = das16_pg_16jr_16,
321 .ai_maxdata = 0x0fff,
323 .ai_pg = das16_pg_none,
325 .i8255_offset = 0x400,
330 .ai_maxdata = 0x0fff,
332 .ai_pg = das16_pg_none,
334 .i8255_offset = 0x400,
339 .ai_maxdata = 0x0fff,
341 .ai_pg = das16_pg_1601,
346 .ai_maxdata = 0x0fff,
348 .ai_pg = das16_pg_1602,
353 .ai_maxdata = 0x0fff,
355 .ai_pg = das16_pg_1601,
358 .i8255_offset = 0x400,
363 .ai_maxdata = 0x0fff,
365 .ai_pg = das16_pg_1602,
368 .i8255_offset = 0x400,
372 .name = "cio-das1401/12",
373 .ai_maxdata = 0x0fff,
375 .ai_pg = das16_pg_1601,
379 .name = "cio-das1402/12",
380 .ai_maxdata = 0x0fff,
382 .ai_pg = das16_pg_1602,
386 .name = "cio-das1402/16",
387 .ai_maxdata = 0xffff,
389 .ai_pg = das16_pg_1602,
393 .name = "cio-das1601/12",
394 .ai_maxdata = 0x0fff,
396 .ai_pg = das16_pg_1601,
399 .i8255_offset = 0x400,
403 .name = "cio-das1602/12",
404 .ai_maxdata = 0x0fff,
406 .ai_pg = das16_pg_1602,
409 .i8255_offset = 0x400,
413 .name = "cio-das1602/16",
414 .ai_maxdata = 0xffff,
416 .ai_pg = das16_pg_1602,
419 .i8255_offset = 0x400,
423 .name = "cio-das16/330",
424 .ai_maxdata = 0x0fff,
426 .ai_pg = das16_pg_16jr,
433 * Period for timer interrupt in jiffies. It's a function
434 * to deal with possibility of dynamic HZ patches
436 static inline int timer_period(void)
441 struct das16_private_struct {
442 struct comedi_isadma *dma;
443 unsigned int clockbase;
444 unsigned int ctrl_reg;
445 unsigned int divisor1;
446 unsigned int divisor2;
447 struct timer_list timer;
448 unsigned long extra_iobase;
449 unsigned int can_burst:1;
450 unsigned int timer_running:1;
453 static void das16_ai_setup_dma(struct comedi_device *dev,
454 struct comedi_subdevice *s,
455 unsigned int unread_samples)
457 struct das16_private_struct *devpriv = dev->private;
458 struct comedi_isadma *dma = devpriv->dma;
459 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
460 unsigned int max_samples = comedi_bytes_to_samples(s, desc->maxsize);
461 unsigned int nsamples;
464 * Determine dma size based on the buffer size plus the number of
465 * unread samples and the number of samples remaining in the command.
467 nsamples = comedi_nsamples_left(s, max_samples + unread_samples);
468 if (nsamples > unread_samples) {
469 nsamples -= unread_samples;
470 desc->size = comedi_samples_to_bytes(s, nsamples);
471 comedi_isadma_program(desc);
475 static void das16_interrupt(struct comedi_device *dev)
477 struct das16_private_struct *devpriv = dev->private;
478 struct comedi_subdevice *s = dev->read_subdev;
479 struct comedi_async *async = s->async;
480 struct comedi_cmd *cmd = &async->cmd;
481 struct comedi_isadma *dma = devpriv->dma;
482 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
483 unsigned long spin_flags;
484 unsigned int residue;
486 unsigned int nsamples;
488 spin_lock_irqsave(&dev->spinlock, spin_flags);
489 if (!(devpriv->ctrl_reg & DAS16_CTRL_DMAE)) {
490 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
495 * The pc104-das16jr (at least) has problems if the dma
496 * transfer is interrupted in the middle of transferring
499 residue = comedi_isadma_disable_on_sample(desc->chan,
500 comedi_bytes_per_sample(s));
502 /* figure out how many samples to read */
503 if (residue > desc->size) {
504 dev_err(dev->class_dev, "residue > transfer size!\n");
505 async->events |= COMEDI_CB_ERROR;
508 nbytes = desc->size - residue;
510 nsamples = comedi_bytes_to_samples(s, nbytes);
512 /* restart DMA if more samples are needed */
514 dma->cur_dma = 1 - dma->cur_dma;
515 das16_ai_setup_dma(dev, s, nsamples);
518 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
520 comedi_buf_write_samples(s, desc->virt_addr, nsamples);
522 if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg)
523 async->events |= COMEDI_CB_EOA;
525 comedi_handle_events(dev, s);
528 static void das16_timer_interrupt(unsigned long arg)
530 struct comedi_device *dev = (struct comedi_device *)arg;
531 struct das16_private_struct *devpriv = dev->private;
534 das16_interrupt(dev);
536 spin_lock_irqsave(&dev->spinlock, flags);
537 if (devpriv->timer_running)
538 mod_timer(&devpriv->timer, jiffies + timer_period());
539 spin_unlock_irqrestore(&dev->spinlock, flags);
542 static void das16_ai_set_mux_range(struct comedi_device *dev,
543 unsigned int first_chan,
544 unsigned int last_chan,
547 const struct das16_board *board = dev->board_ptr;
549 /* set multiplexer */
550 outb(first_chan | (last_chan << 4), dev->iobase + DAS16_MUX_REG);
552 /* some boards do not have programmable gain */
553 if (board->ai_pg == das16_pg_none)
557 * Set gain (this is also burst rate register but according to
558 * computer boards manual, burst rate does nothing, even on
561 outb((das16_gainlists[board->ai_pg])[range],
562 dev->iobase + DAS16_GAIN_REG);
565 static int das16_ai_check_chanlist(struct comedi_device *dev,
566 struct comedi_subdevice *s,
567 struct comedi_cmd *cmd)
569 unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
570 unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
573 for (i = 1; i < cmd->chanlist_len; i++) {
574 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
575 unsigned int range = CR_RANGE(cmd->chanlist[i]);
577 if (chan != ((chan0 + i) % s->n_chan)) {
578 dev_dbg(dev->class_dev,
579 "entries in chanlist must be consecutive channels, counting upwards\n");
583 if (range != range0) {
584 dev_dbg(dev->class_dev,
585 "entries in chanlist must all have the same gain\n");
593 static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
594 struct comedi_cmd *cmd)
596 const struct das16_board *board = dev->board_ptr;
597 struct das16_private_struct *devpriv = dev->private;
599 unsigned int trig_mask;
602 /* Step 1 : check if triggers are trivially valid */
604 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
606 trig_mask = TRIG_FOLLOW;
607 if (devpriv->can_burst)
608 trig_mask |= TRIG_TIMER | TRIG_EXT;
609 err |= comedi_check_trigger_src(&cmd->scan_begin_src, trig_mask);
611 trig_mask = TRIG_TIMER | TRIG_EXT;
612 if (devpriv->can_burst)
613 trig_mask |= TRIG_NOW;
614 err |= comedi_check_trigger_src(&cmd->convert_src, trig_mask);
616 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
617 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
622 /* Step 2a : make sure trigger sources are unique */
624 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
625 err |= comedi_check_trigger_is_unique(cmd->convert_src);
626 err |= comedi_check_trigger_is_unique(cmd->stop_src);
628 /* Step 2b : and mutually compatible */
630 /* make sure scan_begin_src and convert_src dont conflict */
631 if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
633 if (cmd->scan_begin_src != TRIG_FOLLOW && cmd->convert_src != TRIG_NOW)
639 /* Step 3: check if arguments are trivially valid */
641 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
643 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
644 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
646 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
649 /* check against maximum frequency */
650 if (cmd->scan_begin_src == TRIG_TIMER) {
651 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
656 if (cmd->convert_src == TRIG_TIMER) {
657 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
661 if (cmd->stop_src == TRIG_COUNT)
662 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
664 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
669 /* step 4: fix up arguments */
670 if (cmd->scan_begin_src == TRIG_TIMER) {
671 arg = cmd->scan_begin_arg;
672 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
673 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
675 if (cmd->convert_src == TRIG_TIMER) {
676 arg = cmd->convert_arg;
677 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
678 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
683 /* Step 5: check channel list if it exists */
684 if (cmd->chanlist && cmd->chanlist_len > 0)
685 err |= das16_ai_check_chanlist(dev, s, cmd);
693 static unsigned int das16_set_pacer(struct comedi_device *dev, unsigned int ns,
696 comedi_8254_cascade_ns_to_timer(dev->pacer, &ns, flags);
697 comedi_8254_update_divisors(dev->pacer);
698 comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
703 static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s)
705 struct das16_private_struct *devpriv = dev->private;
706 struct comedi_isadma *dma = devpriv->dma;
707 struct comedi_async *async = s->async;
708 struct comedi_cmd *cmd = &async->cmd;
709 unsigned int first_chan = CR_CHAN(cmd->chanlist[0]);
710 unsigned int last_chan = CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]);
711 unsigned int range = CR_RANGE(cmd->chanlist[0]);
715 if (cmd->flags & CMDF_PRIORITY) {
716 dev_err(dev->class_dev,
717 "isa dma transfers cannot be performed with CMDF_PRIORITY, aborting\n");
721 if (devpriv->can_burst)
722 outb(DAS1600_CONV_DISABLE, dev->iobase + DAS1600_CONV_REG);
724 /* set mux and range for chanlist scan */
725 das16_ai_set_mux_range(dev, first_chan, last_chan, range);
727 /* set counter mode and counts */
728 cmd->convert_arg = das16_set_pacer(dev, cmd->convert_arg, cmd->flags);
730 /* enable counters */
732 if (devpriv->can_burst) {
733 if (cmd->convert_src == TRIG_NOW) {
734 outb(DAS1600_BURST_VAL,
735 dev->iobase + DAS1600_BURST_REG);
736 /* set burst length */
737 byte |= DAS16_PACER_BURST_LEN(cmd->chanlist_len - 1);
739 outb(0, dev->iobase + DAS1600_BURST_REG);
742 outb(byte, dev->iobase + DAS16_PACER_REG);
744 /* set up dma transfer */
746 das16_ai_setup_dma(dev, s, 0);
749 spin_lock_irqsave(&dev->spinlock, flags);
750 devpriv->timer_running = 1;
751 devpriv->timer.expires = jiffies + timer_period();
752 add_timer(&devpriv->timer);
754 /* enable DMA interrupt with external or internal pacing */
755 devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE | DAS16_CTRL_PACING_MASK);
756 devpriv->ctrl_reg |= DAS16_CTRL_DMAE;
757 if (cmd->convert_src == TRIG_EXT)
758 devpriv->ctrl_reg |= DAS16_CTRL_EXT_PACER;
760 devpriv->ctrl_reg |= DAS16_CTRL_INT_PACER;
761 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
763 if (devpriv->can_burst)
764 outb(0, dev->iobase + DAS1600_CONV_REG);
765 spin_unlock_irqrestore(&dev->spinlock, flags);
770 static int das16_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
772 struct das16_private_struct *devpriv = dev->private;
773 struct comedi_isadma *dma = devpriv->dma;
776 spin_lock_irqsave(&dev->spinlock, flags);
778 /* disable interrupts, dma and pacer clocked conversions */
779 devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE | DAS16_CTRL_DMAE |
780 DAS16_CTRL_PACING_MASK);
781 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
783 comedi_isadma_disable(dma->chan);
785 /* disable SW timer */
786 if (devpriv->timer_running) {
787 devpriv->timer_running = 0;
788 del_timer(&devpriv->timer);
791 if (devpriv->can_burst)
792 outb(0, dev->iobase + DAS1600_BURST_REG);
794 spin_unlock_irqrestore(&dev->spinlock, flags);
799 static void das16_ai_munge(struct comedi_device *dev,
800 struct comedi_subdevice *s, void *array,
801 unsigned int num_bytes,
802 unsigned int start_chan_index)
804 unsigned short *data = array;
805 unsigned int num_samples = comedi_bytes_to_samples(s, num_bytes);
809 for (i = 0; i < num_samples; i++) {
810 data[i] = le16_to_cpu(buf[i]);
811 if (s->maxdata == 0x0fff)
813 data[i] &= s->maxdata;
817 static int das16_ai_eoc(struct comedi_device *dev,
818 struct comedi_subdevice *s,
819 struct comedi_insn *insn,
820 unsigned long context)
824 status = inb(dev->iobase + DAS16_STATUS_REG);
825 if ((status & DAS16_STATUS_BUSY) == 0)
830 static int das16_ai_insn_read(struct comedi_device *dev,
831 struct comedi_subdevice *s,
832 struct comedi_insn *insn,
835 unsigned int chan = CR_CHAN(insn->chanspec);
836 unsigned int range = CR_RANGE(insn->chanspec);
841 /* set mux and range for single channel */
842 das16_ai_set_mux_range(dev, chan, chan, range);
844 for (i = 0; i < insn->n; i++) {
845 /* trigger conversion */
846 outb_p(0, dev->iobase + DAS16_TRIG_REG);
848 ret = comedi_timeout(dev, s, insn, das16_ai_eoc, 0);
852 val = inb(dev->iobase + DAS16_AI_MSB_REG) << 8;
853 val |= inb(dev->iobase + DAS16_AI_LSB_REG);
854 if (s->maxdata == 0x0fff)
864 static int das16_ao_insn_write(struct comedi_device *dev,
865 struct comedi_subdevice *s,
866 struct comedi_insn *insn,
869 unsigned int chan = CR_CHAN(insn->chanspec);
872 for (i = 0; i < insn->n; i++) {
873 unsigned int val = data[i];
875 s->readback[chan] = val;
879 outb(val & 0xff, dev->iobase + DAS16_AO_LSB_REG(chan));
880 outb((val >> 8) & 0xff, dev->iobase + DAS16_AO_MSB_REG(chan));
886 static int das16_di_insn_bits(struct comedi_device *dev,
887 struct comedi_subdevice *s,
888 struct comedi_insn *insn,
891 data[1] = inb(dev->iobase + DAS16_DIO_REG) & 0xf;
896 static int das16_do_insn_bits(struct comedi_device *dev,
897 struct comedi_subdevice *s,
898 struct comedi_insn *insn,
901 if (comedi_dio_update_state(s, data))
902 outb(s->state, dev->iobase + DAS16_DIO_REG);
909 static int das16_probe(struct comedi_device *dev, struct comedi_devconfig *it)
911 const struct das16_board *board = dev->board_ptr;
914 /* diobits indicates boards */
915 diobits = inb(dev->iobase + DAS16_DIO_REG) & 0xf0;
916 if (board->id != diobits) {
917 dev_err(dev->class_dev,
918 "requested board's id bits are incorrect (0x%x != 0x%x)\n",
926 static void das16_reset(struct comedi_device *dev)
928 outb(0, dev->iobase + DAS16_STATUS_REG);
929 outb(0, dev->iobase + DAS16_CTRL_REG);
930 outb(0, dev->iobase + DAS16_PACER_REG);
933 static void das16_alloc_dma(struct comedi_device *dev, unsigned int dma_chan)
935 struct das16_private_struct *devpriv = dev->private;
937 /* only DMA channels 3 and 1 are valid */
938 if (!(dma_chan == 1 || dma_chan == 3))
941 /* DMA uses two buffers */
942 devpriv->dma = comedi_isadma_alloc(dev, 2, dma_chan, dma_chan,
943 DAS16_DMA_SIZE, COMEDI_ISADMA_READ);
945 setup_timer(&devpriv->timer, das16_timer_interrupt,
950 static void das16_free_dma(struct comedi_device *dev)
952 struct das16_private_struct *devpriv = dev->private;
955 if (devpriv->timer.data)
956 del_timer_sync(&devpriv->timer);
957 comedi_isadma_free(devpriv->dma);
961 static const struct comedi_lrange *das16_ai_range(struct comedi_device *dev,
962 struct comedi_subdevice *s,
963 struct comedi_devconfig *it,
964 unsigned int pg_type,
967 unsigned int min = it->options[4];
968 unsigned int max = it->options[5];
970 /* get any user-defined input range */
971 if (pg_type == das16_pg_none && (min || max)) {
972 struct comedi_lrange *lrange;
973 struct comedi_krange *krange;
975 /* allocate single-range range table */
976 lrange = comedi_alloc_spriv(s,
977 sizeof(*lrange) + sizeof(*krange));
979 return &range_unknown;
981 /* initialize ai range */
983 krange = lrange->range;
986 krange->flags = UNIT_volt;
991 /* use software programmable range */
992 if (status & DAS16_STATUS_UNIPOLAR)
993 return das16_ai_uni_lranges[pg_type];
994 return das16_ai_bip_lranges[pg_type];
997 static const struct comedi_lrange *das16_ao_range(struct comedi_device *dev,
998 struct comedi_subdevice *s,
999 struct comedi_devconfig *it)
1001 unsigned int min = it->options[6];
1002 unsigned int max = it->options[7];
1004 /* get any user-defined output range */
1006 struct comedi_lrange *lrange;
1007 struct comedi_krange *krange;
1009 /* allocate single-range range table */
1010 lrange = comedi_alloc_spriv(s,
1011 sizeof(*lrange) + sizeof(*krange));
1013 return &range_unknown;
1015 /* initialize ao range */
1017 krange = lrange->range;
1020 krange->flags = UNIT_volt;
1025 return &range_unknown;
1028 static int das16_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1030 const struct das16_board *board = dev->board_ptr;
1031 struct das16_private_struct *devpriv;
1032 struct comedi_subdevice *s;
1033 unsigned int osc_base;
1034 unsigned int status;
1037 /* check that clock setting is valid */
1038 if (it->options[3]) {
1039 if (it->options[3] != 1 && it->options[3] != 10) {
1040 dev_err(dev->class_dev,
1041 "Invalid option. Master clock must be set to 1 or 10 (MHz)\n");
1046 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1050 if (board->size < 0x400) {
1051 ret = comedi_request_region(dev, it->options[0], board->size);
1055 ret = comedi_request_region(dev, it->options[0], 0x10);
1058 /* Request an additional region for the 8255 */
1059 ret = __comedi_request_region(dev, dev->iobase + 0x400,
1060 board->size & 0x3ff);
1063 devpriv->extra_iobase = dev->iobase + 0x400;
1064 devpriv->can_burst = 1;
1067 /* probe id bits to make sure they are consistent */
1068 if (das16_probe(dev, it))
1071 /* get master clock speed */
1072 osc_base = I8254_OSC_BASE_1MHZ;
1073 if (devpriv->can_burst) {
1074 status = inb(dev->iobase + DAS1600_STATUS_REG);
1075 if (status & DAS1600_STATUS_CLK_10MHZ)
1076 osc_base = I8254_OSC_BASE_10MHZ;
1079 osc_base = I8254_OSC_BASE_1MHZ / it->options[3];
1082 dev->pacer = comedi_8254_init(dev->iobase + DAS16_TIMER_BASE_REG,
1083 osc_base, I8254_IO8, 0);
1087 das16_alloc_dma(dev, it->options[2]);
1089 ret = comedi_alloc_subdevices(dev, 4 + board->has_8255);
1093 status = inb(dev->iobase + DAS16_STATUS_REG);
1095 /* Analog Input subdevice */
1096 s = &dev->subdevices[0];
1097 s->type = COMEDI_SUBD_AI;
1098 s->subdev_flags = SDF_READABLE;
1099 if (status & DAS16_STATUS_MUXBIT) {
1100 s->subdev_flags |= SDF_GROUND;
1103 s->subdev_flags |= SDF_DIFF;
1106 s->len_chanlist = s->n_chan;
1107 s->maxdata = board->ai_maxdata;
1108 s->range_table = das16_ai_range(dev, s, it, board->ai_pg, status);
1109 s->insn_read = das16_ai_insn_read;
1111 dev->read_subdev = s;
1112 s->subdev_flags |= SDF_CMD_READ;
1113 s->do_cmdtest = das16_cmd_test;
1114 s->do_cmd = das16_cmd_exec;
1115 s->cancel = das16_cancel;
1116 s->munge = das16_ai_munge;
1119 /* Analog Output subdevice */
1120 s = &dev->subdevices[1];
1121 if (board->has_ao) {
1122 s->type = COMEDI_SUBD_AO;
1123 s->subdev_flags = SDF_WRITABLE;
1125 s->maxdata = 0x0fff;
1126 s->range_table = das16_ao_range(dev, s, it);
1127 s->insn_write = das16_ao_insn_write;
1129 ret = comedi_alloc_subdev_readback(s);
1133 s->type = COMEDI_SUBD_UNUSED;
1136 /* Digital Input subdevice */
1137 s = &dev->subdevices[2];
1138 s->type = COMEDI_SUBD_DI;
1139 s->subdev_flags = SDF_READABLE;
1142 s->range_table = &range_digital;
1143 s->insn_bits = das16_di_insn_bits;
1145 /* Digital Output subdevice */
1146 s = &dev->subdevices[3];
1147 s->type = COMEDI_SUBD_DO;
1148 s->subdev_flags = SDF_WRITABLE;
1151 s->range_table = &range_digital;
1152 s->insn_bits = das16_do_insn_bits;
1154 /* initialize digital output lines */
1155 outb(s->state, dev->iobase + DAS16_DIO_REG);
1157 /* 8255 Digital I/O subdevice */
1158 if (board->has_8255) {
1159 s = &dev->subdevices[4];
1160 ret = subdev_8255_init(dev, s, NULL, board->i8255_offset);
1166 /* set the interrupt level */
1167 devpriv->ctrl_reg = DAS16_CTRL_IRQ(dev->irq);
1168 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
1170 if (devpriv->can_burst) {
1171 outb(DAS1600_ENABLE_VAL, dev->iobase + DAS1600_ENABLE_REG);
1172 outb(0, dev->iobase + DAS1600_CONV_REG);
1173 outb(0, dev->iobase + DAS1600_BURST_REG);
1179 static void das16_detach(struct comedi_device *dev)
1181 const struct das16_board *board = dev->board_ptr;
1182 struct das16_private_struct *devpriv = dev->private;
1187 das16_free_dma(dev);
1189 if (devpriv->extra_iobase)
1190 release_region(devpriv->extra_iobase,
1191 board->size & 0x3ff);
1194 comedi_legacy_detach(dev);
1197 static struct comedi_driver das16_driver = {
1198 .driver_name = "das16",
1199 .module = THIS_MODULE,
1200 .attach = das16_attach,
1201 .detach = das16_detach,
1202 .board_name = &das16_boards[0].name,
1203 .num_names = ARRAY_SIZE(das16_boards),
1204 .offset = sizeof(das16_boards[0]),
1206 module_comedi_driver(das16_driver);
1208 MODULE_AUTHOR("Comedi http://www.comedi.org");
1209 MODULE_DESCRIPTION("Comedi driver for DAS16 compatible boards");
1210 MODULE_LICENSE("GPL");