2 comedi/drivers/ni_at_ao.c
3 Driver for NI AT-AO-6/10 boards
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000,2002 David A. Schleef <ds@schleef.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.
20 Description: National Instruments AT-AO-6/10
21 Devices: [National Instruments] AT-AO-6 (at-ao-6), AT-AO-10 (at-ao-10)
24 Updated: Sun Dec 26 12:26:28 EST 2004
26 Configuration options:
27 [0] - I/O port base address
30 [3] - analog output range, set by jumpers on hardware (0 for -10 to 10V
31 bipolar, 1 for 0V to 10V unipolar)
35 * Register-level programming information can be found in NI
36 * document 320379.pdf.
39 #include <linux/module.h>
40 #include "../comedidev.h"
43 /* registers with _2_ are accessed when GRP2WR is set in CFG1 */
45 #define ATAO_SIZE 0x20
47 #define ATAO_2_DMATCCLR 0x00 /* W 16 */
48 #define ATAO_DIN 0x00 /* R 16 */
49 #define ATAO_DOUT 0x00 /* W 16 */
51 #define ATAO_CFG2 0x02 /* W 16 */
69 #define ATAO_2_INT1CLR 0x02 /* W 16 */
71 #define ATAO_CFG3 0x04 /* W 16 */
72 #define DMAMODE 0x0040
75 #define DOUTEN2 0x0008
76 #define DOUTEN1 0x0004
80 #define ATAO_2_INT2CLR 0x04 /* W 16 */
82 #define ATAO_82C53_BASE 0x06 /* RW 8 */
84 #define ATAO_82C53_CNTR1 0x06 /* RW 8 */
85 #define ATAO_82C53_CNTR2 0x07 /* RW 8 */
86 #define ATAO_82C53_CNTR3 0x08 /* RW 8 */
87 #define ATAO_82C53_CNTRCMD 0x09 /* W 8 */
96 /* read-back command */
112 #define ATAO_2_RTSISHFT 0x06 /* W 8 */
115 #define ATAO_2_RTSISTRB 0x07 /* W 8 */
117 #define ATAO_CFG1 0x0a /* W 16 */
118 #define EXTINT2EN 0x8000
119 #define EXTINT1EN 0x4000
120 #define CNTINT2EN 0x2000
121 #define CNTINT1EN 0x1000
122 #define TCINTEN 0x0800
123 #define CNT1SRC 0x0400
124 #define CNT2SRC 0x0200
125 #define FIFOEN 0x0100
126 #define GRP2WR 0x0080
127 #define EXTUPDEN 0x0040
130 #define CH_mask 0x000f
131 #define ATAO_STATUS 0x0a /* R 16 */
138 #define PROMOUT 0x0001
140 #define ATAO_FIFO_WRITE 0x0c /* W 16 */
141 #define ATAO_FIFO_CLEAR 0x0c /* R 16 */
142 #define ATAO_DACn(x) (0x0c + 2*(x)) /* W */
145 * Board descriptions for two imaginary boards. Describing the
146 * boards in this way is optional, and completely driver-dependent.
147 * Some drivers use arrays such as this, other do not.
154 struct atao_private {
160 /* Used for AO readback */
161 unsigned int ao_readback[10];
164 static void atao_reset(struct comedi_device *dev)
166 struct atao_private *devpriv = dev->private;
168 /* This is the reset sequence described in the manual */
171 outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
173 outb(RWSEL0 | MODESEL2, dev->iobase + ATAO_82C53_CNTRCMD);
174 outb(0x03, dev->iobase + ATAO_82C53_CNTR1);
175 outb(CNTRSEL0 | RWSEL0 | MODESEL2, dev->iobase + ATAO_82C53_CNTRCMD);
178 outw(devpriv->cfg2, dev->iobase + ATAO_CFG2);
181 outw(devpriv->cfg3, dev->iobase + ATAO_CFG3);
183 inw(dev->iobase + ATAO_FIFO_CLEAR);
185 devpriv->cfg1 |= GRP2WR;
186 outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
188 outw(0, dev->iobase + ATAO_2_INT1CLR);
189 outw(0, dev->iobase + ATAO_2_INT2CLR);
190 outw(0, dev->iobase + ATAO_2_DMATCCLR);
192 devpriv->cfg1 &= ~GRP2WR;
193 outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
196 static int atao_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
197 struct comedi_insn *insn, unsigned int *data)
199 struct atao_private *devpriv = dev->private;
201 int chan = CR_CHAN(insn->chanspec);
204 for (i = 0; i < insn->n; i++) {
205 bits = data[i] - 0x800;
207 devpriv->cfg1 |= GRP2WR;
208 outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
210 outw(bits, dev->iobase + ATAO_DACn(chan));
212 devpriv->cfg1 &= ~GRP2WR;
213 outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
215 devpriv->ao_readback[chan] = data[i];
221 static int atao_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
222 struct comedi_insn *insn, unsigned int *data)
224 struct atao_private *devpriv = dev->private;
226 int chan = CR_CHAN(insn->chanspec);
228 for (i = 0; i < insn->n; i++)
229 data[i] = devpriv->ao_readback[chan];
234 static int atao_dio_insn_bits(struct comedi_device *dev,
235 struct comedi_subdevice *s,
236 struct comedi_insn *insn, unsigned int *data)
239 s->state &= ~data[0];
240 s->state |= data[0] & data[1];
241 outw(s->state, dev->iobase + ATAO_DOUT);
244 data[1] = inw(dev->iobase + ATAO_DIN);
249 static int atao_dio_insn_config(struct comedi_device *dev,
250 struct comedi_subdevice *s,
251 struct comedi_insn *insn,
254 struct atao_private *devpriv = dev->private;
255 unsigned int chan = CR_CHAN(insn->chanspec);
264 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
268 if (s->io_bits & 0x0f)
269 devpriv->cfg3 |= DOUTEN1;
271 devpriv->cfg3 &= ~DOUTEN1;
272 if (s->io_bits & 0xf0)
273 devpriv->cfg3 |= DOUTEN2;
275 devpriv->cfg3 &= ~DOUTEN2;
277 outw(devpriv->cfg3, dev->iobase + ATAO_CFG3);
283 * Figure 2-1 in the manual shows 3 chips labeled DAC8800, which
284 * are 8-channel 8-bit DACs. These are most likely the calibration
285 * DACs. It is not explicitly stated in the manual how to access
286 * the caldacs, but we can guess.
288 static int atao_calib_insn_read(struct comedi_device *dev,
289 struct comedi_subdevice *s,
290 struct comedi_insn *insn, unsigned int *data)
293 for (i = 0; i < insn->n; i++)
294 data[i] = 0; /* XXX */
298 static int atao_calib_insn_write(struct comedi_device *dev,
299 struct comedi_subdevice *s,
300 struct comedi_insn *insn, unsigned int *data)
302 struct atao_private *devpriv = dev->private;
303 unsigned int bitstring, bit;
304 unsigned int chan = CR_CHAN(insn->chanspec);
306 bitstring = ((chan & 0x7) << 8) | (data[insn->n - 1] & 0xff);
308 for (bit = 1 << (11 - 1); bit; bit >>= 1) {
309 outw(devpriv->cfg2 | ((bit & bitstring) ? SDATA : 0),
310 dev->iobase + ATAO_CFG2);
311 outw(devpriv->cfg2 | SCLK | ((bit & bitstring) ? SDATA : 0),
312 dev->iobase + ATAO_CFG2);
314 /* strobe the appropriate caldac */
315 outw(devpriv->cfg2 | (((chan >> 3) + 1) << 14),
316 dev->iobase + ATAO_CFG2);
317 outw(devpriv->cfg2, dev->iobase + ATAO_CFG2);
322 static int atao_attach(struct comedi_device *dev, struct comedi_devconfig *it)
324 const struct atao_board *board = comedi_board(dev);
325 struct atao_private *devpriv;
326 struct comedi_subdevice *s;
330 ao_unipolar = it->options[3];
332 ret = comedi_request_region(dev, it->options[0], ATAO_SIZE);
336 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
340 ret = comedi_alloc_subdevices(dev, 4);
344 s = &dev->subdevices[0];
345 /* analog output subdevice */
346 s->type = COMEDI_SUBD_AO;
347 s->subdev_flags = SDF_WRITABLE;
348 s->n_chan = board->n_ao_chans;
349 s->maxdata = (1 << 12) - 1;
351 s->range_table = &range_unipolar10;
353 s->range_table = &range_bipolar10;
354 s->insn_write = &atao_ao_winsn;
355 s->insn_read = &atao_ao_rinsn;
357 s = &dev->subdevices[1];
358 /* digital i/o subdevice */
359 s->type = COMEDI_SUBD_DIO;
360 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
363 s->range_table = &range_digital;
364 s->insn_bits = atao_dio_insn_bits;
365 s->insn_config = atao_dio_insn_config;
367 s = &dev->subdevices[2];
368 /* caldac subdevice */
369 s->type = COMEDI_SUBD_CALIB;
370 s->subdev_flags = SDF_WRITABLE | SDF_INTERNAL;
373 s->insn_read = atao_calib_insn_read;
374 s->insn_write = atao_calib_insn_write;
376 s = &dev->subdevices[3];
377 /* eeprom subdevice */
378 /* s->type=COMEDI_SUBD_EEPROM; */
379 s->type = COMEDI_SUBD_UNUSED;
383 printk(KERN_INFO "\n");
388 static const struct atao_board atao_boards[] = {
398 static struct comedi_driver ni_at_ao_driver = {
399 .driver_name = "ni_at_ao",
400 .module = THIS_MODULE,
401 .attach = atao_attach,
402 .detach = comedi_legacy_detach,
403 .board_name = &atao_boards[0].name,
404 .offset = sizeof(struct atao_board),
405 .num_names = ARRAY_SIZE(atao_boards),
407 module_comedi_driver(ni_at_ao_driver);
409 MODULE_AUTHOR("Comedi http://www.comedi.org");
410 MODULE_DESCRIPTION("Comedi low-level driver");
411 MODULE_LICENSE("GPL");