1*8ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 2*8ffdff6aSGreg Kroah-Hartman /* 3*8ffdff6aSGreg Kroah-Hartman * addi_apci_16xx.c 4*8ffdff6aSGreg Kroah-Hartman * Copyright (C) 2004,2005 ADDI-DATA GmbH for the source code of this module. 5*8ffdff6aSGreg Kroah-Hartman * Project manager: S. Weber 6*8ffdff6aSGreg Kroah-Hartman * 7*8ffdff6aSGreg Kroah-Hartman * ADDI-DATA GmbH 8*8ffdff6aSGreg Kroah-Hartman * Dieselstrasse 3 9*8ffdff6aSGreg Kroah-Hartman * D-77833 Ottersweier 10*8ffdff6aSGreg Kroah-Hartman * Tel: +19(0)7223/9493-0 11*8ffdff6aSGreg Kroah-Hartman * Fax: +49(0)7223/9493-92 12*8ffdff6aSGreg Kroah-Hartman * http://www.addi-data.com 13*8ffdff6aSGreg Kroah-Hartman * info@addi-data.com 14*8ffdff6aSGreg Kroah-Hartman */ 15*8ffdff6aSGreg Kroah-Hartman 16*8ffdff6aSGreg Kroah-Hartman #include <linux/module.h> 17*8ffdff6aSGreg Kroah-Hartman 18*8ffdff6aSGreg Kroah-Hartman #include "../comedi_pci.h" 19*8ffdff6aSGreg Kroah-Hartman 20*8ffdff6aSGreg Kroah-Hartman /* 21*8ffdff6aSGreg Kroah-Hartman * Register I/O map 22*8ffdff6aSGreg Kroah-Hartman */ 23*8ffdff6aSGreg Kroah-Hartman #define APCI16XX_IN_REG(x) (((x) * 4) + 0x08) 24*8ffdff6aSGreg Kroah-Hartman #define APCI16XX_OUT_REG(x) (((x) * 4) + 0x14) 25*8ffdff6aSGreg Kroah-Hartman #define APCI16XX_DIR_REG(x) (((x) * 4) + 0x20) 26*8ffdff6aSGreg Kroah-Hartman 27*8ffdff6aSGreg Kroah-Hartman enum apci16xx_boardid { 28*8ffdff6aSGreg Kroah-Hartman BOARD_APCI1648, 29*8ffdff6aSGreg Kroah-Hartman BOARD_APCI1696, 30*8ffdff6aSGreg Kroah-Hartman }; 31*8ffdff6aSGreg Kroah-Hartman 32*8ffdff6aSGreg Kroah-Hartman struct apci16xx_boardinfo { 33*8ffdff6aSGreg Kroah-Hartman const char *name; 34*8ffdff6aSGreg Kroah-Hartman int n_chan; 35*8ffdff6aSGreg Kroah-Hartman }; 36*8ffdff6aSGreg Kroah-Hartman 37*8ffdff6aSGreg Kroah-Hartman static const struct apci16xx_boardinfo apci16xx_boardtypes[] = { 38*8ffdff6aSGreg Kroah-Hartman [BOARD_APCI1648] = { 39*8ffdff6aSGreg Kroah-Hartman .name = "apci1648", 40*8ffdff6aSGreg Kroah-Hartman .n_chan = 48, /* 2 subdevices */ 41*8ffdff6aSGreg Kroah-Hartman }, 42*8ffdff6aSGreg Kroah-Hartman [BOARD_APCI1696] = { 43*8ffdff6aSGreg Kroah-Hartman .name = "apci1696", 44*8ffdff6aSGreg Kroah-Hartman .n_chan = 96, /* 3 subdevices */ 45*8ffdff6aSGreg Kroah-Hartman }, 46*8ffdff6aSGreg Kroah-Hartman }; 47*8ffdff6aSGreg Kroah-Hartman 48*8ffdff6aSGreg Kroah-Hartman static int apci16xx_insn_config(struct comedi_device *dev, 49*8ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s, 50*8ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn, 51*8ffdff6aSGreg Kroah-Hartman unsigned int *data) 52*8ffdff6aSGreg Kroah-Hartman { 53*8ffdff6aSGreg Kroah-Hartman unsigned int chan = CR_CHAN(insn->chanspec); 54*8ffdff6aSGreg Kroah-Hartman unsigned int mask; 55*8ffdff6aSGreg Kroah-Hartman int ret; 56*8ffdff6aSGreg Kroah-Hartman 57*8ffdff6aSGreg Kroah-Hartman if (chan < 8) 58*8ffdff6aSGreg Kroah-Hartman mask = 0x000000ff; 59*8ffdff6aSGreg Kroah-Hartman else if (chan < 16) 60*8ffdff6aSGreg Kroah-Hartman mask = 0x0000ff00; 61*8ffdff6aSGreg Kroah-Hartman else if (chan < 24) 62*8ffdff6aSGreg Kroah-Hartman mask = 0x00ff0000; 63*8ffdff6aSGreg Kroah-Hartman else 64*8ffdff6aSGreg Kroah-Hartman mask = 0xff000000; 65*8ffdff6aSGreg Kroah-Hartman 66*8ffdff6aSGreg Kroah-Hartman ret = comedi_dio_insn_config(dev, s, insn, data, mask); 67*8ffdff6aSGreg Kroah-Hartman if (ret) 68*8ffdff6aSGreg Kroah-Hartman return ret; 69*8ffdff6aSGreg Kroah-Hartman 70*8ffdff6aSGreg Kroah-Hartman outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(s->index)); 71*8ffdff6aSGreg Kroah-Hartman 72*8ffdff6aSGreg Kroah-Hartman return insn->n; 73*8ffdff6aSGreg Kroah-Hartman } 74*8ffdff6aSGreg Kroah-Hartman 75*8ffdff6aSGreg Kroah-Hartman static int apci16xx_dio_insn_bits(struct comedi_device *dev, 76*8ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s, 77*8ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn, 78*8ffdff6aSGreg Kroah-Hartman unsigned int *data) 79*8ffdff6aSGreg Kroah-Hartman { 80*8ffdff6aSGreg Kroah-Hartman if (comedi_dio_update_state(s, data)) 81*8ffdff6aSGreg Kroah-Hartman outl(s->state, dev->iobase + APCI16XX_OUT_REG(s->index)); 82*8ffdff6aSGreg Kroah-Hartman 83*8ffdff6aSGreg Kroah-Hartman data[1] = inl(dev->iobase + APCI16XX_IN_REG(s->index)); 84*8ffdff6aSGreg Kroah-Hartman 85*8ffdff6aSGreg Kroah-Hartman return insn->n; 86*8ffdff6aSGreg Kroah-Hartman } 87*8ffdff6aSGreg Kroah-Hartman 88*8ffdff6aSGreg Kroah-Hartman static int apci16xx_auto_attach(struct comedi_device *dev, 89*8ffdff6aSGreg Kroah-Hartman unsigned long context) 90*8ffdff6aSGreg Kroah-Hartman { 91*8ffdff6aSGreg Kroah-Hartman struct pci_dev *pcidev = comedi_to_pci_dev(dev); 92*8ffdff6aSGreg Kroah-Hartman const struct apci16xx_boardinfo *board = NULL; 93*8ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s; 94*8ffdff6aSGreg Kroah-Hartman unsigned int n_subdevs; 95*8ffdff6aSGreg Kroah-Hartman unsigned int last; 96*8ffdff6aSGreg Kroah-Hartman int i; 97*8ffdff6aSGreg Kroah-Hartman int ret; 98*8ffdff6aSGreg Kroah-Hartman 99*8ffdff6aSGreg Kroah-Hartman if (context < ARRAY_SIZE(apci16xx_boardtypes)) 100*8ffdff6aSGreg Kroah-Hartman board = &apci16xx_boardtypes[context]; 101*8ffdff6aSGreg Kroah-Hartman if (!board) 102*8ffdff6aSGreg Kroah-Hartman return -ENODEV; 103*8ffdff6aSGreg Kroah-Hartman dev->board_ptr = board; 104*8ffdff6aSGreg Kroah-Hartman dev->board_name = board->name; 105*8ffdff6aSGreg Kroah-Hartman 106*8ffdff6aSGreg Kroah-Hartman ret = comedi_pci_enable(dev); 107*8ffdff6aSGreg Kroah-Hartman if (ret) 108*8ffdff6aSGreg Kroah-Hartman return ret; 109*8ffdff6aSGreg Kroah-Hartman 110*8ffdff6aSGreg Kroah-Hartman dev->iobase = pci_resource_start(pcidev, 0); 111*8ffdff6aSGreg Kroah-Hartman 112*8ffdff6aSGreg Kroah-Hartman /* 113*8ffdff6aSGreg Kroah-Hartman * Work out the number of subdevices needed to support all the 114*8ffdff6aSGreg Kroah-Hartman * digital i/o channels on the board. Each subdevice supports 115*8ffdff6aSGreg Kroah-Hartman * up to 32 channels. 116*8ffdff6aSGreg Kroah-Hartman */ 117*8ffdff6aSGreg Kroah-Hartman n_subdevs = board->n_chan / 32; 118*8ffdff6aSGreg Kroah-Hartman if ((n_subdevs * 32) < board->n_chan) { 119*8ffdff6aSGreg Kroah-Hartman last = board->n_chan - (n_subdevs * 32); 120*8ffdff6aSGreg Kroah-Hartman n_subdevs++; 121*8ffdff6aSGreg Kroah-Hartman } else { 122*8ffdff6aSGreg Kroah-Hartman last = 0; 123*8ffdff6aSGreg Kroah-Hartman } 124*8ffdff6aSGreg Kroah-Hartman 125*8ffdff6aSGreg Kroah-Hartman ret = comedi_alloc_subdevices(dev, n_subdevs); 126*8ffdff6aSGreg Kroah-Hartman if (ret) 127*8ffdff6aSGreg Kroah-Hartman return ret; 128*8ffdff6aSGreg Kroah-Hartman 129*8ffdff6aSGreg Kroah-Hartman /* Initialize the TTL digital i/o subdevices */ 130*8ffdff6aSGreg Kroah-Hartman for (i = 0; i < n_subdevs; i++) { 131*8ffdff6aSGreg Kroah-Hartman s = &dev->subdevices[i]; 132*8ffdff6aSGreg Kroah-Hartman s->type = COMEDI_SUBD_DIO; 133*8ffdff6aSGreg Kroah-Hartman s->subdev_flags = SDF_WRITABLE | SDF_READABLE; 134*8ffdff6aSGreg Kroah-Hartman s->n_chan = ((i * 32) < board->n_chan) ? 32 : last; 135*8ffdff6aSGreg Kroah-Hartman s->maxdata = 1; 136*8ffdff6aSGreg Kroah-Hartman s->range_table = &range_digital; 137*8ffdff6aSGreg Kroah-Hartman s->insn_config = apci16xx_insn_config; 138*8ffdff6aSGreg Kroah-Hartman s->insn_bits = apci16xx_dio_insn_bits; 139*8ffdff6aSGreg Kroah-Hartman 140*8ffdff6aSGreg Kroah-Hartman /* Default all channels to inputs */ 141*8ffdff6aSGreg Kroah-Hartman s->io_bits = 0; 142*8ffdff6aSGreg Kroah-Hartman outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(i)); 143*8ffdff6aSGreg Kroah-Hartman } 144*8ffdff6aSGreg Kroah-Hartman 145*8ffdff6aSGreg Kroah-Hartman return 0; 146*8ffdff6aSGreg Kroah-Hartman } 147*8ffdff6aSGreg Kroah-Hartman 148*8ffdff6aSGreg Kroah-Hartman static struct comedi_driver apci16xx_driver = { 149*8ffdff6aSGreg Kroah-Hartman .driver_name = "addi_apci_16xx", 150*8ffdff6aSGreg Kroah-Hartman .module = THIS_MODULE, 151*8ffdff6aSGreg Kroah-Hartman .auto_attach = apci16xx_auto_attach, 152*8ffdff6aSGreg Kroah-Hartman .detach = comedi_pci_detach, 153*8ffdff6aSGreg Kroah-Hartman }; 154*8ffdff6aSGreg Kroah-Hartman 155*8ffdff6aSGreg Kroah-Hartman static int apci16xx_pci_probe(struct pci_dev *dev, 156*8ffdff6aSGreg Kroah-Hartman const struct pci_device_id *id) 157*8ffdff6aSGreg Kroah-Hartman { 158*8ffdff6aSGreg Kroah-Hartman return comedi_pci_auto_config(dev, &apci16xx_driver, id->driver_data); 159*8ffdff6aSGreg Kroah-Hartman } 160*8ffdff6aSGreg Kroah-Hartman 161*8ffdff6aSGreg Kroah-Hartman static const struct pci_device_id apci16xx_pci_table[] = { 162*8ffdff6aSGreg Kroah-Hartman { PCI_VDEVICE(ADDIDATA, 0x1009), BOARD_APCI1648 }, 163*8ffdff6aSGreg Kroah-Hartman { PCI_VDEVICE(ADDIDATA, 0x100a), BOARD_APCI1696 }, 164*8ffdff6aSGreg Kroah-Hartman { 0 } 165*8ffdff6aSGreg Kroah-Hartman }; 166*8ffdff6aSGreg Kroah-Hartman MODULE_DEVICE_TABLE(pci, apci16xx_pci_table); 167*8ffdff6aSGreg Kroah-Hartman 168*8ffdff6aSGreg Kroah-Hartman static struct pci_driver apci16xx_pci_driver = { 169*8ffdff6aSGreg Kroah-Hartman .name = "addi_apci_16xx", 170*8ffdff6aSGreg Kroah-Hartman .id_table = apci16xx_pci_table, 171*8ffdff6aSGreg Kroah-Hartman .probe = apci16xx_pci_probe, 172*8ffdff6aSGreg Kroah-Hartman .remove = comedi_pci_auto_unconfig, 173*8ffdff6aSGreg Kroah-Hartman }; 174*8ffdff6aSGreg Kroah-Hartman module_comedi_pci_driver(apci16xx_driver, apci16xx_pci_driver); 175*8ffdff6aSGreg Kroah-Hartman 176*8ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION("ADDI-DATA APCI-1648/1696, TTL I/O boards"); 177*8ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("Comedi https://www.comedi.org"); 178*8ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL"); 179