Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[deliverable/linux.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 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 /* Board description */
84 struct cb_pcimdas_board {
85 const char *name;
86 unsigned short device_id;
87 int ai_se_chans; /* Inputs in single-ended mode */
88 int ai_diff_chans; /* Inputs in differential mode */
89 int ai_bits; /* analog input resolution */
90 int ai_speed; /* fastest conversion period in ns */
91 int ao_nchan; /* number of analog out channels */
92 int ao_bits; /* analogue output resolution */
93 int has_ao_fifo; /* analog output has fifo */
94 int ao_scan_speed; /* analog output speed for 1602 series (for a scan, not conversion) */
95 int fifo_size; /* number of samples fifo can hold */
96 int dio_bits; /* number of dio bits */
97 int has_dio; /* has DIO */
98 const struct comedi_lrange *ranges;
99 };
100
101 static const struct cb_pcimdas_board cb_pcimdas_boards[] = {
102 {
103 .name = "PCIM-DAS1602/16",
104 .device_id = 0x56,
105 .ai_se_chans = 16,
106 .ai_diff_chans = 8,
107 .ai_bits = 16,
108 .ai_speed = 10000, /* ?? */
109 .ao_nchan = 2,
110 .ao_bits = 12,
111 .has_ao_fifo = 0, /* ?? */
112 .ao_scan_speed = 10000,
113 /* ?? */
114 .fifo_size = 1024,
115 .dio_bits = 24,
116 .has_dio = 1,
117 /* .ranges = &cb_pcimdas_ranges, */
118 },
119 };
120
121 /*
122 * Useful for shorthand access to the particular board structure
123 */
124 #define thisboard ((const struct cb_pcimdas_board *)dev->board_ptr)
125
126 /*
127 * this structure is for data unique to this hardware driver. If
128 * several hardware drivers keep similar information in this structure,
129 * feel free to suggest moving the variable to the struct comedi_device
130 * struct.
131 */
132 struct cb_pcimdas_private {
133 /* base addresses */
134 unsigned long BADR3;
135
136 /* Used for AO readback */
137 unsigned int ao_readback[2];
138 };
139
140 /*
141 * most drivers define the following macro to make it easy to
142 * access the private structure.
143 */
144 #define devpriv ((struct cb_pcimdas_private *)dev->private)
145
146 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
147 struct comedi_subdevice *s,
148 struct comedi_insn *insn, unsigned int *data);
149 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
150 struct comedi_subdevice *s,
151 struct comedi_insn *insn, unsigned int *data);
152 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
153 struct comedi_subdevice *s,
154 struct comedi_insn *insn, unsigned int *data);
155
156 static struct pci_dev *cb_pcimdas_find_pci_dev(struct comedi_device *dev,
157 struct comedi_devconfig *it)
158 {
159 struct pci_dev *pcidev = NULL;
160 int bus = it->options[0];
161 int slot = it->options[1];
162 int i;
163
164 for_each_pci_dev(pcidev) {
165 if (bus || slot) {
166 if (bus != pcidev->bus->number ||
167 slot != PCI_SLOT(pcidev->devfn))
168 continue;
169 }
170 if (pcidev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS)
171 continue;
172
173 for (i = 0; i < ARRAY_SIZE(cb_pcimdas_boards); i++) {
174 if (cb_pcimdas_boards[i].device_id != pcidev->device)
175 continue;
176
177 dev->board_ptr = cb_pcimdas_boards + i;
178 return pcidev;
179 }
180 }
181 dev_err(dev->class_dev,
182 "No supported board found! (req. bus %d, slot %d)\n",
183 bus, slot);
184 return NULL;
185 }
186
187 /*
188 * Attach is called by the Comedi core to configure the driver
189 * for a particular board. If you specified a board_name array
190 * in the driver structure, dev->board_ptr contains that
191 * address.
192 */
193 static int cb_pcimdas_attach(struct comedi_device *dev,
194 struct comedi_devconfig *it)
195 {
196 struct pci_dev *pcidev;
197 struct comedi_subdevice *s;
198 unsigned long iobase_8255;
199 int ret;
200
201 /*
202 * Allocate the private structure area.
203 */
204 if (alloc_private(dev, sizeof(struct cb_pcimdas_private)) < 0)
205 return -ENOMEM;
206
207 pcidev = cb_pcimdas_find_pci_dev(dev, it);
208 if (!pcidev)
209 return -EIO;
210 comedi_set_hw_dev(dev, &pcidev->dev);
211
212 /* Warn about non-tested features */
213 switch (thisboard->device_id) {
214 case 0x56:
215 break;
216 default:
217 dev_dbg(dev->class_dev, "THIS CARD IS UNSUPPORTED.\n");
218 dev_dbg(dev->class_dev,
219 "PLEASE REPORT USAGE TO <mocelet@sucs.org>\n");
220 }
221
222 if (comedi_pci_enable(pcidev, "cb_pcimdas")) {
223 dev_err(dev->class_dev,
224 "Failed to enable PCI device and request regions\n");
225 return -EIO;
226 }
227
228 dev->iobase = pci_resource_start(pcidev, 2);
229 devpriv->BADR3 = pci_resource_start(pcidev, 3);
230 iobase_8255 = pci_resource_start(pcidev, 4);
231
232 /* Dont support IRQ yet */
233 /* get irq */
234 /* if(request_irq(pcidev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */
235 /* { */
236 /* printk(" unable to allocate irq %u\n", pcidev->irq); */
237 /* return -EINVAL; */
238 /* } */
239 /* dev->irq = pcidev->irq; */
240
241 /* Initialize dev->board_name */
242 dev->board_name = thisboard->name;
243
244 ret = comedi_alloc_subdevices(dev, 3);
245 if (ret)
246 return ret;
247
248 s = dev->subdevices + 0;
249 /* dev->read_subdev=s; */
250 /* analog input subdevice */
251 s->type = COMEDI_SUBD_AI;
252 s->subdev_flags = SDF_READABLE | SDF_GROUND;
253 s->n_chan = thisboard->ai_se_chans;
254 s->maxdata = (1 << thisboard->ai_bits) - 1;
255 s->range_table = &range_unknown;
256 s->len_chanlist = 1; /* This is the maximum chanlist length that */
257 /* the board can handle */
258 s->insn_read = cb_pcimdas_ai_rinsn;
259
260 s = dev->subdevices + 1;
261 /* analog output subdevice */
262 s->type = COMEDI_SUBD_AO;
263 s->subdev_flags = SDF_WRITABLE;
264 s->n_chan = thisboard->ao_nchan;
265 s->maxdata = 1 << thisboard->ao_bits;
266 /* ranges are hardware settable, but not software readable. */
267 s->range_table = &range_unknown;
268 s->insn_write = &cb_pcimdas_ao_winsn;
269 s->insn_read = &cb_pcimdas_ao_rinsn;
270
271 s = dev->subdevices + 2;
272 /* digital i/o subdevice */
273 if (thisboard->has_dio)
274 subdev_8255_init(dev, s, NULL, iobase_8255);
275 else
276 s->type = COMEDI_SUBD_UNUSED;
277
278 return 1;
279 }
280
281 static void cb_pcimdas_detach(struct comedi_device *dev)
282 {
283 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
284
285 if (dev->irq)
286 free_irq(dev->irq, dev);
287 if (pcidev) {
288 if (dev->iobase)
289 comedi_pci_disable(pcidev);
290 pci_dev_put(pcidev);
291 }
292 }
293
294 /*
295 * "instructions" read/write data in "one-shot" or "software-triggered"
296 * mode.
297 */
298 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
299 struct comedi_subdevice *s,
300 struct comedi_insn *insn, unsigned int *data)
301 {
302 int n, i;
303 unsigned int d;
304 unsigned int busy;
305 int chan = CR_CHAN(insn->chanspec);
306 unsigned short chanlims;
307 int maxchans;
308
309 /* only support sw initiated reads from a single channel */
310
311 /* check channel number */
312 if ((inb(devpriv->BADR3 + 2) & 0x20) == 0) /* differential mode */
313 maxchans = thisboard->ai_diff_chans;
314 else
315 maxchans = thisboard->ai_se_chans;
316
317 if (chan > (maxchans - 1))
318 return -ETIMEDOUT; /* *** Wrong error code. Fixme. */
319
320 /* configure for sw initiated read */
321 d = inb(devpriv->BADR3 + 5);
322 if ((d & 0x03) > 0) { /* only reset if needed. */
323 d = d & 0xfd;
324 outb(d, devpriv->BADR3 + 5);
325 }
326 outb(0x01, devpriv->BADR3 + 6); /* set bursting off, conversions on */
327 outb(0x00, devpriv->BADR3 + 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
328
329 /*
330 * write channel limits to multiplexer, set Low (bits 0-3) and
331 * High (bits 4-7) channels to chan.
332 */
333 chanlims = chan | (chan << 4);
334 outb(chanlims, devpriv->BADR3 + 0);
335
336 /* convert n samples */
337 for (n = 0; n < insn->n; n++) {
338 /* trigger conversion */
339 outw(0, dev->iobase + 0);
340
341 #define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
342 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
343
344 /* wait for conversion to end */
345 for (i = 0; i < TIMEOUT; i++) {
346 busy = inb(devpriv->BADR3 + 2) & 0x80;
347 if (!busy)
348 break;
349 }
350 if (i == TIMEOUT) {
351 printk("timeout\n");
352 return -ETIMEDOUT;
353 }
354 /* read data */
355 d = inw(dev->iobase + 0);
356
357 /* mangle the data as necessary */
358 /* d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed. */
359
360 data[n] = d;
361 }
362
363 /* return the number of samples read/written */
364 return n;
365 }
366
367 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
368 struct comedi_subdevice *s,
369 struct comedi_insn *insn, unsigned int *data)
370 {
371 int i;
372 int chan = CR_CHAN(insn->chanspec);
373
374 /* Writing a list of values to an AO channel is probably not
375 * very useful, but that's how the interface is defined. */
376 for (i = 0; i < insn->n; i++) {
377 switch (chan) {
378 case 0:
379 outw(data[i] & 0x0FFF, dev->iobase + DAC0_OFFSET);
380 break;
381 case 1:
382 outw(data[i] & 0x0FFF, dev->iobase + DAC1_OFFSET);
383 break;
384 default:
385 return -1;
386 }
387 devpriv->ao_readback[chan] = data[i];
388 }
389
390 /* return the number of samples read/written */
391 return i;
392 }
393
394 /* AO subdevices should have a read insn as well as a write insn.
395 * Usually this means copying a value stored in devpriv. */
396 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
397 struct comedi_subdevice *s,
398 struct comedi_insn *insn, unsigned int *data)
399 {
400 int i;
401 int chan = CR_CHAN(insn->chanspec);
402
403 for (i = 0; i < insn->n; i++)
404 data[i] = devpriv->ao_readback[chan];
405
406 return i;
407 }
408
409 static struct comedi_driver cb_pcimdas_driver = {
410 .driver_name = "cb_pcimdas",
411 .module = THIS_MODULE,
412 .attach = cb_pcimdas_attach,
413 .detach = cb_pcimdas_detach,
414 };
415
416 static int __devinit cb_pcimdas_pci_probe(struct pci_dev *dev,
417 const struct pci_device_id *ent)
418 {
419 return comedi_pci_auto_config(dev, &cb_pcimdas_driver);
420 }
421
422 static void __devexit cb_pcimdas_pci_remove(struct pci_dev *dev)
423 {
424 comedi_pci_auto_unconfig(dev);
425 }
426
427 static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table) = {
428 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTERBOARDS, 0x0056) },
429 { 0 }
430 };
431 MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
432
433 static struct pci_driver cb_pcimdas_pci_driver = {
434 .name = "cb_pcimdas",
435 .id_table = cb_pcimdas_pci_table,
436 .probe = cb_pcimdas_pci_probe,
437 .remove = __devexit_p(cb_pcimdas_pci_remove),
438 };
439 module_comedi_pci_driver(cb_pcimdas_driver, cb_pcimdas_pci_driver);
440
441 MODULE_AUTHOR("Comedi http://www.comedi.org");
442 MODULE_DESCRIPTION("Comedi low-level driver");
443 MODULE_LICENSE("GPL");
This page took 0.041164 seconds and 6 git commands to generate.