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