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