staging: comedi: drivers: use comedi_dio_insn_config() for complex cases
[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 /* board egisters */
43 /* registers with _2_ are accessed when GRP2WR is set in CFG1 */
44
45 #define ATAO_SIZE 0x20
46
47 #define ATAO_2_DMATCCLR         0x00    /* W 16 */
48 #define ATAO_DIN                0x00    /* R 16 */
49 #define ATAO_DOUT               0x00    /* W 16 */
50
51 #define ATAO_CFG2               0x02    /* W 16 */
52 #define CALLD1  0x8000
53 #define CALLD0  0x4000
54 #define FFRTEN  0x2000
55 #define DAC2S8  0x1000
56 #define DAC2S6  0x0800
57 #define DAC2S4  0x0400
58 #define DAC2S2  0x0200
59 #define DAC2S0  0x0100
60 #define LDAC8           0x0080
61 #define LDAC6           0x0040
62 #define LDAC4           0x0020
63 #define LDAC2           0x0010
64 #define LDAC0           0x0008
65 #define PROMEN  0x0004
66 #define SCLK            0x0002
67 #define SDATA           0x0001
68
69 #define ATAO_2_INT1CLR          0x02    /* W 16 */
70
71 #define ATAO_CFG3               0x04    /* W 16 */
72 #define DMAMODE 0x0040
73 #define CLKOUT  0x0020
74 #define RCLKEN  0x0010
75 #define DOUTEN2 0x0008
76 #define DOUTEN1 0x0004
77 #define EN2_5V  0x0002
78 #define SCANEN  0x0001
79
80 #define ATAO_2_INT2CLR          0x04    /* W 16 */
81
82 #define ATAO_82C53_BASE         0x06    /* RW 8 */
83
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 */
88 #define CNTRSEL1        0x80
89 #define CNTRSEL0        0x40
90 #define RWSEL1  0x20
91 #define RWSEL0  0x10
92 #define MODESEL2        0x08
93 #define MODESEL1        0x04
94 #define MODESEL0        0x02
95 #define BCDSEL  0x01
96   /* read-back command */
97 #define COUNT           0x20
98 #define STATUS  0x10
99 #define CNTR3           0x08
100 #define CNTR2           0x04
101 #define CNTR1           0x02
102   /* status */
103 #define OUT             0x80
104 #define _NULL           0x40
105 #define RW1             0x20
106 #define RW0             0x10
107 #define MODE2           0x08
108 #define MODE1           0x04
109 #define MODE0           0x02
110 #define BCD             0x01
111
112 #define ATAO_2_RTSISHFT         0x06    /* W 8 */
113 #define RSI             0x01
114
115 #define ATAO_2_RTSISTRB         0x07    /* W 8 */
116
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
128 #define DMARQ           0x0020
129 #define DMAEN           0x0010
130 #define CH_mask 0x000f
131 #define ATAO_STATUS             0x0a    /* R 16 */
132 #define FH              0x0040
133 #define FE              0x0020
134 #define FF              0x0010
135 #define INT2            0x0008
136 #define INT1            0x0004
137 #define TCINT           0x0002
138 #define PROMOUT 0x0001
139
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 */
143
144 /*
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.
148  */
149 struct atao_board {
150         const char *name;
151         int n_ao_chans;
152 };
153
154 struct atao_private {
155
156         unsigned short cfg1;
157         unsigned short cfg2;
158         unsigned short cfg3;
159
160         /* Used for AO readback */
161         unsigned int ao_readback[10];
162 };
163
164 static void atao_reset(struct comedi_device *dev)
165 {
166         struct atao_private *devpriv = dev->private;
167
168         /* This is the reset sequence described in the manual */
169
170         devpriv->cfg1 = 0;
171         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
172
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);
176
177         devpriv->cfg2 = 0;
178         outw(devpriv->cfg2, dev->iobase + ATAO_CFG2);
179
180         devpriv->cfg3 = 0;
181         outw(devpriv->cfg3, dev->iobase + ATAO_CFG3);
182
183         inw(dev->iobase + ATAO_FIFO_CLEAR);
184
185         devpriv->cfg1 |= GRP2WR;
186         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
187
188         outw(0, dev->iobase + ATAO_2_INT1CLR);
189         outw(0, dev->iobase + ATAO_2_INT2CLR);
190         outw(0, dev->iobase + ATAO_2_DMATCCLR);
191
192         devpriv->cfg1 &= ~GRP2WR;
193         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
194 }
195
196 static int atao_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
197                          struct comedi_insn *insn, unsigned int *data)
198 {
199         struct atao_private *devpriv = dev->private;
200         int i;
201         int chan = CR_CHAN(insn->chanspec);
202         short bits;
203
204         for (i = 0; i < insn->n; i++) {
205                 bits = data[i] - 0x800;
206                 if (chan == 0) {
207                         devpriv->cfg1 |= GRP2WR;
208                         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
209                 }
210                 outw(bits, dev->iobase + ATAO_DACn(chan));
211                 if (chan == 0) {
212                         devpriv->cfg1 &= ~GRP2WR;
213                         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1);
214                 }
215                 devpriv->ao_readback[chan] = data[i];
216         }
217
218         return i;
219 }
220
221 static int atao_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
222                          struct comedi_insn *insn, unsigned int *data)
223 {
224         struct atao_private *devpriv = dev->private;
225         int i;
226         int chan = CR_CHAN(insn->chanspec);
227
228         for (i = 0; i < insn->n; i++)
229                 data[i] = devpriv->ao_readback[chan];
230
231         return i;
232 }
233
234 static int atao_dio_insn_bits(struct comedi_device *dev,
235                               struct comedi_subdevice *s,
236                               struct comedi_insn *insn, unsigned int *data)
237 {
238         if (data[0]) {
239                 s->state &= ~data[0];
240                 s->state |= data[0] & data[1];
241                 outw(s->state, dev->iobase + ATAO_DOUT);
242         }
243
244         data[1] = inw(dev->iobase + ATAO_DIN);
245
246         return insn->n;
247 }
248
249 static int atao_dio_insn_config(struct comedi_device *dev,
250                                 struct comedi_subdevice *s,
251                                 struct comedi_insn *insn,
252                                 unsigned int *data)
253 {
254         struct atao_private *devpriv = dev->private;
255         unsigned int chan = CR_CHAN(insn->chanspec);
256         unsigned int mask;
257         int ret;
258
259         if (chan < 4)
260                 mask = 0x0f;
261         else
262                 mask = 0xf0;
263
264         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
265         if (ret)
266                 return ret;
267
268         if (s->io_bits & 0x0f)
269                 devpriv->cfg3 |= DOUTEN1;
270         else
271                 devpriv->cfg3 &= ~DOUTEN1;
272         if (s->io_bits & 0xf0)
273                 devpriv->cfg3 |= DOUTEN2;
274         else
275                 devpriv->cfg3 &= ~DOUTEN2;
276
277         outw(devpriv->cfg3, dev->iobase + ATAO_CFG3);
278
279         return insn->n;
280 }
281
282 /*
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.
287  */
288 static int atao_calib_insn_read(struct comedi_device *dev,
289                                 struct comedi_subdevice *s,
290                                 struct comedi_insn *insn, unsigned int *data)
291 {
292         int i;
293         for (i = 0; i < insn->n; i++)
294                 data[i] = 0;    /* XXX */
295         return insn->n;
296 }
297
298 static int atao_calib_insn_write(struct comedi_device *dev,
299                                  struct comedi_subdevice *s,
300                                  struct comedi_insn *insn, unsigned int *data)
301 {
302         struct atao_private *devpriv = dev->private;
303         unsigned int bitstring, bit;
304         unsigned int chan = CR_CHAN(insn->chanspec);
305
306         bitstring = ((chan & 0x7) << 8) | (data[insn->n - 1] & 0xff);
307
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);
313         }
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);
318
319         return insn->n;
320 }
321
322 static int atao_attach(struct comedi_device *dev, struct comedi_devconfig *it)
323 {
324         const struct atao_board *board = comedi_board(dev);
325         struct atao_private *devpriv;
326         struct comedi_subdevice *s;
327         int ao_unipolar;
328         int ret;
329
330         ao_unipolar = it->options[3];
331
332         ret = comedi_request_region(dev, it->options[0], ATAO_SIZE);
333         if (ret)
334                 return ret;
335
336         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
337         if (!devpriv)
338                 return -ENOMEM;
339
340         ret = comedi_alloc_subdevices(dev, 4);
341         if (ret)
342                 return ret;
343
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;
350         if (ao_unipolar)
351                 s->range_table = &range_unipolar10;
352         else
353                 s->range_table = &range_bipolar10;
354         s->insn_write = &atao_ao_winsn;
355         s->insn_read = &atao_ao_rinsn;
356
357         s = &dev->subdevices[1];
358         /* digital i/o subdevice */
359         s->type = COMEDI_SUBD_DIO;
360         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
361         s->n_chan = 8;
362         s->maxdata = 1;
363         s->range_table = &range_digital;
364         s->insn_bits = atao_dio_insn_bits;
365         s->insn_config = atao_dio_insn_config;
366
367         s = &dev->subdevices[2];
368         /* caldac subdevice */
369         s->type = COMEDI_SUBD_CALIB;
370         s->subdev_flags = SDF_WRITABLE | SDF_INTERNAL;
371         s->n_chan = 21;
372         s->maxdata = 0xff;
373         s->insn_read = atao_calib_insn_read;
374         s->insn_write = atao_calib_insn_write;
375
376         s = &dev->subdevices[3];
377         /* eeprom subdevice */
378         /* s->type=COMEDI_SUBD_EEPROM; */
379         s->type = COMEDI_SUBD_UNUSED;
380
381         atao_reset(dev);
382
383         printk(KERN_INFO "\n");
384
385         return 0;
386 }
387
388 static const struct atao_board atao_boards[] = {
389         {
390                 .name           = "ai-ao-6",
391                 .n_ao_chans     = 6,
392         }, {
393                 .name           = "ai-ao-10",
394                 .n_ao_chans     = 10,
395         },
396 };
397
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),
406 };
407 module_comedi_driver(ni_at_ao_driver);
408
409 MODULE_AUTHOR("Comedi http://www.comedi.org");
410 MODULE_DESCRIPTION("Comedi low-level driver");
411 MODULE_LICENSE("GPL");