2 * comedi/drivers/pcl818.c
5 * Description: Advantech PCL-818 cards, PCL-718
6 * Author: Michal Dobes <dobes@tesnet.cz>
7 * Devices: [Advantech] PCL-818L (pcl818l), PCL-818H (pcl818h),
8 * PCL-818HD (pcl818hd), PCL-818HG (pcl818hg), PCL-818 (pcl818),
12 * All cards have 16 SE/8 DIFF ADCs, one or two DACs, 16 DI and 16 DO.
13 * Differences are only at maximal sample speed, range list and FIFO
15 * The driver support AI mode 0, 1, 3 other subdevices (AO, DI, DO) support
16 * only mode 0. If DMA/FIFO/INT are disabled then AI support only mode 0.
17 * PCL-818HD and PCL-818HG support 1kword FIFO. Driver support this FIFO
18 * but this code is untested.
19 * A word or two about DMA. Driver support DMA operations at two ways:
20 * 1) DMA uses two buffers and after one is filled then is generated
21 * INT and DMA restart with second buffer. With this mode I'm unable run
22 * more that 80Ksamples/secs without data dropouts on K6/233.
23 * 2) DMA uses one buffer and run in autoinit mode and the data are
24 * from DMA buffer moved on the fly with 2kHz interrupts from RTC.
25 * This mode is used if the interrupt 8 is available for allocation.
26 * If not, then first DMA mode is used. With this I can run at
27 * full speed one card (100ksamples/secs) or two cards with
28 * 60ksamples/secs each (more is problem on account of ISA limitations).
29 * To use this mode you must have compiled kernel with disabled
30 * "Enhanced Real Time Clock Support".
31 * Maybe you can have problems if you use xntpd or similar.
32 * If you've data dropouts with DMA mode 2 then:
34 * b) switch text mode console to fb.
36 * Options for PCL-818L:
38 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7)
39 * [2] - DMA (0=disable, 1, 3)
40 * [3] - 0, 10=10MHz clock for 8254
41 * 1= 1MHz clock for 8254
42 * [4] - 0, 5=A/D input -5V.. +5V
43 * 1, 10=A/D input -10V..+10V
44 * [5] - 0, 5=D/A output 0-5V (internal reference -5V)
45 * 1, 10=D/A output 0-10V (internal reference -10V)
46 * 2 =D/A output unknown (external reference)
48 * Options for PCL-818, PCL-818H:
50 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7)
51 * [2] - DMA (0=disable, 1, 3)
52 * [3] - 0, 10=10MHz clock for 8254
53 * 1= 1MHz clock for 8254
54 * [4] - 0, 5=D/A output 0-5V (internal reference -5V)
55 * 1, 10=D/A output 0-10V (internal reference -10V)
56 * 2 =D/A output unknown (external reference)
58 * Options for PCL-818HD, PCL-818HG:
60 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7)
61 * [2] - DMA/FIFO (-1=use FIFO, 0=disable both FIFO and DMA,
62 * 1=use DMA ch 1, 3=use DMA ch 3)
63 * [3] - 0, 10=10MHz clock for 8254
64 * 1= 1MHz clock for 8254
65 * [4] - 0, 5=D/A output 0-5V (internal reference -5V)
66 * 1, 10=D/A output 0-10V (internal reference -10V)
67 * 2 =D/A output unknown (external reference)
69 * Options for PCL-718:
71 * [1] - IRQ (0=disable, 2, 3, 4, 5, 6, 7)
72 * [2] - DMA (0=disable, 1, 3)
73 * [3] - 0, 10=10MHz clock for 8254
74 * 1= 1MHz clock for 8254
75 * [4] - 0=A/D Range is +/-10V
80 * 5= user defined bipolar
85 * 10= user defined unipolar
86 * [5] - 0, 5=D/A outputs 0-5V (internal reference -5V)
87 * 1, 10=D/A outputs 0-10V (internal reference -10V)
88 * 2=D/A outputs unknown (external reference)
89 * [6] - 0, 60=max 60kHz A/D sampling
90 * 1,100=max 100kHz A/D sampling (PCL-718 with Option 001 installed)
94 #include <linux/module.h>
95 #include <linux/gfp.h>
96 #include <linux/delay.h>
98 #include <linux/interrupt.h>
100 #include "../comedidev.h"
102 #include "comedi_isadma.h"
103 #include "comedi_8254.h"
108 #define PCL818_AI_LSB_REG 0x00
109 #define PCL818_AI_MSB_REG 0x01
110 #define PCL818_RANGE_REG 0x01
111 #define PCL818_MUX_REG 0x02
112 #define PCL818_MUX_SCAN(_first, _last) (((_last) << 4) | (_first))
113 #define PCL818_DO_DI_LSB_REG 0x03
114 #define PCL818_AO_LSB_REG(x) (0x04 + ((x) * 2))
115 #define PCL818_AO_MSB_REG(x) (0x05 + ((x) * 2))
116 #define PCL818_STATUS_REG 0x08
117 #define PCL818_STATUS_NEXT_CHAN_MASK (0xf << 0)
118 #define PCL818_STATUS_INT BIT(4)
119 #define PCL818_STATUS_MUX BIT(5)
120 #define PCL818_STATUS_UNI BIT(6)
121 #define PCL818_STATUS_EOC BIT(7)
122 #define PCL818_CTRL_REG 0x09
123 #define PCL818_CTRL_TRIG(x) (((x) & 0x3) << 0)
124 #define PCL818_CTRL_DISABLE_TRIG PCL818_CTRL_TRIG(0)
125 #define PCL818_CTRL_SOFT_TRIG PCL818_CTRL_TRIG(1)
126 #define PCL818_CTRL_EXT_TRIG PCL818_CTRL_TRIG(2)
127 #define PCL818_CTRL_PACER_TRIG PCL818_CTRL_TRIG(3)
128 #define PCL818_CTRL_DMAE BIT(2)
129 #define PCL818_CTRL_IRQ(x) ((x) << 4)
130 #define PCL818_CTRL_INTE BIT(7)
131 #define PCL818_CNTENABLE_REG 0x0a
132 #define PCL818_CNTENABLE_PACER_TRIG0 BIT(0)
133 #define PCL818_CNTENABLE_CNT0_INT_CLK BIT(1) /* 0=ext clk */
134 #define PCL818_DO_DI_MSB_REG 0x0b
135 #define PCL818_TIMER_BASE 0x0c
137 /* W: fifo enable/disable */
138 #define PCL818_FI_ENABLE 6
139 /* W: fifo interrupt clear */
140 #define PCL818_FI_INTCLR 20
141 /* W: fifo interrupt clear */
142 #define PCL818_FI_FLUSH 25
144 #define PCL818_FI_STATUS 25
145 /* R: one record from FIFO */
146 #define PCL818_FI_DATALO 23
147 #define PCL818_FI_DATAHI 24
149 #define MAGIC_DMA_WORD 0x5a5a
151 static const struct comedi_lrange range_pcl818h_ai = {
165 static const struct comedi_lrange range_pcl818hg_ai = {
182 static const struct comedi_lrange range_pcl818l_l_ai = {
191 static const struct comedi_lrange range_pcl818l_h_ai = {
200 static const struct comedi_lrange range718_bipolar1 = {
206 static const struct comedi_lrange range718_bipolar0_5 = {
212 static const struct comedi_lrange range718_unipolar2 = {
218 static const struct comedi_lrange range718_unipolar1 = {
224 struct pcl818_board {
228 const struct comedi_lrange *ai_range_type;
229 unsigned int has_dma:1;
230 unsigned int has_fifo:1;
231 unsigned int is_818:1;
234 static const struct pcl818_board boardtypes[] = {
239 .ai_range_type = &range_pcl818l_l_ai,
246 .ai_range_type = &range_pcl818h_ai,
253 .ai_range_type = &range_pcl818h_ai,
261 .ai_range_type = &range_pcl818hg_ai,
269 .ai_range_type = &range_pcl818h_ai,
276 .ai_range_type = &range_unipolar5,
281 .ai_range_type = &range_pcl818h_ai,
287 struct pcl818_private {
288 struct comedi_isadma *dma;
289 /* manimal allowed delay between samples (in us) for actual card */
291 /* MUX setting for actual AI operations */
292 unsigned int act_chanlist[16];
293 unsigned int act_chanlist_len; /* how long is actual MUX list */
294 unsigned int act_chanlist_pos; /* actual position in MUX list */
295 unsigned int usefifo:1;
296 unsigned int ai_cmd_running:1;
297 unsigned int ai_cmd_canceled:1;
300 static void pcl818_ai_setup_dma(struct comedi_device *dev,
301 struct comedi_subdevice *s,
302 unsigned int unread_samples)
304 struct pcl818_private *devpriv = dev->private;
305 struct comedi_isadma *dma = devpriv->dma;
306 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
307 unsigned int max_samples = comedi_bytes_to_samples(s, desc->maxsize);
308 unsigned int nsamples;
310 comedi_isadma_disable(dma->chan);
313 * Determine dma size based on the buffer maxsize plus the number of
314 * unread samples and the number of samples remaining in the command.
316 nsamples = comedi_nsamples_left(s, max_samples + unread_samples);
317 if (nsamples > unread_samples) {
318 nsamples -= unread_samples;
319 desc->size = comedi_samples_to_bytes(s, nsamples);
320 comedi_isadma_program(desc);
324 static void pcl818_ai_set_chan_range(struct comedi_device *dev,
328 outb(chan, dev->iobase + PCL818_MUX_REG);
329 outb(range, dev->iobase + PCL818_RANGE_REG);
332 static void pcl818_ai_set_chan_scan(struct comedi_device *dev,
333 unsigned int first_chan,
334 unsigned int last_chan)
336 outb(PCL818_MUX_SCAN(first_chan, last_chan),
337 dev->iobase + PCL818_MUX_REG);
340 static void pcl818_ai_setup_chanlist(struct comedi_device *dev,
341 unsigned int *chanlist,
344 struct pcl818_private *devpriv = dev->private;
345 unsigned int first_chan = CR_CHAN(chanlist[0]);
346 unsigned int last_chan;
350 devpriv->act_chanlist_len = seglen;
351 devpriv->act_chanlist_pos = 0;
353 /* store range list to card */
354 for (i = 0; i < seglen; i++) {
355 last_chan = CR_CHAN(chanlist[i]);
356 range = CR_RANGE(chanlist[i]);
358 devpriv->act_chanlist[i] = last_chan;
360 pcl818_ai_set_chan_range(dev, last_chan, range);
365 pcl818_ai_set_chan_scan(dev, first_chan, last_chan);
368 static void pcl818_ai_clear_eoc(struct comedi_device *dev)
370 /* writing any value clears the interrupt request */
371 outb(0, dev->iobase + PCL818_STATUS_REG);
374 static void pcl818_ai_soft_trig(struct comedi_device *dev)
376 /* writing any value triggers a software conversion */
377 outb(0, dev->iobase + PCL818_AI_LSB_REG);
380 static unsigned int pcl818_ai_get_fifo_sample(struct comedi_device *dev,
381 struct comedi_subdevice *s,
386 val = inb(dev->iobase + PCL818_FI_DATALO);
387 val |= (inb(dev->iobase + PCL818_FI_DATAHI) << 8);
392 return (val >> 4) & s->maxdata;
395 static unsigned int pcl818_ai_get_sample(struct comedi_device *dev,
396 struct comedi_subdevice *s,
401 val = inb(dev->iobase + PCL818_AI_MSB_REG) << 8;
402 val |= inb(dev->iobase + PCL818_AI_LSB_REG);
407 return (val >> 4) & s->maxdata;
410 static int pcl818_ai_eoc(struct comedi_device *dev,
411 struct comedi_subdevice *s,
412 struct comedi_insn *insn,
413 unsigned long context)
417 status = inb(dev->iobase + PCL818_STATUS_REG);
418 if (status & PCL818_STATUS_INT)
423 static bool pcl818_ai_write_sample(struct comedi_device *dev,
424 struct comedi_subdevice *s,
425 unsigned int chan, unsigned int val)
427 struct pcl818_private *devpriv = dev->private;
428 struct comedi_cmd *cmd = &s->async->cmd;
429 unsigned int expected_chan;
431 expected_chan = devpriv->act_chanlist[devpriv->act_chanlist_pos];
432 if (chan != expected_chan) {
433 dev_dbg(dev->class_dev,
434 "A/D mode1/3 %s - channel dropout %d!=%d !\n",
435 (devpriv->dma) ? "DMA" :
436 (devpriv->usefifo) ? "FIFO" : "IRQ",
437 chan, expected_chan);
438 s->async->events |= COMEDI_CB_ERROR;
442 comedi_buf_write_samples(s, &val, 1);
444 devpriv->act_chanlist_pos++;
445 if (devpriv->act_chanlist_pos >= devpriv->act_chanlist_len)
446 devpriv->act_chanlist_pos = 0;
448 if (cmd->stop_src == TRIG_COUNT &&
449 s->async->scans_done >= cmd->stop_arg) {
450 s->async->events |= COMEDI_CB_EOA;
457 static void pcl818_handle_eoc(struct comedi_device *dev,
458 struct comedi_subdevice *s)
463 if (pcl818_ai_eoc(dev, s, NULL, 0)) {
464 dev_err(dev->class_dev, "A/D mode1/3 IRQ without DRDY!\n");
465 s->async->events |= COMEDI_CB_ERROR;
469 val = pcl818_ai_get_sample(dev, s, &chan);
470 pcl818_ai_write_sample(dev, s, chan, val);
473 static void pcl818_handle_dma(struct comedi_device *dev,
474 struct comedi_subdevice *s)
476 struct pcl818_private *devpriv = dev->private;
477 struct comedi_isadma *dma = devpriv->dma;
478 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
479 unsigned short *ptr = desc->virt_addr;
480 unsigned int nsamples = comedi_bytes_to_samples(s, desc->size);
485 /* restart dma with the next buffer */
486 dma->cur_dma = 1 - dma->cur_dma;
487 pcl818_ai_setup_dma(dev, s, nsamples);
489 for (i = 0; i < nsamples; i++) {
492 val = (val >> 4) & s->maxdata;
493 if (!pcl818_ai_write_sample(dev, s, chan, val))
498 static void pcl818_handle_fifo(struct comedi_device *dev,
499 struct comedi_subdevice *s)
506 status = inb(dev->iobase + PCL818_FI_STATUS);
509 dev_err(dev->class_dev, "A/D mode1/3 FIFO overflow!\n");
510 s->async->events |= COMEDI_CB_ERROR;
515 dev_err(dev->class_dev,
516 "A/D mode1/3 FIFO interrupt without data!\n");
517 s->async->events |= COMEDI_CB_ERROR;
526 for (i = 0; i < len; i++) {
527 val = pcl818_ai_get_fifo_sample(dev, s, &chan);
528 if (!pcl818_ai_write_sample(dev, s, chan, val))
533 static irqreturn_t pcl818_interrupt(int irq, void *d)
535 struct comedi_device *dev = d;
536 struct pcl818_private *devpriv = dev->private;
537 struct comedi_subdevice *s = dev->read_subdev;
538 struct comedi_cmd *cmd = &s->async->cmd;
540 if (!dev->attached || !devpriv->ai_cmd_running) {
541 pcl818_ai_clear_eoc(dev);
545 if (devpriv->ai_cmd_canceled) {
547 * The cleanup from ai_cancel() has been delayed
548 * until now because the card doesn't seem to like
549 * being reprogrammed while a DMA transfer is in
552 s->async->scans_done = cmd->stop_arg;
558 pcl818_handle_dma(dev, s);
559 else if (devpriv->usefifo)
560 pcl818_handle_fifo(dev, s);
562 pcl818_handle_eoc(dev, s);
564 pcl818_ai_clear_eoc(dev);
566 comedi_handle_events(dev, s);
570 static int check_channel_list(struct comedi_device *dev,
571 struct comedi_subdevice *s,
572 unsigned int *chanlist, unsigned int n_chan)
574 unsigned int chansegment[16];
575 unsigned int i, nowmustbechan, seglen, segpos;
577 /* correct channel and range number check itself comedi/range.c */
579 dev_err(dev->class_dev, "range/channel list is empty!\n");
584 /* first channel is every time ok */
585 chansegment[0] = chanlist[0];
586 /* build part of chanlist */
587 for (i = 1, seglen = 1; i < n_chan; i++, seglen++) {
588 /* we detect loop, this must by finish */
590 if (chanlist[0] == chanlist[i])
593 (CR_CHAN(chansegment[i - 1]) + 1) % s->n_chan;
594 if (nowmustbechan != CR_CHAN(chanlist[i])) {
595 /* channel list isn't continuous :-( */
596 dev_dbg(dev->class_dev,
597 "channel list must be continuous! chanlist[%i]=%d but must be %d or %d!\n",
598 i, CR_CHAN(chanlist[i]), nowmustbechan,
599 CR_CHAN(chanlist[0]));
602 /* well, this is next correct channel in list */
603 chansegment[i] = chanlist[i];
606 /* check whole chanlist */
607 for (i = 0, segpos = 0; i < n_chan; i++) {
608 if (chanlist[i] != chansegment[i % seglen]) {
609 dev_dbg(dev->class_dev,
610 "bad channel or range number! chanlist[%i]=%d,%d,%d and not %d,%d,%d!\n",
611 i, CR_CHAN(chansegment[i]),
612 CR_RANGE(chansegment[i]),
613 CR_AREF(chansegment[i]),
614 CR_CHAN(chanlist[i % seglen]),
615 CR_RANGE(chanlist[i % seglen]),
616 CR_AREF(chansegment[i % seglen]));
617 return 0; /* chan/gain list is strange */
626 static int check_single_ended(unsigned int port)
628 if (inb(port + PCL818_STATUS_REG) & PCL818_STATUS_MUX)
633 static int ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
634 struct comedi_cmd *cmd)
636 const struct pcl818_board *board = dev->board_ptr;
639 /* Step 1 : check if triggers are trivially valid */
641 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
642 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
643 err |= comedi_check_trigger_src(&cmd->convert_src,
644 TRIG_TIMER | TRIG_EXT);
645 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
646 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
651 /* Step 2a : make sure trigger sources are unique */
653 err |= comedi_check_trigger_is_unique(cmd->convert_src);
654 err |= comedi_check_trigger_is_unique(cmd->stop_src);
656 /* Step 2b : and mutually compatible */
661 /* Step 3: check if arguments are trivially valid */
663 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
664 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
666 if (cmd->convert_src == TRIG_TIMER) {
667 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
669 } else { /* TRIG_EXT */
670 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
673 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
676 if (cmd->stop_src == TRIG_COUNT)
677 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
679 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
684 /* step 4: fix up any arguments */
686 if (cmd->convert_src == TRIG_TIMER) {
687 unsigned int arg = cmd->convert_arg;
689 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
690 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
696 /* step 5: complain about special chanlist considerations */
699 if (!check_channel_list(dev, s, cmd->chanlist,
701 return 5; /* incorrect channels list */
707 static int pcl818_ai_cmd(struct comedi_device *dev,
708 struct comedi_subdevice *s)
710 struct pcl818_private *devpriv = dev->private;
711 struct comedi_isadma *dma = devpriv->dma;
712 struct comedi_cmd *cmd = &s->async->cmd;
713 unsigned int ctrl = 0;
716 if (devpriv->ai_cmd_running)
719 seglen = check_channel_list(dev, s, cmd->chanlist, cmd->chanlist_len);
722 pcl818_ai_setup_chanlist(dev, cmd->chanlist, seglen);
724 devpriv->ai_cmd_running = 1;
725 devpriv->ai_cmd_canceled = 0;
726 devpriv->act_chanlist_pos = 0;
728 if (cmd->convert_src == TRIG_TIMER)
729 ctrl |= PCL818_CTRL_PACER_TRIG;
731 ctrl |= PCL818_CTRL_EXT_TRIG;
733 outb(0, dev->iobase + PCL818_CNTENABLE_REG);
736 /* setup and enable dma for the first buffer */
738 pcl818_ai_setup_dma(dev, s, 0);
740 ctrl |= PCL818_CTRL_INTE | PCL818_CTRL_IRQ(dev->irq) |
742 } else if (devpriv->usefifo) {
744 outb(1, dev->iobase + PCL818_FI_ENABLE);
746 ctrl |= PCL818_CTRL_INTE | PCL818_CTRL_IRQ(dev->irq);
748 outb(ctrl, dev->iobase + PCL818_CTRL_REG);
750 if (cmd->convert_src == TRIG_TIMER) {
751 comedi_8254_update_divisors(dev->pacer);
752 comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
758 static int pcl818_ai_cancel(struct comedi_device *dev,
759 struct comedi_subdevice *s)
761 struct pcl818_private *devpriv = dev->private;
762 struct comedi_isadma *dma = devpriv->dma;
763 struct comedi_cmd *cmd = &s->async->cmd;
765 if (!devpriv->ai_cmd_running)
769 if (cmd->stop_src == TRIG_NONE ||
770 (cmd->stop_src == TRIG_COUNT &&
771 s->async->scans_done < cmd->stop_arg)) {
772 if (!devpriv->ai_cmd_canceled) {
774 * Wait for running dma transfer to end,
775 * do cleanup in interrupt.
777 devpriv->ai_cmd_canceled = 1;
781 comedi_isadma_disable(dma->chan);
784 outb(PCL818_CTRL_DISABLE_TRIG, dev->iobase + PCL818_CTRL_REG);
785 comedi_8254_pacer_enable(dev->pacer, 1, 2, false);
786 pcl818_ai_clear_eoc(dev);
788 if (devpriv->usefifo) { /* FIFO shutdown */
789 outb(0, dev->iobase + PCL818_FI_INTCLR);
790 outb(0, dev->iobase + PCL818_FI_FLUSH);
791 outb(0, dev->iobase + PCL818_FI_ENABLE);
793 devpriv->ai_cmd_running = 0;
794 devpriv->ai_cmd_canceled = 0;
799 static int pcl818_ai_insn_read(struct comedi_device *dev,
800 struct comedi_subdevice *s,
801 struct comedi_insn *insn,
804 unsigned int chan = CR_CHAN(insn->chanspec);
805 unsigned int range = CR_RANGE(insn->chanspec);
809 outb(PCL818_CTRL_SOFT_TRIG, dev->iobase + PCL818_CTRL_REG);
811 pcl818_ai_set_chan_range(dev, chan, range);
812 pcl818_ai_set_chan_scan(dev, chan, chan);
814 for (i = 0; i < insn->n; i++) {
815 pcl818_ai_clear_eoc(dev);
816 pcl818_ai_soft_trig(dev);
818 ret = comedi_timeout(dev, s, insn, pcl818_ai_eoc, 0);
822 data[i] = pcl818_ai_get_sample(dev, s, NULL);
824 pcl818_ai_clear_eoc(dev);
826 return ret ? ret : insn->n;
829 static int pcl818_ao_insn_write(struct comedi_device *dev,
830 struct comedi_subdevice *s,
831 struct comedi_insn *insn,
834 unsigned int chan = CR_CHAN(insn->chanspec);
835 unsigned int val = s->readback[chan];
838 for (i = 0; i < insn->n; i++) {
840 outb((val & 0x000f) << 4,
841 dev->iobase + PCL818_AO_LSB_REG(chan));
842 outb((val & 0x0ff0) >> 4,
843 dev->iobase + PCL818_AO_MSB_REG(chan));
845 s->readback[chan] = val;
850 static int pcl818_di_insn_bits(struct comedi_device *dev,
851 struct comedi_subdevice *s,
852 struct comedi_insn *insn,
855 data[1] = inb(dev->iobase + PCL818_DO_DI_LSB_REG) |
856 (inb(dev->iobase + PCL818_DO_DI_MSB_REG) << 8);
861 static int pcl818_do_insn_bits(struct comedi_device *dev,
862 struct comedi_subdevice *s,
863 struct comedi_insn *insn,
866 if (comedi_dio_update_state(s, data)) {
867 outb(s->state & 0xff, dev->iobase + PCL818_DO_DI_LSB_REG);
868 outb((s->state >> 8), dev->iobase + PCL818_DO_DI_MSB_REG);
876 static void pcl818_reset(struct comedi_device *dev)
878 const struct pcl818_board *board = dev->board_ptr;
881 /* flush and disable the FIFO */
882 if (board->has_fifo) {
883 outb(0, dev->iobase + PCL818_FI_INTCLR);
884 outb(0, dev->iobase + PCL818_FI_FLUSH);
885 outb(0, dev->iobase + PCL818_FI_ENABLE);
888 /* disable analog input trigger */
889 outb(PCL818_CTRL_DISABLE_TRIG, dev->iobase + PCL818_CTRL_REG);
890 pcl818_ai_clear_eoc(dev);
892 pcl818_ai_set_chan_range(dev, 0, 0);
895 outb(0, dev->iobase + PCL818_CNTENABLE_REG);
897 /* set analog output channels to 0V */
898 for (chan = 0; chan < board->n_aochan; chan++) {
899 outb(0, dev->iobase + PCL818_AO_LSB_REG(chan));
900 outb(0, dev->iobase + PCL818_AO_MSB_REG(chan));
903 /* set all digital outputs low */
904 outb(0, dev->iobase + PCL818_DO_DI_MSB_REG);
905 outb(0, dev->iobase + PCL818_DO_DI_LSB_REG);
908 static void pcl818_set_ai_range_table(struct comedi_device *dev,
909 struct comedi_subdevice *s,
910 struct comedi_devconfig *it)
912 const struct pcl818_board *board = dev->board_ptr;
914 /* default to the range table from the boardinfo */
915 s->range_table = board->ai_range_type;
917 /* now check the user config option based on the boardtype */
919 if (it->options[4] == 1 || it->options[4] == 10) {
920 /* secondary range list jumper selectable */
921 s->range_table = &range_pcl818l_h_ai;
924 switch (it->options[4]) {
926 s->range_table = &range_bipolar10;
929 s->range_table = &range_bipolar5;
932 s->range_table = &range_bipolar2_5;
935 s->range_table = &range718_bipolar1;
938 s->range_table = &range718_bipolar0_5;
941 s->range_table = &range_unipolar10;
944 s->range_table = &range_unipolar5;
947 s->range_table = &range718_unipolar2;
950 s->range_table = &range718_unipolar1;
953 s->range_table = &range_unknown;
959 static void pcl818_alloc_dma(struct comedi_device *dev, unsigned int dma_chan)
961 struct pcl818_private *devpriv = dev->private;
963 /* only DMA channels 3 and 1 are valid */
964 if (!(dma_chan == 3 || dma_chan == 1))
967 /* DMA uses two 16K buffers */
968 devpriv->dma = comedi_isadma_alloc(dev, 2, dma_chan, dma_chan,
969 PAGE_SIZE * 4, COMEDI_ISADMA_READ);
972 static void pcl818_free_dma(struct comedi_device *dev)
974 struct pcl818_private *devpriv = dev->private;
977 comedi_isadma_free(devpriv->dma);
980 static int pcl818_attach(struct comedi_device *dev, struct comedi_devconfig *it)
982 const struct pcl818_board *board = dev->board_ptr;
983 struct pcl818_private *devpriv;
984 struct comedi_subdevice *s;
985 unsigned int osc_base;
988 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
992 ret = comedi_request_region(dev, it->options[0],
993 board->has_fifo ? 0x20 : 0x10);
997 /* we can use IRQ 2-7 for async command support */
998 if (it->options[1] >= 2 && it->options[1] <= 7) {
999 ret = request_irq(it->options[1], pcl818_interrupt, 0,
1000 dev->board_name, dev);
1002 dev->irq = it->options[1];
1005 /* should we use the FIFO? */
1006 if (dev->irq && board->has_fifo && it->options[2] == -1)
1007 devpriv->usefifo = 1;
1009 /* we need an IRQ to do DMA on channel 3 or 1 */
1010 if (dev->irq && board->has_dma)
1011 pcl818_alloc_dma(dev, it->options[2]);
1013 /* use 1MHz or 10MHz oscilator */
1014 if ((it->options[3] == 0) || (it->options[3] == 10))
1015 osc_base = I8254_OSC_BASE_10MHZ;
1017 osc_base = I8254_OSC_BASE_1MHZ;
1019 dev->pacer = comedi_8254_init(dev->iobase + PCL818_TIMER_BASE,
1020 osc_base, I8254_IO8, 0);
1024 /* max sampling speed */
1025 devpriv->ns_min = board->ns_min;
1026 if (!board->is_818) {
1027 /* extended PCL718 to 100kHz DAC */
1028 if ((it->options[6] == 1) || (it->options[6] == 100))
1029 devpriv->ns_min = 10000;
1032 ret = comedi_alloc_subdevices(dev, 4);
1036 s = &dev->subdevices[0];
1037 s->type = COMEDI_SUBD_AI;
1038 s->subdev_flags = SDF_READABLE;
1039 if (check_single_ended(dev->iobase)) {
1041 s->subdev_flags |= SDF_COMMON | SDF_GROUND;
1044 s->subdev_flags |= SDF_DIFF;
1046 s->maxdata = 0x0fff;
1048 pcl818_set_ai_range_table(dev, s, it);
1050 s->insn_read = pcl818_ai_insn_read;
1052 dev->read_subdev = s;
1053 s->subdev_flags |= SDF_CMD_READ;
1054 s->len_chanlist = s->n_chan;
1055 s->do_cmdtest = ai_cmdtest;
1056 s->do_cmd = pcl818_ai_cmd;
1057 s->cancel = pcl818_ai_cancel;
1060 /* Analog Output subdevice */
1061 s = &dev->subdevices[1];
1062 if (board->n_aochan) {
1063 s->type = COMEDI_SUBD_AO;
1064 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
1065 s->n_chan = board->n_aochan;
1066 s->maxdata = 0x0fff;
1067 s->range_table = &range_unipolar5;
1068 if (board->is_818) {
1069 if ((it->options[4] == 1) || (it->options[4] == 10))
1070 s->range_table = &range_unipolar10;
1071 if (it->options[4] == 2)
1072 s->range_table = &range_unknown;
1074 if ((it->options[5] == 1) || (it->options[5] == 10))
1075 s->range_table = &range_unipolar10;
1076 if (it->options[5] == 2)
1077 s->range_table = &range_unknown;
1079 s->insn_write = pcl818_ao_insn_write;
1081 ret = comedi_alloc_subdev_readback(s);
1085 s->type = COMEDI_SUBD_UNUSED;
1088 /* Digital Input subdevice */
1089 s = &dev->subdevices[2];
1090 s->type = COMEDI_SUBD_DI;
1091 s->subdev_flags = SDF_READABLE;
1094 s->range_table = &range_digital;
1095 s->insn_bits = pcl818_di_insn_bits;
1097 /* Digital Output subdevice */
1098 s = &dev->subdevices[3];
1099 s->type = COMEDI_SUBD_DO;
1100 s->subdev_flags = SDF_WRITABLE;
1103 s->range_table = &range_digital;
1104 s->insn_bits = pcl818_do_insn_bits;
1111 static void pcl818_detach(struct comedi_device *dev)
1113 struct pcl818_private *devpriv = dev->private;
1116 pcl818_ai_cancel(dev, dev->read_subdev);
1119 pcl818_free_dma(dev);
1120 comedi_legacy_detach(dev);
1123 static struct comedi_driver pcl818_driver = {
1124 .driver_name = "pcl818",
1125 .module = THIS_MODULE,
1126 .attach = pcl818_attach,
1127 .detach = pcl818_detach,
1128 .board_name = &boardtypes[0].name,
1129 .num_names = ARRAY_SIZE(boardtypes),
1130 .offset = sizeof(struct pcl818_board),
1132 module_comedi_driver(pcl818_driver);
1134 MODULE_AUTHOR("Comedi http://www.comedi.org");
1135 MODULE_DESCRIPTION("Comedi low-level driver");
1136 MODULE_LICENSE("GPL");