1/* 2 * comedi/drivers/ni_daq_700.c 3 * Driver for DAQCard-700 DIO/AI 4 * copied from 8255 5 * 6 * COMEDI - Linux Control and Measurement Device Interface 7 * Copyright (C) 1998 David A. Schleef <ds@schleef.org> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 */ 19 20/* 21 * Driver: ni_daq_700 22 * Description: National Instruments PCMCIA DAQCard-700 23 * Author: Fred Brooks <nsaspook@nsaspook.com>, 24 * based on ni_daq_dio24 by Daniel Vecino Castel <dvecino@able.es> 25 * Devices: [National Instruments] PCMCIA DAQ-Card-700 (ni_daq_700) 26 * Status: works 27 * Updated: Wed, 21 May 2014 12:07:20 +0000 28 * 29 * The daqcard-700 appears in Comedi as a digital I/O subdevice (0) with 30 * 16 channels and a analog input subdevice (1) with 16 single-ended channels 31 * or 8 differential channels, and three input ranges. 32 * 33 * Digital: The channel 0 corresponds to the daqcard-700's output 34 * port, bit 0; channel 8 corresponds to the input port, bit 0. 35 * 36 * Digital direction configuration: channels 0-7 output, 8-15 input. 37 * 38 * Analog: The input range is 0 to 4095 with a default of -10 to +10 volts. 39 * Valid ranges: 40 * 0 for -10 to 10V bipolar 41 * 1 for -5 to 5V bipolar 42 * 2 for -2.5 to 2.5V bipolar 43 * 44 * IRQ is assigned but not used. 45 * 46 * Manuals: Register level: http://www.ni.com/pdf/manuals/340698.pdf 47 * User Manual: http://www.ni.com/pdf/manuals/320676d.pdf 48 */ 49 50#include <linux/module.h> 51#include <linux/delay.h> 52#include <linux/interrupt.h> 53 54#include "../comedidev.h" 55 56#include <pcmcia/cistpl.h> 57#include <pcmcia/ds.h> 58 59/* daqcard700 registers */ 60#define DIO_W 0x04 /* WO 8bit */ 61#define DIO_R 0x05 /* RO 8bit */ 62#define CMD_R1 0x00 /* WO 8bit */ 63#define CMD_R2 0x07 /* RW 8bit */ 64#define CMD_R3 0x05 /* W0 8bit */ 65#define STA_R1 0x00 /* RO 8bit */ 66#define STA_R2 0x01 /* RO 8bit */ 67#define ADFIFO_R 0x02 /* RO 16bit */ 68#define ADCLEAR_R 0x01 /* WO 8bit */ 69#define CDA_R0 0x08 /* RW 8bit */ 70#define CDA_R1 0x09 /* RW 8bit */ 71#define CDA_R2 0x0A /* RW 8bit */ 72#define CMO_R 0x0B /* RO 8bit */ 73#define TIC_R 0x06 /* WO 8bit */ 74/* daqcard700 modes */ 75#define CMD_R3_DIFF 0x04 /* diff mode */ 76 77static const struct comedi_lrange range_daq700_ai = { 78 3, 79 { 80 BIP_RANGE(10), 81 BIP_RANGE(5), 82 BIP_RANGE(2.5) 83 } 84}; 85 86static int daq700_dio_insn_bits(struct comedi_device *dev, 87 struct comedi_subdevice *s, 88 struct comedi_insn *insn, 89 unsigned int *data) 90{ 91 unsigned int mask; 92 unsigned int val; 93 94 mask = comedi_dio_update_state(s, data); 95 if (mask) { 96 if (mask & 0xff) 97 outb(s->state & 0xff, dev->iobase + DIO_W); 98 } 99 100 val = s->state & 0xff; 101 val |= inb(dev->iobase + DIO_R) << 8; 102 103 data[1] = val; 104 105 return insn->n; 106} 107 108static int daq700_dio_insn_config(struct comedi_device *dev, 109 struct comedi_subdevice *s, 110 struct comedi_insn *insn, 111 unsigned int *data) 112{ 113 int ret; 114 115 ret = comedi_dio_insn_config(dev, s, insn, data, 0); 116 if (ret) 117 return ret; 118 119 /* The DIO channels are not configurable, fix the io_bits */ 120 s->io_bits = 0x00ff; 121 122 return insn->n; 123} 124 125static int daq700_ai_eoc(struct comedi_device *dev, 126 struct comedi_subdevice *s, 127 struct comedi_insn *insn, 128 unsigned long context) 129{ 130 unsigned int status; 131 132 status = inb(dev->iobase + STA_R2); 133 if ((status & 0x03)) 134 return -EOVERFLOW; 135 status = inb(dev->iobase + STA_R1); 136 if ((status & 0x02)) 137 return -ENODATA; 138 if ((status & 0x11) == 0x01) 139 return 0; 140 return -EBUSY; 141} 142 143static int daq700_ai_rinsn(struct comedi_device *dev, 144 struct comedi_subdevice *s, 145 struct comedi_insn *insn, unsigned int *data) 146{ 147 int n; 148 int d; 149 int ret; 150 unsigned int chan = CR_CHAN(insn->chanspec); 151 unsigned int aref = CR_AREF(insn->chanspec); 152 unsigned int range = CR_RANGE(insn->chanspec); 153 unsigned int r3_bits = 0; 154 155 /* set channel input modes */ 156 if (aref == AREF_DIFF) 157 r3_bits |= CMD_R3_DIFF; 158 /* write channel mode/range */ 159 if (range >= 1) 160 range++; /* convert range to hardware value */ 161 outb(r3_bits | (range & 0x03), dev->iobase + CMD_R3); 162 163 /* write channel to multiplexer */ 164 /* set mask scan bit high to disable scanning */ 165 outb(chan | 0x80, dev->iobase + CMD_R1); 166 /* mux needs 2us to really settle [Fred Brooks]. */ 167 udelay(2); 168 169 /* convert n samples */ 170 for (n = 0; n < insn->n; n++) { 171 /* trigger conversion with out0 L to H */ 172 outb(0x00, dev->iobase + CMD_R2); /* enable ADC conversions */ 173 outb(0x30, dev->iobase + CMO_R); /* mode 0 out0 L, from H */ 174 outb(0x00, dev->iobase + ADCLEAR_R); /* clear the ADC FIFO */ 175 /* read 16bit junk from FIFO to clear */ 176 inw(dev->iobase + ADFIFO_R); 177 /* mode 1 out0 H, L to H, start conversion */ 178 outb(0x32, dev->iobase + CMO_R); 179 180 /* wait for conversion to end */ 181 ret = comedi_timeout(dev, s, insn, daq700_ai_eoc, 0); 182 if (ret) 183 return ret; 184 185 /* read data */ 186 d = inw(dev->iobase + ADFIFO_R); 187 /* mangle the data as necessary */ 188 /* Bipolar Offset Binary: 0 to 4095 for -10 to +10 */ 189 d &= 0x0fff; 190 d ^= 0x0800; 191 data[n] = d; 192 } 193 return n; 194} 195 196/* 197 * Data acquisition is enabled. 198 * The counter 0 output is high. 199 * The I/O connector pin CLK1 drives counter 1 source. 200 * Multiple-channel scanning is disabled. 201 * All interrupts are disabled. 202 * The analog input range is set to +-10 V 203 * The analog input mode is single-ended. 204 * The analog input circuitry is initialized to channel 0. 205 * The A/D FIFO is cleared. 206 */ 207static void daq700_ai_config(struct comedi_device *dev, 208 struct comedi_subdevice *s) 209{ 210 unsigned long iobase = dev->iobase; 211 212 outb(0x80, iobase + CMD_R1); /* disable scanning, ADC to chan 0 */ 213 outb(0x00, iobase + CMD_R2); /* clear all bits */ 214 outb(0x00, iobase + CMD_R3); /* set +-10 range */ 215 outb(0x32, iobase + CMO_R); /* config counter mode1, out0 to H */ 216 outb(0x00, iobase + TIC_R); /* clear counter interrupt */ 217 outb(0x00, iobase + ADCLEAR_R); /* clear the ADC FIFO */ 218 inw(iobase + ADFIFO_R); /* read 16bit junk from FIFO to clear */ 219} 220 221static int daq700_auto_attach(struct comedi_device *dev, 222 unsigned long context) 223{ 224 struct pcmcia_device *link = comedi_to_pcmcia_dev(dev); 225 struct comedi_subdevice *s; 226 int ret; 227 228 link->config_flags |= CONF_AUTO_SET_IO; 229 ret = comedi_pcmcia_enable(dev, NULL); 230 if (ret) 231 return ret; 232 dev->iobase = link->resource[0]->start; 233 234 ret = comedi_alloc_subdevices(dev, 2); 235 if (ret) 236 return ret; 237 238 /* DAQCard-700 dio */ 239 s = &dev->subdevices[0]; 240 s->type = COMEDI_SUBD_DIO; 241 s->subdev_flags = SDF_READABLE | SDF_WRITABLE; 242 s->n_chan = 16; 243 s->range_table = &range_digital; 244 s->maxdata = 1; 245 s->insn_bits = daq700_dio_insn_bits; 246 s->insn_config = daq700_dio_insn_config; 247 s->io_bits = 0x00ff; 248 249 /* DAQCard-700 ai */ 250 s = &dev->subdevices[1]; 251 s->type = COMEDI_SUBD_AI; 252 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF; 253 s->n_chan = 16; 254 s->maxdata = (1 << 12) - 1; 255 s->range_table = &range_daq700_ai; 256 s->insn_read = daq700_ai_rinsn; 257 daq700_ai_config(dev, s); 258 259 return 0; 260} 261 262static struct comedi_driver daq700_driver = { 263 .driver_name = "ni_daq_700", 264 .module = THIS_MODULE, 265 .auto_attach = daq700_auto_attach, 266 .detach = comedi_pcmcia_disable, 267}; 268 269static int daq700_cs_attach(struct pcmcia_device *link) 270{ 271 return comedi_pcmcia_auto_config(link, &daq700_driver); 272} 273 274static const struct pcmcia_device_id daq700_cs_ids[] = { 275 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x4743), 276 PCMCIA_DEVICE_NULL 277}; 278MODULE_DEVICE_TABLE(pcmcia, daq700_cs_ids); 279 280static struct pcmcia_driver daq700_cs_driver = { 281 .name = "ni_daq_700", 282 .owner = THIS_MODULE, 283 .id_table = daq700_cs_ids, 284 .probe = daq700_cs_attach, 285 .remove = comedi_pcmcia_auto_unconfig, 286}; 287module_comedi_pcmcia_driver(daq700_driver, daq700_cs_driver); 288 289MODULE_AUTHOR("Fred Brooks <nsaspook@nsaspook.com>"); 290MODULE_DESCRIPTION( 291 "Comedi driver for National Instruments PCMCIA DAQCard-700 DIO/AI"); 292MODULE_LICENSE("GPL"); 293