18ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28ffdff6aSGreg Kroah-Hartman /*
38ffdff6aSGreg Kroah-Hartman * comedi/drivers/ni_daq_700.c
48ffdff6aSGreg Kroah-Hartman * Driver for DAQCard-700 DIO/AI
58ffdff6aSGreg Kroah-Hartman * copied from 8255
68ffdff6aSGreg Kroah-Hartman *
78ffdff6aSGreg Kroah-Hartman * COMEDI - Linux Control and Measurement Device Interface
88ffdff6aSGreg Kroah-Hartman * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
98ffdff6aSGreg Kroah-Hartman */
108ffdff6aSGreg Kroah-Hartman
118ffdff6aSGreg Kroah-Hartman /*
128ffdff6aSGreg Kroah-Hartman * Driver: ni_daq_700
138ffdff6aSGreg Kroah-Hartman * Description: National Instruments PCMCIA DAQCard-700
148ffdff6aSGreg Kroah-Hartman * Author: Fred Brooks <nsaspook@nsaspook.com>,
158ffdff6aSGreg Kroah-Hartman * based on ni_daq_dio24 by Daniel Vecino Castel <dvecino@able.es>
168ffdff6aSGreg Kroah-Hartman * Devices: [National Instruments] PCMCIA DAQ-Card-700 (ni_daq_700)
178ffdff6aSGreg Kroah-Hartman * Status: works
188ffdff6aSGreg Kroah-Hartman * Updated: Wed, 21 May 2014 12:07:20 +0000
198ffdff6aSGreg Kroah-Hartman *
208ffdff6aSGreg Kroah-Hartman * The daqcard-700 appears in Comedi as a digital I/O subdevice (0) with
218ffdff6aSGreg Kroah-Hartman * 16 channels and a analog input subdevice (1) with 16 single-ended channels
228ffdff6aSGreg Kroah-Hartman * or 8 differential channels, and three input ranges.
238ffdff6aSGreg Kroah-Hartman *
248ffdff6aSGreg Kroah-Hartman * Digital: The channel 0 corresponds to the daqcard-700's output
258ffdff6aSGreg Kroah-Hartman * port, bit 0; channel 8 corresponds to the input port, bit 0.
268ffdff6aSGreg Kroah-Hartman *
278ffdff6aSGreg Kroah-Hartman * Digital direction configuration: channels 0-7 output, 8-15 input.
288ffdff6aSGreg Kroah-Hartman *
298ffdff6aSGreg Kroah-Hartman * Analog: The input range is 0 to 4095 with a default of -10 to +10 volts.
308ffdff6aSGreg Kroah-Hartman * Valid ranges:
318ffdff6aSGreg Kroah-Hartman * 0 for -10 to 10V bipolar
328ffdff6aSGreg Kroah-Hartman * 1 for -5 to 5V bipolar
338ffdff6aSGreg Kroah-Hartman * 2 for -2.5 to 2.5V bipolar
348ffdff6aSGreg Kroah-Hartman *
358ffdff6aSGreg Kroah-Hartman * IRQ is assigned but not used.
368ffdff6aSGreg Kroah-Hartman *
378ffdff6aSGreg Kroah-Hartman * Manuals: Register level: https://www.ni.com/pdf/manuals/340698.pdf
388ffdff6aSGreg Kroah-Hartman * User Manual: https://www.ni.com/pdf/manuals/320676d.pdf
398ffdff6aSGreg Kroah-Hartman */
408ffdff6aSGreg Kroah-Hartman
418ffdff6aSGreg Kroah-Hartman #include <linux/module.h>
428ffdff6aSGreg Kroah-Hartman #include <linux/delay.h>
438ffdff6aSGreg Kroah-Hartman #include <linux/interrupt.h>
44*df0e68c1SIan Abbott #include <linux/comedi/comedi_pcmcia.h>
458ffdff6aSGreg Kroah-Hartman
468ffdff6aSGreg Kroah-Hartman /* daqcard700 registers */
478ffdff6aSGreg Kroah-Hartman #define DIO_W 0x04 /* WO 8bit */
488ffdff6aSGreg Kroah-Hartman #define DIO_R 0x05 /* RO 8bit */
498ffdff6aSGreg Kroah-Hartman #define CMD_R1 0x00 /* WO 8bit */
508ffdff6aSGreg Kroah-Hartman #define CMD_R2 0x07 /* RW 8bit */
518ffdff6aSGreg Kroah-Hartman #define CMD_R3 0x05 /* W0 8bit */
528ffdff6aSGreg Kroah-Hartman #define STA_R1 0x00 /* RO 8bit */
538ffdff6aSGreg Kroah-Hartman #define STA_R2 0x01 /* RO 8bit */
548ffdff6aSGreg Kroah-Hartman #define ADFIFO_R 0x02 /* RO 16bit */
558ffdff6aSGreg Kroah-Hartman #define ADCLEAR_R 0x01 /* WO 8bit */
568ffdff6aSGreg Kroah-Hartman #define CDA_R0 0x08 /* RW 8bit */
578ffdff6aSGreg Kroah-Hartman #define CDA_R1 0x09 /* RW 8bit */
588ffdff6aSGreg Kroah-Hartman #define CDA_R2 0x0A /* RW 8bit */
598ffdff6aSGreg Kroah-Hartman #define CMO_R 0x0B /* RO 8bit */
608ffdff6aSGreg Kroah-Hartman #define TIC_R 0x06 /* WO 8bit */
618ffdff6aSGreg Kroah-Hartman /* daqcard700 modes */
628ffdff6aSGreg Kroah-Hartman #define CMD_R3_DIFF 0x04 /* diff mode */
638ffdff6aSGreg Kroah-Hartman
648ffdff6aSGreg Kroah-Hartman static const struct comedi_lrange range_daq700_ai = {
658ffdff6aSGreg Kroah-Hartman 3,
668ffdff6aSGreg Kroah-Hartman {
678ffdff6aSGreg Kroah-Hartman BIP_RANGE(10),
688ffdff6aSGreg Kroah-Hartman BIP_RANGE(5),
698ffdff6aSGreg Kroah-Hartman BIP_RANGE(2.5)
708ffdff6aSGreg Kroah-Hartman }
718ffdff6aSGreg Kroah-Hartman };
728ffdff6aSGreg Kroah-Hartman
daq700_dio_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)738ffdff6aSGreg Kroah-Hartman static int daq700_dio_insn_bits(struct comedi_device *dev,
748ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s,
758ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn,
768ffdff6aSGreg Kroah-Hartman unsigned int *data)
778ffdff6aSGreg Kroah-Hartman {
788ffdff6aSGreg Kroah-Hartman unsigned int mask;
798ffdff6aSGreg Kroah-Hartman unsigned int val;
808ffdff6aSGreg Kroah-Hartman
818ffdff6aSGreg Kroah-Hartman mask = comedi_dio_update_state(s, data);
828ffdff6aSGreg Kroah-Hartman if (mask) {
838ffdff6aSGreg Kroah-Hartman if (mask & 0xff)
848ffdff6aSGreg Kroah-Hartman outb(s->state & 0xff, dev->iobase + DIO_W);
858ffdff6aSGreg Kroah-Hartman }
868ffdff6aSGreg Kroah-Hartman
878ffdff6aSGreg Kroah-Hartman val = s->state & 0xff;
888ffdff6aSGreg Kroah-Hartman val |= inb(dev->iobase + DIO_R) << 8;
898ffdff6aSGreg Kroah-Hartman
908ffdff6aSGreg Kroah-Hartman data[1] = val;
918ffdff6aSGreg Kroah-Hartman
928ffdff6aSGreg Kroah-Hartman return insn->n;
938ffdff6aSGreg Kroah-Hartman }
948ffdff6aSGreg Kroah-Hartman
daq700_dio_insn_config(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)958ffdff6aSGreg Kroah-Hartman static int daq700_dio_insn_config(struct comedi_device *dev,
968ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s,
978ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn,
988ffdff6aSGreg Kroah-Hartman unsigned int *data)
998ffdff6aSGreg Kroah-Hartman {
1008ffdff6aSGreg Kroah-Hartman int ret;
1018ffdff6aSGreg Kroah-Hartman
1028ffdff6aSGreg Kroah-Hartman ret = comedi_dio_insn_config(dev, s, insn, data, 0);
1038ffdff6aSGreg Kroah-Hartman if (ret)
1048ffdff6aSGreg Kroah-Hartman return ret;
1058ffdff6aSGreg Kroah-Hartman
1068ffdff6aSGreg Kroah-Hartman /* The DIO channels are not configurable, fix the io_bits */
1078ffdff6aSGreg Kroah-Hartman s->io_bits = 0x00ff;
1088ffdff6aSGreg Kroah-Hartman
1098ffdff6aSGreg Kroah-Hartman return insn->n;
1108ffdff6aSGreg Kroah-Hartman }
1118ffdff6aSGreg Kroah-Hartman
daq700_ai_eoc(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned long context)1128ffdff6aSGreg Kroah-Hartman static int daq700_ai_eoc(struct comedi_device *dev,
1138ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s,
1148ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn,
1158ffdff6aSGreg Kroah-Hartman unsigned long context)
1168ffdff6aSGreg Kroah-Hartman {
1178ffdff6aSGreg Kroah-Hartman unsigned int status;
1188ffdff6aSGreg Kroah-Hartman
1198ffdff6aSGreg Kroah-Hartman status = inb(dev->iobase + STA_R2);
1208ffdff6aSGreg Kroah-Hartman if ((status & 0x03))
1218ffdff6aSGreg Kroah-Hartman return -EOVERFLOW;
1228ffdff6aSGreg Kroah-Hartman status = inb(dev->iobase + STA_R1);
1238ffdff6aSGreg Kroah-Hartman if ((status & 0x02))
1248ffdff6aSGreg Kroah-Hartman return -ENODATA;
1258ffdff6aSGreg Kroah-Hartman if ((status & 0x11) == 0x01)
1268ffdff6aSGreg Kroah-Hartman return 0;
1278ffdff6aSGreg Kroah-Hartman return -EBUSY;
1288ffdff6aSGreg Kroah-Hartman }
1298ffdff6aSGreg Kroah-Hartman
daq700_ai_rinsn(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)1308ffdff6aSGreg Kroah-Hartman static int daq700_ai_rinsn(struct comedi_device *dev,
1318ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s,
1328ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn, unsigned int *data)
1338ffdff6aSGreg Kroah-Hartman {
1348ffdff6aSGreg Kroah-Hartman int n;
1358ffdff6aSGreg Kroah-Hartman int d;
1368ffdff6aSGreg Kroah-Hartman int ret;
1378ffdff6aSGreg Kroah-Hartman unsigned int chan = CR_CHAN(insn->chanspec);
1388ffdff6aSGreg Kroah-Hartman unsigned int aref = CR_AREF(insn->chanspec);
1398ffdff6aSGreg Kroah-Hartman unsigned int range = CR_RANGE(insn->chanspec);
1408ffdff6aSGreg Kroah-Hartman unsigned int r3_bits = 0;
1418ffdff6aSGreg Kroah-Hartman
1428ffdff6aSGreg Kroah-Hartman /* set channel input modes */
1438ffdff6aSGreg Kroah-Hartman if (aref == AREF_DIFF)
1448ffdff6aSGreg Kroah-Hartman r3_bits |= CMD_R3_DIFF;
1458ffdff6aSGreg Kroah-Hartman /* write channel mode/range */
1468ffdff6aSGreg Kroah-Hartman if (range >= 1)
1478ffdff6aSGreg Kroah-Hartman range++; /* convert range to hardware value */
1488ffdff6aSGreg Kroah-Hartman outb(r3_bits | (range & 0x03), dev->iobase + CMD_R3);
1498ffdff6aSGreg Kroah-Hartman
1508ffdff6aSGreg Kroah-Hartman /* write channel to multiplexer */
1518ffdff6aSGreg Kroah-Hartman /* set mask scan bit high to disable scanning */
1528ffdff6aSGreg Kroah-Hartman outb(chan | 0x80, dev->iobase + CMD_R1);
1538ffdff6aSGreg Kroah-Hartman /* mux needs 2us to really settle [Fred Brooks]. */
1548ffdff6aSGreg Kroah-Hartman udelay(2);
1558ffdff6aSGreg Kroah-Hartman
1568ffdff6aSGreg Kroah-Hartman /* convert n samples */
1578ffdff6aSGreg Kroah-Hartman for (n = 0; n < insn->n; n++) {
1588ffdff6aSGreg Kroah-Hartman /* trigger conversion with out0 L to H */
1598ffdff6aSGreg Kroah-Hartman outb(0x00, dev->iobase + CMD_R2); /* enable ADC conversions */
1608ffdff6aSGreg Kroah-Hartman outb(0x30, dev->iobase + CMO_R); /* mode 0 out0 L, from H */
1618ffdff6aSGreg Kroah-Hartman outb(0x00, dev->iobase + ADCLEAR_R); /* clear the ADC FIFO */
1628ffdff6aSGreg Kroah-Hartman /* read 16bit junk from FIFO to clear */
1638ffdff6aSGreg Kroah-Hartman inw(dev->iobase + ADFIFO_R);
1648ffdff6aSGreg Kroah-Hartman /* mode 1 out0 H, L to H, start conversion */
1658ffdff6aSGreg Kroah-Hartman outb(0x32, dev->iobase + CMO_R);
1668ffdff6aSGreg Kroah-Hartman
1678ffdff6aSGreg Kroah-Hartman /* wait for conversion to end */
1688ffdff6aSGreg Kroah-Hartman ret = comedi_timeout(dev, s, insn, daq700_ai_eoc, 0);
1698ffdff6aSGreg Kroah-Hartman if (ret)
1708ffdff6aSGreg Kroah-Hartman return ret;
1718ffdff6aSGreg Kroah-Hartman
1728ffdff6aSGreg Kroah-Hartman /* read data */
1738ffdff6aSGreg Kroah-Hartman d = inw(dev->iobase + ADFIFO_R);
1748ffdff6aSGreg Kroah-Hartman /* mangle the data as necessary */
1758ffdff6aSGreg Kroah-Hartman /* Bipolar Offset Binary: 0 to 4095 for -10 to +10 */
1768ffdff6aSGreg Kroah-Hartman d &= 0x0fff;
1778ffdff6aSGreg Kroah-Hartman d ^= 0x0800;
1788ffdff6aSGreg Kroah-Hartman data[n] = d;
1798ffdff6aSGreg Kroah-Hartman }
1808ffdff6aSGreg Kroah-Hartman return n;
1818ffdff6aSGreg Kroah-Hartman }
1828ffdff6aSGreg Kroah-Hartman
1838ffdff6aSGreg Kroah-Hartman /*
1848ffdff6aSGreg Kroah-Hartman * Data acquisition is enabled.
1858ffdff6aSGreg Kroah-Hartman * The counter 0 output is high.
1868ffdff6aSGreg Kroah-Hartman * The I/O connector pin CLK1 drives counter 1 source.
1878ffdff6aSGreg Kroah-Hartman * Multiple-channel scanning is disabled.
1888ffdff6aSGreg Kroah-Hartman * All interrupts are disabled.
1898ffdff6aSGreg Kroah-Hartman * The analog input range is set to +-10 V
1908ffdff6aSGreg Kroah-Hartman * The analog input mode is single-ended.
1918ffdff6aSGreg Kroah-Hartman * The analog input circuitry is initialized to channel 0.
1928ffdff6aSGreg Kroah-Hartman * The A/D FIFO is cleared.
1938ffdff6aSGreg Kroah-Hartman */
daq700_ai_config(struct comedi_device * dev,struct comedi_subdevice * s)1948ffdff6aSGreg Kroah-Hartman static void daq700_ai_config(struct comedi_device *dev,
1958ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s)
1968ffdff6aSGreg Kroah-Hartman {
1978ffdff6aSGreg Kroah-Hartman unsigned long iobase = dev->iobase;
1988ffdff6aSGreg Kroah-Hartman
1998ffdff6aSGreg Kroah-Hartman outb(0x80, iobase + CMD_R1); /* disable scanning, ADC to chan 0 */
2008ffdff6aSGreg Kroah-Hartman outb(0x00, iobase + CMD_R2); /* clear all bits */
2018ffdff6aSGreg Kroah-Hartman outb(0x00, iobase + CMD_R3); /* set +-10 range */
2028ffdff6aSGreg Kroah-Hartman outb(0x32, iobase + CMO_R); /* config counter mode1, out0 to H */
2038ffdff6aSGreg Kroah-Hartman outb(0x00, iobase + TIC_R); /* clear counter interrupt */
2048ffdff6aSGreg Kroah-Hartman outb(0x00, iobase + ADCLEAR_R); /* clear the ADC FIFO */
2058ffdff6aSGreg Kroah-Hartman inw(iobase + ADFIFO_R); /* read 16bit junk from FIFO to clear */
2068ffdff6aSGreg Kroah-Hartman }
2078ffdff6aSGreg Kroah-Hartman
daq700_auto_attach(struct comedi_device * dev,unsigned long context)2088ffdff6aSGreg Kroah-Hartman static int daq700_auto_attach(struct comedi_device *dev,
2098ffdff6aSGreg Kroah-Hartman unsigned long context)
2108ffdff6aSGreg Kroah-Hartman {
2118ffdff6aSGreg Kroah-Hartman struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
2128ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s;
2138ffdff6aSGreg Kroah-Hartman int ret;
2148ffdff6aSGreg Kroah-Hartman
2158ffdff6aSGreg Kroah-Hartman link->config_flags |= CONF_AUTO_SET_IO;
2168ffdff6aSGreg Kroah-Hartman ret = comedi_pcmcia_enable(dev, NULL);
2178ffdff6aSGreg Kroah-Hartman if (ret)
2188ffdff6aSGreg Kroah-Hartman return ret;
2198ffdff6aSGreg Kroah-Hartman dev->iobase = link->resource[0]->start;
2208ffdff6aSGreg Kroah-Hartman
2218ffdff6aSGreg Kroah-Hartman ret = comedi_alloc_subdevices(dev, 2);
2228ffdff6aSGreg Kroah-Hartman if (ret)
2238ffdff6aSGreg Kroah-Hartman return ret;
2248ffdff6aSGreg Kroah-Hartman
2258ffdff6aSGreg Kroah-Hartman /* DAQCard-700 dio */
2268ffdff6aSGreg Kroah-Hartman s = &dev->subdevices[0];
2278ffdff6aSGreg Kroah-Hartman s->type = COMEDI_SUBD_DIO;
2288ffdff6aSGreg Kroah-Hartman s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2298ffdff6aSGreg Kroah-Hartman s->n_chan = 16;
2308ffdff6aSGreg Kroah-Hartman s->range_table = &range_digital;
2318ffdff6aSGreg Kroah-Hartman s->maxdata = 1;
2328ffdff6aSGreg Kroah-Hartman s->insn_bits = daq700_dio_insn_bits;
2338ffdff6aSGreg Kroah-Hartman s->insn_config = daq700_dio_insn_config;
2348ffdff6aSGreg Kroah-Hartman s->io_bits = 0x00ff;
2358ffdff6aSGreg Kroah-Hartman
2368ffdff6aSGreg Kroah-Hartman /* DAQCard-700 ai */
2378ffdff6aSGreg Kroah-Hartman s = &dev->subdevices[1];
2388ffdff6aSGreg Kroah-Hartman s->type = COMEDI_SUBD_AI;
2398ffdff6aSGreg Kroah-Hartman s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
2408ffdff6aSGreg Kroah-Hartman s->n_chan = 16;
2418ffdff6aSGreg Kroah-Hartman s->maxdata = BIT(12) - 1;
2428ffdff6aSGreg Kroah-Hartman s->range_table = &range_daq700_ai;
2438ffdff6aSGreg Kroah-Hartman s->insn_read = daq700_ai_rinsn;
2448ffdff6aSGreg Kroah-Hartman daq700_ai_config(dev, s);
2458ffdff6aSGreg Kroah-Hartman
2468ffdff6aSGreg Kroah-Hartman return 0;
2478ffdff6aSGreg Kroah-Hartman }
2488ffdff6aSGreg Kroah-Hartman
2498ffdff6aSGreg Kroah-Hartman static struct comedi_driver daq700_driver = {
2508ffdff6aSGreg Kroah-Hartman .driver_name = "ni_daq_700",
2518ffdff6aSGreg Kroah-Hartman .module = THIS_MODULE,
2528ffdff6aSGreg Kroah-Hartman .auto_attach = daq700_auto_attach,
2538ffdff6aSGreg Kroah-Hartman .detach = comedi_pcmcia_disable,
2548ffdff6aSGreg Kroah-Hartman };
2558ffdff6aSGreg Kroah-Hartman
daq700_cs_attach(struct pcmcia_device * link)2568ffdff6aSGreg Kroah-Hartman static int daq700_cs_attach(struct pcmcia_device *link)
2578ffdff6aSGreg Kroah-Hartman {
2588ffdff6aSGreg Kroah-Hartman return comedi_pcmcia_auto_config(link, &daq700_driver);
2598ffdff6aSGreg Kroah-Hartman }
2608ffdff6aSGreg Kroah-Hartman
2618ffdff6aSGreg Kroah-Hartman static const struct pcmcia_device_id daq700_cs_ids[] = {
2628ffdff6aSGreg Kroah-Hartman PCMCIA_DEVICE_MANF_CARD(0x010b, 0x4743),
2638ffdff6aSGreg Kroah-Hartman PCMCIA_DEVICE_NULL
2648ffdff6aSGreg Kroah-Hartman };
2658ffdff6aSGreg Kroah-Hartman MODULE_DEVICE_TABLE(pcmcia, daq700_cs_ids);
2668ffdff6aSGreg Kroah-Hartman
2678ffdff6aSGreg Kroah-Hartman static struct pcmcia_driver daq700_cs_driver = {
2688ffdff6aSGreg Kroah-Hartman .name = "ni_daq_700",
2698ffdff6aSGreg Kroah-Hartman .owner = THIS_MODULE,
2708ffdff6aSGreg Kroah-Hartman .id_table = daq700_cs_ids,
2718ffdff6aSGreg Kroah-Hartman .probe = daq700_cs_attach,
2728ffdff6aSGreg Kroah-Hartman .remove = comedi_pcmcia_auto_unconfig,
2738ffdff6aSGreg Kroah-Hartman };
2748ffdff6aSGreg Kroah-Hartman module_comedi_pcmcia_driver(daq700_driver, daq700_cs_driver);
2758ffdff6aSGreg Kroah-Hartman
2768ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("Fred Brooks <nsaspook@nsaspook.com>");
2778ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION(
2788ffdff6aSGreg Kroah-Hartman "Comedi driver for National Instruments PCMCIA DAQCard-700 DIO/AI");
2798ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL");
280