cb_pcidda.c revision 20fb2280815510533cbd7785b53821ca7209345b
1/* 2 comedi/drivers/cb_pcidda.c 3 This intends to be a driver for the ComputerBoards / MeasurementComputing 4 PCI-DDA series. 5 6 Copyright (C) 2001 Ivan Martinez <ivanmr@altavista.com> 7 Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net> 8 9 COMEDI - Linux Control and Measurement Device Interface 10 Copyright (C) 1997-8 David A. Schleef <ds@schleef.org> 11 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. 16 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. 21 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. 25 26*/ 27/* 28Driver: cb_pcidda 29Description: MeasurementComputing PCI-DDA series 30Author: Ivan Martinez <ivanmr@altavista.com>, Frank Mori Hess <fmhess@users.sourceforge.net> 31Status: Supports 08/16, 04/16, 02/16, 08/12, 04/12, and 02/12 32Devices: [Measurement Computing] PCI-DDA08/12 (cb_pcidda), PCI-DDA04/12, 33 PCI-DDA02/12, PCI-DDA08/16, PCI-DDA04/16, PCI-DDA02/16 34 35Configuration 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 39 device will be used. 40 41Only simple analog output writing is supported. 42 43So far it has only been tested with: 44 - PCI-DDA08/12 45Please report success/failure with other different cards to 46<comedi@comedi.org>. 47*/ 48 49#include "../comedidev.h" 50 51#include "comedi_pci.h" 52#include "8255.h" 53 54#define PCI_VENDOR_ID_CB 0x1307 /* PCI vendor number of ComputerBoards */ 55#define N_BOARDS 10 /* Number of boards in cb_pcidda_boards */ 56#define EEPROM_SIZE 128 /* number of entries in eeprom */ 57#define MAX_AO_CHANNELS 8 /* maximum number of ao channels for supported boards */ 58 59/* PCI-DDA base addresses */ 60#define DIGITALIO_BADRINDEX 2 61 /* DIGITAL I/O is pci_dev->resource[2] */ 62#define DIGITALIO_SIZE 8 63 /* DIGITAL I/O uses 8 I/O port addresses */ 64#define DAC_BADRINDEX 3 65 /* DAC is pci_dev->resource[3] */ 66 67/* Digital I/O registers */ 68#define PORT1A 0 /* PORT 1A DATA */ 69 70#define PORT1B 1 /* PORT 1B DATA */ 71 72#define PORT1C 2 /* PORT 1C DATA */ 73 74#define CONTROL1 3 /* CONTROL REGISTER 1 */ 75 76#define PORT2A 4 /* PORT 2A DATA */ 77 78#define PORT2B 5 /* PORT 2B DATA */ 79 80#define PORT2C 6 /* PORT 2C DATA */ 81 82#define CONTROL2 7 /* CONTROL REGISTER 2 */ 83 84/* DAC registers */ 85#define DACONTROL 0 /* D/A CONTROL REGISTER */ 86#define SU 0000001 /* Simultaneous update enabled */ 87#define NOSU 0000000 /* Simultaneous update disabled */ 88#define ENABLEDAC 0000002 /* Enable specified DAC */ 89#define DISABLEDAC 0000000 /* Disable specified DAC */ 90#define RANGE2V5 0000000 /* 2.5V */ 91#define RANGE5V 0000200 /* 5V */ 92#define RANGE10V 0000300 /* 10V */ 93#define UNIP 0000400 /* Unipolar outputs */ 94#define BIP 0000000 /* Bipolar outputs */ 95 96#define DACALIBRATION1 4 /* D/A CALIBRATION REGISTER 1 */ 97/* write bits */ 98#define SERIAL_IN_BIT 0x1 /* serial data input for eeprom, caldacs, reference dac */ 99#define CAL_CHANNEL_MASK (0x7 << 1) 100#define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK) 101/* read bits */ 102#define CAL_COUNTER_MASK 0x1f 103#define CAL_COUNTER_OVERFLOW_BIT 0x20 /* calibration counter overflow status bit */ 104#define AO_BELOW_REF_BIT 0x40 /* analog output is less than reference dac voltage */ 105#define SERIAL_OUT_BIT 0x80 /* serial data out, for reading from eeprom */ 106 107#define DACALIBRATION2 6 /* D/A CALIBRATION REGISTER 2 */ 108#define SELECT_EEPROM_BIT 0x1 /* send serial data in to eeprom */ 109#define DESELECT_REF_DAC_BIT 0x2 /* don't send serial data to MAX542 reference dac */ 110#define DESELECT_CALDAC_BIT(n) (0x4 << (n)) /* don't send serial data to caldac n */ 111#define DUMMY_BIT 0x40 /* manual says to set this bit with no explanation */ 112 113#define DADATA 8 /* FIRST D/A DATA REGISTER (0) */ 114 115static const struct comedi_lrange cb_pcidda_ranges = { 116 6, 117 { 118 BIP_RANGE(10), 119 BIP_RANGE(5), 120 BIP_RANGE(2.5), 121 UNI_RANGE(10), 122 UNI_RANGE(5), 123 UNI_RANGE(2.5), 124 } 125}; 126 127/* 128 * Board descriptions for two imaginary boards. Describing the 129 * boards in this way is optional, and completely driver-dependent. 130 * Some drivers use arrays such as this, other do not. 131 */ 132struct cb_pcidda_board { 133 const char *name; 134 char status; /* Driver status: */ 135 136 /* 137 * 0 - tested 138 * 1 - manual read, not tested 139 * 2 - manual not read 140 */ 141 142 unsigned short device_id; 143 int ao_chans; 144 int ao_bits; 145 const struct comedi_lrange *ranges; 146}; 147 148static const struct cb_pcidda_board cb_pcidda_boards[] = { 149 { 150 .name = "pci-dda02/12", 151 .status = 1, 152 .device_id = 0x20, 153 .ao_chans = 2, 154 .ao_bits = 12, 155 .ranges = &cb_pcidda_ranges, 156 }, 157 { 158 .name = "pci-dda04/12", 159 .status = 1, 160 .device_id = 0x21, 161 .ao_chans = 4, 162 .ao_bits = 12, 163 .ranges = &cb_pcidda_ranges, 164 }, 165 { 166 .name = "pci-dda08/12", 167 .status = 0, 168 .device_id = 0x22, 169 .ao_chans = 8, 170 .ao_bits = 12, 171 .ranges = &cb_pcidda_ranges, 172 }, 173 { 174 .name = "pci-dda02/16", 175 .status = 2, 176 .device_id = 0x23, 177 .ao_chans = 2, 178 .ao_bits = 16, 179 .ranges = &cb_pcidda_ranges, 180 }, 181 { 182 .name = "pci-dda04/16", 183 .status = 2, 184 .device_id = 0x24, 185 .ao_chans = 4, 186 .ao_bits = 16, 187 .ranges = &cb_pcidda_ranges, 188 }, 189 { 190 .name = "pci-dda08/16", 191 .status = 0, 192 .device_id = 0x25, 193 .ao_chans = 8, 194 .ao_bits = 16, 195 .ranges = &cb_pcidda_ranges, 196 }, 197}; 198 199static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = { 200 { 201 PCI_VENDOR_ID_CB, 0x0020, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 202 PCI_VENDOR_ID_CB, 0x0021, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 203 PCI_VENDOR_ID_CB, 0x0022, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 204 PCI_VENDOR_ID_CB, 0x0023, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 205 PCI_VENDOR_ID_CB, 0x0024, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 206 PCI_VENDOR_ID_CB, 0x0025, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 207 0} 208}; 209 210MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table); 211 212/* 213 * Useful for shorthand access to the particular board structure 214 */ 215#define thisboard ((const struct cb_pcidda_board *)dev->board_ptr) 216 217/* this structure is for data unique to this hardware driver. If 218 several hardware drivers keep similar information in this structure, 219 feel free to suggest moving the variable to the struct comedi_device struct. */ 220struct cb_pcidda_private { 221 int data; 222 223 /* would be useful for a PCI device */ 224 struct pci_dev *pci_dev; 225 226 unsigned long digitalio; 227 unsigned long dac; 228 229 /* unsigned long control_status; */ 230 /* unsigned long adc_fifo; */ 231 232 unsigned int dac_cal1_bits; /* bits last written to da calibration register 1 */ 233 unsigned int ao_range[MAX_AO_CHANNELS]; /* current range settings for output channels */ 234 u16 eeprom_data[EEPROM_SIZE]; /* software copy of board's eeprom */ 235}; 236 237/* 238 * most drivers define the following macro to make it easy to 239 * access the private structure. 240 */ 241#define devpriv ((struct cb_pcidda_private *)dev->private) 242 243static int cb_pcidda_attach(struct comedi_device *dev, 244 struct comedi_devconfig *it); 245static int cb_pcidda_detach(struct comedi_device *dev); 246/* static int cb_pcidda_ai_rinsn(struct comedi_device *dev,struct comedi_subdevice *s,struct comedi_insn *insn,unsigned int *data); */ 247static int cb_pcidda_ao_winsn(struct comedi_device *dev, 248 struct comedi_subdevice *s, 249 struct comedi_insn *insn, unsigned int *data); 250 251/* static int cb_pcidda_ai_cmd(struct comedi_device *dev, struct *comedi_subdevice *s);*/ 252/* static int cb_pcidda_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s, struct comedi_cmd *cmd); */ 253/* static int cb_pcidda_ns_to_timer(unsigned int *ns,int *round); */ 254 255static unsigned int cb_pcidda_serial_in(struct comedi_device *dev); 256static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value, 257 unsigned int num_bits); 258static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev, 259 unsigned int address); 260static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel, 261 unsigned int range); 262 263/* 264 * The struct comedi_driver structure tells the Comedi core module 265 * which functions to call to configure/deconfigure (attach/detach) 266 * the board, and also about the kernel module that contains 267 * the device code. 268 */ 269static struct comedi_driver driver_cb_pcidda = { 270 .driver_name = "cb_pcidda", 271 .module = THIS_MODULE, 272 .attach = cb_pcidda_attach, 273 .detach = cb_pcidda_detach, 274}; 275 276/* 277 * Attach is called by the Comedi core to configure the driver 278 * for a particular board. 279 */ 280static int cb_pcidda_attach(struct comedi_device *dev, 281 struct comedi_devconfig *it) 282{ 283 struct comedi_subdevice *s; 284 struct pci_dev *pcidev = NULL; 285 int index; 286 287 printk("comedi%d: cb_pcidda: ", dev->minor); 288 289/* 290 * Allocate the private structure area. 291 */ 292 if (alloc_private(dev, sizeof(struct cb_pcidda_private)) < 0) 293 return -ENOMEM; 294 295/* 296 * Probe the device to determine what device in the series it is. 297 */ 298 printk("\n"); 299 300 for_each_pci_dev(pcidev) { 301 if (pcidev->vendor == PCI_VENDOR_ID_CB) { 302 if (it->options[0] || it->options[1]) { 303 if (pcidev->bus->number != it->options[0] || 304 PCI_SLOT(pcidev->devfn) != it->options[1]) { 305 continue; 306 } 307 } 308 for (index = 0; index < N_BOARDS; index++) { 309 if (cb_pcidda_boards[index].device_id == 310 pcidev->device) { 311 goto found; 312 } 313 } 314 } 315 } 316 if (!pcidev) { 317 printk 318 ("Not a ComputerBoards/MeasurementComputing card on requested position\n"); 319 return -EIO; 320 } 321found: 322 devpriv->pci_dev = pcidev; 323 dev->board_ptr = cb_pcidda_boards + index; 324 /* "thisboard" macro can be used from here. */ 325 printk("Found %s at requested position\n", thisboard->name); 326 327 /* 328 * Enable PCI device and request regions. 329 */ 330 if (comedi_pci_enable(pcidev, thisboard->name)) { 331 printk 332 ("cb_pcidda: failed to enable PCI device and request regions\n"); 333 return -EIO; 334 } 335 336/* 337 * Allocate the I/O ports. 338 */ 339 devpriv->digitalio = 340 pci_resource_start(devpriv->pci_dev, DIGITALIO_BADRINDEX); 341 devpriv->dac = pci_resource_start(devpriv->pci_dev, DAC_BADRINDEX); 342 343/* 344 * Warn about the status of the driver. 345 */ 346 if (thisboard->status == 2) 347 printk 348 ("WARNING: DRIVER FOR THIS BOARD NOT CHECKED WITH MANUAL. " 349 "WORKS ASSUMING FULL COMPATIBILITY WITH PCI-DDA08/12. " 350 "PLEASE REPORT USAGE TO <ivanmr@altavista.com>.\n"); 351 352/* 353 * Initialize dev->board_name. 354 */ 355 dev->board_name = thisboard->name; 356 357/* 358 * Allocate the subdevice structures. 359 */ 360 if (alloc_subdevices(dev, 3) < 0) 361 return -ENOMEM; 362 363 s = dev->subdevices + 0; 364 /* analog output subdevice */ 365 s->type = COMEDI_SUBD_AO; 366 s->subdev_flags = SDF_WRITABLE; 367 s->n_chan = thisboard->ao_chans; 368 s->maxdata = (1 << thisboard->ao_bits) - 1; 369 s->range_table = thisboard->ranges; 370 s->insn_write = cb_pcidda_ao_winsn; 371 372 /* s->subdev_flags |= SDF_CMD_READ; */ 373 /* s->do_cmd = cb_pcidda_ai_cmd; */ 374 /* s->do_cmdtest = cb_pcidda_ai_cmdtest; */ 375 376 /* two 8255 digital io subdevices */ 377 s = dev->subdevices + 1; 378 subdev_8255_init(dev, s, NULL, devpriv->digitalio); 379 s = dev->subdevices + 2; 380 subdev_8255_init(dev, s, NULL, devpriv->digitalio + PORT2A); 381 382 printk(" eeprom:"); 383 for (index = 0; index < EEPROM_SIZE; index++) { 384 devpriv->eeprom_data[index] = cb_pcidda_read_eeprom(dev, index); 385 printk(" %i:0x%x ", index, devpriv->eeprom_data[index]); 386 } 387 printk("\n"); 388 389 /* set calibrations dacs */ 390 for (index = 0; index < thisboard->ao_chans; index++) 391 cb_pcidda_calibrate(dev, index, devpriv->ao_range[index]); 392 393 return 1; 394} 395 396/* 397 * _detach is called to deconfigure a device. It should deallocate 398 * resources. 399 * This function is also called when _attach() fails, so it should be 400 * careful not to release resources that were not necessarily 401 * allocated by _attach(). dev->private and dev->subdevices are 402 * deallocated automatically by the core. 403 */ 404static int cb_pcidda_detach(struct comedi_device *dev) 405{ 406/* 407 * Deallocate the I/O ports. 408 */ 409 if (devpriv) { 410 if (devpriv->pci_dev) { 411 if (devpriv->dac) 412 comedi_pci_disable(devpriv->pci_dev); 413 pci_dev_put(devpriv->pci_dev); 414 } 415 } 416 /* cleanup 8255 */ 417 if (dev->subdevices) { 418 subdev_8255_cleanup(dev, dev->subdevices + 1); 419 subdev_8255_cleanup(dev, dev->subdevices + 2); 420 } 421 422 printk("comedi%d: cb_pcidda: remove\n", dev->minor); 423 424 return 0; 425} 426 427/* 428 * I will program this later... ;-) 429 */ 430#if 0 431static int cb_pcidda_ai_cmd(struct comedi_device *dev, 432 struct comedi_subdevice *s) 433{ 434 printk("cb_pcidda_ai_cmd\n"); 435 printk("subdev: %d\n", cmd->subdev); 436 printk("flags: %d\n", cmd->flags); 437 printk("start_src: %d\n", cmd->start_src); 438 printk("start_arg: %d\n", cmd->start_arg); 439 printk("scan_begin_src: %d\n", cmd->scan_begin_src); 440 printk("convert_src: %d\n", cmd->convert_src); 441 printk("convert_arg: %d\n", cmd->convert_arg); 442 printk("scan_end_src: %d\n", cmd->scan_end_src); 443 printk("scan_end_arg: %d\n", cmd->scan_end_arg); 444 printk("stop_src: %d\n", cmd->stop_src); 445 printk("stop_arg: %d\n", cmd->stop_arg); 446 printk("chanlist_len: %d\n", cmd->chanlist_len); 447} 448#endif 449 450#if 0 451static int cb_pcidda_ai_cmdtest(struct comedi_device *dev, 452 struct comedi_subdevice *s, 453 struct comedi_cmd *cmd) 454{ 455 int err = 0; 456 int tmp; 457 458 /* cmdtest tests a particular command to see if it is valid. 459 * Using the cmdtest ioctl, a user can create a valid cmd 460 * and then have it executes by the cmd ioctl. 461 * 462 * cmdtest returns 1,2,3,4 or 0, depending on which tests 463 * the command passes. */ 464 465 /* step 1: make sure trigger sources are trivially valid */ 466 467 tmp = cmd->start_src; 468 cmd->start_src &= TRIG_NOW; 469 if (!cmd->start_src || tmp != cmd->start_src) 470 err++; 471 472 tmp = cmd->scan_begin_src; 473 cmd->scan_begin_src &= TRIG_TIMER | TRIG_EXT; 474 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src) 475 err++; 476 477 tmp = cmd->convert_src; 478 cmd->convert_src &= TRIG_TIMER | TRIG_EXT; 479 if (!cmd->convert_src || tmp != cmd->convert_src) 480 err++; 481 482 tmp = cmd->scan_end_src; 483 cmd->scan_end_src &= TRIG_COUNT; 484 if (!cmd->scan_end_src || tmp != cmd->scan_end_src) 485 err++; 486 487 tmp = cmd->stop_src; 488 cmd->stop_src &= TRIG_COUNT | TRIG_NONE; 489 if (!cmd->stop_src || tmp != cmd->stop_src) 490 err++; 491 492 if (err) 493 return 1; 494 495 /* step 2: make sure trigger sources are unique and mutually compatible */ 496 497 /* note that mutual compatibility is not an issue here */ 498 if (cmd->scan_begin_src != TRIG_TIMER 499 && cmd->scan_begin_src != TRIG_EXT) 500 err++; 501 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT) 502 err++; 503 if (cmd->stop_src != TRIG_TIMER && cmd->stop_src != TRIG_EXT) 504 err++; 505 506 if (err) 507 return 2; 508 509 /* step 3: make sure arguments are trivially compatible */ 510 511 if (cmd->start_arg != 0) { 512 cmd->start_arg = 0; 513 err++; 514 } 515#define MAX_SPEED 10000 /* in nanoseconds */ 516#define MIN_SPEED 1000000000 /* in nanoseconds */ 517 518 if (cmd->scan_begin_src == TRIG_TIMER) { 519 if (cmd->scan_begin_arg < MAX_SPEED) { 520 cmd->scan_begin_arg = MAX_SPEED; 521 err++; 522 } 523 if (cmd->scan_begin_arg > MIN_SPEED) { 524 cmd->scan_begin_arg = MIN_SPEED; 525 err++; 526 } 527 } else { 528 /* external trigger */ 529 /* should be level/edge, hi/lo specification here */ 530 /* should specify multiple external triggers */ 531 if (cmd->scan_begin_arg > 9) { 532 cmd->scan_begin_arg = 9; 533 err++; 534 } 535 } 536 if (cmd->convert_src == TRIG_TIMER) { 537 if (cmd->convert_arg < MAX_SPEED) { 538 cmd->convert_arg = MAX_SPEED; 539 err++; 540 } 541 if (cmd->convert_arg > MIN_SPEED) { 542 cmd->convert_arg = MIN_SPEED; 543 err++; 544 } 545 } else { 546 /* external trigger */ 547 /* see above */ 548 if (cmd->convert_arg > 9) { 549 cmd->convert_arg = 9; 550 err++; 551 } 552 } 553 554 if (cmd->scan_end_arg != cmd->chanlist_len) { 555 cmd->scan_end_arg = cmd->chanlist_len; 556 err++; 557 } 558 if (cmd->stop_src == TRIG_COUNT) { 559 if (cmd->stop_arg > 0x00ffffff) { 560 cmd->stop_arg = 0x00ffffff; 561 err++; 562 } 563 } else { 564 /* TRIG_NONE */ 565 if (cmd->stop_arg != 0) { 566 cmd->stop_arg = 0; 567 err++; 568 } 569 } 570 571 if (err) 572 return 3; 573 574 /* step 4: fix up any arguments */ 575 576 if (cmd->scan_begin_src == TRIG_TIMER) { 577 tmp = cmd->scan_begin_arg; 578 cb_pcidda_ns_to_timer(&cmd->scan_begin_arg, 579 cmd->flags & TRIG_ROUND_MASK); 580 if (tmp != cmd->scan_begin_arg) 581 err++; 582 } 583 if (cmd->convert_src == TRIG_TIMER) { 584 tmp = cmd->convert_arg; 585 cb_pcidda_ns_to_timer(&cmd->convert_arg, 586 cmd->flags & TRIG_ROUND_MASK); 587 if (tmp != cmd->convert_arg) 588 err++; 589 if (cmd->scan_begin_src == TRIG_TIMER && 590 cmd->scan_begin_arg < 591 cmd->convert_arg * cmd->scan_end_arg) { 592 cmd->scan_begin_arg = 593 cmd->convert_arg * cmd->scan_end_arg; 594 err++; 595 } 596 } 597 598 if (err) 599 return 4; 600 601 return 0; 602} 603#endif 604 605/* This function doesn't require a particular form, this is just 606 * what happens to be used in some of the drivers. It should 607 * convert ns nanoseconds to a counter value suitable for programming 608 * the device. Also, it should adjust ns so that it cooresponds to 609 * the actual time that the device will use. */ 610#if 0 611static int cb_pcidda_ns_to_timer(unsigned int *ns, int round) 612{ 613 /* trivial timer */ 614 return *ns; 615} 616#endif 617 618static int cb_pcidda_ao_winsn(struct comedi_device *dev, 619 struct comedi_subdevice *s, 620 struct comedi_insn *insn, unsigned int *data) 621{ 622 unsigned int command; 623 unsigned int channel, range; 624 625 channel = CR_CHAN(insn->chanspec); 626 range = CR_RANGE(insn->chanspec); 627 628 /* adjust calibration dacs if range has changed */ 629 if (range != devpriv->ao_range[channel]) 630 cb_pcidda_calibrate(dev, channel, range); 631 632 /* output channel configuration */ 633 command = NOSU | ENABLEDAC; 634 635 /* output channel range */ 636 switch (range) { 637 case 0: 638 command |= BIP | RANGE10V; 639 break; 640 case 1: 641 command |= BIP | RANGE5V; 642 break; 643 case 2: 644 command |= BIP | RANGE2V5; 645 break; 646 case 3: 647 command |= UNIP | RANGE10V; 648 break; 649 case 4: 650 command |= UNIP | RANGE5V; 651 break; 652 case 5: 653 command |= UNIP | RANGE2V5; 654 break; 655 }; 656 657 /* output channel specification */ 658 command |= channel << 2; 659 outw(command, devpriv->dac + DACONTROL); 660 661 /* write data */ 662 outw(data[0], devpriv->dac + DADATA + channel * 2); 663 664 /* return the number of samples read/written */ 665 return 1; 666} 667 668/* lowlevel read from eeprom */ 669static unsigned int cb_pcidda_serial_in(struct comedi_device *dev) 670{ 671 unsigned int value = 0; 672 int i; 673 const int value_width = 16; /* number of bits wide values are */ 674 675 for (i = 1; i <= value_width; i++) { 676 /* read bits most significant bit first */ 677 if (inw_p(devpriv->dac + DACALIBRATION1) & SERIAL_OUT_BIT) 678 value |= 1 << (value_width - i); 679 } 680 681 return value; 682} 683 684/* lowlevel write to eeprom/dac */ 685static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value, 686 unsigned int num_bits) 687{ 688 int i; 689 690 for (i = 1; i <= num_bits; i++) { 691 /* send bits most significant bit first */ 692 if (value & (1 << (num_bits - i))) 693 devpriv->dac_cal1_bits |= SERIAL_IN_BIT; 694 else 695 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT; 696 outw_p(devpriv->dac_cal1_bits, devpriv->dac + DACALIBRATION1); 697 } 698} 699 700/* reads a 16 bit value from board's eeprom */ 701static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev, 702 unsigned int address) 703{ 704 unsigned int i; 705 unsigned int cal2_bits; 706 unsigned int value; 707 const int max_num_caldacs = 4; /* one caldac for every two dac channels */ 708 const int read_instruction = 0x6; /* bits to send to tell eeprom we want to read */ 709 const int instruction_length = 3; 710 const int address_length = 8; 711 712 /* send serial output stream to eeprom */ 713 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT; 714 /* deactivate caldacs (one caldac for every two channels) */ 715 for (i = 0; i < max_num_caldacs; i++) 716 cal2_bits |= DESELECT_CALDAC_BIT(i); 717 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2); 718 719 /* tell eeprom we want to read */ 720 cb_pcidda_serial_out(dev, read_instruction, instruction_length); 721 /* send address we want to read from */ 722 cb_pcidda_serial_out(dev, address, address_length); 723 724 value = cb_pcidda_serial_in(dev); 725 726 /* deactivate eeprom */ 727 cal2_bits &= ~SELECT_EEPROM_BIT; 728 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2); 729 730 return value; 731} 732 733/* writes to 8 bit calibration dacs */ 734static void cb_pcidda_write_caldac(struct comedi_device *dev, 735 unsigned int caldac, unsigned int channel, 736 unsigned int value) 737{ 738 unsigned int cal2_bits; 739 unsigned int i; 740 const int num_channel_bits = 3; /* caldacs use 3 bit channel specification */ 741 const int num_caldac_bits = 8; /* 8 bit calibration dacs */ 742 const int max_num_caldacs = 4; /* one caldac for every two dac channels */ 743 744 /* write 3 bit channel */ 745 cb_pcidda_serial_out(dev, channel, num_channel_bits); 746 /* write 8 bit caldac value */ 747 cb_pcidda_serial_out(dev, value, num_caldac_bits); 748 749/* 750* latch stream into appropriate caldac deselect reference dac 751*/ 752 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT; 753 /* deactivate caldacs (one caldac for every two channels) */ 754 for (i = 0; i < max_num_caldacs; i++) 755 cal2_bits |= DESELECT_CALDAC_BIT(i); 756 /* activate the caldac we want */ 757 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac); 758 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2); 759 /* deactivate caldac */ 760 cal2_bits |= DESELECT_CALDAC_BIT(caldac); 761 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2); 762} 763 764/* returns caldac that calibrates given analog out channel */ 765static unsigned int caldac_number(unsigned int channel) 766{ 767 return channel / 2; 768} 769 770/* returns caldac channel that provides fine gain for given ao channel */ 771static unsigned int fine_gain_channel(unsigned int ao_channel) 772{ 773 return 4 * (ao_channel % 2); 774} 775 776/* returns caldac channel that provides coarse gain for given ao channel */ 777static unsigned int coarse_gain_channel(unsigned int ao_channel) 778{ 779 return 1 + 4 * (ao_channel % 2); 780} 781 782/* returns caldac channel that provides coarse offset for given ao channel */ 783static unsigned int coarse_offset_channel(unsigned int ao_channel) 784{ 785 return 2 + 4 * (ao_channel % 2); 786} 787 788/* returns caldac channel that provides fine offset for given ao channel */ 789static unsigned int fine_offset_channel(unsigned int ao_channel) 790{ 791 return 3 + 4 * (ao_channel % 2); 792} 793 794/* returns eeprom address that provides offset for given ao channel and range */ 795static unsigned int offset_eeprom_address(unsigned int ao_channel, 796 unsigned int range) 797{ 798 return 0x7 + 2 * range + 12 * ao_channel; 799} 800 801/* returns eeprom address that provides gain calibration for given ao channel and range */ 802static unsigned int gain_eeprom_address(unsigned int ao_channel, 803 unsigned int range) 804{ 805 return 0x8 + 2 * range + 12 * ao_channel; 806} 807 808/* returns upper byte of eeprom entry, which gives the coarse adjustment values */ 809static unsigned int eeprom_coarse_byte(unsigned int word) 810{ 811 return (word >> 8) & 0xff; 812} 813 814/* returns lower byte of eeprom entry, which gives the fine adjustment values */ 815static unsigned int eeprom_fine_byte(unsigned int word) 816{ 817 return word & 0xff; 818} 819 820/* set caldacs to eeprom values for given channel and range */ 821static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel, 822 unsigned int range) 823{ 824 unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain; 825 826 /* remember range so we can tell when we need to readjust calibration */ 827 devpriv->ao_range[channel] = range; 828 829 /* get values from eeprom data */ 830 coarse_offset = 831 eeprom_coarse_byte(devpriv->eeprom_data 832 [offset_eeprom_address(channel, range)]); 833 fine_offset = 834 eeprom_fine_byte(devpriv->eeprom_data 835 [offset_eeprom_address(channel, range)]); 836 coarse_gain = 837 eeprom_coarse_byte(devpriv->eeprom_data 838 [gain_eeprom_address(channel, range)]); 839 fine_gain = 840 eeprom_fine_byte(devpriv->eeprom_data 841 [gain_eeprom_address(channel, range)]); 842 843 /* set caldacs */ 844 cb_pcidda_write_caldac(dev, caldac_number(channel), 845 coarse_offset_channel(channel), coarse_offset); 846 cb_pcidda_write_caldac(dev, caldac_number(channel), 847 fine_offset_channel(channel), fine_offset); 848 cb_pcidda_write_caldac(dev, caldac_number(channel), 849 coarse_gain_channel(channel), coarse_gain); 850 cb_pcidda_write_caldac(dev, caldac_number(channel), 851 fine_gain_channel(channel), fine_gain); 852} 853 854/* 855 * A convenient macro that defines init_module() and cleanup_module(), 856 * as necessary. 857 */ 858static int __devinit driver_cb_pcidda_pci_probe(struct pci_dev *dev, 859 const struct pci_device_id *ent) 860{ 861 return comedi_pci_auto_config(dev, driver_cb_pcidda.driver_name); 862} 863 864static void __devexit driver_cb_pcidda_pci_remove(struct pci_dev *dev) 865{ 866 comedi_pci_auto_unconfig(dev); 867} 868 869static struct pci_driver driver_cb_pcidda_pci_driver = { 870 .id_table = cb_pcidda_pci_table, 871 .probe = &driver_cb_pcidda_pci_probe, 872 .remove = __devexit_p(&driver_cb_pcidda_pci_remove) 873}; 874 875static int __init driver_cb_pcidda_init_module(void) 876{ 877 int retval; 878 879 retval = comedi_driver_register(&driver_cb_pcidda); 880 if (retval < 0) 881 return retval; 882 883 driver_cb_pcidda_pci_driver.name = (char *)driver_cb_pcidda.driver_name; 884 return pci_register_driver(&driver_cb_pcidda_pci_driver); 885} 886 887static void __exit driver_cb_pcidda_cleanup_module(void) 888{ 889 pci_unregister_driver(&driver_cb_pcidda_pci_driver); 890 comedi_driver_unregister(&driver_cb_pcidda); 891} 892 893module_init(driver_cb_pcidda_init_module); 894module_exit(driver_cb_pcidda_cleanup_module); 895 896MODULE_AUTHOR("Comedi http://www.comedi.org"); 897MODULE_DESCRIPTION("Comedi low-level driver"); 898MODULE_LICENSE("GPL"); 899