Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / comedi / drivers / addi_apci_1516.c
1 /*
2  * addi_apci_1516.c
3  * Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.
4  * Project manager: Eric Stolz
5  *
6  *      ADDI-DATA GmbH
7  *      Dieselstrasse 3
8  *      D-77833 Ottersweier
9  *      Tel: +19(0)7223/9493-0
10  *      Fax: +49(0)7223/9493-92
11  *      http://www.addi-data.com
12  *      info@addi-data.com
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU General Public License as published by the
16  * Free Software Foundation; either version 2 of the License, or (at your
17  * option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22  * more details.
23  */
24
25 #include <linux/module.h>
26
27 #include "../comedi_pci.h"
28 #include "addi_watchdog.h"
29
30 /*
31  * PCI bar 1 I/O Register map - Digital input/output
32  */
33 #define APCI1516_DI_REG                 0x00
34 #define APCI1516_DO_REG                 0x04
35
36 /*
37  * PCI bar 2 I/O Register map - Watchdog (APCI-1516 and APCI-2016)
38  */
39 #define APCI1516_WDOG_REG               0x00
40
41 enum apci1516_boardid {
42         BOARD_APCI1016,
43         BOARD_APCI1516,
44         BOARD_APCI2016,
45 };
46
47 struct apci1516_boardinfo {
48         const char *name;
49         int di_nchan;
50         int do_nchan;
51         int has_wdog;
52 };
53
54 static const struct apci1516_boardinfo apci1516_boardtypes[] = {
55         [BOARD_APCI1016] = {
56                 .name           = "apci1016",
57                 .di_nchan       = 16,
58         },
59         [BOARD_APCI1516] = {
60                 .name           = "apci1516",
61                 .di_nchan       = 8,
62                 .do_nchan       = 8,
63                 .has_wdog       = 1,
64         },
65         [BOARD_APCI2016] = {
66                 .name           = "apci2016",
67                 .do_nchan       = 16,
68                 .has_wdog       = 1,
69         },
70 };
71
72 struct apci1516_private {
73         unsigned long wdog_iobase;
74 };
75
76 static int apci1516_di_insn_bits(struct comedi_device *dev,
77                                  struct comedi_subdevice *s,
78                                  struct comedi_insn *insn,
79                                  unsigned int *data)
80 {
81         data[1] = inw(dev->iobase + APCI1516_DI_REG);
82
83         return insn->n;
84 }
85
86 static int apci1516_do_insn_bits(struct comedi_device *dev,
87                                  struct comedi_subdevice *s,
88                                  struct comedi_insn *insn,
89                                  unsigned int *data)
90 {
91         s->state = inw(dev->iobase + APCI1516_DO_REG);
92
93         if (comedi_dio_update_state(s, data))
94                 outw(s->state, dev->iobase + APCI1516_DO_REG);
95
96         data[1] = s->state;
97
98         return insn->n;
99 }
100
101 static int apci1516_reset(struct comedi_device *dev)
102 {
103         const struct apci1516_boardinfo *this_board = dev->board_ptr;
104         struct apci1516_private *devpriv = dev->private;
105
106         if (!this_board->has_wdog)
107                 return 0;
108
109         outw(0x0, dev->iobase + APCI1516_DO_REG);
110
111         addi_watchdog_reset(devpriv->wdog_iobase);
112
113         return 0;
114 }
115
116 static int apci1516_auto_attach(struct comedi_device *dev,
117                                 unsigned long context)
118 {
119         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
120         const struct apci1516_boardinfo *this_board = NULL;
121         struct apci1516_private *devpriv;
122         struct comedi_subdevice *s;
123         int ret;
124
125         if (context < ARRAY_SIZE(apci1516_boardtypes))
126                 this_board = &apci1516_boardtypes[context];
127         if (!this_board)
128                 return -ENODEV;
129         dev->board_ptr = this_board;
130         dev->board_name = this_board->name;
131
132         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
133         if (!devpriv)
134                 return -ENOMEM;
135
136         ret = comedi_pci_enable(dev);
137         if (ret)
138                 return ret;
139
140         dev->iobase = pci_resource_start(pcidev, 1);
141         devpriv->wdog_iobase = pci_resource_start(pcidev, 2);
142
143         ret = comedi_alloc_subdevices(dev, 3);
144         if (ret)
145                 return ret;
146
147         /* Initialize the digital input subdevice */
148         s = &dev->subdevices[0];
149         if (this_board->di_nchan) {
150                 s->type         = COMEDI_SUBD_DI;
151                 s->subdev_flags = SDF_READABLE;
152                 s->n_chan       = this_board->di_nchan;
153                 s->maxdata      = 1;
154                 s->range_table  = &range_digital;
155                 s->insn_bits    = apci1516_di_insn_bits;
156         } else {
157                 s->type         = COMEDI_SUBD_UNUSED;
158         }
159
160         /* Initialize the digital output subdevice */
161         s = &dev->subdevices[1];
162         if (this_board->do_nchan) {
163                 s->type         = COMEDI_SUBD_DO;
164                 s->subdev_flags = SDF_WRITABLE;
165                 s->n_chan       = this_board->do_nchan;
166                 s->maxdata      = 1;
167                 s->range_table  = &range_digital;
168                 s->insn_bits    = apci1516_do_insn_bits;
169         } else {
170                 s->type         = COMEDI_SUBD_UNUSED;
171         }
172
173         /* Initialize the watchdog subdevice */
174         s = &dev->subdevices[2];
175         if (this_board->has_wdog) {
176                 ret = addi_watchdog_init(s, devpriv->wdog_iobase);
177                 if (ret)
178                         return ret;
179         } else {
180                 s->type         = COMEDI_SUBD_UNUSED;
181         }
182
183         apci1516_reset(dev);
184         return 0;
185 }
186
187 static void apci1516_detach(struct comedi_device *dev)
188 {
189         if (dev->iobase)
190                 apci1516_reset(dev);
191         comedi_pci_detach(dev);
192 }
193
194 static struct comedi_driver apci1516_driver = {
195         .driver_name    = "addi_apci_1516",
196         .module         = THIS_MODULE,
197         .auto_attach    = apci1516_auto_attach,
198         .detach         = apci1516_detach,
199 };
200
201 static int apci1516_pci_probe(struct pci_dev *dev,
202                               const struct pci_device_id *id)
203 {
204         return comedi_pci_auto_config(dev, &apci1516_driver, id->driver_data);
205 }
206
207 static const struct pci_device_id apci1516_pci_table[] = {
208         { PCI_VDEVICE(ADDIDATA, 0x1000), BOARD_APCI1016 },
209         { PCI_VDEVICE(ADDIDATA, 0x1001), BOARD_APCI1516 },
210         { PCI_VDEVICE(ADDIDATA, 0x1002), BOARD_APCI2016 },
211         { 0 }
212 };
213 MODULE_DEVICE_TABLE(pci, apci1516_pci_table);
214
215 static struct pci_driver apci1516_pci_driver = {
216         .name           = "addi_apci_1516",
217         .id_table       = apci1516_pci_table,
218         .probe          = apci1516_pci_probe,
219         .remove         = comedi_pci_auto_unconfig,
220 };
221 module_comedi_pci_driver(apci1516_driver, apci1516_pci_driver);
222
223 MODULE_DESCRIPTION("ADDI-DATA APCI-1016/1516/2016, 16 channel DIO boards");
224 MODULE_AUTHOR("Comedi http://www.comedi.org");
225 MODULE_LICENSE("GPL");