2 comedi/drivers/ni_labpc.c
3 Driver for National Instruments Lab-PC series boards and compatibles
4 Copyright (C) 2001, 2002, 2003 Frank Mori Hess <fmhess@users.sourceforge.net>
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.
20 ************************************************************************
24 Description: National Instruments Lab-PC (& compatibles)
25 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
26 Devices: [National Instruments] Lab-PC-1200 (labpc-1200),
27 Lab-PC-1200AI (labpc-1200ai), Lab-PC+ (lab-pc+), PCI-1200 (ni_labpc)
30 Tested with lab-pc-1200. For the older Lab-PC+, not all input ranges
31 and analog references will work, the available ranges/arefs will
32 depend on how you have configured the jumpers on your board
33 (see your owner's manual).
35 Kernel-level ISA plug-and-play support for the lab-pc-1200
37 yet been added to the driver, mainly due to the fact that
38 I don't know the device id numbers. If you have one
40 please file a bug report at http://comedi.org/
41 so I can get the necessary information from you.
43 The 1200 series boards have onboard calibration dacs for correcting
44 analog input/output offsets and gains. The proper settings for these
45 caldacs are stored on the board's eeprom. To read the caldac values
46 from the eeprom and store them into a file that can be then be used by
47 comedilib, use the comedi_calibrate program.
49 Configuration options - ISA boards:
50 [0] - I/O port base address
51 [1] - IRQ (optional, required for timed or externally triggered conversions)
52 [2] - DMA channel (optional)
54 Configuration options - PCI boards:
58 The Lab-pc+ has quirky chanlist requirements
59 when scanning multiple channels. Multiple channel scan
60 sequence must start at highest channel, then decrement down to
61 channel 0. The rest of the cards can scan down like lab-pc+ or scan
62 up from channel zero. Chanlists consisting of all one channel
63 are also legal, and allow you to pace conversions in bursts.
70 341309a (labpc-1200 register manual)
76 #include <linux/pci.h>
77 #include <linux/interrupt.h>
78 #include <linux/slab.h>
80 #include <linux/delay.h>
82 #include "../comedidev.h"
89 #include "comedi_fc.h"
92 /* size of io region used by board */
94 /* 2 MHz master clock */
95 #define LABPC_TIMER_BASE 500
97 /* Registers for the lab-pc+ */
99 /* write-only registers */
100 #define COMMAND1_REG 0x0
101 #define ADC_GAIN_MASK (0x7 << 4)
102 #define ADC_CHAN_BITS(x) ((x) & 0x7)
103 /* enables multi channel scans */
104 #define ADC_SCAN_EN_BIT 0x80
105 #define COMMAND2_REG 0x1
106 /* enable pretriggering (used in conjunction with SWTRIG) */
107 #define PRETRIG_BIT 0x1
108 /* enable paced conversions on external trigger */
109 #define HWTRIG_BIT 0x2
110 /* enable paced conversions */
111 #define SWTRIG_BIT 0x4
112 /* use two cascaded counters for pacing */
113 #define CASCADE_BIT 0x8
114 #define DAC_PACED_BIT(channel) (0x40 << ((channel) & 0x1))
115 #define COMMAND3_REG 0x2
116 /* enable dma transfers */
117 #define DMA_EN_BIT 0x1
118 /* enable interrupts for 8255 */
119 #define DIO_INTR_EN_BIT 0x2
120 /* enable dma terminal count interrupt */
121 #define DMATC_INTR_EN_BIT 0x4
122 /* enable timer interrupt */
123 #define TIMER_INTR_EN_BIT 0x8
124 /* enable error interrupt */
125 #define ERR_INTR_EN_BIT 0x10
126 /* enable fifo not empty interrupt */
127 #define ADC_FNE_INTR_EN_BIT 0x20
128 #define ADC_CONVERT_REG 0x3
129 #define DAC_LSB_REG(channel) (0x4 + 2 * ((channel) & 0x1))
130 #define DAC_MSB_REG(channel) (0x5 + 2 * ((channel) & 0x1))
131 #define ADC_CLEAR_REG 0x8
132 #define DMATC_CLEAR_REG 0xa
133 #define TIMER_CLEAR_REG 0xc
134 /* 1200 boards only */
135 #define COMMAND6_REG 0xe
136 /* select ground or common-mode reference */
137 #define ADC_COMMON_BIT 0x1
139 #define ADC_UNIP_BIT 0x2
141 #define DAC_UNIP_BIT(channel) (0x4 << ((channel) & 0x1))
142 /* enable fifo half full interrupt */
143 #define ADC_FHF_INTR_EN_BIT 0x20
144 /* enable interrupt on end of hardware count */
145 #define A1_INTR_EN_BIT 0x40
146 /* scan up from channel zero instead of down to zero */
147 #define ADC_SCAN_UP_BIT 0x80
148 #define COMMAND4_REG 0xf
149 /* enables 'interval' scanning */
150 #define INTERVAL_SCAN_EN_BIT 0x1
151 /* enables external signal on counter b1 output to trigger scan */
152 #define EXT_SCAN_EN_BIT 0x2
153 /* chooses direction (output or input) for EXTCONV* line */
154 #define EXT_CONVERT_OUT_BIT 0x4
155 /* chooses differential inputs for adc (in conjunction with board jumper) */
156 #define ADC_DIFF_BIT 0x8
157 #define EXT_CONVERT_DISABLE_BIT 0x10
158 /* 1200 boards only, calibration stuff */
159 #define COMMAND5_REG 0x1c
160 /* enable eeprom for write */
161 #define EEPROM_WRITE_UNPROTECT_BIT 0x4
162 /* enable dithering */
163 #define DITHER_EN_BIT 0x8
164 /* load calibration dac */
165 #define CALDAC_LOAD_BIT 0x10
166 /* serial clock - rising edge writes, falling edge reads */
167 #define SCLOCK_BIT 0x20
168 /* serial data bit for writing to eeprom or calibration dacs */
169 #define SDATA_BIT 0x40
170 /* enable eeprom for read/write */
171 #define EEPROM_EN_BIT 0x80
172 #define INTERVAL_COUNT_REG 0x1e
173 #define INTERVAL_LOAD_REG 0x1f
174 #define INTERVAL_LOAD_BITS 0x1
176 /* read-only registers */
177 #define STATUS1_REG 0x0
178 /* data is available in fifo */
179 #define DATA_AVAIL_BIT 0x1
180 /* overrun has occurred */
181 #define OVERRUN_BIT 0x2
183 #define OVERFLOW_BIT 0x4
184 /* timer interrupt has occurred */
185 #define TIMER_BIT 0x8
186 /* dma terminal count has occurred */
187 #define DMATC_BIT 0x10
188 /* external trigger has occurred */
189 #define EXT_TRIG_BIT 0x40
190 /* 1200 boards only */
191 #define STATUS2_REG 0x1d
192 /* programmable eeprom serial output */
193 #define EEPROM_OUT_BIT 0x1
194 /* counter A1 terminal count */
195 #define A1_TC_BIT 0x2
196 /* fifo not half full */
198 #define ADC_FIFO_REG 0xa
200 #define DIO_BASE_REG 0x10
201 #define COUNTER_A_BASE_REG 0x14
202 #define COUNTER_A_CONTROL_REG (COUNTER_A_BASE_REG + 0x3)
203 /* check modes put conversion pacer output in harmless state (a0 mode 2) */
204 #define INIT_A0_BITS 0x14
205 /* put hardware conversion counter output in harmless state (a1 mode 0) */
206 #define INIT_A1_BITS 0x70
207 #define COUNTER_B_BASE_REG 0x18
211 MODE_SINGLE_CHAN_INTERVAL,
216 static const int labpc_plus_ai_gain_bits[] = {
217 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
218 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
221 static const struct comedi_lrange range_labpc_plus_ai = {
242 const int labpc_1200_ai_gain_bits[] = {
243 0x00, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
244 0x00, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
246 EXPORT_SYMBOL_GPL(labpc_1200_ai_gain_bits);
248 const struct comedi_lrange range_labpc_1200_ai = {
266 EXPORT_SYMBOL_GPL(range_labpc_1200_ai);
268 static const struct comedi_lrange range_labpc_ao = {
275 /* functions that do inb/outb and readb/writeb so we can use
276 * function pointers to decide which to use */
277 static inline unsigned int labpc_inb(unsigned long address)
282 static inline void labpc_outb(unsigned int byte, unsigned long address)
287 static inline unsigned int labpc_readb(unsigned long address)
289 return readb((void __iomem *)address);
292 static inline void labpc_writeb(unsigned int byte, unsigned long address)
294 writeb(byte, (void __iomem *)address);
297 static const struct labpc_boardinfo labpc_boards[] = {
299 .name = "lab-pc-1200",
301 .bustype = isa_bustype,
302 .register_layout = labpc_1200_layout,
304 .ai_range_table = &range_labpc_1200_ai,
305 .ai_range_code = labpc_1200_ai_gain_bits,
308 .name = "lab-pc-1200ai",
310 .bustype = isa_bustype,
311 .register_layout = labpc_1200_layout,
312 .ai_range_table = &range_labpc_1200_ai,
313 .ai_range_code = labpc_1200_ai_gain_bits,
318 .bustype = isa_bustype,
319 .register_layout = labpc_plus_layout,
321 .ai_range_table = &range_labpc_plus_ai,
322 .ai_range_code = labpc_plus_ai_gain_bits,
324 #ifdef CONFIG_COMEDI_PCI_DRIVERS
329 .bustype = pci_bustype,
330 .register_layout = labpc_1200_layout,
332 .ai_range_table = &range_labpc_1200_ai,
333 .ai_range_code = labpc_1200_ai_gain_bits,
340 /* size in bytes of dma buffer */
341 static const int dma_buffer_size = 0xff00;
342 /* 2 bytes per sample */
343 static const int sample_size = 2;
345 static bool labpc_range_is_unipolar(struct comedi_subdevice *s,
348 const struct comedi_lrange *lrange = s->range_table;
349 const struct comedi_krange *krange = &lrange->range[range];
357 static void labpc_clear_adc_fifo(const struct comedi_device *dev)
359 struct labpc_private *devpriv = dev->private;
361 devpriv->write_byte(0x1, dev->iobase + ADC_CLEAR_REG);
362 devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
363 devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
366 static int labpc_ai_insn_read(struct comedi_device *dev,
367 struct comedi_subdevice *s,
368 struct comedi_insn *insn,
371 const struct labpc_boardinfo *board = comedi_board(dev);
372 struct labpc_private *devpriv = dev->private;
379 /* disable timed conversions */
380 spin_lock_irqsave(&dev->spinlock, flags);
381 devpriv->cmd2 &= ~SWTRIG_BIT & ~HWTRIG_BIT & ~PRETRIG_BIT;
382 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
383 spin_unlock_irqrestore(&dev->spinlock, flags);
385 /* disable interrupt generation and dma */
387 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
389 /* set gain and channel */
391 chan = CR_CHAN(insn->chanspec);
392 range = CR_RANGE(insn->chanspec);
393 devpriv->cmd1 |= board->ai_range_code[range];
394 /* munge channel bits for differential/scan disabled mode */
395 if (CR_AREF(insn->chanspec) == AREF_DIFF)
397 devpriv->cmd1 |= ADC_CHAN_BITS(chan);
398 devpriv->write_byte(devpriv->cmd1, dev->iobase + COMMAND1_REG);
400 /* setup cmd6 register for 1200 boards */
401 if (board->register_layout == labpc_1200_layout) {
402 /* reference inputs to ground or common? */
403 if (CR_AREF(insn->chanspec) != AREF_GROUND)
404 devpriv->cmd6 |= ADC_COMMON_BIT;
406 devpriv->cmd6 &= ~ADC_COMMON_BIT;
407 /* bipolar or unipolar range? */
408 if (labpc_range_is_unipolar(s, range))
409 devpriv->cmd6 |= ADC_UNIP_BIT;
411 devpriv->cmd6 &= ~ADC_UNIP_BIT;
412 /* don't interrupt on fifo half full */
413 devpriv->cmd6 &= ~ADC_FHF_INTR_EN_BIT;
414 /* don't enable interrupt on counter a1 terminal count? */
415 devpriv->cmd6 &= ~A1_INTR_EN_BIT;
416 /* write to register */
417 devpriv->write_byte(devpriv->cmd6, dev->iobase + COMMAND6_REG);
419 /* setup cmd4 register */
421 devpriv->cmd4 |= EXT_CONVERT_DISABLE_BIT;
422 /* single-ended/differential */
423 if (CR_AREF(insn->chanspec) == AREF_DIFF)
424 devpriv->cmd4 |= ADC_DIFF_BIT;
425 devpriv->write_byte(devpriv->cmd4, dev->iobase + COMMAND4_REG);
428 * initialize pacer counter output to make sure it doesn't
431 devpriv->write_byte(INIT_A0_BITS, dev->iobase + COUNTER_A_CONTROL_REG);
433 labpc_clear_adc_fifo(dev);
435 for (n = 0; n < insn->n; n++) {
436 /* trigger conversion */
437 devpriv->write_byte(0x1, dev->iobase + ADC_CONVERT_REG);
439 for (i = 0; i < timeout; i++) {
440 if (devpriv->read_byte(dev->iobase +
441 STATUS1_REG) & DATA_AVAIL_BIT)
446 comedi_error(dev, "timeout");
449 lsb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
450 msb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
451 data[n] = (msb << 8) | lsb;
457 #ifdef CONFIG_ISA_DMA_API
458 /* utility function that suggests a dma transfer size in bytes */
459 static unsigned int labpc_suggest_transfer_size(const struct comedi_cmd *cmd)
464 if (cmd->convert_src == TRIG_TIMER)
465 freq = 1000000000 / cmd->convert_arg;
466 /* return some default value */
470 /* make buffer fill in no more than 1/3 second */
471 size = (freq / 3) * sample_size;
473 /* set a minimum and maximum size allowed */
474 if (size > dma_buffer_size)
475 size = dma_buffer_size - dma_buffer_size % sample_size;
476 else if (size < sample_size)
483 static int labpc_use_continuous_mode(const struct comedi_cmd *cmd,
486 if (mode == MODE_SINGLE_CHAN)
489 if (cmd->scan_begin_src == TRIG_FOLLOW)
495 static unsigned int labpc_ai_convert_period(const struct comedi_cmd *cmd,
498 if (cmd->convert_src != TRIG_TIMER)
501 if (mode == MODE_SINGLE_CHAN && cmd->scan_begin_src == TRIG_TIMER)
502 return cmd->scan_begin_arg;
504 return cmd->convert_arg;
507 static void labpc_set_ai_convert_period(struct comedi_cmd *cmd,
508 enum scan_mode mode, unsigned int ns)
510 if (cmd->convert_src != TRIG_TIMER)
513 if (mode == MODE_SINGLE_CHAN &&
514 cmd->scan_begin_src == TRIG_TIMER) {
515 cmd->scan_begin_arg = ns;
516 if (cmd->convert_arg > cmd->scan_begin_arg)
517 cmd->convert_arg = cmd->scan_begin_arg;
519 cmd->convert_arg = ns;
522 static unsigned int labpc_ai_scan_period(const struct comedi_cmd *cmd,
525 if (cmd->scan_begin_src != TRIG_TIMER)
528 if (mode == MODE_SINGLE_CHAN && cmd->convert_src == TRIG_TIMER)
531 return cmd->scan_begin_arg;
534 static void labpc_set_ai_scan_period(struct comedi_cmd *cmd,
535 enum scan_mode mode, unsigned int ns)
537 if (cmd->scan_begin_src != TRIG_TIMER)
540 if (mode == MODE_SINGLE_CHAN && cmd->convert_src == TRIG_TIMER)
543 cmd->scan_begin_arg = ns;
546 /* figures out what counter values to use based on command */
547 static void labpc_adc_timing(struct comedi_device *dev, struct comedi_cmd *cmd,
550 struct labpc_private *devpriv = dev->private;
551 /* max value for 16 bit counter in mode 2 */
552 const int max_counter_value = 0x10000;
553 /* min value for 16 bit counter in mode 2 */
554 const int min_counter_value = 2;
555 unsigned int base_period;
556 unsigned int scan_period;
557 unsigned int convert_period;
560 * if both convert and scan triggers are TRIG_TIMER, then they
561 * both rely on counter b0
563 convert_period = labpc_ai_convert_period(cmd, mode);
564 scan_period = labpc_ai_scan_period(cmd, mode);
565 if (convert_period && scan_period) {
567 * pick the lowest b0 divisor value we can (for maximum input
568 * clock speed on convert and scan counters)
570 devpriv->divisor_b0 = (scan_period - 1) /
571 (LABPC_TIMER_BASE * max_counter_value) + 1;
572 if (devpriv->divisor_b0 < min_counter_value)
573 devpriv->divisor_b0 = min_counter_value;
574 if (devpriv->divisor_b0 > max_counter_value)
575 devpriv->divisor_b0 = max_counter_value;
577 base_period = LABPC_TIMER_BASE * devpriv->divisor_b0;
579 /* set a0 for conversion frequency and b1 for scan frequency */
580 switch (cmd->flags & TRIG_ROUND_MASK) {
582 case TRIG_ROUND_NEAREST:
583 devpriv->divisor_a0 =
584 (convert_period + (base_period / 2)) / base_period;
585 devpriv->divisor_b1 =
586 (scan_period + (base_period / 2)) / base_period;
589 devpriv->divisor_a0 =
590 (convert_period + (base_period - 1)) / base_period;
591 devpriv->divisor_b1 =
592 (scan_period + (base_period - 1)) / base_period;
594 case TRIG_ROUND_DOWN:
595 devpriv->divisor_a0 = convert_period / base_period;
596 devpriv->divisor_b1 = scan_period / base_period;
599 /* make sure a0 and b1 values are acceptable */
600 if (devpriv->divisor_a0 < min_counter_value)
601 devpriv->divisor_a0 = min_counter_value;
602 if (devpriv->divisor_a0 > max_counter_value)
603 devpriv->divisor_a0 = max_counter_value;
604 if (devpriv->divisor_b1 < min_counter_value)
605 devpriv->divisor_b1 = min_counter_value;
606 if (devpriv->divisor_b1 > max_counter_value)
607 devpriv->divisor_b1 = max_counter_value;
608 /* write corrected timings to command */
609 labpc_set_ai_convert_period(cmd, mode,
610 base_period * devpriv->divisor_a0);
611 labpc_set_ai_scan_period(cmd, mode,
612 base_period * devpriv->divisor_b1);
614 * if only one TRIG_TIMER is used, we can employ the generic
615 * cascaded timing functions
617 } else if (scan_period) {
619 * calculate cascaded counter values
620 * that give desired scan timing
622 i8253_cascade_ns_to_timer_2div(LABPC_TIMER_BASE,
623 &(devpriv->divisor_b1),
624 &(devpriv->divisor_b0),
626 cmd->flags & TRIG_ROUND_MASK);
627 labpc_set_ai_scan_period(cmd, mode, scan_period);
628 } else if (convert_period) {
630 * calculate cascaded counter values
631 * that give desired conversion timing
633 i8253_cascade_ns_to_timer_2div(LABPC_TIMER_BASE,
634 &(devpriv->divisor_a0),
635 &(devpriv->divisor_b0),
637 cmd->flags & TRIG_ROUND_MASK);
638 labpc_set_ai_convert_period(cmd, mode, convert_period);
642 static enum scan_mode labpc_ai_scan_mode(const struct comedi_cmd *cmd)
644 if (cmd->chanlist_len == 1)
645 return MODE_SINGLE_CHAN;
647 /* chanlist may be NULL during cmdtest. */
648 if (cmd->chanlist == NULL)
649 return MODE_MULT_CHAN_UP;
651 if (CR_CHAN(cmd->chanlist[0]) == CR_CHAN(cmd->chanlist[1]))
652 return MODE_SINGLE_CHAN_INTERVAL;
654 if (CR_CHAN(cmd->chanlist[0]) < CR_CHAN(cmd->chanlist[1]))
655 return MODE_MULT_CHAN_UP;
657 if (CR_CHAN(cmd->chanlist[0]) > CR_CHAN(cmd->chanlist[1]))
658 return MODE_MULT_CHAN_DOWN;
660 pr_err("ni_labpc: bug! cannot determine AI scan mode\n");
664 static int labpc_ai_chanlist_invalid(const struct comedi_device *dev,
665 const struct comedi_cmd *cmd,
668 int channel, range, aref, i;
670 if (cmd->chanlist == NULL)
673 if (mode == MODE_SINGLE_CHAN)
676 if (mode == MODE_SINGLE_CHAN_INTERVAL) {
677 if (cmd->chanlist_len > 0xff) {
679 "ni_labpc: chanlist too long for single channel interval mode\n");
684 channel = CR_CHAN(cmd->chanlist[0]);
685 range = CR_RANGE(cmd->chanlist[0]);
686 aref = CR_AREF(cmd->chanlist[0]);
688 for (i = 0; i < cmd->chanlist_len; i++) {
691 case MODE_SINGLE_CHAN_INTERVAL:
692 if (CR_CHAN(cmd->chanlist[i]) != channel) {
694 "channel scanning order specified in chanlist is not supported by hardware.\n");
698 case MODE_MULT_CHAN_UP:
699 if (CR_CHAN(cmd->chanlist[i]) != i) {
701 "channel scanning order specified in chanlist is not supported by hardware.\n");
705 case MODE_MULT_CHAN_DOWN:
706 if (CR_CHAN(cmd->chanlist[i]) !=
707 cmd->chanlist_len - i - 1) {
709 "channel scanning order specified in chanlist is not supported by hardware.\n");
714 dev_err(dev->class_dev,
715 "ni_labpc: bug! in chanlist check\n");
720 if (CR_RANGE(cmd->chanlist[i]) != range) {
722 "entries in chanlist must all have the same range\n");
726 if (CR_AREF(cmd->chanlist[i]) != aref) {
728 "entries in chanlist must all have the same reference\n");
736 static int labpc_ai_cmdtest(struct comedi_device *dev,
737 struct comedi_subdevice *s, struct comedi_cmd *cmd)
739 const struct labpc_boardinfo *board = comedi_board(dev);
742 unsigned int stop_mask;
745 /* Step 1 : check if triggers are trivially valid */
747 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
748 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
749 TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT);
750 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
751 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
753 stop_mask = TRIG_COUNT | TRIG_NONE;
754 if (board->register_layout == labpc_1200_layout)
755 stop_mask |= TRIG_EXT;
756 err |= cfc_check_trigger_src(&cmd->stop_src, stop_mask);
761 /* Step 2a : make sure trigger sources are unique */
763 err |= cfc_check_trigger_is_unique(cmd->start_src);
764 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
765 err |= cfc_check_trigger_is_unique(cmd->convert_src);
766 err |= cfc_check_trigger_is_unique(cmd->stop_src);
768 /* Step 2b : and mutually compatible */
770 /* can't have external stop and start triggers at once */
771 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
777 /* Step 3: check if arguments are trivially valid */
779 if (cmd->start_arg == TRIG_NOW)
780 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
782 if (!cmd->chanlist_len)
784 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
786 if (cmd->convert_src == TRIG_TIMER)
787 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
790 /* make sure scan timing is not too fast */
791 if (cmd->scan_begin_src == TRIG_TIMER) {
792 if (cmd->convert_src == TRIG_TIMER)
793 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
794 cmd->convert_arg * cmd->chanlist_len);
795 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
796 board->ai_speed * cmd->chanlist_len);
799 switch (cmd->stop_src) {
801 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
804 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
807 * TRIG_EXT doesn't care since it doesn't
808 * trigger off a numbered channel
817 /* step 4: fix up any arguments */
819 tmp = cmd->convert_arg;
820 tmp2 = cmd->scan_begin_arg;
821 mode = labpc_ai_scan_mode(cmd);
822 labpc_adc_timing(dev, cmd, mode);
823 if (tmp != cmd->convert_arg || tmp2 != cmd->scan_begin_arg)
829 if (labpc_ai_chanlist_invalid(dev, cmd, mode))
835 static inline int labpc_counter_load(struct comedi_device *dev,
836 unsigned long base_address,
837 unsigned int counter_number,
838 unsigned int count, unsigned int mode)
840 const struct labpc_boardinfo *board = comedi_board(dev);
843 return i8254_mm_load((void __iomem *)base_address, 0,
844 counter_number, count, mode);
846 return i8254_load(base_address, 0, counter_number, count, mode);
849 static int labpc_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
851 const struct labpc_boardinfo *board = comedi_board(dev);
852 struct labpc_private *devpriv = dev->private;
853 int channel, range, aref;
855 struct comedi_async *async = s->async;
856 struct comedi_cmd *cmd = &async->cmd;
857 enum transfer_type xfer;
862 comedi_error(dev, "no irq assigned, cannot perform command");
866 range = CR_RANGE(cmd->chanlist[0]);
867 aref = CR_AREF(cmd->chanlist[0]);
869 /* make sure board is disabled before setting up acquisition */
870 spin_lock_irqsave(&dev->spinlock, flags);
871 devpriv->cmd2 &= ~SWTRIG_BIT & ~HWTRIG_BIT & ~PRETRIG_BIT;
872 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
873 spin_unlock_irqrestore(&dev->spinlock, flags);
876 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
878 /* initialize software conversion count */
879 if (cmd->stop_src == TRIG_COUNT)
880 devpriv->count = cmd->stop_arg * cmd->chanlist_len;
882 /* setup hardware conversion counter */
883 if (cmd->stop_src == TRIG_EXT) {
885 * load counter a1 with count of 3
886 * (pc+ manual says this is minimum allowed) using mode 0
888 ret = labpc_counter_load(dev, dev->iobase + COUNTER_A_BASE_REG,
891 comedi_error(dev, "error loading counter a1");
895 * otherwise, just put a1 in mode 0
896 * with no count to set its output low
898 devpriv->write_byte(INIT_A1_BITS,
899 dev->iobase + COUNTER_A_CONTROL_REG);
901 #ifdef CONFIG_ISA_DMA_API
902 /* figure out what method we will use to transfer data */
903 if (devpriv->dma_chan && /* need a dma channel allocated */
905 * dma unsafe at RT priority,
906 * and too much setup time for TRIG_WAKE_EOS for
908 (cmd->flags & (TRIG_WAKE_EOS | TRIG_RT)) == 0 &&
909 /* only available on the isa boards */
910 board->bustype == isa_bustype) {
911 xfer = isa_dma_transfer;
912 /* pc-plus has no fifo-half full interrupt */
915 if (board->register_layout == labpc_1200_layout &&
916 /* wake-end-of-scan should interrupt on fifo not empty */
917 (cmd->flags & TRIG_WAKE_EOS) == 0 &&
918 /* make sure we are taking more than just a few points */
919 (cmd->stop_src != TRIG_COUNT || devpriv->count > 256)) {
920 xfer = fifo_half_full_transfer;
922 xfer = fifo_not_empty_transfer;
923 devpriv->current_transfer = xfer;
924 mode = labpc_ai_scan_mode(cmd);
926 /* setup cmd6 register for 1200 boards */
927 if (board->register_layout == labpc_1200_layout) {
928 /* reference inputs to ground or common? */
929 if (aref != AREF_GROUND)
930 devpriv->cmd6 |= ADC_COMMON_BIT;
932 devpriv->cmd6 &= ~ADC_COMMON_BIT;
933 /* bipolar or unipolar range? */
934 if (labpc_range_is_unipolar(s, range))
935 devpriv->cmd6 |= ADC_UNIP_BIT;
937 devpriv->cmd6 &= ~ADC_UNIP_BIT;
938 /* interrupt on fifo half full? */
939 if (xfer == fifo_half_full_transfer)
940 devpriv->cmd6 |= ADC_FHF_INTR_EN_BIT;
942 devpriv->cmd6 &= ~ADC_FHF_INTR_EN_BIT;
943 /* enable interrupt on counter a1 terminal count? */
944 if (cmd->stop_src == TRIG_EXT)
945 devpriv->cmd6 |= A1_INTR_EN_BIT;
947 devpriv->cmd6 &= ~A1_INTR_EN_BIT;
948 /* are we scanning up or down through channels? */
949 if (mode == MODE_MULT_CHAN_UP)
950 devpriv->cmd6 |= ADC_SCAN_UP_BIT;
952 devpriv->cmd6 &= ~ADC_SCAN_UP_BIT;
953 /* write to register */
954 devpriv->write_byte(devpriv->cmd6, dev->iobase + COMMAND6_REG);
957 /* setup channel list, etc (cmd1 register) */
959 if (mode == MODE_MULT_CHAN_UP)
960 channel = CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]);
962 channel = CR_CHAN(cmd->chanlist[0]);
963 /* munge channel bits for differential / scan disabled mode */
964 if ((mode == MODE_SINGLE_CHAN || mode == MODE_SINGLE_CHAN_INTERVAL) &&
967 devpriv->cmd1 |= ADC_CHAN_BITS(channel);
968 devpriv->cmd1 |= board->ai_range_code[range];
969 devpriv->write_byte(devpriv->cmd1, dev->iobase + COMMAND1_REG);
970 /* manual says to set scan enable bit on second pass */
971 if (mode == MODE_MULT_CHAN_UP || mode == MODE_MULT_CHAN_DOWN) {
972 devpriv->cmd1 |= ADC_SCAN_EN_BIT;
973 /* need a brief delay before enabling scan, or scan
974 * list will get screwed when you switch
975 * between scan up to scan down mode - dunno why */
977 devpriv->write_byte(devpriv->cmd1, dev->iobase + COMMAND1_REG);
980 devpriv->write_byte(cmd->chanlist_len,
981 dev->iobase + INTERVAL_COUNT_REG);
983 devpriv->write_byte(INTERVAL_LOAD_BITS,
984 dev->iobase + INTERVAL_LOAD_REG);
986 if (cmd->convert_src == TRIG_TIMER || cmd->scan_begin_src == TRIG_TIMER) {
988 labpc_adc_timing(dev, cmd, mode);
989 /* load counter b0 in mode 3 */
990 ret = labpc_counter_load(dev, dev->iobase + COUNTER_B_BASE_REG,
991 0, devpriv->divisor_b0, 3);
993 comedi_error(dev, "error loading counter b0");
997 /* set up conversion pacing */
998 if (labpc_ai_convert_period(cmd, mode)) {
999 /* load counter a0 in mode 2 */
1000 ret = labpc_counter_load(dev, dev->iobase + COUNTER_A_BASE_REG,
1001 0, devpriv->divisor_a0, 2);
1003 comedi_error(dev, "error loading counter a0");
1007 devpriv->write_byte(INIT_A0_BITS,
1008 dev->iobase + COUNTER_A_CONTROL_REG);
1010 /* set up scan pacing */
1011 if (labpc_ai_scan_period(cmd, mode)) {
1012 /* load counter b1 in mode 2 */
1013 ret = labpc_counter_load(dev, dev->iobase + COUNTER_B_BASE_REG,
1014 1, devpriv->divisor_b1, 2);
1016 comedi_error(dev, "error loading counter b1");
1021 labpc_clear_adc_fifo(dev);
1023 #ifdef CONFIG_ISA_DMA_API
1024 /* set up dma transfer */
1025 if (xfer == isa_dma_transfer) {
1026 unsigned long irq_flags;
1028 irq_flags = claim_dma_lock();
1029 disable_dma(devpriv->dma_chan);
1030 /* clear flip-flop to make sure 2-byte registers for
1031 * count and address get set correctly */
1032 clear_dma_ff(devpriv->dma_chan);
1033 set_dma_addr(devpriv->dma_chan,
1034 virt_to_bus(devpriv->dma_buffer));
1035 /* set appropriate size of transfer */
1036 devpriv->dma_transfer_size = labpc_suggest_transfer_size(cmd);
1037 if (cmd->stop_src == TRIG_COUNT &&
1038 devpriv->count * sample_size < devpriv->dma_transfer_size) {
1039 devpriv->dma_transfer_size =
1040 devpriv->count * sample_size;
1042 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
1043 enable_dma(devpriv->dma_chan);
1044 release_dma_lock(irq_flags);
1045 /* enable board's dma */
1046 devpriv->cmd3 |= DMA_EN_BIT | DMATC_INTR_EN_BIT;
1048 devpriv->cmd3 &= ~DMA_EN_BIT & ~DMATC_INTR_EN_BIT;
1051 /* enable error interrupts */
1052 devpriv->cmd3 |= ERR_INTR_EN_BIT;
1053 /* enable fifo not empty interrupt? */
1054 if (xfer == fifo_not_empty_transfer)
1055 devpriv->cmd3 |= ADC_FNE_INTR_EN_BIT;
1057 devpriv->cmd3 &= ~ADC_FNE_INTR_EN_BIT;
1058 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
1060 /* setup any external triggering/pacing (cmd4 register) */
1062 if (cmd->convert_src != TRIG_EXT)
1063 devpriv->cmd4 |= EXT_CONVERT_DISABLE_BIT;
1064 /* XXX should discard first scan when using interval scanning
1065 * since manual says it is not synced with scan clock */
1066 if (labpc_use_continuous_mode(cmd, mode) == 0) {
1067 devpriv->cmd4 |= INTERVAL_SCAN_EN_BIT;
1068 if (cmd->scan_begin_src == TRIG_EXT)
1069 devpriv->cmd4 |= EXT_SCAN_EN_BIT;
1071 /* single-ended/differential */
1072 if (aref == AREF_DIFF)
1073 devpriv->cmd4 |= ADC_DIFF_BIT;
1074 devpriv->write_byte(devpriv->cmd4, dev->iobase + COMMAND4_REG);
1076 /* startup acquisition */
1079 /* use 2 cascaded counters for pacing */
1080 spin_lock_irqsave(&dev->spinlock, flags);
1081 devpriv->cmd2 |= CASCADE_BIT;
1082 switch (cmd->start_src) {
1084 devpriv->cmd2 |= HWTRIG_BIT;
1085 devpriv->cmd2 &= ~PRETRIG_BIT & ~SWTRIG_BIT;
1088 devpriv->cmd2 |= SWTRIG_BIT;
1089 devpriv->cmd2 &= ~PRETRIG_BIT & ~HWTRIG_BIT;
1092 comedi_error(dev, "bug with start_src");
1093 spin_unlock_irqrestore(&dev->spinlock, flags);
1097 switch (cmd->stop_src) {
1099 devpriv->cmd2 |= HWTRIG_BIT | PRETRIG_BIT;
1105 comedi_error(dev, "bug with stop_src");
1106 spin_unlock_irqrestore(&dev->spinlock, flags);
1109 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
1110 spin_unlock_irqrestore(&dev->spinlock, flags);
1115 static int labpc_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1117 struct labpc_private *devpriv = dev->private;
1118 unsigned long flags;
1120 spin_lock_irqsave(&dev->spinlock, flags);
1121 devpriv->cmd2 &= ~SWTRIG_BIT & ~HWTRIG_BIT & ~PRETRIG_BIT;
1122 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
1123 spin_unlock_irqrestore(&dev->spinlock, flags);
1126 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
1131 #ifdef CONFIG_ISA_DMA_API
1132 static void labpc_drain_dma(struct comedi_device *dev)
1134 struct labpc_private *devpriv = dev->private;
1135 struct comedi_subdevice *s = dev->read_subdev;
1136 struct comedi_async *async = s->async;
1138 unsigned long flags;
1139 unsigned int max_points, num_points, residue, leftover;
1142 status = devpriv->stat1;
1144 flags = claim_dma_lock();
1145 disable_dma(devpriv->dma_chan);
1146 /* clear flip-flop to make sure 2-byte registers for
1147 * count and address get set correctly */
1148 clear_dma_ff(devpriv->dma_chan);
1150 /* figure out how many points to read */
1151 max_points = devpriv->dma_transfer_size / sample_size;
1152 /* residue is the number of points left to be done on the dma
1153 * transfer. It should always be zero at this point unless
1154 * the stop_src is set to external triggering.
1156 residue = get_dma_residue(devpriv->dma_chan) / sample_size;
1157 num_points = max_points - residue;
1158 if (devpriv->count < num_points && async->cmd.stop_src == TRIG_COUNT)
1159 num_points = devpriv->count;
1161 /* figure out how many points will be stored next time */
1163 if (async->cmd.stop_src != TRIG_COUNT) {
1164 leftover = devpriv->dma_transfer_size / sample_size;
1165 } else if (devpriv->count > num_points) {
1166 leftover = devpriv->count - num_points;
1167 if (leftover > max_points)
1168 leftover = max_points;
1171 /* write data to comedi buffer */
1172 for (i = 0; i < num_points; i++)
1173 cfc_write_to_buffer(s, devpriv->dma_buffer[i]);
1175 if (async->cmd.stop_src == TRIG_COUNT)
1176 devpriv->count -= num_points;
1178 /* set address and count for next transfer */
1179 set_dma_addr(devpriv->dma_chan, virt_to_bus(devpriv->dma_buffer));
1180 set_dma_count(devpriv->dma_chan, leftover * sample_size);
1181 release_dma_lock(flags);
1183 async->events |= COMEDI_CB_BLOCK;
1186 static void handle_isa_dma(struct comedi_device *dev)
1188 struct labpc_private *devpriv = dev->private;
1190 labpc_drain_dma(dev);
1192 enable_dma(devpriv->dma_chan);
1194 /* clear dma tc interrupt */
1195 devpriv->write_byte(0x1, dev->iobase + DMATC_CLEAR_REG);
1199 /* read all available samples from ai fifo */
1200 static int labpc_drain_fifo(struct comedi_device *dev)
1202 struct labpc_private *devpriv = dev->private;
1203 unsigned int lsb, msb;
1205 struct comedi_async *async = dev->read_subdev->async;
1206 const int timeout = 10000;
1209 devpriv->stat1 = devpriv->read_byte(dev->iobase + STATUS1_REG);
1211 for (i = 0; (devpriv->stat1 & DATA_AVAIL_BIT) && i < timeout;
1213 /* quit if we have all the data we want */
1214 if (async->cmd.stop_src == TRIG_COUNT) {
1215 if (devpriv->count == 0)
1219 lsb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
1220 msb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
1221 data = (msb << 8) | lsb;
1222 cfc_write_to_buffer(dev->read_subdev, data);
1223 devpriv->stat1 = devpriv->read_byte(dev->iobase + STATUS1_REG);
1226 comedi_error(dev, "ai timeout, fifo never empties");
1227 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1234 /* makes sure all data acquired by board is transferred to comedi (used
1235 * when acquisition is terminated by stop_src == TRIG_EXT). */
1236 static void labpc_drain_dregs(struct comedi_device *dev)
1238 #ifdef CONFIG_ISA_DMA_API
1239 struct labpc_private *devpriv = dev->private;
1241 if (devpriv->current_transfer == isa_dma_transfer)
1242 labpc_drain_dma(dev);
1245 labpc_drain_fifo(dev);
1248 /* interrupt service routine */
1249 static irqreturn_t labpc_interrupt(int irq, void *d)
1251 struct comedi_device *dev = d;
1252 const struct labpc_boardinfo *board = comedi_board(dev);
1253 struct labpc_private *devpriv = dev->private;
1254 struct comedi_subdevice *s = dev->read_subdev;
1255 struct comedi_async *async;
1256 struct comedi_cmd *cmd;
1258 if (!dev->attached) {
1259 comedi_error(dev, "premature interrupt");
1267 /* read board status */
1268 devpriv->stat1 = devpriv->read_byte(dev->iobase + STATUS1_REG);
1269 if (board->register_layout == labpc_1200_layout)
1270 devpriv->stat2 = devpriv->read_byte(dev->iobase + STATUS2_REG);
1272 if ((devpriv->stat1 & (DMATC_BIT | TIMER_BIT | OVERFLOW_BIT |
1273 OVERRUN_BIT | DATA_AVAIL_BIT)) == 0
1274 && (devpriv->stat2 & A1_TC_BIT) == 0
1275 && (devpriv->stat2 & FNHF_BIT)) {
1279 if (devpriv->stat1 & OVERRUN_BIT) {
1280 /* clear error interrupt */
1281 devpriv->write_byte(0x1, dev->iobase + ADC_CLEAR_REG);
1282 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1283 comedi_event(dev, s);
1284 comedi_error(dev, "overrun");
1288 #ifdef CONFIG_ISA_DMA_API
1289 if (devpriv->current_transfer == isa_dma_transfer) {
1291 * if a dma terminal count of external stop trigger
1294 if (devpriv->stat1 & DMATC_BIT ||
1295 (board->register_layout == labpc_1200_layout
1296 && devpriv->stat2 & A1_TC_BIT)) {
1297 handle_isa_dma(dev);
1301 labpc_drain_fifo(dev);
1303 if (devpriv->stat1 & TIMER_BIT) {
1304 comedi_error(dev, "handled timer interrupt?");
1306 devpriv->write_byte(0x1, dev->iobase + TIMER_CLEAR_REG);
1309 if (devpriv->stat1 & OVERFLOW_BIT) {
1310 /* clear error interrupt */
1311 devpriv->write_byte(0x1, dev->iobase + ADC_CLEAR_REG);
1312 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1313 comedi_event(dev, s);
1314 comedi_error(dev, "overflow");
1317 /* handle external stop trigger */
1318 if (cmd->stop_src == TRIG_EXT) {
1319 if (devpriv->stat2 & A1_TC_BIT) {
1320 labpc_drain_dregs(dev);
1321 labpc_cancel(dev, s);
1322 async->events |= COMEDI_CB_EOA;
1326 /* TRIG_COUNT end of acquisition */
1327 if (cmd->stop_src == TRIG_COUNT) {
1328 if (devpriv->count == 0) {
1329 labpc_cancel(dev, s);
1330 async->events |= COMEDI_CB_EOA;
1334 comedi_event(dev, s);
1338 static int labpc_ao_insn_write(struct comedi_device *dev,
1339 struct comedi_subdevice *s,
1340 struct comedi_insn *insn,
1343 const struct labpc_boardinfo *board = comedi_board(dev);
1344 struct labpc_private *devpriv = dev->private;
1346 unsigned long flags;
1349 channel = CR_CHAN(insn->chanspec);
1351 /* turn off pacing of analog output channel */
1352 /* note: hardware bug in daqcard-1200 means pacing cannot
1353 * be independently enabled/disabled for its the two channels */
1354 spin_lock_irqsave(&dev->spinlock, flags);
1355 devpriv->cmd2 &= ~DAC_PACED_BIT(channel);
1356 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
1357 spin_unlock_irqrestore(&dev->spinlock, flags);
1360 if (board->register_layout == labpc_1200_layout) {
1361 range = CR_RANGE(insn->chanspec);
1362 if (labpc_range_is_unipolar(s, range))
1363 devpriv->cmd6 |= DAC_UNIP_BIT(channel);
1365 devpriv->cmd6 &= ~DAC_UNIP_BIT(channel);
1366 /* write to register */
1367 devpriv->write_byte(devpriv->cmd6, dev->iobase + COMMAND6_REG);
1370 lsb = data[0] & 0xff;
1371 msb = (data[0] >> 8) & 0xff;
1372 devpriv->write_byte(lsb, dev->iobase + DAC_LSB_REG(channel));
1373 devpriv->write_byte(msb, dev->iobase + DAC_MSB_REG(channel));
1375 /* remember value for readback */
1376 devpriv->ao_value[channel] = data[0];
1381 static int labpc_ao_insn_read(struct comedi_device *dev,
1382 struct comedi_subdevice *s,
1383 struct comedi_insn *insn,
1386 struct labpc_private *devpriv = dev->private;
1388 data[0] = devpriv->ao_value[CR_CHAN(insn->chanspec)];
1393 static int labpc_8255_mmio(int dir, int port, int data, unsigned long iobase)
1396 writeb(data, (void __iomem *)(iobase + port));
1399 return readb((void __iomem *)(iobase + port));
1403 /* lowlevel write to eeprom/dac */
1404 static void labpc_serial_out(struct comedi_device *dev, unsigned int value,
1405 unsigned int value_width)
1407 struct labpc_private *devpriv = dev->private;
1410 for (i = 1; i <= value_width; i++) {
1411 /* clear serial clock */
1412 devpriv->cmd5 &= ~SCLOCK_BIT;
1413 /* send bits most significant bit first */
1414 if (value & (1 << (value_width - i)))
1415 devpriv->cmd5 |= SDATA_BIT;
1417 devpriv->cmd5 &= ~SDATA_BIT;
1419 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1420 /* set clock to load bit */
1421 devpriv->cmd5 |= SCLOCK_BIT;
1423 devpriv->write_byte(devpriv->cmd5,
1424 dev->iobase + COMMAND5_REG);
1428 /* lowlevel read from eeprom */
1429 static unsigned int labpc_serial_in(struct comedi_device *dev)
1431 struct labpc_private *devpriv = dev->private;
1432 unsigned int value = 0;
1434 const int value_width = 8; /* number of bits wide values are */
1436 for (i = 1; i <= value_width; i++) {
1437 /* set serial clock */
1438 devpriv->cmd5 |= SCLOCK_BIT;
1440 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1441 /* clear clock bit */
1442 devpriv->cmd5 &= ~SCLOCK_BIT;
1444 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1445 /* read bits most significant bit first */
1447 devpriv->stat2 = devpriv->read_byte(dev->iobase + STATUS2_REG);
1448 if (devpriv->stat2 & EEPROM_OUT_BIT)
1449 value |= 1 << (value_width - i);
1455 static unsigned int labpc_eeprom_read(struct comedi_device *dev,
1456 unsigned int address)
1458 struct labpc_private *devpriv = dev->private;
1460 /* bits to tell eeprom to expect a read */
1461 const int read_instruction = 0x3;
1462 /* 8 bit write lengths to eeprom */
1463 const int write_length = 8;
1465 /* enable read/write to eeprom */
1466 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1468 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1469 devpriv->cmd5 |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
1471 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1473 /* send read instruction */
1474 labpc_serial_out(dev, read_instruction, write_length);
1475 /* send 8 bit address to read from */
1476 labpc_serial_out(dev, address, write_length);
1478 value = labpc_serial_in(dev);
1480 /* disable read/write to eeprom */
1481 devpriv->cmd5 &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1483 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1488 static unsigned int labpc_eeprom_read_status(struct comedi_device *dev)
1490 struct labpc_private *devpriv = dev->private;
1492 const int read_status_instruction = 0x5;
1493 const int write_length = 8; /* 8 bit write lengths to eeprom */
1495 /* enable read/write to eeprom */
1496 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1498 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1499 devpriv->cmd5 |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
1501 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1503 /* send read status instruction */
1504 labpc_serial_out(dev, read_status_instruction, write_length);
1506 value = labpc_serial_in(dev);
1508 /* disable read/write to eeprom */
1509 devpriv->cmd5 &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1511 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1516 static int labpc_eeprom_write(struct comedi_device *dev,
1517 unsigned int address, unsigned int value)
1519 struct labpc_private *devpriv = dev->private;
1520 const int write_enable_instruction = 0x6;
1521 const int write_instruction = 0x2;
1522 const int write_length = 8; /* 8 bit write lengths to eeprom */
1523 const int write_in_progress_bit = 0x1;
1524 const int timeout = 10000;
1527 /* make sure there isn't already a write in progress */
1528 for (i = 0; i < timeout; i++) {
1529 if ((labpc_eeprom_read_status(dev) & write_in_progress_bit) ==
1534 comedi_error(dev, "eeprom write timed out");
1537 /* update software copy of eeprom */
1538 devpriv->eeprom_data[address] = value;
1540 /* enable read/write to eeprom */
1541 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1543 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1544 devpriv->cmd5 |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
1546 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1548 /* send write_enable instruction */
1549 labpc_serial_out(dev, write_enable_instruction, write_length);
1550 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1552 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1554 /* send write instruction */
1555 devpriv->cmd5 |= EEPROM_EN_BIT;
1557 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1558 labpc_serial_out(dev, write_instruction, write_length);
1559 /* send 8 bit address to write to */
1560 labpc_serial_out(dev, address, write_length);
1562 labpc_serial_out(dev, value, write_length);
1563 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1565 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1567 /* disable read/write to eeprom */
1568 devpriv->cmd5 &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1570 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1575 /* writes to 8 bit calibration dacs */
1576 static void write_caldac(struct comedi_device *dev, unsigned int channel,
1579 struct labpc_private *devpriv = dev->private;
1581 if (value == devpriv->caldac[channel])
1583 devpriv->caldac[channel] = value;
1585 /* clear caldac load bit and make sure we don't write to eeprom */
1587 ~CALDAC_LOAD_BIT & ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1589 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1591 /* write 4 bit channel */
1592 labpc_serial_out(dev, channel, 4);
1593 /* write 8 bit caldac value */
1594 labpc_serial_out(dev, value, 8);
1596 /* set and clear caldac bit to load caldac value */
1597 devpriv->cmd5 |= CALDAC_LOAD_BIT;
1599 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1600 devpriv->cmd5 &= ~CALDAC_LOAD_BIT;
1602 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1605 static int labpc_calib_insn_write(struct comedi_device *dev,
1606 struct comedi_subdevice *s,
1607 struct comedi_insn *insn,
1610 int channel = CR_CHAN(insn->chanspec);
1612 write_caldac(dev, channel, data[0]);
1616 static int labpc_calib_insn_read(struct comedi_device *dev,
1617 struct comedi_subdevice *s,
1618 struct comedi_insn *insn,
1621 struct labpc_private *devpriv = dev->private;
1623 data[0] = devpriv->caldac[CR_CHAN(insn->chanspec)];
1628 static int labpc_eeprom_insn_write(struct comedi_device *dev,
1629 struct comedi_subdevice *s,
1630 struct comedi_insn *insn,
1633 int channel = CR_CHAN(insn->chanspec);
1636 /* only allow writes to user area of eeprom */
1637 if (channel < 16 || channel > 127) {
1638 dev_dbg(dev->class_dev,
1639 "eeprom writes are only allowed to channels 16 through 127 (the pointer and user areas)\n");
1643 ret = labpc_eeprom_write(dev, channel, data[0]);
1650 static int labpc_eeprom_insn_read(struct comedi_device *dev,
1651 struct comedi_subdevice *s,
1652 struct comedi_insn *insn,
1655 struct labpc_private *devpriv = dev->private;
1657 data[0] = devpriv->eeprom_data[CR_CHAN(insn->chanspec)];
1662 int labpc_common_attach(struct comedi_device *dev, unsigned long iobase,
1663 unsigned int irq, unsigned int dma_chan)
1665 const struct labpc_boardinfo *board = comedi_board(dev);
1666 struct labpc_private *devpriv = dev->private;
1667 struct comedi_subdevice *s;
1668 unsigned long isr_flags;
1672 dev->board_name = board->name;
1674 dev_info(dev->class_dev, "ni_labpc: %s\n", dev->board_name);
1676 dev_err(dev->class_dev, "io base address is zero!\n");
1679 /* request io regions for isa boards */
1680 if (board->bustype == isa_bustype) {
1681 /* check if io addresses are available */
1682 if (!request_region(iobase, LABPC_SIZE, dev->board_name)) {
1683 dev_err(dev->class_dev, "I/O port conflict\n");
1687 dev->iobase = iobase;
1689 if (board->has_mmio) {
1690 devpriv->read_byte = labpc_readb;
1691 devpriv->write_byte = labpc_writeb;
1693 devpriv->read_byte = labpc_inb;
1694 devpriv->write_byte = labpc_outb;
1696 /* initialize board's command registers */
1697 devpriv->write_byte(devpriv->cmd1, dev->iobase + COMMAND1_REG);
1698 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
1699 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
1700 devpriv->write_byte(devpriv->cmd4, dev->iobase + COMMAND4_REG);
1701 if (board->register_layout == labpc_1200_layout) {
1702 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1703 devpriv->write_byte(devpriv->cmd6, dev->iobase + COMMAND6_REG);
1709 if (board->bustype == pci_bustype ||
1710 board->bustype == pcmcia_bustype)
1711 isr_flags |= IRQF_SHARED;
1712 if (request_irq(irq, labpc_interrupt, isr_flags,
1713 dev->board_name, dev)) {
1714 dev_err(dev->class_dev, "unable to allocate irq %u\n",
1721 #ifdef CONFIG_ISA_DMA_API
1722 /* grab dma channel */
1724 dev_err(dev->class_dev, "invalid dma channel %u\n", dma_chan);
1726 } else if (dma_chan) {
1727 unsigned long dma_flags;
1729 /* allocate dma buffer */
1730 devpriv->dma_buffer = kmalloc(dma_buffer_size,
1731 GFP_KERNEL | GFP_DMA);
1732 if (devpriv->dma_buffer == NULL)
1735 if (request_dma(dma_chan, dev->board_name)) {
1736 dev_err(dev->class_dev,
1737 "failed to allocate dma channel %u\n",
1741 devpriv->dma_chan = dma_chan;
1742 dma_flags = claim_dma_lock();
1743 disable_dma(devpriv->dma_chan);
1744 set_dma_mode(devpriv->dma_chan, DMA_MODE_READ);
1745 release_dma_lock(dma_flags);
1749 ret = comedi_alloc_subdevices(dev, 5);
1753 /* analog input subdevice */
1754 s = &dev->subdevices[0];
1755 s->type = COMEDI_SUBD_AI;
1756 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_COMMON | SDF_DIFF;
1758 s->len_chanlist = 8;
1759 s->maxdata = 0x0fff;
1760 s->range_table = board->ai_range_table;
1761 s->insn_read = labpc_ai_insn_read;
1763 dev->read_subdev = s;
1764 s->subdev_flags |= SDF_CMD_READ;
1765 s->do_cmd = labpc_ai_cmd;
1766 s->do_cmdtest = labpc_ai_cmdtest;
1767 s->cancel = labpc_cancel;
1771 s = &dev->subdevices[1];
1772 if (board->has_ao) {
1773 s->type = COMEDI_SUBD_AO;
1774 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
1775 s->n_chan = NUM_AO_CHAN;
1776 s->maxdata = 0x0fff;
1777 s->range_table = &range_labpc_ao;
1778 s->insn_read = labpc_ao_insn_read;
1779 s->insn_write = labpc_ao_insn_write;
1781 /* initialize analog outputs to a known value */
1782 for (i = 0; i < s->n_chan; i++) {
1785 devpriv->ao_value[i] = s->maxdata / 2;
1786 lsb = devpriv->ao_value[i] & 0xff;
1787 msb = (devpriv->ao_value[i] >> 8) & 0xff;
1788 devpriv->write_byte(lsb, dev->iobase + DAC_LSB_REG(i));
1789 devpriv->write_byte(msb, dev->iobase + DAC_MSB_REG(i));
1792 s->type = COMEDI_SUBD_UNUSED;
1796 s = &dev->subdevices[2];
1797 ret = subdev_8255_init(dev, s,
1798 (board->has_mmio) ? labpc_8255_mmio : NULL,
1799 dev->iobase + DIO_BASE_REG);
1803 /* calibration subdevices for boards that have one */
1804 s = &dev->subdevices[3];
1805 if (board->register_layout == labpc_1200_layout) {
1806 s->type = COMEDI_SUBD_CALIB;
1807 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
1810 s->insn_read = labpc_calib_insn_read;
1811 s->insn_write = labpc_calib_insn_write;
1813 for (i = 0; i < s->n_chan; i++)
1814 write_caldac(dev, i, s->maxdata / 2);
1816 s->type = COMEDI_SUBD_UNUSED;
1819 s = &dev->subdevices[4];
1820 if (board->register_layout == labpc_1200_layout) {
1821 s->type = COMEDI_SUBD_MEMORY;
1822 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
1823 s->n_chan = EEPROM_SIZE;
1825 s->insn_read = labpc_eeprom_insn_read;
1826 s->insn_write = labpc_eeprom_insn_write;
1828 for (i = 0; i < s->n_chan; i++)
1829 devpriv->eeprom_data[i] = labpc_eeprom_read(dev, i);
1831 s->type = COMEDI_SUBD_UNUSED;
1835 EXPORT_SYMBOL_GPL(labpc_common_attach);
1837 static int labpc_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1839 const struct labpc_boardinfo *board = comedi_board(dev);
1840 struct labpc_private *devpriv;
1841 unsigned long iobase = 0;
1842 unsigned int irq = 0;
1843 unsigned int dma_chan = 0;
1845 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1848 dev->private = devpriv;
1850 /* get base address, irq etc. based on bustype */
1851 switch (board->bustype) {
1853 #ifdef CONFIG_ISA_DMA_API
1854 iobase = it->options[0];
1855 irq = it->options[1];
1856 dma_chan = it->options[2];
1858 dev_err(dev->class_dev,
1859 "ni_labpc driver has not been built with ISA DMA support.\n");
1864 #ifdef CONFIG_COMEDI_PCI_DRIVERS
1865 dev_err(dev->class_dev,
1866 "manual configuration of PCI board '%s' is not supported\n",
1870 dev_err(dev->class_dev,
1871 "ni_labpc driver has not been built with PCI support.\n");
1876 dev_err(dev->class_dev,
1877 "ni_labpc: bug! couldn't determine board type\n");
1882 return labpc_common_attach(dev, iobase, irq, dma_chan);
1885 static const struct labpc_boardinfo *
1886 labpc_pci_find_boardinfo(struct pci_dev *pcidev)
1888 unsigned int device_id = pcidev->device;
1891 for (n = 0; n < ARRAY_SIZE(labpc_boards); n++) {
1892 const struct labpc_boardinfo *board = &labpc_boards[n];
1893 if (board->bustype == pci_bustype &&
1894 board->device_id == device_id)
1900 static int labpc_auto_attach(struct comedi_device *dev,
1901 unsigned long context_unused)
1903 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1904 struct labpc_private *devpriv;
1905 unsigned long iobase;
1909 if (!IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS))
1912 ret = comedi_pci_enable(dev);
1916 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1919 dev->private = devpriv;
1921 dev->board_ptr = labpc_pci_find_boardinfo(pcidev);
1922 if (!dev->board_ptr)
1924 devpriv->mite = mite_alloc(pcidev);
1927 ret = mite_setup(devpriv->mite);
1930 iobase = (unsigned long)devpriv->mite->daq_io_addr;
1931 irq = mite_irq(devpriv->mite);
1932 return labpc_common_attach(dev, iobase, irq, 0);
1935 void labpc_common_detach(struct comedi_device *dev)
1937 const struct labpc_boardinfo *board = comedi_board(dev);
1938 struct labpc_private *devpriv = dev->private;
1939 struct comedi_subdevice *s;
1943 if (dev->subdevices) {
1944 s = &dev->subdevices[2];
1945 subdev_8255_cleanup(dev, s);
1947 #ifdef CONFIG_ISA_DMA_API
1948 /* only free stuff if it has been allocated by _attach */
1949 kfree(devpriv->dma_buffer);
1950 if (devpriv->dma_chan)
1951 free_dma(devpriv->dma_chan);
1954 free_irq(dev->irq, dev);
1955 if (board->bustype == isa_bustype && dev->iobase)
1956 release_region(dev->iobase, LABPC_SIZE);
1957 #ifdef CONFIG_COMEDI_PCI_DRIVERS
1958 if (devpriv->mite) {
1959 mite_unsetup(devpriv->mite);
1960 mite_free(devpriv->mite);
1962 if (board->bustype == pci_bustype)
1963 comedi_pci_disable(dev);
1966 EXPORT_SYMBOL_GPL(labpc_common_detach);
1968 static struct comedi_driver labpc_driver = {
1969 .driver_name = "ni_labpc",
1970 .module = THIS_MODULE,
1971 .attach = labpc_attach,
1972 .auto_attach = labpc_auto_attach,
1973 .detach = labpc_common_detach,
1974 .num_names = ARRAY_SIZE(labpc_boards),
1975 .board_name = &labpc_boards[0].name,
1976 .offset = sizeof(struct labpc_boardinfo),
1979 #ifdef CONFIG_COMEDI_PCI_DRIVERS
1980 static DEFINE_PCI_DEVICE_TABLE(labpc_pci_table) = {
1981 { PCI_DEVICE(PCI_VENDOR_ID_NI, 0x161) },
1984 MODULE_DEVICE_TABLE(pci, labpc_pci_table);
1986 static int labpc_pci_probe(struct pci_dev *dev,
1987 const struct pci_device_id *id)
1989 return comedi_pci_auto_config(dev, &labpc_driver, id->driver_data);
1992 static struct pci_driver labpc_pci_driver = {
1994 .id_table = labpc_pci_table,
1995 .probe = labpc_pci_probe,
1996 .remove = comedi_pci_auto_unconfig,
1998 module_comedi_pci_driver(labpc_driver, labpc_pci_driver);
2000 module_comedi_driver(labpc_driver);
2004 MODULE_AUTHOR("Comedi http://www.comedi.org");
2005 MODULE_DESCRIPTION("Comedi low-level driver");
2006 MODULE_LICENSE("GPL");