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.
24 * Description: MeasurementComputing PCI-DDA series
25 * Devices: (Measurement Computing) PCI-DDA08/12 [pci-dda08/12]
26 * (Measurement Computing) PCI-DDA04/12 [pci-dda04/12]
27 * (Measurement Computing) PCI-DDA02/12 [pci-dda02/12]
28 * (Measurement Computing) PCI-DDA08/16 [pci-dda08/16]
29 * (Measurement Computing) PCI-DDA04/16 [pci-dda04/16]
30 * (Measurement Computing) PCI-DDA02/16 [pci-dda02/16]
31 * Author: Ivan Martinez <ivanmr@altavista.com>
32 * Frank Mori Hess <fmhess@users.sourceforge.net>
35 * Configuration options: not applicable, uses PCI auto config
37 * Only simple analog output writing is supported.
40 #include <linux/module.h>
41 #include <linux/pci.h>
43 #include "../comedidev.h"
45 #include "comedi_fc.h"
48 #define EEPROM_SIZE 128 /* number of entries in eeprom */
49 /* maximum number of ao channels for supported boards */
50 #define MAX_AO_CHANNELS 8
52 /* Digital I/O registers */
53 #define CB_DDA_DIO0_8255_BASE 0x00
54 #define CB_DDA_DIO1_8255_BASE 0x04
57 #define CB_DDA_DA_CTRL_REG 0x00 /* D/A Control Register */
58 #define CB_DDA_DA_CTRL_SU (1 << 0) /* Simultaneous update */
59 #define CB_DDA_DA_CTRL_EN (1 << 1) /* Enable specified DAC */
60 #define CB_DDA_DA_CTRL_DAC(x) ((x) << 2) /* Specify DAC channel */
61 #define CB_DDA_DA_CTRL_RANGE2V5 (0 << 6) /* 2.5V range */
62 #define CB_DDA_DA_CTRL_RANGE5V (2 << 6) /* 5V range */
63 #define CB_DDA_DA_CTRL_RANGE10V (3 << 6) /* 10V range */
64 #define CB_DDA_DA_CTRL_UNIP (1 << 8) /* Unipolar range */
66 #define DACALIBRATION1 4 /* D/A CALIBRATION REGISTER 1 */
68 /* serial data input for eeprom, caldacs, reference dac */
69 #define SERIAL_IN_BIT 0x1
70 #define CAL_CHANNEL_MASK (0x7 << 1)
71 #define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK)
73 #define CAL_COUNTER_MASK 0x1f
74 /* calibration counter overflow status bit */
75 #define CAL_COUNTER_OVERFLOW_BIT 0x20
76 /* analog output is less than reference dac voltage */
77 #define AO_BELOW_REF_BIT 0x40
78 #define SERIAL_OUT_BIT 0x80 /* serial data out, for reading from eeprom */
80 #define DACALIBRATION2 6 /* D/A CALIBRATION REGISTER 2 */
81 #define SELECT_EEPROM_BIT 0x1 /* send serial data in to eeprom */
82 /* don't send serial data to MAX542 reference dac */
83 #define DESELECT_REF_DAC_BIT 0x2
84 /* don't send serial data to caldac n */
85 #define DESELECT_CALDAC_BIT(n) (0x4 << (n))
86 /* manual says to set this bit with no explanation */
87 #define DUMMY_BIT 0x40
89 #define CB_DDA_DA_DATA_REG(x) (0x08 + ((x) * 2))
91 /* Offsets for the caldac channels */
92 #define CB_DDA_CALDAC_FINE_GAIN 0
93 #define CB_DDA_CALDAC_COURSE_GAIN 1
94 #define CB_DDA_CALDAC_COURSE_OFFSET 2
95 #define CB_DDA_CALDAC_FINE_OFFSET 3
97 static const struct comedi_lrange cb_pcidda_ranges = {
108 enum cb_pcidda_boardid {
117 struct cb_pcidda_board {
123 static const struct cb_pcidda_board cb_pcidda_boards[] = {
125 .name = "pci-dda02/12",
130 .name = "pci-dda04/12",
135 .name = "pci-dda08/12",
140 .name = "pci-dda02/16",
145 .name = "pci-dda04/16",
150 .name = "pci-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 /* set caldacs to eeprom values for given channel and range */
266 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
269 struct cb_pcidda_private *devpriv = dev->private;
270 unsigned int caldac = channel / 2; /* two caldacs per channel */
271 unsigned int chan = 4 * (channel % 2); /* caldac channel base */
272 unsigned int index = 2 * range + 12 * channel;
276 /* save range so we can tell when we need to readjust calibration */
277 devpriv->ao_range[channel] = range;
279 /* get values from eeprom data */
280 offset = devpriv->eeprom_data[0x7 + index];
281 gain = devpriv->eeprom_data[0x8 + index];
284 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_OFFSET,
285 (offset >> 8) & 0xff);
286 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_OFFSET,
288 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_GAIN,
290 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_GAIN,
294 static int cb_pcidda_ao_insn_write(struct comedi_device *dev,
295 struct comedi_subdevice *s,
296 struct comedi_insn *insn,
299 struct cb_pcidda_private *devpriv = dev->private;
300 unsigned int channel = CR_CHAN(insn->chanspec);
301 unsigned int range = CR_RANGE(insn->chanspec);
304 if (range != devpriv->ao_range[channel])
305 cb_pcidda_calibrate(dev, channel, range);
307 ctrl = CB_DDA_DA_CTRL_EN | CB_DDA_DA_CTRL_DAC(channel);
312 ctrl |= CB_DDA_DA_CTRL_RANGE10V;
316 ctrl |= CB_DDA_DA_CTRL_RANGE5V;
320 ctrl |= CB_DDA_DA_CTRL_RANGE2V5;
325 ctrl |= CB_DDA_DA_CTRL_UNIP;
327 outw(ctrl, dev->iobase + CB_DDA_DA_CTRL_REG);
329 outw(data[0], dev->iobase + CB_DDA_DA_DATA_REG(channel));
334 static int cb_pcidda_auto_attach(struct comedi_device *dev,
335 unsigned long context)
337 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
338 const struct cb_pcidda_board *thisboard = NULL;
339 struct cb_pcidda_private *devpriv;
340 struct comedi_subdevice *s;
341 unsigned long iobase_8255;
345 if (context < ARRAY_SIZE(cb_pcidda_boards))
346 thisboard = &cb_pcidda_boards[context];
349 dev->board_ptr = thisboard;
350 dev->board_name = thisboard->name;
352 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
356 ret = comedi_pci_enable(dev);
359 dev->iobase = pci_resource_start(pcidev, 3);
360 iobase_8255 = pci_resource_start(pcidev, 2);
362 ret = comedi_alloc_subdevices(dev, 3);
366 s = &dev->subdevices[0];
367 /* analog output subdevice */
368 s->type = COMEDI_SUBD_AO;
369 s->subdev_flags = SDF_WRITABLE;
370 s->n_chan = thisboard->ao_chans;
371 s->maxdata = (1 << thisboard->ao_bits) - 1;
372 s->range_table = &cb_pcidda_ranges;
373 s->insn_write = cb_pcidda_ao_insn_write;
375 /* two 8255 digital io subdevices */
376 for (i = 0; i < 2; i++) {
377 s = &dev->subdevices[1 + i];
378 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
383 /* Read the caldac eeprom data */
384 for (i = 0; i < EEPROM_SIZE; i++)
385 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
387 /* set calibrations dacs */
388 for (i = 0; i < thisboard->ao_chans; i++)
389 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
391 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
396 static struct comedi_driver cb_pcidda_driver = {
397 .driver_name = "cb_pcidda",
398 .module = THIS_MODULE,
399 .auto_attach = cb_pcidda_auto_attach,
400 .detach = comedi_pci_disable,
403 static int cb_pcidda_pci_probe(struct pci_dev *dev,
404 const struct pci_device_id *id)
406 return comedi_pci_auto_config(dev, &cb_pcidda_driver,
410 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
411 { PCI_VDEVICE(CB, 0x0020), BOARD_DDA02_12 },
412 { PCI_VDEVICE(CB, 0x0021), BOARD_DDA04_12 },
413 { PCI_VDEVICE(CB, 0x0022), BOARD_DDA08_12 },
414 { PCI_VDEVICE(CB, 0x0023), BOARD_DDA02_16 },
415 { PCI_VDEVICE(CB, 0x0024), BOARD_DDA04_16 },
416 { PCI_VDEVICE(CB, 0x0025), BOARD_DDA08_16 },
419 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
421 static struct pci_driver cb_pcidda_pci_driver = {
423 .id_table = cb_pcidda_pci_table,
424 .probe = cb_pcidda_pci_probe,
425 .remove = comedi_pci_auto_unconfig,
427 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
429 MODULE_AUTHOR("Comedi http://www.comedi.org");
430 MODULE_DESCRIPTION("Comedi low-level driver");
431 MODULE_LICENSE("GPL");