18ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 28ffdff6aSGreg Kroah-Hartman /* 38ffdff6aSGreg Kroah-Hartman * addi_apci_16xx.c 48ffdff6aSGreg Kroah-Hartman * Copyright (C) 2004,2005 ADDI-DATA GmbH for the source code of this module. 58ffdff6aSGreg Kroah-Hartman * Project manager: S. Weber 68ffdff6aSGreg Kroah-Hartman * 78ffdff6aSGreg Kroah-Hartman * ADDI-DATA GmbH 88ffdff6aSGreg Kroah-Hartman * Dieselstrasse 3 98ffdff6aSGreg Kroah-Hartman * D-77833 Ottersweier 108ffdff6aSGreg Kroah-Hartman * Tel: +19(0)7223/9493-0 118ffdff6aSGreg Kroah-Hartman * Fax: +49(0)7223/9493-92 128ffdff6aSGreg Kroah-Hartman * http://www.addi-data.com 138ffdff6aSGreg Kroah-Hartman * info@addi-data.com 148ffdff6aSGreg Kroah-Hartman */ 158ffdff6aSGreg Kroah-Hartman 168ffdff6aSGreg Kroah-Hartman #include <linux/module.h> 17*df0e68c1SIan Abbott #include <linux/comedi/comedi_pci.h> 188ffdff6aSGreg Kroah-Hartman 198ffdff6aSGreg Kroah-Hartman /* 208ffdff6aSGreg Kroah-Hartman * Register I/O map 218ffdff6aSGreg Kroah-Hartman */ 228ffdff6aSGreg Kroah-Hartman #define APCI16XX_IN_REG(x) (((x) * 4) + 0x08) 238ffdff6aSGreg Kroah-Hartman #define APCI16XX_OUT_REG(x) (((x) * 4) + 0x14) 248ffdff6aSGreg Kroah-Hartman #define APCI16XX_DIR_REG(x) (((x) * 4) + 0x20) 258ffdff6aSGreg Kroah-Hartman 268ffdff6aSGreg Kroah-Hartman enum apci16xx_boardid { 278ffdff6aSGreg Kroah-Hartman BOARD_APCI1648, 288ffdff6aSGreg Kroah-Hartman BOARD_APCI1696, 298ffdff6aSGreg Kroah-Hartman }; 308ffdff6aSGreg Kroah-Hartman 318ffdff6aSGreg Kroah-Hartman struct apci16xx_boardinfo { 328ffdff6aSGreg Kroah-Hartman const char *name; 338ffdff6aSGreg Kroah-Hartman int n_chan; 348ffdff6aSGreg Kroah-Hartman }; 358ffdff6aSGreg Kroah-Hartman 368ffdff6aSGreg Kroah-Hartman static const struct apci16xx_boardinfo apci16xx_boardtypes[] = { 378ffdff6aSGreg Kroah-Hartman [BOARD_APCI1648] = { 388ffdff6aSGreg Kroah-Hartman .name = "apci1648", 398ffdff6aSGreg Kroah-Hartman .n_chan = 48, /* 2 subdevices */ 408ffdff6aSGreg Kroah-Hartman }, 418ffdff6aSGreg Kroah-Hartman [BOARD_APCI1696] = { 428ffdff6aSGreg Kroah-Hartman .name = "apci1696", 438ffdff6aSGreg Kroah-Hartman .n_chan = 96, /* 3 subdevices */ 448ffdff6aSGreg Kroah-Hartman }, 458ffdff6aSGreg Kroah-Hartman }; 468ffdff6aSGreg Kroah-Hartman 478ffdff6aSGreg Kroah-Hartman static int apci16xx_insn_config(struct comedi_device *dev, 488ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s, 498ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn, 508ffdff6aSGreg Kroah-Hartman unsigned int *data) 518ffdff6aSGreg Kroah-Hartman { 528ffdff6aSGreg Kroah-Hartman unsigned int chan = CR_CHAN(insn->chanspec); 538ffdff6aSGreg Kroah-Hartman unsigned int mask; 548ffdff6aSGreg Kroah-Hartman int ret; 558ffdff6aSGreg Kroah-Hartman 568ffdff6aSGreg Kroah-Hartman if (chan < 8) 578ffdff6aSGreg Kroah-Hartman mask = 0x000000ff; 588ffdff6aSGreg Kroah-Hartman else if (chan < 16) 598ffdff6aSGreg Kroah-Hartman mask = 0x0000ff00; 608ffdff6aSGreg Kroah-Hartman else if (chan < 24) 618ffdff6aSGreg Kroah-Hartman mask = 0x00ff0000; 628ffdff6aSGreg Kroah-Hartman else 638ffdff6aSGreg Kroah-Hartman mask = 0xff000000; 648ffdff6aSGreg Kroah-Hartman 658ffdff6aSGreg Kroah-Hartman ret = comedi_dio_insn_config(dev, s, insn, data, mask); 668ffdff6aSGreg Kroah-Hartman if (ret) 678ffdff6aSGreg Kroah-Hartman return ret; 688ffdff6aSGreg Kroah-Hartman 698ffdff6aSGreg Kroah-Hartman outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(s->index)); 708ffdff6aSGreg Kroah-Hartman 718ffdff6aSGreg Kroah-Hartman return insn->n; 728ffdff6aSGreg Kroah-Hartman } 738ffdff6aSGreg Kroah-Hartman 748ffdff6aSGreg Kroah-Hartman static int apci16xx_dio_insn_bits(struct comedi_device *dev, 758ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s, 768ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn, 778ffdff6aSGreg Kroah-Hartman unsigned int *data) 788ffdff6aSGreg Kroah-Hartman { 798ffdff6aSGreg Kroah-Hartman if (comedi_dio_update_state(s, data)) 808ffdff6aSGreg Kroah-Hartman outl(s->state, dev->iobase + APCI16XX_OUT_REG(s->index)); 818ffdff6aSGreg Kroah-Hartman 828ffdff6aSGreg Kroah-Hartman data[1] = inl(dev->iobase + APCI16XX_IN_REG(s->index)); 838ffdff6aSGreg Kroah-Hartman 848ffdff6aSGreg Kroah-Hartman return insn->n; 858ffdff6aSGreg Kroah-Hartman } 868ffdff6aSGreg Kroah-Hartman 878ffdff6aSGreg Kroah-Hartman static int apci16xx_auto_attach(struct comedi_device *dev, 888ffdff6aSGreg Kroah-Hartman unsigned long context) 898ffdff6aSGreg Kroah-Hartman { 908ffdff6aSGreg Kroah-Hartman struct pci_dev *pcidev = comedi_to_pci_dev(dev); 918ffdff6aSGreg Kroah-Hartman const struct apci16xx_boardinfo *board = NULL; 928ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s; 938ffdff6aSGreg Kroah-Hartman unsigned int n_subdevs; 948ffdff6aSGreg Kroah-Hartman unsigned int last; 958ffdff6aSGreg Kroah-Hartman int i; 968ffdff6aSGreg Kroah-Hartman int ret; 978ffdff6aSGreg Kroah-Hartman 988ffdff6aSGreg Kroah-Hartman if (context < ARRAY_SIZE(apci16xx_boardtypes)) 998ffdff6aSGreg Kroah-Hartman board = &apci16xx_boardtypes[context]; 1008ffdff6aSGreg Kroah-Hartman if (!board) 1018ffdff6aSGreg Kroah-Hartman return -ENODEV; 1028ffdff6aSGreg Kroah-Hartman dev->board_ptr = board; 1038ffdff6aSGreg Kroah-Hartman dev->board_name = board->name; 1048ffdff6aSGreg Kroah-Hartman 1058ffdff6aSGreg Kroah-Hartman ret = comedi_pci_enable(dev); 1068ffdff6aSGreg Kroah-Hartman if (ret) 1078ffdff6aSGreg Kroah-Hartman return ret; 1088ffdff6aSGreg Kroah-Hartman 1098ffdff6aSGreg Kroah-Hartman dev->iobase = pci_resource_start(pcidev, 0); 1108ffdff6aSGreg Kroah-Hartman 1118ffdff6aSGreg Kroah-Hartman /* 1128ffdff6aSGreg Kroah-Hartman * Work out the number of subdevices needed to support all the 1138ffdff6aSGreg Kroah-Hartman * digital i/o channels on the board. Each subdevice supports 1148ffdff6aSGreg Kroah-Hartman * up to 32 channels. 1158ffdff6aSGreg Kroah-Hartman */ 1168ffdff6aSGreg Kroah-Hartman n_subdevs = board->n_chan / 32; 1178ffdff6aSGreg Kroah-Hartman if ((n_subdevs * 32) < board->n_chan) { 1188ffdff6aSGreg Kroah-Hartman last = board->n_chan - (n_subdevs * 32); 1198ffdff6aSGreg Kroah-Hartman n_subdevs++; 1208ffdff6aSGreg Kroah-Hartman } else { 1218ffdff6aSGreg Kroah-Hartman last = 0; 1228ffdff6aSGreg Kroah-Hartman } 1238ffdff6aSGreg Kroah-Hartman 1248ffdff6aSGreg Kroah-Hartman ret = comedi_alloc_subdevices(dev, n_subdevs); 1258ffdff6aSGreg Kroah-Hartman if (ret) 1268ffdff6aSGreg Kroah-Hartman return ret; 1278ffdff6aSGreg Kroah-Hartman 1288ffdff6aSGreg Kroah-Hartman /* Initialize the TTL digital i/o subdevices */ 1298ffdff6aSGreg Kroah-Hartman for (i = 0; i < n_subdevs; i++) { 1308ffdff6aSGreg Kroah-Hartman s = &dev->subdevices[i]; 1318ffdff6aSGreg Kroah-Hartman s->type = COMEDI_SUBD_DIO; 1328ffdff6aSGreg Kroah-Hartman s->subdev_flags = SDF_WRITABLE | SDF_READABLE; 1338ffdff6aSGreg Kroah-Hartman s->n_chan = ((i * 32) < board->n_chan) ? 32 : last; 1348ffdff6aSGreg Kroah-Hartman s->maxdata = 1; 1358ffdff6aSGreg Kroah-Hartman s->range_table = &range_digital; 1368ffdff6aSGreg Kroah-Hartman s->insn_config = apci16xx_insn_config; 1378ffdff6aSGreg Kroah-Hartman s->insn_bits = apci16xx_dio_insn_bits; 1388ffdff6aSGreg Kroah-Hartman 1398ffdff6aSGreg Kroah-Hartman /* Default all channels to inputs */ 1408ffdff6aSGreg Kroah-Hartman s->io_bits = 0; 1418ffdff6aSGreg Kroah-Hartman outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(i)); 1428ffdff6aSGreg Kroah-Hartman } 1438ffdff6aSGreg Kroah-Hartman 1448ffdff6aSGreg Kroah-Hartman return 0; 1458ffdff6aSGreg Kroah-Hartman } 1468ffdff6aSGreg Kroah-Hartman 1478ffdff6aSGreg Kroah-Hartman static struct comedi_driver apci16xx_driver = { 1488ffdff6aSGreg Kroah-Hartman .driver_name = "addi_apci_16xx", 1498ffdff6aSGreg Kroah-Hartman .module = THIS_MODULE, 1508ffdff6aSGreg Kroah-Hartman .auto_attach = apci16xx_auto_attach, 1518ffdff6aSGreg Kroah-Hartman .detach = comedi_pci_detach, 1528ffdff6aSGreg Kroah-Hartman }; 1538ffdff6aSGreg Kroah-Hartman 1548ffdff6aSGreg Kroah-Hartman static int apci16xx_pci_probe(struct pci_dev *dev, 1558ffdff6aSGreg Kroah-Hartman const struct pci_device_id *id) 1568ffdff6aSGreg Kroah-Hartman { 1578ffdff6aSGreg Kroah-Hartman return comedi_pci_auto_config(dev, &apci16xx_driver, id->driver_data); 1588ffdff6aSGreg Kroah-Hartman } 1598ffdff6aSGreg Kroah-Hartman 1608ffdff6aSGreg Kroah-Hartman static const struct pci_device_id apci16xx_pci_table[] = { 1618ffdff6aSGreg Kroah-Hartman { PCI_VDEVICE(ADDIDATA, 0x1009), BOARD_APCI1648 }, 1628ffdff6aSGreg Kroah-Hartman { PCI_VDEVICE(ADDIDATA, 0x100a), BOARD_APCI1696 }, 1638ffdff6aSGreg Kroah-Hartman { 0 } 1648ffdff6aSGreg Kroah-Hartman }; 1658ffdff6aSGreg Kroah-Hartman MODULE_DEVICE_TABLE(pci, apci16xx_pci_table); 1668ffdff6aSGreg Kroah-Hartman 1678ffdff6aSGreg Kroah-Hartman static struct pci_driver apci16xx_pci_driver = { 1688ffdff6aSGreg Kroah-Hartman .name = "addi_apci_16xx", 1698ffdff6aSGreg Kroah-Hartman .id_table = apci16xx_pci_table, 1708ffdff6aSGreg Kroah-Hartman .probe = apci16xx_pci_probe, 1718ffdff6aSGreg Kroah-Hartman .remove = comedi_pci_auto_unconfig, 1728ffdff6aSGreg Kroah-Hartman }; 1738ffdff6aSGreg Kroah-Hartman module_comedi_pci_driver(apci16xx_driver, apci16xx_pci_driver); 1748ffdff6aSGreg Kroah-Hartman 1758ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION("ADDI-DATA APCI-1648/1696, TTL I/O boards"); 1768ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("Comedi https://www.comedi.org"); 1778ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL"); 178