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