2 comedi/drivers/cb_pcidda.c
3 This intends to be a driver for the ComputerBoards / MeasurementComputing
6 Copyright (C) 2001 Ivan Martinez <ivanmr@altavista.com>
7 Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
9 COMEDI - Linux Control and Measurement Device Interface
10 Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 Description: MeasurementComputing PCI-DDA series
30 Author: Ivan Martinez <ivanmr@altavista.com>, Frank Mori Hess <fmhess@users.sourceforge.net>
31 Status: Supports 08/16, 04/16, 02/16, 08/12, 04/12, and 02/12
32 Devices: [Measurement Computing] PCI-DDA08/12 (cb_pcidda), PCI-DDA04/12,
33 PCI-DDA02/12, PCI-DDA08/16, PCI-DDA04/16, PCI-DDA02/16
35 Configuration options:
36 [0] - PCI bus of device (optional)
37 [1] - PCI slot of device (optional)
38 If bus/slot is not specified, the first available PCI
41 Only simple analog output writing is supported.
43 So far it has only been tested with:
45 Please report success/failure with other different cards to
49 #include "../comedidev.h"
51 #include "comedi_fc.h"
55 * ComputerBoards PCI Device ID's supported by this driver
57 #define PCI_VENDOR_ID_CB 0x1307
58 #define PCI_DEVICE_ID_DDA02_12 0x0020
59 #define PCI_DEVICE_ID_DDA04_12 0x0021
60 #define PCI_DEVICE_ID_DDA08_12 0x0022
61 #define PCI_DEVICE_ID_DDA02_16 0x0023
62 #define PCI_DEVICE_ID_DDA04_16 0x0024
63 #define PCI_DEVICE_ID_DDA08_16 0x0025
65 #define EEPROM_SIZE 128 /* number of entries in eeprom */
66 /* maximum number of ao channels for supported boards */
67 #define MAX_AO_CHANNELS 8
69 /* Digital I/O registers */
70 #define PORT1A 0 /* PORT 1A DATA */
72 #define PORT1B 1 /* PORT 1B DATA */
74 #define PORT1C 2 /* PORT 1C DATA */
76 #define CONTROL1 3 /* CONTROL REGISTER 1 */
78 #define PORT2A 4 /* PORT 2A DATA */
80 #define PORT2B 5 /* PORT 2B DATA */
82 #define PORT2C 6 /* PORT 2C DATA */
84 #define CONTROL2 7 /* CONTROL REGISTER 2 */
87 #define DACONTROL 0 /* D/A CONTROL REGISTER */
88 #define SU 0000001 /* Simultaneous update enabled */
89 #define NOSU 0000000 /* Simultaneous update disabled */
90 #define ENABLEDAC 0000002 /* Enable specified DAC */
91 #define DISABLEDAC 0000000 /* Disable specified DAC */
92 #define RANGE2V5 0000000 /* 2.5V */
93 #define RANGE5V 0000200 /* 5V */
94 #define RANGE10V 0000300 /* 10V */
95 #define UNIP 0000400 /* Unipolar outputs */
96 #define BIP 0000000 /* Bipolar outputs */
98 #define DACALIBRATION1 4 /* D/A CALIBRATION REGISTER 1 */
100 /* serial data input for eeprom, caldacs, reference dac */
101 #define SERIAL_IN_BIT 0x1
102 #define CAL_CHANNEL_MASK (0x7 << 1)
103 #define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK)
105 #define CAL_COUNTER_MASK 0x1f
106 /* calibration counter overflow status bit */
107 #define CAL_COUNTER_OVERFLOW_BIT 0x20
108 /* analog output is less than reference dac voltage */
109 #define AO_BELOW_REF_BIT 0x40
110 #define SERIAL_OUT_BIT 0x80 /* serial data out, for reading from eeprom */
112 #define DACALIBRATION2 6 /* D/A CALIBRATION REGISTER 2 */
113 #define SELECT_EEPROM_BIT 0x1 /* send serial data in to eeprom */
114 /* don't send serial data to MAX542 reference dac */
115 #define DESELECT_REF_DAC_BIT 0x2
116 /* don't send serial data to caldac n */
117 #define DESELECT_CALDAC_BIT(n) (0x4 << (n))
118 /* manual says to set this bit with no explanation */
119 #define DUMMY_BIT 0x40
121 #define DADATA 8 /* FIRST D/A DATA REGISTER (0) */
123 static const struct comedi_lrange cb_pcidda_ranges = {
136 * Board descriptions for two imaginary boards. Describing the
137 * boards in this way is optional, and completely driver-dependent.
138 * Some drivers use arrays such as this, other do not.
140 struct cb_pcidda_board {
142 char status; /* Driver status: */
146 * 1 - manual read, not tested
147 * 2 - manual not read
150 unsigned short device_id;
153 const struct comedi_lrange *ranges;
156 static const struct cb_pcidda_board cb_pcidda_boards[] = {
158 .name = "pci-dda02/12",
160 .device_id = PCI_DEVICE_ID_DDA02_12,
163 .ranges = &cb_pcidda_ranges,
166 .name = "pci-dda04/12",
168 .device_id = PCI_DEVICE_ID_DDA04_12,
171 .ranges = &cb_pcidda_ranges,
174 .name = "pci-dda08/12",
176 .device_id = PCI_DEVICE_ID_DDA08_12,
179 .ranges = &cb_pcidda_ranges,
182 .name = "pci-dda02/16",
184 .device_id = PCI_DEVICE_ID_DDA02_16,
187 .ranges = &cb_pcidda_ranges,
190 .name = "pci-dda04/16",
192 .device_id = PCI_DEVICE_ID_DDA04_16,
195 .ranges = &cb_pcidda_ranges,
198 .name = "pci-dda08/16",
200 .device_id = PCI_DEVICE_ID_DDA08_16,
203 .ranges = &cb_pcidda_ranges,
208 * this structure is for data unique to this hardware driver. If
209 * several hardware drivers keep similar information in this structure,
210 * feel free to suggest moving the variable to the struct comedi_device
213 struct cb_pcidda_private {
216 unsigned long digitalio;
219 /* unsigned long control_status; */
220 /* unsigned long adc_fifo; */
222 /* bits last written to da calibration register 1 */
223 unsigned int dac_cal1_bits;
224 /* current range settings for output channels */
225 unsigned int ao_range[MAX_AO_CHANNELS];
226 u16 eeprom_data[EEPROM_SIZE]; /* software copy of board's eeprom */
229 /* lowlevel read from eeprom */
230 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
232 struct cb_pcidda_private *devpriv = dev->private;
233 unsigned int value = 0;
235 const int value_width = 16; /* number of bits wide values are */
237 for (i = 1; i <= value_width; i++) {
238 /* read bits most significant bit first */
239 if (inw_p(devpriv->dac + DACALIBRATION1) & SERIAL_OUT_BIT)
240 value |= 1 << (value_width - i);
246 /* lowlevel write to eeprom/dac */
247 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
248 unsigned int num_bits)
250 struct cb_pcidda_private *devpriv = dev->private;
253 for (i = 1; i <= num_bits; i++) {
254 /* send bits most significant bit first */
255 if (value & (1 << (num_bits - i)))
256 devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
258 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
259 outw_p(devpriv->dac_cal1_bits, devpriv->dac + DACALIBRATION1);
263 /* reads a 16 bit value from board's eeprom */
264 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
265 unsigned int address)
267 struct cb_pcidda_private *devpriv = dev->private;
269 unsigned int cal2_bits;
271 /* one caldac for every two dac channels */
272 const int max_num_caldacs = 4;
273 /* bits to send to tell eeprom we want to read */
274 const int read_instruction = 0x6;
275 const int instruction_length = 3;
276 const int address_length = 8;
278 /* send serial output stream to eeprom */
279 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
280 /* deactivate caldacs (one caldac for every two channels) */
281 for (i = 0; i < max_num_caldacs; i++)
282 cal2_bits |= DESELECT_CALDAC_BIT(i);
283 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2);
285 /* tell eeprom we want to read */
286 cb_pcidda_serial_out(dev, read_instruction, instruction_length);
287 /* send address we want to read from */
288 cb_pcidda_serial_out(dev, address, address_length);
290 value = cb_pcidda_serial_in(dev);
292 /* deactivate eeprom */
293 cal2_bits &= ~SELECT_EEPROM_BIT;
294 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2);
299 /* writes to 8 bit calibration dacs */
300 static void cb_pcidda_write_caldac(struct comedi_device *dev,
301 unsigned int caldac, unsigned int channel,
304 struct cb_pcidda_private *devpriv = dev->private;
305 unsigned int cal2_bits;
307 /* caldacs use 3 bit channel specification */
308 const int num_channel_bits = 3;
309 const int num_caldac_bits = 8; /* 8 bit calibration dacs */
310 /* one caldac for every two dac channels */
311 const int max_num_caldacs = 4;
313 /* write 3 bit channel */
314 cb_pcidda_serial_out(dev, channel, num_channel_bits);
315 /* write 8 bit caldac value */
316 cb_pcidda_serial_out(dev, value, num_caldac_bits);
319 * latch stream into appropriate caldac deselect reference dac
321 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
322 /* deactivate caldacs (one caldac for every two channels) */
323 for (i = 0; i < max_num_caldacs; i++)
324 cal2_bits |= DESELECT_CALDAC_BIT(i);
325 /* activate the caldac we want */
326 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
327 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2);
328 /* deactivate caldac */
329 cal2_bits |= DESELECT_CALDAC_BIT(caldac);
330 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2);
333 /* returns caldac that calibrates given analog out channel */
334 static unsigned int caldac_number(unsigned int channel)
339 /* returns caldac channel that provides fine gain for given ao channel */
340 static unsigned int fine_gain_channel(unsigned int ao_channel)
342 return 4 * (ao_channel % 2);
345 /* returns caldac channel that provides coarse gain for given ao channel */
346 static unsigned int coarse_gain_channel(unsigned int ao_channel)
348 return 1 + 4 * (ao_channel % 2);
351 /* returns caldac channel that provides coarse offset for given ao channel */
352 static unsigned int coarse_offset_channel(unsigned int ao_channel)
354 return 2 + 4 * (ao_channel % 2);
357 /* returns caldac channel that provides fine offset for given ao channel */
358 static unsigned int fine_offset_channel(unsigned int ao_channel)
360 return 3 + 4 * (ao_channel % 2);
363 /* returns eeprom address that provides offset for given ao channel and range */
364 static unsigned int offset_eeprom_address(unsigned int ao_channel,
367 return 0x7 + 2 * range + 12 * ao_channel;
371 * returns eeprom address that provides gain calibration for given ao
374 static unsigned int gain_eeprom_address(unsigned int ao_channel,
377 return 0x8 + 2 * range + 12 * ao_channel;
381 * returns upper byte of eeprom entry, which gives the coarse adjustment
384 static unsigned int eeprom_coarse_byte(unsigned int word)
386 return (word >> 8) & 0xff;
389 /* returns lower byte of eeprom entry, which gives the fine adjustment values */
390 static unsigned int eeprom_fine_byte(unsigned int word)
395 /* set caldacs to eeprom values for given channel and range */
396 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
399 struct cb_pcidda_private *devpriv = dev->private;
400 unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain;
402 /* remember range so we can tell when we need to readjust calibration */
403 devpriv->ao_range[channel] = range;
405 /* get values from eeprom data */
407 eeprom_coarse_byte(devpriv->eeprom_data
408 [offset_eeprom_address(channel, range)]);
410 eeprom_fine_byte(devpriv->eeprom_data
411 [offset_eeprom_address(channel, range)]);
413 eeprom_coarse_byte(devpriv->eeprom_data
414 [gain_eeprom_address(channel, range)]);
416 eeprom_fine_byte(devpriv->eeprom_data
417 [gain_eeprom_address(channel, range)]);
420 cb_pcidda_write_caldac(dev, caldac_number(channel),
421 coarse_offset_channel(channel), coarse_offset);
422 cb_pcidda_write_caldac(dev, caldac_number(channel),
423 fine_offset_channel(channel), fine_offset);
424 cb_pcidda_write_caldac(dev, caldac_number(channel),
425 coarse_gain_channel(channel), coarse_gain);
426 cb_pcidda_write_caldac(dev, caldac_number(channel),
427 fine_gain_channel(channel), fine_gain);
430 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
431 struct comedi_subdevice *s,
432 struct comedi_insn *insn, unsigned int *data)
434 struct cb_pcidda_private *devpriv = dev->private;
435 unsigned int command;
436 unsigned int channel, range;
438 channel = CR_CHAN(insn->chanspec);
439 range = CR_RANGE(insn->chanspec);
441 /* adjust calibration dacs if range has changed */
442 if (range != devpriv->ao_range[channel])
443 cb_pcidda_calibrate(dev, channel, range);
445 /* output channel configuration */
446 command = NOSU | ENABLEDAC;
448 /* output channel range */
451 command |= BIP | RANGE10V;
454 command |= BIP | RANGE5V;
457 command |= BIP | RANGE2V5;
460 command |= UNIP | RANGE10V;
463 command |= UNIP | RANGE5V;
466 command |= UNIP | RANGE2V5;
470 /* output channel specification */
471 command |= channel << 2;
472 outw(command, devpriv->dac + DACONTROL);
475 outw(data[0], devpriv->dac + DADATA + channel * 2);
477 /* return the number of samples read/written */
481 static const void *cb_pcidda_find_boardinfo(struct comedi_device *dev,
482 struct pci_dev *pcidev)
484 const struct cb_pcidda_board *thisboard;
487 for (i = 0; i < ARRAY_SIZE(cb_pcidda_boards); i++) {
488 thisboard = &cb_pcidda_boards[i];
489 if (thisboard->device_id != pcidev->device)
495 static int cb_pcidda_attach_pci(struct comedi_device *dev,
496 struct pci_dev *pcidev)
498 const struct cb_pcidda_board *thisboard;
499 struct cb_pcidda_private *devpriv;
500 struct comedi_subdevice *s;
504 thisboard = cb_pcidda_find_boardinfo(dev, pcidev);
507 dev->board_ptr = thisboard;
508 dev->board_name = thisboard->name;
510 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
513 dev->private = devpriv;
515 ret = comedi_pci_enable(pcidev, dev->board_name);
519 devpriv->digitalio = pci_resource_start(pcidev, 2);
520 devpriv->dac = pci_resource_start(pcidev, 3);
521 dev->iobase = devpriv->dac;
523 if (thisboard->status == 2)
525 ("WARNING: DRIVER FOR THIS BOARD NOT CHECKED WITH MANUAL. "
526 "WORKS ASSUMING FULL COMPATIBILITY WITH PCI-DDA08/12. "
527 "PLEASE REPORT USAGE TO <ivanmr@altavista.com>.\n");
529 ret = comedi_alloc_subdevices(dev, 3);
533 s = &dev->subdevices[0];
534 /* analog output subdevice */
535 s->type = COMEDI_SUBD_AO;
536 s->subdev_flags = SDF_WRITABLE;
537 s->n_chan = thisboard->ao_chans;
538 s->maxdata = (1 << thisboard->ao_bits) - 1;
539 s->range_table = thisboard->ranges;
540 s->insn_write = cb_pcidda_ao_winsn;
542 /* two 8255 digital io subdevices */
543 s = &dev->subdevices[1];
544 subdev_8255_init(dev, s, NULL, devpriv->digitalio);
545 s = &dev->subdevices[2];
546 subdev_8255_init(dev, s, NULL, devpriv->digitalio + PORT2A);
548 dev_dbg(dev->class_dev, "eeprom:\n");
549 for (index = 0; index < EEPROM_SIZE; index++) {
550 devpriv->eeprom_data[index] = cb_pcidda_read_eeprom(dev, index);
551 dev_dbg(dev->class_dev, "%i:0x%x\n", index,
552 devpriv->eeprom_data[index]);
555 /* set calibrations dacs */
556 for (index = 0; index < thisboard->ao_chans; index++)
557 cb_pcidda_calibrate(dev, index, devpriv->ao_range[index]);
559 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
564 static void cb_pcidda_detach(struct comedi_device *dev)
566 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
568 if (dev->subdevices) {
569 subdev_8255_cleanup(dev, &dev->subdevices[1]);
570 subdev_8255_cleanup(dev, &dev->subdevices[2]);
574 comedi_pci_disable(pcidev);
578 static struct comedi_driver cb_pcidda_driver = {
579 .driver_name = "cb_pcidda",
580 .module = THIS_MODULE,
581 .attach_pci = cb_pcidda_attach_pci,
582 .detach = cb_pcidda_detach,
585 static int __devinit cb_pcidda_pci_probe(struct pci_dev *dev,
586 const struct pci_device_id *ent)
588 return comedi_pci_auto_config(dev, &cb_pcidda_driver);
591 static void __devexit cb_pcidda_pci_remove(struct pci_dev *dev)
593 comedi_pci_auto_unconfig(dev);
596 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
597 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_12) },
598 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_12) },
599 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_12) },
600 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_16) },
601 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_16) },
602 { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_16) },
605 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
607 static struct pci_driver cb_pcidda_pci_driver = {
609 .id_table = cb_pcidda_pci_table,
610 .probe = cb_pcidda_pci_probe,
611 .remove = __devexit_p(cb_pcidda_pci_remove),
613 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
615 MODULE_AUTHOR("Comedi http://www.comedi.org");
616 MODULE_DESCRIPTION("Comedi low-level driver");
617 MODULE_LICENSE("GPL");