2 comedi/drivers/amplc_dio200.c
3 Driver for Amplicon PC272E and PCI272 DIO boards.
4 (Support for other boards in Amplicon 200 series may be added at
5 a later date, e.g. PCI215.)
7 Copyright (C) 2005 MEV Ltd. <http://www.mev.co.uk/>
9 COMEDI - Linux Control and Measurement Device Interface
10 Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Driver: amplc_dio200
29 * Description: Amplicon 200 Series Digital I/O
30 * Author: Ian Abbott <abbotti@mev.co.uk>
31 * Devices: [Amplicon] PC212E (pc212e), PC214E (pc214e), PC215E (pc215e),
32 * PCI215 (pci215), PCIe215 (pcie215), PC218E (pc218e), PCIe236 (pcie236),
33 * PC272E (pc272e), PCI272 (pci272), PCIe296 (pcie296)
34 * Updated: Wed, 24 Oct 2012 16:22:34 +0100
37 * Configuration options - PC212E, PC214E, PC215E, PC218E, PC272E:
38 * [0] - I/O port base address
39 * [1] - IRQ (optional, but commands won't work without it)
41 * Manual configuration of PCI(e) cards is not supported; they are configured
44 * Passing a zero for an option is the same as leaving it unspecified.
48 * PC212E PC214E PC215E/PCI215
49 * ------------- ------------- -------------
52 * 1 CTR-Y1 PPI-Y PPI-Y
53 * 2 CTR-Y2 CTR-Z1* CTR-Z1
54 * 3 CTR-Z1 INTERRUPT* CTR-Z2
58 * PCIe215 PC218E PCIe236
59 * ------------- ------------- -------------
61 * 0 PPI-X CTR-X1 PPI-X
62 * 1 UNUSED CTR-X2 UNUSED
63 * 2 PPI-Y CTR-Y1 UNUSED
64 * 3 UNUSED CTR-Y2 UNUSED
65 * 4 CTR-Z1 CTR-Z1 CTR-Z1
66 * 5 CTR-Z2 CTR-Z2 CTR-Z2
67 * 6 TIMER INTERRUPT TIMER
68 * 7 INTERRUPT INTERRUPT
70 * PC272E/PCI272 PCIe296
71 * ------------- -------------
82 * Each PPI is a 8255 chip providing 24 DIO channels. The DIO channels
83 * are configurable as inputs or outputs in four groups:
85 * Port A - channels 0 to 7
86 * Port B - channels 8 to 15
87 * Port CL - channels 16 to 19
88 * Port CH - channels 20 to 23
90 * Only mode 0 of the 8255 chips is supported.
92 * Each CTR is a 8254 chip providing 3 16-bit counter channels. Each
93 * channel is configured individually with INSN_CONFIG instructions. The
94 * specific type of configuration instruction is specified in data[0].
95 * Some configuration instructions expect an additional parameter in
96 * data[1]; others return a value in data[1]. The following configuration
97 * instructions are supported:
99 * INSN_CONFIG_SET_COUNTER_MODE. Sets the counter channel's mode and
100 * BCD/binary setting specified in data[1].
102 * INSN_CONFIG_8254_READ_STATUS. Reads the status register value for the
103 * counter channel into data[1].
105 * INSN_CONFIG_SET_CLOCK_SRC. Sets the counter channel's clock source as
106 * specified in data[1] (this is a hardware-specific value). Not
107 * supported on PC214E. For the other boards, valid clock sources are
110 * 0. CLK n, the counter channel's dedicated CLK input from the SK1
111 * connector. (N.B. for other values, the counter channel's CLKn
112 * pin on the SK1 connector is an output!)
113 * 1. Internal 10 MHz clock.
114 * 2. Internal 1 MHz clock.
115 * 3. Internal 100 kHz clock.
116 * 4. Internal 10 kHz clock.
117 * 5. Internal 1 kHz clock.
118 * 6. OUT n-1, the output of counter channel n-1 (see note 1 below).
119 * 7. Ext Clock, the counter chip's dedicated Ext Clock input from
120 * the SK1 connector. This pin is shared by all three counter
121 * channels on the chip.
123 * For the PCIe boards, clock sources in the range 0 to 31 are allowed
124 * and the following additional clock sources are defined:
126 * 8. HIGH logic level.
127 * 9. LOW logic level.
128 * 10. "Pattern present" signal.
129 * 11. Internal 20 MHz clock.
131 * INSN_CONFIG_GET_CLOCK_SRC. Returns the counter channel's current
132 * clock source in data[1]. For internal clock sources, data[2] is set
133 * to the period in ns.
135 * INSN_CONFIG_SET_GATE_SRC. Sets the counter channel's gate source as
136 * specified in data[2] (this is a hardware-specific value). Not
137 * supported on PC214E. For the other boards, valid gate sources are 0
140 * 0. VCC (internal +5V d.c.), i.e. gate permanently enabled.
141 * 1. GND (internal 0V d.c.), i.e. gate permanently disabled.
142 * 2. GAT n, the counter channel's dedicated GAT input from the SK1
143 * connector. (N.B. for other values, the counter channel's GATn
144 * pin on the SK1 connector is an output!)
145 * 3. /OUT n-2, the inverted output of counter channel n-2 (see note
152 * For the PCIe boards, gate sources in the range 0 to 31 are allowed;
153 * the following additional clock sources and clock sources 6 and 7 are
156 * 6. /GAT n, negated version of the counter channel's dedicated
157 * GAT input (negated version of gate source 2).
158 * 7. OUT n-2, the non-inverted output of counter channel n-2
159 * (negated version of gate source 3).
160 * 8. "Pattern present" signal, HIGH while pattern present.
161 * 9. "Pattern occurred" latched signal, latches HIGH when pattern
163 * 10. "Pattern gone away" latched signal, latches LOW when pattern
164 * goes away after it occurred.
165 * 11. Negated "pattern present" signal, LOW while pattern present
166 * (negated version of gate source 8).
167 * 12. Negated "pattern occurred" latched signal, latches LOW when
168 * pattern occurs (negated version of gate source 9).
169 * 13. Negated "pattern gone away" latched signal, latches LOW when
170 * pattern goes away after it occurred (negated version of gate
173 * INSN_CONFIG_GET_GATE_SRC. Returns the counter channel's current gate
176 * Clock and gate interconnection notes:
178 * 1. Clock source OUT n-1 is the output of the preceding channel on the
179 * same counter subdevice if n > 0, or the output of channel 2 on the
180 * preceding counter subdevice (see note 3) if n = 0.
182 * 2. Gate source /OUT n-2 is the inverted output of channel 0 on the
183 * same counter subdevice if n = 2, or the inverted output of channel n+1
184 * on the preceding counter subdevice (see note 3) if n < 2.
186 * 3. The counter subdevices are connected in a ring, so the highest
187 * counter subdevice precedes the lowest.
189 * The 'TIMER' subdevice is a free-running 32-bit timer subdevice.
191 * The 'INTERRUPT' subdevice pretends to be a digital input subdevice. The
192 * digital inputs come from the interrupt status register. The number of
193 * channels matches the number of interrupt sources. The PC214E does not
194 * have an interrupt status register; see notes on 'INTERRUPT SOURCES'
199 * PC212E PC214E PC215E/PCI215
200 * ------------- ------------- -------------
202 * 0 PPI-X-C0 JUMPER-J5 PPI-X-C0
203 * 1 PPI-X-C3 PPI-X-C3
204 * 2 CTR-Y1-OUT1 PPI-Y-C0
205 * 3 CTR-Y2-OUT1 PPI-Y-C3
206 * 4 CTR-Z1-OUT1 CTR-Z1-OUT1
207 * 5 CTR-Z2-OUT1 CTR-Z2-OUT1
209 * PCIe215 PC218E PCIe236
210 * ------------- ------------- -------------
212 * 0 PPI-X-C0 CTR-X1-OUT1 PPI-X-C0
213 * 1 PPI-X-C3 CTR-X2-OUT1 PPI-X-C3
214 * 2 PPI-Y-C0 CTR-Y1-OUT1 unused
215 * 3 PPI-Y-C3 CTR-Y2-OUT1 unused
216 * 4 CTR-Z1-OUT1 CTR-Z1-OUT1 CTR-Z1-OUT1
217 * 5 CTR-Z2-OUT1 CTR-Z2-OUT1 CTR-Z2-OUT1
219 * PC272E/PCI272 PCIe296
220 * ------------- -------------
222 * 0 PPI-X-C0 PPI-X1-C0
223 * 1 PPI-X-C3 PPI-X1-C3
224 * 2 PPI-Y-C0 PPI-Y1-C0
225 * 3 PPI-Y-C3 PPI-Y1-C3
226 * 4 PPI-Z-C0 CTR-Z1-OUT1
227 * 5 PPI-Z-C3 CTR-Z2-OUT1
229 * When an interrupt source is enabled in the interrupt source enable
230 * register, a rising edge on the source signal latches the corresponding
231 * bit to 1 in the interrupt status register.
233 * When the interrupt status register value as a whole (actually, just the
234 * 6 least significant bits) goes from zero to non-zero, the board will
235 * generate an interrupt. For level-triggered hardware interrupts (PCI
236 * card), the interrupt will remain asserted until the interrupt status
237 * register is cleared to zero. For edge-triggered hardware interrupts
238 * (ISA card), no further interrupts will occur until the interrupt status
239 * register is cleared to zero. To clear a bit to zero in the interrupt
240 * status register, the corresponding interrupt source must be disabled
241 * in the interrupt source enable register (there is no separate interrupt
244 * The PC214E does not have an interrupt source enable register or an
245 * interrupt status register; its 'INTERRUPT' subdevice has a single
246 * channel and its interrupt source is selected by the position of jumper
251 * The driver supports a read streaming acquisition command on the
252 * 'INTERRUPT' subdevice. The channel list selects the interrupt sources
253 * to be enabled. All channels will be sampled together (convert_src ==
254 * TRIG_NOW). The scan begins a short time after the hardware interrupt
255 * occurs, subject to interrupt latencies (scan_begin_src == TRIG_EXT,
256 * scan_begin_arg == 0). The value read from the interrupt status register
257 * is packed into a short value, one bit per requested channel, in the
258 * order they appear in the channel list.
261 #include <linux/pci.h>
262 #include <linux/interrupt.h>
263 #include <linux/slab.h>
265 #include "../comedidev.h"
267 #include "comedi_fc.h"
270 #define DIO200_DRIVER_NAME "amplc_dio200"
272 #define DO_ISA IS_ENABLED(CONFIG_COMEDI_AMPLC_DIO200_ISA)
273 #define DO_PCI IS_ENABLED(CONFIG_COMEDI_AMPLC_DIO200_PCI)
276 #define PCI_DEVICE_ID_AMPLICON_PCI272 0x000a
277 #define PCI_DEVICE_ID_AMPLICON_PCI215 0x000b
278 #define PCI_DEVICE_ID_AMPLICON_PCIE236 0x0011
279 #define PCI_DEVICE_ID_AMPLICON_PCIE215 0x0012
280 #define PCI_DEVICE_ID_AMPLICON_PCIE296 0x0014
282 /* 8255 control register bits */
283 #define CR_C_LO_IO 0x01
285 #define CR_B_MODE 0x04
286 #define CR_C_HI_IO 0x08
288 #define CR_A_MODE(a) ((a)<<5)
291 /* 200 series registers */
292 #define DIO200_IO_SIZE 0x20
293 #define DIO200_PCIE_IO_SIZE 0x4000
294 #define DIO200_XCLK_SCE 0x18 /* Group X clock selection register */
295 #define DIO200_YCLK_SCE 0x19 /* Group Y clock selection register */
296 #define DIO200_ZCLK_SCE 0x1a /* Group Z clock selection register */
297 #define DIO200_XGAT_SCE 0x1b /* Group X gate selection register */
298 #define DIO200_YGAT_SCE 0x1c /* Group Y gate selection register */
299 #define DIO200_ZGAT_SCE 0x1d /* Group Z gate selection register */
300 #define DIO200_INT_SCE 0x1e /* Interrupt enable/status register */
301 /* Extra registers for new PCIe boards */
302 #define DIO200_ENHANCE 0x20 /* 1 to enable enhanced features */
303 #define DIO200_VERSION 0x24 /* Hardware version register */
304 #define DIO200_TS_CONFIG 0x600 /* Timestamp timer config register */
305 #define DIO200_TS_COUNT 0x602 /* Timestamp timer count register */
308 * Functions for constructing value for DIO_200_?CLK_SCE and
309 * DIO_200_?GAT_SCE registers:
311 * 'which' is: 0 for CTR-X1, CTR-Y1, CTR-Z1; 1 for CTR-X2, CTR-Y2 or CTR-Z2.
312 * 'chan' is the channel: 0, 1 or 2.
313 * 'source' is the signal source: 0 to 7, or 0 to 31 for "enhanced" boards.
315 static unsigned char clk_gat_sce(unsigned int which, unsigned int chan,
318 return (which << 5) | (chan << 3) |
319 ((source & 030) << 3) | (source & 007);
322 static unsigned char clk_sce(unsigned int which, unsigned int chan,
325 return clk_gat_sce(which, chan, source);
328 static unsigned char gat_sce(unsigned int which, unsigned int chan,
331 return clk_gat_sce(which, chan, source);
335 * Periods of the internal clock sources in nanoseconds.
337 static const unsigned int clock_period[32] = {
338 [1] = 100, /* 10 MHz */
339 [2] = 1000, /* 1 MHz */
340 [3] = 10000, /* 100 kHz */
341 [4] = 100000, /* 10 kHz */
342 [5] = 1000000, /* 1 kHz */
343 [11] = 50, /* 20 MHz (enhanced boards) */
344 /* clock sources 12 and later reserved for enhanced boards */
348 * Timestamp timer configuration register (for new PCIe boards).
350 #define TS_CONFIG_RESET 0x100 /* Reset counter to zero. */
351 #define TS_CONFIG_CLK_SRC_MASK 0x0FF /* Clock source. */
352 #define TS_CONFIG_MAX_CLK_SRC 2 /* Maximum clock source value. */
355 * Periods of the timestamp timer clock sources in nanoseconds.
357 static const unsigned int ts_clock_period[TS_CONFIG_MAX_CLK_SRC + 1] = {
358 1, /* 1 nanosecond (but with 20 ns granularity). */
359 1000, /* 1 microsecond. */
360 1000000, /* 1 millisecond. */
366 enum dio200_regtype { no_regtype = 0, io_regtype, mmio_regtype };
367 struct dio200_region {
369 unsigned long iobase; /* I/O base address */
370 unsigned char __iomem *membase; /* mapped MMIO base address */
372 enum dio200_regtype regtype;
378 enum dio200_sdtype { sd_none, sd_intr, sd_8255, sd_8254, sd_timer };
380 #define DIO200_MAX_SUBDEVS 8
381 #define DIO200_MAX_ISNS 6
383 struct dio200_layout {
384 unsigned short n_subdevs; /* number of subdevices */
385 unsigned char sdtype[DIO200_MAX_SUBDEVS]; /* enum dio200_sdtype */
386 unsigned char sdinfo[DIO200_MAX_SUBDEVS]; /* depends on sdtype */
387 bool has_int_sce:1; /* has interrupt enable/status reg */
388 bool has_clk_gat_sce:1; /* has clock/gate selection registers */
389 bool has_enhancements:1; /* has enhanced features */
393 * Board descriptions.
396 enum dio200_bustype { isa_bustype, pci_bustype };
398 enum dio200_pci_model {
406 struct dio200_board {
408 struct dio200_layout layout;
409 enum dio200_bustype bustype;
410 unsigned char mainbar;
411 unsigned char mainshift;
412 unsigned int mainsize;
416 static const struct dio200_board dio200_isa_boards[] = {
419 .bustype = isa_bustype,
420 .mainsize = DIO200_IO_SIZE,
423 .sdtype = {sd_8255, sd_8254, sd_8254, sd_8254, sd_8254,
425 .sdinfo = {0x00, 0x08, 0x0C, 0x10, 0x14, 0x3F},
427 .has_clk_gat_sce = true,
432 .bustype = isa_bustype,
433 .mainsize = DIO200_IO_SIZE,
436 .sdtype = {sd_8255, sd_8255, sd_8254, sd_intr},
437 .sdinfo = {0x00, 0x08, 0x10, 0x01},
442 .bustype = isa_bustype,
443 .mainsize = DIO200_IO_SIZE,
446 .sdtype = {sd_8255, sd_8255, sd_8254, sd_8254, sd_intr},
447 .sdinfo = {0x00, 0x08, 0x10, 0x14, 0x3F},
449 .has_clk_gat_sce = true,
454 .bustype = isa_bustype,
455 .mainsize = DIO200_IO_SIZE,
458 .sdtype = {sd_8254, sd_8254, sd_8255, sd_8254, sd_8254,
460 .sdinfo = {0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x3F},
462 .has_clk_gat_sce = true,
467 .bustype = isa_bustype,
468 .mainsize = DIO200_IO_SIZE,
471 .sdtype = {sd_8255, sd_8255, sd_8255, sd_intr},
472 .sdinfo = {0x00, 0x08, 0x10, 0x3F},
480 static const struct dio200_board dio200_pci_boards[] = {
483 .bustype = pci_bustype,
485 .mainsize = DIO200_IO_SIZE,
488 .sdtype = {sd_8255, sd_8255, sd_8254, sd_8254, sd_intr},
489 .sdinfo = {0x00, 0x08, 0x10, 0x14, 0x3F},
491 .has_clk_gat_sce = true,
496 .bustype = pci_bustype,
498 .mainsize = DIO200_IO_SIZE,
501 .sdtype = {sd_8255, sd_8255, sd_8255, sd_intr},
502 .sdinfo = {0x00, 0x08, 0x10, 0x3F},
508 .bustype = pci_bustype,
511 .mainsize = DIO200_PCIE_IO_SIZE,
514 .sdtype = {sd_8255, sd_none, sd_8255, sd_none,
515 sd_8254, sd_8254, sd_timer, sd_intr},
516 .sdinfo = {0x00, 0x00, 0x08, 0x00,
517 0x10, 0x14, 0x00, 0x3F},
519 .has_clk_gat_sce = true,
520 .has_enhancements = true,
525 .bustype = pci_bustype,
528 .mainsize = DIO200_PCIE_IO_SIZE,
531 .sdtype = {sd_8255, sd_none, sd_none, sd_none,
532 sd_8254, sd_8254, sd_timer, sd_intr},
533 .sdinfo = {0x00, 0x00, 0x00, 0x00,
534 0x10, 0x14, 0x00, 0x3F},
536 .has_clk_gat_sce = true,
537 .has_enhancements = true,
542 .bustype = pci_bustype,
545 .mainsize = DIO200_PCIE_IO_SIZE,
548 .sdtype = {sd_8255, sd_8255, sd_8255, sd_8255,
549 sd_8254, sd_8254, sd_timer, sd_intr},
550 .sdinfo = {0x00, 0x04, 0x08, 0x0C,
551 0x10, 0x14, 0x00, 0x3F},
553 .has_clk_gat_sce = true,
554 .has_enhancements = true,
560 /* this structure is for data unique to this hardware driver. If
561 several hardware drivers keep similar information in this structure,
562 feel free to suggest moving the variable to the struct comedi_device struct.
564 struct dio200_private {
565 struct dio200_region io; /* Register region */
569 struct dio200_subdev_8254 {
570 unsigned int ofs; /* Counter base offset */
571 unsigned int clk_sce_ofs; /* CLK_SCE base address */
572 unsigned int gat_sce_ofs; /* GAT_SCE base address */
573 int which; /* Bit 5 of CLK_SCE or GAT_SCE */
574 unsigned int clock_src[3]; /* Current clock sources */
575 unsigned int gate_src[3]; /* Current gate sources */
579 struct dio200_subdev_8255 {
580 unsigned int ofs; /* DIO base offset */
583 struct dio200_subdev_intr {
587 unsigned int valid_isns;
588 unsigned int enabled_isns;
589 unsigned int stopcount;
593 static inline const struct dio200_layout *
594 dio200_board_layout(const struct dio200_board *board)
596 return &board->layout;
599 static inline const struct dio200_layout *
600 dio200_dev_layout(struct comedi_device *dev)
602 return dio200_board_layout(comedi_board(dev));
605 static inline bool is_pci_board(const struct dio200_board *board)
607 return DO_PCI && board->bustype == pci_bustype;
610 static inline bool is_isa_board(const struct dio200_board *board)
612 return DO_ISA && board->bustype == isa_bustype;
616 * Read 8-bit register.
618 static unsigned char dio200_read8(struct comedi_device *dev,
621 const struct dio200_board *thisboard = comedi_board(dev);
622 struct dio200_private *devpriv = dev->private;
624 offset <<= thisboard->mainshift;
625 if (devpriv->io.regtype == io_regtype)
626 return inb(devpriv->io.u.iobase + offset);
628 return readb(devpriv->io.u.membase + offset);
632 * Write 8-bit register.
634 static void dio200_write8(struct comedi_device *dev, unsigned int offset,
637 const struct dio200_board *thisboard = comedi_board(dev);
638 struct dio200_private *devpriv = dev->private;
640 offset <<= thisboard->mainshift;
641 if (devpriv->io.regtype == io_regtype)
642 outb(val, devpriv->io.u.iobase + offset);
644 writeb(val, devpriv->io.u.membase + offset);
648 * Read 32-bit register.
650 static unsigned int dio200_read32(struct comedi_device *dev,
653 const struct dio200_board *thisboard = comedi_board(dev);
654 struct dio200_private *devpriv = dev->private;
656 offset <<= thisboard->mainshift;
657 if (devpriv->io.regtype == io_regtype)
658 return inl(devpriv->io.u.iobase + offset);
660 return readl(devpriv->io.u.membase + offset);
664 * Write 32-bit register.
666 static void dio200_write32(struct comedi_device *dev, unsigned int offset,
669 const struct dio200_board *thisboard = comedi_board(dev);
670 struct dio200_private *devpriv = dev->private;
672 offset <<= thisboard->mainshift;
673 if (devpriv->io.regtype == io_regtype)
674 outl(val, devpriv->io.u.iobase + offset);
676 writel(val, devpriv->io.u.membase + offset);
680 * This function checks and requests an I/O region, reporting an error
681 * if there is a conflict.
684 dio200_request_region(struct comedi_device *dev,
685 unsigned long from, unsigned long extent)
687 if (!from || !request_region(from, extent, DIO200_DRIVER_NAME)) {
688 dev_err(dev->class_dev, "I/O port conflict (%#lx,%lu)!\n",
696 * 'insn_bits' function for an 'INTERRUPT' subdevice.
699 dio200_subdev_intr_insn_bits(struct comedi_device *dev,
700 struct comedi_subdevice *s,
701 struct comedi_insn *insn, unsigned int *data)
703 const struct dio200_layout *layout = dio200_dev_layout(dev);
704 struct dio200_subdev_intr *subpriv = s->private;
706 if (layout->has_int_sce) {
707 /* Just read the interrupt status register. */
708 data[1] = dio200_read8(dev, subpriv->ofs) & subpriv->valid_isns;
710 /* No interrupt status register. */
718 * Called to stop acquisition for an 'INTERRUPT' subdevice.
720 static void dio200_stop_intr(struct comedi_device *dev,
721 struct comedi_subdevice *s)
723 const struct dio200_layout *layout = dio200_dev_layout(dev);
724 struct dio200_subdev_intr *subpriv = s->private;
727 subpriv->enabled_isns = 0;
728 if (layout->has_int_sce)
729 dio200_write8(dev, subpriv->ofs, 0);
733 * Called to start acquisition for an 'INTERRUPT' subdevice.
735 static int dio200_start_intr(struct comedi_device *dev,
736 struct comedi_subdevice *s)
740 const struct dio200_layout *layout = dio200_dev_layout(dev);
741 struct dio200_subdev_intr *subpriv = s->private;
742 struct comedi_cmd *cmd = &s->async->cmd;
745 if (!subpriv->continuous && subpriv->stopcount == 0) {
746 /* An empty acquisition! */
747 s->async->events |= COMEDI_CB_EOA;
751 /* Determine interrupt sources to enable. */
754 for (n = 0; n < cmd->chanlist_len; n++)
755 isn_bits |= (1U << CR_CHAN(cmd->chanlist[n]));
757 isn_bits &= subpriv->valid_isns;
758 /* Enable interrupt sources. */
759 subpriv->enabled_isns = isn_bits;
760 if (layout->has_int_sce)
761 dio200_write8(dev, subpriv->ofs, isn_bits);
768 * Internal trigger function to start acquisition for an 'INTERRUPT' subdevice.
771 dio200_inttrig_start_intr(struct comedi_device *dev, struct comedi_subdevice *s,
772 unsigned int trignum)
774 struct dio200_subdev_intr *subpriv;
781 subpriv = s->private;
783 spin_lock_irqsave(&subpriv->spinlock, flags);
784 s->async->inttrig = NULL;
786 event = dio200_start_intr(dev, s);
788 spin_unlock_irqrestore(&subpriv->spinlock, flags);
791 comedi_event(dev, s);
797 * This is called from the interrupt service routine to handle a read
798 * scan on an 'INTERRUPT' subdevice.
800 static int dio200_handle_read_intr(struct comedi_device *dev,
801 struct comedi_subdevice *s)
803 const struct dio200_layout *layout = dio200_dev_layout(dev);
804 struct dio200_subdev_intr *subpriv = s->private;
807 unsigned cur_enabled;
808 unsigned int oldevents;
813 spin_lock_irqsave(&subpriv->spinlock, flags);
814 oldevents = s->async->events;
815 if (layout->has_int_sce) {
817 * Collect interrupt sources that have triggered and disable
818 * them temporarily. Loop around until no extra interrupt
819 * sources have triggered, at which point, the valid part of
820 * the interrupt status register will read zero, clearing the
821 * cause of the interrupt.
823 * Mask off interrupt sources already seen to avoid infinite
824 * loop in case of misconfiguration.
826 cur_enabled = subpriv->enabled_isns;
827 while ((intstat = (dio200_read8(dev, subpriv->ofs) &
828 subpriv->valid_isns & ~triggered)) != 0) {
829 triggered |= intstat;
830 cur_enabled &= ~triggered;
831 dio200_write8(dev, subpriv->ofs, cur_enabled);
835 * No interrupt status register. Assume the single interrupt
836 * source has triggered.
838 triggered = subpriv->enabled_isns;
843 * Some interrupt sources have triggered and have been
844 * temporarily disabled to clear the cause of the interrupt.
846 * Reenable them NOW to minimize the time they are disabled.
848 cur_enabled = subpriv->enabled_isns;
849 if (layout->has_int_sce)
850 dio200_write8(dev, subpriv->ofs, cur_enabled);
852 if (subpriv->active) {
854 * The command is still active.
856 * Ignore interrupt sources that the command isn't
857 * interested in (just in case there's a race
860 if (triggered & subpriv->enabled_isns) {
861 /* Collect scan data. */
863 unsigned int n, ch, len;
866 len = s->async->cmd.chanlist_len;
867 for (n = 0; n < len; n++) {
868 ch = CR_CHAN(s->async->cmd.chanlist[n]);
869 if (triggered & (1U << ch))
872 /* Write the scan to the buffer. */
873 if (comedi_buf_put(s->async, val)) {
874 s->async->events |= (COMEDI_CB_BLOCK |
877 /* Error! Stop acquisition. */
878 dio200_stop_intr(dev, s);
879 s->async->events |= COMEDI_CB_ERROR
880 | COMEDI_CB_OVERFLOW;
881 comedi_error(dev, "buffer overflow");
884 /* Check for end of acquisition. */
885 if (!subpriv->continuous) {
886 /* stop_src == TRIG_COUNT */
887 if (subpriv->stopcount > 0) {
888 subpriv->stopcount--;
889 if (subpriv->stopcount == 0) {
892 dio200_stop_intr(dev,
900 spin_unlock_irqrestore(&subpriv->spinlock, flags);
902 if (oldevents != s->async->events)
903 comedi_event(dev, s);
905 return (triggered != 0);
909 * 'cancel' function for an 'INTERRUPT' subdevice.
911 static int dio200_subdev_intr_cancel(struct comedi_device *dev,
912 struct comedi_subdevice *s)
914 struct dio200_subdev_intr *subpriv = s->private;
917 spin_lock_irqsave(&subpriv->spinlock, flags);
919 dio200_stop_intr(dev, s);
921 spin_unlock_irqrestore(&subpriv->spinlock, flags);
927 * 'do_cmdtest' function for an 'INTERRUPT' subdevice.
930 dio200_subdev_intr_cmdtest(struct comedi_device *dev,
931 struct comedi_subdevice *s, struct comedi_cmd *cmd)
935 /* Step 1 : check if triggers are trivially valid */
937 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
938 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
939 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
940 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
941 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
946 /* Step 2a : make sure trigger sources are unique */
948 err |= cfc_check_trigger_is_unique(cmd->start_src);
949 err |= cfc_check_trigger_is_unique(cmd->stop_src);
951 /* Step 2b : and mutually compatible */
956 /* Step 3: check if arguments are trivially valid */
958 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
959 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
960 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
961 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
963 switch (cmd->stop_src) {
965 /* any count allowed */
968 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
977 /* step 4: fix up any arguments */
979 /* if (err) return 4; */
985 * 'do_cmd' function for an 'INTERRUPT' subdevice.
987 static int dio200_subdev_intr_cmd(struct comedi_device *dev,
988 struct comedi_subdevice *s)
990 struct comedi_cmd *cmd = &s->async->cmd;
991 struct dio200_subdev_intr *subpriv = s->private;
995 spin_lock_irqsave(&subpriv->spinlock, flags);
998 /* Set up end of acquisition. */
999 switch (cmd->stop_src) {
1001 subpriv->continuous = 0;
1002 subpriv->stopcount = cmd->stop_arg;
1006 subpriv->continuous = 1;
1007 subpriv->stopcount = 0;
1011 /* Set up start of acquisition. */
1012 switch (cmd->start_src) {
1014 s->async->inttrig = dio200_inttrig_start_intr;
1018 event = dio200_start_intr(dev, s);
1021 spin_unlock_irqrestore(&subpriv->spinlock, flags);
1024 comedi_event(dev, s);
1030 * This function initializes an 'INTERRUPT' subdevice.
1033 dio200_subdev_intr_init(struct comedi_device *dev, struct comedi_subdevice *s,
1034 unsigned int offset, unsigned valid_isns)
1036 const struct dio200_layout *layout = dio200_dev_layout(dev);
1037 struct dio200_subdev_intr *subpriv;
1039 subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
1043 subpriv->ofs = offset;
1044 subpriv->valid_isns = valid_isns;
1045 spin_lock_init(&subpriv->spinlock);
1047 if (layout->has_int_sce)
1048 /* Disable interrupt sources. */
1049 dio200_write8(dev, subpriv->ofs, 0);
1051 s->private = subpriv;
1052 s->type = COMEDI_SUBD_DI;
1053 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
1054 if (layout->has_int_sce) {
1055 s->n_chan = DIO200_MAX_ISNS;
1056 s->len_chanlist = DIO200_MAX_ISNS;
1058 /* No interrupt source register. Support single channel. */
1060 s->len_chanlist = 1;
1062 s->range_table = &range_digital;
1064 s->insn_bits = dio200_subdev_intr_insn_bits;
1065 s->do_cmdtest = dio200_subdev_intr_cmdtest;
1066 s->do_cmd = dio200_subdev_intr_cmd;
1067 s->cancel = dio200_subdev_intr_cancel;
1073 * This function cleans up an 'INTERRUPT' subdevice.
1076 dio200_subdev_intr_cleanup(struct comedi_device *dev,
1077 struct comedi_subdevice *s)
1079 struct dio200_subdev_intr *subpriv = s->private;
1084 * Interrupt service routine.
1086 static irqreturn_t dio200_interrupt(int irq, void *d)
1088 struct comedi_device *dev = d;
1089 struct dio200_private *devpriv = dev->private;
1090 struct comedi_subdevice *s;
1096 if (devpriv->intr_sd >= 0) {
1097 s = &dev->subdevices[devpriv->intr_sd];
1098 handled = dio200_handle_read_intr(dev, s);
1103 return IRQ_RETVAL(handled);
1107 * Read an '8254' counter subdevice channel.
1110 dio200_subdev_8254_read_chan(struct comedi_device *dev,
1111 struct comedi_subdevice *s, unsigned int chan)
1113 struct dio200_subdev_8254 *subpriv = s->private;
1118 dio200_write8(dev, subpriv->ofs + i8254_control_reg, val);
1120 val = dio200_read8(dev, subpriv->ofs + chan);
1121 val += dio200_read8(dev, subpriv->ofs + chan) << 8;
1126 * Write an '8254' subdevice channel.
1129 dio200_subdev_8254_write_chan(struct comedi_device *dev,
1130 struct comedi_subdevice *s, unsigned int chan,
1133 struct dio200_subdev_8254 *subpriv = s->private;
1135 /* write lsb, msb */
1136 dio200_write8(dev, subpriv->ofs + chan, count & 0xff);
1137 dio200_write8(dev, subpriv->ofs + chan, (count >> 8) & 0xff);
1141 * Set mode of an '8254' subdevice channel.
1144 dio200_subdev_8254_set_mode(struct comedi_device *dev,
1145 struct comedi_subdevice *s, unsigned int chan,
1148 struct dio200_subdev_8254 *subpriv = s->private;
1152 byte |= 0x30; /* access order: lsb, msb */
1153 byte |= (mode & 0xf); /* counter mode and BCD|binary */
1154 dio200_write8(dev, subpriv->ofs + i8254_control_reg, byte);
1158 * Read status byte of an '8254' counter subdevice channel.
1161 dio200_subdev_8254_status(struct comedi_device *dev,
1162 struct comedi_subdevice *s, unsigned int chan)
1164 struct dio200_subdev_8254 *subpriv = s->private;
1167 dio200_write8(dev, subpriv->ofs + i8254_control_reg,
1168 0xe0 | (2 << chan));
1170 return dio200_read8(dev, subpriv->ofs + chan);
1174 * Handle 'insn_read' for an '8254' counter subdevice.
1177 dio200_subdev_8254_read(struct comedi_device *dev, struct comedi_subdevice *s,
1178 struct comedi_insn *insn, unsigned int *data)
1180 struct dio200_subdev_8254 *subpriv = s->private;
1181 int chan = CR_CHAN(insn->chanspec);
1183 unsigned long flags;
1185 for (n = 0; n < insn->n; n++) {
1186 spin_lock_irqsave(&subpriv->spinlock, flags);
1187 data[n] = dio200_subdev_8254_read_chan(dev, s, chan);
1188 spin_unlock_irqrestore(&subpriv->spinlock, flags);
1194 * Handle 'insn_write' for an '8254' counter subdevice.
1197 dio200_subdev_8254_write(struct comedi_device *dev, struct comedi_subdevice *s,
1198 struct comedi_insn *insn, unsigned int *data)
1200 struct dio200_subdev_8254 *subpriv = s->private;
1201 int chan = CR_CHAN(insn->chanspec);
1203 unsigned long flags;
1205 for (n = 0; n < insn->n; n++) {
1206 spin_lock_irqsave(&subpriv->spinlock, flags);
1207 dio200_subdev_8254_write_chan(dev, s, chan, data[n]);
1208 spin_unlock_irqrestore(&subpriv->spinlock, flags);
1214 * Set gate source for an '8254' counter subdevice channel.
1217 dio200_subdev_8254_set_gate_src(struct comedi_device *dev,
1218 struct comedi_subdevice *s,
1219 unsigned int counter_number,
1220 unsigned int gate_src)
1222 const struct dio200_layout *layout = dio200_dev_layout(dev);
1223 struct dio200_subdev_8254 *subpriv = s->private;
1226 if (!layout->has_clk_gat_sce)
1228 if (counter_number > 2)
1230 if (gate_src > (layout->has_enhancements ? 31 : 7))
1233 subpriv->gate_src[counter_number] = gate_src;
1234 byte = gat_sce(subpriv->which, counter_number, gate_src);
1235 dio200_write8(dev, subpriv->gat_sce_ofs, byte);
1241 * Get gate source for an '8254' counter subdevice channel.
1244 dio200_subdev_8254_get_gate_src(struct comedi_device *dev,
1245 struct comedi_subdevice *s,
1246 unsigned int counter_number)
1248 const struct dio200_layout *layout = dio200_dev_layout(dev);
1249 struct dio200_subdev_8254 *subpriv = s->private;
1251 if (!layout->has_clk_gat_sce)
1253 if (counter_number > 2)
1256 return subpriv->gate_src[counter_number];
1260 * Set clock source for an '8254' counter subdevice channel.
1263 dio200_subdev_8254_set_clock_src(struct comedi_device *dev,
1264 struct comedi_subdevice *s,
1265 unsigned int counter_number,
1266 unsigned int clock_src)
1268 const struct dio200_layout *layout = dio200_dev_layout(dev);
1269 struct dio200_subdev_8254 *subpriv = s->private;
1272 if (!layout->has_clk_gat_sce)
1274 if (counter_number > 2)
1276 if (clock_src > (layout->has_enhancements ? 31 : 7))
1279 subpriv->clock_src[counter_number] = clock_src;
1280 byte = clk_sce(subpriv->which, counter_number, clock_src);
1281 dio200_write8(dev, subpriv->clk_sce_ofs, byte);
1287 * Get clock source for an '8254' counter subdevice channel.
1290 dio200_subdev_8254_get_clock_src(struct comedi_device *dev,
1291 struct comedi_subdevice *s,
1292 unsigned int counter_number,
1293 unsigned int *period_ns)
1295 const struct dio200_layout *layout = dio200_dev_layout(dev);
1296 struct dio200_subdev_8254 *subpriv = s->private;
1299 if (!layout->has_clk_gat_sce)
1301 if (counter_number > 2)
1304 clock_src = subpriv->clock_src[counter_number];
1305 *period_ns = clock_period[clock_src];
1310 * Handle 'insn_config' for an '8254' counter subdevice.
1313 dio200_subdev_8254_config(struct comedi_device *dev, struct comedi_subdevice *s,
1314 struct comedi_insn *insn, unsigned int *data)
1316 struct dio200_subdev_8254 *subpriv = s->private;
1318 int chan = CR_CHAN(insn->chanspec);
1319 unsigned long flags;
1321 spin_lock_irqsave(&subpriv->spinlock, flags);
1323 case INSN_CONFIG_SET_COUNTER_MODE:
1324 if (data[1] > (I8254_MODE5 | I8254_BINARY))
1327 dio200_subdev_8254_set_mode(dev, s, chan, data[1]);
1329 case INSN_CONFIG_8254_READ_STATUS:
1330 data[1] = dio200_subdev_8254_status(dev, s, chan);
1332 case INSN_CONFIG_SET_GATE_SRC:
1333 ret = dio200_subdev_8254_set_gate_src(dev, s, chan, data[2]);
1337 case INSN_CONFIG_GET_GATE_SRC:
1338 ret = dio200_subdev_8254_get_gate_src(dev, s, chan);
1345 case INSN_CONFIG_SET_CLOCK_SRC:
1346 ret = dio200_subdev_8254_set_clock_src(dev, s, chan, data[1]);
1350 case INSN_CONFIG_GET_CLOCK_SRC:
1351 ret = dio200_subdev_8254_get_clock_src(dev, s, chan, &data[2]);
1362 spin_unlock_irqrestore(&subpriv->spinlock, flags);
1363 return ret < 0 ? ret : insn->n;
1367 * This function initializes an '8254' counter subdevice.
1370 dio200_subdev_8254_init(struct comedi_device *dev, struct comedi_subdevice *s,
1371 unsigned int offset)
1373 const struct dio200_layout *layout = dio200_dev_layout(dev);
1374 struct dio200_subdev_8254 *subpriv;
1377 subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
1381 s->private = subpriv;
1382 s->type = COMEDI_SUBD_COUNTER;
1383 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
1385 s->maxdata = 0xFFFF;
1386 s->insn_read = dio200_subdev_8254_read;
1387 s->insn_write = dio200_subdev_8254_write;
1388 s->insn_config = dio200_subdev_8254_config;
1390 spin_lock_init(&subpriv->spinlock);
1391 subpriv->ofs = offset;
1392 if (layout->has_clk_gat_sce) {
1393 /* Derive CLK_SCE and GAT_SCE register offsets from
1395 subpriv->clk_sce_ofs = DIO200_XCLK_SCE + (offset >> 3);
1396 subpriv->gat_sce_ofs = DIO200_XGAT_SCE + (offset >> 3);
1397 subpriv->which = (offset >> 2) & 1;
1400 /* Initialize channels. */
1401 for (chan = 0; chan < 3; chan++) {
1402 dio200_subdev_8254_set_mode(dev, s, chan,
1403 I8254_MODE0 | I8254_BINARY);
1404 if (layout->has_clk_gat_sce) {
1405 /* Gate source 0 is VCC (logic 1). */
1406 dio200_subdev_8254_set_gate_src(dev, s, chan, 0);
1407 /* Clock source 0 is the dedicated clock input. */
1408 dio200_subdev_8254_set_clock_src(dev, s, chan, 0);
1416 * This function cleans up an '8254' counter subdevice.
1419 dio200_subdev_8254_cleanup(struct comedi_device *dev,
1420 struct comedi_subdevice *s)
1422 struct dio200_subdev_intr *subpriv = s->private;
1427 * This function sets I/O directions for an '8255' DIO subdevice.
1429 static void dio200_subdev_8255_set_dir(struct comedi_device *dev,
1430 struct comedi_subdevice *s)
1432 struct dio200_subdev_8255 *subpriv = s->private;
1436 /* 1 in io_bits indicates output, 1 in config indicates input */
1437 if (!(s->io_bits & 0x0000ff))
1439 if (!(s->io_bits & 0x00ff00))
1441 if (!(s->io_bits & 0x0f0000))
1442 config |= CR_C_LO_IO;
1443 if (!(s->io_bits & 0xf00000))
1444 config |= CR_C_HI_IO;
1445 dio200_write8(dev, subpriv->ofs + 3, config);
1449 * Handle 'insn_bits' for an '8255' DIO subdevice.
1451 static int dio200_subdev_8255_bits(struct comedi_device *dev,
1452 struct comedi_subdevice *s,
1453 struct comedi_insn *insn, unsigned int *data)
1455 struct dio200_subdev_8255 *subpriv = s->private;
1458 s->state &= ~data[0];
1459 s->state |= (data[0] & data[1]);
1461 dio200_write8(dev, subpriv->ofs, s->state & 0xff);
1462 if (data[0] & 0xff00)
1463 dio200_write8(dev, subpriv->ofs + 1,
1464 (s->state >> 8) & 0xff);
1465 if (data[0] & 0xff0000)
1466 dio200_write8(dev, subpriv->ofs + 2,
1467 (s->state >> 16) & 0xff);
1469 data[1] = dio200_read8(dev, subpriv->ofs);
1470 data[1] |= dio200_read8(dev, subpriv->ofs + 1) << 8;
1471 data[1] |= dio200_read8(dev, subpriv->ofs + 2) << 16;
1476 * Handle 'insn_config' for an '8255' DIO subdevice.
1478 static int dio200_subdev_8255_config(struct comedi_device *dev,
1479 struct comedi_subdevice *s,
1480 struct comedi_insn *insn,
1486 mask = 1 << CR_CHAN(insn->chanspec);
1487 if (mask & 0x0000ff)
1489 else if (mask & 0x00ff00)
1491 else if (mask & 0x0f0000)
1496 case INSN_CONFIG_DIO_INPUT:
1497 s->io_bits &= ~bits;
1499 case INSN_CONFIG_DIO_OUTPUT:
1502 case INSN_CONFIG_DIO_QUERY:
1503 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
1509 dio200_subdev_8255_set_dir(dev, s);
1514 * This function initializes an '8255' DIO subdevice.
1516 * offset is the offset to the 8255 chip.
1518 static int dio200_subdev_8255_init(struct comedi_device *dev,
1519 struct comedi_subdevice *s,
1520 unsigned int offset)
1522 struct dio200_subdev_8255 *subpriv;
1524 subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
1527 subpriv->ofs = offset;
1528 s->private = subpriv;
1529 s->type = COMEDI_SUBD_DIO;
1530 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1532 s->range_table = &range_digital;
1534 s->insn_bits = dio200_subdev_8255_bits;
1535 s->insn_config = dio200_subdev_8255_config;
1538 dio200_subdev_8255_set_dir(dev, s);
1543 * This function cleans up an '8255' DIO subdevice.
1545 static void dio200_subdev_8255_cleanup(struct comedi_device *dev,
1546 struct comedi_subdevice *s)
1548 struct dio200_subdev_8255 *subpriv = s->private;
1554 * Handle 'insn_read' for a timer subdevice.
1556 static int dio200_subdev_timer_read(struct comedi_device *dev,
1557 struct comedi_subdevice *s,
1558 struct comedi_insn *insn,
1563 for (n = 0; n < insn->n; n++)
1564 data[n] = dio200_read32(dev, DIO200_TS_COUNT);
1569 * Reset timer subdevice.
1571 static void dio200_subdev_timer_reset(struct comedi_device *dev,
1572 struct comedi_subdevice *s)
1576 clock = dio200_read32(dev, DIO200_TS_CONFIG) & TS_CONFIG_CLK_SRC_MASK;
1577 dio200_write32(dev, DIO200_TS_CONFIG, clock | TS_CONFIG_RESET);
1578 dio200_write32(dev, DIO200_TS_CONFIG, clock);
1582 * Get timer subdevice clock source and period.
1584 static void dio200_subdev_timer_get_clock_src(struct comedi_device *dev,
1585 struct comedi_subdevice *s,
1587 unsigned int *period)
1591 clk = dio200_read32(dev, DIO200_TS_CONFIG) & TS_CONFIG_CLK_SRC_MASK;
1593 *period = (clk < ARRAY_SIZE(ts_clock_period)) ?
1594 ts_clock_period[clk] : 0;
1598 * Set timer subdevice clock source.
1600 static int dio200_subdev_timer_set_clock_src(struct comedi_device *dev,
1601 struct comedi_subdevice *s,
1604 if (src > TS_CONFIG_MAX_CLK_SRC)
1606 dio200_write32(dev, DIO200_TS_CONFIG, src);
1611 * Handle 'insn_config' for a timer subdevice.
1613 static int dio200_subdev_timer_config(struct comedi_device *dev,
1614 struct comedi_subdevice *s,
1615 struct comedi_insn *insn,
1621 case INSN_CONFIG_RESET:
1622 dio200_subdev_timer_reset(dev, s);
1624 case INSN_CONFIG_SET_CLOCK_SRC:
1625 ret = dio200_subdev_timer_set_clock_src(dev, s, data[1]);
1629 case INSN_CONFIG_GET_CLOCK_SRC:
1630 dio200_subdev_timer_get_clock_src(dev, s, &data[1], &data[2]);
1636 return ret < 0 ? ret : insn->n;
1640 * This function initializes a timer subdevice.
1642 * Uses the timestamp timer registers. There is only one timestamp timer.
1644 static int dio200_subdev_timer_init(struct comedi_device *dev,
1645 struct comedi_subdevice *s)
1647 s->type = COMEDI_SUBD_TIMER;
1648 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
1650 s->maxdata = 0xFFFFFFFF;
1651 s->insn_read = dio200_subdev_timer_read;
1652 s->insn_config = dio200_subdev_timer_config;
1657 * This function cleans up a timer subdevice.
1659 static void dio200_subdev_timer_cleanup(struct comedi_device *dev,
1660 struct comedi_subdevice *s)
1662 /* Nothing to do. */
1666 * This function does some special set-up for the PCIe boards
1667 * PCIe215, PCIe236, PCIe296.
1669 static int dio200_pcie_board_setup(struct comedi_device *dev)
1671 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1672 void __iomem *brbase;
1673 resource_size_t brlen;
1676 * The board uses Altera Cyclone IV with PCI-Express hard IP.
1677 * The FPGA configuration has the PCI-Express Avalon-MM Bridge
1678 * Control registers in PCI BAR 0, offset 0, and the length of
1679 * these registers is 0x4000.
1681 * We need to write 0x80 to the "Avalon-MM to PCI-Express Interrupt
1682 * Enable" register at offset 0x50 to allow generation of PCIe
1683 * interrupts when RXmlrq_i is asserted in the SOPC Builder system.
1685 brlen = pci_resource_len(pcidev, 0);
1686 if (brlen < 0x4000 ||
1687 !(pci_resource_flags(pcidev, 0) & IORESOURCE_MEM)) {
1688 dev_err(dev->class_dev, "error! bad PCI region!\n");
1691 brbase = ioremap_nocache(pci_resource_start(pcidev, 0), brlen);
1693 dev_err(dev->class_dev, "error! failed to map registers!\n");
1696 writel(0x80, brbase + 0x50);
1698 /* Enable "enhanced" features of board. */
1699 dio200_write8(dev, DIO200_ENHANCE, 1);
1703 static void dio200_report_attach(struct comedi_device *dev, unsigned int irq)
1705 const struct dio200_board *thisboard = comedi_board(dev);
1706 struct dio200_private *devpriv = dev->private;
1707 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1711 if (is_isa_board(thisboard))
1712 tmplen = scnprintf(tmpbuf, sizeof(tmpbuf),
1713 "(base %#lx) ", devpriv->io.u.iobase);
1714 else if (is_pci_board(thisboard))
1715 tmplen = scnprintf(tmpbuf, sizeof(tmpbuf),
1716 "(pci %s) ", pci_name(pcidev));
1720 tmplen += scnprintf(&tmpbuf[tmplen], sizeof(tmpbuf) - tmplen,
1722 (dev->irq ? "" : " UNAVAILABLE"));
1724 tmplen += scnprintf(&tmpbuf[tmplen], sizeof(tmpbuf) - tmplen,
1726 dev_info(dev->class_dev, "%s %sattached\n", dev->board_name, tmpbuf);
1729 static int dio200_common_attach(struct comedi_device *dev, unsigned int irq,
1730 unsigned long req_irq_flags)
1732 const struct dio200_board *thisboard = comedi_board(dev);
1733 struct dio200_private *devpriv = dev->private;
1734 const struct dio200_layout *layout = dio200_board_layout(thisboard);
1735 struct comedi_subdevice *s;
1740 devpriv->intr_sd = -1;
1741 dev->board_name = thisboard->name;
1743 ret = comedi_alloc_subdevices(dev, layout->n_subdevs);
1747 for (n = 0; n < dev->n_subdevices; n++) {
1748 s = &dev->subdevices[n];
1749 switch (layout->sdtype[n]) {
1751 /* counter subdevice (8254) */
1752 ret = dio200_subdev_8254_init(dev, s,
1758 /* digital i/o subdevice (8255) */
1759 ret = dio200_subdev_8255_init(dev, s,
1765 /* 'INTERRUPT' subdevice */
1767 ret = dio200_subdev_intr_init(dev, s,
1773 devpriv->intr_sd = n;
1775 s->type = COMEDI_SUBD_UNUSED;
1779 /* Only on PCIe boards. */
1781 ret = dio200_subdev_timer_init(dev, s);
1785 s->type = COMEDI_SUBD_UNUSED;
1789 s->type = COMEDI_SUBD_UNUSED;
1793 sdx = devpriv->intr_sd;
1794 if (sdx >= 0 && sdx < dev->n_subdevices)
1795 dev->read_subdev = &dev->subdevices[sdx];
1797 if (request_irq(irq, dio200_interrupt, req_irq_flags,
1798 DIO200_DRIVER_NAME, dev) >= 0) {
1801 dev_warn(dev->class_dev,
1802 "warning! irq %u unavailable!\n", irq);
1805 dio200_report_attach(dev, irq);
1809 /* Only called for ISA boards. */
1810 static int dio200_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1812 const struct dio200_board *thisboard = comedi_board(dev);
1813 struct dio200_private *devpriv;
1814 unsigned long iobase;
1821 dev_info(dev->class_dev, DIO200_DRIVER_NAME ": attach\n");
1823 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1826 dev->private = devpriv;
1828 iobase = it->options[0];
1829 irq = it->options[1];
1830 ret = dio200_request_region(dev, iobase, thisboard->mainsize);
1833 devpriv->io.u.iobase = iobase;
1834 devpriv->io.regtype = io_regtype;
1835 return dio200_common_attach(dev, irq, 0);
1839 * The auto_attach hook is called at PCI probe time via
1840 * comedi_pci_auto_config(). dev->board_ptr is NULL on entry.
1841 * The context should be an index into dio200_pci_boards[].
1843 static int dio200_auto_attach(struct comedi_device *dev,
1844 unsigned long context_model)
1846 struct pci_dev *pci_dev = comedi_to_pci_dev(dev);
1847 const struct dio200_board *thisboard = NULL;
1848 struct dio200_private *devpriv;
1849 resource_size_t base, len;
1856 if (context_model < ARRAY_SIZE(dio200_pci_boards))
1857 thisboard = &dio200_pci_boards[context_model];
1860 dev->board_ptr = thisboard;
1861 dev->board_name = thisboard->name;
1863 dev_info(dev->class_dev, DIO200_DRIVER_NAME ": attach pci %s\n",
1866 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1869 dev->private = devpriv;
1871 ret = comedi_pci_enable(dev);
1875 bar = thisboard->mainbar;
1876 base = pci_resource_start(pci_dev, bar);
1877 len = pci_resource_len(pci_dev, bar);
1878 if (len < thisboard->mainsize) {
1879 dev_err(dev->class_dev, "error! PCI region size too small!\n");
1882 if ((pci_resource_flags(pci_dev, bar) & IORESOURCE_MEM) != 0) {
1883 devpriv->io.u.membase = ioremap_nocache(base, len);
1884 if (!devpriv->io.u.membase) {
1885 dev_err(dev->class_dev,
1886 "error! cannot remap registers\n");
1889 devpriv->io.regtype = mmio_regtype;
1891 devpriv->io.u.iobase = (unsigned long)base;
1892 devpriv->io.regtype = io_regtype;
1894 switch (context_model) {
1898 ret = dio200_pcie_board_setup(dev);
1905 return dio200_common_attach(dev, pci_dev->irq, IRQF_SHARED);
1908 static void dio200_detach(struct comedi_device *dev)
1910 const struct dio200_board *thisboard = comedi_board(dev);
1911 struct dio200_private *devpriv = dev->private;
1912 const struct dio200_layout *layout;
1915 if (!thisboard || !devpriv)
1918 free_irq(dev->irq, dev);
1919 if (dev->subdevices) {
1920 layout = dio200_board_layout(thisboard);
1921 for (n = 0; n < dev->n_subdevices; n++) {
1922 struct comedi_subdevice *s = &dev->subdevices[n];
1923 switch (layout->sdtype[n]) {
1925 dio200_subdev_8254_cleanup(dev, s);
1928 dio200_subdev_8255_cleanup(dev, s);
1931 dio200_subdev_intr_cleanup(dev, s);
1934 /* Only on PCIe boards. */
1936 dio200_subdev_timer_cleanup(dev, s);
1943 if (is_isa_board(thisboard)) {
1944 if (devpriv->io.regtype == io_regtype)
1945 release_region(devpriv->io.u.iobase,
1946 thisboard->mainsize);
1947 } else if (is_pci_board(thisboard)) {
1948 if (devpriv->io.regtype == mmio_regtype)
1949 iounmap(devpriv->io.u.membase);
1950 comedi_pci_disable(dev);
1955 * The struct comedi_driver structure tells the Comedi core module
1956 * which functions to call to configure/deconfigure (attach/detach)
1957 * the board, and also about the kernel module that contains
1960 static struct comedi_driver amplc_dio200_driver = {
1961 .driver_name = DIO200_DRIVER_NAME,
1962 .module = THIS_MODULE,
1963 .attach = dio200_attach,
1964 .auto_attach = dio200_auto_attach,
1965 .detach = dio200_detach,
1967 .board_name = &dio200_isa_boards[0].name,
1968 .offset = sizeof(struct dio200_board),
1969 .num_names = ARRAY_SIZE(dio200_isa_boards),
1974 static DEFINE_PCI_DEVICE_TABLE(dio200_pci_table) = {
1976 PCI_VDEVICE(AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI215),
1979 PCI_VDEVICE(AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI272),
1982 PCI_VDEVICE(AMPLICON, PCI_DEVICE_ID_AMPLICON_PCIE236),
1985 PCI_VDEVICE(AMPLICON, PCI_DEVICE_ID_AMPLICON_PCIE215),
1988 PCI_VDEVICE(AMPLICON, PCI_DEVICE_ID_AMPLICON_PCIE296),
1994 MODULE_DEVICE_TABLE(pci, dio200_pci_table);
1996 static int amplc_dio200_pci_probe(struct pci_dev *dev,
1997 const struct pci_device_id *id)
1999 return comedi_pci_auto_config(dev, &lc_dio200_driver,
2003 static struct pci_driver amplc_dio200_pci_driver = {
2004 .name = DIO200_DRIVER_NAME,
2005 .id_table = dio200_pci_table,
2006 .probe = &lc_dio200_pci_probe,
2007 .remove = comedi_pci_auto_unconfig,
2009 module_comedi_pci_driver(amplc_dio200_driver, amplc_dio200_pci_driver);
2011 module_comedi_driver(amplc_dio200_driver);
2014 MODULE_AUTHOR("Comedi http://www.comedi.org");
2015 MODULE_DESCRIPTION("Comedi low-level driver");
2016 MODULE_LICENSE("GPL");