3 * Comedi driver for PC-LabCard PCL-711 and AdSys ACL-8112 and compatibles
4 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
5 * Janne Jalkanen <jalkanen@cs.hut.fi>
6 * Eric Bunn <ebu@cs.hut.fi>
8 * COMEDI - Linux Control and Measurement Device Interface
9 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
24 * Description: Advantech PCL-711 and 711b, ADLink ACL-8112
25 * Devices: (Advantech) PCL-711 [pcl711]
26 * (Advantech) PCL-711B [pcl711b]
27 * (AdLink) ACL-8112HG [acl8112hg]
28 * (AdLink) ACL-8112DG [acl8112dg]
29 * Author: David A. Schleef <ds@schleef.org>
30 * Janne Jalkanen <jalkanen@cs.hut.fi>
31 * Eric Bunn <ebu@cs.hut.fi>
33 * Status: mostly complete
35 * Configuration Options:
40 #include <linux/module.h>
41 #include <linux/delay.h>
42 #include <linux/interrupt.h>
44 #include "../comedidev.h"
46 #include "comedi_fc.h"
50 * I/O port register map
52 #define PCL711_TIMER_BASE 0x00
53 #define PCL711_AI_LSB_REG 0x04
54 #define PCL711_AI_MSB_REG 0x05
55 #define PCL711_AI_MSB_DRDY (1 << 4)
56 #define PCL711_AO_LSB_REG(x) (0x04 + ((x) * 2))
57 #define PCL711_AO_MSB_REG(x) (0x05 + ((x) * 2))
58 #define PCL711_DI_LSB_REG 0x06
59 #define PCL711_DI_MSB_REG 0x07
60 #define PCL711_INT_STAT_REG 0x08
61 #define PCL711_INT_STAT_CLR (0 << 0) /* any value will work */
62 #define PCL711_AI_GAIN_REG 0x09
63 #define PCL711_AI_GAIN(x) (((x) & 0xf) << 0)
64 #define PCL711_MUX_REG 0x0a
65 #define PCL711_MUX_CHAN(x) (((x) & 0xf) << 0)
66 #define PCL711_MUX_CS0 (1 << 4)
67 #define PCL711_MUX_CS1 (1 << 5)
68 #define PCL711_MUX_DIFF (PCL711_MUX_CS0 | PCL711_MUX_CS1)
69 #define PCL711_MODE_REG 0x0b
70 #define PCL711_MODE_DEFAULT (0 << 0)
71 #define PCL711_MODE_SOFTTRIG (1 << 0)
72 #define PCL711_MODE_EXT (2 << 0)
73 #define PCL711_MODE_EXT_IRQ (3 << 0)
74 #define PCL711_MODE_PACER (4 << 0)
75 #define PCL711_MODE_PACER_IRQ (6 << 0)
76 #define PCL711_MODE_IRQ(x) (((x) & 0x7) << 4)
77 #define PCL711_SOFTTRIG_REG 0x0c
78 #define PCL711_SOFTTRIG (0 << 0) /* any value will work */
79 #define PCL711_DO_LSB_REG 0x0d
80 #define PCL711_DO_MSB_REG 0x0e
82 static const struct comedi_lrange range_pcl711b_ai = {
92 static const struct comedi_lrange range_acl8112hg_ai = {
109 static const struct comedi_lrange range_acl8112dg_ai = {
123 struct pcl711_board {
128 const struct comedi_lrange *ai_range_type;
131 static const struct pcl711_board boardtypes[] = {
136 .ai_range_type = &range_bipolar5,
142 .ai_range_type = &range_pcl711b_ai,
148 .ai_range_type = &range_acl8112hg_ai,
154 .ai_range_type = &range_acl8112dg_ai,
158 struct pcl711_private {
160 unsigned int ao_readback[2];
161 unsigned int divisor1;
162 unsigned int divisor2;
165 static void pcl711_ai_set_mode(struct comedi_device *dev, unsigned int mode)
168 * The pcl711b board uses bits in the mode register to select the
169 * interrupt. The other boards supported by this driver all use
170 * jumpers on the board.
172 * Enables the interrupt when needed on the pcl711b board. These
173 * bits do nothing on the other boards.
175 if (mode == PCL711_MODE_EXT_IRQ || mode == PCL711_MODE_PACER_IRQ)
176 mode |= PCL711_MODE_IRQ(dev->irq);
178 outb(mode, dev->iobase + PCL711_MODE_REG);
181 static unsigned int pcl711_ai_get_sample(struct comedi_device *dev,
182 struct comedi_subdevice *s)
186 val = inb(dev->iobase + PCL711_AI_MSB_REG) << 8;
187 val |= inb(dev->iobase + PCL711_AI_LSB_REG);
189 return val & s->maxdata;
192 static int pcl711_ai_cancel(struct comedi_device *dev,
193 struct comedi_subdevice *s)
195 outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
196 pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
200 static irqreturn_t pcl711_interrupt(int irq, void *d)
202 struct comedi_device *dev = d;
203 struct pcl711_private *devpriv = dev->private;
204 struct comedi_subdevice *s = dev->read_subdev;
207 if (!dev->attached) {
208 comedi_error(dev, "spurious interrupt");
212 data = pcl711_ai_get_sample(dev, s);
214 outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
216 if (comedi_buf_put(s->async, data) == 0) {
217 s->async->events |= COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
219 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
220 if (s->async->cmd.stop_src == TRIG_COUNT &&
221 !(--devpriv->ntrig)) {
222 pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
223 s->async->events |= COMEDI_CB_EOA;
226 comedi_event(dev, s);
230 static void pcl711_set_changain(struct comedi_device *dev,
231 struct comedi_subdevice *s,
232 unsigned int chanspec)
234 unsigned int chan = CR_CHAN(chanspec);
235 unsigned int range = CR_RANGE(chanspec);
236 unsigned int aref = CR_AREF(chanspec);
237 unsigned int mux = 0;
239 outb(PCL711_AI_GAIN(range), dev->iobase + PCL711_AI_GAIN_REG);
242 /* Select the correct MPC508A chip */
243 if (aref == AREF_DIFF) {
245 mux |= PCL711_MUX_DIFF;
248 mux |= PCL711_MUX_CS0;
250 mux |= PCL711_MUX_CS1;
253 outb(mux | PCL711_MUX_CHAN(chan), dev->iobase + PCL711_MUX_REG);
256 static int pcl711_ai_wait_for_eoc(struct comedi_device *dev,
257 unsigned int timeout)
262 msb = inb(dev->iobase + PCL711_AI_MSB_REG);
263 if ((msb & PCL711_AI_MSB_DRDY) == 0)
270 static int pcl711_ai_insn_read(struct comedi_device *dev,
271 struct comedi_subdevice *s,
272 struct comedi_insn *insn,
278 pcl711_set_changain(dev, s, insn->chanspec);
280 pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
282 for (i = 0; i < insn->n; i++) {
283 outb(PCL711_SOFTTRIG, dev->iobase + PCL711_SOFTTRIG_REG);
285 ret = pcl711_ai_wait_for_eoc(dev, 100);
289 data[i] = pcl711_ai_get_sample(dev, s);
295 static int pcl711_ai_cmdtest(struct comedi_device *dev,
296 struct comedi_subdevice *s, struct comedi_cmd *cmd)
298 struct pcl711_private *devpriv = dev->private;
302 /* Step 1 : check if triggers are trivially valid */
304 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
305 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
306 TRIG_TIMER | TRIG_EXT);
307 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
308 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
309 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
314 /* Step 2a : make sure trigger sources are unique */
316 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
317 err |= cfc_check_trigger_is_unique(cmd->stop_src);
319 /* Step 2b : and mutually compatible */
324 /* Step 3: check if arguments are trivially valid */
326 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
328 if (cmd->scan_begin_src == TRIG_EXT) {
329 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
331 #define MAX_SPEED 1000
332 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
336 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
337 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
339 if (cmd->stop_src == TRIG_NONE) {
340 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
350 if (cmd->scan_begin_src == TRIG_TIMER) {
351 tmp = cmd->scan_begin_arg;
352 i8253_cascade_ns_to_timer(I8254_OSC_BASE_2MHZ,
355 &cmd->scan_begin_arg,
357 if (tmp != cmd->scan_begin_arg)
367 static int pcl711_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
369 struct pcl711_private *devpriv = dev->private;
370 struct comedi_cmd *cmd = &s->async->cmd;
372 pcl711_set_changain(dev, s, cmd->chanlist[0]);
374 if (cmd->stop_src == TRIG_COUNT) {
375 if (cmd->stop_arg == 0) {
376 /* an empty acquisition */
377 s->async->events |= COMEDI_CB_EOA;
378 comedi_event(dev, s);
381 devpriv->ntrig = cmd->stop_arg;
384 if (cmd->scan_begin_src == TRIG_TIMER) {
385 i8254_load(dev->iobase + PCL711_TIMER_BASE, 0,
386 1, devpriv->divisor1, I8254_MODE2 | I8254_BINARY);
387 i8254_load(dev->iobase + PCL711_TIMER_BASE, 0,
388 2, devpriv->divisor2, I8254_MODE2 | I8254_BINARY);
390 outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
392 pcl711_ai_set_mode(dev, PCL711_MODE_PACER_IRQ);
394 pcl711_ai_set_mode(dev, PCL711_MODE_EXT_IRQ);
400 static void pcl711_ao_write(struct comedi_device *dev,
401 unsigned int chan, unsigned int val)
403 outb(val & 0xff, dev->iobase + PCL711_AO_LSB_REG(chan));
404 outb((val >> 8) & 0xff, dev->iobase + PCL711_AO_MSB_REG(chan));
407 static int pcl711_ao_insn_write(struct comedi_device *dev,
408 struct comedi_subdevice *s,
409 struct comedi_insn *insn,
412 struct pcl711_private *devpriv = dev->private;
413 unsigned int chan = CR_CHAN(insn->chanspec);
414 unsigned int val = devpriv->ao_readback[chan];
417 for (i = 0; i < insn->n; i++) {
419 pcl711_ao_write(dev, chan, val);
421 devpriv->ao_readback[chan] = val;
426 static int pcl711_ao_insn_read(struct comedi_device *dev,
427 struct comedi_subdevice *s,
428 struct comedi_insn *insn,
431 struct pcl711_private *devpriv = dev->private;
432 unsigned int chan = CR_CHAN(insn->chanspec);
435 for (i = 0; i < insn->n; i++)
436 data[i] = devpriv->ao_readback[chan];
441 static int pcl711_di_insn_bits(struct comedi_device *dev,
442 struct comedi_subdevice *s,
443 struct comedi_insn *insn,
448 val = inb(dev->iobase + PCL711_DI_LSB_REG);
449 val |= (inb(dev->iobase + PCL711_DI_MSB_REG) << 8);
456 static int pcl711_do_insn_bits(struct comedi_device *dev,
457 struct comedi_subdevice *s,
458 struct comedi_insn *insn,
463 mask = comedi_dio_update_state(s, data);
466 outb(s->state & 0xff, dev->iobase + PCL711_DO_LSB_REG);
468 outb((s->state >> 8), dev->iobase + PCL711_DO_MSB_REG);
476 static int pcl711_attach(struct comedi_device *dev, struct comedi_devconfig *it)
478 const struct pcl711_board *board = comedi_board(dev);
479 struct pcl711_private *devpriv;
480 struct comedi_subdevice *s;
483 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
487 ret = comedi_request_region(dev, it->options[0], 0x10);
491 if (it->options[1] && it->options[1] <= board->maxirq) {
492 ret = request_irq(it->options[1], pcl711_interrupt, 0,
493 dev->board_name, dev);
495 dev->irq = it->options[1];
498 ret = comedi_alloc_subdevices(dev, 4);
502 /* Analog Input subdevice */
503 s = &dev->subdevices[0];
504 s->type = COMEDI_SUBD_AI;
505 s->subdev_flags = SDF_READABLE | SDF_GROUND;
506 if (board->n_aichan > 8)
507 s->subdev_flags |= SDF_DIFF;
508 s->n_chan = board->n_aichan;
510 s->range_table = board->ai_range_type;
511 s->insn_read = pcl711_ai_insn_read;
513 dev->read_subdev = s;
514 s->subdev_flags |= SDF_CMD_READ;
516 s->do_cmdtest = pcl711_ai_cmdtest;
517 s->do_cmd = pcl711_ai_cmd;
518 s->cancel = pcl711_ai_cancel;
521 /* Analog Output subdevice */
522 s = &dev->subdevices[1];
523 s->type = COMEDI_SUBD_AO;
524 s->subdev_flags = SDF_WRITABLE;
525 s->n_chan = board->n_aochan;
527 s->range_table = &range_bipolar5;
528 s->insn_write = pcl711_ao_insn_write;
529 s->insn_read = pcl711_ao_insn_read;
531 /* Digital Input subdevice */
532 s = &dev->subdevices[2];
533 s->type = COMEDI_SUBD_DI;
534 s->subdev_flags = SDF_READABLE;
537 s->range_table = &range_digital;
538 s->insn_bits = pcl711_di_insn_bits;
540 /* Digital Output subdevice */
541 s = &dev->subdevices[3];
542 s->type = COMEDI_SUBD_DO;
543 s->subdev_flags = SDF_WRITABLE;
546 s->range_table = &range_digital;
547 s->insn_bits = pcl711_do_insn_bits;
550 pcl711_ao_write(dev, 0, 0x0);
551 pcl711_ao_write(dev, 1, 0x0);
556 static struct comedi_driver pcl711_driver = {
557 .driver_name = "pcl711",
558 .module = THIS_MODULE,
559 .attach = pcl711_attach,
560 .detach = comedi_legacy_detach,
561 .board_name = &boardtypes[0].name,
562 .num_names = ARRAY_SIZE(boardtypes),
563 .offset = sizeof(struct pcl711_board),
565 module_comedi_driver(pcl711_driver);
567 MODULE_AUTHOR("Comedi http://www.comedi.org");
568 MODULE_DESCRIPTION("Comedi driver for PCL-711 compatible boards");
569 MODULE_LICENSE("GPL");