1 // SPDX-License-Identifier: GPL-2.0+
4 * Comedi driver for Winsystems PC-104 based 48/96-channel DIO boards.
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 2006 Calin A. Culianu <calin@ajvar.org>
12 * Description: Winsystems PC-104 based 48/96-channel DIO boards.
13 * Devices: [Winsystems] PCM-UIO48A (pcmuio48), PCM-UIO96A (pcmuio96)
14 * Author: Calin Culianu <calin@ajvar.org>
15 * Updated: Fri, 13 Jan 2006 12:01:01 -0500
18 * A driver for the relatively straightforward-to-program PCM-UIO48A and
19 * PCM-UIO96A boards from Winsystems. These boards use either one or two
20 * (in the 96-DIO version) WS16C48 ASIC HighDensity I/O Chips (HDIO). This
21 * chip is interesting in that each I/O line is individually programmable
22 * for INPUT or OUTPUT (thus comedi_dio_config can be done on a per-channel
23 * basis). Also, each chip supports edge-triggered interrupts for the first
24 * 24 I/O lines. Of course, since the 96-channel version of the board has
25 * two ASICs, it can detect polarity changes on up to 48 I/O lines. Since
26 * this is essentially an (non-PnP) ISA board, I/O Address and IRQ selection
27 * are done through jumpers on the board. You need to pass that information
28 * to this driver as the first and second comedi_config option, respectively.
29 * Note that the 48-channel version uses 16 bytes of IO memory and the 96-
30 * channel version uses 32-bytes (in case you are worried about conflicts).
31 * The 48-channel board is split into two 24-channel comedi subdevices. The
32 * 96-channel board is split into 4 24-channel DIO subdevices.
34 * Note that IRQ support has been added, but it is untested.
36 * To use edge-detection IRQ support, pass the IRQs of both ASICS (for the
37 * 96 channel version) or just 1 ASIC (for 48-channel version). Then, use
38 * comedi_commands with TRIG_NOW. Your callback will be called each time an
39 * edge is triggered, and the data values will be two sample_t's, which
40 * should be concatenated to form one 32-bit unsigned int. This value is
41 * the mask of channels that had edges detected from your channel list. Note
42 * that the bits positions in the mask correspond to positions in your
43 * chanlist when you specified the command and *not* channel id's!
45 * To set the polarity of the edge-detection interrupts pass a nonzero value
46 * for either CR_RANGE or CR_AREF for edge-up polarity, or a zero value for
47 * both CR_RANGE and CR_AREF if you want edge-down polarity.
49 * In the 48-channel version:
51 * On subdev 0, the first 24 channels are edge-detect channels.
53 * In the 96-channel board you have the following channels that can do edge
56 * subdev 0, channels 0-24 (first 24 channels of 1st ASIC)
57 * subdev 2, channels 0-24 (first 24 channels of 2nd ASIC)
59 * Configuration Options:
60 * [0] - I/O port base address
61 * [1] - IRQ (for first ASIC, or first 24 channels)
62 * [2] - IRQ (for second ASIC, pcmuio96 only - IRQ for chans 48-72
63 * can be the same as first irq!)
66 #include <linux/module.h>
67 #include <linux/interrupt.h>
69 #include "../comedidev.h"
74 * Offset Page 0 Page 1 Page 2 Page 3
75 * ------ ----------- ----------- ----------- -----------
76 * 0x00 Port 0 I/O Port 0 I/O Port 0 I/O Port 0 I/O
77 * 0x01 Port 1 I/O Port 1 I/O Port 1 I/O Port 1 I/O
78 * 0x02 Port 2 I/O Port 2 I/O Port 2 I/O Port 2 I/O
79 * 0x03 Port 3 I/O Port 3 I/O Port 3 I/O Port 3 I/O
80 * 0x04 Port 4 I/O Port 4 I/O Port 4 I/O Port 4 I/O
81 * 0x05 Port 5 I/O Port 5 I/O Port 5 I/O Port 5 I/O
82 * 0x06 INT_PENDING INT_PENDING INT_PENDING INT_PENDING
83 * 0x07 Page/Lock Page/Lock Page/Lock Page/Lock
84 * 0x08 N/A POL_0 ENAB_0 INT_ID0
85 * 0x09 N/A POL_1 ENAB_1 INT_ID1
86 * 0x0a N/A POL_2 ENAB_2 INT_ID2
88 #define PCMUIO_PORT_REG(x) (0x00 + (x))
89 #define PCMUIO_INT_PENDING_REG 0x06
90 #define PCMUIO_PAGE_LOCK_REG 0x07
91 #define PCMUIO_LOCK_PORT(x) ((1 << (x)) & 0x3f)
92 #define PCMUIO_PAGE(x) (((x) & 0x3) << 6)
93 #define PCMUIO_PAGE_MASK PCMUIO_PAGE(3)
94 #define PCMUIO_PAGE_POL 1
95 #define PCMUIO_PAGE_ENAB 2
96 #define PCMUIO_PAGE_INT_ID 3
97 #define PCMUIO_PAGE_REG(x) (0x08 + (x))
99 #define PCMUIO_ASIC_IOSIZE 0x10
100 #define PCMUIO_MAX_ASICS 2
102 struct pcmuio_board {
107 static const struct pcmuio_board pcmuio_boards[] = {
118 spinlock_t pagelock; /* protects the page registers */
119 spinlock_t spinlock; /* protects member variables */
120 unsigned int enabled_mask;
121 unsigned int active:1;
124 struct pcmuio_private {
125 struct pcmuio_asic asics[PCMUIO_MAX_ASICS];
129 static inline unsigned long pcmuio_asic_iobase(struct comedi_device *dev,
132 return dev->iobase + (asic * PCMUIO_ASIC_IOSIZE);
135 static inline int pcmuio_subdevice_to_asic(struct comedi_subdevice *s)
138 * subdevice 0 and 1 are handled by the first asic
139 * subdevice 2 and 3 are handled by the second asic
144 static inline int pcmuio_subdevice_to_port(struct comedi_subdevice *s)
147 * subdevice 0 and 2 use port registers 0-2
148 * subdevice 1 and 3 use port registers 3-5
150 return (s->index % 2) ? 3 : 0;
153 static void pcmuio_write(struct comedi_device *dev, unsigned int val,
154 int asic, int page, int port)
156 struct pcmuio_private *devpriv = dev->private;
157 struct pcmuio_asic *chip = &devpriv->asics[asic];
158 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
161 spin_lock_irqsave(&chip->pagelock, flags);
163 /* Port registers are valid for any page */
164 outb(val & 0xff, iobase + PCMUIO_PORT_REG(port + 0));
165 outb((val >> 8) & 0xff, iobase + PCMUIO_PORT_REG(port + 1));
166 outb((val >> 16) & 0xff, iobase + PCMUIO_PORT_REG(port + 2));
168 outb(PCMUIO_PAGE(page), iobase + PCMUIO_PAGE_LOCK_REG);
169 outb(val & 0xff, iobase + PCMUIO_PAGE_REG(0));
170 outb((val >> 8) & 0xff, iobase + PCMUIO_PAGE_REG(1));
171 outb((val >> 16) & 0xff, iobase + PCMUIO_PAGE_REG(2));
173 spin_unlock_irqrestore(&chip->pagelock, flags);
176 static unsigned int pcmuio_read(struct comedi_device *dev,
177 int asic, int page, int port)
179 struct pcmuio_private *devpriv = dev->private;
180 struct pcmuio_asic *chip = &devpriv->asics[asic];
181 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
185 spin_lock_irqsave(&chip->pagelock, flags);
187 /* Port registers are valid for any page */
188 val = inb(iobase + PCMUIO_PORT_REG(port + 0));
189 val |= (inb(iobase + PCMUIO_PORT_REG(port + 1)) << 8);
190 val |= (inb(iobase + PCMUIO_PORT_REG(port + 2)) << 16);
192 outb(PCMUIO_PAGE(page), iobase + PCMUIO_PAGE_LOCK_REG);
193 val = inb(iobase + PCMUIO_PAGE_REG(0));
194 val |= (inb(iobase + PCMUIO_PAGE_REG(1)) << 8);
195 val |= (inb(iobase + PCMUIO_PAGE_REG(2)) << 16);
197 spin_unlock_irqrestore(&chip->pagelock, flags);
203 * Each channel can be individually programmed for input or output.
204 * Writing a '0' to a channel causes the corresponding output pin
205 * to go to a high-z state (pulled high by an external 10K resistor).
206 * This allows it to be used as an input. When used in the input mode,
207 * a read reflects the inverted state of the I/O pin, such that a
208 * high on the pin will read as a '0' in the register. Writing a '1'
209 * to a bit position causes the pin to sink current (up to 12mA),
210 * effectively pulling it low.
212 static int pcmuio_dio_insn_bits(struct comedi_device *dev,
213 struct comedi_subdevice *s,
214 struct comedi_insn *insn,
217 int asic = pcmuio_subdevice_to_asic(s);
218 int port = pcmuio_subdevice_to_port(s);
219 unsigned int chanmask = (1 << s->n_chan) - 1;
223 mask = comedi_dio_update_state(s, data);
226 * Outputs are inverted, invert the state and
227 * update the channels.
229 * The s->io_bits mask makes sure the input channels
230 * are '0' so that the outputs pins stay in a high
233 val = ~s->state & chanmask;
235 pcmuio_write(dev, val, asic, 0, port);
238 /* get inverted state of the channels from the port */
239 val = pcmuio_read(dev, asic, 0, port);
241 /* return the true state of the channels */
242 data[1] = ~val & chanmask;
247 static int pcmuio_dio_insn_config(struct comedi_device *dev,
248 struct comedi_subdevice *s,
249 struct comedi_insn *insn,
252 int asic = pcmuio_subdevice_to_asic(s);
253 int port = pcmuio_subdevice_to_port(s);
256 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
260 if (data[0] == INSN_CONFIG_DIO_INPUT)
261 pcmuio_write(dev, s->io_bits, asic, 0, port);
266 static void pcmuio_reset(struct comedi_device *dev)
268 const struct pcmuio_board *board = dev->board_ptr;
271 for (asic = 0; asic < board->num_asics; ++asic) {
272 /* first, clear all the DIO port bits */
273 pcmuio_write(dev, 0, asic, 0, 0);
274 pcmuio_write(dev, 0, asic, 0, 3);
276 /* Next, clear all the paged registers for each page */
277 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_POL, 0);
278 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_ENAB, 0);
279 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_INT_ID, 0);
283 /* chip->spinlock is already locked */
284 static void pcmuio_stop_intr(struct comedi_device *dev,
285 struct comedi_subdevice *s)
287 struct pcmuio_private *devpriv = dev->private;
288 int asic = pcmuio_subdevice_to_asic(s);
289 struct pcmuio_asic *chip = &devpriv->asics[asic];
291 chip->enabled_mask = 0;
293 s->async->inttrig = NULL;
295 /* disable all intrs for this subdev.. */
296 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_ENAB, 0);
299 static void pcmuio_handle_intr_subdev(struct comedi_device *dev,
300 struct comedi_subdevice *s,
301 unsigned int triggered)
303 struct pcmuio_private *devpriv = dev->private;
304 int asic = pcmuio_subdevice_to_asic(s);
305 struct pcmuio_asic *chip = &devpriv->asics[asic];
306 struct comedi_cmd *cmd = &s->async->cmd;
307 unsigned int val = 0;
311 spin_lock_irqsave(&chip->spinlock, flags);
316 if (!(triggered & chip->enabled_mask))
319 for (i = 0; i < cmd->chanlist_len; i++) {
320 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
322 if (triggered & (1 << chan))
326 comedi_buf_write_samples(s, &val, 1);
328 if (cmd->stop_src == TRIG_COUNT &&
329 s->async->scans_done >= cmd->stop_arg)
330 s->async->events |= COMEDI_CB_EOA;
333 spin_unlock_irqrestore(&chip->spinlock, flags);
335 comedi_handle_events(dev, s);
338 static int pcmuio_handle_asic_interrupt(struct comedi_device *dev, int asic)
340 /* there are could be two asics so we can't use dev->read_subdev */
341 struct comedi_subdevice *s = &dev->subdevices[asic * 2];
342 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
345 /* are there any interrupts pending */
346 val = inb(iobase + PCMUIO_INT_PENDING_REG) & 0x07;
350 /* get, and clear, the pending interrupts */
351 val = pcmuio_read(dev, asic, PCMUIO_PAGE_INT_ID, 0);
352 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_INT_ID, 0);
354 /* handle the pending interrupts */
355 pcmuio_handle_intr_subdev(dev, s, val);
360 static irqreturn_t pcmuio_interrupt(int irq, void *d)
362 struct comedi_device *dev = d;
363 struct pcmuio_private *devpriv = dev->private;
367 handled += pcmuio_handle_asic_interrupt(dev, 0);
368 if (irq == devpriv->irq2)
369 handled += pcmuio_handle_asic_interrupt(dev, 1);
371 return handled ? IRQ_HANDLED : IRQ_NONE;
374 /* chip->spinlock is already locked */
375 static void pcmuio_start_intr(struct comedi_device *dev,
376 struct comedi_subdevice *s)
378 struct pcmuio_private *devpriv = dev->private;
379 int asic = pcmuio_subdevice_to_asic(s);
380 struct pcmuio_asic *chip = &devpriv->asics[asic];
381 struct comedi_cmd *cmd = &s->async->cmd;
382 unsigned int bits = 0;
383 unsigned int pol_bits = 0;
386 chip->enabled_mask = 0;
389 for (i = 0; i < cmd->chanlist_len; i++) {
390 unsigned int chanspec = cmd->chanlist[i];
391 unsigned int chan = CR_CHAN(chanspec);
392 unsigned int range = CR_RANGE(chanspec);
393 unsigned int aref = CR_AREF(chanspec);
396 pol_bits |= ((aref || range) ? 1 : 0) << chan;
399 bits &= ((1 << s->n_chan) - 1);
400 chip->enabled_mask = bits;
402 /* set pol and enab intrs for this subdev.. */
403 pcmuio_write(dev, pol_bits, asic, PCMUIO_PAGE_POL, 0);
404 pcmuio_write(dev, bits, asic, PCMUIO_PAGE_ENAB, 0);
407 static int pcmuio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
409 struct pcmuio_private *devpriv = dev->private;
410 int asic = pcmuio_subdevice_to_asic(s);
411 struct pcmuio_asic *chip = &devpriv->asics[asic];
414 spin_lock_irqsave(&chip->spinlock, flags);
416 pcmuio_stop_intr(dev, s);
417 spin_unlock_irqrestore(&chip->spinlock, flags);
422 static int pcmuio_inttrig_start_intr(struct comedi_device *dev,
423 struct comedi_subdevice *s,
424 unsigned int trig_num)
426 struct pcmuio_private *devpriv = dev->private;
427 struct comedi_cmd *cmd = &s->async->cmd;
428 int asic = pcmuio_subdevice_to_asic(s);
429 struct pcmuio_asic *chip = &devpriv->asics[asic];
432 if (trig_num != cmd->start_arg)
435 spin_lock_irqsave(&chip->spinlock, flags);
436 s->async->inttrig = NULL;
438 pcmuio_start_intr(dev, s);
440 spin_unlock_irqrestore(&chip->spinlock, flags);
446 * 'do_cmd' function for an 'INTERRUPT' subdevice.
448 static int pcmuio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
450 struct pcmuio_private *devpriv = dev->private;
451 struct comedi_cmd *cmd = &s->async->cmd;
452 int asic = pcmuio_subdevice_to_asic(s);
453 struct pcmuio_asic *chip = &devpriv->asics[asic];
456 spin_lock_irqsave(&chip->spinlock, flags);
459 /* Set up start of acquisition. */
460 if (cmd->start_src == TRIG_INT)
461 s->async->inttrig = pcmuio_inttrig_start_intr;
463 pcmuio_start_intr(dev, s);
465 spin_unlock_irqrestore(&chip->spinlock, flags);
470 static int pcmuio_cmdtest(struct comedi_device *dev,
471 struct comedi_subdevice *s,
472 struct comedi_cmd *cmd)
476 /* Step 1 : check if triggers are trivially valid */
478 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
479 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
480 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
481 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
482 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
487 /* Step 2a : make sure trigger sources are unique */
489 err |= comedi_check_trigger_is_unique(cmd->start_src);
490 err |= comedi_check_trigger_is_unique(cmd->stop_src);
492 /* Step 2b : and mutually compatible */
497 /* Step 3: check if arguments are trivially valid */
499 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
500 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
501 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
502 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
505 if (cmd->stop_src == TRIG_COUNT)
506 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
508 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
513 /* step 4: fix up any arguments */
515 /* if (err) return 4; */
520 static int pcmuio_attach(struct comedi_device *dev, struct comedi_devconfig *it)
522 const struct pcmuio_board *board = dev->board_ptr;
523 struct comedi_subdevice *s;
524 struct pcmuio_private *devpriv;
528 ret = comedi_request_region(dev, it->options[0],
529 board->num_asics * PCMUIO_ASIC_IOSIZE);
533 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
537 for (i = 0; i < PCMUIO_MAX_ASICS; ++i) {
538 struct pcmuio_asic *chip = &devpriv->asics[i];
540 spin_lock_init(&chip->pagelock);
541 spin_lock_init(&chip->spinlock);
546 if (it->options[1]) {
547 /* request the irq for the 1st asic */
548 ret = request_irq(it->options[1], pcmuio_interrupt, 0,
549 dev->board_name, dev);
551 dev->irq = it->options[1];
554 if (board->num_asics == 2) {
555 if (it->options[2] == dev->irq) {
556 /* the same irq (or none) is used by both asics */
557 devpriv->irq2 = it->options[2];
558 } else if (it->options[2]) {
559 /* request the irq for the 2nd asic */
560 ret = request_irq(it->options[2], pcmuio_interrupt, 0,
561 dev->board_name, dev);
563 devpriv->irq2 = it->options[2];
567 ret = comedi_alloc_subdevices(dev, board->num_asics * 2);
571 for (i = 0; i < dev->n_subdevices; ++i) {
572 s = &dev->subdevices[i];
573 s->type = COMEDI_SUBD_DIO;
574 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
577 s->range_table = &range_digital;
578 s->insn_bits = pcmuio_dio_insn_bits;
579 s->insn_config = pcmuio_dio_insn_config;
581 /* subdevices 0 and 2 can support interrupts */
582 if ((i == 0 && dev->irq) || (i == 2 && devpriv->irq2)) {
583 /* setup the interrupt subdevice */
584 dev->read_subdev = s;
585 s->subdev_flags |= SDF_CMD_READ | SDF_LSAMPL |
587 s->len_chanlist = s->n_chan;
588 s->cancel = pcmuio_cancel;
589 s->do_cmd = pcmuio_cmd;
590 s->do_cmdtest = pcmuio_cmdtest;
597 static void pcmuio_detach(struct comedi_device *dev)
599 struct pcmuio_private *devpriv = dev->private;
604 /* free the 2nd irq if used, the core will free the 1st one */
605 if (devpriv->irq2 && devpriv->irq2 != dev->irq)
606 free_irq(devpriv->irq2, dev);
608 comedi_legacy_detach(dev);
611 static struct comedi_driver pcmuio_driver = {
612 .driver_name = "pcmuio",
613 .module = THIS_MODULE,
614 .attach = pcmuio_attach,
615 .detach = pcmuio_detach,
616 .board_name = &pcmuio_boards[0].name,
617 .offset = sizeof(struct pcmuio_board),
618 .num_names = ARRAY_SIZE(pcmuio_boards),
620 module_comedi_driver(pcmuio_driver);
622 MODULE_AUTHOR("Comedi https://www.comedi.org");
623 MODULE_DESCRIPTION("Comedi low-level driver");
624 MODULE_LICENSE("GPL");