1 // SPDX-License-Identifier: GPL-2.0+
4 * Comedi driver for 6/12-Channel D/A Output and DIO cards
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
12 * Description: Advantech PCL-726 & compatibles
13 * Author: David A. Schleef <ds@schleef.org>
15 * Devices: [Advantech] PCL-726 (pcl726), PCL-727 (pcl727), PCL-728 (pcl728),
16 * [ADLink] ACL-6126 (acl6126), ACL-6128 (acl6128)
18 * Configuration Options:
20 * [1] - IRQ (ACL-6126 only)
21 * [2] - D/A output range for channel 0
22 * [3] - D/A output range for channel 1
24 * Boards with > 2 analog output channels:
25 * [4] - D/A output range for channel 2
26 * [5] - D/A output range for channel 3
27 * [6] - D/A output range for channel 4
28 * [7] - D/A output range for channel 5
30 * Boards with > 6 analog output channels:
31 * [8] - D/A output range for channel 6
32 * [9] - D/A output range for channel 7
33 * [10] - D/A output range for channel 8
34 * [11] - D/A output range for channel 9
35 * [12] - D/A output range for channel 10
36 * [13] - D/A output range for channel 11
38 * For PCL-726 the D/A output ranges are:
39 * 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA, 5: unknown
42 * 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: 4-20mA
44 * For PCL-728 and ACL-6128:
45 * 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA, 5: 0-20mA
48 * 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA
51 #include <linux/module.h>
52 #include <linux/interrupt.h>
54 #include "../comedidev.h"
56 #define PCL726_AO_MSB_REG(x) (0x00 + ((x) * 2))
57 #define PCL726_AO_LSB_REG(x) (0x01 + ((x) * 2))
58 #define PCL726_DO_MSB_REG 0x0c
59 #define PCL726_DO_LSB_REG 0x0d
60 #define PCL726_DI_MSB_REG 0x0e
61 #define PCL726_DI_LSB_REG 0x0f
63 #define PCL727_DI_MSB_REG 0x00
64 #define PCL727_DI_LSB_REG 0x01
65 #define PCL727_DO_MSB_REG 0x18
66 #define PCL727_DO_LSB_REG 0x19
68 static const struct comedi_lrange *const rangelist_726[] = {
77 static const struct comedi_lrange *const rangelist_727[] = {
84 static const struct comedi_lrange *const rangelist_728[] = {
96 unsigned int irq_mask;
97 const struct comedi_lrange *const *ao_ranges;
100 unsigned int have_dio:1;
101 unsigned int is_pcl727:1;
104 static const struct pcl726_board pcl726_boards[] = {
108 .ao_ranges = &rangelist_726[0],
109 .ao_num_ranges = ARRAY_SIZE(rangelist_726),
115 .ao_ranges = &rangelist_727[0],
116 .ao_num_ranges = ARRAY_SIZE(rangelist_727),
123 .ao_num_ranges = ARRAY_SIZE(rangelist_728),
124 .ao_ranges = &rangelist_728[0],
130 .ao_num_ranges = ARRAY_SIZE(rangelist_726),
131 .ao_ranges = &rangelist_726[0],
137 .ao_num_ranges = ARRAY_SIZE(rangelist_728),
138 .ao_ranges = &rangelist_728[0],
143 struct pcl726_private {
144 const struct comedi_lrange *rangelist[12];
145 unsigned int cmd_running:1;
148 static int pcl726_intr_insn_bits(struct comedi_device *dev,
149 struct comedi_subdevice *s,
150 struct comedi_insn *insn,
157 static int pcl726_intr_cmdtest(struct comedi_device *dev,
158 struct comedi_subdevice *s,
159 struct comedi_cmd *cmd)
163 /* Step 1 : check if triggers are trivially valid */
165 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
166 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
167 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
168 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
169 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
174 /* Step 2a : make sure trigger sources are unique */
175 /* Step 2b : and mutually compatible */
177 /* Step 3: check if arguments are trivially valid */
179 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
180 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
181 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
182 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
184 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
189 /* Step 4: fix up any arguments */
191 /* Step 5: check channel list if it exists */
196 static int pcl726_intr_cmd(struct comedi_device *dev,
197 struct comedi_subdevice *s)
199 struct pcl726_private *devpriv = dev->private;
201 devpriv->cmd_running = 1;
206 static int pcl726_intr_cancel(struct comedi_device *dev,
207 struct comedi_subdevice *s)
209 struct pcl726_private *devpriv = dev->private;
211 devpriv->cmd_running = 0;
216 static irqreturn_t pcl726_interrupt(int irq, void *d)
218 struct comedi_device *dev = d;
219 struct comedi_subdevice *s = dev->read_subdev;
220 struct pcl726_private *devpriv = dev->private;
222 if (devpriv->cmd_running) {
223 unsigned short val = 0;
225 pcl726_intr_cancel(dev, s);
227 comedi_buf_write_samples(s, &val, 1);
228 comedi_handle_events(dev, s);
234 static int pcl726_ao_insn_write(struct comedi_device *dev,
235 struct comedi_subdevice *s,
236 struct comedi_insn *insn,
239 unsigned int chan = CR_CHAN(insn->chanspec);
240 unsigned int range = CR_RANGE(insn->chanspec);
243 for (i = 0; i < insn->n; i++) {
244 unsigned int val = data[i];
246 s->readback[chan] = val;
248 /* bipolar data to the DAC is two's complement */
249 if (comedi_chan_range_is_bipolar(s, chan, range))
250 val = comedi_offset_munge(s, val);
252 /* order is important, MSB then LSB */
253 outb((val >> 8) & 0xff, dev->iobase + PCL726_AO_MSB_REG(chan));
254 outb(val & 0xff, dev->iobase + PCL726_AO_LSB_REG(chan));
260 static int pcl726_di_insn_bits(struct comedi_device *dev,
261 struct comedi_subdevice *s,
262 struct comedi_insn *insn,
265 const struct pcl726_board *board = dev->board_ptr;
268 if (board->is_pcl727) {
269 val = inb(dev->iobase + PCL727_DI_LSB_REG);
270 val |= (inb(dev->iobase + PCL727_DI_MSB_REG) << 8);
272 val = inb(dev->iobase + PCL726_DI_LSB_REG);
273 val |= (inb(dev->iobase + PCL726_DI_MSB_REG) << 8);
281 static int pcl726_do_insn_bits(struct comedi_device *dev,
282 struct comedi_subdevice *s,
283 struct comedi_insn *insn,
286 const struct pcl726_board *board = dev->board_ptr;
287 unsigned long io = dev->iobase;
290 mask = comedi_dio_update_state(s, data);
292 if (board->is_pcl727) {
294 outb(s->state & 0xff, io + PCL727_DO_LSB_REG);
296 outb((s->state >> 8), io + PCL727_DO_MSB_REG);
299 outb(s->state & 0xff, io + PCL726_DO_LSB_REG);
301 outb((s->state >> 8), io + PCL726_DO_MSB_REG);
310 static int pcl726_attach(struct comedi_device *dev,
311 struct comedi_devconfig *it)
313 const struct pcl726_board *board = dev->board_ptr;
314 struct pcl726_private *devpriv;
315 struct comedi_subdevice *s;
320 ret = comedi_request_region(dev, it->options[0], board->io_len);
324 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
329 * Hook up the external trigger source interrupt only if the
330 * user config option is valid and the board supports interrupts.
332 if (it->options[1] && (board->irq_mask & (1 << it->options[1]))) {
333 ret = request_irq(it->options[1], pcl726_interrupt, 0,
334 dev->board_name, dev);
336 /* External trigger source is from Pin-17 of CN3 */
337 dev->irq = it->options[1];
341 /* setup the per-channel analog output range_table_list */
342 for (i = 0; i < 12; i++) {
343 unsigned int opt = it->options[2 + i];
345 if (opt < board->ao_num_ranges && i < board->ao_nchan)
346 devpriv->rangelist[i] = board->ao_ranges[opt];
348 devpriv->rangelist[i] = &range_unknown;
351 subdev = board->have_dio ? 3 : 1;
354 ret = comedi_alloc_subdevices(dev, subdev);
360 /* Analog Output subdevice */
361 s = &dev->subdevices[subdev++];
362 s->type = COMEDI_SUBD_AO;
363 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
364 s->n_chan = board->ao_nchan;
366 s->range_table_list = devpriv->rangelist;
367 s->insn_write = pcl726_ao_insn_write;
369 ret = comedi_alloc_subdev_readback(s);
373 if (board->have_dio) {
374 /* Digital Input subdevice */
375 s = &dev->subdevices[subdev++];
376 s->type = COMEDI_SUBD_DI;
377 s->subdev_flags = SDF_READABLE;
380 s->insn_bits = pcl726_di_insn_bits;
381 s->range_table = &range_digital;
383 /* Digital Output subdevice */
384 s = &dev->subdevices[subdev++];
385 s->type = COMEDI_SUBD_DO;
386 s->subdev_flags = SDF_WRITABLE;
389 s->insn_bits = pcl726_do_insn_bits;
390 s->range_table = &range_digital;
394 /* Digital Input subdevice - Interrupt support */
395 s = &dev->subdevices[subdev++];
396 dev->read_subdev = s;
397 s->type = COMEDI_SUBD_DI;
398 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
401 s->range_table = &range_digital;
402 s->insn_bits = pcl726_intr_insn_bits;
404 s->do_cmdtest = pcl726_intr_cmdtest;
405 s->do_cmd = pcl726_intr_cmd;
406 s->cancel = pcl726_intr_cancel;
412 static struct comedi_driver pcl726_driver = {
413 .driver_name = "pcl726",
414 .module = THIS_MODULE,
415 .attach = pcl726_attach,
416 .detach = comedi_legacy_detach,
417 .board_name = &pcl726_boards[0].name,
418 .num_names = ARRAY_SIZE(pcl726_boards),
419 .offset = sizeof(struct pcl726_board),
421 module_comedi_driver(pcl726_driver);
423 MODULE_AUTHOR("Comedi https://www.comedi.org");
424 MODULE_DESCRIPTION("Comedi driver for Advantech PCL-726 & compatibles");
425 MODULE_LICENSE("GPL");