1/* 2 comedi/drivers/cb_pcimdas.c 3 Comedi driver for Computer Boards PCIM-DAS1602/16 4 5 COMEDI - Linux Control and Measurement Device Interface 6 Copyright (C) 2000 David A. Schleef <ds@schleef.org> 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 22*/ 23/* 24Driver: cb_pcimdas 25Description: Measurement Computing PCI Migration series boards 26Devices: [ComputerBoards] PCIM-DAS1602/16 (cb_pcimdas) 27Author: Richard Bytheway 28Updated: Wed, 13 Nov 2002 12:34:56 +0000 29Status: experimental 30 31Written to support the PCIM-DAS1602/16 on a 2.4 series kernel. 32 33Configuration Options: 34 [0] - PCI bus number 35 [1] - PCI slot number 36 37Developed from cb_pcidas and skel by Richard Bytheway (mocelet@sucs.org). 38Only supports DIO, AO and simple AI in it's present form. 39No interrupts, multi channel or FIFO AI, although the card looks like it could support this. 40See http://www.mccdaq.com/PDFs/Manuals/pcim-das1602-16.pdf for more details. 41*/ 42 43#include "../comedidev.h" 44 45#include <linux/delay.h> 46#include <linux/interrupt.h> 47 48#include "comedi_pci.h" 49#include "plx9052.h" 50#include "8255.h" 51 52/* #define CBPCIMDAS_DEBUG */ 53#undef CBPCIMDAS_DEBUG 54 55#define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307 56 57/* Registers for the PCIM-DAS1602/16 */ 58 59/* sizes of io regions (bytes) */ 60#define BADR0_SIZE 2 /* ?? */ 61#define BADR1_SIZE 4 62#define BADR2_SIZE 6 63#define BADR3_SIZE 16 64#define BADR4_SIZE 4 65 66/* DAC Offsets */ 67#define ADC_TRIG 0 68#define DAC0_OFFSET 2 69#define DAC1_OFFSET 4 70 71/* AI and Counter Constants */ 72#define MUX_LIMITS 0 73#define MAIN_CONN_DIO 1 74#define ADC_STAT 2 75#define ADC_CONV_STAT 3 76#define ADC_INT 4 77#define ADC_PACER 5 78#define BURST_MODE 6 79#define PROG_GAIN 7 80#define CLK8254_1_DATA 8 81#define CLK8254_2_DATA 9 82#define CLK8254_3_DATA 10 83#define CLK8254_CONTROL 11 84#define USER_COUNTER 12 85#define RESID_COUNT_H 13 86#define RESID_COUNT_L 14 87 88/* Board description */ 89struct cb_pcimdas_board { 90 const char *name; 91 unsigned short device_id; 92 int ai_se_chans; /* Inputs in single-ended mode */ 93 int ai_diff_chans; /* Inputs in differential mode */ 94 int ai_bits; /* analog input resolution */ 95 int ai_speed; /* fastest conversion period in ns */ 96 int ao_nchan; /* number of analog out channels */ 97 int ao_bits; /* analogue output resolution */ 98 int has_ao_fifo; /* analog output has fifo */ 99 int ao_scan_speed; /* analog output speed for 1602 series (for a scan, not conversion) */ 100 int fifo_size; /* number of samples fifo can hold */ 101 int dio_bits; /* number of dio bits */ 102 int has_dio; /* has DIO */ 103 const struct comedi_lrange *ranges; 104}; 105 106static const struct cb_pcimdas_board cb_pcimdas_boards[] = { 107 { 108 .name = "PCIM-DAS1602/16", 109 .device_id = 0x56, 110 .ai_se_chans = 16, 111 .ai_diff_chans = 8, 112 .ai_bits = 16, 113 .ai_speed = 10000, /* ?? */ 114 .ao_nchan = 2, 115 .ao_bits = 12, 116 .has_ao_fifo = 0, /* ?? */ 117 .ao_scan_speed = 10000, 118 /* ?? */ 119 .fifo_size = 1024, 120 .dio_bits = 24, 121 .has_dio = 1, 122/* .ranges = &cb_pcimdas_ranges, */ 123 }, 124}; 125 126/* This is used by modprobe to translate PCI IDs to drivers. Should 127 * only be used for PCI and ISA-PnP devices */ 128static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table) = { 129 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTERBOARDS, 0x0056) }, 130 { 0 } 131}; 132 133MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table); 134 135#define N_BOARDS 1 /* Max number of boards supported */ 136 137/* 138 * Useful for shorthand access to the particular board structure 139 */ 140#define thisboard ((const struct cb_pcimdas_board *)dev->board_ptr) 141 142/* this structure is for data unique to this hardware driver. If 143 several hardware drivers keep similar information in this structure, 144 feel free to suggest moving the variable to the struct comedi_device struct. */ 145struct cb_pcimdas_private { 146 int data; 147 148 /* would be useful for a PCI device */ 149 struct pci_dev *pci_dev; 150 151 /* base addresses */ 152 unsigned long BADR0; 153 unsigned long BADR1; 154 unsigned long BADR2; 155 unsigned long BADR3; 156 unsigned long BADR4; 157 158 /* Used for AO readback */ 159 unsigned int ao_readback[2]; 160 161 /* Used for DIO */ 162 unsigned short int port_a; /* copy of BADR4+0 */ 163 unsigned short int port_b; /* copy of BADR4+1 */ 164 unsigned short int port_c; /* copy of BADR4+2 */ 165 unsigned short int dio_mode; /* copy of BADR4+3 */ 166 167}; 168 169/* 170 * most drivers define the following macro to make it easy to 171 * access the private structure. 172 */ 173#define devpriv ((struct cb_pcimdas_private *)dev->private) 174 175/* 176 * The struct comedi_driver structure tells the Comedi core module 177 * which functions to call to configure/deconfigure (attach/detach) 178 * the board, and also about the kernel module that contains 179 * the device code. 180 */ 181static int cb_pcimdas_attach(struct comedi_device *dev, 182 struct comedi_devconfig *it); 183static int cb_pcimdas_detach(struct comedi_device *dev); 184static struct comedi_driver driver_cb_pcimdas = { 185 .driver_name = "cb_pcimdas", 186 .module = THIS_MODULE, 187 .attach = cb_pcimdas_attach, 188 .detach = cb_pcimdas_detach, 189}; 190 191static int cb_pcimdas_ai_rinsn(struct comedi_device *dev, 192 struct comedi_subdevice *s, 193 struct comedi_insn *insn, unsigned int *data); 194static int cb_pcimdas_ao_winsn(struct comedi_device *dev, 195 struct comedi_subdevice *s, 196 struct comedi_insn *insn, unsigned int *data); 197static int cb_pcimdas_ao_rinsn(struct comedi_device *dev, 198 struct comedi_subdevice *s, 199 struct comedi_insn *insn, unsigned int *data); 200 201/* 202 * Attach is called by the Comedi core to configure the driver 203 * for a particular board. If you specified a board_name array 204 * in the driver structure, dev->board_ptr contains that 205 * address. 206 */ 207static int cb_pcimdas_attach(struct comedi_device *dev, 208 struct comedi_devconfig *it) 209{ 210 struct comedi_subdevice *s; 211 struct pci_dev *pcidev = NULL; 212 int index; 213 /* int i; */ 214 215/* 216 * Allocate the private structure area. 217 */ 218 if (alloc_private(dev, sizeof(struct cb_pcimdas_private)) < 0) 219 return -ENOMEM; 220 221/* 222 * Probe the device to determine what device in the series it is. 223 */ 224 225 for_each_pci_dev(pcidev) { 226 /* is it not a computer boards card? */ 227 if (pcidev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS) 228 continue; 229 /* loop through cards supported by this driver */ 230 for (index = 0; index < N_BOARDS; index++) { 231 if (cb_pcimdas_boards[index].device_id != 232 pcidev->device) 233 continue; 234 /* was a particular bus/slot requested? */ 235 if (it->options[0] || it->options[1]) { 236 /* are we on the wrong bus/slot? */ 237 if (pcidev->bus->number != it->options[0] || 238 PCI_SLOT(pcidev->devfn) != it->options[1]) { 239 continue; 240 } 241 } 242 devpriv->pci_dev = pcidev; 243 dev->board_ptr = cb_pcimdas_boards + index; 244 goto found; 245 } 246 } 247 248 dev_err(dev->hw_dev, "No supported ComputerBoards/MeasurementComputing card found on requested position\n"); 249 return -EIO; 250 251found: 252 253 dev_dbg(dev->hw_dev, "Found %s on bus %i, slot %i\n", 254 cb_pcimdas_boards[index].name, pcidev->bus->number, 255 PCI_SLOT(pcidev->devfn)); 256 257 /* Warn about non-tested features */ 258 switch (thisboard->device_id) { 259 case 0x56: 260 break; 261 default: 262 dev_dbg(dev->hw_dev, "THIS CARD IS UNSUPPORTED.\n" 263 "PLEASE REPORT USAGE TO <mocelet@sucs.org>\n"); 264 } 265 266 if (comedi_pci_enable(pcidev, "cb_pcimdas")) { 267 dev_err(dev->hw_dev, "Failed to enable PCI device and request regions\n"); 268 return -EIO; 269 } 270 271 devpriv->BADR0 = pci_resource_start(devpriv->pci_dev, 0); 272 devpriv->BADR1 = pci_resource_start(devpriv->pci_dev, 1); 273 devpriv->BADR2 = pci_resource_start(devpriv->pci_dev, 2); 274 devpriv->BADR3 = pci_resource_start(devpriv->pci_dev, 3); 275 devpriv->BADR4 = pci_resource_start(devpriv->pci_dev, 4); 276 277 dev_dbg(dev->hw_dev, "devpriv->BADR0 = 0x%lx\n", devpriv->BADR0); 278 dev_dbg(dev->hw_dev, "devpriv->BADR1 = 0x%lx\n", devpriv->BADR1); 279 dev_dbg(dev->hw_dev, "devpriv->BADR2 = 0x%lx\n", devpriv->BADR2); 280 dev_dbg(dev->hw_dev, "devpriv->BADR3 = 0x%lx\n", devpriv->BADR3); 281 dev_dbg(dev->hw_dev, "devpriv->BADR4 = 0x%lx\n", devpriv->BADR4); 282 283/* Dont support IRQ yet */ 284/* get irq */ 285/* if(request_irq(devpriv->pci_dev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */ 286/* { */ 287/* printk(" unable to allocate irq %u\n", devpriv->pci_dev->irq); */ 288/* return -EINVAL; */ 289/* } */ 290/* dev->irq = devpriv->pci_dev->irq; */ 291 292 /* Initialize dev->board_name */ 293 dev->board_name = thisboard->name; 294 295/* 296 * Allocate the subdevice structures. alloc_subdevice() is a 297 * convenient macro defined in comedidev.h. 298 */ 299 if (alloc_subdevices(dev, 3) < 0) 300 return -ENOMEM; 301 302 s = dev->subdevices + 0; 303 /* dev->read_subdev=s; */ 304 /* analog input subdevice */ 305 s->type = COMEDI_SUBD_AI; 306 s->subdev_flags = SDF_READABLE | SDF_GROUND; 307 s->n_chan = thisboard->ai_se_chans; 308 s->maxdata = (1 << thisboard->ai_bits) - 1; 309 s->range_table = &range_unknown; 310 s->len_chanlist = 1; /* This is the maximum chanlist length that */ 311 /* the board can handle */ 312 s->insn_read = cb_pcimdas_ai_rinsn; 313 314 s = dev->subdevices + 1; 315 /* analog output subdevice */ 316 s->type = COMEDI_SUBD_AO; 317 s->subdev_flags = SDF_WRITABLE; 318 s->n_chan = thisboard->ao_nchan; 319 s->maxdata = 1 << thisboard->ao_bits; 320 s->range_table = &range_unknown; /* ranges are hardware settable, but not software readable. */ 321 s->insn_write = &cb_pcimdas_ao_winsn; 322 s->insn_read = &cb_pcimdas_ao_rinsn; 323 324 s = dev->subdevices + 2; 325 /* digital i/o subdevice */ 326 if (thisboard->has_dio) 327 subdev_8255_init(dev, s, NULL, devpriv->BADR4); 328 else 329 s->type = COMEDI_SUBD_UNUSED; 330 331 return 1; 332} 333 334/* 335 * _detach is called to deconfigure a device. It should deallocate 336 * resources. 337 * This function is also called when _attach() fails, so it should be 338 * careful not to release resources that were not necessarily 339 * allocated by _attach(). dev->private and dev->subdevices are 340 * deallocated automatically by the core. 341 */ 342static int cb_pcimdas_detach(struct comedi_device *dev) 343{ 344 if (devpriv) { 345 dev_dbg(dev->hw_dev, "devpriv->BADR0 = 0x%lx\n", 346 devpriv->BADR0); 347 dev_dbg(dev->hw_dev, "devpriv->BADR1 = 0x%lx\n", 348 devpriv->BADR1); 349 dev_dbg(dev->hw_dev, "devpriv->BADR2 = 0x%lx\n", 350 devpriv->BADR2); 351 dev_dbg(dev->hw_dev, "devpriv->BADR3 = 0x%lx\n", 352 devpriv->BADR3); 353 dev_dbg(dev->hw_dev, "devpriv->BADR4 = 0x%lx\n", 354 devpriv->BADR4); 355 } 356 357 if (dev->irq) 358 free_irq(dev->irq, dev); 359 if (devpriv) { 360 if (devpriv->pci_dev) { 361 if (devpriv->BADR0) 362 comedi_pci_disable(devpriv->pci_dev); 363 pci_dev_put(devpriv->pci_dev); 364 } 365 } 366 367 return 0; 368} 369 370/* 371 * "instructions" read/write data in "one-shot" or "software-triggered" 372 * mode. 373 */ 374static int cb_pcimdas_ai_rinsn(struct comedi_device *dev, 375 struct comedi_subdevice *s, 376 struct comedi_insn *insn, unsigned int *data) 377{ 378 int n, i; 379 unsigned int d; 380 unsigned int busy; 381 int chan = CR_CHAN(insn->chanspec); 382 unsigned short chanlims; 383 int maxchans; 384 385 /* only support sw initiated reads from a single channel */ 386 387 /* check channel number */ 388 if ((inb(devpriv->BADR3 + 2) & 0x20) == 0) /* differential mode */ 389 maxchans = thisboard->ai_diff_chans; 390 else 391 maxchans = thisboard->ai_se_chans; 392 393 if (chan > (maxchans - 1)) 394 return -ETIMEDOUT; /* *** Wrong error code. Fixme. */ 395 396 /* configure for sw initiated read */ 397 d = inb(devpriv->BADR3 + 5); 398 if ((d & 0x03) > 0) { /* only reset if needed. */ 399 d = d & 0xfd; 400 outb(d, devpriv->BADR3 + 5); 401 } 402 outb(0x01, devpriv->BADR3 + 6); /* set bursting off, conversions on */ 403 outb(0x00, devpriv->BADR3 + 7); /* set range to 10V. UP/BP is controlled by a switch on the board */ 404 405 /* write channel limits to multiplexer, set Low (bits 0-3) and High (bits 4-7) channels to chan. */ 406 chanlims = chan | (chan << 4); 407 outb(chanlims, devpriv->BADR3 + 0); 408 409 /* convert n samples */ 410 for (n = 0; n < insn->n; n++) { 411 /* trigger conversion */ 412 outw(0, devpriv->BADR2 + 0); 413 414#define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */ 415 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */ 416 417 /* wait for conversion to end */ 418 for (i = 0; i < TIMEOUT; i++) { 419 busy = inb(devpriv->BADR3 + 2) & 0x80; 420 if (!busy) 421 break; 422 } 423 if (i == TIMEOUT) { 424 printk("timeout\n"); 425 return -ETIMEDOUT; 426 } 427 /* read data */ 428 d = inw(devpriv->BADR2 + 0); 429 430 /* mangle the data as necessary */ 431 /* d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed. */ 432 433 data[n] = d; 434 } 435 436 /* return the number of samples read/written */ 437 return n; 438} 439 440static int cb_pcimdas_ao_winsn(struct comedi_device *dev, 441 struct comedi_subdevice *s, 442 struct comedi_insn *insn, unsigned int *data) 443{ 444 int i; 445 int chan = CR_CHAN(insn->chanspec); 446 447 /* Writing a list of values to an AO channel is probably not 448 * very useful, but that's how the interface is defined. */ 449 for (i = 0; i < insn->n; i++) { 450 switch (chan) { 451 case 0: 452 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC0_OFFSET); 453 break; 454 case 1: 455 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC1_OFFSET); 456 break; 457 default: 458 return -1; 459 } 460 devpriv->ao_readback[chan] = data[i]; 461 } 462 463 /* return the number of samples read/written */ 464 return i; 465} 466 467/* AO subdevices should have a read insn as well as a write insn. 468 * Usually this means copying a value stored in devpriv. */ 469static int cb_pcimdas_ao_rinsn(struct comedi_device *dev, 470 struct comedi_subdevice *s, 471 struct comedi_insn *insn, unsigned int *data) 472{ 473 int i; 474 int chan = CR_CHAN(insn->chanspec); 475 476 for (i = 0; i < insn->n; i++) 477 data[i] = devpriv->ao_readback[chan]; 478 479 return i; 480} 481 482/* 483 * A convenient macro that defines init_module() and cleanup_module(), 484 * as necessary. 485 */ 486static int __devinit driver_cb_pcimdas_pci_probe(struct pci_dev *dev, 487 const struct pci_device_id 488 *ent) 489{ 490 return comedi_pci_auto_config(dev, driver_cb_pcimdas.driver_name); 491} 492 493static void __devexit driver_cb_pcimdas_pci_remove(struct pci_dev *dev) 494{ 495 comedi_pci_auto_unconfig(dev); 496} 497 498static struct pci_driver driver_cb_pcimdas_pci_driver = { 499 .id_table = cb_pcimdas_pci_table, 500 .probe = &driver_cb_pcimdas_pci_probe, 501 .remove = __devexit_p(&driver_cb_pcimdas_pci_remove) 502}; 503 504static int __init driver_cb_pcimdas_init_module(void) 505{ 506 int retval; 507 508 retval = comedi_driver_register(&driver_cb_pcimdas); 509 if (retval < 0) 510 return retval; 511 512 driver_cb_pcimdas_pci_driver.name = 513 (char *)driver_cb_pcimdas.driver_name; 514 return pci_register_driver(&driver_cb_pcimdas_pci_driver); 515} 516 517static void __exit driver_cb_pcimdas_cleanup_module(void) 518{ 519 pci_unregister_driver(&driver_cb_pcimdas_pci_driver); 520 comedi_driver_unregister(&driver_cb_pcimdas); 521} 522 523module_init(driver_cb_pcimdas_init_module); 524module_exit(driver_cb_pcimdas_cleanup_module); 525 526MODULE_AUTHOR("Comedi http://www.comedi.org"); 527MODULE_DESCRIPTION("Comedi low-level driver"); 528MODULE_LICENSE("GPL"); 529