Linux-libre 3.17-rc7-gnu
[librecmc/linux-libre.git] / drivers / staging / comedi / drivers / cb_pcimdas.c
1 /*
2     comedi/drivers/cb_pcimdas.c
3     Comedi driver for Computer Boards PCIM-DAS1602/16
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 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: cb_pcimdas
20 Description: Measurement Computing PCI Migration series boards
21 Devices: [ComputerBoards] PCIM-DAS1602/16 (cb_pcimdas)
22 Author: Richard Bytheway
23 Updated: Wed, 13 Nov 2002 12:34:56 +0000
24 Status: experimental
25
26 Written to support the PCIM-DAS1602/16 on a 2.4 series kernel.
27
28 Configuration Options:
29     [0] - PCI bus number
30     [1] - PCI slot number
31
32 Developed from cb_pcidas and skel by Richard Bytheway (mocelet@sucs.org).
33 Only supports DIO, AO and simple AI in it's present form.
34 No interrupts, multi channel or FIFO AI,
35 although the card looks like it could support this.
36 See http://www.mccdaq.com/PDFs/Manuals/pcim-das1602-16.pdf for more details.
37 */
38
39 #include <linux/module.h>
40 #include <linux/pci.h>
41 #include <linux/interrupt.h>
42
43 #include "../comedidev.h"
44
45 #include "plx9052.h"
46 #include "8255.h"
47
48 /* Registers for the PCIM-DAS1602/16 */
49
50 /* DAC Offsets */
51 #define ADC_TRIG 0
52 #define DAC0_OFFSET 2
53 #define DAC1_OFFSET 4
54
55 /* AI and Counter Constants */
56 #define MUX_LIMITS 0
57 #define MAIN_CONN_DIO 1
58 #define ADC_STAT 2
59 #define ADC_CONV_STAT 3
60 #define ADC_INT 4
61 #define ADC_PACER 5
62 #define BURST_MODE 6
63 #define PROG_GAIN 7
64 #define CLK8254_1_DATA 8
65 #define CLK8254_2_DATA 9
66 #define CLK8254_3_DATA 10
67 #define CLK8254_CONTROL 11
68 #define USER_COUNTER 12
69 #define RESID_COUNT_H 13
70 #define RESID_COUNT_L 14
71
72 /*
73  * this structure is for data unique to this hardware driver.  If
74  * several hardware drivers keep similar information in this structure,
75  * feel free to suggest moving the variable to the struct comedi_device
76  * struct.
77  */
78 struct cb_pcimdas_private {
79         /* base addresses */
80         unsigned long BADR3;
81
82         /* Used for AO readback */
83         unsigned int ao_readback[2];
84 };
85
86 static int cb_pcimdas_ai_eoc(struct comedi_device *dev,
87                              struct comedi_subdevice *s,
88                              struct comedi_insn *insn,
89                              unsigned long context)
90 {
91         struct cb_pcimdas_private *devpriv = dev->private;
92         unsigned int status;
93
94         status = inb(devpriv->BADR3 + 2);
95         if ((status & 0x80) == 0)
96                 return 0;
97         return -EBUSY;
98 }
99
100 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
101                                struct comedi_subdevice *s,
102                                struct comedi_insn *insn, unsigned int *data)
103 {
104         struct cb_pcimdas_private *devpriv = dev->private;
105         int n;
106         unsigned int d;
107         int chan = CR_CHAN(insn->chanspec);
108         unsigned short chanlims;
109         int maxchans;
110         int ret;
111
112         /*  only support sw initiated reads from a single channel */
113
114         /* check channel number */
115         if ((inb(devpriv->BADR3 + 2) & 0x20) == 0)      /* differential mode */
116                 maxchans = s->n_chan / 2;
117         else
118                 maxchans = s->n_chan;
119
120         if (chan > (maxchans - 1))
121                 return -ETIMEDOUT;      /* *** Wrong error code. Fixme. */
122
123         /* configure for sw initiated read */
124         d = inb(devpriv->BADR3 + 5);
125         if ((d & 0x03) > 0) {   /* only reset if needed. */
126                 d = d & 0xfd;
127                 outb(d, devpriv->BADR3 + 5);
128         }
129
130         /* set bursting off, conversions on */
131         outb(0x01, devpriv->BADR3 + 6);
132
133         /* set range to 10V. UP/BP is controlled by a switch on the board */
134         outb(0x00, devpriv->BADR3 + 7);
135
136         /*
137          * write channel limits to multiplexer, set Low (bits 0-3) and
138          * High (bits 4-7) channels to chan.
139          */
140         chanlims = chan | (chan << 4);
141         outb(chanlims, devpriv->BADR3 + 0);
142
143         /* convert n samples */
144         for (n = 0; n < insn->n; n++) {
145                 /* trigger conversion */
146                 outw(0, dev->iobase + 0);
147
148                 /* wait for conversion to end */
149                 ret = comedi_timeout(dev, s, insn, cb_pcimdas_ai_eoc, 0);
150                 if (ret)
151                         return ret;
152
153                 /* read data */
154                 data[n] = inw(dev->iobase + 0);
155         }
156
157         /* return the number of samples read/written */
158         return n;
159 }
160
161 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
162                                struct comedi_subdevice *s,
163                                struct comedi_insn *insn, unsigned int *data)
164 {
165         struct cb_pcimdas_private *devpriv = dev->private;
166         int i;
167         int chan = CR_CHAN(insn->chanspec);
168
169         /* Writing a list of values to an AO channel is probably not
170          * very useful, but that's how the interface is defined. */
171         for (i = 0; i < insn->n; i++) {
172                 switch (chan) {
173                 case 0:
174                         outw(data[i] & 0x0FFF, dev->iobase + DAC0_OFFSET);
175                         break;
176                 case 1:
177                         outw(data[i] & 0x0FFF, dev->iobase + DAC1_OFFSET);
178                         break;
179                 default:
180                         return -1;
181                 }
182                 devpriv->ao_readback[chan] = data[i];
183         }
184
185         /* return the number of samples read/written */
186         return i;
187 }
188
189 /* AO subdevices should have a read insn as well as a write insn.
190  * Usually this means copying a value stored in devpriv. */
191 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
192                                struct comedi_subdevice *s,
193                                struct comedi_insn *insn, unsigned int *data)
194 {
195         struct cb_pcimdas_private *devpriv = dev->private;
196         int i;
197         int chan = CR_CHAN(insn->chanspec);
198
199         for (i = 0; i < insn->n; i++)
200                 data[i] = devpriv->ao_readback[chan];
201
202         return i;
203 }
204
205 static int cb_pcimdas_auto_attach(struct comedi_device *dev,
206                                             unsigned long context_unused)
207 {
208         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
209         struct cb_pcimdas_private *devpriv;
210         struct comedi_subdevice *s;
211         unsigned long iobase_8255;
212         int ret;
213
214         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
215         if (!devpriv)
216                 return -ENOMEM;
217
218         ret = comedi_pci_enable(dev);
219         if (ret)
220                 return ret;
221
222         dev->iobase = pci_resource_start(pcidev, 2);
223         devpriv->BADR3 = pci_resource_start(pcidev, 3);
224         iobase_8255 = pci_resource_start(pcidev, 4);
225
226         ret = comedi_alloc_subdevices(dev, 3);
227         if (ret)
228                 return ret;
229
230         s = &dev->subdevices[0];
231         /* dev->read_subdev=s; */
232         /*  analog input subdevice */
233         s->type = COMEDI_SUBD_AI;
234         s->subdev_flags = SDF_READABLE | SDF_GROUND;
235         s->n_chan = 16;
236         s->maxdata = 0xffff;
237         s->range_table = &range_unknown;
238         s->len_chanlist = 1;    /*  This is the maximum chanlist length that */
239         /*  the board can handle */
240         s->insn_read = cb_pcimdas_ai_rinsn;
241
242         s = &dev->subdevices[1];
243         /*  analog output subdevice */
244         s->type = COMEDI_SUBD_AO;
245         s->subdev_flags = SDF_WRITABLE;
246         s->n_chan = 2;
247         s->maxdata = 0xfff;
248         /* ranges are hardware settable, but not software readable. */
249         s->range_table = &range_unknown;
250         s->insn_write = &cb_pcimdas_ao_winsn;
251         s->insn_read = &cb_pcimdas_ao_rinsn;
252
253         s = &dev->subdevices[2];
254         /* digital i/o subdevice */
255         ret = subdev_8255_init(dev, s, NULL, iobase_8255);
256         if (ret)
257                 return ret;
258
259         return 0;
260 }
261
262 static void cb_pcimdas_detach(struct comedi_device *dev)
263 {
264         if (dev->irq)
265                 free_irq(dev->irq, dev);
266         comedi_pci_disable(dev);
267 }
268
269 static struct comedi_driver cb_pcimdas_driver = {
270         .driver_name    = "cb_pcimdas",
271         .module         = THIS_MODULE,
272         .auto_attach    = cb_pcimdas_auto_attach,
273         .detach         = cb_pcimdas_detach,
274 };
275
276 static int cb_pcimdas_pci_probe(struct pci_dev *dev,
277                                 const struct pci_device_id *id)
278 {
279         return comedi_pci_auto_config(dev, &cb_pcimdas_driver,
280                                       id->driver_data);
281 }
282
283 static const struct pci_device_id cb_pcimdas_pci_table[] = {
284         { PCI_DEVICE(PCI_VENDOR_ID_CB, 0x0056) },
285         { 0 }
286 };
287 MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
288
289 static struct pci_driver cb_pcimdas_pci_driver = {
290         .name           = "cb_pcimdas",
291         .id_table       = cb_pcimdas_pci_table,
292         .probe          = cb_pcimdas_pci_probe,
293         .remove         = comedi_pci_auto_unconfig,
294 };
295 module_comedi_pci_driver(cb_pcimdas_driver, cb_pcimdas_pci_driver);
296
297 MODULE_AUTHOR("Comedi http://www.comedi.org");
298 MODULE_DESCRIPTION("Comedi low-level driver");
299 MODULE_LICENSE("GPL");