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 CB_DDA_DIO0_8255_BASE 0x00
65 #define CB_DDA_DIO1_8255_BASE 0x04
68 #define DACONTROL 0 /* D/A CONTROL REGISTER */
69 #define SU 0000001 /* Simultaneous update enabled */
70 #define NOSU 0000000 /* Simultaneous update disabled */
71 #define ENABLEDAC 0000002 /* Enable specified DAC */
72 #define DISABLEDAC 0000000 /* Disable specified DAC */
73 #define RANGE2V5 0000000 /* 2.5V */
74 #define RANGE5V 0000200 /* 5V */
75 #define RANGE10V 0000300 /* 10V */
76 #define UNIP 0000400 /* Unipolar outputs */
77 #define BIP 0000000 /* Bipolar outputs */
79 #define DACALIBRATION1 4 /* D/A CALIBRATION REGISTER 1 */
81 /* serial data input for eeprom, caldacs, reference dac */
82 #define SERIAL_IN_BIT 0x1
83 #define CAL_CHANNEL_MASK (0x7 << 1)
84 #define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK)
86 #define CAL_COUNTER_MASK 0x1f
87 /* calibration counter overflow status bit */
88 #define CAL_COUNTER_OVERFLOW_BIT 0x20
89 /* analog output is less than reference dac voltage */
90 #define AO_BELOW_REF_BIT 0x40
91 #define SERIAL_OUT_BIT 0x80 /* serial data out, for reading from eeprom */
93 #define DACALIBRATION2 6 /* D/A CALIBRATION REGISTER 2 */
94 #define SELECT_EEPROM_BIT 0x1 /* send serial data in to eeprom */
95 /* don't send serial data to MAX542 reference dac */
96 #define DESELECT_REF_DAC_BIT 0x2
97 /* don't send serial data to caldac n */
98 #define DESELECT_CALDAC_BIT(n) (0x4 << (n))
99 /* manual says to set this bit with no explanation */
100 #define DUMMY_BIT 0x40
102 #define DADATA 8 /* FIRST D/A DATA REGISTER (0) */
104 static const struct comedi_lrange cb_pcidda_ranges = {
115 struct cb_pcidda_board {
117 unsigned short device_id;
122 static const struct cb_pcidda_board cb_pcidda_boards[] = {
124 .name = "pci-dda02/12",
125 .device_id = PCI_DEVICE_ID_DDA02_12,
129 .name = "pci-dda04/12",
130 .device_id = PCI_DEVICE_ID_DDA04_12,
134 .name = "pci-dda08/12",
135 .device_id = PCI_DEVICE_ID_DDA08_12,
139 .name = "pci-dda02/16",
140 .device_id = PCI_DEVICE_ID_DDA02_16,
144 .name = "pci-dda04/16",
145 .device_id = PCI_DEVICE_ID_DDA04_16,
149 .name = "pci-dda08/16",
150 .device_id = PCI_DEVICE_ID_DDA08_16,
156 struct cb_pcidda_private {
157 /* bits last written to da calibration register 1 */
158 unsigned int dac_cal1_bits;
159 /* current range settings for output channels */
160 unsigned int ao_range[MAX_AO_CHANNELS];
161 u16 eeprom_data[EEPROM_SIZE]; /* software copy of board's eeprom */
164 /* lowlevel read from eeprom */
165 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
167 unsigned int value = 0;
169 const int value_width = 16; /* number of bits wide values are */
171 for (i = 1; i <= value_width; i++) {
172 /* read bits most significant bit first */
173 if (inw_p(dev->iobase + DACALIBRATION1) & SERIAL_OUT_BIT)
174 value |= 1 << (value_width - i);
180 /* lowlevel write to eeprom/dac */
181 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
182 unsigned int num_bits)
184 struct cb_pcidda_private *devpriv = dev->private;
187 for (i = 1; i <= num_bits; i++) {
188 /* send bits most significant bit first */
189 if (value & (1 << (num_bits - i)))
190 devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
192 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
193 outw_p(devpriv->dac_cal1_bits, dev->iobase + DACALIBRATION1);
197 /* reads a 16 bit value from board's eeprom */
198 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
199 unsigned int address)
202 unsigned int cal2_bits;
204 /* one caldac for every two dac channels */
205 const int max_num_caldacs = 4;
206 /* bits to send to tell eeprom we want to read */
207 const int read_instruction = 0x6;
208 const int instruction_length = 3;
209 const int address_length = 8;
211 /* send serial output stream to eeprom */
212 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
213 /* deactivate caldacs (one caldac for every two channels) */
214 for (i = 0; i < max_num_caldacs; i++)
215 cal2_bits |= DESELECT_CALDAC_BIT(i);
216 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
218 /* tell eeprom we want to read */
219 cb_pcidda_serial_out(dev, read_instruction, instruction_length);
220 /* send address we want to read from */
221 cb_pcidda_serial_out(dev, address, address_length);
223 value = cb_pcidda_serial_in(dev);
225 /* deactivate eeprom */
226 cal2_bits &= ~SELECT_EEPROM_BIT;
227 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
232 /* writes to 8 bit calibration dacs */
233 static void cb_pcidda_write_caldac(struct comedi_device *dev,
234 unsigned int caldac, unsigned int channel,
237 unsigned int cal2_bits;
239 /* caldacs use 3 bit channel specification */
240 const int num_channel_bits = 3;
241 const int num_caldac_bits = 8; /* 8 bit calibration dacs */
242 /* one caldac for every two dac channels */
243 const int max_num_caldacs = 4;
245 /* write 3 bit channel */
246 cb_pcidda_serial_out(dev, channel, num_channel_bits);
247 /* write 8 bit caldac value */
248 cb_pcidda_serial_out(dev, value, num_caldac_bits);
251 * latch stream into appropriate caldac deselect reference dac
253 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
254 /* deactivate caldacs (one caldac for every two channels) */
255 for (i = 0; i < max_num_caldacs; i++)
256 cal2_bits |= DESELECT_CALDAC_BIT(i);
257 /* activate the caldac we want */
258 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
259 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
260 /* deactivate caldac */
261 cal2_bits |= DESELECT_CALDAC_BIT(caldac);
262 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
265 /* returns caldac that calibrates given analog out channel */
266 static unsigned int caldac_number(unsigned int channel)
271 /* returns caldac channel that provides fine gain for given ao channel */
272 static unsigned int fine_gain_channel(unsigned int ao_channel)
274 return 4 * (ao_channel % 2);
277 /* returns caldac channel that provides coarse gain for given ao channel */
278 static unsigned int coarse_gain_channel(unsigned int ao_channel)
280 return 1 + 4 * (ao_channel % 2);
283 /* returns caldac channel that provides coarse offset for given ao channel */
284 static unsigned int coarse_offset_channel(unsigned int ao_channel)
286 return 2 + 4 * (ao_channel % 2);
289 /* returns caldac channel that provides fine offset for given ao channel */
290 static unsigned int fine_offset_channel(unsigned int ao_channel)
292 return 3 + 4 * (ao_channel % 2);
295 /* returns eeprom address that provides offset for given ao channel and range */
296 static unsigned int offset_eeprom_address(unsigned int ao_channel,
299 return 0x7 + 2 * range + 12 * ao_channel;
303 * returns eeprom address that provides gain calibration for given ao
306 static unsigned int gain_eeprom_address(unsigned int ao_channel,
309 return 0x8 + 2 * range + 12 * ao_channel;
313 * returns upper byte of eeprom entry, which gives the coarse adjustment
316 static unsigned int eeprom_coarse_byte(unsigned int word)
318 return (word >> 8) & 0xff;
321 /* returns lower byte of eeprom entry, which gives the fine adjustment values */
322 static unsigned int eeprom_fine_byte(unsigned int word)
327 /* set caldacs to eeprom values for given channel and range */
328 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
331 struct cb_pcidda_private *devpriv = dev->private;
332 unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain;
334 /* remember range so we can tell when we need to readjust calibration */
335 devpriv->ao_range[channel] = range;
337 /* get values from eeprom data */
339 eeprom_coarse_byte(devpriv->eeprom_data
340 [offset_eeprom_address(channel, range)]);
342 eeprom_fine_byte(devpriv->eeprom_data
343 [offset_eeprom_address(channel, range)]);
345 eeprom_coarse_byte(devpriv->eeprom_data
346 [gain_eeprom_address(channel, range)]);
348 eeprom_fine_byte(devpriv->eeprom_data
349 [gain_eeprom_address(channel, range)]);
352 cb_pcidda_write_caldac(dev, caldac_number(channel),
353 coarse_offset_channel(channel), coarse_offset);
354 cb_pcidda_write_caldac(dev, caldac_number(channel),
355 fine_offset_channel(channel), fine_offset);
356 cb_pcidda_write_caldac(dev, caldac_number(channel),
357 coarse_gain_channel(channel), coarse_gain);
358 cb_pcidda_write_caldac(dev, caldac_number(channel),
359 fine_gain_channel(channel), fine_gain);
362 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
363 struct comedi_subdevice *s,
364 struct comedi_insn *insn, unsigned int *data)
366 struct cb_pcidda_private *devpriv = dev->private;
367 unsigned int command;
368 unsigned int channel, range;
370 channel = CR_CHAN(insn->chanspec);
371 range = CR_RANGE(insn->chanspec);
373 /* adjust calibration dacs if range has changed */
374 if (range != devpriv->ao_range[channel])
375 cb_pcidda_calibrate(dev, channel, range);
377 /* output channel configuration */
378 command = NOSU | ENABLEDAC;
380 /* output channel range */
383 command |= BIP | RANGE10V;
386 command |= BIP | RANGE5V;
389 command |= BIP | RANGE2V5;
392 command |= UNIP | RANGE10V;
395 command |= UNIP | RANGE5V;
398 command |= UNIP | RANGE2V5;
402 /* output channel specification */
403 command |= channel << 2;
404 outw(command, dev->iobase + DACONTROL);
407 outw(data[0], dev->iobase + DADATA + channel * 2);
409 /* return the number of samples read/written */
413 static const void *cb_pcidda_find_boardinfo(struct comedi_device *dev,
414 struct pci_dev *pcidev)
416 const struct cb_pcidda_board *thisboard;
419 for (i = 0; i < ARRAY_SIZE(cb_pcidda_boards); i++) {
420 thisboard = &cb_pcidda_boards[i];
421 if (thisboard->device_id != pcidev->device)
427 static int cb_pcidda_attach_pci(struct comedi_device *dev,
428 struct pci_dev *pcidev)
430 const struct cb_pcidda_board *thisboard;
431 struct cb_pcidda_private *devpriv;
432 struct comedi_subdevice *s;
433 unsigned long iobase_8255;
437 thisboard = cb_pcidda_find_boardinfo(dev, pcidev);
440 dev->board_ptr = thisboard;
441 dev->board_name = thisboard->name;
443 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
446 dev->private = devpriv;
448 ret = comedi_pci_enable(pcidev, dev->board_name);
451 dev->iobase = pci_resource_start(pcidev, 3);
452 iobase_8255 = pci_resource_start(pcidev, 2);
454 ret = comedi_alloc_subdevices(dev, 3);
458 s = &dev->subdevices[0];
459 /* analog output subdevice */
460 s->type = COMEDI_SUBD_AO;
461 s->subdev_flags = SDF_WRITABLE;
462 s->n_chan = thisboard->ao_chans;
463 s->maxdata = (1 << thisboard->ao_bits) - 1;
464 s->range_table = &cb_pcidda_ranges;
465 s->insn_write = cb_pcidda_ao_winsn;
467 /* two 8255 digital io subdevices */
468 for (i = 0; i < 2; i++) {
469 s = &dev->subdevices[1 + i];
470 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
475 /* Read the caldac eeprom data */
476 for (i = 0; i < EEPROM_SIZE; i++)
477 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
479 /* set calibrations dacs */
480 for (i = 0; i < thisboard->ao_chans; i++)
481 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
483 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
488 static void cb_pcidda_detach(struct comedi_device *dev)
490 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
492 if (dev->subdevices) {
493 subdev_8255_cleanup(dev, &dev->subdevices[1]);
494 subdev_8255_cleanup(dev, &dev->subdevices[2]);
498 comedi_pci_disable(pcidev);
502 static struct comedi_driver cb_pcidda_driver = {
503 .driver_name = "cb_pcidda",
504 .module = THIS_MODULE,
505 .attach_pci = cb_pcidda_attach_pci,
506 .detach = cb_pcidda_detach,
509 static int __devinit cb_pcidda_pci_probe(struct pci_dev *dev,
510 const struct pci_device_id *ent)
512 return comedi_pci_auto_config(dev, &cb_pcidda_driver);
515 static void __devexit cb_pcidda_pci_remove(struct pci_dev *dev)
517 comedi_pci_auto_unconfig(dev);
520 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
521 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_12) },
522 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_12) },
523 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_12) },
524 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_16) },
525 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_16) },
526 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_16) },
529 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
531 static struct pci_driver cb_pcidda_pci_driver = {
533 .id_table = cb_pcidda_pci_table,
534 .probe = cb_pcidda_pci_probe,
535 .remove = __devexit_p(cb_pcidda_pci_remove),
537 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
539 MODULE_AUTHOR("Comedi http://www.comedi.org");
540 MODULE_DESCRIPTION("Comedi low-level driver");
541 MODULE_LICENSE("GPL");