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;
854 #ifdef CONFIG_ISA_DMA_API
855 unsigned long irq_flags;
858 struct comedi_async *async = s->async;
859 struct comedi_cmd *cmd = &async->cmd;
860 enum transfer_type xfer;
865 comedi_error(dev, "no irq assigned, cannot perform command");
869 range = CR_RANGE(cmd->chanlist[0]);
870 aref = CR_AREF(cmd->chanlist[0]);
872 /* make sure board is disabled before setting up acquisition */
873 spin_lock_irqsave(&dev->spinlock, flags);
874 devpriv->cmd2 &= ~SWTRIG_BIT & ~HWTRIG_BIT & ~PRETRIG_BIT;
875 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
876 spin_unlock_irqrestore(&dev->spinlock, flags);
879 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
881 /* initialize software conversion count */
882 if (cmd->stop_src == TRIG_COUNT)
883 devpriv->count = cmd->stop_arg * cmd->chanlist_len;
885 /* setup hardware conversion counter */
886 if (cmd->stop_src == TRIG_EXT) {
888 * load counter a1 with count of 3
889 * (pc+ manual says this is minimum allowed) using mode 0
891 ret = labpc_counter_load(dev, dev->iobase + COUNTER_A_BASE_REG,
894 comedi_error(dev, "error loading counter a1");
898 * otherwise, just put a1 in mode 0
899 * with no count to set its output low
901 devpriv->write_byte(INIT_A1_BITS,
902 dev->iobase + COUNTER_A_CONTROL_REG);
904 #ifdef CONFIG_ISA_DMA_API
905 /* figure out what method we will use to transfer data */
906 if (devpriv->dma_chan && /* need a dma channel allocated */
908 * dma unsafe at RT priority,
909 * and too much setup time for TRIG_WAKE_EOS for
911 (cmd->flags & (TRIG_WAKE_EOS | TRIG_RT)) == 0 &&
912 /* only available on the isa boards */
913 board->bustype == isa_bustype) {
914 xfer = isa_dma_transfer;
915 /* pc-plus has no fifo-half full interrupt */
918 if (board->register_layout == labpc_1200_layout &&
919 /* wake-end-of-scan should interrupt on fifo not empty */
920 (cmd->flags & TRIG_WAKE_EOS) == 0 &&
921 /* make sure we are taking more than just a few points */
922 (cmd->stop_src != TRIG_COUNT || devpriv->count > 256)) {
923 xfer = fifo_half_full_transfer;
925 xfer = fifo_not_empty_transfer;
926 devpriv->current_transfer = xfer;
927 mode = labpc_ai_scan_mode(cmd);
929 /* setup cmd6 register for 1200 boards */
930 if (board->register_layout == labpc_1200_layout) {
931 /* reference inputs to ground or common? */
932 if (aref != AREF_GROUND)
933 devpriv->cmd6 |= ADC_COMMON_BIT;
935 devpriv->cmd6 &= ~ADC_COMMON_BIT;
936 /* bipolar or unipolar range? */
937 if (labpc_range_is_unipolar(s, range))
938 devpriv->cmd6 |= ADC_UNIP_BIT;
940 devpriv->cmd6 &= ~ADC_UNIP_BIT;
941 /* interrupt on fifo half full? */
942 if (xfer == fifo_half_full_transfer)
943 devpriv->cmd6 |= ADC_FHF_INTR_EN_BIT;
945 devpriv->cmd6 &= ~ADC_FHF_INTR_EN_BIT;
946 /* enable interrupt on counter a1 terminal count? */
947 if (cmd->stop_src == TRIG_EXT)
948 devpriv->cmd6 |= A1_INTR_EN_BIT;
950 devpriv->cmd6 &= ~A1_INTR_EN_BIT;
951 /* are we scanning up or down through channels? */
952 if (mode == MODE_MULT_CHAN_UP)
953 devpriv->cmd6 |= ADC_SCAN_UP_BIT;
955 devpriv->cmd6 &= ~ADC_SCAN_UP_BIT;
956 /* write to register */
957 devpriv->write_byte(devpriv->cmd6, dev->iobase + COMMAND6_REG);
960 /* setup channel list, etc (cmd1 register) */
962 if (mode == MODE_MULT_CHAN_UP)
963 channel = CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]);
965 channel = CR_CHAN(cmd->chanlist[0]);
966 /* munge channel bits for differential / scan disabled mode */
967 if ((mode == MODE_SINGLE_CHAN || mode == MODE_SINGLE_CHAN_INTERVAL) &&
970 devpriv->cmd1 |= ADC_CHAN_BITS(channel);
971 devpriv->cmd1 |= board->ai_range_code[range];
972 devpriv->write_byte(devpriv->cmd1, dev->iobase + COMMAND1_REG);
973 /* manual says to set scan enable bit on second pass */
974 if (mode == MODE_MULT_CHAN_UP || mode == MODE_MULT_CHAN_DOWN) {
975 devpriv->cmd1 |= ADC_SCAN_EN_BIT;
976 /* need a brief delay before enabling scan, or scan
977 * list will get screwed when you switch
978 * between scan up to scan down mode - dunno why */
980 devpriv->write_byte(devpriv->cmd1, dev->iobase + COMMAND1_REG);
983 devpriv->write_byte(cmd->chanlist_len,
984 dev->iobase + INTERVAL_COUNT_REG);
986 devpriv->write_byte(INTERVAL_LOAD_BITS,
987 dev->iobase + INTERVAL_LOAD_REG);
989 if (cmd->convert_src == TRIG_TIMER || cmd->scan_begin_src == TRIG_TIMER) {
991 labpc_adc_timing(dev, cmd, mode);
992 /* load counter b0 in mode 3 */
993 ret = labpc_counter_load(dev, dev->iobase + COUNTER_B_BASE_REG,
994 0, devpriv->divisor_b0, 3);
996 comedi_error(dev, "error loading counter b0");
1000 /* set up conversion pacing */
1001 if (labpc_ai_convert_period(cmd, mode)) {
1002 /* load counter a0 in mode 2 */
1003 ret = labpc_counter_load(dev, dev->iobase + COUNTER_A_BASE_REG,
1004 0, devpriv->divisor_a0, 2);
1006 comedi_error(dev, "error loading counter a0");
1010 devpriv->write_byte(INIT_A0_BITS,
1011 dev->iobase + COUNTER_A_CONTROL_REG);
1013 /* set up scan pacing */
1014 if (labpc_ai_scan_period(cmd, mode)) {
1015 /* load counter b1 in mode 2 */
1016 ret = labpc_counter_load(dev, dev->iobase + COUNTER_B_BASE_REG,
1017 1, devpriv->divisor_b1, 2);
1019 comedi_error(dev, "error loading counter b1");
1024 labpc_clear_adc_fifo(dev);
1026 #ifdef CONFIG_ISA_DMA_API
1027 /* set up dma transfer */
1028 if (xfer == isa_dma_transfer) {
1029 irq_flags = claim_dma_lock();
1030 disable_dma(devpriv->dma_chan);
1031 /* clear flip-flop to make sure 2-byte registers for
1032 * count and address get set correctly */
1033 clear_dma_ff(devpriv->dma_chan);
1034 set_dma_addr(devpriv->dma_chan,
1035 virt_to_bus(devpriv->dma_buffer));
1036 /* set appropriate size of transfer */
1037 devpriv->dma_transfer_size = labpc_suggest_transfer_size(cmd);
1038 if (cmd->stop_src == TRIG_COUNT &&
1039 devpriv->count * sample_size < devpriv->dma_transfer_size) {
1040 devpriv->dma_transfer_size =
1041 devpriv->count * sample_size;
1043 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
1044 enable_dma(devpriv->dma_chan);
1045 release_dma_lock(irq_flags);
1046 /* enable board's dma */
1047 devpriv->cmd3 |= DMA_EN_BIT | DMATC_INTR_EN_BIT;
1049 devpriv->cmd3 &= ~DMA_EN_BIT & ~DMATC_INTR_EN_BIT;
1052 /* enable error interrupts */
1053 devpriv->cmd3 |= ERR_INTR_EN_BIT;
1054 /* enable fifo not empty interrupt? */
1055 if (xfer == fifo_not_empty_transfer)
1056 devpriv->cmd3 |= ADC_FNE_INTR_EN_BIT;
1058 devpriv->cmd3 &= ~ADC_FNE_INTR_EN_BIT;
1059 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
1061 /* setup any external triggering/pacing (cmd4 register) */
1063 if (cmd->convert_src != TRIG_EXT)
1064 devpriv->cmd4 |= EXT_CONVERT_DISABLE_BIT;
1065 /* XXX should discard first scan when using interval scanning
1066 * since manual says it is not synced with scan clock */
1067 if (labpc_use_continuous_mode(cmd, mode) == 0) {
1068 devpriv->cmd4 |= INTERVAL_SCAN_EN_BIT;
1069 if (cmd->scan_begin_src == TRIG_EXT)
1070 devpriv->cmd4 |= EXT_SCAN_EN_BIT;
1072 /* single-ended/differential */
1073 if (aref == AREF_DIFF)
1074 devpriv->cmd4 |= ADC_DIFF_BIT;
1075 devpriv->write_byte(devpriv->cmd4, dev->iobase + COMMAND4_REG);
1077 /* startup acquisition */
1080 /* use 2 cascaded counters for pacing */
1081 spin_lock_irqsave(&dev->spinlock, flags);
1082 devpriv->cmd2 |= CASCADE_BIT;
1083 switch (cmd->start_src) {
1085 devpriv->cmd2 |= HWTRIG_BIT;
1086 devpriv->cmd2 &= ~PRETRIG_BIT & ~SWTRIG_BIT;
1089 devpriv->cmd2 |= SWTRIG_BIT;
1090 devpriv->cmd2 &= ~PRETRIG_BIT & ~HWTRIG_BIT;
1093 comedi_error(dev, "bug with start_src");
1094 spin_unlock_irqrestore(&dev->spinlock, flags);
1098 switch (cmd->stop_src) {
1100 devpriv->cmd2 |= HWTRIG_BIT | PRETRIG_BIT;
1106 comedi_error(dev, "bug with stop_src");
1107 spin_unlock_irqrestore(&dev->spinlock, flags);
1110 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
1111 spin_unlock_irqrestore(&dev->spinlock, flags);
1116 static int labpc_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1118 struct labpc_private *devpriv = dev->private;
1119 unsigned long flags;
1121 spin_lock_irqsave(&dev->spinlock, flags);
1122 devpriv->cmd2 &= ~SWTRIG_BIT & ~HWTRIG_BIT & ~PRETRIG_BIT;
1123 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
1124 spin_unlock_irqrestore(&dev->spinlock, flags);
1127 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
1132 #ifdef CONFIG_ISA_DMA_API
1133 static void labpc_drain_dma(struct comedi_device *dev)
1135 struct labpc_private *devpriv = dev->private;
1136 struct comedi_subdevice *s = dev->read_subdev;
1137 struct comedi_async *async = s->async;
1139 unsigned long flags;
1140 unsigned int max_points, num_points, residue, leftover;
1143 status = devpriv->stat1;
1145 flags = claim_dma_lock();
1146 disable_dma(devpriv->dma_chan);
1147 /* clear flip-flop to make sure 2-byte registers for
1148 * count and address get set correctly */
1149 clear_dma_ff(devpriv->dma_chan);
1151 /* figure out how many points to read */
1152 max_points = devpriv->dma_transfer_size / sample_size;
1153 /* residue is the number of points left to be done on the dma
1154 * transfer. It should always be zero at this point unless
1155 * the stop_src is set to external triggering.
1157 residue = get_dma_residue(devpriv->dma_chan) / sample_size;
1158 num_points = max_points - residue;
1159 if (devpriv->count < num_points && async->cmd.stop_src == TRIG_COUNT)
1160 num_points = devpriv->count;
1162 /* figure out how many points will be stored next time */
1164 if (async->cmd.stop_src != TRIG_COUNT) {
1165 leftover = devpriv->dma_transfer_size / sample_size;
1166 } else if (devpriv->count > num_points) {
1167 leftover = devpriv->count - num_points;
1168 if (leftover > max_points)
1169 leftover = max_points;
1172 /* write data to comedi buffer */
1173 for (i = 0; i < num_points; i++)
1174 cfc_write_to_buffer(s, devpriv->dma_buffer[i]);
1176 if (async->cmd.stop_src == TRIG_COUNT)
1177 devpriv->count -= num_points;
1179 /* set address and count for next transfer */
1180 set_dma_addr(devpriv->dma_chan, virt_to_bus(devpriv->dma_buffer));
1181 set_dma_count(devpriv->dma_chan, leftover * sample_size);
1182 release_dma_lock(flags);
1184 async->events |= COMEDI_CB_BLOCK;
1187 static void handle_isa_dma(struct comedi_device *dev)
1189 struct labpc_private *devpriv = dev->private;
1191 labpc_drain_dma(dev);
1193 enable_dma(devpriv->dma_chan);
1195 /* clear dma tc interrupt */
1196 devpriv->write_byte(0x1, dev->iobase + DMATC_CLEAR_REG);
1200 /* read all available samples from ai fifo */
1201 static int labpc_drain_fifo(struct comedi_device *dev)
1203 struct labpc_private *devpriv = dev->private;
1204 unsigned int lsb, msb;
1206 struct comedi_async *async = dev->read_subdev->async;
1207 const int timeout = 10000;
1210 devpriv->stat1 = devpriv->read_byte(dev->iobase + STATUS1_REG);
1212 for (i = 0; (devpriv->stat1 & DATA_AVAIL_BIT) && i < timeout;
1214 /* quit if we have all the data we want */
1215 if (async->cmd.stop_src == TRIG_COUNT) {
1216 if (devpriv->count == 0)
1220 lsb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
1221 msb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
1222 data = (msb << 8) | lsb;
1223 cfc_write_to_buffer(dev->read_subdev, data);
1224 devpriv->stat1 = devpriv->read_byte(dev->iobase + STATUS1_REG);
1227 comedi_error(dev, "ai timeout, fifo never empties");
1228 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1235 /* makes sure all data acquired by board is transferred to comedi (used
1236 * when acquisition is terminated by stop_src == TRIG_EXT). */
1237 static void labpc_drain_dregs(struct comedi_device *dev)
1239 #ifdef CONFIG_ISA_DMA_API
1240 struct labpc_private *devpriv = dev->private;
1242 if (devpriv->current_transfer == isa_dma_transfer)
1243 labpc_drain_dma(dev);
1246 labpc_drain_fifo(dev);
1249 /* interrupt service routine */
1250 static irqreturn_t labpc_interrupt(int irq, void *d)
1252 struct comedi_device *dev = d;
1253 const struct labpc_boardinfo *board = comedi_board(dev);
1254 struct labpc_private *devpriv = dev->private;
1255 struct comedi_subdevice *s = dev->read_subdev;
1256 struct comedi_async *async;
1257 struct comedi_cmd *cmd;
1259 if (!dev->attached) {
1260 comedi_error(dev, "premature interrupt");
1268 /* read board status */
1269 devpriv->stat1 = devpriv->read_byte(dev->iobase + STATUS1_REG);
1270 if (board->register_layout == labpc_1200_layout)
1271 devpriv->stat2 = devpriv->read_byte(dev->iobase + STATUS2_REG);
1273 if ((devpriv->stat1 & (DMATC_BIT | TIMER_BIT | OVERFLOW_BIT |
1274 OVERRUN_BIT | DATA_AVAIL_BIT)) == 0
1275 && (devpriv->stat2 & A1_TC_BIT) == 0
1276 && (devpriv->stat2 & FNHF_BIT)) {
1280 if (devpriv->stat1 & OVERRUN_BIT) {
1281 /* clear error interrupt */
1282 devpriv->write_byte(0x1, dev->iobase + ADC_CLEAR_REG);
1283 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1284 comedi_event(dev, s);
1285 comedi_error(dev, "overrun");
1289 #ifdef CONFIG_ISA_DMA_API
1290 if (devpriv->current_transfer == isa_dma_transfer) {
1292 * if a dma terminal count of external stop trigger
1295 if (devpriv->stat1 & DMATC_BIT ||
1296 (board->register_layout == labpc_1200_layout
1297 && devpriv->stat2 & A1_TC_BIT)) {
1298 handle_isa_dma(dev);
1302 labpc_drain_fifo(dev);
1304 if (devpriv->stat1 & TIMER_BIT) {
1305 comedi_error(dev, "handled timer interrupt?");
1307 devpriv->write_byte(0x1, dev->iobase + TIMER_CLEAR_REG);
1310 if (devpriv->stat1 & OVERFLOW_BIT) {
1311 /* clear error interrupt */
1312 devpriv->write_byte(0x1, dev->iobase + ADC_CLEAR_REG);
1313 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1314 comedi_event(dev, s);
1315 comedi_error(dev, "overflow");
1318 /* handle external stop trigger */
1319 if (cmd->stop_src == TRIG_EXT) {
1320 if (devpriv->stat2 & A1_TC_BIT) {
1321 labpc_drain_dregs(dev);
1322 labpc_cancel(dev, s);
1323 async->events |= COMEDI_CB_EOA;
1327 /* TRIG_COUNT end of acquisition */
1328 if (cmd->stop_src == TRIG_COUNT) {
1329 if (devpriv->count == 0) {
1330 labpc_cancel(dev, s);
1331 async->events |= COMEDI_CB_EOA;
1335 comedi_event(dev, s);
1339 static int labpc_ao_insn_write(struct comedi_device *dev,
1340 struct comedi_subdevice *s,
1341 struct comedi_insn *insn,
1344 const struct labpc_boardinfo *board = comedi_board(dev);
1345 struct labpc_private *devpriv = dev->private;
1347 unsigned long flags;
1350 channel = CR_CHAN(insn->chanspec);
1352 /* turn off pacing of analog output channel */
1353 /* note: hardware bug in daqcard-1200 means pacing cannot
1354 * be independently enabled/disabled for its the two channels */
1355 spin_lock_irqsave(&dev->spinlock, flags);
1356 devpriv->cmd2 &= ~DAC_PACED_BIT(channel);
1357 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
1358 spin_unlock_irqrestore(&dev->spinlock, flags);
1361 if (board->register_layout == labpc_1200_layout) {
1362 range = CR_RANGE(insn->chanspec);
1363 if (labpc_range_is_unipolar(s, range))
1364 devpriv->cmd6 |= DAC_UNIP_BIT(channel);
1366 devpriv->cmd6 &= ~DAC_UNIP_BIT(channel);
1367 /* write to register */
1368 devpriv->write_byte(devpriv->cmd6, dev->iobase + COMMAND6_REG);
1371 lsb = data[0] & 0xff;
1372 msb = (data[0] >> 8) & 0xff;
1373 devpriv->write_byte(lsb, dev->iobase + DAC_LSB_REG(channel));
1374 devpriv->write_byte(msb, dev->iobase + DAC_MSB_REG(channel));
1376 /* remember value for readback */
1377 devpriv->ao_value[channel] = data[0];
1382 static int labpc_ao_insn_read(struct comedi_device *dev,
1383 struct comedi_subdevice *s,
1384 struct comedi_insn *insn,
1387 struct labpc_private *devpriv = dev->private;
1389 data[0] = devpriv->ao_value[CR_CHAN(insn->chanspec)];
1394 static int labpc_8255_mmio(int dir, int port, int data, unsigned long iobase)
1397 writeb(data, (void __iomem *)(iobase + port));
1400 return readb((void __iomem *)(iobase + port));
1404 /* lowlevel write to eeprom/dac */
1405 static void labpc_serial_out(struct comedi_device *dev, unsigned int value,
1406 unsigned int value_width)
1408 struct labpc_private *devpriv = dev->private;
1411 for (i = 1; i <= value_width; i++) {
1412 /* clear serial clock */
1413 devpriv->cmd5 &= ~SCLOCK_BIT;
1414 /* send bits most significant bit first */
1415 if (value & (1 << (value_width - i)))
1416 devpriv->cmd5 |= SDATA_BIT;
1418 devpriv->cmd5 &= ~SDATA_BIT;
1420 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1421 /* set clock to load bit */
1422 devpriv->cmd5 |= SCLOCK_BIT;
1424 devpriv->write_byte(devpriv->cmd5,
1425 dev->iobase + COMMAND5_REG);
1429 /* lowlevel read from eeprom */
1430 static unsigned int labpc_serial_in(struct comedi_device *dev)
1432 struct labpc_private *devpriv = dev->private;
1433 unsigned int value = 0;
1435 const int value_width = 8; /* number of bits wide values are */
1437 for (i = 1; i <= value_width; i++) {
1438 /* set serial clock */
1439 devpriv->cmd5 |= SCLOCK_BIT;
1441 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1442 /* clear clock bit */
1443 devpriv->cmd5 &= ~SCLOCK_BIT;
1445 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1446 /* read bits most significant bit first */
1448 devpriv->stat2 = devpriv->read_byte(dev->iobase + STATUS2_REG);
1449 if (devpriv->stat2 & EEPROM_OUT_BIT)
1450 value |= 1 << (value_width - i);
1456 static unsigned int labpc_eeprom_read(struct comedi_device *dev,
1457 unsigned int address)
1459 struct labpc_private *devpriv = dev->private;
1461 /* bits to tell eeprom to expect a read */
1462 const int read_instruction = 0x3;
1463 /* 8 bit write lengths to eeprom */
1464 const int write_length = 8;
1466 /* enable read/write to eeprom */
1467 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1469 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1470 devpriv->cmd5 |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
1472 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1474 /* send read instruction */
1475 labpc_serial_out(dev, read_instruction, write_length);
1476 /* send 8 bit address to read from */
1477 labpc_serial_out(dev, address, write_length);
1479 value = labpc_serial_in(dev);
1481 /* disable read/write to eeprom */
1482 devpriv->cmd5 &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1484 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1489 static unsigned int labpc_eeprom_read_status(struct comedi_device *dev)
1491 struct labpc_private *devpriv = dev->private;
1493 const int read_status_instruction = 0x5;
1494 const int write_length = 8; /* 8 bit write lengths to eeprom */
1496 /* enable read/write to eeprom */
1497 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1499 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1500 devpriv->cmd5 |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
1502 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1504 /* send read status instruction */
1505 labpc_serial_out(dev, read_status_instruction, write_length);
1507 value = labpc_serial_in(dev);
1509 /* disable read/write to eeprom */
1510 devpriv->cmd5 &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1512 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1517 static int labpc_eeprom_write(struct comedi_device *dev,
1518 unsigned int address, unsigned int value)
1520 struct labpc_private *devpriv = dev->private;
1521 const int write_enable_instruction = 0x6;
1522 const int write_instruction = 0x2;
1523 const int write_length = 8; /* 8 bit write lengths to eeprom */
1524 const int write_in_progress_bit = 0x1;
1525 const int timeout = 10000;
1528 /* make sure there isn't already a write in progress */
1529 for (i = 0; i < timeout; i++) {
1530 if ((labpc_eeprom_read_status(dev) & write_in_progress_bit) ==
1535 comedi_error(dev, "eeprom write timed out");
1538 /* update software copy of eeprom */
1539 devpriv->eeprom_data[address] = value;
1541 /* enable read/write to eeprom */
1542 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1544 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1545 devpriv->cmd5 |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
1547 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1549 /* send write_enable instruction */
1550 labpc_serial_out(dev, write_enable_instruction, write_length);
1551 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1553 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1555 /* send write instruction */
1556 devpriv->cmd5 |= EEPROM_EN_BIT;
1558 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1559 labpc_serial_out(dev, write_instruction, write_length);
1560 /* send 8 bit address to write to */
1561 labpc_serial_out(dev, address, write_length);
1563 labpc_serial_out(dev, value, write_length);
1564 devpriv->cmd5 &= ~EEPROM_EN_BIT;
1566 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1568 /* disable read/write to eeprom */
1569 devpriv->cmd5 &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1571 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1576 /* writes to 8 bit calibration dacs */
1577 static void write_caldac(struct comedi_device *dev, unsigned int channel,
1580 struct labpc_private *devpriv = dev->private;
1582 if (value == devpriv->caldac[channel])
1584 devpriv->caldac[channel] = value;
1586 /* clear caldac load bit and make sure we don't write to eeprom */
1588 ~CALDAC_LOAD_BIT & ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1590 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1592 /* write 4 bit channel */
1593 labpc_serial_out(dev, channel, 4);
1594 /* write 8 bit caldac value */
1595 labpc_serial_out(dev, value, 8);
1597 /* set and clear caldac bit to load caldac value */
1598 devpriv->cmd5 |= CALDAC_LOAD_BIT;
1600 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1601 devpriv->cmd5 &= ~CALDAC_LOAD_BIT;
1603 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1606 static int labpc_calib_insn_write(struct comedi_device *dev,
1607 struct comedi_subdevice *s,
1608 struct comedi_insn *insn,
1611 int channel = CR_CHAN(insn->chanspec);
1613 write_caldac(dev, channel, data[0]);
1617 static int labpc_calib_insn_read(struct comedi_device *dev,
1618 struct comedi_subdevice *s,
1619 struct comedi_insn *insn,
1622 struct labpc_private *devpriv = dev->private;
1624 data[0] = devpriv->caldac[CR_CHAN(insn->chanspec)];
1629 static int labpc_eeprom_insn_write(struct comedi_device *dev,
1630 struct comedi_subdevice *s,
1631 struct comedi_insn *insn,
1634 int channel = CR_CHAN(insn->chanspec);
1637 /* only allow writes to user area of eeprom */
1638 if (channel < 16 || channel > 127) {
1639 dev_dbg(dev->class_dev,
1640 "eeprom writes are only allowed to channels 16 through 127 (the pointer and user areas)\n");
1644 ret = labpc_eeprom_write(dev, channel, data[0]);
1651 static int labpc_eeprom_insn_read(struct comedi_device *dev,
1652 struct comedi_subdevice *s,
1653 struct comedi_insn *insn,
1656 struct labpc_private *devpriv = dev->private;
1658 data[0] = devpriv->eeprom_data[CR_CHAN(insn->chanspec)];
1663 int labpc_common_attach(struct comedi_device *dev, unsigned long iobase,
1664 unsigned int irq, unsigned int dma_chan)
1666 const struct labpc_boardinfo *board = comedi_board(dev);
1667 struct labpc_private *devpriv = dev->private;
1668 struct comedi_subdevice *s;
1669 unsigned long isr_flags;
1673 dev->board_name = board->name;
1675 dev_info(dev->class_dev, "ni_labpc: %s\n", dev->board_name);
1677 dev_err(dev->class_dev, "io base address is zero!\n");
1680 /* request io regions for isa boards */
1681 if (board->bustype == isa_bustype) {
1682 /* check if io addresses are available */
1683 if (!request_region(iobase, LABPC_SIZE, dev->board_name)) {
1684 dev_err(dev->class_dev, "I/O port conflict\n");
1688 dev->iobase = iobase;
1690 if (board->has_mmio) {
1691 devpriv->read_byte = labpc_readb;
1692 devpriv->write_byte = labpc_writeb;
1694 devpriv->read_byte = labpc_inb;
1695 devpriv->write_byte = labpc_outb;
1697 /* initialize board's command registers */
1698 devpriv->write_byte(devpriv->cmd1, dev->iobase + COMMAND1_REG);
1699 devpriv->write_byte(devpriv->cmd2, dev->iobase + COMMAND2_REG);
1700 devpriv->write_byte(devpriv->cmd3, dev->iobase + COMMAND3_REG);
1701 devpriv->write_byte(devpriv->cmd4, dev->iobase + COMMAND4_REG);
1702 if (board->register_layout == labpc_1200_layout) {
1703 devpriv->write_byte(devpriv->cmd5, dev->iobase + COMMAND5_REG);
1704 devpriv->write_byte(devpriv->cmd6, dev->iobase + COMMAND6_REG);
1710 if (board->bustype == pci_bustype ||
1711 board->bustype == pcmcia_bustype)
1712 isr_flags |= IRQF_SHARED;
1713 if (request_irq(irq, labpc_interrupt, isr_flags,
1714 dev->board_name, dev)) {
1715 dev_err(dev->class_dev, "unable to allocate irq %u\n",
1722 #ifdef CONFIG_ISA_DMA_API
1723 /* grab dma channel */
1725 dev_err(dev->class_dev, "invalid dma channel %u\n", dma_chan);
1727 } else if (dma_chan) {
1728 unsigned long dma_flags;
1730 /* allocate dma buffer */
1731 devpriv->dma_buffer = kmalloc(dma_buffer_size,
1732 GFP_KERNEL | GFP_DMA);
1733 if (devpriv->dma_buffer == NULL)
1736 if (request_dma(dma_chan, dev->board_name)) {
1737 dev_err(dev->class_dev,
1738 "failed to allocate dma channel %u\n",
1742 devpriv->dma_chan = dma_chan;
1743 dma_flags = claim_dma_lock();
1744 disable_dma(devpriv->dma_chan);
1745 set_dma_mode(devpriv->dma_chan, DMA_MODE_READ);
1746 release_dma_lock(dma_flags);
1750 ret = comedi_alloc_subdevices(dev, 5);
1754 /* analog input subdevice */
1755 s = &dev->subdevices[0];
1756 s->type = COMEDI_SUBD_AI;
1757 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_COMMON | SDF_DIFF;
1759 s->len_chanlist = 8;
1760 s->maxdata = 0x0fff;
1761 s->range_table = board->ai_range_table;
1762 s->insn_read = labpc_ai_insn_read;
1764 dev->read_subdev = s;
1765 s->subdev_flags |= SDF_CMD_READ;
1766 s->do_cmd = labpc_ai_cmd;
1767 s->do_cmdtest = labpc_ai_cmdtest;
1768 s->cancel = labpc_cancel;
1772 s = &dev->subdevices[1];
1773 if (board->has_ao) {
1774 s->type = COMEDI_SUBD_AO;
1775 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
1776 s->n_chan = NUM_AO_CHAN;
1777 s->maxdata = 0x0fff;
1778 s->range_table = &range_labpc_ao;
1779 s->insn_read = labpc_ao_insn_read;
1780 s->insn_write = labpc_ao_insn_write;
1782 /* initialize analog outputs to a known value */
1783 for (i = 0; i < s->n_chan; i++) {
1786 devpriv->ao_value[i] = s->maxdata / 2;
1787 lsb = devpriv->ao_value[i] & 0xff;
1788 msb = (devpriv->ao_value[i] >> 8) & 0xff;
1789 devpriv->write_byte(lsb, dev->iobase + DAC_LSB_REG(i));
1790 devpriv->write_byte(msb, dev->iobase + DAC_MSB_REG(i));
1793 s->type = COMEDI_SUBD_UNUSED;
1797 s = &dev->subdevices[2];
1798 ret = subdev_8255_init(dev, s,
1799 (board->has_mmio) ? labpc_8255_mmio : NULL,
1800 dev->iobase + DIO_BASE_REG);
1804 /* calibration subdevices for boards that have one */
1805 s = &dev->subdevices[3];
1806 if (board->register_layout == labpc_1200_layout) {
1807 s->type = COMEDI_SUBD_CALIB;
1808 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
1811 s->insn_read = labpc_calib_insn_read;
1812 s->insn_write = labpc_calib_insn_write;
1814 for (i = 0; i < s->n_chan; i++)
1815 write_caldac(dev, i, s->maxdata / 2);
1817 s->type = COMEDI_SUBD_UNUSED;
1820 s = &dev->subdevices[4];
1821 if (board->register_layout == labpc_1200_layout) {
1822 s->type = COMEDI_SUBD_MEMORY;
1823 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
1824 s->n_chan = EEPROM_SIZE;
1826 s->insn_read = labpc_eeprom_insn_read;
1827 s->insn_write = labpc_eeprom_insn_write;
1829 for (i = 0; i < s->n_chan; i++)
1830 devpriv->eeprom_data[i] = labpc_eeprom_read(dev, i);
1832 s->type = COMEDI_SUBD_UNUSED;
1836 EXPORT_SYMBOL_GPL(labpc_common_attach);
1838 static int labpc_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1840 const struct labpc_boardinfo *board = comedi_board(dev);
1841 struct labpc_private *devpriv;
1842 unsigned long iobase = 0;
1843 unsigned int irq = 0;
1844 unsigned int dma_chan = 0;
1846 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1849 dev->private = devpriv;
1851 /* get base address, irq etc. based on bustype */
1852 switch (board->bustype) {
1854 #ifdef CONFIG_ISA_DMA_API
1855 iobase = it->options[0];
1856 irq = it->options[1];
1857 dma_chan = it->options[2];
1859 dev_err(dev->class_dev,
1860 "ni_labpc driver has not been built with ISA DMA support.\n");
1865 #ifdef CONFIG_COMEDI_PCI_DRIVERS
1866 dev_err(dev->class_dev,
1867 "manual configuration of PCI board '%s' is not supported\n",
1871 dev_err(dev->class_dev,
1872 "ni_labpc driver has not been built with PCI support.\n");
1877 dev_err(dev->class_dev,
1878 "ni_labpc: bug! couldn't determine board type\n");
1883 return labpc_common_attach(dev, iobase, irq, dma_chan);
1886 static const struct labpc_boardinfo *
1887 labpc_pci_find_boardinfo(struct pci_dev *pcidev)
1889 unsigned int device_id = pcidev->device;
1892 for (n = 0; n < ARRAY_SIZE(labpc_boards); n++) {
1893 const struct labpc_boardinfo *board = &labpc_boards[n];
1894 if (board->bustype == pci_bustype &&
1895 board->device_id == device_id)
1901 static int labpc_auto_attach(struct comedi_device *dev,
1902 unsigned long context_unused)
1904 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1905 struct labpc_private *devpriv;
1906 unsigned long iobase;
1910 if (!IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS))
1913 ret = comedi_pci_enable(dev);
1917 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1920 dev->private = devpriv;
1922 dev->board_ptr = labpc_pci_find_boardinfo(pcidev);
1923 if (!dev->board_ptr)
1925 devpriv->mite = mite_alloc(pcidev);
1928 ret = mite_setup(devpriv->mite);
1931 iobase = (unsigned long)devpriv->mite->daq_io_addr;
1932 irq = mite_irq(devpriv->mite);
1933 return labpc_common_attach(dev, iobase, irq, 0);
1936 void labpc_common_detach(struct comedi_device *dev)
1938 const struct labpc_boardinfo *board = comedi_board(dev);
1939 struct labpc_private *devpriv = dev->private;
1940 struct comedi_subdevice *s;
1944 if (dev->subdevices) {
1945 s = &dev->subdevices[2];
1946 subdev_8255_cleanup(dev, s);
1948 #ifdef CONFIG_ISA_DMA_API
1949 /* only free stuff if it has been allocated by _attach */
1950 kfree(devpriv->dma_buffer);
1951 if (devpriv->dma_chan)
1952 free_dma(devpriv->dma_chan);
1955 free_irq(dev->irq, dev);
1956 if (board->bustype == isa_bustype && dev->iobase)
1957 release_region(dev->iobase, LABPC_SIZE);
1958 #ifdef CONFIG_COMEDI_PCI_DRIVERS
1959 if (devpriv->mite) {
1960 mite_unsetup(devpriv->mite);
1961 mite_free(devpriv->mite);
1963 if (board->bustype == pci_bustype)
1964 comedi_pci_disable(dev);
1967 EXPORT_SYMBOL_GPL(labpc_common_detach);
1969 static struct comedi_driver labpc_driver = {
1970 .driver_name = "ni_labpc",
1971 .module = THIS_MODULE,
1972 .attach = labpc_attach,
1973 .auto_attach = labpc_auto_attach,
1974 .detach = labpc_common_detach,
1975 .num_names = ARRAY_SIZE(labpc_boards),
1976 .board_name = &labpc_boards[0].name,
1977 .offset = sizeof(struct labpc_boardinfo),
1980 #ifdef CONFIG_COMEDI_PCI_DRIVERS
1981 static DEFINE_PCI_DEVICE_TABLE(labpc_pci_table) = {
1982 { PCI_DEVICE(PCI_VENDOR_ID_NI, 0x161) },
1985 MODULE_DEVICE_TABLE(pci, labpc_pci_table);
1987 static int labpc_pci_probe(struct pci_dev *dev,
1988 const struct pci_device_id *id)
1990 return comedi_pci_auto_config(dev, &labpc_driver, id->driver_data);
1993 static struct pci_driver labpc_pci_driver = {
1995 .id_table = labpc_pci_table,
1996 .probe = labpc_pci_probe,
1997 .remove = comedi_pci_auto_unconfig,
1999 module_comedi_pci_driver(labpc_driver, labpc_pci_driver);
2001 module_comedi_driver(labpc_driver);
2005 MODULE_AUTHOR("Comedi http://www.comedi.org");
2006 MODULE_DESCRIPTION("Comedi low-level driver");
2007 MODULE_LICENSE("GPL");