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