OSDN Git Service

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