3 * Comedi driver for 6/12-Channel D/A Output and DIO cards
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 * Description: Advantech PCL-726 & compatibles
22 * Author: David A. Schleef <ds@schleef.org>
24 * Devices: [Advantech] PCL-726 (pcl726), PCL-727 (pcl727), PCL-728 (pcl728),
25 * [ADLink] ACL-6126 (acl6126), ACL-6128 (acl6128)
27 * Configuration Options:
29 * [1] - IRQ (ACL-6126 only)
30 * [2] - D/A output range for channel 0
31 * [3] - D/A output range for channel 1
33 * Boards with > 2 analog output channels:
34 * [4] - D/A output range for channel 2
35 * [5] - D/A output range for channel 3
36 * [6] - D/A output range for channel 4
37 * [7] - D/A output range for channel 5
39 * Boards with > 6 analog output channels:
40 * [8] - D/A output range for channel 6
41 * [9] - D/A output range for channel 7
42 * [10] - D/A output range for channel 8
43 * [11] - D/A output range for channel 9
44 * [12] - D/A output range for channel 10
45 * [13] - D/A output range for channel 11
47 * For PCL-726 the D/A output ranges are:
48 * 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA, 5: unknown
51 * 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: 4-20mA
53 * For PCL-728 and ACL-6128:
54 * 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA, 5: 0-20mA
57 * 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA
60 #include <linux/module.h>
61 #include <linux/interrupt.h>
63 #include "../comedidev.h"
65 #define PCL726_AO_MSB_REG(x) (0x00 + ((x) * 2))
66 #define PCL726_AO_LSB_REG(x) (0x01 + ((x) * 2))
67 #define PCL726_DO_MSB_REG 0x0c
68 #define PCL726_DO_LSB_REG 0x0d
69 #define PCL726_DI_MSB_REG 0x0e
70 #define PCL726_DI_LSB_REG 0x0f
72 #define PCL727_DI_MSB_REG 0x00
73 #define PCL727_DI_LSB_REG 0x01
74 #define PCL727_DO_MSB_REG 0x18
75 #define PCL727_DO_LSB_REG 0x19
77 static const struct comedi_lrange *const rangelist_726[] = {
86 static const struct comedi_lrange *const rangelist_727[] = {
93 static const struct comedi_lrange *const rangelist_728[] = {
102 struct pcl726_board {
104 unsigned long io_len;
105 unsigned int irq_mask;
106 const struct comedi_lrange *const *ao_ranges;
109 unsigned int have_dio:1;
110 unsigned int is_pcl727:1;
113 static const struct pcl726_board pcl726_boards[] = {
117 .ao_ranges = &rangelist_726[0],
118 .ao_num_ranges = ARRAY_SIZE(rangelist_726),
124 .ao_ranges = &rangelist_727[0],
125 .ao_num_ranges = ARRAY_SIZE(rangelist_727),
132 .ao_num_ranges = ARRAY_SIZE(rangelist_728),
133 .ao_ranges = &rangelist_728[0],
139 .ao_num_ranges = ARRAY_SIZE(rangelist_726),
140 .ao_ranges = &rangelist_726[0],
146 .ao_num_ranges = ARRAY_SIZE(rangelist_728),
147 .ao_ranges = &rangelist_728[0],
152 struct pcl726_private {
153 const struct comedi_lrange *rangelist[12];
154 unsigned int cmd_running:1;
157 static int pcl726_intr_insn_bits(struct comedi_device *dev,
158 struct comedi_subdevice *s,
159 struct comedi_insn *insn,
166 static int pcl726_intr_cmdtest(struct comedi_device *dev,
167 struct comedi_subdevice *s,
168 struct comedi_cmd *cmd)
172 /* Step 1 : check if triggers are trivially valid */
174 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
175 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
176 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
177 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
178 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
183 /* Step 2a : make sure trigger sources are unique */
184 /* Step 2b : and mutually compatible */
186 /* Step 3: check if arguments are trivially valid */
188 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
189 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
190 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
191 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
193 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
198 /* Step 4: fix up any arguments */
200 /* Step 5: check channel list if it exists */
205 static int pcl726_intr_cmd(struct comedi_device *dev,
206 struct comedi_subdevice *s)
208 struct pcl726_private *devpriv = dev->private;
210 devpriv->cmd_running = 1;
215 static int pcl726_intr_cancel(struct comedi_device *dev,
216 struct comedi_subdevice *s)
218 struct pcl726_private *devpriv = dev->private;
220 devpriv->cmd_running = 0;
225 static irqreturn_t pcl726_interrupt(int irq, void *d)
227 struct comedi_device *dev = d;
228 struct comedi_subdevice *s = dev->read_subdev;
229 struct pcl726_private *devpriv = dev->private;
231 if (devpriv->cmd_running) {
232 pcl726_intr_cancel(dev, s);
234 comedi_buf_write_samples(s, &s->state, 1);
235 comedi_handle_events(dev, s);
241 static int pcl726_ao_insn_write(struct comedi_device *dev,
242 struct comedi_subdevice *s,
243 struct comedi_insn *insn,
246 unsigned int chan = CR_CHAN(insn->chanspec);
247 unsigned int range = CR_RANGE(insn->chanspec);
250 for (i = 0; i < insn->n; i++) {
251 unsigned int val = data[i];
253 s->readback[chan] = val;
255 /* bipolar data to the DAC is two's complement */
256 if (comedi_chan_range_is_bipolar(s, chan, range))
257 val = comedi_offset_munge(s, val);
259 /* order is important, MSB then LSB */
260 outb((val >> 8) & 0xff, dev->iobase + PCL726_AO_MSB_REG(chan));
261 outb(val & 0xff, dev->iobase + PCL726_AO_LSB_REG(chan));
267 static int pcl726_di_insn_bits(struct comedi_device *dev,
268 struct comedi_subdevice *s,
269 struct comedi_insn *insn,
272 const struct pcl726_board *board = dev->board_ptr;
275 if (board->is_pcl727) {
276 val = inb(dev->iobase + PCL727_DI_LSB_REG);
277 val |= (inb(dev->iobase + PCL727_DI_MSB_REG) << 8);
279 val = inb(dev->iobase + PCL726_DI_LSB_REG);
280 val |= (inb(dev->iobase + PCL726_DI_MSB_REG) << 8);
288 static int pcl726_do_insn_bits(struct comedi_device *dev,
289 struct comedi_subdevice *s,
290 struct comedi_insn *insn,
293 const struct pcl726_board *board = dev->board_ptr;
294 unsigned long io = dev->iobase;
297 mask = comedi_dio_update_state(s, data);
299 if (board->is_pcl727) {
301 outb(s->state & 0xff, io + PCL727_DO_LSB_REG);
303 outb((s->state >> 8), io + PCL727_DO_MSB_REG);
306 outb(s->state & 0xff, io + PCL726_DO_LSB_REG);
308 outb((s->state >> 8), io + PCL726_DO_MSB_REG);
317 static int pcl726_attach(struct comedi_device *dev,
318 struct comedi_devconfig *it)
320 const struct pcl726_board *board = dev->board_ptr;
321 struct pcl726_private *devpriv;
322 struct comedi_subdevice *s;
327 ret = comedi_request_region(dev, it->options[0], board->io_len);
331 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
336 * Hook up the external trigger source interrupt only if the
337 * user config option is valid and the board supports interrupts.
339 if (it->options[1] && (board->irq_mask & (1 << it->options[1]))) {
340 ret = request_irq(it->options[1], pcl726_interrupt, 0,
341 dev->board_name, dev);
343 /* External trigger source is from Pin-17 of CN3 */
344 dev->irq = it->options[1];
348 /* setup the per-channel analog output range_table_list */
349 for (i = 0; i < 12; i++) {
350 unsigned int opt = it->options[2 + i];
352 if (opt < board->ao_num_ranges && i < board->ao_nchan)
353 devpriv->rangelist[i] = board->ao_ranges[opt];
355 devpriv->rangelist[i] = &range_unknown;
358 subdev = board->have_dio ? 3 : 1;
361 ret = comedi_alloc_subdevices(dev, subdev);
367 /* Analog Output subdevice */
368 s = &dev->subdevices[subdev++];
369 s->type = COMEDI_SUBD_AO;
370 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
371 s->n_chan = board->ao_nchan;
373 s->range_table_list = devpriv->rangelist;
374 s->insn_write = pcl726_ao_insn_write;
376 ret = comedi_alloc_subdev_readback(s);
380 if (board->have_dio) {
381 /* Digital Input subdevice */
382 s = &dev->subdevices[subdev++];
383 s->type = COMEDI_SUBD_DI;
384 s->subdev_flags = SDF_READABLE;
387 s->insn_bits = pcl726_di_insn_bits;
388 s->range_table = &range_digital;
390 /* Digital Output subdevice */
391 s = &dev->subdevices[subdev++];
392 s->type = COMEDI_SUBD_DO;
393 s->subdev_flags = SDF_WRITABLE;
396 s->insn_bits = pcl726_do_insn_bits;
397 s->range_table = &range_digital;
401 /* Digial Input subdevice - Interrupt support */
402 s = &dev->subdevices[subdev++];
403 dev->read_subdev = s;
404 s->type = COMEDI_SUBD_DI;
405 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
408 s->range_table = &range_digital;
409 s->insn_bits = pcl726_intr_insn_bits;
411 s->do_cmdtest = pcl726_intr_cmdtest;
412 s->do_cmd = pcl726_intr_cmd;
413 s->cancel = pcl726_intr_cancel;
419 static struct comedi_driver pcl726_driver = {
420 .driver_name = "pcl726",
421 .module = THIS_MODULE,
422 .attach = pcl726_attach,
423 .detach = comedi_legacy_detach,
424 .board_name = &pcl726_boards[0].name,
425 .num_names = ARRAY_SIZE(pcl726_boards),
426 .offset = sizeof(struct pcl726_board),
428 module_comedi_driver(pcl726_driver);
430 MODULE_AUTHOR("Comedi http://www.comedi.org");
431 MODULE_DESCRIPTION("Comedi driver for Advantech PCL-726 & compatibles");
432 MODULE_LICENSE("GPL");