1 // SPDX-License-Identifier: GPL-2.0+
3 * comedi/drivers/ni_labpc_common.c
5 * Common support code for "ni_labpc", "ni_labpc_pci" and "ni_labpc_cs".
7 * Copyright (C) 2001-2003 Frank Mori Hess <fmhess@users.sourceforge.net>
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
16 #include "../comedidev.h"
18 #include "comedi_8254.h"
21 #include "ni_labpc_regs.h"
22 #include "ni_labpc_isadma.h"
26 MODE_SINGLE_CHAN_INTERVAL,
31 static const struct comedi_lrange range_labpc_plus_ai = {
52 static const struct comedi_lrange range_labpc_1200_ai = {
71 static const struct comedi_lrange range_labpc_ao = {
79 * functions that do inb/outb and readb/writeb so we can use
80 * function pointers to decide which to use
82 static unsigned int labpc_inb(struct comedi_device *dev, unsigned long reg)
84 return inb(dev->iobase + reg);
87 static void labpc_outb(struct comedi_device *dev,
88 unsigned int byte, unsigned long reg)
90 outb(byte, dev->iobase + reg);
93 static unsigned int labpc_readb(struct comedi_device *dev, unsigned long reg)
95 return readb(dev->mmio + reg);
98 static void labpc_writeb(struct comedi_device *dev,
99 unsigned int byte, unsigned long reg)
101 writeb(byte, dev->mmio + reg);
104 static int labpc_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
106 struct labpc_private *devpriv = dev->private;
109 spin_lock_irqsave(&dev->spinlock, flags);
110 devpriv->cmd2 &= ~(CMD2_SWTRIG | CMD2_HWTRIG | CMD2_PRETRIG);
111 devpriv->write_byte(dev, devpriv->cmd2, CMD2_REG);
112 spin_unlock_irqrestore(&dev->spinlock, flags);
115 devpriv->write_byte(dev, devpriv->cmd3, CMD3_REG);
120 static void labpc_ai_set_chan_and_gain(struct comedi_device *dev,
126 const struct labpc_boardinfo *board = dev->board_ptr;
127 struct labpc_private *devpriv = dev->private;
129 if (board->is_labpc1200) {
131 * The LabPC-1200 boards do not have a gain
132 * of '0x10'. Skip the range values that would
133 * result in this gain.
135 range += (range > 0) + (range > 7);
138 /* munge channel bits for differential/scan disabled mode */
139 if ((mode == MODE_SINGLE_CHAN || mode == MODE_SINGLE_CHAN_INTERVAL) &&
142 devpriv->cmd1 = CMD1_MA(chan);
143 devpriv->cmd1 |= CMD1_GAIN(range);
145 devpriv->write_byte(dev, devpriv->cmd1, CMD1_REG);
148 static void labpc_setup_cmd6_reg(struct comedi_device *dev,
149 struct comedi_subdevice *s,
151 enum transfer_type xfer,
156 const struct labpc_boardinfo *board = dev->board_ptr;
157 struct labpc_private *devpriv = dev->private;
159 if (!board->is_labpc1200)
162 /* reference inputs to ground or common? */
163 if (aref != AREF_GROUND)
164 devpriv->cmd6 |= CMD6_NRSE;
166 devpriv->cmd6 &= ~CMD6_NRSE;
168 /* bipolar or unipolar range? */
169 if (comedi_range_is_unipolar(s, range))
170 devpriv->cmd6 |= CMD6_ADCUNI;
172 devpriv->cmd6 &= ~CMD6_ADCUNI;
174 /* interrupt on fifo half full? */
175 if (xfer == fifo_half_full_transfer)
176 devpriv->cmd6 |= CMD6_HFINTEN;
178 devpriv->cmd6 &= ~CMD6_HFINTEN;
180 /* enable interrupt on counter a1 terminal count? */
182 devpriv->cmd6 |= CMD6_DQINTEN;
184 devpriv->cmd6 &= ~CMD6_DQINTEN;
186 /* are we scanning up or down through channels? */
187 if (mode == MODE_MULT_CHAN_UP)
188 devpriv->cmd6 |= CMD6_SCANUP;
190 devpriv->cmd6 &= ~CMD6_SCANUP;
192 devpriv->write_byte(dev, devpriv->cmd6, CMD6_REG);
195 static unsigned int labpc_read_adc_fifo(struct comedi_device *dev)
197 struct labpc_private *devpriv = dev->private;
198 unsigned int lsb = devpriv->read_byte(dev, ADC_FIFO_REG);
199 unsigned int msb = devpriv->read_byte(dev, ADC_FIFO_REG);
201 return (msb << 8) | lsb;
204 static void labpc_clear_adc_fifo(struct comedi_device *dev)
206 struct labpc_private *devpriv = dev->private;
208 devpriv->write_byte(dev, 0x1, ADC_FIFO_CLEAR_REG);
209 labpc_read_adc_fifo(dev);
212 static int labpc_ai_eoc(struct comedi_device *dev,
213 struct comedi_subdevice *s,
214 struct comedi_insn *insn,
215 unsigned long context)
217 struct labpc_private *devpriv = dev->private;
219 devpriv->stat1 = devpriv->read_byte(dev, STAT1_REG);
220 if (devpriv->stat1 & STAT1_DAVAIL)
225 static int labpc_ai_insn_read(struct comedi_device *dev,
226 struct comedi_subdevice *s,
227 struct comedi_insn *insn,
230 struct labpc_private *devpriv = dev->private;
231 unsigned int chan = CR_CHAN(insn->chanspec);
232 unsigned int range = CR_RANGE(insn->chanspec);
233 unsigned int aref = CR_AREF(insn->chanspec);
237 /* disable timed conversions, interrupt generation and dma */
238 labpc_cancel(dev, s);
240 labpc_ai_set_chan_and_gain(dev, MODE_SINGLE_CHAN, chan, range, aref);
242 labpc_setup_cmd6_reg(dev, s, MODE_SINGLE_CHAN, fifo_not_empty_transfer,
245 /* setup cmd4 register */
247 devpriv->cmd4 |= CMD4_ECLKRCV;
248 /* single-ended/differential */
249 if (aref == AREF_DIFF)
250 devpriv->cmd4 |= CMD4_SEDIFF;
251 devpriv->write_byte(dev, devpriv->cmd4, CMD4_REG);
253 /* initialize pacer counter to prevent any problems */
254 comedi_8254_set_mode(devpriv->counter, 0, I8254_MODE2 | I8254_BINARY);
256 labpc_clear_adc_fifo(dev);
258 for (i = 0; i < insn->n; i++) {
259 /* trigger conversion */
260 devpriv->write_byte(dev, 0x1, ADC_START_CONVERT_REG);
262 ret = comedi_timeout(dev, s, insn, labpc_ai_eoc, 0);
266 data[i] = labpc_read_adc_fifo(dev);
272 static bool labpc_use_continuous_mode(const struct comedi_cmd *cmd,
275 if (mode == MODE_SINGLE_CHAN || cmd->scan_begin_src == TRIG_FOLLOW)
281 static unsigned int labpc_ai_convert_period(const struct comedi_cmd *cmd,
284 if (cmd->convert_src != TRIG_TIMER)
287 if (mode == MODE_SINGLE_CHAN && cmd->scan_begin_src == TRIG_TIMER)
288 return cmd->scan_begin_arg;
290 return cmd->convert_arg;
293 static void labpc_set_ai_convert_period(struct comedi_cmd *cmd,
294 enum scan_mode mode, unsigned int ns)
296 if (cmd->convert_src != TRIG_TIMER)
299 if (mode == MODE_SINGLE_CHAN &&
300 cmd->scan_begin_src == TRIG_TIMER) {
301 cmd->scan_begin_arg = ns;
302 if (cmd->convert_arg > cmd->scan_begin_arg)
303 cmd->convert_arg = cmd->scan_begin_arg;
305 cmd->convert_arg = ns;
309 static unsigned int labpc_ai_scan_period(const struct comedi_cmd *cmd,
312 if (cmd->scan_begin_src != TRIG_TIMER)
315 if (mode == MODE_SINGLE_CHAN && cmd->convert_src == TRIG_TIMER)
318 return cmd->scan_begin_arg;
321 static void labpc_set_ai_scan_period(struct comedi_cmd *cmd,
322 enum scan_mode mode, unsigned int ns)
324 if (cmd->scan_begin_src != TRIG_TIMER)
327 if (mode == MODE_SINGLE_CHAN && cmd->convert_src == TRIG_TIMER)
330 cmd->scan_begin_arg = ns;
333 /* figures out what counter values to use based on command */
334 static void labpc_adc_timing(struct comedi_device *dev, struct comedi_cmd *cmd,
337 struct comedi_8254 *pacer = dev->pacer;
338 unsigned int convert_period = labpc_ai_convert_period(cmd, mode);
339 unsigned int scan_period = labpc_ai_scan_period(cmd, mode);
340 unsigned int base_period;
343 * If both convert and scan triggers are TRIG_TIMER, then they
344 * both rely on counter b0. If only one TRIG_TIMER is used, we
345 * can use the generic cascaded timing functions.
347 if (convert_period && scan_period) {
349 * pick the lowest divisor value we can (for maximum input
350 * clock speed on convert and scan counters)
352 pacer->next_div1 = (scan_period - 1) /
353 (pacer->osc_base * I8254_MAX_COUNT) + 1;
355 comedi_check_trigger_arg_min(&pacer->next_div1, 2);
356 comedi_check_trigger_arg_max(&pacer->next_div1,
359 base_period = pacer->osc_base * pacer->next_div1;
361 /* set a0 for conversion frequency and b1 for scan frequency */
362 switch (cmd->flags & CMDF_ROUND_MASK) {
364 case CMDF_ROUND_NEAREST:
365 pacer->next_div = DIV_ROUND_CLOSEST(convert_period,
367 pacer->next_div2 = DIV_ROUND_CLOSEST(scan_period,
371 pacer->next_div = DIV_ROUND_UP(convert_period,
373 pacer->next_div2 = DIV_ROUND_UP(scan_period,
376 case CMDF_ROUND_DOWN:
377 pacer->next_div = convert_period / base_period;
378 pacer->next_div2 = scan_period / base_period;
381 /* make sure a0 and b1 values are acceptable */
382 comedi_check_trigger_arg_min(&pacer->next_div, 2);
383 comedi_check_trigger_arg_max(&pacer->next_div, I8254_MAX_COUNT);
384 comedi_check_trigger_arg_min(&pacer->next_div2, 2);
385 comedi_check_trigger_arg_max(&pacer->next_div2,
388 /* write corrected timings to command */
389 labpc_set_ai_convert_period(cmd, mode,
390 base_period * pacer->next_div);
391 labpc_set_ai_scan_period(cmd, mode,
392 base_period * pacer->next_div2);
393 } else if (scan_period) {
395 * calculate cascaded counter values
396 * that give desired scan timing
397 * (pacer->next_div2 / pacer->next_div1)
399 comedi_8254_cascade_ns_to_timer(pacer, &scan_period,
401 labpc_set_ai_scan_period(cmd, mode, scan_period);
402 } else if (convert_period) {
404 * calculate cascaded counter values
405 * that give desired conversion timing
406 * (pacer->next_div / pacer->next_div1)
408 comedi_8254_cascade_ns_to_timer(pacer, &convert_period,
410 /* transfer div2 value so correct timer gets updated */
411 pacer->next_div = pacer->next_div2;
412 labpc_set_ai_convert_period(cmd, mode, convert_period);
416 static enum scan_mode labpc_ai_scan_mode(const struct comedi_cmd *cmd)
421 if (cmd->chanlist_len == 1)
422 return MODE_SINGLE_CHAN;
424 /* chanlist may be NULL during cmdtest */
426 return MODE_MULT_CHAN_UP;
428 chan0 = CR_CHAN(cmd->chanlist[0]);
429 chan1 = CR_CHAN(cmd->chanlist[1]);
432 return MODE_MULT_CHAN_UP;
435 return MODE_MULT_CHAN_DOWN;
437 return MODE_SINGLE_CHAN_INTERVAL;
440 static int labpc_ai_check_chanlist(struct comedi_device *dev,
441 struct comedi_subdevice *s,
442 struct comedi_cmd *cmd)
444 enum scan_mode mode = labpc_ai_scan_mode(cmd);
445 unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
446 unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
447 unsigned int aref0 = CR_AREF(cmd->chanlist[0]);
450 for (i = 0; i < cmd->chanlist_len; i++) {
451 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
452 unsigned int range = CR_RANGE(cmd->chanlist[i]);
453 unsigned int aref = CR_AREF(cmd->chanlist[i]);
456 case MODE_SINGLE_CHAN:
458 case MODE_SINGLE_CHAN_INTERVAL:
460 dev_dbg(dev->class_dev,
461 "channel scanning order specified in chanlist is not supported by hardware\n");
465 case MODE_MULT_CHAN_UP:
467 dev_dbg(dev->class_dev,
468 "channel scanning order specified in chanlist is not supported by hardware\n");
472 case MODE_MULT_CHAN_DOWN:
473 if (chan != (cmd->chanlist_len - i - 1)) {
474 dev_dbg(dev->class_dev,
475 "channel scanning order specified in chanlist is not supported by hardware\n");
481 if (range != range0) {
482 dev_dbg(dev->class_dev,
483 "entries in chanlist must all have the same range\n");
488 dev_dbg(dev->class_dev,
489 "entries in chanlist must all have the same reference\n");
497 static int labpc_ai_cmdtest(struct comedi_device *dev,
498 struct comedi_subdevice *s, struct comedi_cmd *cmd)
500 const struct labpc_boardinfo *board = dev->board_ptr;
503 unsigned int stop_mask;
506 /* Step 1 : check if triggers are trivially valid */
508 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
509 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
510 TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT);
511 err |= comedi_check_trigger_src(&cmd->convert_src,
512 TRIG_TIMER | TRIG_EXT);
513 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
515 stop_mask = TRIG_COUNT | TRIG_NONE;
516 if (board->is_labpc1200)
517 stop_mask |= TRIG_EXT;
518 err |= comedi_check_trigger_src(&cmd->stop_src, stop_mask);
523 /* Step 2a : make sure trigger sources are unique */
525 err |= comedi_check_trigger_is_unique(cmd->start_src);
526 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
527 err |= comedi_check_trigger_is_unique(cmd->convert_src);
528 err |= comedi_check_trigger_is_unique(cmd->stop_src);
530 /* Step 2b : and mutually compatible */
532 /* can't have external stop and start triggers at once */
533 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
539 /* Step 3: check if arguments are trivially valid */
541 switch (cmd->start_src) {
543 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
546 /* start_arg value is ignored */
550 if (!cmd->chanlist_len)
552 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
555 if (cmd->convert_src == TRIG_TIMER) {
556 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
560 /* make sure scan timing is not too fast */
561 if (cmd->scan_begin_src == TRIG_TIMER) {
562 if (cmd->convert_src == TRIG_TIMER) {
563 err |= comedi_check_trigger_arg_min(&cmd->
568 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
573 switch (cmd->stop_src) {
575 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
578 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
581 * TRIG_EXT doesn't care since it doesn't
582 * trigger off a numbered channel
591 /* step 4: fix up any arguments */
593 tmp = cmd->convert_arg;
594 tmp2 = cmd->scan_begin_arg;
595 mode = labpc_ai_scan_mode(cmd);
596 labpc_adc_timing(dev, cmd, mode);
597 if (tmp != cmd->convert_arg || tmp2 != cmd->scan_begin_arg)
603 /* Step 5: check channel list if it exists */
604 if (cmd->chanlist && cmd->chanlist_len > 0)
605 err |= labpc_ai_check_chanlist(dev, s, cmd);
613 static int labpc_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
615 const struct labpc_boardinfo *board = dev->board_ptr;
616 struct labpc_private *devpriv = dev->private;
617 struct comedi_async *async = s->async;
618 struct comedi_cmd *cmd = &async->cmd;
619 enum scan_mode mode = labpc_ai_scan_mode(cmd);
620 unsigned int chanspec = (mode == MODE_MULT_CHAN_UP) ?
621 cmd->chanlist[cmd->chanlist_len - 1] :
623 unsigned int chan = CR_CHAN(chanspec);
624 unsigned int range = CR_RANGE(chanspec);
625 unsigned int aref = CR_AREF(chanspec);
626 enum transfer_type xfer;
629 /* make sure board is disabled before setting up acquisition */
630 labpc_cancel(dev, s);
632 /* initialize software conversion count */
633 if (cmd->stop_src == TRIG_COUNT)
634 devpriv->count = cmd->stop_arg * cmd->chanlist_len;
636 /* setup hardware conversion counter */
637 if (cmd->stop_src == TRIG_EXT) {
639 * load counter a1 with count of 3
640 * (pc+ manual says this is minimum allowed) using mode 0
642 comedi_8254_load(devpriv->counter, 1,
643 3, I8254_MODE0 | I8254_BINARY);
645 /* just put counter a1 in mode 0 to set its output low */
646 comedi_8254_set_mode(devpriv->counter, 1,
647 I8254_MODE0 | I8254_BINARY);
650 /* figure out what method we will use to transfer data */
652 (cmd->flags & (CMDF_WAKE_EOS | CMDF_PRIORITY)) == 0) {
654 * dma unsafe at RT priority,
655 * and too much setup time for CMDF_WAKE_EOS
657 xfer = isa_dma_transfer;
658 } else if (board->is_labpc1200 &&
659 (cmd->flags & CMDF_WAKE_EOS) == 0 &&
660 (cmd->stop_src != TRIG_COUNT || devpriv->count > 256)) {
662 * pc-plus has no fifo-half full interrupt
663 * wake-end-of-scan should interrupt on fifo not empty
664 * make sure we are taking more than just a few points
666 xfer = fifo_half_full_transfer;
668 xfer = fifo_not_empty_transfer;
670 devpriv->current_transfer = xfer;
672 labpc_ai_set_chan_and_gain(dev, mode, chan, range, aref);
674 labpc_setup_cmd6_reg(dev, s, mode, xfer, range, aref,
675 (cmd->stop_src == TRIG_EXT));
677 /* manual says to set scan enable bit on second pass */
678 if (mode == MODE_MULT_CHAN_UP || mode == MODE_MULT_CHAN_DOWN) {
679 devpriv->cmd1 |= CMD1_SCANEN;
681 * Need a brief delay before enabling scan, or scan
682 * list will get screwed when you switch between
683 * scan up to scan down mode - dunno why.
686 devpriv->write_byte(dev, devpriv->cmd1, CMD1_REG);
689 devpriv->write_byte(dev, cmd->chanlist_len, INTERVAL_COUNT_REG);
691 devpriv->write_byte(dev, 0x1, INTERVAL_STROBE_REG);
693 if (cmd->convert_src == TRIG_TIMER ||
694 cmd->scan_begin_src == TRIG_TIMER) {
695 struct comedi_8254 *pacer = dev->pacer;
696 struct comedi_8254 *counter = devpriv->counter;
698 comedi_8254_update_divisors(pacer);
701 comedi_8254_load(pacer, 0, pacer->divisor1,
702 I8254_MODE3 | I8254_BINARY);
704 /* set up conversion pacing */
705 comedi_8254_set_mode(counter, 0, I8254_MODE2 | I8254_BINARY);
706 if (labpc_ai_convert_period(cmd, mode))
707 comedi_8254_write(counter, 0, pacer->divisor);
709 /* set up scan pacing */
710 if (labpc_ai_scan_period(cmd, mode))
711 comedi_8254_load(pacer, 1, pacer->divisor2,
712 I8254_MODE2 | I8254_BINARY);
715 labpc_clear_adc_fifo(dev);
717 if (xfer == isa_dma_transfer)
718 labpc_setup_dma(dev, s);
720 /* enable error interrupts */
721 devpriv->cmd3 |= CMD3_ERRINTEN;
722 /* enable fifo not empty interrupt? */
723 if (xfer == fifo_not_empty_transfer)
724 devpriv->cmd3 |= CMD3_FIFOINTEN;
725 devpriv->write_byte(dev, devpriv->cmd3, CMD3_REG);
727 /* setup any external triggering/pacing (cmd4 register) */
729 if (cmd->convert_src != TRIG_EXT)
730 devpriv->cmd4 |= CMD4_ECLKRCV;
732 * XXX should discard first scan when using interval scanning
733 * since manual says it is not synced with scan clock.
735 if (!labpc_use_continuous_mode(cmd, mode)) {
736 devpriv->cmd4 |= CMD4_INTSCAN;
737 if (cmd->scan_begin_src == TRIG_EXT)
738 devpriv->cmd4 |= CMD4_EOIRCV;
740 /* single-ended/differential */
741 if (aref == AREF_DIFF)
742 devpriv->cmd4 |= CMD4_SEDIFF;
743 devpriv->write_byte(dev, devpriv->cmd4, CMD4_REG);
745 /* startup acquisition */
747 spin_lock_irqsave(&dev->spinlock, flags);
749 /* use 2 cascaded counters for pacing */
750 devpriv->cmd2 |= CMD2_TBSEL;
752 devpriv->cmd2 &= ~(CMD2_SWTRIG | CMD2_HWTRIG | CMD2_PRETRIG);
753 if (cmd->start_src == TRIG_EXT)
754 devpriv->cmd2 |= CMD2_HWTRIG;
756 devpriv->cmd2 |= CMD2_SWTRIG;
757 if (cmd->stop_src == TRIG_EXT)
758 devpriv->cmd2 |= (CMD2_HWTRIG | CMD2_PRETRIG);
760 devpriv->write_byte(dev, devpriv->cmd2, CMD2_REG);
762 spin_unlock_irqrestore(&dev->spinlock, flags);
767 /* read all available samples from ai fifo */
768 static int labpc_drain_fifo(struct comedi_device *dev)
770 struct labpc_private *devpriv = dev->private;
771 struct comedi_async *async = dev->read_subdev->async;
772 struct comedi_cmd *cmd = &async->cmd;
774 const int timeout = 10000;
777 devpriv->stat1 = devpriv->read_byte(dev, STAT1_REG);
779 for (i = 0; (devpriv->stat1 & STAT1_DAVAIL) && i < timeout;
781 /* quit if we have all the data we want */
782 if (cmd->stop_src == TRIG_COUNT) {
783 if (devpriv->count == 0)
787 data = labpc_read_adc_fifo(dev);
788 comedi_buf_write_samples(dev->read_subdev, &data, 1);
789 devpriv->stat1 = devpriv->read_byte(dev, STAT1_REG);
792 dev_err(dev->class_dev, "ai timeout, fifo never empties\n");
793 async->events |= COMEDI_CB_ERROR;
801 * Makes sure all data acquired by board is transferred to comedi (used
802 * when acquisition is terminated by stop_src == TRIG_EXT).
804 static void labpc_drain_dregs(struct comedi_device *dev)
806 struct labpc_private *devpriv = dev->private;
808 if (devpriv->current_transfer == isa_dma_transfer)
809 labpc_drain_dma(dev);
811 labpc_drain_fifo(dev);
814 /* interrupt service routine */
815 static irqreturn_t labpc_interrupt(int irq, void *d)
817 struct comedi_device *dev = d;
818 const struct labpc_boardinfo *board = dev->board_ptr;
819 struct labpc_private *devpriv = dev->private;
820 struct comedi_subdevice *s = dev->read_subdev;
821 struct comedi_async *async;
822 struct comedi_cmd *cmd;
824 if (!dev->attached) {
825 dev_err(dev->class_dev, "premature interrupt\n");
832 /* read board status */
833 devpriv->stat1 = devpriv->read_byte(dev, STAT1_REG);
834 if (board->is_labpc1200)
835 devpriv->stat2 = devpriv->read_byte(dev, STAT2_REG);
837 if ((devpriv->stat1 & (STAT1_GATA0 | STAT1_CNTINT | STAT1_OVERFLOW |
838 STAT1_OVERRUN | STAT1_DAVAIL)) == 0 &&
839 (devpriv->stat2 & STAT2_OUTA1) == 0 &&
840 (devpriv->stat2 & STAT2_FIFONHF)) {
844 if (devpriv->stat1 & STAT1_OVERRUN) {
845 /* clear error interrupt */
846 devpriv->write_byte(dev, 0x1, ADC_FIFO_CLEAR_REG);
847 async->events |= COMEDI_CB_ERROR;
848 comedi_handle_events(dev, s);
849 dev_err(dev->class_dev, "overrun\n");
853 if (devpriv->current_transfer == isa_dma_transfer)
854 labpc_handle_dma_status(dev);
856 labpc_drain_fifo(dev);
858 if (devpriv->stat1 & STAT1_CNTINT) {
859 dev_err(dev->class_dev, "handled timer interrupt?\n");
861 devpriv->write_byte(dev, 0x1, TIMER_CLEAR_REG);
864 if (devpriv->stat1 & STAT1_OVERFLOW) {
865 /* clear error interrupt */
866 devpriv->write_byte(dev, 0x1, ADC_FIFO_CLEAR_REG);
867 async->events |= COMEDI_CB_ERROR;
868 comedi_handle_events(dev, s);
869 dev_err(dev->class_dev, "overflow\n");
872 /* handle external stop trigger */
873 if (cmd->stop_src == TRIG_EXT) {
874 if (devpriv->stat2 & STAT2_OUTA1) {
875 labpc_drain_dregs(dev);
876 async->events |= COMEDI_CB_EOA;
880 /* TRIG_COUNT end of acquisition */
881 if (cmd->stop_src == TRIG_COUNT) {
882 if (devpriv->count == 0)
883 async->events |= COMEDI_CB_EOA;
886 comedi_handle_events(dev, s);
890 static void labpc_ao_write(struct comedi_device *dev,
891 struct comedi_subdevice *s,
892 unsigned int chan, unsigned int val)
894 struct labpc_private *devpriv = dev->private;
896 devpriv->write_byte(dev, val & 0xff, DAC_LSB_REG(chan));
897 devpriv->write_byte(dev, (val >> 8) & 0xff, DAC_MSB_REG(chan));
899 s->readback[chan] = val;
902 static int labpc_ao_insn_write(struct comedi_device *dev,
903 struct comedi_subdevice *s,
904 struct comedi_insn *insn,
907 const struct labpc_boardinfo *board = dev->board_ptr;
908 struct labpc_private *devpriv = dev->private;
909 unsigned int channel;
914 channel = CR_CHAN(insn->chanspec);
917 * Turn off pacing of analog output channel.
918 * NOTE: hardware bug in daqcard-1200 means pacing cannot
919 * be independently enabled/disabled for its the two channels.
921 spin_lock_irqsave(&dev->spinlock, flags);
922 devpriv->cmd2 &= ~CMD2_LDAC(channel);
923 devpriv->write_byte(dev, devpriv->cmd2, CMD2_REG);
924 spin_unlock_irqrestore(&dev->spinlock, flags);
927 if (board->is_labpc1200) {
928 range = CR_RANGE(insn->chanspec);
929 if (comedi_range_is_unipolar(s, range))
930 devpriv->cmd6 |= CMD6_DACUNI(channel);
932 devpriv->cmd6 &= ~CMD6_DACUNI(channel);
933 /* write to register */
934 devpriv->write_byte(dev, devpriv->cmd6, CMD6_REG);
937 for (i = 0; i < insn->n; i++)
938 labpc_ao_write(dev, s, channel, data[i]);
943 /* lowlevel write to eeprom/dac */
944 static void labpc_serial_out(struct comedi_device *dev, unsigned int value,
945 unsigned int value_width)
947 struct labpc_private *devpriv = dev->private;
950 for (i = 1; i <= value_width; i++) {
951 /* clear serial clock */
952 devpriv->cmd5 &= ~CMD5_SCLK;
953 /* send bits most significant bit first */
954 if (value & (1 << (value_width - i)))
955 devpriv->cmd5 |= CMD5_SDATA;
957 devpriv->cmd5 &= ~CMD5_SDATA;
959 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
960 /* set clock to load bit */
961 devpriv->cmd5 |= CMD5_SCLK;
963 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
967 /* lowlevel read from eeprom */
968 static unsigned int labpc_serial_in(struct comedi_device *dev)
970 struct labpc_private *devpriv = dev->private;
971 unsigned int value = 0;
973 const int value_width = 8; /* number of bits wide values are */
975 for (i = 1; i <= value_width; i++) {
976 /* set serial clock */
977 devpriv->cmd5 |= CMD5_SCLK;
979 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
980 /* clear clock bit */
981 devpriv->cmd5 &= ~CMD5_SCLK;
983 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
984 /* read bits most significant bit first */
986 devpriv->stat2 = devpriv->read_byte(dev, STAT2_REG);
987 if (devpriv->stat2 & STAT2_PROMOUT)
988 value |= 1 << (value_width - i);
994 static unsigned int labpc_eeprom_read(struct comedi_device *dev,
995 unsigned int address)
997 struct labpc_private *devpriv = dev->private;
999 /* bits to tell eeprom to expect a read */
1000 const int read_instruction = 0x3;
1001 /* 8 bit write lengths to eeprom */
1002 const int write_length = 8;
1004 /* enable read/write to eeprom */
1005 devpriv->cmd5 &= ~CMD5_EEPROMCS;
1007 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1008 devpriv->cmd5 |= (CMD5_EEPROMCS | CMD5_WRTPRT);
1010 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1012 /* send read instruction */
1013 labpc_serial_out(dev, read_instruction, write_length);
1014 /* send 8 bit address to read from */
1015 labpc_serial_out(dev, address, write_length);
1017 value = labpc_serial_in(dev);
1019 /* disable read/write to eeprom */
1020 devpriv->cmd5 &= ~(CMD5_EEPROMCS | CMD5_WRTPRT);
1022 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1027 static unsigned int labpc_eeprom_read_status(struct comedi_device *dev)
1029 struct labpc_private *devpriv = dev->private;
1031 const int read_status_instruction = 0x5;
1032 const int write_length = 8; /* 8 bit write lengths to eeprom */
1034 /* enable read/write to eeprom */
1035 devpriv->cmd5 &= ~CMD5_EEPROMCS;
1037 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1038 devpriv->cmd5 |= (CMD5_EEPROMCS | CMD5_WRTPRT);
1040 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1042 /* send read status instruction */
1043 labpc_serial_out(dev, read_status_instruction, write_length);
1045 value = labpc_serial_in(dev);
1047 /* disable read/write to eeprom */
1048 devpriv->cmd5 &= ~(CMD5_EEPROMCS | CMD5_WRTPRT);
1050 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1055 static void labpc_eeprom_write(struct comedi_device *dev,
1056 unsigned int address, unsigned int value)
1058 struct labpc_private *devpriv = dev->private;
1059 const int write_enable_instruction = 0x6;
1060 const int write_instruction = 0x2;
1061 const int write_length = 8; /* 8 bit write lengths to eeprom */
1063 /* enable read/write to eeprom */
1064 devpriv->cmd5 &= ~CMD5_EEPROMCS;
1066 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1067 devpriv->cmd5 |= (CMD5_EEPROMCS | CMD5_WRTPRT);
1069 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1071 /* send write_enable instruction */
1072 labpc_serial_out(dev, write_enable_instruction, write_length);
1073 devpriv->cmd5 &= ~CMD5_EEPROMCS;
1075 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1077 /* send write instruction */
1078 devpriv->cmd5 |= CMD5_EEPROMCS;
1080 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1081 labpc_serial_out(dev, write_instruction, write_length);
1082 /* send 8 bit address to write to */
1083 labpc_serial_out(dev, address, write_length);
1085 labpc_serial_out(dev, value, write_length);
1086 devpriv->cmd5 &= ~CMD5_EEPROMCS;
1088 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1090 /* disable read/write to eeprom */
1091 devpriv->cmd5 &= ~(CMD5_EEPROMCS | CMD5_WRTPRT);
1093 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1096 /* writes to 8 bit calibration dacs */
1097 static void write_caldac(struct comedi_device *dev, unsigned int channel,
1100 struct labpc_private *devpriv = dev->private;
1102 /* clear caldac load bit and make sure we don't write to eeprom */
1103 devpriv->cmd5 &= ~(CMD5_CALDACLD | CMD5_EEPROMCS | CMD5_WRTPRT);
1105 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1107 /* write 4 bit channel */
1108 labpc_serial_out(dev, channel, 4);
1109 /* write 8 bit caldac value */
1110 labpc_serial_out(dev, value, 8);
1112 /* set and clear caldac bit to load caldac value */
1113 devpriv->cmd5 |= CMD5_CALDACLD;
1115 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1116 devpriv->cmd5 &= ~CMD5_CALDACLD;
1118 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1121 static int labpc_calib_insn_write(struct comedi_device *dev,
1122 struct comedi_subdevice *s,
1123 struct comedi_insn *insn,
1126 unsigned int chan = CR_CHAN(insn->chanspec);
1129 * Only write the last data value to the caldac. Preceding
1130 * data would be overwritten anyway.
1133 unsigned int val = data[insn->n - 1];
1135 if (s->readback[chan] != val) {
1136 write_caldac(dev, chan, val);
1137 s->readback[chan] = val;
1144 static int labpc_eeprom_ready(struct comedi_device *dev,
1145 struct comedi_subdevice *s,
1146 struct comedi_insn *insn,
1147 unsigned long context)
1149 unsigned int status;
1151 /* make sure there isn't already a write in progress */
1152 status = labpc_eeprom_read_status(dev);
1153 if ((status & 0x1) == 0)
1158 static int labpc_eeprom_insn_write(struct comedi_device *dev,
1159 struct comedi_subdevice *s,
1160 struct comedi_insn *insn,
1163 unsigned int chan = CR_CHAN(insn->chanspec);
1166 /* only allow writes to user area of eeprom */
1167 if (chan < 16 || chan > 127)
1171 * Only write the last data value to the eeprom. Preceding
1172 * data would be overwritten anyway.
1175 unsigned int val = data[insn->n - 1];
1177 ret = comedi_timeout(dev, s, insn, labpc_eeprom_ready, 0);
1181 labpc_eeprom_write(dev, chan, val);
1182 s->readback[chan] = val;
1188 int labpc_common_attach(struct comedi_device *dev,
1189 unsigned int irq, unsigned long isr_flags)
1191 const struct labpc_boardinfo *board = dev->board_ptr;
1192 struct labpc_private *devpriv;
1193 struct comedi_subdevice *s;
1197 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1202 devpriv->read_byte = labpc_readb;
1203 devpriv->write_byte = labpc_writeb;
1205 devpriv->read_byte = labpc_inb;
1206 devpriv->write_byte = labpc_outb;
1209 /* initialize board's command registers */
1210 devpriv->write_byte(dev, devpriv->cmd1, CMD1_REG);
1211 devpriv->write_byte(dev, devpriv->cmd2, CMD2_REG);
1212 devpriv->write_byte(dev, devpriv->cmd3, CMD3_REG);
1213 devpriv->write_byte(dev, devpriv->cmd4, CMD4_REG);
1214 if (board->is_labpc1200) {
1215 devpriv->write_byte(dev, devpriv->cmd5, CMD5_REG);
1216 devpriv->write_byte(dev, devpriv->cmd6, CMD6_REG);
1220 ret = request_irq(irq, labpc_interrupt, isr_flags,
1221 dev->board_name, dev);
1227 dev->pacer = comedi_8254_mm_init(dev->mmio + COUNTER_B_BASE_REG,
1228 I8254_OSC_BASE_2MHZ,
1230 devpriv->counter = comedi_8254_mm_init(dev->mmio +
1232 I8254_OSC_BASE_2MHZ,
1235 dev->pacer = comedi_8254_init(dev->iobase + COUNTER_B_BASE_REG,
1236 I8254_OSC_BASE_2MHZ,
1238 devpriv->counter = comedi_8254_init(dev->iobase +
1240 I8254_OSC_BASE_2MHZ,
1243 if (!dev->pacer || !devpriv->counter)
1246 ret = comedi_alloc_subdevices(dev, 5);
1250 /* analog input subdevice */
1251 s = &dev->subdevices[0];
1252 s->type = COMEDI_SUBD_AI;
1253 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_COMMON | SDF_DIFF;
1255 s->len_chanlist = 8;
1256 s->maxdata = 0x0fff;
1257 s->range_table = board->is_labpc1200 ?
1258 &range_labpc_1200_ai : &range_labpc_plus_ai;
1259 s->insn_read = labpc_ai_insn_read;
1261 dev->read_subdev = s;
1262 s->subdev_flags |= SDF_CMD_READ;
1263 s->do_cmd = labpc_ai_cmd;
1264 s->do_cmdtest = labpc_ai_cmdtest;
1265 s->cancel = labpc_cancel;
1269 s = &dev->subdevices[1];
1270 if (board->has_ao) {
1271 s->type = COMEDI_SUBD_AO;
1272 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
1274 s->maxdata = 0x0fff;
1275 s->range_table = &range_labpc_ao;
1276 s->insn_write = labpc_ao_insn_write;
1278 ret = comedi_alloc_subdev_readback(s);
1282 /* initialize analog outputs to a known value */
1283 for (i = 0; i < s->n_chan; i++)
1284 labpc_ao_write(dev, s, i, s->maxdata / 2);
1286 s->type = COMEDI_SUBD_UNUSED;
1290 s = &dev->subdevices[2];
1292 ret = subdev_8255_mm_init(dev, s, NULL, DIO_BASE_REG);
1294 ret = subdev_8255_init(dev, s, NULL, DIO_BASE_REG);
1298 /* calibration subdevices for boards that have one */
1299 s = &dev->subdevices[3];
1300 if (board->is_labpc1200) {
1301 s->type = COMEDI_SUBD_CALIB;
1302 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
1305 s->insn_write = labpc_calib_insn_write;
1307 ret = comedi_alloc_subdev_readback(s);
1311 for (i = 0; i < s->n_chan; i++) {
1312 write_caldac(dev, i, s->maxdata / 2);
1313 s->readback[i] = s->maxdata / 2;
1316 s->type = COMEDI_SUBD_UNUSED;
1319 /* EEPROM (256 bytes) */
1320 s = &dev->subdevices[4];
1321 if (board->is_labpc1200) {
1322 s->type = COMEDI_SUBD_MEMORY;
1323 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
1326 s->insn_write = labpc_eeprom_insn_write;
1328 ret = comedi_alloc_subdev_readback(s);
1332 for (i = 0; i < s->n_chan; i++)
1333 s->readback[i] = labpc_eeprom_read(dev, i);
1335 s->type = COMEDI_SUBD_UNUSED;
1340 EXPORT_SYMBOL_GPL(labpc_common_attach);
1342 void labpc_common_detach(struct comedi_device *dev)
1344 struct labpc_private *devpriv = dev->private;
1347 kfree(devpriv->counter);
1349 EXPORT_SYMBOL_GPL(labpc_common_detach);
1351 static int __init labpc_common_init(void)
1355 module_init(labpc_common_init);
1357 static void __exit labpc_common_exit(void)
1360 module_exit(labpc_common_exit);
1362 MODULE_AUTHOR("Comedi http://www.comedi.org");
1363 MODULE_DESCRIPTION("Comedi helper for ni_labpc, ni_labpc_pci, ni_labpc_cs");
1364 MODULE_LICENSE("GPL");