b7d31f7144f2fcc13c89b3c7c3934c2af38e60de
[firefly-linux-kernel-4.4.55.git] / drivers / staging / 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 /*
19 Driver: ni_at_ao
20 Description: National Instruments AT-AO-6/10
21 Devices: [National Instruments] AT-AO-6 (at-ao-6), AT-AO-10 (at-ao-10)
22 Status: should work
23 Author: ds
24 Updated: Sun Dec 26 12:26:28 EST 2004
25
26 Configuration options:
27   [0] - I/O port base address
28   [1] - IRQ (unused)
29   [2] - DMA (unused)
30   [3] - analog output range, set by jumpers on hardware (0 for -10 to 10V
31         bipolar, 1 for 0V to 10V unipolar)
32
33 */
34 /*
35  * Register-level programming information can be found in NI
36  * document 320379.pdf.
37  */
38
39 #include <linux/module.h>
40 #include "../comedidev.h"
41
42 /*
43  * Register map
44  */
45 #define ATAO_DIN                0x00    /* R 16 */
46 #define ATAO_DOUT               0x00    /* W 16 */
47 #define ATAO_CFG2               0x02    /* W 16 */
48 #define CALLD1                  (1 << 15)
49 #define CALLD0                  (1 << 14)
50 #define FFRTEN                  (1 << 13)
51 #define DAC2S8                  (1 << 12)
52 #define DAC2S6                  (1 << 11)
53 #define DAC2S4                  (1 << 10)
54 #define DAC2S2                  (1 << 9)
55 #define DAC2S0                  (1 << 8)
56 #define LDAC8                   (1 << 7)
57 #define LDAC6                   (1 << 6)
58 #define LDAC4                   (1 << 5)
59 #define LDAC2                   (1 << 4)
60 #define LDAC0                   (1 << 3)
61 #define PROMEN                  (1 << 2)
62 #define SCLK                    (1 << 1)
63 #define SDATA                   (1 << 0)
64 #define ATAO_CFG3               0x04    /* W 16 */
65 #define DMAMODE                 (1 << 6)
66 #define CLKOUT                  (1 << 5)
67 #define RCLKEN                  (1 << 4)
68 #define DOUTEN2                 (1 << 3)
69 #define DOUTEN1                 (1 << 2)
70 #define EN2_5V                  (1 << 1)
71 #define SCANEN                  (1 << 0)
72 #define ATAO_82C53_BASE         0x06    /* RW 8 */
73 #define ATAO_82C53_CNTR1        0x06    /* RW 8 */
74 #define ATAO_82C53_CNTR2        0x07    /* RW 8 */
75 #define ATAO_82C53_CNTR3        0x08    /* RW 8 */
76 #define ATAO_82C53_CNTRCMD      0x09    /* W 8 */
77 #define CNTRSEL1                (1 << 7)
78 #define CNTRSEL0                (1 << 6)
79 #define RWSEL1                  (1 << 5)
80 #define RWSEL0                  (1 << 4)
81 #define MODESEL2                (1 << 3)
82 #define MODESEL1                (1 << 2)
83 #define MODESEL0                (1 << 1)
84 #define BCDSEL                  (1 << 0)
85   /* read-back command */
86 #define COUNT                   (1 << 5)
87 #define STATUS                  (1 << 4)
88 #define CNTR3                   (1 << 3)
89 #define CNTR2                   (1 << 2)
90 #define CNTR1                   (1 << 1)
91   /* status */
92 #define OUT                     (1 << 7)
93 #define _NULL                   (1 << 6)
94 #define RW1                     (1 << 5)
95 #define RW0                     (1 << 4)
96 #define MODE2                   (1 << 3)
97 #define MODE1                   (1 << 2)
98 #define MODE0                   (1 << 1)
99 #define BCD                     (1 << 0)
100 #define ATAO_CFG1               0x0a    /* W 16 */
101 #define EXTINT2EN               (1 << 15)
102 #define EXTINT1EN               (1 << 14)
103 #define CNTINT2EN               (1 << 13)
104 #define CNTINT1EN               (1 << 12)
105 #define TCINTEN                 (1 << 11)
106 #define CNT1SRC                 (1 << 10)
107 #define CNT2SRC                 (1 << 9)
108 #define FIFOEN                  (1 << 8)
109 #define GRP2WR                  (1 << 7)
110 #define EXTUPDEN                (1 << 6)
111 #define DMARQ                   (1 << 5)
112 #define DMAEN                   (1 << 4)
113 #define CH_mask                 (0xf << 0)
114 #define ATAO_STATUS             0x0a    /* R 16 */
115 #define FH                      (1 << 6)
116 #define FE                      (1 << 5)
117 #define FF                      (1 << 4)
118 #define INT2                    (1 << 3)
119 #define INT1                    (1 << 2)
120 #define TCINT                   (1 << 1)
121 #define PROMOUT                 (1 << 0)
122 #define ATAO_FIFO_WRITE         0x0c    /* W 16 */
123 #define ATAO_FIFO_CLEAR         0x0c    /* R 16 */
124 #define ATAO_DACn(x)            (0x0c + ((x) * 2))      /* W */
125
126 /* registers with _2_ are accessed when GRP2WR is set in CFG1 */
127
128 #define ATAO_2_DMATCCLR         0x00    /* W 16 */
129 #define ATAO_2_INT1CLR          0x02    /* W 16 */
130 #define ATAO_2_INT2CLR          0x04    /* W 16 */
131 #define ATAO_2_RTSISHFT         0x06    /* W 8 */
132 #define ATAO_RTSISHFT_RSI       (1 << 0)
133 #define ATAO_2_RTSISTRB         0x07    /* W 8 */
134
135 /*
136  * Board descriptions for two imaginary boards.  Describing the
137  * boards in this way is optional, and completely driver-dependent.
138  * Some drivers use arrays such as this, other do not.
139  */
140 struct atao_board {
141         const char *name;
142         int n_ao_chans;
143 };
144
145 struct atao_private {
146
147         unsigned short cfg1;
148         unsigned short cfg2;
149         unsigned short cfg3;
150
151         /* Used for AO readback */
152         unsigned int ao_readback[10];
153 };
154
155 static void atao_reset(struct comedi_device *dev)
156 {
157         struct atao_private *devpriv = dev->private;
158
159         /* This is the reset sequence described in the manual */
160
161         devpriv->cfg1 = 0;
162         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
163
164         outb(RWSEL0 | MODESEL2, dev->iobase + ATAO_82C53_CNTRCMD);
165         outb(0x03, dev->iobase + ATAO_82C53_CNTR1);
166         outb(CNTRSEL0 | RWSEL0 | MODESEL2, dev->iobase + ATAO_82C53_CNTRCMD);
167
168         devpriv->cfg2 = 0;
169         outw(devpriv->cfg2, dev->iobase + ATAO_CFG2);
170
171         devpriv->cfg3 = 0;
172         outw(devpriv->cfg3, dev->iobase + ATAO_CFG3);
173
174         inw(dev->iobase + ATAO_FIFO_CLEAR);
175
176         devpriv->cfg1 |= GRP2WR;
177         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
178
179         outw(0, dev->iobase + ATAO_2_INT1CLR);
180         outw(0, dev->iobase + ATAO_2_INT2CLR);
181         outw(0, dev->iobase + ATAO_2_DMATCCLR);
182
183         devpriv->cfg1 &= ~GRP2WR;
184         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
185 }
186
187 static int atao_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
188                          struct comedi_insn *insn, unsigned int *data)
189 {
190         struct atao_private *devpriv = dev->private;
191         int i;
192         int chan = CR_CHAN(insn->chanspec);
193         short bits;
194
195         for (i = 0; i < insn->n; i++) {
196                 bits = data[i] - 0x800;
197                 if (chan == 0) {
198                         devpriv->cfg1 |= GRP2WR;
199                         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
200                 }
201                 outw(bits, dev->iobase + ATAO_DACn(chan));
202                 if (chan == 0) {
203                         devpriv->cfg1 &= ~GRP2WR;
204                         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
205                 }
206                 devpriv->ao_readback[chan] = data[i];
207         }
208
209         return i;
210 }
211
212 static int atao_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
213                          struct comedi_insn *insn, unsigned int *data)
214 {
215         struct atao_private *devpriv = dev->private;
216         int i;
217         int chan = CR_CHAN(insn->chanspec);
218
219         for (i = 0; i < insn->n; i++)
220                 data[i] = devpriv->ao_readback[chan];
221
222         return i;
223 }
224
225 static int atao_dio_insn_bits(struct comedi_device *dev,
226                               struct comedi_subdevice *s,
227                               struct comedi_insn *insn,
228                               unsigned int *data)
229 {
230         if (comedi_dio_update_state(s, data))
231                 outw(s->state, dev->iobase + ATAO_DOUT);
232
233         data[1] = inw(dev->iobase + ATAO_DIN);
234
235         return insn->n;
236 }
237
238 static int atao_dio_insn_config(struct comedi_device *dev,
239                                 struct comedi_subdevice *s,
240                                 struct comedi_insn *insn,
241                                 unsigned int *data)
242 {
243         struct atao_private *devpriv = dev->private;
244         unsigned int chan = CR_CHAN(insn->chanspec);
245         unsigned int mask;
246         int ret;
247
248         if (chan < 4)
249                 mask = 0x0f;
250         else
251                 mask = 0xf0;
252
253         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
254         if (ret)
255                 return ret;
256
257         if (s->io_bits & 0x0f)
258                 devpriv->cfg3 |= DOUTEN1;
259         else
260                 devpriv->cfg3 &= ~DOUTEN1;
261         if (s->io_bits & 0xf0)
262                 devpriv->cfg3 |= DOUTEN2;
263         else
264                 devpriv->cfg3 &= ~DOUTEN2;
265
266         outw(devpriv->cfg3, dev->iobase + ATAO_CFG3);
267
268         return insn->n;
269 }
270
271 /*
272  * Figure 2-1 in the manual shows 3 chips labeled DAC8800, which
273  * are 8-channel 8-bit DACs.  These are most likely the calibration
274  * DACs.  It is not explicitly stated in the manual how to access
275  * the caldacs, but we can guess.
276  */
277 static int atao_calib_insn_read(struct comedi_device *dev,
278                                 struct comedi_subdevice *s,
279                                 struct comedi_insn *insn, unsigned int *data)
280 {
281         int i;
282         for (i = 0; i < insn->n; i++)
283                 data[i] = 0;    /* XXX */
284         return insn->n;
285 }
286
287 static int atao_calib_insn_write(struct comedi_device *dev,
288                                  struct comedi_subdevice *s,
289                                  struct comedi_insn *insn, unsigned int *data)
290 {
291         struct atao_private *devpriv = dev->private;
292         unsigned int bitstring, bit;
293         unsigned int chan = CR_CHAN(insn->chanspec);
294
295         bitstring = ((chan & 0x7) << 8) | (data[insn->n - 1] & 0xff);
296
297         for (bit = 1 << (11 - 1); bit; bit >>= 1) {
298                 outw(devpriv->cfg2 | ((bit & bitstring) ? SDATA : 0),
299                      dev->iobase + ATAO_CFG2);
300                 outw(devpriv->cfg2 | SCLK | ((bit & bitstring) ? SDATA : 0),
301                      dev->iobase + ATAO_CFG2);
302         }
303         /* strobe the appropriate caldac */
304         outw(devpriv->cfg2 | (((chan >> 3) + 1) << 14),
305              dev->iobase + ATAO_CFG2);
306         outw(devpriv->cfg2, dev->iobase + ATAO_CFG2);
307
308         return insn->n;
309 }
310
311 static int atao_attach(struct comedi_device *dev, struct comedi_devconfig *it)
312 {
313         const struct atao_board *board = comedi_board(dev);
314         struct atao_private *devpriv;
315         struct comedi_subdevice *s;
316         int ao_unipolar;
317         int ret;
318
319         ao_unipolar = it->options[3];
320
321         ret = comedi_request_region(dev, it->options[0], 0x20);
322         if (ret)
323                 return ret;
324
325         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
326         if (!devpriv)
327                 return -ENOMEM;
328
329         ret = comedi_alloc_subdevices(dev, 4);
330         if (ret)
331                 return ret;
332
333         s = &dev->subdevices[0];
334         /* analog output subdevice */
335         s->type = COMEDI_SUBD_AO;
336         s->subdev_flags = SDF_WRITABLE;
337         s->n_chan = board->n_ao_chans;
338         s->maxdata = (1 << 12) - 1;
339         if (ao_unipolar)
340                 s->range_table = &range_unipolar10;
341         else
342                 s->range_table = &range_bipolar10;
343         s->insn_write = &atao_ao_winsn;
344         s->insn_read = &atao_ao_rinsn;
345
346         s = &dev->subdevices[1];
347         /* digital i/o subdevice */
348         s->type = COMEDI_SUBD_DIO;
349         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
350         s->n_chan = 8;
351         s->maxdata = 1;
352         s->range_table = &range_digital;
353         s->insn_bits = atao_dio_insn_bits;
354         s->insn_config = atao_dio_insn_config;
355
356         s = &dev->subdevices[2];
357         /* caldac subdevice */
358         s->type = COMEDI_SUBD_CALIB;
359         s->subdev_flags = SDF_WRITABLE | SDF_INTERNAL;
360         s->n_chan = 21;
361         s->maxdata = 0xff;
362         s->insn_read = atao_calib_insn_read;
363         s->insn_write = atao_calib_insn_write;
364
365         s = &dev->subdevices[3];
366         /* eeprom subdevice */
367         /* s->type=COMEDI_SUBD_EEPROM; */
368         s->type = COMEDI_SUBD_UNUSED;
369
370         atao_reset(dev);
371
372         printk(KERN_INFO "\n");
373
374         return 0;
375 }
376
377 static const struct atao_board atao_boards[] = {
378         {
379                 .name           = "ai-ao-6",
380                 .n_ao_chans     = 6,
381         }, {
382                 .name           = "ai-ao-10",
383                 .n_ao_chans     = 10,
384         },
385 };
386
387 static struct comedi_driver ni_at_ao_driver = {
388         .driver_name    = "ni_at_ao",
389         .module         = THIS_MODULE,
390         .attach         = atao_attach,
391         .detach         = comedi_legacy_detach,
392         .board_name     = &atao_boards[0].name,
393         .offset         = sizeof(struct atao_board),
394         .num_names      = ARRAY_SIZE(atao_boards),
395 };
396 module_comedi_driver(ni_at_ao_driver);
397
398 MODULE_AUTHOR("Comedi http://www.comedi.org");
399 MODULE_DESCRIPTION("Comedi low-level driver");
400 MODULE_LICENSE("GPL");