Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / comedi / drivers / addi_apci_2032.c
1 /*
2  * addi_apci_2032.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/interrupt.h>
27 #include <linux/slab.h>
28
29 #include "../comedi_pci.h"
30 #include "addi_watchdog.h"
31
32 /*
33  * PCI bar 1 I/O Register map
34  */
35 #define APCI2032_DO_REG                 0x00
36 #define APCI2032_INT_CTRL_REG           0x04
37 #define APCI2032_INT_CTRL_VCC_ENA       (1 << 0)
38 #define APCI2032_INT_CTRL_CC_ENA        (1 << 1)
39 #define APCI2032_INT_STATUS_REG         0x08
40 #define APCI2032_INT_STATUS_VCC         (1 << 0)
41 #define APCI2032_INT_STATUS_CC          (1 << 1)
42 #define APCI2032_STATUS_REG             0x0c
43 #define APCI2032_STATUS_IRQ             (1 << 0)
44 #define APCI2032_WDOG_REG               0x10
45
46 struct apci2032_int_private {
47         spinlock_t spinlock;
48         bool active;
49         unsigned char enabled_isns;
50 };
51
52 static int apci2032_do_insn_bits(struct comedi_device *dev,
53                                  struct comedi_subdevice *s,
54                                  struct comedi_insn *insn,
55                                  unsigned int *data)
56 {
57         s->state = inl(dev->iobase + APCI2032_DO_REG);
58
59         if (comedi_dio_update_state(s, data))
60                 outl(s->state, dev->iobase + APCI2032_DO_REG);
61
62         data[1] = s->state;
63
64         return insn->n;
65 }
66
67 static int apci2032_int_insn_bits(struct comedi_device *dev,
68                                   struct comedi_subdevice *s,
69                                   struct comedi_insn *insn,
70                                   unsigned int *data)
71 {
72         data[1] = inl(dev->iobase + APCI2032_INT_STATUS_REG) & 3;
73         return insn->n;
74 }
75
76 static void apci2032_int_stop(struct comedi_device *dev,
77                               struct comedi_subdevice *s)
78 {
79         struct apci2032_int_private *subpriv = s->private;
80
81         subpriv->active = false;
82         subpriv->enabled_isns = 0;
83         outl(0x0, dev->iobase + APCI2032_INT_CTRL_REG);
84 }
85
86 static int apci2032_int_cmdtest(struct comedi_device *dev,
87                                 struct comedi_subdevice *s,
88                                 struct comedi_cmd *cmd)
89 {
90         int err = 0;
91
92         /* Step 1 : check if triggers are trivially valid */
93
94         err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
95         err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
96         err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
97         err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
98         err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
99
100         if (err)
101                 return 1;
102
103         /* Step 2a : make sure trigger sources are unique */
104         err |= comedi_check_trigger_is_unique(cmd->stop_src);
105
106         /* Step 2b : and mutually compatible */
107
108         if (err)
109                 return 2;
110
111         /* Step 3: check if arguments are trivially valid */
112
113         err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
114         err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
115         err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
116         err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
117                                            cmd->chanlist_len);
118         if (cmd->stop_src == TRIG_COUNT)
119                 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
120         else    /* TRIG_NONE */
121                 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
122
123         if (err)
124                 return 3;
125
126         /* Step 4: fix up any arguments */
127
128         /* Step 5: check channel list if it exists */
129
130         return 0;
131 }
132
133 static int apci2032_int_cmd(struct comedi_device *dev,
134                             struct comedi_subdevice *s)
135 {
136         struct comedi_cmd *cmd = &s->async->cmd;
137         struct apci2032_int_private *subpriv = s->private;
138         unsigned char enabled_isns;
139         unsigned int n;
140         unsigned long flags;
141
142         enabled_isns = 0;
143         for (n = 0; n < cmd->chanlist_len; n++)
144                 enabled_isns |= 1 << CR_CHAN(cmd->chanlist[n]);
145
146         spin_lock_irqsave(&subpriv->spinlock, flags);
147
148         subpriv->enabled_isns = enabled_isns;
149         subpriv->active = true;
150         outl(enabled_isns, dev->iobase + APCI2032_INT_CTRL_REG);
151
152         spin_unlock_irqrestore(&subpriv->spinlock, flags);
153
154         return 0;
155 }
156
157 static int apci2032_int_cancel(struct comedi_device *dev,
158                                struct comedi_subdevice *s)
159 {
160         struct apci2032_int_private *subpriv = s->private;
161         unsigned long flags;
162
163         spin_lock_irqsave(&subpriv->spinlock, flags);
164         if (subpriv->active)
165                 apci2032_int_stop(dev, s);
166         spin_unlock_irqrestore(&subpriv->spinlock, flags);
167
168         return 0;
169 }
170
171 static irqreturn_t apci2032_interrupt(int irq, void *d)
172 {
173         struct comedi_device *dev = d;
174         struct comedi_subdevice *s = dev->read_subdev;
175         struct comedi_cmd *cmd = &s->async->cmd;
176         struct apci2032_int_private *subpriv;
177         unsigned int val;
178
179         if (!dev->attached)
180                 return IRQ_NONE;
181
182         /* Check if VCC OR CC interrupt has occurred */
183         val = inl(dev->iobase + APCI2032_STATUS_REG) & APCI2032_STATUS_IRQ;
184         if (!val)
185                 return IRQ_NONE;
186
187         subpriv = s->private;
188         spin_lock(&subpriv->spinlock);
189
190         val = inl(dev->iobase + APCI2032_INT_STATUS_REG) & 3;
191         /* Disable triggered interrupt sources. */
192         outl(~val & 3, dev->iobase + APCI2032_INT_CTRL_REG);
193         /*
194          * Note: We don't reenable the triggered interrupt sources because they
195          * are level-sensitive, hardware error status interrupt sources and
196          * they'd keep triggering interrupts repeatedly.
197          */
198
199         if (subpriv->active && (val & subpriv->enabled_isns) != 0) {
200                 unsigned short bits = 0;
201                 int i;
202
203                 /* Bits in scan data correspond to indices in channel list. */
204                 for (i = 0; i < cmd->chanlist_len; i++) {
205                         unsigned int chan = CR_CHAN(cmd->chanlist[i]);
206
207                         if (val & (1 << chan))
208                                 bits |= (1 << i);
209                 }
210
211                 comedi_buf_write_samples(s, &bits, 1);
212
213                 if (cmd->stop_src == TRIG_COUNT &&
214                     s->async->scans_done >= cmd->stop_arg)
215                         s->async->events |= COMEDI_CB_EOA;
216         }
217
218         spin_unlock(&subpriv->spinlock);
219
220         comedi_handle_events(dev, s);
221
222         return IRQ_HANDLED;
223 }
224
225 static int apci2032_reset(struct comedi_device *dev)
226 {
227         outl(0x0, dev->iobase + APCI2032_DO_REG);
228         outl(0x0, dev->iobase + APCI2032_INT_CTRL_REG);
229
230         addi_watchdog_reset(dev->iobase + APCI2032_WDOG_REG);
231
232         return 0;
233 }
234
235 static int apci2032_auto_attach(struct comedi_device *dev,
236                                 unsigned long context_unused)
237 {
238         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
239         struct comedi_subdevice *s;
240         int ret;
241
242         ret = comedi_pci_enable(dev);
243         if (ret)
244                 return ret;
245         dev->iobase = pci_resource_start(pcidev, 1);
246         apci2032_reset(dev);
247
248         if (pcidev->irq > 0) {
249                 ret = request_irq(pcidev->irq, apci2032_interrupt,
250                                   IRQF_SHARED, dev->board_name, dev);
251                 if (ret == 0)
252                         dev->irq = pcidev->irq;
253         }
254
255         ret = comedi_alloc_subdevices(dev, 3);
256         if (ret)
257                 return ret;
258
259         /* Initialize the digital output subdevice */
260         s = &dev->subdevices[0];
261         s->type         = COMEDI_SUBD_DO;
262         s->subdev_flags = SDF_WRITABLE;
263         s->n_chan       = 32;
264         s->maxdata      = 1;
265         s->range_table  = &range_digital;
266         s->insn_bits    = apci2032_do_insn_bits;
267
268         /* Initialize the watchdog subdevice */
269         s = &dev->subdevices[1];
270         ret = addi_watchdog_init(s, dev->iobase + APCI2032_WDOG_REG);
271         if (ret)
272                 return ret;
273
274         /* Initialize the interrupt subdevice */
275         s = &dev->subdevices[2];
276         s->type         = COMEDI_SUBD_DI;
277         s->subdev_flags = SDF_READABLE;
278         s->n_chan       = 2;
279         s->maxdata      = 1;
280         s->range_table  = &range_digital;
281         s->insn_bits    = apci2032_int_insn_bits;
282         if (dev->irq) {
283                 struct apci2032_int_private *subpriv;
284
285                 dev->read_subdev = s;
286                 subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
287                 if (!subpriv)
288                         return -ENOMEM;
289                 spin_lock_init(&subpriv->spinlock);
290                 s->private      = subpriv;
291                 s->subdev_flags = SDF_READABLE | SDF_CMD_READ | SDF_PACKED;
292                 s->len_chanlist = 2;
293                 s->do_cmdtest   = apci2032_int_cmdtest;
294                 s->do_cmd       = apci2032_int_cmd;
295                 s->cancel       = apci2032_int_cancel;
296         }
297
298         return 0;
299 }
300
301 static void apci2032_detach(struct comedi_device *dev)
302 {
303         if (dev->iobase)
304                 apci2032_reset(dev);
305         comedi_pci_detach(dev);
306         if (dev->read_subdev)
307                 kfree(dev->read_subdev->private);
308 }
309
310 static struct comedi_driver apci2032_driver = {
311         .driver_name    = "addi_apci_2032",
312         .module         = THIS_MODULE,
313         .auto_attach    = apci2032_auto_attach,
314         .detach         = apci2032_detach,
315 };
316
317 static int apci2032_pci_probe(struct pci_dev *dev,
318                               const struct pci_device_id *id)
319 {
320         return comedi_pci_auto_config(dev, &apci2032_driver, id->driver_data);
321 }
322
323 static const struct pci_device_id apci2032_pci_table[] = {
324         { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x1004) },
325         { 0 }
326 };
327 MODULE_DEVICE_TABLE(pci, apci2032_pci_table);
328
329 static struct pci_driver apci2032_pci_driver = {
330         .name           = "addi_apci_2032",
331         .id_table       = apci2032_pci_table,
332         .probe          = apci2032_pci_probe,
333         .remove         = comedi_pci_auto_unconfig,
334 };
335 module_comedi_pci_driver(apci2032_driver, apci2032_pci_driver);
336
337 MODULE_AUTHOR("Comedi http://www.comedi.org");
338 MODULE_DESCRIPTION("ADDI-DATA APCI-2032, 32 channel DO boards");
339 MODULE_LICENSE("GPL");