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 = {
129 * Board descriptions for two imaginary boards. Describing the
130 * boards in this way is optional, and completely driver-dependent.
131 * Some drivers use arrays such as this, other do not.
133 struct cb_pcidda_board {
135 char status; /* Driver status: */
139 * 1 - manual read, not tested
140 * 2 - manual not read
143 unsigned short device_id;
148 static const struct cb_pcidda_board cb_pcidda_boards[] = {
150 .name = "pci-dda02/12",
152 .device_id = PCI_DEVICE_ID_DDA02_12,
157 .name = "pci-dda04/12",
159 .device_id = PCI_DEVICE_ID_DDA04_12,
164 .name = "pci-dda08/12",
166 .device_id = PCI_DEVICE_ID_DDA08_12,
171 .name = "pci-dda02/16",
173 .device_id = PCI_DEVICE_ID_DDA02_16,
178 .name = "pci-dda04/16",
180 .device_id = PCI_DEVICE_ID_DDA04_16,
185 .name = "pci-dda08/16",
187 .device_id = PCI_DEVICE_ID_DDA08_16,
193 struct cb_pcidda_private {
194 /* bits last written to da calibration register 1 */
195 unsigned int dac_cal1_bits;
196 /* current range settings for output channels */
197 unsigned int ao_range[MAX_AO_CHANNELS];
198 u16 eeprom_data[EEPROM_SIZE]; /* software copy of board's eeprom */
201 /* lowlevel read from eeprom */
202 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
204 unsigned int value = 0;
206 const int value_width = 16; /* number of bits wide values are */
208 for (i = 1; i <= value_width; i++) {
209 /* read bits most significant bit first */
210 if (inw_p(dev->iobase + DACALIBRATION1) & SERIAL_OUT_BIT)
211 value |= 1 << (value_width - i);
217 /* lowlevel write to eeprom/dac */
218 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
219 unsigned int num_bits)
221 struct cb_pcidda_private *devpriv = dev->private;
224 for (i = 1; i <= num_bits; i++) {
225 /* send bits most significant bit first */
226 if (value & (1 << (num_bits - i)))
227 devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
229 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
230 outw_p(devpriv->dac_cal1_bits, dev->iobase + DACALIBRATION1);
234 /* reads a 16 bit value from board's eeprom */
235 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
236 unsigned int address)
239 unsigned int cal2_bits;
241 /* one caldac for every two dac channels */
242 const int max_num_caldacs = 4;
243 /* bits to send to tell eeprom we want to read */
244 const int read_instruction = 0x6;
245 const int instruction_length = 3;
246 const int address_length = 8;
248 /* send serial output stream to eeprom */
249 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
250 /* deactivate caldacs (one caldac for every two channels) */
251 for (i = 0; i < max_num_caldacs; i++)
252 cal2_bits |= DESELECT_CALDAC_BIT(i);
253 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
255 /* tell eeprom we want to read */
256 cb_pcidda_serial_out(dev, read_instruction, instruction_length);
257 /* send address we want to read from */
258 cb_pcidda_serial_out(dev, address, address_length);
260 value = cb_pcidda_serial_in(dev);
262 /* deactivate eeprom */
263 cal2_bits &= ~SELECT_EEPROM_BIT;
264 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
269 /* writes to 8 bit calibration dacs */
270 static void cb_pcidda_write_caldac(struct comedi_device *dev,
271 unsigned int caldac, unsigned int channel,
274 unsigned int cal2_bits;
276 /* caldacs use 3 bit channel specification */
277 const int num_channel_bits = 3;
278 const int num_caldac_bits = 8; /* 8 bit calibration dacs */
279 /* one caldac for every two dac channels */
280 const int max_num_caldacs = 4;
282 /* write 3 bit channel */
283 cb_pcidda_serial_out(dev, channel, num_channel_bits);
284 /* write 8 bit caldac value */
285 cb_pcidda_serial_out(dev, value, num_caldac_bits);
288 * latch stream into appropriate caldac deselect reference dac
290 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
291 /* deactivate caldacs (one caldac for every two channels) */
292 for (i = 0; i < max_num_caldacs; i++)
293 cal2_bits |= DESELECT_CALDAC_BIT(i);
294 /* activate the caldac we want */
295 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
296 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
297 /* deactivate caldac */
298 cal2_bits |= DESELECT_CALDAC_BIT(caldac);
299 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
302 /* returns caldac that calibrates given analog out channel */
303 static unsigned int caldac_number(unsigned int channel)
308 /* returns caldac channel that provides fine gain for given ao channel */
309 static unsigned int fine_gain_channel(unsigned int ao_channel)
311 return 4 * (ao_channel % 2);
314 /* returns caldac channel that provides coarse gain for given ao channel */
315 static unsigned int coarse_gain_channel(unsigned int ao_channel)
317 return 1 + 4 * (ao_channel % 2);
320 /* returns caldac channel that provides coarse offset for given ao channel */
321 static unsigned int coarse_offset_channel(unsigned int ao_channel)
323 return 2 + 4 * (ao_channel % 2);
326 /* returns caldac channel that provides fine offset for given ao channel */
327 static unsigned int fine_offset_channel(unsigned int ao_channel)
329 return 3 + 4 * (ao_channel % 2);
332 /* returns eeprom address that provides offset for given ao channel and range */
333 static unsigned int offset_eeprom_address(unsigned int ao_channel,
336 return 0x7 + 2 * range + 12 * ao_channel;
340 * returns eeprom address that provides gain calibration for given ao
343 static unsigned int gain_eeprom_address(unsigned int ao_channel,
346 return 0x8 + 2 * range + 12 * ao_channel;
350 * returns upper byte of eeprom entry, which gives the coarse adjustment
353 static unsigned int eeprom_coarse_byte(unsigned int word)
355 return (word >> 8) & 0xff;
358 /* returns lower byte of eeprom entry, which gives the fine adjustment values */
359 static unsigned int eeprom_fine_byte(unsigned int word)
364 /* set caldacs to eeprom values for given channel and range */
365 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
368 struct cb_pcidda_private *devpriv = dev->private;
369 unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain;
371 /* remember range so we can tell when we need to readjust calibration */
372 devpriv->ao_range[channel] = range;
374 /* get values from eeprom data */
376 eeprom_coarse_byte(devpriv->eeprom_data
377 [offset_eeprom_address(channel, range)]);
379 eeprom_fine_byte(devpriv->eeprom_data
380 [offset_eeprom_address(channel, range)]);
382 eeprom_coarse_byte(devpriv->eeprom_data
383 [gain_eeprom_address(channel, range)]);
385 eeprom_fine_byte(devpriv->eeprom_data
386 [gain_eeprom_address(channel, range)]);
389 cb_pcidda_write_caldac(dev, caldac_number(channel),
390 coarse_offset_channel(channel), coarse_offset);
391 cb_pcidda_write_caldac(dev, caldac_number(channel),
392 fine_offset_channel(channel), fine_offset);
393 cb_pcidda_write_caldac(dev, caldac_number(channel),
394 coarse_gain_channel(channel), coarse_gain);
395 cb_pcidda_write_caldac(dev, caldac_number(channel),
396 fine_gain_channel(channel), fine_gain);
399 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
400 struct comedi_subdevice *s,
401 struct comedi_insn *insn, unsigned int *data)
403 struct cb_pcidda_private *devpriv = dev->private;
404 unsigned int command;
405 unsigned int channel, range;
407 channel = CR_CHAN(insn->chanspec);
408 range = CR_RANGE(insn->chanspec);
410 /* adjust calibration dacs if range has changed */
411 if (range != devpriv->ao_range[channel])
412 cb_pcidda_calibrate(dev, channel, range);
414 /* output channel configuration */
415 command = NOSU | ENABLEDAC;
417 /* output channel range */
420 command |= BIP | RANGE10V;
423 command |= BIP | RANGE5V;
426 command |= BIP | RANGE2V5;
429 command |= UNIP | RANGE10V;
432 command |= UNIP | RANGE5V;
435 command |= UNIP | RANGE2V5;
439 /* output channel specification */
440 command |= channel << 2;
441 outw(command, dev->iobase + DACONTROL);
444 outw(data[0], dev->iobase + DADATA + channel * 2);
446 /* return the number of samples read/written */
450 static const void *cb_pcidda_find_boardinfo(struct comedi_device *dev,
451 struct pci_dev *pcidev)
453 const struct cb_pcidda_board *thisboard;
456 for (i = 0; i < ARRAY_SIZE(cb_pcidda_boards); i++) {
457 thisboard = &cb_pcidda_boards[i];
458 if (thisboard->device_id != pcidev->device)
464 static int cb_pcidda_attach_pci(struct comedi_device *dev,
465 struct pci_dev *pcidev)
467 const struct cb_pcidda_board *thisboard;
468 struct cb_pcidda_private *devpriv;
469 struct comedi_subdevice *s;
470 unsigned long iobase_8255;
474 thisboard = cb_pcidda_find_boardinfo(dev, pcidev);
477 dev->board_ptr = thisboard;
478 dev->board_name = thisboard->name;
480 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
483 dev->private = devpriv;
485 ret = comedi_pci_enable(pcidev, dev->board_name);
488 dev->iobase = pci_resource_start(pcidev, 3);
489 iobase_8255 = pci_resource_start(pcidev, 2);
491 if (thisboard->status == 2)
493 ("WARNING: DRIVER FOR THIS BOARD NOT CHECKED WITH MANUAL. "
494 "WORKS ASSUMING FULL COMPATIBILITY WITH PCI-DDA08/12. "
495 "PLEASE REPORT USAGE TO <ivanmr@altavista.com>.\n");
497 ret = comedi_alloc_subdevices(dev, 3);
501 s = &dev->subdevices[0];
502 /* analog output subdevice */
503 s->type = COMEDI_SUBD_AO;
504 s->subdev_flags = SDF_WRITABLE;
505 s->n_chan = thisboard->ao_chans;
506 s->maxdata = (1 << thisboard->ao_bits) - 1;
507 s->range_table = &cb_pcidda_ranges;
508 s->insn_write = cb_pcidda_ao_winsn;
510 /* two 8255 digital io subdevices */
511 for (i = 0; i < 2; i++) {
512 s = &dev->subdevices[1 + i];
513 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
518 /* Read the caldac eeprom data */
519 for (i = 0; i < EEPROM_SIZE; i++)
520 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
522 /* set calibrations dacs */
523 for (i = 0; i < thisboard->ao_chans; i++)
524 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
526 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
531 static void cb_pcidda_detach(struct comedi_device *dev)
533 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
535 if (dev->subdevices) {
536 subdev_8255_cleanup(dev, &dev->subdevices[1]);
537 subdev_8255_cleanup(dev, &dev->subdevices[2]);
541 comedi_pci_disable(pcidev);
545 static struct comedi_driver cb_pcidda_driver = {
546 .driver_name = "cb_pcidda",
547 .module = THIS_MODULE,
548 .attach_pci = cb_pcidda_attach_pci,
549 .detach = cb_pcidda_detach,
552 static int __devinit cb_pcidda_pci_probe(struct pci_dev *dev,
553 const struct pci_device_id *ent)
555 return comedi_pci_auto_config(dev, &cb_pcidda_driver);
558 static void __devexit cb_pcidda_pci_remove(struct pci_dev *dev)
560 comedi_pci_auto_unconfig(dev);
563 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
564 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_12) },
565 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_12) },
566 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_12) },
567 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_16) },
568 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_16) },
569 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_16) },
572 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
574 static struct pci_driver cb_pcidda_pci_driver = {
576 .id_table = cb_pcidda_pci_table,
577 .probe = cb_pcidda_pci_probe,
578 .remove = __devexit_p(cb_pcidda_pci_remove),
580 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
582 MODULE_AUTHOR("Comedi http://www.comedi.org");
583 MODULE_DESCRIPTION("Comedi low-level driver");
584 MODULE_LICENSE("GPL");