Added some ack'ing of b interrupts, and do acks before handling
[comedi.git] / comedi / drivers / ni_at_ao.c
1 /*
2     comedi/drivers/ni_at_ao.c
3     Driver for NI AT-AO-6/10 boards
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000,2002 David A. Schleef <ds@schleef.org>
7
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.
12
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.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23 /*
24 Driver: ni_at_ao.o
25 Description: National Instruments AT-AO-6/10
26 Devices: [National Instruments] AT-AO-6 (at-ao-6), AT-AO-10 (at-ao-10)
27 Status: should work
28 Author: ds
29 Updated: Sun Dec 26 12:26:28 EST 2004
30
31 Configuration options:
32   [0] - I/O port base address
33   [1] - IRQ (unused)
34   [2] - DMA (unused)
35   [3] - analog output range, set by jumpers on hardware (0 for -10 to 10V bipolar, 1 for 0V to 10V unipolar)
36
37 */
38 /*
39  * Register-level programming information can be found in NI
40  * document 320379.pdf.
41  */
42
43 #include <linux/comedidev.h>
44
45 #include <linux/ioport.h>
46
47
48 /* board egisters */
49 /* registers with _2_ are accessed when GRP2WR is set in CFG1 */
50
51 #define ATAO_SIZE 0x20
52
53 #define ATAO_2_DMATCCLR         0x00    /* W 16 */
54 #define ATAO_DIN                0x00    /* R 16 */
55 #define ATAO_DOUT               0x00    /* W 16 */
56
57 #define ATAO_CFG2               0x02    /* W 16 */
58   #define CALLD1        0x8000
59   #define CALLD0        0x4000
60   #define FFRTEN        0x2000
61   #define DAC2S8        0x1000
62   #define DAC2S6        0x0800
63   #define DAC2S4        0x0400
64   #define DAC2S2        0x0200
65   #define DAC2S0        0x0100
66   #define LDAC8         0x0080
67   #define LDAC6         0x0040
68   #define LDAC4         0x0020
69   #define LDAC2         0x0010
70   #define LDAC0         0x0008
71   #define PROMEN        0x0004
72   #define SCLK          0x0002
73   #define SDATA         0x0001
74
75 #define ATAO_2_INT1CLR          0x02    /* W 16 */
76
77 #define ATAO_CFG3               0x04    /* W 16 */
78   #define DMAMODE       0x0040
79   #define CLKOUT        0x0020
80   #define RCLKEN        0x0010
81   #define DOUTEN2       0x0008
82   #define DOUTEN1       0x0004
83   #define EN2_5V        0x0002
84   #define SCANEN        0x0001
85
86 #define ATAO_2_INT2CLR          0x04    /* W 16 */
87
88 #define ATAO_82C53_BASE         0x06    /* RW 8 */
89
90 #define ATAO_82C53_CNTR1        0x06    /* RW 8 */
91 #define ATAO_82C53_CNTR2        0x07    /* RW 8 */
92 #define ATAO_82C53_CNTR3        0x08    /* RW 8 */
93 #define ATAO_82C53_CNTRCMD      0x09    /* W 8 */
94   #define CNTRSEL1      0x80
95   #define CNTRSEL0      0x40
96   #define RWSEL1        0x20
97   #define RWSEL0        0x10
98   #define MODESEL2      0x08
99   #define MODESEL1      0x04
100   #define MODESEL0      0x02
101   #define BCDSEL        0x01
102   /* read-back command */
103   #define COUNT         0x20
104   #define STATUS        0x10
105   #define CNTR3         0x08
106   #define CNTR2         0x04
107   #define CNTR1         0x02
108   /* status */
109   #define OUT           0x80
110   #define _NULL         0x40
111   #define RW1           0x20
112   #define RW0           0x10
113   #define MODE2         0x08
114   #define MODE1         0x04
115   #define MODE0         0x02
116   #define BCD           0x01
117
118 #define ATAO_2_RTSISHFT         0x06    /* W 8 */
119   #define RSI           0x01
120
121 #define ATAO_2_RTSISTRB         0x07    /* W 8 */
122
123 #define ATAO_CFG1               0x0a    /* W 16 */
124   #define EXTINT2EN     0x8000
125   #define EXTINT1EN     0x4000
126   #define CNTINT2EN     0x2000
127   #define CNTINT1EN     0x1000
128   #define TCINTEN       0x0800
129   #define CNT1SRC       0x0400
130   #define CNT2SRC       0x0200
131   #define FIFOEN        0x0100
132   #define GRP2WR        0x0080
133   #define EXTUPDEN      0x0040
134   #define DMARQ         0x0020
135   #define DMAEN         0x0010
136   #define CH_mask       0x000f
137 #define ATAO_STATUS             0x0a    /* R 16 */
138   #define FH            0x0040
139   #define FE            0x0020
140   #define FF            0x0010
141   #define INT2          0x0008
142   #define INT1          0x0004
143   #define TCINT         0x0002
144   #define PROMOUT       0x0001
145
146 #define ATAO_FIFO_WRITE         0x0c    /* W 16 */
147 #define ATAO_FIFO_CLEAR         0x0c    /* R 16 */
148 #define ATAO_DACn(x)            (0x0c + 2*(x))  /* W */
149
150 /*
151  * Board descriptions for two imaginary boards.  Describing the
152  * boards in this way is optional, and completely driver-dependent.
153  * Some drivers use arrays such as this, other do not.
154  */
155 typedef struct atao_board_struct{
156         char *name;
157         int n_ao_chans;
158 }atao_board;
159 static atao_board atao_boards[] = {
160         {
161         name:           "ai-ao-6",
162         n_ao_chans:     6,
163         },
164         {
165         name:           "ai-ao-10",
166         n_ao_chans:     10,
167         },
168 };
169 #define thisboard ((atao_board *)dev->board_ptr)
170
171 typedef struct{
172         unsigned short cfg1;
173         unsigned short cfg2;
174         unsigned short cfg3;
175
176         /* Used for AO readback */
177         lsampl_t ao_readback[10];
178 }atao_private;
179 #define devpriv ((atao_private *)dev->private)
180
181 static int atao_attach(comedi_device *dev,comedi_devconfig *it);
182 static int atao_detach(comedi_device *dev);
183 static comedi_driver driver_atao={
184         driver_name:    "ni_at_ao",
185         module:         THIS_MODULE,
186         attach:         atao_attach,
187         detach:         atao_detach,
188         board_name:     (const char**)atao_boards,
189         offset:         sizeof(atao_board),
190         num_names:      sizeof(atao_boards) / sizeof(atao_board),
191 };
192 COMEDI_INITCLEANUP(driver_atao);
193
194 static void atao_reset(comedi_device *dev);
195
196 static int atao_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
197 static int atao_ao_rinsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
198 static int atao_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
199         comedi_insn *insn,lsampl_t *data);
200 static int atao_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
201         comedi_insn *insn,lsampl_t *data);
202 static int atao_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
203         comedi_insn *insn,lsampl_t *data);
204 static int atao_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
205         comedi_insn *insn,lsampl_t *data);
206
207 static int atao_attach(comedi_device *dev,comedi_devconfig *it)
208 {
209         comedi_subdevice *s;
210         unsigned long iobase;
211         int ao_unipolar;
212
213         iobase = it->options[0];
214         if(iobase==0)iobase = 0x1c0;
215         ao_unipolar = it->options[3];
216
217         printk("comedi%d: ni_at_ao: 0x%04lx",dev->minor,iobase);
218
219         if(!request_region(iobase, ATAO_SIZE, "ni_at_ao")){
220                 printk(" I/O port conflict\n");
221                 return -EIO;
222         }
223         dev->iobase = iobase;
224
225         //dev->board_ptr = atao_probe(dev);
226
227         dev->board_name = thisboard->name;
228
229         if(alloc_private(dev,sizeof(atao_private))<0)
230                 return -ENOMEM;
231
232         if(alloc_subdevices(dev, 4)<0)
233                 return -ENOMEM;
234
235         s=dev->subdevices+0;
236         /* analog output subdevice */
237         s->type=COMEDI_SUBD_AO;
238         s->subdev_flags=SDF_WRITABLE;
239         s->n_chan=thisboard->n_ao_chans;
240         s->maxdata=(1<<12)-1;
241         if(ao_unipolar)
242                 s->range_table=&range_unipolar10;
243         else
244                 s->range_table=&range_bipolar10;
245         s->insn_write = &atao_ao_winsn;
246         s->insn_read = &atao_ao_rinsn;
247
248         s=dev->subdevices+1;
249         /* digital i/o subdevice */
250         s->type=COMEDI_SUBD_DIO;
251         s->subdev_flags=SDF_READABLE|SDF_WRITABLE;
252         s->n_chan=8;
253         s->maxdata=1;
254         s->range_table=&range_digital;
255         s->insn_bits = atao_dio_insn_bits;
256         s->insn_config = atao_dio_insn_config;
257
258         s=dev->subdevices+2;
259         /* caldac subdevice */
260         s->type=COMEDI_SUBD_CALIB;
261         s->subdev_flags = SDF_WRITABLE|SDF_INTERNAL;
262         s->n_chan = 21;
263         s->maxdata = 0xff;
264         s->insn_read = atao_calib_insn_read;
265         s->insn_write = atao_calib_insn_write;
266
267         s=dev->subdevices+3;
268         /* eeprom subdevice */
269         //s->type=COMEDI_SUBD_EEPROM;
270         s->type=COMEDI_SUBD_UNUSED;
271
272         atao_reset(dev);
273
274         printk("\n");
275
276         return 0;
277 }
278
279
280 static int atao_detach(comedi_device *dev)
281 {
282         printk("comedi%d: atao: remove\n",dev->minor);
283
284         if(dev->iobase)
285                 release_region(dev->iobase, ATAO_SIZE);
286
287         return 0;
288 }
289
290
291 static void atao_reset(comedi_device *dev)
292 {
293         /* This is the reset sequence described in the manual */
294
295         devpriv->cfg1 = 0;
296         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
297
298         outb(RWSEL0 | MODESEL2, dev->iobase + ATAO_82C53_CNTRCMD);
299         outb(0x03, dev->iobase + ATAO_82C53_CNTR1);
300         outb(CNTRSEL0 | RWSEL0 | MODESEL2, dev->iobase + ATAO_82C53_CNTRCMD);
301
302         devpriv->cfg2 = 0;
303         outw(devpriv->cfg2, dev->iobase + ATAO_CFG2);
304
305         devpriv->cfg3 = 0;
306         outw(devpriv->cfg3, dev->iobase + ATAO_CFG3);
307
308         inw(dev->iobase + ATAO_FIFO_CLEAR);
309
310         devpriv->cfg1 |= GRP2WR;
311         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
312
313         outw(0, dev->iobase + ATAO_2_INT1CLR);
314         outw(0, dev->iobase + ATAO_2_INT2CLR);
315         outw(0, dev->iobase + ATAO_2_DMATCCLR);
316
317         devpriv->cfg1 &= ~GRP2WR;
318         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
319 }
320
321
322 static int atao_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
323 {
324         int i;
325         int chan = CR_CHAN(insn->chanspec);
326         short bits;
327
328         for(i=0;i<insn->n;i++){
329                 bits = data[i] - 0x800;
330                 if(chan == 0)
331                 {
332                         devpriv->cfg1 |= GRP2WR;
333                         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
334                 }
335                 outw(bits, dev->iobase + ATAO_DACn(chan));
336                 if(chan == 0)
337                 {
338                         devpriv->cfg1 &= ~GRP2WR;
339                         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
340                 }
341                 devpriv->ao_readback[chan] = data[i];
342         }
343
344         return i;
345 }
346
347 static int atao_ao_rinsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
348 {
349         int i;
350         int chan = CR_CHAN(insn->chanspec);
351
352         for(i=0;i<insn->n;i++)
353                 data[i] = devpriv->ao_readback[chan];
354
355         return i;
356 }
357
358 static int atao_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
359         comedi_insn *insn,lsampl_t *data)
360 {
361         if(insn->n!=2)return -EINVAL;
362
363         if(data[0]){
364                 s->state &= ~data[0];
365                 s->state |= data[0]&data[1];
366                 outw(s->state,dev->iobase + ATAO_DOUT);
367         }
368
369         data[1]=inw(dev->iobase + ATAO_DIN);
370
371         return 2;
372 }
373
374 static int atao_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
375         comedi_insn *insn,lsampl_t *data)
376 {
377         int chan=CR_CHAN(insn->chanspec);
378         unsigned int mask, bit;
379
380         /* The input or output configuration of each digital line is
381          * configured by a special insn_config instruction.  chanspec
382          * contains the channel to be changed, and data[0] contains the
383          * value COMEDI_INPUT or COMEDI_OUTPUT. */
384
385         mask = (chan < 4) ? 0x0f : 0xf0;
386         bit = (chan < 4) ? DOUTEN1 : DOUTEN2;
387
388         switch(data[0])
389         {
390         case INSN_CONFIG_DIO_OUTPUT:
391                 s->io_bits |= mask;
392                 devpriv->cfg3 |= bit;
393                 break;
394         case INSN_CONFIG_DIO_INPUT:
395                 s->io_bits &= ~mask;
396                 devpriv->cfg3 &= ~bit;
397                 break;
398         case INSN_CONFIG_DIO_QUERY:
399                 data[1] = (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
400                 return insn->n;
401                 break;
402         default:
403                 return -EINVAL;
404                 break;
405         }
406
407         outw(devpriv->cfg3, dev->iobase + ATAO_CFG3);
408
409         return 1;
410 }
411
412
413 /*
414  * Figure 2-1 in the manual shows 3 chips labeled DAC8800, which
415  * are 8-channel 8-bit DACs.  These are most likely the calibration
416  * DACs.  It is not explicitly stated in the manual how to access
417  * the caldacs, but we can guess.
418  */
419 static int atao_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
420         comedi_insn *insn,lsampl_t *data)
421 {
422         int i;
423         for(i=0;i<insn->n;i++){
424                 data[i]=0; /* XXX */
425         }
426         return insn->n;
427 }
428
429 static int atao_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
430         comedi_insn *insn,lsampl_t *data)
431 {
432         unsigned int bitstring, bit;
433         unsigned int chan = CR_CHAN(insn->chanspec);
434
435         bitstring = ((chan&0x7)<<8)|(data[insn->n-1]&0xff);
436
437         for(bit=1<<(11-1);bit;bit>>=1){
438                 outw(devpriv->cfg2 | ((bit&bitstring)?SDATA:0), dev->iobase + ATAO_CFG2);
439                 outw(devpriv->cfg2 | SCLK | ((bit&bitstring)?SDATA:0), dev->iobase + ATAO_CFG2);
440         }
441         /* strobe the appropriate caldac */
442         outw(devpriv->cfg2 | (((chan>>3) + 1)<<14), dev->iobase + ATAO_CFG2);
443         outw(devpriv->cfg2, dev->iobase + ATAO_CFG2);
444
445         return insn->n;
446 }
447
448