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