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), PCL-711B (pcl711b),
26 * [ADLink] ACL-8112HG (acl8112hg), ACL-8112DG (acl8112dg)
27 * Author: David A. Schleef <ds@schleef.org>
28 * Janne Jalkanen <jalkanen@cs.hut.fi>
29 * Eric Bunn <ebu@cs.hut.fi>
31 * Status: mostly complete
33 * Configuration Options:
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/interrupt.h>
42 #include "../comedidev.h"
44 #include "comedi_8254.h"
47 * I/O port register map
49 #define PCL711_TIMER_BASE 0x00
50 #define PCL711_AI_LSB_REG 0x04
51 #define PCL711_AI_MSB_REG 0x05
52 #define PCL711_AI_MSB_DRDY (1 << 4)
53 #define PCL711_AO_LSB_REG(x) (0x04 + ((x) * 2))
54 #define PCL711_AO_MSB_REG(x) (0x05 + ((x) * 2))
55 #define PCL711_DI_LSB_REG 0x06
56 #define PCL711_DI_MSB_REG 0x07
57 #define PCL711_INT_STAT_REG 0x08
58 #define PCL711_INT_STAT_CLR (0 << 0) /* any value will work */
59 #define PCL711_AI_GAIN_REG 0x09
60 #define PCL711_AI_GAIN(x) (((x) & 0xf) << 0)
61 #define PCL711_MUX_REG 0x0a
62 #define PCL711_MUX_CHAN(x) (((x) & 0xf) << 0)
63 #define PCL711_MUX_CS0 (1 << 4)
64 #define PCL711_MUX_CS1 (1 << 5)
65 #define PCL711_MUX_DIFF (PCL711_MUX_CS0 | PCL711_MUX_CS1)
66 #define PCL711_MODE_REG 0x0b
67 #define PCL711_MODE_DEFAULT (0 << 0)
68 #define PCL711_MODE_SOFTTRIG (1 << 0)
69 #define PCL711_MODE_EXT (2 << 0)
70 #define PCL711_MODE_EXT_IRQ (3 << 0)
71 #define PCL711_MODE_PACER (4 << 0)
72 #define PCL711_MODE_PACER_IRQ (6 << 0)
73 #define PCL711_MODE_IRQ(x) (((x) & 0x7) << 4)
74 #define PCL711_SOFTTRIG_REG 0x0c
75 #define PCL711_SOFTTRIG (0 << 0) /* any value will work */
76 #define PCL711_DO_LSB_REG 0x0d
77 #define PCL711_DO_MSB_REG 0x0e
79 static const struct comedi_lrange range_pcl711b_ai = {
89 static const struct comedi_lrange range_acl8112hg_ai = {
106 static const struct comedi_lrange range_acl8112dg_ai = {
120 struct pcl711_board {
125 const struct comedi_lrange *ai_range_type;
128 static const struct pcl711_board boardtypes[] = {
133 .ai_range_type = &range_bipolar5,
139 .ai_range_type = &range_pcl711b_ai,
145 .ai_range_type = &range_acl8112hg_ai,
151 .ai_range_type = &range_acl8112dg_ai,
155 static void pcl711_ai_set_mode(struct comedi_device *dev, unsigned int mode)
158 * The pcl711b board uses bits in the mode register to select the
159 * interrupt. The other boards supported by this driver all use
160 * jumpers on the board.
162 * Enables the interrupt when needed on the pcl711b board. These
163 * bits do nothing on the other boards.
165 if (mode == PCL711_MODE_EXT_IRQ || mode == PCL711_MODE_PACER_IRQ)
166 mode |= PCL711_MODE_IRQ(dev->irq);
168 outb(mode, dev->iobase + PCL711_MODE_REG);
171 static unsigned int pcl711_ai_get_sample(struct comedi_device *dev,
172 struct comedi_subdevice *s)
176 val = inb(dev->iobase + PCL711_AI_MSB_REG) << 8;
177 val |= inb(dev->iobase + PCL711_AI_LSB_REG);
179 return val & s->maxdata;
182 static int pcl711_ai_cancel(struct comedi_device *dev,
183 struct comedi_subdevice *s)
185 outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
186 pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
190 static irqreturn_t pcl711_interrupt(int irq, void *d)
192 struct comedi_device *dev = d;
193 struct comedi_subdevice *s = dev->read_subdev;
194 struct comedi_cmd *cmd = &s->async->cmd;
197 if (!dev->attached) {
198 dev_err(dev->class_dev, "spurious interrupt\n");
202 data = pcl711_ai_get_sample(dev, s);
204 outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
206 comedi_buf_write_samples(s, &data, 1);
208 if (cmd->stop_src == TRIG_COUNT &&
209 s->async->scans_done >= cmd->stop_arg)
210 s->async->events |= COMEDI_CB_EOA;
212 comedi_handle_events(dev, s);
217 static void pcl711_set_changain(struct comedi_device *dev,
218 struct comedi_subdevice *s,
219 unsigned int chanspec)
221 unsigned int chan = CR_CHAN(chanspec);
222 unsigned int range = CR_RANGE(chanspec);
223 unsigned int aref = CR_AREF(chanspec);
224 unsigned int mux = 0;
226 outb(PCL711_AI_GAIN(range), dev->iobase + PCL711_AI_GAIN_REG);
229 /* Select the correct MPC508A chip */
230 if (aref == AREF_DIFF) {
232 mux |= PCL711_MUX_DIFF;
235 mux |= PCL711_MUX_CS0;
237 mux |= PCL711_MUX_CS1;
240 outb(mux | PCL711_MUX_CHAN(chan), dev->iobase + PCL711_MUX_REG);
243 static int pcl711_ai_eoc(struct comedi_device *dev,
244 struct comedi_subdevice *s,
245 struct comedi_insn *insn,
246 unsigned long context)
250 status = inb(dev->iobase + PCL711_AI_MSB_REG);
251 if ((status & PCL711_AI_MSB_DRDY) == 0)
256 static int pcl711_ai_insn_read(struct comedi_device *dev,
257 struct comedi_subdevice *s,
258 struct comedi_insn *insn,
264 pcl711_set_changain(dev, s, insn->chanspec);
266 pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
268 for (i = 0; i < insn->n; i++) {
269 outb(PCL711_SOFTTRIG, dev->iobase + PCL711_SOFTTRIG_REG);
271 ret = comedi_timeout(dev, s, insn, pcl711_ai_eoc, 0);
275 data[i] = pcl711_ai_get_sample(dev, s);
281 static int pcl711_ai_cmdtest(struct comedi_device *dev,
282 struct comedi_subdevice *s, struct comedi_cmd *cmd)
286 /* Step 1 : check if triggers are trivially valid */
288 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
289 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
290 TRIG_TIMER | TRIG_EXT);
291 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
292 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
293 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
298 /* Step 2a : make sure trigger sources are unique */
300 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
301 err |= comedi_check_trigger_is_unique(cmd->stop_src);
303 /* Step 2b : and mutually compatible */
308 /* Step 3: check if arguments are trivially valid */
310 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
312 if (cmd->scan_begin_src == TRIG_EXT) {
313 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
315 #define MAX_SPEED 1000
316 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
320 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
321 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
324 if (cmd->stop_src == TRIG_COUNT)
325 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
327 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
334 if (cmd->scan_begin_src == TRIG_TIMER) {
335 unsigned int arg = cmd->scan_begin_arg;
337 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
338 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
347 static int pcl711_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
349 struct comedi_cmd *cmd = &s->async->cmd;
351 pcl711_set_changain(dev, s, cmd->chanlist[0]);
353 if (cmd->scan_begin_src == TRIG_TIMER) {
354 comedi_8254_update_divisors(dev->pacer);
355 comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
356 outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
357 pcl711_ai_set_mode(dev, PCL711_MODE_PACER_IRQ);
359 pcl711_ai_set_mode(dev, PCL711_MODE_EXT_IRQ);
365 static void pcl711_ao_write(struct comedi_device *dev,
366 unsigned int chan, unsigned int val)
368 outb(val & 0xff, dev->iobase + PCL711_AO_LSB_REG(chan));
369 outb((val >> 8) & 0xff, dev->iobase + PCL711_AO_MSB_REG(chan));
372 static int pcl711_ao_insn_write(struct comedi_device *dev,
373 struct comedi_subdevice *s,
374 struct comedi_insn *insn,
377 unsigned int chan = CR_CHAN(insn->chanspec);
378 unsigned int val = s->readback[chan];
381 for (i = 0; i < insn->n; i++) {
383 pcl711_ao_write(dev, chan, val);
385 s->readback[chan] = val;
390 static int pcl711_di_insn_bits(struct comedi_device *dev,
391 struct comedi_subdevice *s,
392 struct comedi_insn *insn,
397 val = inb(dev->iobase + PCL711_DI_LSB_REG);
398 val |= (inb(dev->iobase + PCL711_DI_MSB_REG) << 8);
405 static int pcl711_do_insn_bits(struct comedi_device *dev,
406 struct comedi_subdevice *s,
407 struct comedi_insn *insn,
412 mask = comedi_dio_update_state(s, data);
415 outb(s->state & 0xff, dev->iobase + PCL711_DO_LSB_REG);
417 outb((s->state >> 8), dev->iobase + PCL711_DO_MSB_REG);
425 static int pcl711_attach(struct comedi_device *dev, struct comedi_devconfig *it)
427 const struct pcl711_board *board = dev->board_ptr;
428 struct comedi_subdevice *s;
431 ret = comedi_request_region(dev, it->options[0], 0x10);
435 if (it->options[1] && it->options[1] <= board->maxirq) {
436 ret = request_irq(it->options[1], pcl711_interrupt, 0,
437 dev->board_name, dev);
439 dev->irq = it->options[1];
442 dev->pacer = comedi_8254_init(dev->iobase + PCL711_TIMER_BASE,
443 I8254_OSC_BASE_2MHZ, I8254_IO8, 0);
447 ret = comedi_alloc_subdevices(dev, 4);
451 /* Analog Input subdevice */
452 s = &dev->subdevices[0];
453 s->type = COMEDI_SUBD_AI;
454 s->subdev_flags = SDF_READABLE | SDF_GROUND;
455 if (board->n_aichan > 8)
456 s->subdev_flags |= SDF_DIFF;
457 s->n_chan = board->n_aichan;
459 s->range_table = board->ai_range_type;
460 s->insn_read = pcl711_ai_insn_read;
462 dev->read_subdev = s;
463 s->subdev_flags |= SDF_CMD_READ;
465 s->do_cmdtest = pcl711_ai_cmdtest;
466 s->do_cmd = pcl711_ai_cmd;
467 s->cancel = pcl711_ai_cancel;
470 /* Analog Output subdevice */
471 s = &dev->subdevices[1];
472 s->type = COMEDI_SUBD_AO;
473 s->subdev_flags = SDF_WRITABLE;
474 s->n_chan = board->n_aochan;
476 s->range_table = &range_bipolar5;
477 s->insn_write = pcl711_ao_insn_write;
479 ret = comedi_alloc_subdev_readback(s);
483 /* Digital Input subdevice */
484 s = &dev->subdevices[2];
485 s->type = COMEDI_SUBD_DI;
486 s->subdev_flags = SDF_READABLE;
489 s->range_table = &range_digital;
490 s->insn_bits = pcl711_di_insn_bits;
492 /* Digital Output subdevice */
493 s = &dev->subdevices[3];
494 s->type = COMEDI_SUBD_DO;
495 s->subdev_flags = SDF_WRITABLE;
498 s->range_table = &range_digital;
499 s->insn_bits = pcl711_do_insn_bits;
502 pcl711_ao_write(dev, 0, 0x0);
503 pcl711_ao_write(dev, 1, 0x0);
508 static struct comedi_driver pcl711_driver = {
509 .driver_name = "pcl711",
510 .module = THIS_MODULE,
511 .attach = pcl711_attach,
512 .detach = comedi_legacy_detach,
513 .board_name = &boardtypes[0].name,
514 .num_names = ARRAY_SIZE(boardtypes),
515 .offset = sizeof(struct pcl711_board),
517 module_comedi_driver(pcl711_driver);
519 MODULE_AUTHOR("Comedi http://www.comedi.org");
520 MODULE_DESCRIPTION("Comedi driver for PCL-711 compatible boards");
521 MODULE_LICENSE("GPL");