Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cb_pcimdas.c
Go to the documentation of this file.
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 <[email protected]>
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 ([email protected]).
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 */
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  * this structure is for data unique to this hardware driver. If
123  * several hardware drivers keep similar information in this structure,
124  * feel free to suggest moving the variable to the struct comedi_device
125  * struct.
126  */
128  /* base addresses */
129  unsigned long BADR3;
130 
131  /* Used for AO readback */
132  unsigned int ao_readback[2];
133 };
134 
135 /*
136  * "instructions" read/write data in "one-shot" or "software-triggered"
137  * mode.
138  */
139 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
140  struct comedi_subdevice *s,
141  struct comedi_insn *insn, unsigned int *data)
142 {
143  const struct cb_pcimdas_board *thisboard = comedi_board(dev);
144  struct cb_pcimdas_private *devpriv = dev->private;
145  int n, i;
146  unsigned int d;
147  unsigned int busy;
148  int chan = CR_CHAN(insn->chanspec);
149  unsigned short chanlims;
150  int maxchans;
151 
152  /* only support sw initiated reads from a single channel */
153 
154  /* check channel number */
155  if ((inb(devpriv->BADR3 + 2) & 0x20) == 0) /* differential mode */
156  maxchans = thisboard->ai_diff_chans;
157  else
158  maxchans = thisboard->ai_se_chans;
159 
160  if (chan > (maxchans - 1))
161  return -ETIMEDOUT; /* *** Wrong error code. Fixme. */
162 
163  /* configure for sw initiated read */
164  d = inb(devpriv->BADR3 + 5);
165  if ((d & 0x03) > 0) { /* only reset if needed. */
166  d = d & 0xfd;
167  outb(d, devpriv->BADR3 + 5);
168  }
169  outb(0x01, devpriv->BADR3 + 6); /* set bursting off, conversions on */
170  outb(0x00, devpriv->BADR3 + 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
171 
172  /*
173  * write channel limits to multiplexer, set Low (bits 0-3) and
174  * High (bits 4-7) channels to chan.
175  */
176  chanlims = chan | (chan << 4);
177  outb(chanlims, devpriv->BADR3 + 0);
178 
179  /* convert n samples */
180  for (n = 0; n < insn->n; n++) {
181  /* trigger conversion */
182  outw(0, dev->iobase + 0);
183 
184 #define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
185  /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
186 
187  /* wait for conversion to end */
188  for (i = 0; i < TIMEOUT; i++) {
189  busy = inb(devpriv->BADR3 + 2) & 0x80;
190  if (!busy)
191  break;
192  }
193  if (i == TIMEOUT) {
194  printk("timeout\n");
195  return -ETIMEDOUT;
196  }
197  /* read data */
198  d = inw(dev->iobase + 0);
199 
200  /* mangle the data as necessary */
201  /* d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed. */
202 
203  data[n] = d;
204  }
205 
206  /* return the number of samples read/written */
207  return n;
208 }
209 
210 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
211  struct comedi_subdevice *s,
212  struct comedi_insn *insn, unsigned int *data)
213 {
214  struct cb_pcimdas_private *devpriv = dev->private;
215  int i;
216  int chan = CR_CHAN(insn->chanspec);
217 
218  /* Writing a list of values to an AO channel is probably not
219  * very useful, but that's how the interface is defined. */
220  for (i = 0; i < insn->n; i++) {
221  switch (chan) {
222  case 0:
223  outw(data[i] & 0x0FFF, dev->iobase + DAC0_OFFSET);
224  break;
225  case 1:
226  outw(data[i] & 0x0FFF, dev->iobase + DAC1_OFFSET);
227  break;
228  default:
229  return -1;
230  }
231  devpriv->ao_readback[chan] = data[i];
232  }
233 
234  /* return the number of samples read/written */
235  return i;
236 }
237 
238 /* AO subdevices should have a read insn as well as a write insn.
239  * Usually this means copying a value stored in devpriv. */
240 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
241  struct comedi_subdevice *s,
242  struct comedi_insn *insn, unsigned int *data)
243 {
244  struct cb_pcimdas_private *devpriv = dev->private;
245  int i;
246  int chan = CR_CHAN(insn->chanspec);
247 
248  for (i = 0; i < insn->n; i++)
249  data[i] = devpriv->ao_readback[chan];
250 
251  return i;
252 }
253 
254 static const void *cb_pcimdas_find_boardinfo(struct comedi_device *dev,
255  struct pci_dev *pcidev)
256 {
257  const struct cb_pcimdas_board *thisboard;
258  int i;
259 
260  for (i = 0; i < ARRAY_SIZE(cb_pcimdas_boards); i++) {
261  thisboard = &cb_pcimdas_boards[i];
262  if (thisboard->device_id == pcidev->device)
263  return thisboard;
264  }
265  return NULL;
266 }
267 
268 static int cb_pcimdas_attach_pci(struct comedi_device *dev,
269  struct pci_dev *pcidev)
270 {
271  const struct cb_pcimdas_board *thisboard;
272  struct cb_pcimdas_private *devpriv;
273  struct comedi_subdevice *s;
274  unsigned long iobase_8255;
275  int ret;
276 
277  comedi_set_hw_dev(dev, &pcidev->dev);
278 
279  thisboard = cb_pcimdas_find_boardinfo(dev, pcidev);
280  if (!thisboard)
281  return -ENODEV;
282  dev->board_ptr = thisboard;
283  dev->board_name = thisboard->name;
284 
285  ret = alloc_private(dev, sizeof(*devpriv));
286  if (ret)
287  return ret;
288  devpriv = dev->private;
289 
290  /* Warn about non-tested features */
291  switch (thisboard->device_id) {
292  case 0x56:
293  break;
294  default:
295  dev_dbg(dev->class_dev, "THIS CARD IS UNSUPPORTED.\n");
296  dev_dbg(dev->class_dev,
297  "PLEASE REPORT USAGE TO <[email protected]>\n");
298  }
299 
300  ret = comedi_pci_enable(pcidev, dev->board_name);
301  if (ret)
302  return ret;
303 
304  dev->iobase = pci_resource_start(pcidev, 2);
305  devpriv->BADR3 = pci_resource_start(pcidev, 3);
306  iobase_8255 = pci_resource_start(pcidev, 4);
307 
308 /* Dont support IRQ yet */
309 /* get irq */
310 /* if(request_irq(pcidev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */
311 /* { */
312 /* printk(" unable to allocate irq %u\n", pcidev->irq); */
313 /* return -EINVAL; */
314 /* } */
315 /* dev->irq = pcidev->irq; */
316 
317  ret = comedi_alloc_subdevices(dev, 3);
318  if (ret)
319  return ret;
320 
321  s = &dev->subdevices[0];
322  /* dev->read_subdev=s; */
323  /* analog input subdevice */
324  s->type = COMEDI_SUBD_AI;
326  s->n_chan = thisboard->ai_se_chans;
327  s->maxdata = (1 << thisboard->ai_bits) - 1;
329  s->len_chanlist = 1; /* This is the maximum chanlist length that */
330  /* the board can handle */
331  s->insn_read = cb_pcimdas_ai_rinsn;
332 
333  s = &dev->subdevices[1];
334  /* analog output subdevice */
335  s->type = COMEDI_SUBD_AO;
337  s->n_chan = thisboard->ao_nchan;
338  s->maxdata = 1 << thisboard->ao_bits;
339  /* ranges are hardware settable, but not software readable. */
341  s->insn_write = &cb_pcimdas_ao_winsn;
342  s->insn_read = &cb_pcimdas_ao_rinsn;
343 
344  s = &dev->subdevices[2];
345  /* digital i/o subdevice */
346  if (thisboard->has_dio)
347  subdev_8255_init(dev, s, NULL, iobase_8255);
348  else
350 
351  dev_info(dev->class_dev, "%s attached\n", dev->board_name);
352 
353  return 0;
354 }
355 
356 static void cb_pcimdas_detach(struct comedi_device *dev)
357 {
358  struct pci_dev *pcidev = comedi_to_pci_dev(dev);
359 
360  if (dev->irq)
361  free_irq(dev->irq, dev);
362  if (pcidev) {
363  if (dev->iobase)
364  comedi_pci_disable(pcidev);
365  }
366 }
367 
368 static struct comedi_driver cb_pcimdas_driver = {
369  .driver_name = "cb_pcimdas",
370  .module = THIS_MODULE,
371  .attach_pci = cb_pcimdas_attach_pci,
372  .detach = cb_pcimdas_detach,
373 };
374 
375 static int __devinit cb_pcimdas_pci_probe(struct pci_dev *dev,
376  const struct pci_device_id *ent)
377 {
378  return comedi_pci_auto_config(dev, &cb_pcimdas_driver);
379 }
380 
381 static void __devexit cb_pcimdas_pci_remove(struct pci_dev *dev)
382 {
384 }
385 
386 static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table) = {
388  { 0 }
389 };
390 MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
391 
392 static struct pci_driver cb_pcimdas_pci_driver = {
393  .name = "cb_pcimdas",
394  .id_table = cb_pcimdas_pci_table,
395  .probe = cb_pcimdas_pci_probe,
396  .remove = __devexit_p(cb_pcimdas_pci_remove),
397 };
398 module_comedi_pci_driver(cb_pcimdas_driver, cb_pcimdas_pci_driver);
399 
400 MODULE_AUTHOR("Comedi http://www.comedi.org");
401 MODULE_DESCRIPTION("Comedi low-level driver");
402 MODULE_LICENSE("GPL");