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