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 CB_DDA_DA_CTRL_REG 0x00 /* D/A Control Register */
69 #define CB_DDA_DA_CTRL_SU (1 << 0) /* Simultaneous update */
70 #define CB_DDA_DA_CTRL_EN (1 << 1) /* Enable specified DAC */
71 #define CB_DDA_DA_CTRL_DAC(x) ((x) << 2) /* Specify DAC channel */
72 #define CB_DDA_DA_CTRL_RANGE2V5 (0 << 6) /* 2.5V range */
73 #define CB_DDA_DA_CTRL_RANGE5V (2 << 6) /* 5V range */
74 #define CB_DDA_DA_CTRL_RANGE10V (3 << 6) /* 10V range */
75 #define CB_DDA_DA_CTRL_UNIP (1 << 8) /* Unipolar range */
77 #define DACALIBRATION1 4 /* D/A CALIBRATION REGISTER 1 */
79 /* serial data input for eeprom, caldacs, reference dac */
80 #define SERIAL_IN_BIT 0x1
81 #define CAL_CHANNEL_MASK (0x7 << 1)
82 #define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK)
84 #define CAL_COUNTER_MASK 0x1f
85 /* calibration counter overflow status bit */
86 #define CAL_COUNTER_OVERFLOW_BIT 0x20
87 /* analog output is less than reference dac voltage */
88 #define AO_BELOW_REF_BIT 0x40
89 #define SERIAL_OUT_BIT 0x80 /* serial data out, for reading from eeprom */
91 #define DACALIBRATION2 6 /* D/A CALIBRATION REGISTER 2 */
92 #define SELECT_EEPROM_BIT 0x1 /* send serial data in to eeprom */
93 /* don't send serial data to MAX542 reference dac */
94 #define DESELECT_REF_DAC_BIT 0x2
95 /* don't send serial data to caldac n */
96 #define DESELECT_CALDAC_BIT(n) (0x4 << (n))
97 /* manual says to set this bit with no explanation */
98 #define DUMMY_BIT 0x40
100 #define DADATA 8 /* FIRST D/A DATA REGISTER (0) */
102 static const struct comedi_lrange cb_pcidda_ranges = {
113 struct cb_pcidda_board {
115 unsigned short device_id;
120 static const struct cb_pcidda_board cb_pcidda_boards[] = {
122 .name = "pci-dda02/12",
123 .device_id = PCI_DEVICE_ID_DDA02_12,
127 .name = "pci-dda04/12",
128 .device_id = PCI_DEVICE_ID_DDA04_12,
132 .name = "pci-dda08/12",
133 .device_id = PCI_DEVICE_ID_DDA08_12,
137 .name = "pci-dda02/16",
138 .device_id = PCI_DEVICE_ID_DDA02_16,
142 .name = "pci-dda04/16",
143 .device_id = PCI_DEVICE_ID_DDA04_16,
147 .name = "pci-dda08/16",
148 .device_id = PCI_DEVICE_ID_DDA08_16,
154 struct cb_pcidda_private {
155 /* bits last written to da calibration register 1 */
156 unsigned int dac_cal1_bits;
157 /* current range settings for output channels */
158 unsigned int ao_range[MAX_AO_CHANNELS];
159 u16 eeprom_data[EEPROM_SIZE]; /* software copy of board's eeprom */
162 /* lowlevel read from eeprom */
163 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
165 unsigned int value = 0;
167 const int value_width = 16; /* number of bits wide values are */
169 for (i = 1; i <= value_width; i++) {
170 /* read bits most significant bit first */
171 if (inw_p(dev->iobase + DACALIBRATION1) & SERIAL_OUT_BIT)
172 value |= 1 << (value_width - i);
178 /* lowlevel write to eeprom/dac */
179 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
180 unsigned int num_bits)
182 struct cb_pcidda_private *devpriv = dev->private;
185 for (i = 1; i <= num_bits; i++) {
186 /* send bits most significant bit first */
187 if (value & (1 << (num_bits - i)))
188 devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
190 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
191 outw_p(devpriv->dac_cal1_bits, dev->iobase + DACALIBRATION1);
195 /* reads a 16 bit value from board's eeprom */
196 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
197 unsigned int address)
200 unsigned int cal2_bits;
202 /* one caldac for every two dac channels */
203 const int max_num_caldacs = 4;
204 /* bits to send to tell eeprom we want to read */
205 const int read_instruction = 0x6;
206 const int instruction_length = 3;
207 const int address_length = 8;
209 /* send serial output stream to eeprom */
210 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
211 /* deactivate caldacs (one caldac for every two channels) */
212 for (i = 0; i < max_num_caldacs; i++)
213 cal2_bits |= DESELECT_CALDAC_BIT(i);
214 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
216 /* tell eeprom we want to read */
217 cb_pcidda_serial_out(dev, read_instruction, instruction_length);
218 /* send address we want to read from */
219 cb_pcidda_serial_out(dev, address, address_length);
221 value = cb_pcidda_serial_in(dev);
223 /* deactivate eeprom */
224 cal2_bits &= ~SELECT_EEPROM_BIT;
225 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
230 /* writes to 8 bit calibration dacs */
231 static void cb_pcidda_write_caldac(struct comedi_device *dev,
232 unsigned int caldac, unsigned int channel,
235 unsigned int cal2_bits;
237 /* caldacs use 3 bit channel specification */
238 const int num_channel_bits = 3;
239 const int num_caldac_bits = 8; /* 8 bit calibration dacs */
240 /* one caldac for every two dac channels */
241 const int max_num_caldacs = 4;
243 /* write 3 bit channel */
244 cb_pcidda_serial_out(dev, channel, num_channel_bits);
245 /* write 8 bit caldac value */
246 cb_pcidda_serial_out(dev, value, num_caldac_bits);
249 * latch stream into appropriate caldac deselect reference dac
251 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
252 /* deactivate caldacs (one caldac for every two channels) */
253 for (i = 0; i < max_num_caldacs; i++)
254 cal2_bits |= DESELECT_CALDAC_BIT(i);
255 /* activate the caldac we want */
256 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
257 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
258 /* deactivate caldac */
259 cal2_bits |= DESELECT_CALDAC_BIT(caldac);
260 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
263 /* returns caldac that calibrates given analog out channel */
264 static unsigned int caldac_number(unsigned int channel)
269 /* returns caldac channel that provides fine gain for given ao channel */
270 static unsigned int fine_gain_channel(unsigned int ao_channel)
272 return 4 * (ao_channel % 2);
275 /* returns caldac channel that provides coarse gain for given ao channel */
276 static unsigned int coarse_gain_channel(unsigned int ao_channel)
278 return 1 + 4 * (ao_channel % 2);
281 /* returns caldac channel that provides coarse offset for given ao channel */
282 static unsigned int coarse_offset_channel(unsigned int ao_channel)
284 return 2 + 4 * (ao_channel % 2);
287 /* returns caldac channel that provides fine offset for given ao channel */
288 static unsigned int fine_offset_channel(unsigned int ao_channel)
290 return 3 + 4 * (ao_channel % 2);
293 /* returns eeprom address that provides offset for given ao channel and range */
294 static unsigned int offset_eeprom_address(unsigned int ao_channel,
297 return 0x7 + 2 * range + 12 * ao_channel;
301 * returns eeprom address that provides gain calibration for given ao
304 static unsigned int gain_eeprom_address(unsigned int ao_channel,
307 return 0x8 + 2 * range + 12 * ao_channel;
311 * returns upper byte of eeprom entry, which gives the coarse adjustment
314 static unsigned int eeprom_coarse_byte(unsigned int word)
316 return (word >> 8) & 0xff;
319 /* returns lower byte of eeprom entry, which gives the fine adjustment values */
320 static unsigned int eeprom_fine_byte(unsigned int word)
325 /* set caldacs to eeprom values for given channel and range */
326 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
329 struct cb_pcidda_private *devpriv = dev->private;
330 unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain;
332 /* remember range so we can tell when we need to readjust calibration */
333 devpriv->ao_range[channel] = range;
335 /* get values from eeprom data */
337 eeprom_coarse_byte(devpriv->eeprom_data
338 [offset_eeprom_address(channel, range)]);
340 eeprom_fine_byte(devpriv->eeprom_data
341 [offset_eeprom_address(channel, range)]);
343 eeprom_coarse_byte(devpriv->eeprom_data
344 [gain_eeprom_address(channel, range)]);
346 eeprom_fine_byte(devpriv->eeprom_data
347 [gain_eeprom_address(channel, range)]);
350 cb_pcidda_write_caldac(dev, caldac_number(channel),
351 coarse_offset_channel(channel), coarse_offset);
352 cb_pcidda_write_caldac(dev, caldac_number(channel),
353 fine_offset_channel(channel), fine_offset);
354 cb_pcidda_write_caldac(dev, caldac_number(channel),
355 coarse_gain_channel(channel), coarse_gain);
356 cb_pcidda_write_caldac(dev, caldac_number(channel),
357 fine_gain_channel(channel), fine_gain);
360 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
361 struct comedi_subdevice *s,
362 struct comedi_insn *insn, unsigned int *data)
364 struct cb_pcidda_private *devpriv = dev->private;
365 unsigned int channel = CR_CHAN(insn->chanspec);
366 unsigned int range = CR_RANGE(insn->chanspec);
369 /* adjust calibration dacs if range has changed */
370 if (range != devpriv->ao_range[channel])
371 cb_pcidda_calibrate(dev, channel, range);
373 ctrl = CB_DDA_DA_CTRL_EN | CB_DDA_DA_CTRL_DAC(channel);
378 ctrl |= CB_DDA_DA_CTRL_RANGE10V;
382 ctrl |= CB_DDA_DA_CTRL_RANGE5V;
386 ctrl |= CB_DDA_DA_CTRL_RANGE2V5;
391 ctrl |= CB_DDA_DA_CTRL_UNIP;
393 outw(ctrl, dev->iobase + CB_DDA_DA_CTRL_REG);
396 outw(data[0], dev->iobase + DADATA + channel * 2);
398 /* return the number of samples read/written */
402 static const void *cb_pcidda_find_boardinfo(struct comedi_device *dev,
403 struct pci_dev *pcidev)
405 const struct cb_pcidda_board *thisboard;
408 for (i = 0; i < ARRAY_SIZE(cb_pcidda_boards); i++) {
409 thisboard = &cb_pcidda_boards[i];
410 if (thisboard->device_id != pcidev->device)
416 static int cb_pcidda_attach_pci(struct comedi_device *dev,
417 struct pci_dev *pcidev)
419 const struct cb_pcidda_board *thisboard;
420 struct cb_pcidda_private *devpriv;
421 struct comedi_subdevice *s;
422 unsigned long iobase_8255;
426 thisboard = cb_pcidda_find_boardinfo(dev, pcidev);
429 dev->board_ptr = thisboard;
430 dev->board_name = thisboard->name;
432 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
435 dev->private = devpriv;
437 ret = comedi_pci_enable(pcidev, dev->board_name);
440 dev->iobase = pci_resource_start(pcidev, 3);
441 iobase_8255 = pci_resource_start(pcidev, 2);
443 ret = comedi_alloc_subdevices(dev, 3);
447 s = &dev->subdevices[0];
448 /* analog output subdevice */
449 s->type = COMEDI_SUBD_AO;
450 s->subdev_flags = SDF_WRITABLE;
451 s->n_chan = thisboard->ao_chans;
452 s->maxdata = (1 << thisboard->ao_bits) - 1;
453 s->range_table = &cb_pcidda_ranges;
454 s->insn_write = cb_pcidda_ao_winsn;
456 /* two 8255 digital io subdevices */
457 for (i = 0; i < 2; i++) {
458 s = &dev->subdevices[1 + i];
459 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
464 /* Read the caldac eeprom data */
465 for (i = 0; i < EEPROM_SIZE; i++)
466 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
468 /* set calibrations dacs */
469 for (i = 0; i < thisboard->ao_chans; i++)
470 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
472 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
477 static void cb_pcidda_detach(struct comedi_device *dev)
479 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
481 if (dev->subdevices) {
482 subdev_8255_cleanup(dev, &dev->subdevices[1]);
483 subdev_8255_cleanup(dev, &dev->subdevices[2]);
487 comedi_pci_disable(pcidev);
491 static struct comedi_driver cb_pcidda_driver = {
492 .driver_name = "cb_pcidda",
493 .module = THIS_MODULE,
494 .attach_pci = cb_pcidda_attach_pci,
495 .detach = cb_pcidda_detach,
498 static int __devinit cb_pcidda_pci_probe(struct pci_dev *dev,
499 const struct pci_device_id *ent)
501 return comedi_pci_auto_config(dev, &cb_pcidda_driver);
504 static void __devexit cb_pcidda_pci_remove(struct pci_dev *dev)
506 comedi_pci_auto_unconfig(dev);
509 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
510 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_12) },
511 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_12) },
512 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_12) },
513 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_16) },
514 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_16) },
515 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_16) },
518 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
520 static struct pci_driver cb_pcidda_pci_driver = {
522 .id_table = cb_pcidda_pci_table,
523 .probe = cb_pcidda_pci_probe,
524 .remove = __devexit_p(cb_pcidda_pci_remove),
526 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
528 MODULE_AUTHOR("Comedi http://www.comedi.org");
529 MODULE_DESCRIPTION("Comedi low-level driver");
530 MODULE_LICENSE("GPL");