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>
53 #include <linux/comedi/comedidev.h>
55 #define PCL726_AO_MSB_REG(x) (0x00 + ((x) * 2))
56 #define PCL726_AO_LSB_REG(x) (0x01 + ((x) * 2))
57 #define PCL726_DO_MSB_REG 0x0c
58 #define PCL726_DO_LSB_REG 0x0d
59 #define PCL726_DI_MSB_REG 0x0e
60 #define PCL726_DI_LSB_REG 0x0f
62 #define PCL727_DI_MSB_REG 0x00
63 #define PCL727_DI_LSB_REG 0x01
64 #define PCL727_DO_MSB_REG 0x18
65 #define PCL727_DO_LSB_REG 0x19
67 static const struct comedi_lrange *const rangelist_726[] = {
76 static const struct comedi_lrange *const rangelist_727[] = {
83 static const struct comedi_lrange *const rangelist_728[] = {
95 unsigned int irq_mask;
96 const struct comedi_lrange *const *ao_ranges;
99 unsigned int have_dio:1;
100 unsigned int is_pcl727:1;
103 static const struct pcl726_board pcl726_boards[] = {
107 .ao_ranges = &rangelist_726[0],
108 .ao_num_ranges = ARRAY_SIZE(rangelist_726),
114 .ao_ranges = &rangelist_727[0],
115 .ao_num_ranges = ARRAY_SIZE(rangelist_727),
122 .ao_num_ranges = ARRAY_SIZE(rangelist_728),
123 .ao_ranges = &rangelist_728[0],
129 .ao_num_ranges = ARRAY_SIZE(rangelist_726),
130 .ao_ranges = &rangelist_726[0],
136 .ao_num_ranges = ARRAY_SIZE(rangelist_728),
137 .ao_ranges = &rangelist_728[0],
142 struct pcl726_private {
143 const struct comedi_lrange *rangelist[12];
144 unsigned int cmd_running:1;
147 static int pcl726_intr_insn_bits(struct comedi_device *dev,
148 struct comedi_subdevice *s,
149 struct comedi_insn *insn,
156 static int pcl726_intr_cmdtest(struct comedi_device *dev,
157 struct comedi_subdevice *s,
158 struct comedi_cmd *cmd)
162 /* Step 1 : check if triggers are trivially valid */
164 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
165 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
166 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
167 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
168 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
173 /* Step 2a : make sure trigger sources are unique */
174 /* Step 2b : and mutually compatible */
176 /* Step 3: check if arguments are trivially valid */
178 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
179 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
180 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
181 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
183 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
188 /* Step 4: fix up any arguments */
190 /* Step 5: check channel list if it exists */
195 static int pcl726_intr_cmd(struct comedi_device *dev,
196 struct comedi_subdevice *s)
198 struct pcl726_private *devpriv = dev->private;
200 devpriv->cmd_running = 1;
205 static int pcl726_intr_cancel(struct comedi_device *dev,
206 struct comedi_subdevice *s)
208 struct pcl726_private *devpriv = dev->private;
210 devpriv->cmd_running = 0;
215 static irqreturn_t pcl726_interrupt(int irq, void *d)
217 struct comedi_device *dev = d;
218 struct comedi_subdevice *s = dev->read_subdev;
219 struct pcl726_private *devpriv = dev->private;
221 if (devpriv->cmd_running) {
222 unsigned short val = 0;
224 pcl726_intr_cancel(dev, s);
226 comedi_buf_write_samples(s, &val, 1);
227 comedi_handle_events(dev, s);
233 static int pcl726_ao_insn_write(struct comedi_device *dev,
234 struct comedi_subdevice *s,
235 struct comedi_insn *insn,
238 unsigned int chan = CR_CHAN(insn->chanspec);
239 unsigned int range = CR_RANGE(insn->chanspec);
242 for (i = 0; i < insn->n; i++) {
243 unsigned int val = data[i];
245 s->readback[chan] = val;
247 /* bipolar data to the DAC is two's complement */
248 if (comedi_chan_range_is_bipolar(s, chan, range))
249 val = comedi_offset_munge(s, val);
251 /* order is important, MSB then LSB */
252 outb((val >> 8) & 0xff, dev->iobase + PCL726_AO_MSB_REG(chan));
253 outb(val & 0xff, dev->iobase + PCL726_AO_LSB_REG(chan));
259 static int pcl726_di_insn_bits(struct comedi_device *dev,
260 struct comedi_subdevice *s,
261 struct comedi_insn *insn,
264 const struct pcl726_board *board = dev->board_ptr;
267 if (board->is_pcl727) {
268 val = inb(dev->iobase + PCL727_DI_LSB_REG);
269 val |= (inb(dev->iobase + PCL727_DI_MSB_REG) << 8);
271 val = inb(dev->iobase + PCL726_DI_LSB_REG);
272 val |= (inb(dev->iobase + PCL726_DI_MSB_REG) << 8);
280 static int pcl726_do_insn_bits(struct comedi_device *dev,
281 struct comedi_subdevice *s,
282 struct comedi_insn *insn,
285 const struct pcl726_board *board = dev->board_ptr;
286 unsigned long io = dev->iobase;
289 mask = comedi_dio_update_state(s, data);
291 if (board->is_pcl727) {
293 outb(s->state & 0xff, io + PCL727_DO_LSB_REG);
295 outb((s->state >> 8), io + PCL727_DO_MSB_REG);
298 outb(s->state & 0xff, io + PCL726_DO_LSB_REG);
300 outb((s->state >> 8), io + PCL726_DO_MSB_REG);
309 static int pcl726_attach(struct comedi_device *dev,
310 struct comedi_devconfig *it)
312 const struct pcl726_board *board = dev->board_ptr;
313 struct pcl726_private *devpriv;
314 struct comedi_subdevice *s;
319 ret = comedi_request_region(dev, it->options[0], board->io_len);
323 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
328 * Hook up the external trigger source interrupt only if the
329 * user config option is valid and the board supports interrupts.
331 if (it->options[1] && (board->irq_mask & (1 << it->options[1]))) {
332 ret = request_irq(it->options[1], pcl726_interrupt, 0,
333 dev->board_name, dev);
335 /* External trigger source is from Pin-17 of CN3 */
336 dev->irq = it->options[1];
340 /* setup the per-channel analog output range_table_list */
341 for (i = 0; i < 12; i++) {
342 unsigned int opt = it->options[2 + i];
344 if (opt < board->ao_num_ranges && i < board->ao_nchan)
345 devpriv->rangelist[i] = board->ao_ranges[opt];
347 devpriv->rangelist[i] = &range_unknown;
350 subdev = board->have_dio ? 3 : 1;
353 ret = comedi_alloc_subdevices(dev, subdev);
359 /* Analog Output subdevice */
360 s = &dev->subdevices[subdev++];
361 s->type = COMEDI_SUBD_AO;
362 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
363 s->n_chan = board->ao_nchan;
365 s->range_table_list = devpriv->rangelist;
366 s->insn_write = pcl726_ao_insn_write;
368 ret = comedi_alloc_subdev_readback(s);
372 if (board->have_dio) {
373 /* Digital Input subdevice */
374 s = &dev->subdevices[subdev++];
375 s->type = COMEDI_SUBD_DI;
376 s->subdev_flags = SDF_READABLE;
379 s->insn_bits = pcl726_di_insn_bits;
380 s->range_table = &range_digital;
382 /* Digital Output subdevice */
383 s = &dev->subdevices[subdev++];
384 s->type = COMEDI_SUBD_DO;
385 s->subdev_flags = SDF_WRITABLE;
388 s->insn_bits = pcl726_do_insn_bits;
389 s->range_table = &range_digital;
393 /* Digital Input subdevice - Interrupt support */
394 s = &dev->subdevices[subdev++];
395 dev->read_subdev = s;
396 s->type = COMEDI_SUBD_DI;
397 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
400 s->range_table = &range_digital;
401 s->insn_bits = pcl726_intr_insn_bits;
403 s->do_cmdtest = pcl726_intr_cmdtest;
404 s->do_cmd = pcl726_intr_cmd;
405 s->cancel = pcl726_intr_cancel;
411 static struct comedi_driver pcl726_driver = {
412 .driver_name = "pcl726",
413 .module = THIS_MODULE,
414 .attach = pcl726_attach,
415 .detach = comedi_legacy_detach,
416 .board_name = &pcl726_boards[0].name,
417 .num_names = ARRAY_SIZE(pcl726_boards),
418 .offset = sizeof(struct pcl726_board),
420 module_comedi_driver(pcl726_driver);
422 MODULE_AUTHOR("Comedi https://www.comedi.org");
423 MODULE_DESCRIPTION("Comedi driver for Advantech PCL-726 & compatibles");
424 MODULE_LICENSE("GPL");