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