Merge tag 'edac_fixes_for_3.10' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / 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  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, write to the Free Software Foundation, Inc.,
26  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  *
28  * You should also find the complete GPL in the COPYING file accompanying
29  * this source code.
30  */
31
32 #include <linux/pci.h>
33
34 #include "../comedidev.h"
35 #include "addi_watchdog.h"
36 #include "comedi_fc.h"
37
38 /*
39  * PCI bar 1 I/O Register map - Digital input/output
40  */
41 #define APCI1516_DI_REG                 0x00
42 #define APCI1516_DO_REG                 0x04
43
44 /*
45  * PCI bar 2 I/O Register map - Watchdog (APCI-1516 and APCI-2016)
46  */
47 #define APCI1516_WDOG_REG               0x00
48
49 enum apci1516_boardid {
50         BOARD_APCI1016,
51         BOARD_APCI1516,
52         BOARD_APCI2016,
53 };
54
55 struct apci1516_boardinfo {
56         const char *name;
57         int di_nchan;
58         int do_nchan;
59         int has_wdog;
60 };
61
62 static const struct apci1516_boardinfo apci1516_boardtypes[] = {
63         [BOARD_APCI1016] = {
64                 .name           = "apci1016",
65                 .di_nchan       = 16,
66         },
67         [BOARD_APCI1516] = {
68                 .name           = "apci1516",
69                 .di_nchan       = 8,
70                 .do_nchan       = 8,
71                 .has_wdog       = 1,
72         },
73         [BOARD_APCI2016] = {
74                 .name           = "apci2016",
75                 .do_nchan       = 16,
76                 .has_wdog       = 1,
77         },
78 };
79
80 struct apci1516_private {
81         unsigned long wdog_iobase;
82 };
83
84 static int apci1516_di_insn_bits(struct comedi_device *dev,
85                                  struct comedi_subdevice *s,
86                                  struct comedi_insn *insn,
87                                  unsigned int *data)
88 {
89         data[1] = inw(dev->iobase + APCI1516_DI_REG);
90
91         return insn->n;
92 }
93
94 static int apci1516_do_insn_bits(struct comedi_device *dev,
95                                  struct comedi_subdevice *s,
96                                  struct comedi_insn *insn,
97                                  unsigned int *data)
98 {
99         unsigned int mask = data[0];
100         unsigned int bits = data[1];
101
102         s->state = inw(dev->iobase + APCI1516_DO_REG);
103         if (mask) {
104                 s->state &= ~mask;
105                 s->state |= (bits & mask);
106
107                 outw(s->state, dev->iobase + APCI1516_DO_REG);
108         }
109
110         data[1] = s->state;
111
112         return insn->n;
113 }
114
115 static int apci1516_reset(struct comedi_device *dev)
116 {
117         const struct apci1516_boardinfo *this_board = comedi_board(dev);
118         struct apci1516_private *devpriv = dev->private;
119
120         if (!this_board->has_wdog)
121                 return 0;
122
123         outw(0x0, dev->iobase + APCI1516_DO_REG);
124
125         addi_watchdog_reset(devpriv->wdog_iobase);
126
127         return 0;
128 }
129
130 static int apci1516_auto_attach(struct comedi_device *dev,
131                                 unsigned long context)
132 {
133         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
134         const struct apci1516_boardinfo *this_board = NULL;
135         struct apci1516_private *devpriv;
136         struct comedi_subdevice *s;
137         int ret;
138
139         if (context < ARRAY_SIZE(apci1516_boardtypes))
140                 this_board = &apci1516_boardtypes[context];
141         if (!this_board)
142                 return -ENODEV;
143         dev->board_ptr = this_board;
144         dev->board_name = this_board->name;
145
146         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
147         if (!devpriv)
148                 return -ENOMEM;
149         dev->private = devpriv;
150
151         ret = comedi_pci_enable(dev);
152         if (ret)
153                 return ret;
154
155         dev->iobase = pci_resource_start(pcidev, 1);
156         devpriv->wdog_iobase = pci_resource_start(pcidev, 2);
157
158         ret = comedi_alloc_subdevices(dev, 3);
159         if (ret)
160                 return ret;
161
162         /* Initialize the digital input subdevice */
163         s = &dev->subdevices[0];
164         if (this_board->di_nchan) {
165                 s->type         = COMEDI_SUBD_DI;
166                 s->subdev_flags = SDF_READABLE;
167                 s->n_chan       = this_board->di_nchan;
168                 s->maxdata      = 1;
169                 s->range_table  = &range_digital;
170                 s->insn_bits    = apci1516_di_insn_bits;
171         } else {
172                 s->type         = COMEDI_SUBD_UNUSED;
173         }
174
175         /* Initialize the digital output subdevice */
176         s = &dev->subdevices[1];
177         if (this_board->do_nchan) {
178                 s->type         = COMEDI_SUBD_DO;
179                 s->subdev_flags = SDF_WRITEABLE;
180                 s->n_chan       = this_board->do_nchan;
181                 s->maxdata      = 1;
182                 s->range_table  = &range_digital;
183                 s->insn_bits    = apci1516_do_insn_bits;
184         } else {
185                 s->type         = COMEDI_SUBD_UNUSED;
186         }
187
188         /* Initialize the watchdog subdevice */
189         s = &dev->subdevices[2];
190         if (this_board->has_wdog) {
191                 ret = addi_watchdog_init(s, devpriv->wdog_iobase);
192                 if (ret)
193                         return ret;
194         } else {
195                 s->type         = COMEDI_SUBD_UNUSED;
196         }
197
198         apci1516_reset(dev);
199         return 0;
200 }
201
202 static void apci1516_detach(struct comedi_device *dev)
203 {
204         if (dev->iobase)
205                 apci1516_reset(dev);
206         comedi_spriv_free(dev, 2);
207         comedi_pci_disable(dev);
208 }
209
210 static struct comedi_driver apci1516_driver = {
211         .driver_name    = "addi_apci_1516",
212         .module         = THIS_MODULE,
213         .auto_attach    = apci1516_auto_attach,
214         .detach         = apci1516_detach,
215 };
216
217 static int apci1516_pci_probe(struct pci_dev *dev,
218                               const struct pci_device_id *id)
219 {
220         return comedi_pci_auto_config(dev, &apci1516_driver, id->driver_data);
221 }
222
223 static DEFINE_PCI_DEVICE_TABLE(apci1516_pci_table) = {
224         { PCI_VDEVICE(ADDIDATA, 0x1000), BOARD_APCI1016 },
225         { PCI_VDEVICE(ADDIDATA, 0x1001), BOARD_APCI1516 },
226         { PCI_VDEVICE(ADDIDATA, 0x1002), BOARD_APCI2016 },
227         { 0 }
228 };
229 MODULE_DEVICE_TABLE(pci, apci1516_pci_table);
230
231 static struct pci_driver apci1516_pci_driver = {
232         .name           = "addi_apci_1516",
233         .id_table       = apci1516_pci_table,
234         .probe          = apci1516_pci_probe,
235         .remove         = comedi_pci_auto_unconfig,
236 };
237 module_comedi_pci_driver(apci1516_driver, apci1516_pci_driver);
238
239 MODULE_DESCRIPTION("ADDI-DATA APCI-1016/1516/2016, 16 channel DIO boards");
240 MODULE_AUTHOR("Comedi http://www.comedi.org");
241 MODULE_LICENSE("GPL");