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