2 * comedi/drivers/pcl812.c
4 * Author: Michal Dobes <dobes@tesnet.cz>
6 * hardware driver for Advantech cards
7 * card: PCL-812, PCL-812PG, PCL-813, PCL-813B
8 * driver: pcl812, pcl812pg, pcl813, pcl813b
10 * card: ACL-8112DG, ACL-8112HG, ACL-8112PG, ACL-8113, ACL-8216
11 * driver: acl8112dg, acl8112hg, acl8112pg, acl8113, acl8216
12 * and for ICP DAS cards
13 * card: ISO-813, A-821PGH, A-821PGL, A-821PGL-NDA, A-822PGH, A-822PGL,
14 * driver: iso813, a821pgh, a-821pgl, a-821pglnda, a822pgh, a822pgl,
15 * card: A-823PGH, A-823PGL, A-826PG
16 * driver: a823pgh, a823pgl, a826pg
21 * Description: Advantech PCL-812/PG, PCL-813/B,
22 * ADLink ACL-8112DG/HG/PG, ACL-8113, ACL-8216,
23 * ICP DAS A-821PGH/PGL/PGL-NDA, A-822PGH/PGL, A-823PGH/PGL, A-826PG,
25 * Author: Michal Dobes <dobes@tesnet.cz>
26 * Devices: [Advantech] PCL-812 (pcl812), PCL-812PG (pcl812pg),
27 * PCL-813 (pcl813), PCL-813B (pcl813b), [ADLink] ACL-8112DG (acl8112dg),
28 * ACL-8112HG (acl8112hg), ACL-8113 (acl-8113), ACL-8216 (acl8216),
29 * [ICP] ISO-813 (iso813), A-821PGH (a821pgh), A-821PGL (a821pgl),
30 * A-821PGL-NDA (a821pclnda), A-822PGH (a822pgh), A-822PGL (a822pgl),
31 * A-823PGH (a823pgh), A-823PGL (a823pgl), A-826PG (a826pg)
32 * Updated: Mon, 06 Aug 2007 12:03:15 +0100
33 * Status: works (I hope. My board fire up under my hands
34 * and I cann't test all features.)
36 * This driver supports insn and cmd interfaces. Some boards support only insn
37 * because their hardware don't allow more (PCL-813/B, ACL-8113, ISO-813).
38 * Data transfer over DMA is supported only when you measure only one
39 * channel, this is too hardware limitation of these boards.
41 * Options for PCL-812:
43 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7; 10, 11, 12, 14, 15)
44 * [2] - DMA (0=disable, 1, 3)
45 * [3] - 0=trigger source is internal 8253 with 2MHz clock
46 * 1=trigger source is external
47 * [4] - 0=A/D input range is +/-10V
48 * 1=A/D input range is +/-5V
49 * 2=A/D input range is +/-2.5V
50 * 3=A/D input range is +/-1.25V
51 * 4=A/D input range is +/-0.625V
52 * 5=A/D input range is +/-0.3125V
53 * [5] - 0=D/A outputs 0-5V (internal reference -5V)
54 * 1=D/A outputs 0-10V (internal reference -10V)
55 * 2=D/A outputs unknown (external reference)
57 * Options for PCL-812PG, ACL-8112PG:
59 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7; 10, 11, 12, 14, 15)
60 * [2] - DMA (0=disable, 1, 3)
61 * [3] - 0=trigger source is internal 8253 with 2MHz clock
62 * 1=trigger source is external
63 * [4] - 0=A/D have max +/-5V input
64 * 1=A/D have max +/-10V input
65 * [5] - 0=D/A outputs 0-5V (internal reference -5V)
66 * 1=D/A outputs 0-10V (internal reference -10V)
67 * 2=D/A outputs unknown (external reference)
69 * Options for ACL-8112DG/HG, A-822PGL/PGH, A-823PGL/PGH, ACL-8216, A-826PG:
71 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7; 10, 11, 12, 14, 15)
72 * [2] - DMA (0=disable, 1, 3)
73 * [3] - 0=trigger source is internal 8253 with 2MHz clock
74 * 1=trigger source is external
75 * [4] - 0=A/D channels are S.E.
76 * 1=A/D channels are DIFF
77 * [5] - 0=D/A outputs 0-5V (internal reference -5V)
78 * 1=D/A outputs 0-10V (internal reference -10V)
79 * 2=D/A outputs unknown (external reference)
81 * Options for A-821PGL/PGH:
83 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7)
84 * [2] - 0=A/D channels are S.E.
85 * 1=A/D channels are DIFF
86 * [3] - 0=D/A output 0-5V (internal reference -5V)
87 * 1=D/A output 0-10V (internal reference -10V)
89 * Options for A-821PGL-NDA:
91 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7)
92 * [2] - 0=A/D channels are S.E.
93 * 1=A/D channels are DIFF
95 * Options for PCL-813:
98 * Options for PCL-813B:
100 * [1] - 0= bipolar inputs
103 * Options for ACL-8113, ISO-813:
105 * [1] - 0= 10V bipolar inputs
106 * 1= 10V unipolar inputs
107 * 2= 20V bipolar inputs
108 * 3= 20V unipolar inputs
111 #include <linux/module.h>
112 #include <linux/interrupt.h>
113 #include <linux/gfp.h>
114 #include <linux/delay.h>
115 #include <linux/io.h>
117 #include "../comedidev.h"
119 #include "comedi_isadma.h"
120 #include "comedi_fc.h"
123 /* hardware types of the cards */
124 #define boardPCL812PG 0 /* and ACL-8112PG */
125 #define boardPCL813B 1
126 #define boardPCL812 2
127 #define boardPCL813 3
128 #define boardISO813 5
129 #define boardACL8113 6
130 #define boardACL8112 7 /* ACL-8112DG/HG, A-822PGL/PGH, A-823PGL/PGH */
131 #define boardACL8216 8 /* and ICP DAS A-826PG */
132 #define boardA821 9 /* PGH, PGL, PGL/NDA versions */
137 #define PCL812_TIMER_BASE 0x00
138 #define PCL812_AI_LSB_REG 0x04
139 #define PCL812_AI_MSB_REG 0x05
140 #define PCL812_AI_MSB_DRDY (1 << 4)
141 #define PCL812_AO_LSB_REG(x) (0x04 + ((x) * 2))
142 #define PCL812_AO_MSB_REG(x) (0x05 + ((x) * 2))
143 #define PCL812_DI_LSB_REG 0x06
144 #define PCL812_DI_MSB_REG 0x07
145 #define PCL812_STATUS_REG 0x08
146 #define PCL812_STATUS_DRDY (1 << 5)
147 #define PCL812_RANGE_REG 0x09
148 #define PCL812_MUX_REG 0x0a
149 #define PCL812_MUX_CHAN(x) ((x) << 0)
150 #define PCL812_MUX_CS0 (1 << 4)
151 #define PCL812_MUX_CS1 (1 << 5)
152 #define PCL812_CTRL_REG 0x0b
153 #define PCL812_CTRL_DISABLE_TRIG (0 << 0)
154 #define PCL812_CTRL_SOFT_TRIG (1 << 0)
155 #define PCL812_CTRL_PACER_DMA_TRIG (2 << 0)
156 #define PCL812_CTRL_PACER_EOC_TRIG (6 << 0)
157 #define PCL812_SOFTTRIG_REG 0x0c
158 #define PCL812_DO_LSB_REG 0x0d
159 #define PCL812_DO_MSB_REG 0x0e
161 #define MAX_CHANLIST_LEN 256 /* length of scan list */
163 static const struct comedi_lrange range_pcl812pg_ai = {
173 static const struct comedi_lrange range_pcl812pg2_ai = {
183 static const struct comedi_lrange range812_bipolar1_25 = {
189 static const struct comedi_lrange range812_bipolar0_625 = {
195 static const struct comedi_lrange range812_bipolar0_3125 = {
201 static const struct comedi_lrange range_pcl813b_ai = {
210 static const struct comedi_lrange range_pcl813b2_ai = {
219 static const struct comedi_lrange range_iso813_1_ai = {
229 static const struct comedi_lrange range_iso813_1_2_ai = {
239 static const struct comedi_lrange range_iso813_2_ai = {
248 static const struct comedi_lrange range_iso813_2_2_ai = {
257 static const struct comedi_lrange range_acl8113_1_ai = {
266 static const struct comedi_lrange range_acl8113_1_2_ai = {
275 static const struct comedi_lrange range_acl8113_2_ai = {
283 static const struct comedi_lrange range_acl8113_2_2_ai = {
291 static const struct comedi_lrange range_acl8112dg_ai = {
305 static const struct comedi_lrange range_acl8112hg_ai = {
322 static const struct comedi_lrange range_a821pgh_ai = {
331 struct pcl812_board {
336 unsigned int ai_ns_min;
337 const struct comedi_lrange *rangelist_ai;
338 unsigned int IRQbits;
339 unsigned int has_dma:1;
340 unsigned int has_16bit_ai:1;
341 unsigned int has_mpc508_mux:1;
342 unsigned int has_dio:1;
345 static const struct pcl812_board boardtypes[] = {
348 .board_type = boardPCL812,
352 .rangelist_ai = &range_bipolar10,
358 .board_type = boardPCL812PG,
362 .rangelist_ai = &range_pcl812pg_ai,
368 .board_type = boardPCL812PG,
372 .rangelist_ai = &range_pcl812pg_ai,
378 .board_type = boardACL8112,
379 .n_aichan = 16, /* 8 differential */
382 .rangelist_ai = &range_acl8112dg_ai,
389 .board_type = boardACL8112,
390 .n_aichan = 16, /* 8 differential */
393 .rangelist_ai = &range_acl8112hg_ai,
400 .board_type = boardA821,
401 .n_aichan = 16, /* 8 differential */
404 .rangelist_ai = &range_pcl813b_ai,
408 .name = "a821pglnda",
409 .board_type = boardA821,
410 .n_aichan = 16, /* 8 differential */
412 .rangelist_ai = &range_pcl813b_ai,
416 .board_type = boardA821,
417 .n_aichan = 16, /* 8 differential */
420 .rangelist_ai = &range_a821pgh_ai,
425 .board_type = boardACL8112,
426 .n_aichan = 16, /* 8 differential */
429 .rangelist_ai = &range_acl8112dg_ai,
435 .board_type = boardACL8112,
436 .n_aichan = 16, /* 8 differential */
439 .rangelist_ai = &range_acl8112hg_ai,
445 .board_type = boardACL8112,
446 .n_aichan = 16, /* 8 differential */
449 .rangelist_ai = &range_acl8112dg_ai,
455 .board_type = boardACL8112,
456 .n_aichan = 16, /* 8 differential */
459 .rangelist_ai = &range_acl8112hg_ai,
465 .board_type = boardPCL813,
467 .rangelist_ai = &range_pcl813b_ai,
470 .board_type = boardPCL813B,
472 .rangelist_ai = &range_pcl813b_ai,
475 .board_type = boardACL8113,
477 .rangelist_ai = &range_acl8113_1_ai,
480 .board_type = boardISO813,
482 .rangelist_ai = &range_iso813_1_ai,
485 .board_type = boardACL8216,
486 .n_aichan = 16, /* 8 differential */
489 .rangelist_ai = &range_pcl813b2_ai,
497 .board_type = boardACL8216,
498 .n_aichan = 16, /* 8 differential */
501 .rangelist_ai = &range_pcl813b2_ai,
509 struct pcl812_private {
510 struct comedi_isadma *dma;
511 unsigned char range_correction; /* =1 we must add 1 to range number */
512 unsigned int last_ai_chanspec;
513 unsigned char mode_reg_int; /* there is stored INT number for some card */
514 unsigned int ai_poll_ptr; /* how many sampes transfer poll */
515 unsigned int dma_runs_to_end; /* how many times we must switch DMA buffers */
516 unsigned int last_dma_run; /* how many bytes to transfer on last DMA buffer */
517 unsigned int max_812_ai_mode0_rangewait; /* setling time for gain */
518 unsigned int divisor1;
519 unsigned int divisor2;
520 unsigned int use_diff:1;
521 unsigned int use_mpc508:1;
522 unsigned int use_ext_trg:1;
523 unsigned int ai_dma:1;
524 unsigned int ai_eos:1;
527 static void pcl812_start_pacer(struct comedi_device *dev, bool load_timers)
529 struct pcl812_private *devpriv = dev->private;
530 unsigned long timer_base = dev->iobase + PCL812_TIMER_BASE;
532 i8254_set_mode(timer_base, 0, 2, I8254_MODE2 | I8254_BINARY);
533 i8254_set_mode(timer_base, 0, 1, I8254_MODE2 | I8254_BINARY);
537 i8254_write(timer_base, 0, 2, devpriv->divisor2);
538 i8254_write(timer_base, 0, 1, devpriv->divisor1);
542 static void pcl812_ai_setup_dma(struct comedi_device *dev,
543 struct comedi_subdevice *s)
545 struct pcl812_private *devpriv = dev->private;
546 struct comedi_isadma *dma = devpriv->dma;
547 struct comedi_isadma_desc *desc0 = &dma->desc[0];
548 struct comedi_isadma_desc *desc1 = &dma->desc[1];
549 struct comedi_cmd *cmd = &s->async->cmd;
552 /* we use EOS, so adapt DMA buffer to one scan */
553 if (devpriv->ai_eos) {
554 desc0->size = comedi_bytes_per_scan(s);
555 desc1->size = comedi_bytes_per_scan(s);
556 devpriv->dma_runs_to_end = 1;
558 desc0->size = desc0->maxsize;
559 desc1->size = desc1->maxsize;
560 if (s->async->prealloc_bufsz < desc0->maxsize)
561 desc0->size = s->async->prealloc_bufsz;
562 if (s->async->prealloc_bufsz < desc1->maxsize)
563 desc1->size = s->async->prealloc_bufsz;
564 if (cmd->stop_src == TRIG_NONE) {
565 devpriv->dma_runs_to_end = 1;
567 /* how many samples we must transfer? */
568 bytes = cmd->stop_arg * comedi_bytes_per_scan(s);
570 /* how many DMA pages we must fill */
571 devpriv->dma_runs_to_end = bytes / desc0->size;
573 /* on last dma transfer must be moved */
574 devpriv->last_dma_run = bytes % desc0->size;
575 if (devpriv->dma_runs_to_end == 0)
576 desc0->size = devpriv->last_dma_run;
577 devpriv->dma_runs_to_end--;
580 if (desc0->size > desc0->maxsize) {
581 desc0->size = desc0->maxsize;
584 if (desc1->size > desc1->maxsize) {
585 desc1->size = desc1->maxsize;
590 comedi_isadma_program(desc0);
593 static void pcl812_ai_setup_next_dma(struct comedi_device *dev,
594 struct comedi_subdevice *s)
596 struct pcl812_private *devpriv = dev->private;
597 struct comedi_isadma *dma = devpriv->dma;
598 struct comedi_isadma_desc *desc;
600 comedi_isadma_disable(dma->chan);
601 dma->cur_dma = 1 - dma->cur_dma;
603 desc = &dma->desc[dma->cur_dma];
604 if (!devpriv->ai_eos) {
605 if (devpriv->dma_runs_to_end)
606 devpriv->dma_runs_to_end--;
608 desc->size = devpriv->last_dma_run;
611 comedi_isadma_program(desc);
614 static void pcl812_ai_set_chan_range(struct comedi_device *dev,
615 unsigned int chanspec, char wait)
617 struct pcl812_private *devpriv = dev->private;
618 unsigned int chan = CR_CHAN(chanspec);
619 unsigned int range = CR_RANGE(chanspec);
620 unsigned int mux = 0;
622 if (chanspec == devpriv->last_ai_chanspec)
625 devpriv->last_ai_chanspec = chanspec;
627 if (devpriv->use_mpc508) {
628 if (devpriv->use_diff) {
629 mux |= PCL812_MUX_CS0 | PCL812_MUX_CS1;
632 mux |= PCL812_MUX_CS0;
634 mux |= PCL812_MUX_CS1;
638 outb(mux | PCL812_MUX_CHAN(chan), dev->iobase + PCL812_MUX_REG);
639 outb(range + devpriv->range_correction, dev->iobase + PCL812_RANGE_REG);
643 * XXX this depends on selected range and can be very long for
644 * some high gain ranges!
646 udelay(devpriv->max_812_ai_mode0_rangewait);
649 static void pcl812_ai_clear_eoc(struct comedi_device *dev)
651 /* writing any value clears the interrupt request */
652 outb(0, dev->iobase + PCL812_STATUS_REG);
655 static void pcl812_ai_soft_trig(struct comedi_device *dev)
657 /* writing any value triggers a software conversion */
658 outb(255, dev->iobase + PCL812_SOFTTRIG_REG);
661 static unsigned int pcl812_ai_get_sample(struct comedi_device *dev,
662 struct comedi_subdevice *s)
666 val = inb(dev->iobase + PCL812_AI_MSB_REG) << 8;
667 val |= inb(dev->iobase + PCL812_AI_LSB_REG);
669 return val & s->maxdata;
672 static int pcl812_ai_eoc(struct comedi_device *dev,
673 struct comedi_subdevice *s,
674 struct comedi_insn *insn,
675 unsigned long context)
679 if (s->maxdata > 0x0fff) {
680 status = inb(dev->iobase + PCL812_STATUS_REG);
681 if ((status & PCL812_STATUS_DRDY) == 0)
684 status = inb(dev->iobase + PCL812_AI_MSB_REG);
685 if ((status & PCL812_AI_MSB_DRDY) == 0)
691 static int pcl812_ai_cmdtest(struct comedi_device *dev,
692 struct comedi_subdevice *s, struct comedi_cmd *cmd)
694 const struct pcl812_board *board = dev->board_ptr;
695 struct pcl812_private *devpriv = dev->private;
700 /* Step 1 : check if triggers are trivially valid */
702 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
703 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
705 if (devpriv->use_ext_trg)
709 err |= cfc_check_trigger_src(&cmd->convert_src, flags);
711 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
712 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
717 /* Step 2a : make sure trigger sources are unique */
719 err |= cfc_check_trigger_is_unique(cmd->stop_src);
721 /* Step 2b : and mutually compatible */
726 /* Step 3: check if arguments are trivially valid */
728 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
729 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
731 if (cmd->convert_src == TRIG_TIMER)
732 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
735 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
737 err |= cfc_check_trigger_arg_min(&cmd->chanlist_len, 1);
738 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
740 if (cmd->stop_src == TRIG_COUNT)
741 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
743 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
748 /* step 4: fix up any arguments */
750 if (cmd->convert_src == TRIG_TIMER) {
751 arg = cmd->convert_arg;
752 i8253_cascade_ns_to_timer(I8254_OSC_BASE_2MHZ,
756 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, arg);
765 static int pcl812_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
767 struct pcl812_private *devpriv = dev->private;
768 struct comedi_cmd *cmd = &s->async->cmd;
769 unsigned int ctrl = 0;
772 pcl812_start_pacer(dev, false);
774 pcl812_ai_set_chan_range(dev, cmd->chanlist[0], 1);
776 if (devpriv->dma) { /* check if we can use DMA transfer */
778 for (i = 1; i < cmd->chanlist_len; i++)
779 if (cmd->chanlist[0] != cmd->chanlist[i]) {
780 /* we cann't use DMA :-( */
788 devpriv->ai_poll_ptr = 0;
790 /* don't we want wake up every scan? */
791 if (cmd->flags & CMDF_WAKE_EOS) {
794 /* DMA is useless for this situation */
795 if (cmd->chanlist_len == 1)
800 pcl812_ai_setup_dma(dev, s);
802 switch (cmd->convert_src) {
804 pcl812_start_pacer(dev, true);
809 ctrl |= PCL812_CTRL_PACER_DMA_TRIG;
811 ctrl |= PCL812_CTRL_PACER_EOC_TRIG;
812 outb(devpriv->mode_reg_int | ctrl, dev->iobase + PCL812_CTRL_REG);
817 static bool pcl812_ai_next_chan(struct comedi_device *dev,
818 struct comedi_subdevice *s)
820 struct comedi_cmd *cmd = &s->async->cmd;
822 if (cmd->stop_src == TRIG_COUNT &&
823 s->async->scans_done >= cmd->stop_arg) {
824 s->async->events |= COMEDI_CB_EOA;
831 static void pcl812_handle_eoc(struct comedi_device *dev,
832 struct comedi_subdevice *s)
834 struct comedi_cmd *cmd = &s->async->cmd;
835 unsigned int chan = s->async->cur_chan;
836 unsigned int next_chan;
839 if (pcl812_ai_eoc(dev, s, NULL, 0)) {
840 dev_dbg(dev->class_dev, "A/D cmd IRQ without DRDY!\n");
841 s->async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
845 val = pcl812_ai_get_sample(dev, s);
846 comedi_buf_write_samples(s, &val, 1);
848 /* Set up next channel. Added by abbotti 2010-01-20, but untested. */
849 next_chan = s->async->cur_chan;
850 if (cmd->chanlist[chan] != cmd->chanlist[next_chan])
851 pcl812_ai_set_chan_range(dev, cmd->chanlist[next_chan], 0);
853 pcl812_ai_next_chan(dev, s);
856 static void transfer_from_dma_buf(struct comedi_device *dev,
857 struct comedi_subdevice *s,
859 unsigned int bufptr, unsigned int len)
864 for (i = len; i; i--) {
866 comedi_buf_write_samples(s, &val, 1);
868 if (!pcl812_ai_next_chan(dev, s))
873 static void pcl812_handle_dma(struct comedi_device *dev,
874 struct comedi_subdevice *s)
876 struct pcl812_private *devpriv = dev->private;
877 struct comedi_isadma *dma = devpriv->dma;
878 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
879 unsigned int nsamples;
882 pcl812_ai_setup_next_dma(dev, s);
884 nsamples = comedi_bytes_to_samples(s, desc->size) -
885 devpriv->ai_poll_ptr;
886 bufptr = devpriv->ai_poll_ptr;
887 devpriv->ai_poll_ptr = 0;
889 transfer_from_dma_buf(dev, s, desc->virt_addr, bufptr, nsamples);
892 static irqreturn_t pcl812_interrupt(int irq, void *d)
894 struct comedi_device *dev = d;
895 struct comedi_subdevice *s = dev->read_subdev;
896 struct pcl812_private *devpriv = dev->private;
898 if (!dev->attached) {
899 pcl812_ai_clear_eoc(dev);
904 pcl812_handle_dma(dev, s);
906 pcl812_handle_eoc(dev, s);
908 pcl812_ai_clear_eoc(dev);
910 comedi_handle_events(dev, s);
914 static int pcl812_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
916 struct pcl812_private *devpriv = dev->private;
917 struct comedi_isadma *dma = devpriv->dma;
918 struct comedi_isadma_desc *desc;
923 /* poll is valid only for DMA transfer */
924 if (!devpriv->ai_dma)
927 spin_lock_irqsave(&dev->spinlock, flags);
929 poll = comedi_isadma_poll(dma);
930 poll = comedi_bytes_to_samples(s, poll);
931 if (poll > devpriv->ai_poll_ptr) {
932 desc = &dma->desc[dma->cur_dma];
933 transfer_from_dma_buf(dev, s, desc->virt_addr,
934 devpriv->ai_poll_ptr,
935 poll - devpriv->ai_poll_ptr);
936 /* new buffer position */
937 devpriv->ai_poll_ptr = poll;
939 ret = comedi_buf_n_bytes_ready(s);
945 spin_unlock_irqrestore(&dev->spinlock, flags);
950 static int pcl812_ai_cancel(struct comedi_device *dev,
951 struct comedi_subdevice *s)
953 struct pcl812_private *devpriv = dev->private;
956 comedi_isadma_disable(devpriv->dma->chan);
958 outb(devpriv->mode_reg_int | PCL812_CTRL_DISABLE_TRIG,
959 dev->iobase + PCL812_CTRL_REG);
960 pcl812_start_pacer(dev, false);
961 pcl812_ai_clear_eoc(dev);
965 static int pcl812_ai_insn_read(struct comedi_device *dev,
966 struct comedi_subdevice *s,
967 struct comedi_insn *insn,
970 struct pcl812_private *devpriv = dev->private;
974 outb(devpriv->mode_reg_int | PCL812_CTRL_SOFT_TRIG,
975 dev->iobase + PCL812_CTRL_REG);
977 pcl812_ai_set_chan_range(dev, insn->chanspec, 1);
979 for (i = 0; i < insn->n; i++) {
980 pcl812_ai_clear_eoc(dev);
981 pcl812_ai_soft_trig(dev);
983 ret = comedi_timeout(dev, s, insn, pcl812_ai_eoc, 0);
987 data[i] = pcl812_ai_get_sample(dev, s);
989 outb(devpriv->mode_reg_int | PCL812_CTRL_DISABLE_TRIG,
990 dev->iobase + PCL812_CTRL_REG);
991 pcl812_ai_clear_eoc(dev);
993 return ret ? ret : insn->n;
996 static int pcl812_ao_insn_write(struct comedi_device *dev,
997 struct comedi_subdevice *s,
998 struct comedi_insn *insn,
1001 unsigned int chan = CR_CHAN(insn->chanspec);
1002 unsigned int val = s->readback[chan];
1005 for (i = 0; i < insn->n; i++) {
1007 outb(val & 0xff, dev->iobase + PCL812_AO_LSB_REG(chan));
1008 outb((val >> 8) & 0x0f, dev->iobase + PCL812_AO_MSB_REG(chan));
1010 s->readback[chan] = val;
1015 static int pcl812_di_insn_bits(struct comedi_device *dev,
1016 struct comedi_subdevice *s,
1017 struct comedi_insn *insn,
1020 data[1] = inb(dev->iobase + PCL812_DI_LSB_REG) |
1021 (inb(dev->iobase + PCL812_DI_MSB_REG) << 8);
1026 static int pcl812_do_insn_bits(struct comedi_device *dev,
1027 struct comedi_subdevice *s,
1028 struct comedi_insn *insn,
1031 if (comedi_dio_update_state(s, data)) {
1032 outb(s->state & 0xff, dev->iobase + PCL812_DO_LSB_REG);
1033 outb((s->state >> 8), dev->iobase + PCL812_DO_MSB_REG);
1041 static void pcl812_reset(struct comedi_device *dev)
1043 const struct pcl812_board *board = dev->board_ptr;
1044 struct pcl812_private *devpriv = dev->private;
1047 /* disable analog input trigger */
1048 outb(devpriv->mode_reg_int | PCL812_CTRL_DISABLE_TRIG,
1049 dev->iobase + PCL812_CTRL_REG);
1050 pcl812_ai_clear_eoc(dev);
1054 pcl812_start_pacer(dev, false);
1057 * Invalidate last_ai_chanspec then set analog input to
1058 * known channel/range.
1060 devpriv->last_ai_chanspec = CR_PACK(16, 0, 0);
1061 pcl812_ai_set_chan_range(dev, CR_PACK(0, 0, 0), 0);
1063 /* set analog output channels to 0V */
1064 for (chan = 0; chan < board->n_aochan; chan++) {
1065 outb(0, dev->iobase + PCL812_AO_LSB_REG(chan));
1066 outb(0, dev->iobase + PCL812_AO_MSB_REG(chan));
1069 /* set all digital outputs low */
1070 if (board->has_dio) {
1071 outb(0, dev->iobase + PCL812_DO_MSB_REG);
1072 outb(0, dev->iobase + PCL812_DO_LSB_REG);
1076 static void pcl812_set_ai_range_table(struct comedi_device *dev,
1077 struct comedi_subdevice *s,
1078 struct comedi_devconfig *it)
1080 const struct pcl812_board *board = dev->board_ptr;
1081 struct pcl812_private *devpriv = dev->private;
1083 /* default to the range table from the boardinfo */
1084 s->range_table = board->rangelist_ai;
1086 /* now check the user config option based on the boardtype */
1087 switch (board->board_type) {
1089 if (it->options[4] == 1)
1090 s->range_table = &range_pcl812pg2_ai;
1093 switch (it->options[4]) {
1095 s->range_table = &range_bipolar10;
1098 s->range_table = &range_bipolar5;
1101 s->range_table = &range_bipolar2_5;
1104 s->range_table = &range812_bipolar1_25;
1107 s->range_table = &range812_bipolar0_625;
1110 s->range_table = &range812_bipolar0_3125;
1113 s->range_table = &range_bipolar10;
1118 if (it->options[1] == 1)
1119 s->range_table = &range_pcl813b2_ai;
1122 switch (it->options[1]) {
1124 s->range_table = &range_iso813_1_ai;
1127 s->range_table = &range_iso813_1_2_ai;
1130 s->range_table = &range_iso813_2_ai;
1131 devpriv->range_correction = 1;
1134 s->range_table = &range_iso813_2_2_ai;
1135 devpriv->range_correction = 1;
1138 s->range_table = &range_iso813_1_ai;
1143 switch (it->options[1]) {
1145 s->range_table = &range_acl8113_1_ai;
1148 s->range_table = &range_acl8113_1_2_ai;
1151 s->range_table = &range_acl8113_2_ai;
1152 devpriv->range_correction = 1;
1155 s->range_table = &range_acl8113_2_2_ai;
1156 devpriv->range_correction = 1;
1159 s->range_table = &range_acl8113_1_ai;
1166 static void pcl812_alloc_dma(struct comedi_device *dev, unsigned int dma_chan)
1168 struct pcl812_private *devpriv = dev->private;
1170 /* only DMA channels 3 and 1 are valid */
1171 if (!(dma_chan == 3 || dma_chan == 1))
1174 /* DMA uses two 8K buffers */
1175 devpriv->dma = comedi_isadma_alloc(dev, 2, dma_chan, dma_chan,
1176 PAGE_SIZE * 2, COMEDI_ISADMA_READ);
1179 static void pcl812_free_dma(struct comedi_device *dev)
1181 struct pcl812_private *devpriv = dev->private;
1184 comedi_isadma_free(devpriv->dma);
1187 static int pcl812_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1189 const struct pcl812_board *board = dev->board_ptr;
1190 struct pcl812_private *devpriv;
1191 struct comedi_subdevice *s;
1196 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1200 ret = comedi_request_region(dev, it->options[0], 0x10);
1204 if ((1 << it->options[1]) & board->IRQbits) {
1205 ret = request_irq(it->options[1], pcl812_interrupt, 0,
1206 dev->board_name, dev);
1208 dev->irq = it->options[1];
1211 /* we need an IRQ to do DMA on channel 3 or 1 */
1212 if (dev->irq && board->has_dma)
1213 pcl812_alloc_dma(dev, it->options[2]);
1215 /* differential analog inputs? */
1216 switch (board->board_type) {
1218 if (it->options[2] == 1)
1219 devpriv->use_diff = 1;
1223 if (it->options[4] == 1)
1224 devpriv->use_diff = 1;
1228 n_subdevices = 1; /* all boardtypes have analog inputs */
1229 if (board->n_aochan > 0)
1234 ret = comedi_alloc_subdevices(dev, n_subdevices);
1240 /* Analog Input subdevice */
1241 s = &dev->subdevices[subdev];
1242 s->type = COMEDI_SUBD_AI;
1243 s->subdev_flags = SDF_READABLE;
1244 if (devpriv->use_diff) {
1245 s->subdev_flags |= SDF_DIFF;
1246 s->n_chan = board->n_aichan / 2;
1248 s->subdev_flags |= SDF_GROUND;
1249 s->n_chan = board->n_aichan;
1251 s->maxdata = board->has_16bit_ai ? 0xffff : 0x0fff;
1253 pcl812_set_ai_range_table(dev, s, it);
1255 s->insn_read = pcl812_ai_insn_read;
1258 dev->read_subdev = s;
1259 s->subdev_flags |= SDF_CMD_READ;
1260 s->len_chanlist = MAX_CHANLIST_LEN;
1261 s->do_cmdtest = pcl812_ai_cmdtest;
1262 s->do_cmd = pcl812_ai_cmd;
1263 s->poll = pcl812_ai_poll;
1264 s->cancel = pcl812_ai_cancel;
1267 devpriv->use_mpc508 = board->has_mpc508_mux;
1272 if (board->n_aochan > 0) {
1273 s = &dev->subdevices[subdev];
1274 s->type = COMEDI_SUBD_AO;
1275 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
1276 s->n_chan = board->n_aochan;
1278 s->range_table = &range_unipolar5;
1279 switch (board->board_type) {
1281 if (it->options[3] == 1)
1282 s->range_table = &range_unipolar10;
1288 if (it->options[5] == 1)
1289 s->range_table = &range_unipolar10;
1290 if (it->options[5] == 2)
1291 s->range_table = &range_unknown;
1294 s->insn_write = pcl812_ao_insn_write;
1296 ret = comedi_alloc_subdev_readback(s);
1303 if (board->has_dio) {
1304 /* Digital Input subdevice */
1305 s = &dev->subdevices[subdev];
1306 s->type = COMEDI_SUBD_DI;
1307 s->subdev_flags = SDF_READABLE;
1310 s->range_table = &range_digital;
1311 s->insn_bits = pcl812_di_insn_bits;
1314 /* Digital Output subdevice */
1315 s = &dev->subdevices[subdev];
1316 s->type = COMEDI_SUBD_DO;
1317 s->subdev_flags = SDF_WRITABLE;
1320 s->range_table = &range_digital;
1321 s->insn_bits = pcl812_do_insn_bits;
1325 switch (board->board_type) {
1330 devpriv->max_812_ai_mode0_rangewait = 1;
1331 if (it->options[3] > 0)
1332 /* we use external trigger */
1333 devpriv->use_ext_trg = 1;
1336 devpriv->max_812_ai_mode0_rangewait = 1;
1337 devpriv->mode_reg_int = (dev->irq << 4) & 0xf0;
1343 /* maybe there must by greatest timeout */
1344 devpriv->max_812_ai_mode0_rangewait = 5;
1353 static void pcl812_detach(struct comedi_device *dev)
1355 pcl812_free_dma(dev);
1356 comedi_legacy_detach(dev);
1359 static struct comedi_driver pcl812_driver = {
1360 .driver_name = "pcl812",
1361 .module = THIS_MODULE,
1362 .attach = pcl812_attach,
1363 .detach = pcl812_detach,
1364 .board_name = &boardtypes[0].name,
1365 .num_names = ARRAY_SIZE(boardtypes),
1366 .offset = sizeof(struct pcl812_board),
1368 module_comedi_driver(pcl812_driver);
1370 MODULE_AUTHOR("Comedi http://www.comedi.org");
1371 MODULE_DESCRIPTION("Comedi low-level driver");
1372 MODULE_LICENSE("GPL");