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