staging: comedi: ni_pcidio: remove custom DPRINTK macro
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / drivers / ni_at_ao.c
1 /*
2  * 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 /*
20  * Driver: ni_at_ao
21  * Description: National Instruments AT-AO-6/10
22  * Devices: (National Instruments) AT-AO-6 [at-ao-6]
23  *          (National Instruments) AT-AO-10 [at-ao-10]
24  * Status: should work
25  * Author: David A. Schleef <ds@schleef.org>
26  * Updated: Sun Dec 26 12:26:28 EST 2004
27  *
28  * Configuration options:
29  *   [0] - I/O port base address
30  *   [1] - IRQ (unused)
31  *   [2] - DMA (unused)
32  *   [3] - analog output range, set by jumpers on hardware
33  *         0 for -10 to 10V bipolar
34  *         1 for 0V to 10V unipolar
35  */
36
37 #include <linux/module.h>
38
39 #include "../comedidev.h"
40
41 #include "8253.h"
42
43 /*
44  * Register map
45  *
46  * Register-level programming information can be found in NI
47  * document 320379.pdf.
48  */
49 #define ATAO_DIO_REG            0x00
50 #define ATAO_CFG2_REG           0x02
51 #define ATAO_CFG2_CALLD_NOP     (0 << 14)
52 #define ATAO_CFG2_CALLD(x)      ((((x) >> 3) + 1) << 14)
53 #define ATAO_CFG2_FFRTEN        (1 << 13)
54 #define ATAO_CFG2_DACS(x)       (1 << (((x) / 2) + 8))
55 #define ATAO_CFG2_LDAC(x)       (1 << (((x) / 2) + 3))
56 #define ATAO_CFG2_PROMEN        (1 << 2)
57 #define ATAO_CFG2_SCLK          (1 << 1)
58 #define ATAO_CFG2_SDATA         (1 << 0)
59 #define ATAO_CFG3_REG           0x04
60 #define ATAO_CFG3_DMAMODE       (1 << 6)
61 #define ATAO_CFG3_CLKOUT        (1 << 5)
62 #define ATAO_CFG3_RCLKEN        (1 << 4)
63 #define ATAO_CFG3_DOUTEN2       (1 << 3)
64 #define ATAO_CFG3_DOUTEN1       (1 << 2)
65 #define ATAO_CFG3_EN2_5V        (1 << 1)
66 #define ATAO_CFG3_SCANEN        (1 << 0)
67 #define ATAO_82C53_BASE         0x06
68 #define ATAO_CFG1_REG           0x0a
69 #define ATAO_CFG1_EXTINT2EN     (1 << 15)
70 #define ATAO_CFG1_EXTINT1EN     (1 << 14)
71 #define ATAO_CFG1_CNTINT2EN     (1 << 13)
72 #define ATAO_CFG1_CNTINT1EN     (1 << 12)
73 #define ATAO_CFG1_TCINTEN       (1 << 11)
74 #define ATAO_CFG1_CNT1SRC       (1 << 10)
75 #define ATAO_CFG1_CNT2SRC       (1 << 9)
76 #define ATAO_CFG1_FIFOEN        (1 << 8)
77 #define ATAO_CFG1_GRP2WR        (1 << 7)
78 #define ATAO_CFG1_EXTUPDEN      (1 << 6)
79 #define ATAO_CFG1_DMARQ         (1 << 5)
80 #define ATAO_CFG1_DMAEN         (1 << 4)
81 #define ATAO_CFG1_CH(x)         (((x) & 0xf) << 0)
82 #define ATAO_STATUS_REG         0x0a
83 #define ATAO_STATUS_FH          (1 << 6)
84 #define ATAO_STATUS_FE          (1 << 5)
85 #define ATAO_STATUS_FF          (1 << 4)
86 #define ATAO_STATUS_INT2        (1 << 3)
87 #define ATAO_STATUS_INT1        (1 << 2)
88 #define ATAO_STATUS_TCINT       (1 << 1)
89 #define ATAO_STATUS_PROMOUT     (1 << 0)
90 #define ATAO_FIFO_WRITE_REG     0x0c
91 #define ATAO_FIFO_CLEAR_REG     0x0c
92 #define ATAO_AO_REG(x)          (0x0c + ((x) * 2))
93
94 /* registers with _2_ are accessed when GRP2WR is set in CFG1 */
95 #define ATAO_2_DMATCCLR_REG     0x00
96 #define ATAO_2_INT1CLR_REG      0x02
97 #define ATAO_2_INT2CLR_REG      0x04
98 #define ATAO_2_RTSISHFT_REG     0x06
99 #define ATAO_2_RTSISHFT_RSI     (1 << 0)
100 #define ATAO_2_RTSISTRB_REG     0x07
101
102 struct atao_board {
103         const char *name;
104         int n_ao_chans;
105 };
106
107 static const struct atao_board atao_boards[] = {
108         {
109                 .name           = "at-ao-6",
110                 .n_ao_chans     = 6,
111         }, {
112                 .name           = "at-ao-10",
113                 .n_ao_chans     = 10,
114         },
115 };
116
117 struct atao_private {
118         unsigned short cfg1;
119         unsigned short cfg3;
120
121         /* Used for AO readback */
122         unsigned int ao_readback[10];
123
124         /* Used for caldac readback */
125         unsigned char caldac[21];
126 };
127
128 static void atao_select_reg_group(struct comedi_device *dev, int group)
129 {
130         struct atao_private *devpriv = dev->private;
131
132         if (group)
133                 devpriv->cfg1 |= ATAO_CFG1_GRP2WR;
134         else
135                 devpriv->cfg1 &= ~ATAO_CFG1_GRP2WR;
136         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1_REG);
137 }
138
139 static int atao_ao_insn_write(struct comedi_device *dev,
140                               struct comedi_subdevice *s,
141                               struct comedi_insn *insn,
142                               unsigned int *data)
143 {
144         struct atao_private *devpriv = dev->private;
145         unsigned int chan = CR_CHAN(insn->chanspec);
146         unsigned int val;
147         int i;
148
149         if (chan == 0)
150                 atao_select_reg_group(dev, 1);
151
152         for (i = 0; i < insn->n; i++) {
153                 val = data[i];
154                 devpriv->ao_readback[chan] = val;
155
156                 /* munge offset binary (unsigned) to two's complement */
157                 val = comedi_offset_munge(s, val);
158                 outw(val, dev->iobase + ATAO_AO_REG(chan));
159         }
160
161         if (chan == 0)
162                 atao_select_reg_group(dev, 0);
163
164         return insn->n;
165 }
166
167 static int atao_ao_insn_read(struct comedi_device *dev,
168                              struct comedi_subdevice *s,
169                              struct comedi_insn *insn,
170                              unsigned int *data)
171 {
172         struct atao_private *devpriv = dev->private;
173         unsigned int chan = CR_CHAN(insn->chanspec);
174         int i;
175
176         for (i = 0; i < insn->n; i++)
177                 data[i] = devpriv->ao_readback[chan];
178
179         return insn->n;
180 }
181
182 static int atao_dio_insn_bits(struct comedi_device *dev,
183                               struct comedi_subdevice *s,
184                               struct comedi_insn *insn,
185                               unsigned int *data)
186 {
187         if (comedi_dio_update_state(s, data))
188                 outw(s->state, dev->iobase + ATAO_DIO_REG);
189
190         data[1] = inw(dev->iobase + ATAO_DIO_REG);
191
192         return insn->n;
193 }
194
195 static int atao_dio_insn_config(struct comedi_device *dev,
196                                 struct comedi_subdevice *s,
197                                 struct comedi_insn *insn,
198                                 unsigned int *data)
199 {
200         struct atao_private *devpriv = dev->private;
201         unsigned int chan = CR_CHAN(insn->chanspec);
202         unsigned int mask;
203         int ret;
204
205         if (chan < 4)
206                 mask = 0x0f;
207         else
208                 mask = 0xf0;
209
210         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
211         if (ret)
212                 return ret;
213
214         if (s->io_bits & 0x0f)
215                 devpriv->cfg3 |= ATAO_CFG3_DOUTEN1;
216         else
217                 devpriv->cfg3 &= ~ATAO_CFG3_DOUTEN1;
218         if (s->io_bits & 0xf0)
219                 devpriv->cfg3 |= ATAO_CFG3_DOUTEN2;
220         else
221                 devpriv->cfg3 &= ~ATAO_CFG3_DOUTEN2;
222
223         outw(devpriv->cfg3, dev->iobase + ATAO_CFG3_REG);
224
225         return insn->n;
226 }
227
228 /*
229  * There are three DAC8800 TrimDACs on the board. These are 8-channel,
230  * 8-bit DACs that are used to calibrate the Analog Output channels.
231  * The factory default calibration values are stored in the EEPROM.
232  * The TrimDACs, and EEPROM addresses, are mapped as:
233  *
234  *        Channel       EEPROM  Description
235  *   -----------------  ------  -----------------------------------
236  *    0 - DAC0 Chan 0    0x30   AO Channel 0 Offset
237  *    1 - DAC0 Chan 1    0x31   AO Channel 0 Gain
238  *    2 - DAC0 Chan 2    0x32   AO Channel 1 Offset
239  *    3 - DAC0 Chan 3    0x33   AO Channel 1 Gain
240  *    4 - DAC0 Chan 4    0x34   AO Channel 2 Offset
241  *    5 - DAC0 Chan 5    0x35   AO Channel 2 Gain
242  *    6 - DAC0 Chan 6    0x36   AO Channel 3 Offset
243  *    7 - DAC0 Chan 7    0x37   AO Channel 3 Gain
244  *    8 - DAC1 Chan 0    0x38   AO Channel 4 Offset
245  *    9 - DAC1 Chan 1    0x39   AO Channel 4 Gain
246  *   10 - DAC1 Chan 2    0x3a   AO Channel 5 Offset
247  *   11 - DAC1 Chan 3    0x3b   AO Channel 5 Gain
248  *   12 - DAC1 Chan 4    0x3c   2.5V Offset
249  *   13 - DAC1 Chan 5    0x3d   AO Channel 6 Offset (at-ao-10 only)
250  *   14 - DAC1 Chan 6    0x3e   AO Channel 6 Gain   (at-ao-10 only)
251  *   15 - DAC1 Chan 7    0x3f   AO Channel 7 Offset (at-ao-10 only)
252  *   16 - DAC2 Chan 0    0x40   AO Channel 7 Gain   (at-ao-10 only)
253  *   17 - DAC2 Chan 1    0x41   AO Channel 8 Offset (at-ao-10 only)
254  *   18 - DAC2 Chan 2    0x42   AO Channel 8 Gain   (at-ao-10 only)
255  *   19 - DAC2 Chan 3    0x43   AO Channel 9 Offset (at-ao-10 only)
256  *   20 - DAC2 Chan 4    0x44   AO Channel 9 Gain   (at-ao-10 only)
257  *        DAC2 Chan 5    0x45   Reserved
258  *        DAC2 Chan 6    0x46   Reserved
259  *        DAC2 Chan 7    0x47   Reserved
260  */
261 static int atao_calib_insn_write(struct comedi_device *dev,
262                                  struct comedi_subdevice *s,
263                                  struct comedi_insn *insn,
264                                  unsigned int *data)
265 {
266         struct atao_private *devpriv = dev->private;
267         unsigned int chan = CR_CHAN(insn->chanspec);
268         unsigned int bitstring;
269         unsigned int val;
270         int bit;
271
272         if (insn->n == 0)
273                 return 0;
274
275         devpriv->caldac[chan] = data[insn->n - 1] & s->maxdata;
276
277         /* write the channel and last data value to the caldac */
278         bitstring = ((chan & 0x7) << 8) | devpriv->caldac[chan];
279
280         /* clock the bitstring to the caldac; MSB -> LSB */
281         for (bit = 1 << 10; bit; bit >>= 1) {
282                 val = (bit & bitstring) ? ATAO_CFG2_SDATA : 0;
283
284                 outw(val, dev->iobase + ATAO_CFG2_REG);
285                 outw(val | ATAO_CFG2_SCLK, dev->iobase + ATAO_CFG2_REG);
286         }
287
288         /* strobe the caldac to load the value */
289         outw(ATAO_CFG2_CALLD(chan), dev->iobase + ATAO_CFG2_REG);
290         outw(ATAO_CFG2_CALLD_NOP, dev->iobase + ATAO_CFG2_REG);
291
292         return insn->n;
293 }
294
295 static int atao_calib_insn_read(struct comedi_device *dev,
296                                 struct comedi_subdevice *s,
297                                 struct comedi_insn *insn,
298                                 unsigned int *data)
299 {
300         struct atao_private *devpriv = dev->private;
301         unsigned int chan = CR_CHAN(insn->chanspec);
302         int i;
303
304         for (i = 0; i < insn->n; i++)
305                 data[i] = devpriv->caldac[chan];
306
307         return insn->n;
308 }
309
310 static void atao_reset(struct comedi_device *dev)
311 {
312         struct atao_private *devpriv = dev->private;
313
314         /* This is the reset sequence described in the manual */
315
316         devpriv->cfg1 = 0;
317         outw(devpriv->cfg1, dev->iobase + ATAO_CFG1_REG);
318
319         /* Put outputs of counter 1 and counter 2 in a high state */
320         i8254_load(dev->iobase + ATAO_82C53_BASE, 0,
321                    0, 0x0003, I8254_MODE4 | I8254_BINARY);
322         i8254_set_mode(dev->iobase + ATAO_82C53_BASE, 0,
323                    1, I8254_MODE4 | I8254_BINARY);
324
325         outw(ATAO_CFG2_CALLD_NOP, dev->iobase + ATAO_CFG2_REG);
326
327         devpriv->cfg3 = 0;
328         outw(devpriv->cfg3, dev->iobase + ATAO_CFG3_REG);
329
330         inw(dev->iobase + ATAO_FIFO_CLEAR_REG);
331
332         atao_select_reg_group(dev, 1);
333         outw(0, dev->iobase + ATAO_2_INT1CLR_REG);
334         outw(0, dev->iobase + ATAO_2_INT2CLR_REG);
335         outw(0, dev->iobase + ATAO_2_DMATCCLR_REG);
336         atao_select_reg_group(dev, 0);
337 }
338
339 static int atao_attach(struct comedi_device *dev, struct comedi_devconfig *it)
340 {
341         const struct atao_board *board = comedi_board(dev);
342         struct atao_private *devpriv;
343         struct comedi_subdevice *s;
344         int ret;
345
346         ret = comedi_request_region(dev, it->options[0], 0x20);
347         if (ret)
348                 return ret;
349
350         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
351         if (!devpriv)
352                 return -ENOMEM;
353
354         ret = comedi_alloc_subdevices(dev, 4);
355         if (ret)
356                 return ret;
357
358         /* Analog Output subdevice */
359         s = &dev->subdevices[0];
360         s->type         = COMEDI_SUBD_AO;
361         s->subdev_flags = SDF_WRITABLE;
362         s->n_chan       = board->n_ao_chans;
363         s->maxdata      = 0x0fff;
364         s->range_table  = it->options[3] ? &range_unipolar10 : &range_bipolar10;
365         s->insn_write   = atao_ao_insn_write;
366         s->insn_read    = atao_ao_insn_read;
367
368         /* Digital I/O subdevice */
369         s = &dev->subdevices[1];
370         s->type         = COMEDI_SUBD_DIO;
371         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
372         s->n_chan       = 8;
373         s->maxdata      = 1;
374         s->range_table  = &range_digital;
375         s->insn_bits    = atao_dio_insn_bits;
376         s->insn_config  = atao_dio_insn_config;
377
378         /* caldac subdevice */
379         s = &dev->subdevices[2];
380         s->type         = COMEDI_SUBD_CALIB;
381         s->subdev_flags = SDF_WRITABLE | SDF_INTERNAL;
382         s->n_chan       = (board->n_ao_chans * 2) + 1;
383         s->maxdata      = 0xff;
384         s->insn_read    = atao_calib_insn_read;
385         s->insn_write   = atao_calib_insn_write;
386
387         /* EEPROM subdevice */
388         s = &dev->subdevices[3];
389         s->type         = COMEDI_SUBD_UNUSED;
390
391         atao_reset(dev);
392
393         return 0;
394 }
395
396 static struct comedi_driver ni_at_ao_driver = {
397         .driver_name    = "ni_at_ao",
398         .module         = THIS_MODULE,
399         .attach         = atao_attach,
400         .detach         = comedi_legacy_detach,
401         .board_name     = &atao_boards[0].name,
402         .offset         = sizeof(struct atao_board),
403         .num_names      = ARRAY_SIZE(atao_boards),
404 };
405 module_comedi_driver(ni_at_ao_driver);
406
407 MODULE_AUTHOR("Comedi http://www.comedi.org");
408 MODULE_DESCRIPTION("Comedi driver for NI AT-AO-6/10 boards");
409 MODULE_LICENSE("GPL");