3 * Comedi driver for Winsystems PC-104 based 48/96-channel DIO boards.
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2006 Calin A. Culianu <calin@ajvar.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: Winsystems PC-104 based 48/96-channel DIO boards.
22 * Devices: [Winsystems] PCM-UIO48A (pcmuio48), PCM-UIO96A (pcmuio96)
23 * Author: Calin Culianu <calin@ajvar.org>
24 * Updated: Fri, 13 Jan 2006 12:01:01 -0500
27 * A driver for the relatively straightforward-to-program PCM-UIO48A and
28 * PCM-UIO96A boards from Winsystems. These boards use either one or two
29 * (in the 96-DIO version) WS16C48 ASIC HighDensity I/O Chips (HDIO). This
30 * chip is interesting in that each I/O line is individually programmable
31 * for INPUT or OUTPUT (thus comedi_dio_config can be done on a per-channel
32 * basis). Also, each chip supports edge-triggered interrupts for the first
33 * 24 I/O lines. Of course, since the 96-channel version of the board has
34 * two ASICs, it can detect polarity changes on up to 48 I/O lines. Since
35 * this is essentially an (non-PnP) ISA board, I/O Address and IRQ selection
36 * are done through jumpers on the board. You need to pass that information
37 * to this driver as the first and second comedi_config option, respectively.
38 * Note that the 48-channel version uses 16 bytes of IO memory and the 96-
39 * channel version uses 32-bytes (in case you are worried about conflicts).
40 * The 48-channel board is split into two 24-channel comedi subdevices. The
41 * 96-channel board is split into 4 24-channel DIO subdevices.
43 * Note that IRQ support has been added, but it is untested.
45 * To use edge-detection IRQ support, pass the IRQs of both ASICS (for the
46 * 96 channel version) or just 1 ASIC (for 48-channel version). Then, use
47 * comedi_commands with TRIG_NOW. Your callback will be called each time an
48 * edge is triggered, and the data values will be two sample_t's, which
49 * should be concatenated to form one 32-bit unsigned int. This value is
50 * the mask of channels that had edges detected from your channel list. Note
51 * that the bits positions in the mask correspond to positions in your
52 * chanlist when you specified the command and *not* channel id's!
54 * To set the polarity of the edge-detection interrupts pass a nonzero value
55 * for either CR_RANGE or CR_AREF for edge-up polarity, or a zero value for
56 * both CR_RANGE and CR_AREF if you want edge-down polarity.
58 * In the 48-channel version:
60 * On subdev 0, the first 24 channels channels are edge-detect channels.
62 * In the 96-channel board you have the following channels that can do edge
65 * subdev 0, channels 0-24 (first 24 channels of 1st ASIC)
66 * subdev 2, channels 0-24 (first 24 channels of 2nd ASIC)
68 * Configuration Options:
69 * [0] - I/O port base address
70 * [1] - IRQ (for first ASIC, or first 24 channels)
71 * [2] - IRQ (for second ASIC, pcmuio96 only - IRQ for chans 48-72
72 * can be the same as first irq!)
75 #include <linux/module.h>
76 #include <linux/interrupt.h>
78 #include "../comedidev.h"
83 * Offset Page 0 Page 1 Page 2 Page 3
84 * ------ ----------- ----------- ----------- -----------
85 * 0x00 Port 0 I/O Port 0 I/O Port 0 I/O Port 0 I/O
86 * 0x01 Port 1 I/O Port 1 I/O Port 1 I/O Port 1 I/O
87 * 0x02 Port 2 I/O Port 2 I/O Port 2 I/O Port 2 I/O
88 * 0x03 Port 3 I/O Port 3 I/O Port 3 I/O Port 3 I/O
89 * 0x04 Port 4 I/O Port 4 I/O Port 4 I/O Port 4 I/O
90 * 0x05 Port 5 I/O Port 5 I/O Port 5 I/O Port 5 I/O
91 * 0x06 INT_PENDING INT_PENDING INT_PENDING INT_PENDING
92 * 0x07 Page/Lock Page/Lock Page/Lock Page/Lock
93 * 0x08 N/A POL_0 ENAB_0 INT_ID0
94 * 0x09 N/A POL_1 ENAB_1 INT_ID1
95 * 0x0a N/A POL_2 ENAB_2 INT_ID2
97 #define PCMUIO_PORT_REG(x) (0x00 + (x))
98 #define PCMUIO_INT_PENDING_REG 0x06
99 #define PCMUIO_PAGE_LOCK_REG 0x07
100 #define PCMUIO_LOCK_PORT(x) ((1 << (x)) & 0x3f)
101 #define PCMUIO_PAGE(x) (((x) & 0x3) << 6)
102 #define PCMUIO_PAGE_MASK PCMUIO_PAGE(3)
103 #define PCMUIO_PAGE_POL 1
104 #define PCMUIO_PAGE_ENAB 2
105 #define PCMUIO_PAGE_INT_ID 3
106 #define PCMUIO_PAGE_REG(x) (0x08 + (x))
108 #define PCMUIO_ASIC_IOSIZE 0x10
109 #define PCMUIO_MAX_ASICS 2
111 struct pcmuio_board {
116 static const struct pcmuio_board pcmuio_boards[] = {
127 spinlock_t pagelock; /* protects the page registers */
128 spinlock_t spinlock; /* protects member variables */
129 unsigned int enabled_mask;
130 unsigned int active:1;
133 struct pcmuio_private {
134 struct pcmuio_asic asics[PCMUIO_MAX_ASICS];
138 static inline unsigned long pcmuio_asic_iobase(struct comedi_device *dev,
141 return dev->iobase + (asic * PCMUIO_ASIC_IOSIZE);
144 static inline int pcmuio_subdevice_to_asic(struct comedi_subdevice *s)
147 * subdevice 0 and 1 are handled by the first asic
148 * subdevice 2 and 3 are handled by the second asic
153 static inline int pcmuio_subdevice_to_port(struct comedi_subdevice *s)
156 * subdevice 0 and 2 use port registers 0-2
157 * subdevice 1 and 3 use port registers 3-5
159 return (s->index % 2) ? 3 : 0;
162 static void pcmuio_write(struct comedi_device *dev, unsigned int val,
163 int asic, int page, int port)
165 struct pcmuio_private *devpriv = dev->private;
166 struct pcmuio_asic *chip = &devpriv->asics[asic];
167 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
170 spin_lock_irqsave(&chip->pagelock, flags);
172 /* Port registers are valid for any page */
173 outb(val & 0xff, iobase + PCMUIO_PORT_REG(port + 0));
174 outb((val >> 8) & 0xff, iobase + PCMUIO_PORT_REG(port + 1));
175 outb((val >> 16) & 0xff, iobase + PCMUIO_PORT_REG(port + 2));
177 outb(PCMUIO_PAGE(page), iobase + PCMUIO_PAGE_LOCK_REG);
178 outb(val & 0xff, iobase + PCMUIO_PAGE_REG(0));
179 outb((val >> 8) & 0xff, iobase + PCMUIO_PAGE_REG(1));
180 outb((val >> 16) & 0xff, iobase + PCMUIO_PAGE_REG(2));
182 spin_unlock_irqrestore(&chip->pagelock, flags);
185 static unsigned int pcmuio_read(struct comedi_device *dev,
186 int asic, int page, int port)
188 struct pcmuio_private *devpriv = dev->private;
189 struct pcmuio_asic *chip = &devpriv->asics[asic];
190 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
194 spin_lock_irqsave(&chip->pagelock, flags);
196 /* Port registers are valid for any page */
197 val = inb(iobase + PCMUIO_PORT_REG(port + 0));
198 val |= (inb(iobase + PCMUIO_PORT_REG(port + 1)) << 8);
199 val |= (inb(iobase + PCMUIO_PORT_REG(port + 2)) << 16);
201 outb(PCMUIO_PAGE(page), iobase + PCMUIO_PAGE_LOCK_REG);
202 val = inb(iobase + PCMUIO_PAGE_REG(0));
203 val |= (inb(iobase + PCMUIO_PAGE_REG(1)) << 8);
204 val |= (inb(iobase + PCMUIO_PAGE_REG(2)) << 16);
206 spin_unlock_irqrestore(&chip->pagelock, flags);
212 * Each channel can be individually programmed for input or output.
213 * Writing a '0' to a channel causes the corresponding output pin
214 * to go to a high-z state (pulled high by an external 10K resistor).
215 * This allows it to be used as an input. When used in the input mode,
216 * a read reflects the inverted state of the I/O pin, such that a
217 * high on the pin will read as a '0' in the register. Writing a '1'
218 * to a bit position causes the pin to sink current (up to 12mA),
219 * effectively pulling it low.
221 static int pcmuio_dio_insn_bits(struct comedi_device *dev,
222 struct comedi_subdevice *s,
223 struct comedi_insn *insn,
226 int asic = pcmuio_subdevice_to_asic(s);
227 int port = pcmuio_subdevice_to_port(s);
228 unsigned int chanmask = (1 << s->n_chan) - 1;
232 mask = comedi_dio_update_state(s, data);
235 * Outputs are inverted, invert the state and
236 * update the channels.
238 * The s->io_bits mask makes sure the input channels
239 * are '0' so that the outputs pins stay in a high
242 val = ~s->state & chanmask;
244 pcmuio_write(dev, val, asic, 0, port);
247 /* get inverted state of the channels from the port */
248 val = pcmuio_read(dev, asic, 0, port);
250 /* return the true state of the channels */
251 data[1] = ~val & chanmask;
256 static int pcmuio_dio_insn_config(struct comedi_device *dev,
257 struct comedi_subdevice *s,
258 struct comedi_insn *insn,
261 int asic = pcmuio_subdevice_to_asic(s);
262 int port = pcmuio_subdevice_to_port(s);
265 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
269 if (data[0] == INSN_CONFIG_DIO_INPUT)
270 pcmuio_write(dev, s->io_bits, asic, 0, port);
275 static void pcmuio_reset(struct comedi_device *dev)
277 const struct pcmuio_board *board = dev->board_ptr;
280 for (asic = 0; asic < board->num_asics; ++asic) {
281 /* first, clear all the DIO port bits */
282 pcmuio_write(dev, 0, asic, 0, 0);
283 pcmuio_write(dev, 0, asic, 0, 3);
285 /* Next, clear all the paged registers for each page */
286 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_POL, 0);
287 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_ENAB, 0);
288 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_INT_ID, 0);
292 /* chip->spinlock is already locked */
293 static void pcmuio_stop_intr(struct comedi_device *dev,
294 struct comedi_subdevice *s)
296 struct pcmuio_private *devpriv = dev->private;
297 int asic = pcmuio_subdevice_to_asic(s);
298 struct pcmuio_asic *chip = &devpriv->asics[asic];
300 chip->enabled_mask = 0;
302 s->async->inttrig = NULL;
304 /* disable all intrs for this subdev.. */
305 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_ENAB, 0);
308 static void pcmuio_handle_intr_subdev(struct comedi_device *dev,
309 struct comedi_subdevice *s,
312 struct pcmuio_private *devpriv = dev->private;
313 int asic = pcmuio_subdevice_to_asic(s);
314 struct pcmuio_asic *chip = &devpriv->asics[asic];
315 struct comedi_cmd *cmd = &s->async->cmd;
316 unsigned int val = 0;
320 spin_lock_irqsave(&chip->spinlock, flags);
325 if (!(triggered & chip->enabled_mask))
328 for (i = 0; i < cmd->chanlist_len; i++) {
329 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
331 if (triggered & (1 << chan))
335 comedi_buf_write_samples(s, &val, 1);
337 if (cmd->stop_src == TRIG_COUNT &&
338 s->async->scans_done >= cmd->stop_arg)
339 s->async->events |= COMEDI_CB_EOA;
342 spin_unlock_irqrestore(&chip->spinlock, flags);
344 comedi_handle_events(dev, s);
347 static int pcmuio_handle_asic_interrupt(struct comedi_device *dev, int asic)
349 /* there are could be two asics so we can't use dev->read_subdev */
350 struct comedi_subdevice *s = &dev->subdevices[asic * 2];
351 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
354 /* are there any interrupts pending */
355 val = inb(iobase + PCMUIO_INT_PENDING_REG) & 0x07;
359 /* get, and clear, the pending interrupts */
360 val = pcmuio_read(dev, asic, PCMUIO_PAGE_INT_ID, 0);
361 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_INT_ID, 0);
363 /* handle the pending interrupts */
364 pcmuio_handle_intr_subdev(dev, s, val);
369 static irqreturn_t pcmuio_interrupt(int irq, void *d)
371 struct comedi_device *dev = d;
372 struct pcmuio_private *devpriv = dev->private;
376 handled += pcmuio_handle_asic_interrupt(dev, 0);
377 if (irq == devpriv->irq2)
378 handled += pcmuio_handle_asic_interrupt(dev, 1);
380 return handled ? IRQ_HANDLED : IRQ_NONE;
383 /* chip->spinlock is already locked */
384 static void pcmuio_start_intr(struct comedi_device *dev,
385 struct comedi_subdevice *s)
387 struct pcmuio_private *devpriv = dev->private;
388 int asic = pcmuio_subdevice_to_asic(s);
389 struct pcmuio_asic *chip = &devpriv->asics[asic];
390 struct comedi_cmd *cmd = &s->async->cmd;
391 unsigned int bits = 0;
392 unsigned int pol_bits = 0;
395 chip->enabled_mask = 0;
398 for (i = 0; i < cmd->chanlist_len; i++) {
399 unsigned int chanspec = cmd->chanlist[i];
400 unsigned int chan = CR_CHAN(chanspec);
401 unsigned int range = CR_RANGE(chanspec);
402 unsigned int aref = CR_AREF(chanspec);
405 pol_bits |= ((aref || range) ? 1 : 0) << chan;
408 bits &= ((1 << s->n_chan) - 1);
409 chip->enabled_mask = bits;
411 /* set pol and enab intrs for this subdev.. */
412 pcmuio_write(dev, pol_bits, asic, PCMUIO_PAGE_POL, 0);
413 pcmuio_write(dev, bits, asic, PCMUIO_PAGE_ENAB, 0);
416 static int pcmuio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
418 struct pcmuio_private *devpriv = dev->private;
419 int asic = pcmuio_subdevice_to_asic(s);
420 struct pcmuio_asic *chip = &devpriv->asics[asic];
423 spin_lock_irqsave(&chip->spinlock, flags);
425 pcmuio_stop_intr(dev, s);
426 spin_unlock_irqrestore(&chip->spinlock, flags);
431 static int pcmuio_inttrig_start_intr(struct comedi_device *dev,
432 struct comedi_subdevice *s,
433 unsigned int trig_num)
435 struct pcmuio_private *devpriv = dev->private;
436 struct comedi_cmd *cmd = &s->async->cmd;
437 int asic = pcmuio_subdevice_to_asic(s);
438 struct pcmuio_asic *chip = &devpriv->asics[asic];
441 if (trig_num != cmd->start_arg)
444 spin_lock_irqsave(&chip->spinlock, flags);
445 s->async->inttrig = NULL;
447 pcmuio_start_intr(dev, s);
449 spin_unlock_irqrestore(&chip->spinlock, flags);
455 * 'do_cmd' function for an 'INTERRUPT' subdevice.
457 static int pcmuio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
459 struct pcmuio_private *devpriv = dev->private;
460 struct comedi_cmd *cmd = &s->async->cmd;
461 int asic = pcmuio_subdevice_to_asic(s);
462 struct pcmuio_asic *chip = &devpriv->asics[asic];
465 spin_lock_irqsave(&chip->spinlock, flags);
468 /* Set up start of acquisition. */
469 if (cmd->start_src == TRIG_INT)
470 s->async->inttrig = pcmuio_inttrig_start_intr;
472 pcmuio_start_intr(dev, s);
474 spin_unlock_irqrestore(&chip->spinlock, flags);
479 static int pcmuio_cmdtest(struct comedi_device *dev,
480 struct comedi_subdevice *s,
481 struct comedi_cmd *cmd)
485 /* Step 1 : check if triggers are trivially valid */
487 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
488 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
489 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
490 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
491 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
496 /* Step 2a : make sure trigger sources are unique */
498 err |= comedi_check_trigger_is_unique(cmd->start_src);
499 err |= comedi_check_trigger_is_unique(cmd->stop_src);
501 /* Step 2b : and mutually compatible */
506 /* Step 3: check if arguments are trivially valid */
508 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
509 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
510 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
511 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
514 if (cmd->stop_src == TRIG_COUNT)
515 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
517 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
522 /* step 4: fix up any arguments */
524 /* if (err) return 4; */
529 static int pcmuio_attach(struct comedi_device *dev, struct comedi_devconfig *it)
531 const struct pcmuio_board *board = dev->board_ptr;
532 struct comedi_subdevice *s;
533 struct pcmuio_private *devpriv;
537 ret = comedi_request_region(dev, it->options[0],
538 board->num_asics * PCMUIO_ASIC_IOSIZE);
542 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
546 for (i = 0; i < PCMUIO_MAX_ASICS; ++i) {
547 struct pcmuio_asic *chip = &devpriv->asics[i];
549 spin_lock_init(&chip->pagelock);
550 spin_lock_init(&chip->spinlock);
555 if (it->options[1]) {
556 /* request the irq for the 1st asic */
557 ret = request_irq(it->options[1], pcmuio_interrupt, 0,
558 dev->board_name, dev);
560 dev->irq = it->options[1];
563 if (board->num_asics == 2) {
564 if (it->options[2] == dev->irq) {
565 /* the same irq (or none) is used by both asics */
566 devpriv->irq2 = it->options[2];
567 } else if (it->options[2]) {
568 /* request the irq for the 2nd asic */
569 ret = request_irq(it->options[2], pcmuio_interrupt, 0,
570 dev->board_name, dev);
572 devpriv->irq2 = it->options[2];
576 ret = comedi_alloc_subdevices(dev, board->num_asics * 2);
580 for (i = 0; i < dev->n_subdevices; ++i) {
581 s = &dev->subdevices[i];
582 s->type = COMEDI_SUBD_DIO;
583 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
586 s->range_table = &range_digital;
587 s->insn_bits = pcmuio_dio_insn_bits;
588 s->insn_config = pcmuio_dio_insn_config;
590 /* subdevices 0 and 2 can support interrupts */
591 if ((i == 0 && dev->irq) || (i == 2 && devpriv->irq2)) {
592 /* setup the interrupt subdevice */
593 dev->read_subdev = s;
594 s->subdev_flags |= SDF_CMD_READ | SDF_LSAMPL |
596 s->len_chanlist = s->n_chan;
597 s->cancel = pcmuio_cancel;
598 s->do_cmd = pcmuio_cmd;
599 s->do_cmdtest = pcmuio_cmdtest;
606 static void pcmuio_detach(struct comedi_device *dev)
608 struct pcmuio_private *devpriv = dev->private;
613 /* free the 2nd irq if used, the core will free the 1st one */
614 if (devpriv->irq2 && devpriv->irq2 != dev->irq)
615 free_irq(devpriv->irq2, dev);
617 comedi_legacy_detach(dev);
620 static struct comedi_driver pcmuio_driver = {
621 .driver_name = "pcmuio",
622 .module = THIS_MODULE,
623 .attach = pcmuio_attach,
624 .detach = pcmuio_detach,
625 .board_name = &pcmuio_boards[0].name,
626 .offset = sizeof(struct pcmuio_board),
627 .num_names = ARRAY_SIZE(pcmuio_boards),
629 module_comedi_driver(pcmuio_driver);
631 MODULE_AUTHOR("Comedi http://www.comedi.org");
632 MODULE_DESCRIPTION("Comedi low-level driver");
633 MODULE_LICENSE("GPL");