1 // SPDX-License-Identifier: GPL-2.0+
3 * COMEDI driver for the ADLINK PCI-723x/743x series boards.
4 * Copyright (C) 2012 H Hartley Sweeten <hsweeten@visionengravers.com>
6 * Based on the adl_pci7230 driver written by:
7 * David Fernandez <dfcastelao@gmail.com>
8 * and the adl_pci7432 driver written by:
9 * Michel Lachaine <mike@mikelachaine.ca>
11 * COMEDI - Linux Control and Measurement Device Interface
12 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
17 * Description: 32/64-Channel Isolated Digital I/O Boards
18 * Devices: [ADLink] PCI-7230 (adl_pci7230), PCI-7233 (adl_pci7233),
19 * PCI-7234 (adl_pci7234), PCI-7432 (adl_pci7432), PCI-7433 (adl_pci7433),
20 * PCI-7434 (adl_pci7434)
21 * Author: H Hartley Sweeten <hsweeten@visionengravers.com>
22 * Updated: Fri, 20 Nov 2020 14:49:36 +0000
23 * Status: works (tested on PCI-7230)
25 * One or two subdevices are setup by this driver depending on
26 * the number of digital inputs and/or outputs provided by the
27 * board. Each subdevice has a maximum of 32 channels.
29 * PCI-7230 - 4 subdevices: 0 - 16 input, 1 - 16 output,
30 * 2 - IRQ_IDI0, 3 - IRQ_IDI1
31 * PCI-7233 - 1 subdevice: 0 - 32 input
32 * PCI-7234 - 1 subdevice: 0 - 32 output
33 * PCI-7432 - 2 subdevices: 0 - 32 input, 1 - 32 output
34 * PCI-7433 - 2 subdevices: 0 - 32 input, 1 - 32 input
35 * PCI-7434 - 2 subdevices: 0 - 32 output, 1 - 32 output
37 * The PCI-7230, PCI-7432 and PCI-7433 boards also support external
38 * interrupt signals on digital input channels 0 and 1. The PCI-7233
39 * has dual-interrupt sources for change-of-state (COS) on any 16
40 * digital input channels of LSB and for COS on any 16 digital input
43 * Currently, this driver only supports interrupts for PCI-7230.
45 * Configuration Options: not applicable, uses comedi PCI auto config
48 #include <linux/module.h>
49 #include <linux/comedi/comedi_pci.h>
54 * Register I/O map (32-bit access only)
56 #define PCI7X3X_DIO_REG 0x0000 /* in the DigIO Port area */
57 #define PCI743X_DIO_REG 0x0004
59 #define ADL_PT_CLRIRQ 0x0040 /* in the DigIO Port area */
61 #define LINTI1_EN_ACT_IDI0 (PLX9052_INTCSR_LI1ENAB | PLX9052_INTCSR_LI1STAT)
62 #define LINTI2_EN_ACT_IDI1 (PLX9052_INTCSR_LI2ENAB | PLX9052_INTCSR_LI2STAT)
63 #define EN_PCI_LINT2H_LINT1H \
64 (PLX9052_INTCSR_PCIENAB | PLX9052_INTCSR_LI2POL | PLX9052_INTCSR_LI1POL)
66 enum adl_pci7x3x_boardid {
75 struct adl_pci7x3x_boardinfo {
83 static const struct adl_pci7x3x_boardinfo adl_pci7x3x_boards[] = {
85 .name = "adl_pci7230",
86 .nsubdevs = 4, /* IDI, IDO, IRQ_IDI0, IRQ_IDI1 */
92 .name = "adl_pci7233",
97 .name = "adl_pci7234",
102 .name = "adl_pci7432",
108 .name = "adl_pci7433",
113 .name = "adl_pci7434",
119 struct adl_pci7x3x_dev_private_data {
120 unsigned long lcr_io_base;
121 unsigned int int_ctrl;
124 struct adl_pci7x3x_sd_private_data {
125 spinlock_t subd_slock; /* spin-lock for cmd_running */
126 unsigned long port_offset;
127 short int cmd_running;
130 static void process_irq(struct comedi_device *dev, unsigned int subdev,
131 unsigned short intcsr)
133 struct comedi_subdevice *s = &dev->subdevices[subdev];
134 struct adl_pci7x3x_sd_private_data *sd_priv = s->private;
135 unsigned long reg = sd_priv->port_offset;
136 struct comedi_async *async_p = s->async;
139 unsigned short val = inw(dev->iobase + reg);
141 spin_lock(&sd_priv->subd_slock);
142 if (sd_priv->cmd_running)
143 comedi_buf_write_samples(s, &val, 1);
144 spin_unlock(&sd_priv->subd_slock);
145 comedi_handle_events(dev, s);
149 static irqreturn_t adl_pci7x3x_interrupt(int irq, void *p_device)
151 struct comedi_device *dev = p_device;
152 struct adl_pci7x3x_dev_private_data *dev_private = dev->private;
153 unsigned long cpu_flags;
155 bool li1stat, li2stat;
157 if (!dev->attached) {
158 /* Ignore interrupt before device fully attached. */
159 /* Might not even have allocated subdevices yet! */
163 /* Check if we are source of interrupt */
164 spin_lock_irqsave(&dev->spinlock, cpu_flags);
165 intcsr = inl(dev_private->lcr_io_base + PLX9052_INTCSR);
166 li1stat = (intcsr & LINTI1_EN_ACT_IDI0) == LINTI1_EN_ACT_IDI0;
167 li2stat = (intcsr & LINTI2_EN_ACT_IDI1) == LINTI2_EN_ACT_IDI1;
168 if (li1stat || li2stat) {
169 /* clear all current interrupt flags */
170 /* Fixme: Reset all 2 Int Flags */
171 outb(0x00, dev->iobase + ADL_PT_CLRIRQ);
173 spin_unlock_irqrestore(&dev->spinlock, cpu_flags);
175 /* SubDev 2, 3 = Isolated DigIn , on "SCSI2" jack!*/
177 if (li1stat) /* 0x0005 LINTi1 is Enabled && IDI0 is 1 */
178 process_irq(dev, 2, intcsr);
180 if (li2stat) /* 0x0028 LINTi2 is Enabled && IDI1 is 1 */
181 process_irq(dev, 3, intcsr);
183 return IRQ_RETVAL(li1stat || li2stat);
186 static int adl_pci7x3x_asy_cmdtest(struct comedi_device *dev,
187 struct comedi_subdevice *s,
188 struct comedi_cmd *cmd)
192 /* Step 1 : check if triggers are trivially valid */
194 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
195 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
196 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
197 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
198 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
203 /* Step 2a : make sure trigger sources are unique */
204 /* Step 2b : and mutually compatible */
206 /* Step 3: check if arguments are trivially valid */
208 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
209 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
210 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
211 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
213 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
218 /* Step 4: fix up any arguments */
220 /* Step 5: check channel list if it exists */
225 static int adl_pci7x3x_asy_cmd(struct comedi_device *dev,
226 struct comedi_subdevice *s)
228 struct adl_pci7x3x_dev_private_data *dev_private = dev->private;
229 struct adl_pci7x3x_sd_private_data *sd_priv = s->private;
230 unsigned long cpu_flags;
231 unsigned int int_enab;
234 /* enable LINTi1 == IDI sdi[0] Ch 0 IRQ ActHigh */
235 int_enab = PLX9052_INTCSR_LI1ENAB;
237 /* enable LINTi2 == IDI sdi[0] Ch 1 IRQ ActHigh */
238 int_enab = PLX9052_INTCSR_LI2ENAB;
241 spin_lock_irqsave(&dev->spinlock, cpu_flags);
242 dev_private->int_ctrl |= int_enab;
243 outl(dev_private->int_ctrl, dev_private->lcr_io_base + PLX9052_INTCSR);
244 spin_unlock_irqrestore(&dev->spinlock, cpu_flags);
246 spin_lock_irqsave(&sd_priv->subd_slock, cpu_flags);
247 sd_priv->cmd_running = 1;
248 spin_unlock_irqrestore(&sd_priv->subd_slock, cpu_flags);
253 static int adl_pci7x3x_asy_cancel(struct comedi_device *dev,
254 struct comedi_subdevice *s)
256 struct adl_pci7x3x_dev_private_data *dev_private = dev->private;
257 struct adl_pci7x3x_sd_private_data *sd_priv = s->private;
258 unsigned long cpu_flags;
259 unsigned int int_enab;
261 spin_lock_irqsave(&sd_priv->subd_slock, cpu_flags);
262 sd_priv->cmd_running = 0;
263 spin_unlock_irqrestore(&sd_priv->subd_slock, cpu_flags);
264 /* disable Interrupts */
266 int_enab = PLX9052_INTCSR_LI1ENAB;
268 int_enab = PLX9052_INTCSR_LI2ENAB;
269 spin_lock_irqsave(&dev->spinlock, cpu_flags);
270 dev_private->int_ctrl &= ~int_enab;
271 outl(dev_private->int_ctrl, dev_private->lcr_io_base + PLX9052_INTCSR);
272 spin_unlock_irqrestore(&dev->spinlock, cpu_flags);
277 /* same as _di_insn_bits because the IRQ-pins are the DI-ports */
278 static int adl_pci7x3x_dirq_insn_bits(struct comedi_device *dev,
279 struct comedi_subdevice *s,
280 struct comedi_insn *insn,
283 struct adl_pci7x3x_sd_private_data *sd_priv = s->private;
284 unsigned long reg = (unsigned long)sd_priv->port_offset;
286 data[1] = inl(dev->iobase + reg);
291 static int adl_pci7x3x_do_insn_bits(struct comedi_device *dev,
292 struct comedi_subdevice *s,
293 struct comedi_insn *insn,
296 unsigned long reg = (unsigned long)s->private;
298 if (comedi_dio_update_state(s, data)) {
299 unsigned int val = s->state;
301 if (s->n_chan == 16) {
303 * It seems the PCI-7230 needs the 16-bit DO state
304 * to be shifted left by 16 bits before being written
305 * to the 32-bit register. Set the value in both
306 * halves of the register to be sure.
310 outl(val, dev->iobase + reg);
318 static int adl_pci7x3x_di_insn_bits(struct comedi_device *dev,
319 struct comedi_subdevice *s,
320 struct comedi_insn *insn,
323 unsigned long reg = (unsigned long)s->private;
325 data[1] = inl(dev->iobase + reg);
330 static int adl_pci7x3x_reset(struct comedi_device *dev)
332 struct adl_pci7x3x_dev_private_data *dev_private = dev->private;
334 /* disable Interrupts */
335 dev_private->int_ctrl = 0x00; /* Disable PCI + LINTi2 + LINTi1 */
336 outl(dev_private->int_ctrl, dev_private->lcr_io_base + PLX9052_INTCSR);
341 static int adl_pci7x3x_auto_attach(struct comedi_device *dev,
342 unsigned long context)
344 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
345 const struct adl_pci7x3x_boardinfo *board = NULL;
346 struct comedi_subdevice *s;
347 struct adl_pci7x3x_dev_private_data *dev_private;
353 if (context < ARRAY_SIZE(adl_pci7x3x_boards))
354 board = &adl_pci7x3x_boards[context];
357 dev->board_ptr = board;
358 dev->board_name = board->name;
360 dev_private = comedi_alloc_devpriv(dev, sizeof(*dev_private));
364 ret = comedi_pci_enable(dev);
367 dev->iobase = pci_resource_start(pcidev, 2);
368 dev_private->lcr_io_base = pci_resource_start(pcidev, 1);
370 adl_pci7x3x_reset(dev);
372 if (board->irq_nchan) {
373 /* discard all evtl. old IRQs */
374 outb(0x00, dev->iobase + ADL_PT_CLRIRQ);
377 ret = request_irq(pcidev->irq, adl_pci7x3x_interrupt,
378 IRQF_SHARED, dev->board_name, dev);
380 dev->irq = pcidev->irq;
381 /* 0x52 PCI + IDI Ch 1 Ch 0 IRQ Off ActHigh */
382 dev_private->int_ctrl = EN_PCI_LINT2H_LINT1H;
383 outl(dev_private->int_ctrl,
384 dev_private->lcr_io_base + PLX9052_INTCSR);
389 ret = comedi_alloc_subdevices(dev, board->nsubdevs);
395 if (board->di_nchan) {
396 nchan = min(board->di_nchan, 32);
398 s = &dev->subdevices[subdev];
399 /* Isolated digital inputs 0 to 15/31 */
400 s->type = COMEDI_SUBD_DI;
401 s->subdev_flags = SDF_READABLE;
404 s->insn_bits = adl_pci7x3x_di_insn_bits;
405 s->range_table = &range_digital;
407 s->private = (void *)PCI7X3X_DIO_REG;
411 nchan = board->di_nchan - nchan;
413 s = &dev->subdevices[subdev];
414 /* Isolated digital inputs 32 to 63 */
415 s->type = COMEDI_SUBD_DI;
416 s->subdev_flags = SDF_READABLE;
419 s->insn_bits = adl_pci7x3x_di_insn_bits;
420 s->range_table = &range_digital;
422 s->private = (void *)PCI743X_DIO_REG;
428 if (board->do_nchan) {
429 nchan = min(board->do_nchan, 32);
431 s = &dev->subdevices[subdev];
432 /* Isolated digital outputs 0 to 15/31 */
433 s->type = COMEDI_SUBD_DO;
434 s->subdev_flags = SDF_WRITABLE;
437 s->insn_bits = adl_pci7x3x_do_insn_bits;
438 s->range_table = &range_digital;
440 s->private = (void *)PCI7X3X_DIO_REG;
444 nchan = board->do_nchan - nchan;
446 s = &dev->subdevices[subdev];
447 /* Isolated digital outputs 32 to 63 */
448 s->type = COMEDI_SUBD_DO;
449 s->subdev_flags = SDF_WRITABLE;
452 s->insn_bits = adl_pci7x3x_do_insn_bits;
453 s->range_table = &range_digital;
455 s->private = (void *)PCI743X_DIO_REG;
461 for (ic = 0; ic < board->irq_nchan; ++ic) {
462 struct adl_pci7x3x_sd_private_data *sd_priv;
466 s = &dev->subdevices[subdev];
467 /* Isolated digital inputs 0 or 1 */
468 s->type = COMEDI_SUBD_DI;
469 s->subdev_flags = SDF_READABLE;
472 s->insn_bits = adl_pci7x3x_dirq_insn_bits;
473 s->range_table = &range_digital;
475 sd_priv = comedi_alloc_spriv(s, sizeof(*sd_priv));
479 spin_lock_init(&sd_priv->subd_slock);
480 sd_priv->port_offset = PCI7X3X_DIO_REG;
481 sd_priv->cmd_running = 0;
484 dev->read_subdev = s;
485 s->type = COMEDI_SUBD_DI;
486 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
488 s->do_cmdtest = adl_pci7x3x_asy_cmdtest;
489 s->do_cmd = adl_pci7x3x_asy_cmd;
490 s->cancel = adl_pci7x3x_asy_cancel;
499 static void adl_pci7x3x_detach(struct comedi_device *dev)
502 adl_pci7x3x_reset(dev);
503 comedi_pci_detach(dev);
506 static struct comedi_driver adl_pci7x3x_driver = {
507 .driver_name = "adl_pci7x3x",
508 .module = THIS_MODULE,
509 .auto_attach = adl_pci7x3x_auto_attach,
510 .detach = adl_pci7x3x_detach,
513 static int adl_pci7x3x_pci_probe(struct pci_dev *dev,
514 const struct pci_device_id *id)
516 return comedi_pci_auto_config(dev, &adl_pci7x3x_driver,
520 static const struct pci_device_id adl_pci7x3x_pci_table[] = {
521 { PCI_VDEVICE(ADLINK, 0x7230), BOARD_PCI7230 },
522 { PCI_VDEVICE(ADLINK, 0x7233), BOARD_PCI7233 },
523 { PCI_VDEVICE(ADLINK, 0x7234), BOARD_PCI7234 },
524 { PCI_VDEVICE(ADLINK, 0x7432), BOARD_PCI7432 },
525 { PCI_VDEVICE(ADLINK, 0x7433), BOARD_PCI7433 },
526 { PCI_VDEVICE(ADLINK, 0x7434), BOARD_PCI7434 },
529 MODULE_DEVICE_TABLE(pci, adl_pci7x3x_pci_table);
531 static struct pci_driver adl_pci7x3x_pci_driver = {
532 .name = "adl_pci7x3x",
533 .id_table = adl_pci7x3x_pci_table,
534 .probe = adl_pci7x3x_pci_probe,
535 .remove = comedi_pci_auto_unconfig,
537 module_comedi_pci_driver(adl_pci7x3x_driver, adl_pci7x3x_pci_driver);
539 MODULE_DESCRIPTION("ADLINK PCI-723x/743x Isolated Digital I/O boards");
540 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
541 MODULE_LICENSE("GPL");