c897d39f6b2b847ef62cdced0054e6320b480c9f
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / drivers / 8255_pci.c
1 /*
2  * COMEDI driver for generic PCI based 8255 digital i/o boards
3  * Copyright (C) 2012 H Hartley Sweeten <hsweeten@visionengravers.com>
4  *
5  * Based on the tested adl_pci7296 driver written by:
6  *      Jon Grierson <jd@renko.co.uk>
7  * and the experimental cb_pcidio driver written by:
8  *      Yoshiya Matsuzaka
9  *
10  * COMEDI - Linux Control and Measurement Device Interface
11  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 /*
29 Driver: 8255_pci
30 Description: Generic PCI based 8255 Digital I/O boards
31 Devices: (ADLink) PCI-7224 [adl_pci-7224] - 24 channels
32          (ADLink) PCI-7248 [adl_pci-7248] - 48 channels
33          (ADLink) PCI-7296 [adl_pci-7296] - 96 channels
34          (Measurement Computing) PCI-DIO24 [cb_pci-dio24] - 24 channels
35          (Measurement Computing) PCI-DIO24H [cb_pci-dio24h] - 24 channels
36          (Measurement Computing) PCI-DIO48H [cb_pci-dio48h] - 48 channels
37          (Measurement Computing) PCI-DIO96H [cb_pci-dio96h] - 96 channels
38          (National Instruments) PCI-DIO-96 [ni_pci-dio-96] - 96 channels
39          (National Instruments) PCI-DIO-96B [ni_pci-dio-96b] - 96 channels
40          (National Instruments) PXI-6508 [ni_pxi-6508] - 96 channels
41          (National Instruments) PCI-6503 [ni_pci-6503] - 24 channels
42          (National Instruments) PCI-6503B [ni_pci-6503b] - 24 channels
43          (National Instruments) PCI-6503X [ni_pci-6503x] - 24 channels
44          (National Instruments) PXI-6503 [ni_pxi-6503] - 24 channels
45 Author: H Hartley Sweeten <hsweeten@visionengravers.com>
46 Updated: Wed, 12 Sep 2012 11:52:01 -0700
47 Status: untested
48
49 Some of these boards also have an 8254 programmable timer/counter
50 chip. This chip is not currently supported by this driver.
51
52 Interrupt support for these boards is also not currently supported.
53
54 Configuration Options: not applicable, uses PCI auto config
55 */
56
57 #include "../comedidev.h"
58
59 #include "8255.h"
60
61 /*
62  * PCI Device ID's supported by this driver
63  */
64 #define PCI_DEVICE_ID_ADLINK_PCI7224    0x7224
65 #define PCI_DEVICE_ID_ADLINK_PCI7248    0x7248
66 #define PCI_DEVICE_ID_ADLINK_PCI7296    0x7296
67
68 #define PCI_DEVICE_ID_CB_PCIDIO48H      0x000b
69 #define PCI_DEVICE_ID_CB_PCIDIO24H      0x0014
70 #define PCI_DEVICE_ID_CB_PCIDIO96H      0x0017
71 #define PCI_DEVICE_ID_CB_PCIDIO24       0x0028
72
73 #define PCI_DEVICE_ID_NI_PCIDIO96       0x0160
74 #define PCI_DEVICE_ID_NI_PCI6503        0x0400
75 #define PCI_DEVICE_ID_NI_PCI6503B       0x1250
76 #define PCI_DEVICE_ID_NI_PXI6508        0x13c0
77 #define PCI_DEVICE_ID_NI_PCIDIO96B      0x1630
78 #define PCI_DEVICE_ID_NI_PCI6503X       0x17d0
79 #define PCI_DEVICE_ID_NI_PXI_6503       0x1800
80
81 struct pci_8255_boardinfo {
82         const char *name;
83         unsigned short vendor;
84         unsigned short device;
85         int dio_badr;
86         int is_mmio;
87         int n_8255;
88 };
89
90 static const struct pci_8255_boardinfo pci_8255_boards[] = {
91         {
92                 .name           = "adl_pci-7224",
93                 .vendor         = PCI_VENDOR_ID_ADLINK,
94                 .device         = PCI_DEVICE_ID_ADLINK_PCI7224,
95                 .dio_badr       = 2,
96                 .n_8255         = 1,
97         }, {
98                 .name           = "adl_pci-7248",
99                 .vendor         = PCI_VENDOR_ID_ADLINK,
100                 .device         = PCI_DEVICE_ID_ADLINK_PCI7248,
101                 .dio_badr       = 2,
102                 .n_8255         = 2,
103         }, {
104                 .name           = "adl_pci-7296",
105                 .vendor         = PCI_VENDOR_ID_ADLINK,
106                 .device         = PCI_DEVICE_ID_ADLINK_PCI7296,
107                 .dio_badr       = 2,
108                 .n_8255         = 4,
109         }, {
110                 .name           = "cb_pci-dio24",
111                 .vendor         = PCI_VENDOR_ID_CB,
112                 .device         = PCI_DEVICE_ID_CB_PCIDIO24,
113                 .dio_badr       = 2,
114                 .n_8255         = 1,
115         }, {
116                 .name           = "cb_pci-dio24h",
117                 .vendor         = PCI_VENDOR_ID_CB,
118                 .device         = PCI_DEVICE_ID_CB_PCIDIO24H,
119                 .dio_badr       = 2,
120                 .n_8255         = 1,
121         }, {
122                 .name           = "cb_pci-dio48h",
123                 .vendor         = PCI_VENDOR_ID_CB,
124                 .device         = PCI_DEVICE_ID_CB_PCIDIO48H,
125                 .dio_badr       = 1,
126                 .n_8255         = 2,
127         }, {
128                 .name           = "cb_pci-dio96h",
129                 .vendor         = PCI_VENDOR_ID_CB,
130                 .device         = PCI_DEVICE_ID_CB_PCIDIO96H,
131                 .dio_badr       = 2,
132                 .n_8255         = 4,
133         }, {
134                 .name           = "ni_pci-dio-96",
135                 .vendor         = PCI_VENDOR_ID_NI,
136                 .device         = PCI_DEVICE_ID_NI_PCIDIO96,
137                 .dio_badr       = 1,
138                 .is_mmio        = 1,
139                 .n_8255         = 4,
140         }, {
141                 .name           = "ni_pci-dio-96b",
142                 .vendor         = PCI_VENDOR_ID_NI,
143                 .device         = PCI_DEVICE_ID_NI_PCIDIO96B,
144                 .dio_badr       = 1,
145                 .is_mmio        = 1,
146                 .n_8255         = 4,
147         }, {
148                 .name           = "ni_pxi-6508",
149                 .vendor         = PCI_VENDOR_ID_NI,
150                 .device         = PCI_DEVICE_ID_NI_PXI6508,
151                 .dio_badr       = 1,
152                 .is_mmio        = 1,
153                 .n_8255         = 4,
154         }, {
155                 .name           = "ni_pci-6503",
156                 .vendor         = PCI_VENDOR_ID_NI,
157                 .device         = PCI_DEVICE_ID_NI_PCI6503,
158                 .dio_badr       = 1,
159                 .is_mmio        = 1,
160                 .n_8255         = 1,
161         }, {
162                 .name           = "ni_pci-6503b",
163                 .vendor         = PCI_VENDOR_ID_NI,
164                 .device         = PCI_DEVICE_ID_NI_PCI6503B,
165                 .dio_badr       = 1,
166                 .is_mmio        = 1,
167                 .n_8255         = 1,
168         }, {
169                 .name           = "ni_pci-6503x",
170                 .vendor         = PCI_VENDOR_ID_NI,
171                 .device         = PCI_DEVICE_ID_NI_PCI6503X,
172                 .dio_badr       = 1,
173                 .is_mmio        = 1,
174                 .n_8255         = 1,
175         }, {
176                 .name           = "ni_pxi-6503",
177                 .vendor         = PCI_VENDOR_ID_NI,
178                 .device         = PCI_DEVICE_ID_NI_PXI_6503,
179                 .dio_badr       = 1,
180                 .is_mmio        = 1,
181                 .n_8255         = 1,
182         },
183 };
184
185 struct pci_8255_private {
186         void __iomem *mmio_base;
187 };
188
189 static int pci_8255_mmio(int dir, int port, int data, unsigned long iobase)
190 {
191         void __iomem *mmio_base = (void __iomem *)iobase;
192
193         if (dir) {
194                 writeb(data, mmio_base + port);
195                 return 0;
196         } else {
197                 return readb(mmio_base  + port);
198         }
199 }
200
201 static const void *pci_8255_find_boardinfo(struct comedi_device *dev,
202                                               struct pci_dev *pcidev)
203 {
204         const struct pci_8255_boardinfo *board;
205         int i;
206
207         for (i = 0; i < ARRAY_SIZE(pci_8255_boards); i++) {
208                 board = &pci_8255_boards[i];
209                 if (pcidev->vendor == board->vendor &&
210                     pcidev->device == board->device)
211                         return board;
212         }
213         return NULL;
214 }
215
216 static int __devinit pci_8255_auto_attach(struct comedi_device *dev,
217                                           unsigned long context_unused)
218 {
219         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
220         const struct pci_8255_boardinfo *board;
221         struct pci_8255_private *devpriv;
222         struct comedi_subdevice *s;
223         resource_size_t iobase;
224         unsigned long len;
225         int ret;
226         int i;
227
228         board = pci_8255_find_boardinfo(dev, pcidev);
229         if (!board)
230                 return -ENODEV;
231         dev->board_ptr = board;
232         dev->board_name = board->name;
233
234         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
235         if (!devpriv)
236                 return -ENOMEM;
237         dev->private = devpriv;
238
239         ret = comedi_pci_enable(pcidev, dev->board_name);
240         if (ret)
241                 return ret;
242         iobase = pci_resource_start(pcidev, board->dio_badr);
243         len = pci_resource_len(pcidev, board->dio_badr);
244
245         if (board->is_mmio) {
246                 devpriv->mmio_base = ioremap(iobase, len);
247                 if (!devpriv->mmio_base)
248                         return -ENOMEM;
249         }
250         dev->iobase = iobase;
251
252         /*
253          * One, two, or four subdevices are setup by this driver depending
254          * on the number of channels provided by the board. Each subdevice
255          * has 24 channels supported by the 8255 module.
256          */
257         ret = comedi_alloc_subdevices(dev, board->n_8255);
258         if (ret)
259                 return ret;
260
261         for (i = 0; i < board->n_8255; i++) {
262                 s = &dev->subdevices[i];
263                 if (board->is_mmio) {
264                         iobase = (unsigned long)(devpriv->mmio_base + (i * 4));
265                         ret = subdev_8255_init(dev, s, pci_8255_mmio, iobase);
266                 } else {
267                         iobase = dev->iobase + (i * 4);
268                         ret = subdev_8255_init(dev, s, NULL, iobase);
269                 }
270                 if (ret)
271                         return ret;
272         }
273
274         dev_info(dev->class_dev, "%s attached (%d digital i/o channels)\n",
275                 dev->board_name, board->n_8255 * 24);
276
277         return 0;
278 }
279
280 static void pci_8255_detach(struct comedi_device *dev)
281 {
282         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
283         const struct pci_8255_boardinfo *board = comedi_board(dev);
284         struct pci_8255_private *devpriv = dev->private;
285         struct comedi_subdevice *s;
286         int i;
287
288         if (!board || !devpriv)
289                 return;
290         if (dev->subdevices) {
291                 for (i = 0; i < board->n_8255; i++) {
292                         s = &dev->subdevices[i];
293                         subdev_8255_cleanup(dev, s);
294                 }
295         }
296         if (pcidev) {
297                 if (devpriv->mmio_base)
298                         iounmap(devpriv->mmio_base);
299                 if (dev->iobase)
300                         comedi_pci_disable(pcidev);
301         }
302 }
303
304 static struct comedi_driver pci_8255_driver = {
305         .driver_name    = "8255_pci",
306         .module         = THIS_MODULE,
307         .auto_attach    = pci_8255_auto_attach,
308         .detach         = pci_8255_detach,
309 };
310
311 static int __devinit pci_8255_pci_probe(struct pci_dev *dev,
312                                         const struct pci_device_id *ent)
313 {
314         return comedi_pci_auto_config(dev, &pci_8255_driver);
315 }
316
317 static void __devexit pci_8255_pci_remove(struct pci_dev *dev)
318 {
319         comedi_pci_auto_unconfig(dev);
320 }
321
322 static DEFINE_PCI_DEVICE_TABLE(pci_8255_pci_table) = {
323         { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_ADLINK_PCI7224) },
324         { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_ADLINK_PCI7248) },
325         { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_ADLINK_PCI7296) },
326         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_CB_PCIDIO24) },
327         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_CB_PCIDIO24H) },
328         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_CB_PCIDIO48H) },
329         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_CB_PCIDIO96H) },
330         { PCI_DEVICE(PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCIDIO96) },
331         { PCI_DEVICE(PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCIDIO96B) },
332         { PCI_DEVICE(PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI6508) },
333         { PCI_DEVICE(PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI6503) },
334         { PCI_DEVICE(PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI6503B) },
335         { PCI_DEVICE(PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI6503X) },
336         { PCI_DEVICE(PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI_6503) },
337         { 0 }
338 };
339 MODULE_DEVICE_TABLE(pci, pci_8255_pci_table);
340
341 static struct pci_driver pci_8255_pci_driver = {
342         .name           = "8255_pci",
343         .id_table       = pci_8255_pci_table,
344         .probe          = pci_8255_pci_probe,
345         .remove         = pci_8255_pci_remove,
346 };
347 module_comedi_pci_driver(pci_8255_driver, pci_8255_pci_driver);
348
349 MODULE_DESCRIPTION("COMEDI - Generic PCI based 8255 Digital I/O boards");
350 MODULE_AUTHOR("Comedi http://www.comedi.org");
351 MODULE_LICENSE("GPL");