]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/rti800.c
Linux 3.12-rc6
[karo-tx-linux.git] / drivers / staging / comedi / drivers / rti800.c
1 /*
2  * comedi/drivers/rti800.c
3  * Hardware driver for Analog Devices RTI-800/815 board
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) 1998 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: rti800
21  * Description: Analog Devices RTI-800/815
22  * Devices: (Analog Devices) RTI-800 [rti800]
23  *          (Analog Devices) RTI-815 [rti815]
24  * Author: David A. Schleef <ds@schleef.org>
25  * Status: unknown
26  * Updated: Fri, 05 Sep 2008 14:50:44 +0100
27  *
28  * Configuration options:
29  *   [0] - I/O port base address
30  *   [1] - IRQ (not supported / unused)
31  *   [2] - A/D mux/reference (number of channels)
32  *         0 = differential
33  *         1 = pseudodifferential (common)
34  *         2 = single-ended
35  *   [3] - A/D range
36  *         0 = [-10,10]
37  *         1 = [-5,5]
38  *         2 = [0,10]
39  *   [4] - A/D encoding
40  *         0 = two's complement
41  *         1 = straight binary
42  *   [5] - DAC 0 range
43  *         0 = [-10,10]
44  *         1 = [0,10]
45  *   [6] - DAC 0 encoding
46  *         0 = two's complement
47  *         1 = straight binary
48  *   [7] - DAC 1 range (same as DAC 0)
49  *   [8] - DAC 1 encoding (same as DAC 0)
50  */
51
52 #include <linux/module.h>
53 #include <linux/delay.h>
54 #include <linux/interrupt.h>
55 #include "../comedidev.h"
56
57 /*
58  * Register map
59  */
60 #define RTI800_CSR              0x00
61 #define RTI800_CSR_BUSY         (1 << 7)
62 #define RTI800_CSR_DONE         (1 << 6)
63 #define RTI800_CSR_OVERRUN      (1 << 5)
64 #define RTI800_CSR_TCR          (1 << 4)
65 #define RTI800_CSR_DMA_ENAB     (1 << 3)
66 #define RTI800_CSR_INTR_TC      (1 << 2)
67 #define RTI800_CSR_INTR_EC      (1 << 1)
68 #define RTI800_CSR_INTR_OVRN    (1 << 0)
69 #define RTI800_MUXGAIN          0x01
70 #define RTI800_CONVERT          0x02
71 #define RTI800_ADCLO            0x03
72 #define RTI800_ADCHI            0x04
73 #define RTI800_DAC0LO           0x05
74 #define RTI800_DAC0HI           0x06
75 #define RTI800_DAC1LO           0x07
76 #define RTI800_DAC1HI           0x08
77 #define RTI800_CLRFLAGS         0x09
78 #define RTI800_DI               0x0a
79 #define RTI800_DO               0x0b
80 #define RTI800_9513A_DATA       0x0c
81 #define RTI800_9513A_CNTRL      0x0d
82 #define RTI800_9513A_STATUS     0x0d
83
84 #define RTI800_IOSIZE           0x10
85
86 #define RTI800_AI_TIMEOUT       100
87
88 static const struct comedi_lrange range_rti800_ai_10_bipolar = {
89         4, {
90                 BIP_RANGE(10),
91                 BIP_RANGE(1),
92                 BIP_RANGE(0.1),
93                 BIP_RANGE(0.02)
94         }
95 };
96
97 static const struct comedi_lrange range_rti800_ai_5_bipolar = {
98         4, {
99                 BIP_RANGE(5),
100                 BIP_RANGE(0.5),
101                 BIP_RANGE(0.05),
102                 BIP_RANGE(0.01)
103         }
104 };
105
106 static const struct comedi_lrange range_rti800_ai_unipolar = {
107         4, {
108                 UNI_RANGE(10),
109                 UNI_RANGE(1),
110                 UNI_RANGE(0.1),
111                 UNI_RANGE(0.02)
112         }
113 };
114
115 static const struct comedi_lrange *const rti800_ai_ranges[] = {
116         &range_rti800_ai_10_bipolar,
117         &range_rti800_ai_5_bipolar,
118         &range_rti800_ai_unipolar,
119 };
120
121 static const struct comedi_lrange *const rti800_ao_ranges[] = {
122         &range_bipolar10,
123         &range_unipolar10,
124 };
125
126 struct rti800_board {
127         const char *name;
128         int has_ao;
129 };
130
131 static const struct rti800_board rti800_boardtypes[] = {
132         {
133                 .name           = "rti800",
134         }, {
135                 .name           = "rti815",
136                 .has_ao         = 1,
137         },
138 };
139
140 struct rti800_private {
141         bool adc_2comp;
142         bool dac_2comp[2];
143         const struct comedi_lrange *ao_range_type_list[2];
144         unsigned int ao_readback[2];
145         unsigned char muxgain_bits;
146 };
147
148 static int rti800_ai_wait_for_conversion(struct comedi_device *dev,
149                                          int timeout)
150 {
151         unsigned char status;
152         int i;
153
154         for (i = 0; i < timeout; i++) {
155                 status = inb(dev->iobase + RTI800_CSR);
156                 if (status & RTI800_CSR_OVERRUN) {
157                         outb(0, dev->iobase + RTI800_CLRFLAGS);
158                         return -EIO;
159                 }
160                 if (status & RTI800_CSR_DONE)
161                         return 0;
162                 udelay(1);
163         }
164         return -ETIME;
165 }
166
167 static int rti800_ai_insn_read(struct comedi_device *dev,
168                                struct comedi_subdevice *s,
169                                struct comedi_insn *insn,
170                                unsigned int *data)
171 {
172         struct rti800_private *devpriv = dev->private;
173         unsigned int chan = CR_CHAN(insn->chanspec);
174         unsigned int gain = CR_RANGE(insn->chanspec);
175         unsigned char muxgain_bits;
176         int ret;
177         int i;
178
179         inb(dev->iobase + RTI800_ADCHI);
180         outb(0, dev->iobase + RTI800_CLRFLAGS);
181
182         muxgain_bits = chan | (gain << 5);
183         if (muxgain_bits != devpriv->muxgain_bits) {
184                 devpriv->muxgain_bits = muxgain_bits;
185                 outb(devpriv->muxgain_bits, dev->iobase + RTI800_MUXGAIN);
186                 /*
187                  * Without a delay here, the RTI_CSR_OVERRUN bit
188                  * gets set, and you will have an error.
189                  */
190                 if (insn->n > 0) {
191                         int delay = (gain == 0) ? 10 :
192                                     (gain == 1) ? 20 :
193                                     (gain == 2) ? 40 : 80;
194
195                         udelay(delay);
196                 }
197         }
198
199         for (i = 0; i < insn->n; i++) {
200                 outb(0, dev->iobase + RTI800_CONVERT);
201                 ret = rti800_ai_wait_for_conversion(dev, RTI800_AI_TIMEOUT);
202                 if (ret)
203                         return ret;
204
205                 data[i] = inb(dev->iobase + RTI800_ADCLO);
206                 data[i] |= (inb(dev->iobase + RTI800_ADCHI) & 0xf) << 8;
207
208                 if (devpriv->adc_2comp)
209                         data[i] ^= 0x800;
210         }
211
212         return insn->n;
213 }
214
215 static int rti800_ao_insn_read(struct comedi_device *dev,
216                                struct comedi_subdevice *s,
217                                struct comedi_insn *insn,
218                                unsigned int *data)
219 {
220         struct rti800_private *devpriv = dev->private;
221         unsigned int chan = CR_CHAN(insn->chanspec);
222         int i;
223
224         for (i = 0; i < insn->n; i++)
225                 data[i] = devpriv->ao_readback[chan];
226
227         return insn->n;
228 }
229
230 static int rti800_ao_insn_write(struct comedi_device *dev,
231                                 struct comedi_subdevice *s,
232                                 struct comedi_insn *insn,
233                                 unsigned int *data)
234 {
235         struct rti800_private *devpriv = dev->private;
236         unsigned int chan = CR_CHAN(insn->chanspec);
237         int reg_lo = chan ? RTI800_DAC1LO : RTI800_DAC0LO;
238         int reg_hi = chan ? RTI800_DAC1HI : RTI800_DAC0HI;
239         int val = devpriv->ao_readback[chan];
240         int i;
241
242         for (i = 0; i < insn->n; i++) {
243                 val = data[i];
244                 if (devpriv->dac_2comp[chan])
245                         val ^= 0x800;
246
247                 outb(val & 0xff, dev->iobase + reg_lo);
248                 outb((val >> 8) & 0xff, dev->iobase + reg_hi);
249         }
250
251         devpriv->ao_readback[chan] = val;
252
253         return insn->n;
254 }
255
256 static int rti800_di_insn_bits(struct comedi_device *dev,
257                                struct comedi_subdevice *s,
258                                struct comedi_insn *insn,
259                                unsigned int *data)
260 {
261         data[1] = inb(dev->iobase + RTI800_DI);
262         return insn->n;
263 }
264
265 static int rti800_do_insn_bits(struct comedi_device *dev,
266                                struct comedi_subdevice *s,
267                                struct comedi_insn *insn,
268                                unsigned int *data)
269 {
270         unsigned int mask = data[0];
271         unsigned int bits = data[1];
272
273         if (mask) {
274                 s->state &= ~mask;
275                 s->state |= (bits & mask);
276
277                 /* Outputs are inverted... */
278                 outb(s->state ^ 0xff, dev->iobase + RTI800_DO);
279         }
280
281         data[1] = s->state;
282
283         return insn->n;
284 }
285
286 static int rti800_attach(struct comedi_device *dev, struct comedi_devconfig *it)
287 {
288         const struct rti800_board *board = comedi_board(dev);
289         struct rti800_private *devpriv;
290         struct comedi_subdevice *s;
291         int ret;
292
293         ret = comedi_request_region(dev, it->options[0], RTI800_IOSIZE);
294         if (ret)
295                 return ret;
296
297         outb(0, dev->iobase + RTI800_CSR);
298         inb(dev->iobase + RTI800_ADCHI);
299         outb(0, dev->iobase + RTI800_CLRFLAGS);
300
301         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
302         if (!devpriv)
303                 return -ENOMEM;
304
305         devpriv->adc_2comp = (it->options[4] == 0);
306         devpriv->dac_2comp[0] = (it->options[6] == 0);
307         devpriv->dac_2comp[1] = (it->options[8] == 0);
308         /* invalid, forces the MUXGAIN register to be set when first used */
309         devpriv->muxgain_bits = 0xff;
310
311         ret = comedi_alloc_subdevices(dev, 4);
312         if (ret)
313                 return ret;
314
315         s = &dev->subdevices[0];
316         /* ai subdevice */
317         s->type         = COMEDI_SUBD_AI;
318         s->subdev_flags = SDF_READABLE | SDF_GROUND;
319         s->n_chan       = (it->options[2] ? 16 : 8);
320         s->insn_read    = rti800_ai_insn_read;
321         s->maxdata      = 0x0fff;
322         s->range_table  = (it->options[3] < ARRAY_SIZE(rti800_ai_ranges))
323                                 ? rti800_ai_ranges[it->options[3]]
324                                 : &range_unknown;
325
326         s = &dev->subdevices[1];
327         if (board->has_ao) {
328                 /* ao subdevice (only on rti815) */
329                 s->type         = COMEDI_SUBD_AO;
330                 s->subdev_flags = SDF_WRITABLE;
331                 s->n_chan       = 2;
332                 s->insn_read    = rti800_ao_insn_read;
333                 s->insn_write   = rti800_ao_insn_write;
334                 s->maxdata      = 0x0fff;
335                 s->range_table_list = devpriv->ao_range_type_list;
336                 devpriv->ao_range_type_list[0] =
337                         (it->options[5] < ARRAY_SIZE(rti800_ao_ranges))
338                                 ? rti800_ao_ranges[it->options[5]]
339                                 : &range_unknown;
340                 devpriv->ao_range_type_list[1] =
341                         (it->options[7] < ARRAY_SIZE(rti800_ao_ranges))
342                                 ? rti800_ao_ranges[it->options[7]]
343                                 : &range_unknown;
344         } else {
345                 s->type         = COMEDI_SUBD_UNUSED;
346         }
347
348         s = &dev->subdevices[2];
349         /* di */
350         s->type         = COMEDI_SUBD_DI;
351         s->subdev_flags = SDF_READABLE;
352         s->n_chan       = 8;
353         s->insn_bits    = rti800_di_insn_bits;
354         s->maxdata      = 1;
355         s->range_table  = &range_digital;
356
357         s = &dev->subdevices[3];
358         /* do */
359         s->type         = COMEDI_SUBD_DO;
360         s->subdev_flags = SDF_WRITABLE;
361         s->n_chan       = 8;
362         s->insn_bits    = rti800_do_insn_bits;
363         s->maxdata      = 1;
364         s->range_table  = &range_digital;
365
366         /*
367          * There is also an Am9513 timer on these boards. This subdevice
368          * is not currently supported.
369          */
370
371         return 0;
372 }
373
374 static struct comedi_driver rti800_driver = {
375         .driver_name    = "rti800",
376         .module         = THIS_MODULE,
377         .attach         = rti800_attach,
378         .detach         = comedi_legacy_detach,
379         .num_names      = ARRAY_SIZE(rti800_boardtypes),
380         .board_name     = &rti800_boardtypes[0].name,
381         .offset         = sizeof(struct rti800_board),
382 };
383 module_comedi_driver(rti800_driver);
384
385 MODULE_DESCRIPTION("Comedi: RTI-800 Multifunction Analog/Digital board");
386 MODULE_AUTHOR("Comedi http://www.comedi.org");
387 MODULE_LICENSE("GPL");