10ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten/* 20ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * addi_apci_16xx.c 30ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * Copyright (C) 2004,2005 ADDI-DATA GmbH for the source code of this module. 40ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * Project manager: S. Weber 50ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * 60ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * ADDI-DATA GmbH 70ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * Dieselstrasse 3 80ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * D-77833 Ottersweier 90ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * Tel: +19(0)7223/9493-0 100ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * Fax: +49(0)7223/9493-92 110ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * http://www.addi-data.com 120ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * info@addi-data.com 130ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * 140ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * This program is free software; you can redistribute it and/or modify it 150ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * under the terms of the GNU General Public License as published by the 160ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * Free Software Foundation; either version 2 of the License, or (at your 170ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * option) any later version. 180ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * 190ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * This program is distributed in the hope that it will be useful, but WITHOUT 200ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 210ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 220ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten * more details. 230ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten */ 240ecb3ea9cef6fe92860d065a0d9a87c9f7f50243H Hartley Sweeten 25ce157f8032bbd46d9427034c335b0afd751da25dH Hartley Sweeten#include <linux/module.h> 2633782dd5edf8db3cdb7c81a3523bf743dd0209b7H Hartley Sweeten#include <linux/pci.h> 2733782dd5edf8db3cdb7c81a3523bf743dd0209b7H Hartley Sweeten 283d41c44370a9a1e78e53c9997289347ec97d46eeH Hartley Sweeten#include "../comedidev.h" 293d41c44370a9a1e78e53c9997289347ec97d46eeH Hartley Sweeten 3092eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten/* 3192eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten * Register I/O map 3292eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten */ 3392eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten#define APCI16XX_IN_REG(x) (((x) * 4) + 0x08) 3492eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten#define APCI16XX_OUT_REG(x) (((x) * 4) + 0x14) 3592eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten#define APCI16XX_DIR_REG(x) (((x) * 4) + 0x20) 36317285d71acccbda2fbab7e53d6b33c52a151a32H Hartley Sweeten 37a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweetenenum apci16xx_boardid { 38a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten BOARD_APCI1648, 39a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten BOARD_APCI1696, 40a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten}; 41a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten 422a2e63dd98ff5310b4677e177939dd2232e0c1c3H Hartley Sweetenstruct apci16xx_boardinfo { 432a2e63dd98ff5310b4677e177939dd2232e0c1c3H Hartley Sweeten const char *name; 442a2e63dd98ff5310b4677e177939dd2232e0c1c3H Hartley Sweeten int n_chan; 452a2e63dd98ff5310b4677e177939dd2232e0c1c3H Hartley Sweeten}; 462a2e63dd98ff5310b4677e177939dd2232e0c1c3H Hartley Sweeten 472a2e63dd98ff5310b4677e177939dd2232e0c1c3H Hartley Sweetenstatic const struct apci16xx_boardinfo apci16xx_boardtypes[] = { 48a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten [BOARD_APCI1648] = { 492a2e63dd98ff5310b4677e177939dd2232e0c1c3H Hartley Sweeten .name = "apci1648", 5092eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten .n_chan = 48, /* 2 subdevices */ 51a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten }, 52a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten [BOARD_APCI1696] = { 532a2e63dd98ff5310b4677e177939dd2232e0c1c3H Hartley Sweeten .name = "apci1696", 5492eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten .n_chan = 96, /* 3 subdevices */ 55c0a053b8b24d72413a82a16a41f635e6040ccf34H Hartley Sweeten }, 56c0a053b8b24d72413a82a16a41f635e6040ccf34H Hartley Sweeten}; 57c0a053b8b24d72413a82a16a41f635e6040ccf34H Hartley Sweeten 5892eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweetenstatic int apci16xx_insn_config(struct comedi_device *dev, 5992eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten struct comedi_subdevice *s, 6092eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten struct comedi_insn *insn, 6192eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten unsigned int *data) 6292eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten{ 635dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten unsigned int chan = CR_CHAN(insn->chanspec); 645dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten unsigned int mask; 655dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten int ret; 665dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten 675dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten if (chan < 8) 685dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten mask = 0x000000ff; 695dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten else if (chan < 16) 705dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten mask = 0x0000ff00; 715dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten else if (chan < 24) 725dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten mask = 0x00ff0000; 7392eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten else 745dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten mask = 0xff000000; 755dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten 765dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten ret = comedi_dio_insn_config(dev, s, insn, data, mask); 775dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten if (ret) 785dacadcca3176e2b1f8db662e503c080484d71faH Hartley Sweeten return ret; 7992eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten 8092eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(s->index)); 8192eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten 8292eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten return insn->n; 8392eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten} 8492eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten 8592eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweetenstatic int apci16xx_dio_insn_bits(struct comedi_device *dev, 8692eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten struct comedi_subdevice *s, 8792eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten struct comedi_insn *insn, 8892eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten unsigned int *data) 8992eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten{ 90a485f4d60e7de92b65e3f03ccd6c3b8cb0062c32H Hartley Sweeten if (comedi_dio_update_state(s, data)) 9192eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten outl(s->state, dev->iobase + APCI16XX_OUT_REG(s->index)); 9292eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten 9392eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten data[1] = inl(dev->iobase + APCI16XX_IN_REG(s->index)); 9492eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten 9592eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten return insn->n; 9692eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten} 9792eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten 98368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweetenstatic int apci16xx_auto_attach(struct comedi_device *dev, 99a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten unsigned long context) 100368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten{ 101368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten struct pci_dev *pcidev = comedi_to_pci_dev(dev); 102a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten const struct apci16xx_boardinfo *board = NULL; 103368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten struct comedi_subdevice *s; 10492eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten unsigned int n_subdevs; 10592eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten unsigned int last; 10692eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten int i; 107b6e7714cc261ef478f95449a32565d41378c9a87H Hartley Sweeten int ret; 108368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten 109a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten if (context < ARRAY_SIZE(apci16xx_boardtypes)) 110a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten board = &apci16xx_boardtypes[context]; 1110e2bd50b393084a338a6a21063ef012c5a2720fbH Hartley Sweeten if (!board) 112368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten return -ENODEV; 1130e2bd50b393084a338a6a21063ef012c5a2720fbH Hartley Sweeten dev->board_ptr = board; 1140e2bd50b393084a338a6a21063ef012c5a2720fbH Hartley Sweeten dev->board_name = board->name; 115368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten 116818f569fe930c5b8a05d1a44ece3c63c99c13c88H Hartley Sweeten ret = comedi_pci_enable(dev); 117368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten if (ret) 118368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten return ret; 119368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten 120ebea8c69d323acfc13f85b067a8608ec10794638H Hartley Sweeten dev->iobase = pci_resource_start(pcidev, 0); 121368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten 12292eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten /* 12392eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten * Work out the nubmer of subdevices needed to support all the 12492eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten * digital i/o channels on the board. Each subdevice supports 12592eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten * up to 32 channels. 12692eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten */ 12792eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten n_subdevs = board->n_chan / 32; 12892eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten if ((n_subdevs * 32) < board->n_chan) { 12992eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten last = board->n_chan - (n_subdevs * 32); 13092eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten n_subdevs++; 13192eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten } else { 13292eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten last = 0; 13392eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten } 13492eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten 13592eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten ret = comedi_alloc_subdevices(dev, n_subdevs); 136368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten if (ret) 137368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten return ret; 138368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten 13992eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten /* Initialize the TTL digital i/o subdevices */ 14092eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten for (i = 0; i < n_subdevs; i++) { 14192eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s = &dev->subdevices[i]; 14292eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s->type = COMEDI_SUBD_DIO; 14392eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s->subdev_flags = SDF_WRITEABLE | SDF_READABLE; 14492eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s->n_chan = ((i * 32) < board->n_chan) ? 32 : last; 14592eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s->maxdata = 1; 14692eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s->range_table = &range_digital; 14792eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s->insn_config = apci16xx_insn_config; 14892eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s->insn_bits = apci16xx_dio_insn_bits; 14992eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten 15092eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten /* Default all channels to inputs */ 15192eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten s->io_bits = 0; 15292eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(i)); 15392eb37959f0e98376969e97bb6518c0f4b738241H Hartley Sweeten } 154368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten 155368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten return 0; 156368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten} 157368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten 15820a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweetenstatic struct comedi_driver apci16xx_driver = { 15920a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten .driver_name = "addi_apci_16xx", 16020a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten .module = THIS_MODULE, 161368463328badaa5431416574d9a1f60de8ae04cfH Hartley Sweeten .auto_attach = apci16xx_auto_attach, 162aac307f9dd5ce1fe651140a036ab4b0a0571b54aH Hartley Sweeten .detach = comedi_pci_detach, 16320a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten}; 16420a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten 165a690b7e535f2f97a3a05ee570715abeb60a8910fBill Pembertonstatic int apci16xx_pci_probe(struct pci_dev *dev, 166b8f4ac237e382accd4b30c75043939f7ed9e79a6H Hartley Sweeten const struct pci_device_id *id) 16720a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten{ 168b8f4ac237e382accd4b30c75043939f7ed9e79a6H Hartley Sweeten return comedi_pci_auto_config(dev, &apci16xx_driver, id->driver_data); 16920a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten} 17020a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten 17141e043fcfa2236bb2c4a8335eb09f4c8cee224b3Jingoo Hanstatic const struct pci_device_id apci16xx_pci_table[] = { 172a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten { PCI_VDEVICE(ADDIDATA, 0x1009), BOARD_APCI1648 }, 173a4732f354487ba42ad3dc22dbdab33a3137e6041H Hartley Sweeten { PCI_VDEVICE(ADDIDATA, 0x100a), BOARD_APCI1696 }, 174317285d71acccbda2fbab7e53d6b33c52a151a32H Hartley Sweeten { 0 } 175317285d71acccbda2fbab7e53d6b33c52a151a32H Hartley Sweeten}; 17620a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley SweetenMODULE_DEVICE_TABLE(pci, apci16xx_pci_table); 177317285d71acccbda2fbab7e53d6b33c52a151a32H Hartley Sweeten 17820a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweetenstatic struct pci_driver apci16xx_pci_driver = { 17920a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten .name = "addi_apci_16xx", 18020a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten .id_table = apci16xx_pci_table, 18120a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten .probe = apci16xx_pci_probe, 1829901a4d75d007686e8f6473189cafc4b216b7449Peter Huewe .remove = comedi_pci_auto_unconfig, 18320a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweeten}; 18420a22b706b8ee37d1a2282f2c9bf7f2c73a5a7a5H Hartley Sweetenmodule_comedi_pci_driver(apci16xx_driver, apci16xx_pci_driver); 18590f703d30dd3e0c16ff80f35e34e511385a05ad5Arun Thomas 1865e72c1744fda105e0f1de6e33bd901a4df1bbaedH Hartley SweetenMODULE_DESCRIPTION("ADDI-DATA APCI-1648/1696, TTL I/O boards"); 18790f703d30dd3e0c16ff80f35e34e511385a05ad5Arun ThomasMODULE_AUTHOR("Comedi http://www.comedi.org"); 18890f703d30dd3e0c16ff80f35e34e511385a05ad5Arun ThomasMODULE_LICENSE("GPL"); 189