2 * comedi/drivers/cb_pcidda.c
3 * Driver for the ComputerBoards / MeasurementComputing PCI-DDA series.
5 * Copyright (C) 2001 Ivan Martinez <ivanmr@altavista.com>
6 * Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
8 * COMEDI - Linux Control and Measurement Device Interface
9 * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Description: MeasurementComputing PCI-DDA series
29 * Devices: (Measurement Computing) PCI-DDA08/12 [pci-dda08/12]
30 * (Measurement Computing) PCI-DDA04/12 [pci-dda04/12]
31 * (Measurement Computing) PCI-DDA02/12 [pci-dda02/12]
32 * (Measurement Computing) PCI-DDA08/16 [pci-dda08/16]
33 * (Measurement Computing) PCI-DDA04/16 [pci-dda04/16]
34 * (Measurement Computing) PCI-DDA02/16 [pci-dda02/16]
35 * Author: Ivan Martinez <ivanmr@altavista.com>
36 * Frank Mori Hess <fmhess@users.sourceforge.net>
39 * Configuration options: not applicable, uses PCI auto config
41 * Only simple analog output writing is supported.
44 #include "../comedidev.h"
46 #include "comedi_fc.h"
50 * ComputerBoards PCI Device ID's supported by this driver
52 #define PCI_DEVICE_ID_DDA02_12 0x0020
53 #define PCI_DEVICE_ID_DDA04_12 0x0021
54 #define PCI_DEVICE_ID_DDA08_12 0x0022
55 #define PCI_DEVICE_ID_DDA02_16 0x0023
56 #define PCI_DEVICE_ID_DDA04_16 0x0024
57 #define PCI_DEVICE_ID_DDA08_16 0x0025
59 #define EEPROM_SIZE 128 /* number of entries in eeprom */
60 /* maximum number of ao channels for supported boards */
61 #define MAX_AO_CHANNELS 8
63 /* Digital I/O registers */
64 #define PORT1A 0 /* PORT 1A DATA */
66 #define PORT1B 1 /* PORT 1B DATA */
68 #define PORT1C 2 /* PORT 1C DATA */
70 #define CONTROL1 3 /* CONTROL REGISTER 1 */
72 #define PORT2A 4 /* PORT 2A DATA */
74 #define PORT2B 5 /* PORT 2B DATA */
76 #define PORT2C 6 /* PORT 2C DATA */
78 #define CONTROL2 7 /* CONTROL REGISTER 2 */
81 #define DACONTROL 0 /* D/A CONTROL REGISTER */
82 #define SU 0000001 /* Simultaneous update enabled */
83 #define NOSU 0000000 /* Simultaneous update disabled */
84 #define ENABLEDAC 0000002 /* Enable specified DAC */
85 #define DISABLEDAC 0000000 /* Disable specified DAC */
86 #define RANGE2V5 0000000 /* 2.5V */
87 #define RANGE5V 0000200 /* 5V */
88 #define RANGE10V 0000300 /* 10V */
89 #define UNIP 0000400 /* Unipolar outputs */
90 #define BIP 0000000 /* Bipolar outputs */
92 #define DACALIBRATION1 4 /* D/A CALIBRATION REGISTER 1 */
94 /* serial data input for eeprom, caldacs, reference dac */
95 #define SERIAL_IN_BIT 0x1
96 #define CAL_CHANNEL_MASK (0x7 << 1)
97 #define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK)
99 #define CAL_COUNTER_MASK 0x1f
100 /* calibration counter overflow status bit */
101 #define CAL_COUNTER_OVERFLOW_BIT 0x20
102 /* analog output is less than reference dac voltage */
103 #define AO_BELOW_REF_BIT 0x40
104 #define SERIAL_OUT_BIT 0x80 /* serial data out, for reading from eeprom */
106 #define DACALIBRATION2 6 /* D/A CALIBRATION REGISTER 2 */
107 #define SELECT_EEPROM_BIT 0x1 /* send serial data in to eeprom */
108 /* don't send serial data to MAX542 reference dac */
109 #define DESELECT_REF_DAC_BIT 0x2
110 /* don't send serial data to caldac n */
111 #define DESELECT_CALDAC_BIT(n) (0x4 << (n))
112 /* manual says to set this bit with no explanation */
113 #define DUMMY_BIT 0x40
115 #define DADATA 8 /* FIRST D/A DATA REGISTER (0) */
117 static const struct comedi_lrange cb_pcidda_ranges = {
128 struct cb_pcidda_board {
130 unsigned short device_id;
135 static const struct cb_pcidda_board cb_pcidda_boards[] = {
137 .name = "pci-dda02/12",
138 .device_id = PCI_DEVICE_ID_DDA02_12,
142 .name = "pci-dda04/12",
143 .device_id = PCI_DEVICE_ID_DDA04_12,
147 .name = "pci-dda08/12",
148 .device_id = PCI_DEVICE_ID_DDA08_12,
152 .name = "pci-dda02/16",
153 .device_id = PCI_DEVICE_ID_DDA02_16,
157 .name = "pci-dda04/16",
158 .device_id = PCI_DEVICE_ID_DDA04_16,
162 .name = "pci-dda08/16",
163 .device_id = PCI_DEVICE_ID_DDA08_16,
169 struct cb_pcidda_private {
170 /* bits last written to da calibration register 1 */
171 unsigned int dac_cal1_bits;
172 /* current range settings for output channels */
173 unsigned int ao_range[MAX_AO_CHANNELS];
174 u16 eeprom_data[EEPROM_SIZE]; /* software copy of board's eeprom */
177 /* lowlevel read from eeprom */
178 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
180 unsigned int value = 0;
182 const int value_width = 16; /* number of bits wide values are */
184 for (i = 1; i <= value_width; i++) {
185 /* read bits most significant bit first */
186 if (inw_p(dev->iobase + DACALIBRATION1) & SERIAL_OUT_BIT)
187 value |= 1 << (value_width - i);
193 /* lowlevel write to eeprom/dac */
194 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
195 unsigned int num_bits)
197 struct cb_pcidda_private *devpriv = dev->private;
200 for (i = 1; i <= num_bits; i++) {
201 /* send bits most significant bit first */
202 if (value & (1 << (num_bits - i)))
203 devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
205 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
206 outw_p(devpriv->dac_cal1_bits, dev->iobase + DACALIBRATION1);
210 /* reads a 16 bit value from board's eeprom */
211 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
212 unsigned int address)
215 unsigned int cal2_bits;
217 /* one caldac for every two dac channels */
218 const int max_num_caldacs = 4;
219 /* bits to send to tell eeprom we want to read */
220 const int read_instruction = 0x6;
221 const int instruction_length = 3;
222 const int address_length = 8;
224 /* send serial output stream to eeprom */
225 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
226 /* deactivate caldacs (one caldac for every two channels) */
227 for (i = 0; i < max_num_caldacs; i++)
228 cal2_bits |= DESELECT_CALDAC_BIT(i);
229 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
231 /* tell eeprom we want to read */
232 cb_pcidda_serial_out(dev, read_instruction, instruction_length);
233 /* send address we want to read from */
234 cb_pcidda_serial_out(dev, address, address_length);
236 value = cb_pcidda_serial_in(dev);
238 /* deactivate eeprom */
239 cal2_bits &= ~SELECT_EEPROM_BIT;
240 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
245 /* writes to 8 bit calibration dacs */
246 static void cb_pcidda_write_caldac(struct comedi_device *dev,
247 unsigned int caldac, unsigned int channel,
250 unsigned int cal2_bits;
252 /* caldacs use 3 bit channel specification */
253 const int num_channel_bits = 3;
254 const int num_caldac_bits = 8; /* 8 bit calibration dacs */
255 /* one caldac for every two dac channels */
256 const int max_num_caldacs = 4;
258 /* write 3 bit channel */
259 cb_pcidda_serial_out(dev, channel, num_channel_bits);
260 /* write 8 bit caldac value */
261 cb_pcidda_serial_out(dev, value, num_caldac_bits);
264 * latch stream into appropriate caldac deselect reference dac
266 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
267 /* deactivate caldacs (one caldac for every two channels) */
268 for (i = 0; i < max_num_caldacs; i++)
269 cal2_bits |= DESELECT_CALDAC_BIT(i);
270 /* activate the caldac we want */
271 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
272 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
273 /* deactivate caldac */
274 cal2_bits |= DESELECT_CALDAC_BIT(caldac);
275 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
278 /* returns caldac that calibrates given analog out channel */
279 static unsigned int caldac_number(unsigned int channel)
284 /* returns caldac channel that provides fine gain for given ao channel */
285 static unsigned int fine_gain_channel(unsigned int ao_channel)
287 return 4 * (ao_channel % 2);
290 /* returns caldac channel that provides coarse gain for given ao channel */
291 static unsigned int coarse_gain_channel(unsigned int ao_channel)
293 return 1 + 4 * (ao_channel % 2);
296 /* returns caldac channel that provides coarse offset for given ao channel */
297 static unsigned int coarse_offset_channel(unsigned int ao_channel)
299 return 2 + 4 * (ao_channel % 2);
302 /* returns caldac channel that provides fine offset for given ao channel */
303 static unsigned int fine_offset_channel(unsigned int ao_channel)
305 return 3 + 4 * (ao_channel % 2);
308 /* returns eeprom address that provides offset for given ao channel and range */
309 static unsigned int offset_eeprom_address(unsigned int ao_channel,
312 return 0x7 + 2 * range + 12 * ao_channel;
316 * returns eeprom address that provides gain calibration for given ao
319 static unsigned int gain_eeprom_address(unsigned int ao_channel,
322 return 0x8 + 2 * range + 12 * ao_channel;
326 * returns upper byte of eeprom entry, which gives the coarse adjustment
329 static unsigned int eeprom_coarse_byte(unsigned int word)
331 return (word >> 8) & 0xff;
334 /* returns lower byte of eeprom entry, which gives the fine adjustment values */
335 static unsigned int eeprom_fine_byte(unsigned int word)
340 /* set caldacs to eeprom values for given channel and range */
341 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
344 struct cb_pcidda_private *devpriv = dev->private;
345 unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain;
347 /* remember range so we can tell when we need to readjust calibration */
348 devpriv->ao_range[channel] = range;
350 /* get values from eeprom data */
352 eeprom_coarse_byte(devpriv->eeprom_data
353 [offset_eeprom_address(channel, range)]);
355 eeprom_fine_byte(devpriv->eeprom_data
356 [offset_eeprom_address(channel, range)]);
358 eeprom_coarse_byte(devpriv->eeprom_data
359 [gain_eeprom_address(channel, range)]);
361 eeprom_fine_byte(devpriv->eeprom_data
362 [gain_eeprom_address(channel, range)]);
365 cb_pcidda_write_caldac(dev, caldac_number(channel),
366 coarse_offset_channel(channel), coarse_offset);
367 cb_pcidda_write_caldac(dev, caldac_number(channel),
368 fine_offset_channel(channel), fine_offset);
369 cb_pcidda_write_caldac(dev, caldac_number(channel),
370 coarse_gain_channel(channel), coarse_gain);
371 cb_pcidda_write_caldac(dev, caldac_number(channel),
372 fine_gain_channel(channel), fine_gain);
375 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
376 struct comedi_subdevice *s,
377 struct comedi_insn *insn, unsigned int *data)
379 struct cb_pcidda_private *devpriv = dev->private;
380 unsigned int command;
381 unsigned int channel, range;
383 channel = CR_CHAN(insn->chanspec);
384 range = CR_RANGE(insn->chanspec);
386 /* adjust calibration dacs if range has changed */
387 if (range != devpriv->ao_range[channel])
388 cb_pcidda_calibrate(dev, channel, range);
390 /* output channel configuration */
391 command = NOSU | ENABLEDAC;
393 /* output channel range */
396 command |= BIP | RANGE10V;
399 command |= BIP | RANGE5V;
402 command |= BIP | RANGE2V5;
405 command |= UNIP | RANGE10V;
408 command |= UNIP | RANGE5V;
411 command |= UNIP | RANGE2V5;
415 /* output channel specification */
416 command |= channel << 2;
417 outw(command, dev->iobase + DACONTROL);
420 outw(data[0], dev->iobase + DADATA + channel * 2);
422 /* return the number of samples read/written */
426 static const void *cb_pcidda_find_boardinfo(struct comedi_device *dev,
427 struct pci_dev *pcidev)
429 const struct cb_pcidda_board *thisboard;
432 for (i = 0; i < ARRAY_SIZE(cb_pcidda_boards); i++) {
433 thisboard = &cb_pcidda_boards[i];
434 if (thisboard->device_id != pcidev->device)
440 static int cb_pcidda_attach_pci(struct comedi_device *dev,
441 struct pci_dev *pcidev)
443 const struct cb_pcidda_board *thisboard;
444 struct cb_pcidda_private *devpriv;
445 struct comedi_subdevice *s;
446 unsigned long iobase_8255;
450 thisboard = cb_pcidda_find_boardinfo(dev, pcidev);
453 dev->board_ptr = thisboard;
454 dev->board_name = thisboard->name;
456 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
459 dev->private = devpriv;
461 ret = comedi_pci_enable(pcidev, dev->board_name);
464 dev->iobase = pci_resource_start(pcidev, 3);
465 iobase_8255 = pci_resource_start(pcidev, 2);
467 ret = comedi_alloc_subdevices(dev, 3);
471 s = &dev->subdevices[0];
472 /* analog output subdevice */
473 s->type = COMEDI_SUBD_AO;
474 s->subdev_flags = SDF_WRITABLE;
475 s->n_chan = thisboard->ao_chans;
476 s->maxdata = (1 << thisboard->ao_bits) - 1;
477 s->range_table = &cb_pcidda_ranges;
478 s->insn_write = cb_pcidda_ao_winsn;
480 /* two 8255 digital io subdevices */
481 for (i = 0; i < 2; i++) {
482 s = &dev->subdevices[1 + i];
483 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
488 /* Read the caldac eeprom data */
489 for (i = 0; i < EEPROM_SIZE; i++)
490 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
492 /* set calibrations dacs */
493 for (i = 0; i < thisboard->ao_chans; i++)
494 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
496 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
501 static void cb_pcidda_detach(struct comedi_device *dev)
503 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
505 if (dev->subdevices) {
506 subdev_8255_cleanup(dev, &dev->subdevices[1]);
507 subdev_8255_cleanup(dev, &dev->subdevices[2]);
511 comedi_pci_disable(pcidev);
515 static struct comedi_driver cb_pcidda_driver = {
516 .driver_name = "cb_pcidda",
517 .module = THIS_MODULE,
518 .attach_pci = cb_pcidda_attach_pci,
519 .detach = cb_pcidda_detach,
522 static int __devinit cb_pcidda_pci_probe(struct pci_dev *dev,
523 const struct pci_device_id *ent)
525 return comedi_pci_auto_config(dev, &cb_pcidda_driver);
528 static void __devexit cb_pcidda_pci_remove(struct pci_dev *dev)
530 comedi_pci_auto_unconfig(dev);
533 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
534 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_12) },
535 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_12) },
536 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_12) },
537 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_16) },
538 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_16) },
539 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_16) },
542 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
544 static struct pci_driver cb_pcidda_pci_driver = {
546 .id_table = cb_pcidda_pci_table,
547 .probe = cb_pcidda_pci_probe,
548 .remove = __devexit_p(cb_pcidda_pci_remove),
550 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
552 MODULE_AUTHOR("Comedi http://www.comedi.org");
553 MODULE_DESCRIPTION("Comedi low-level driver");
554 MODULE_LICENSE("GPL");