Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / comedi / drivers / amplc_pci236.c
1 /*
2  * comedi/drivers/amplc_pci236.c
3  * Driver for Amplicon PCI236 DIO boards.
4  *
5  * Copyright (C) 2002-2014 MEV Ltd. <http://www.mev.co.uk/>
6  *
7  * COMEDI - Linux Control and Measurement Device Interface
8  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 /*
21  * Driver: amplc_pci236
22  * Description: Amplicon PCI236
23  * Author: Ian Abbott <abbotti@mev.co.uk>
24  * Devices: [Amplicon] PCI236 (amplc_pci236)
25  * Updated: Fri, 25 Jul 2014 15:32:40 +0000
26  * Status: works
27  *
28  * Configuration options:
29  *   none
30  *
31  * Manual configuration of PCI board (PCI236) is not supported; it is
32  * configured automatically.
33  *
34  * The PCI236 board has a single 8255 appearing as subdevice 0.
35  *
36  * Subdevice 1 pretends to be a digital input device, but it always
37  * returns 0 when read. However, if you run a command with
38  * scan_begin_src=TRIG_EXT, a rising edge on port C bit 3 acts as an
39  * external trigger, which can be used to wake up tasks.  This is like
40  * the comedi_parport device.  If no interrupt is connected, then
41  * subdevice 1 is unused.
42  */
43
44 #include <linux/module.h>
45 #include <linux/interrupt.h>
46
47 #include "../comedi_pci.h"
48
49 #include "amplc_pc236.h"
50 #include "plx9052.h"
51
52 /* Disable, and clear, interrupts */
53 #define PCI236_INTR_DISABLE     (PLX9052_INTCSR_LI1POL |        \
54                                  PLX9052_INTCSR_LI2POL |        \
55                                  PLX9052_INTCSR_LI1SEL |        \
56                                  PLX9052_INTCSR_LI1CLRINT)
57
58 /* Enable, and clear, interrupts */
59 #define PCI236_INTR_ENABLE      (PLX9052_INTCSR_LI1ENAB |       \
60                                  PLX9052_INTCSR_LI1POL |        \
61                                  PLX9052_INTCSR_LI2POL |        \
62                                  PLX9052_INTCSR_PCIENAB |       \
63                                  PLX9052_INTCSR_LI1SEL |        \
64                                  PLX9052_INTCSR_LI1CLRINT)
65
66 static void pci236_intr_update_cb(struct comedi_device *dev, bool enable)
67 {
68         struct pc236_private *devpriv = dev->private;
69
70         /* this will also clear the "local interrupt 1" latch */
71         outl(enable ? PCI236_INTR_ENABLE : PCI236_INTR_DISABLE,
72              devpriv->lcr_iobase + PLX9052_INTCSR);
73 }
74
75 static bool pci236_intr_chk_clr_cb(struct comedi_device *dev)
76 {
77         struct pc236_private *devpriv = dev->private;
78
79         /* check if interrupt occurred */
80         if (!(inl(devpriv->lcr_iobase + PLX9052_INTCSR) &
81               PLX9052_INTCSR_LI1STAT))
82                 return false;
83         /* clear the interrupt */
84         pci236_intr_update_cb(dev, devpriv->enable_irq);
85         return true;
86 }
87
88 static const struct pc236_board pc236_pci_board = {
89         .name = "pci236",
90         .intr_update_cb = pci236_intr_update_cb,
91         .intr_chk_clr_cb = pci236_intr_chk_clr_cb,
92 };
93
94 static int pci236_auto_attach(struct comedi_device *dev,
95                               unsigned long context_unused)
96 {
97         struct pci_dev *pci_dev = comedi_to_pci_dev(dev);
98         struct pc236_private *devpriv;
99         unsigned long iobase;
100         int ret;
101
102         dev_info(dev->class_dev, "amplc_pci236: attach pci %s\n",
103                  pci_name(pci_dev));
104
105         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
106         if (!devpriv)
107                 return -ENOMEM;
108
109         dev->board_ptr = &pc236_pci_board;
110         dev->board_name = pc236_pci_board.name;
111         ret = comedi_pci_enable(dev);
112         if (ret)
113                 return ret;
114
115         devpriv->lcr_iobase = pci_resource_start(pci_dev, 1);
116         iobase = pci_resource_start(pci_dev, 2);
117         return amplc_pc236_common_attach(dev, iobase, pci_dev->irq,
118                                          IRQF_SHARED);
119 }
120
121 static struct comedi_driver amplc_pci236_driver = {
122         .driver_name = "amplc_pci236",
123         .module = THIS_MODULE,
124         .auto_attach = pci236_auto_attach,
125         .detach = comedi_pci_detach,
126 };
127
128 static const struct pci_device_id pci236_pci_table[] = {
129         { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, 0x0009) },
130         { 0 }
131 };
132
133 MODULE_DEVICE_TABLE(pci, pci236_pci_table);
134
135 static int amplc_pci236_pci_probe(struct pci_dev *dev,
136                                   const struct pci_device_id *id)
137 {
138         return comedi_pci_auto_config(dev, &amplc_pci236_driver,
139                                       id->driver_data);
140 }
141
142 static struct pci_driver amplc_pci236_pci_driver = {
143         .name           = "amplc_pci236",
144         .id_table       = pci236_pci_table,
145         .probe          = &amplc_pci236_pci_probe,
146         .remove         = comedi_pci_auto_unconfig,
147 };
148
149 module_comedi_pci_driver(amplc_pci236_driver, amplc_pci236_pci_driver);
150
151 MODULE_AUTHOR("Comedi http://www.comedi.org");
152 MODULE_DESCRIPTION("Comedi driver for Amplicon PCI236 DIO boards");
153 MODULE_LICENSE("GPL");