Merge commit 'ed30f24e8d07d30aa3e69d1f508f4d7bd2e8ea14' of git://git.linaro.org/landi...
[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 <linux/pci.h>
58
59 #include "../comedidev.h"
60
61 #include "8255.h"
62
63 enum pci_8255_boardid {
64         BOARD_ADLINK_PCI7224,
65         BOARD_ADLINK_PCI7248,
66         BOARD_ADLINK_PCI7296,
67         BOARD_CB_PCIDIO24,
68         BOARD_CB_PCIDIO24H,
69         BOARD_CB_PCIDIO48H,
70         BOARD_CB_PCIDIO96H,
71         BOARD_NI_PCIDIO96,
72         BOARD_NI_PCIDIO96B,
73         BOARD_NI_PXI6508,
74         BOARD_NI_PCI6503,
75         BOARD_NI_PCI6503B,
76         BOARD_NI_PCI6503X,
77         BOARD_NI_PXI_6503,
78 };
79
80 struct pci_8255_boardinfo {
81         const char *name;
82         int dio_badr;
83         int n_8255;
84 };
85
86 static const struct pci_8255_boardinfo pci_8255_boards[] = {
87         [BOARD_ADLINK_PCI7224] = {
88                 .name           = "adl_pci-7224",
89                 .dio_badr       = 2,
90                 .n_8255         = 1,
91         },
92         [BOARD_ADLINK_PCI7248] = {
93                 .name           = "adl_pci-7248",
94                 .dio_badr       = 2,
95                 .n_8255         = 2,
96         },
97         [BOARD_ADLINK_PCI7296] = {
98                 .name           = "adl_pci-7296",
99                 .dio_badr       = 2,
100                 .n_8255         = 4,
101         },
102         [BOARD_CB_PCIDIO24] = {
103                 .name           = "cb_pci-dio24",
104                 .dio_badr       = 2,
105                 .n_8255         = 1,
106         },
107         [BOARD_CB_PCIDIO24H] = {
108                 .name           = "cb_pci-dio24h",
109                 .dio_badr       = 2,
110                 .n_8255         = 1,
111         },
112         [BOARD_CB_PCIDIO48H] = {
113                 .name           = "cb_pci-dio48h",
114                 .dio_badr       = 1,
115                 .n_8255         = 2,
116         },
117         [BOARD_CB_PCIDIO96H] = {
118                 .name           = "cb_pci-dio96h",
119                 .dio_badr       = 2,
120                 .n_8255         = 4,
121         },
122         [BOARD_NI_PCIDIO96] = {
123                 .name           = "ni_pci-dio-96",
124                 .dio_badr       = 1,
125                 .n_8255         = 4,
126         },
127         [BOARD_NI_PCIDIO96B] = {
128                 .name           = "ni_pci-dio-96b",
129                 .dio_badr       = 1,
130                 .n_8255         = 4,
131         },
132         [BOARD_NI_PXI6508] = {
133                 .name           = "ni_pxi-6508",
134                 .dio_badr       = 1,
135                 .n_8255         = 4,
136         },
137         [BOARD_NI_PCI6503] = {
138                 .name           = "ni_pci-6503",
139                 .dio_badr       = 1,
140                 .n_8255         = 1,
141         },
142         [BOARD_NI_PCI6503B] = {
143                 .name           = "ni_pci-6503b",
144                 .dio_badr       = 1,
145                 .n_8255         = 1,
146         },
147         [BOARD_NI_PCI6503X] = {
148                 .name           = "ni_pci-6503x",
149                 .dio_badr       = 1,
150                 .n_8255         = 1,
151         },
152         [BOARD_NI_PXI_6503] = {
153                 .name           = "ni_pxi-6503",
154                 .dio_badr       = 1,
155                 .n_8255         = 1,
156         },
157 };
158
159 struct pci_8255_private {
160         void __iomem *mmio_base;
161 };
162
163 static int pci_8255_mmio(int dir, int port, int data, unsigned long iobase)
164 {
165         void __iomem *mmio_base = (void __iomem *)iobase;
166
167         if (dir) {
168                 writeb(data, mmio_base + port);
169                 return 0;
170         } else {
171                 return readb(mmio_base  + port);
172         }
173 }
174
175 static int pci_8255_auto_attach(struct comedi_device *dev,
176                                 unsigned long context)
177 {
178         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
179         const struct pci_8255_boardinfo *board = NULL;
180         struct pci_8255_private *devpriv;
181         struct comedi_subdevice *s;
182         bool is_mmio;
183         int ret;
184         int i;
185
186         if (context < ARRAY_SIZE(pci_8255_boards))
187                 board = &pci_8255_boards[context];
188         if (!board)
189                 return -ENODEV;
190         dev->board_ptr = board;
191         dev->board_name = board->name;
192
193         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
194         if (!devpriv)
195                 return -ENOMEM;
196         dev->private = devpriv;
197
198         ret = comedi_pci_enable(dev);
199         if (ret)
200                 return ret;
201
202         is_mmio = (pci_resource_flags(pcidev, board->dio_badr) &
203                    IORESOURCE_MEM) != 0;
204         if (is_mmio) {
205                 devpriv->mmio_base = pci_ioremap_bar(pcidev, board->dio_badr);
206                 if (!devpriv->mmio_base)
207                         return -ENOMEM;
208         } else {
209                 dev->iobase = pci_resource_start(pcidev, board->dio_badr);
210         }
211
212         /*
213          * One, two, or four subdevices are setup by this driver depending
214          * on the number of channels provided by the board. Each subdevice
215          * has 24 channels supported by the 8255 module.
216          */
217         ret = comedi_alloc_subdevices(dev, board->n_8255);
218         if (ret)
219                 return ret;
220
221         for (i = 0; i < board->n_8255; i++) {
222                 unsigned long iobase;
223
224                 s = &dev->subdevices[i];
225                 if (is_mmio) {
226                         iobase = (unsigned long)(devpriv->mmio_base + (i * 4));
227                         ret = subdev_8255_init(dev, s, pci_8255_mmio, iobase);
228                 } else {
229                         iobase = dev->iobase + (i * 4);
230                         ret = subdev_8255_init(dev, s, NULL, iobase);
231                 }
232                 if (ret)
233                         return ret;
234         }
235
236         dev_info(dev->class_dev, "%s attached (%d digital i/o channels)\n",
237                 dev->board_name, board->n_8255 * 24);
238
239         return 0;
240 }
241
242 static void pci_8255_detach(struct comedi_device *dev)
243 {
244         struct pci_8255_private *devpriv = dev->private;
245         int i;
246
247         for (i = 0; i < dev->n_subdevices; i++)
248                 comedi_spriv_free(dev, i);
249         if (devpriv && devpriv->mmio_base)
250                 iounmap(devpriv->mmio_base);
251         comedi_pci_disable(dev);
252 }
253
254 static struct comedi_driver pci_8255_driver = {
255         .driver_name    = "8255_pci",
256         .module         = THIS_MODULE,
257         .auto_attach    = pci_8255_auto_attach,
258         .detach         = pci_8255_detach,
259 };
260
261 static int pci_8255_pci_probe(struct pci_dev *dev,
262                               const struct pci_device_id *id)
263 {
264         return comedi_pci_auto_config(dev, &pci_8255_driver, id->driver_data);
265 }
266
267 static DEFINE_PCI_DEVICE_TABLE(pci_8255_pci_table) = {
268         { PCI_VDEVICE(ADLINK, 0x7224), BOARD_ADLINK_PCI7224 },
269         { PCI_VDEVICE(ADLINK, 0x7248), BOARD_ADLINK_PCI7248 },
270         { PCI_VDEVICE(ADLINK, 0x7296), BOARD_ADLINK_PCI7296 },
271         { PCI_VDEVICE(CB, 0x0028), BOARD_CB_PCIDIO24 },
272         { PCI_VDEVICE(CB, 0x0014), BOARD_CB_PCIDIO24H },
273         { PCI_VDEVICE(CB, 0x000b), BOARD_CB_PCIDIO48H },
274         { PCI_VDEVICE(CB, 0x0017), BOARD_CB_PCIDIO96H },
275         { PCI_VDEVICE(NI, 0x0160), BOARD_NI_PCIDIO96 },
276         { PCI_VDEVICE(NI, 0x1630), BOARD_NI_PCIDIO96B },
277         { PCI_VDEVICE(NI, 0x13c0), BOARD_NI_PXI6508 },
278         { PCI_VDEVICE(NI, 0x0400), BOARD_NI_PCI6503 },
279         { PCI_VDEVICE(NI, 0x1250), BOARD_NI_PCI6503B },
280         { PCI_VDEVICE(NI, 0x17d0), BOARD_NI_PCI6503X },
281         { PCI_VDEVICE(NI, 0x1800), BOARD_NI_PXI_6503 },
282         { 0 }
283 };
284 MODULE_DEVICE_TABLE(pci, pci_8255_pci_table);
285
286 static struct pci_driver pci_8255_pci_driver = {
287         .name           = "8255_pci",
288         .id_table       = pci_8255_pci_table,
289         .probe          = pci_8255_pci_probe,
290         .remove         = comedi_pci_auto_unconfig,
291 };
292 module_comedi_pci_driver(pci_8255_driver, pci_8255_pci_driver);
293
294 MODULE_DESCRIPTION("COMEDI - Generic PCI based 8255 Digital I/O boards");
295 MODULE_AUTHOR("Comedi http://www.comedi.org");
296 MODULE_LICENSE("GPL");