Linux-libre 4.19.116-gnu
[librecmc/linux-libre.git] / drivers / staging / comedi / drivers / adv_pci1720.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * COMEDI driver for Advantech PCI-1720U
4  * Copyright (c) 2015 H Hartley Sweeten <hsweeten@visionengravers.com>
5  *
6  * Separated from the adv_pci1710 driver written by:
7  * Michal Dobes <dobes@tesnet.cz>
8  *
9  * COMEDI - Linux Control and Measurement Device Interface
10  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
11  */
12
13 /*
14  * Driver: adv_pci1720
15  * Description: 4-channel Isolated D/A Output board
16  * Devices: [Advantech] PCI-7120U (adv_pci1720)
17  * Author: H Hartley Sweeten <hsweeten@visionengravers.com>
18  * Updated: Fri, 29 Oct 2015 17:19:35 -0700
19  * Status: untested
20  *
21  * Configuration options: not applicable, uses PCI auto config
22  *
23  * The PCI-1720 has 4 isolated 12-bit analog output channels with multiple
24  * output ranges. It also has a BoardID switch to allow differentiating
25  * multiple boards in the system.
26  *
27  * The analog outputs can operate in two modes, immediate and synchronized.
28  * This driver currently does not support the synchronized output mode.
29  *
30  * Jumpers JP1 to JP4 are used to set the current sink ranges for each
31  * analog output channel. In order to use the current sink ranges, the
32  * unipolar 5V range must be used. The voltage output and sink output for
33  * each channel is available on the connector as separate pins.
34  *
35  * Jumper JP5 controls the "hot" reset state of the analog outputs.
36  * Depending on its setting, the analog outputs will either keep the
37  * last settings and output values or reset to the default state after
38  * a "hot" reset. The default state for all channels is uniploar 5V range
39  * and all the output values are 0V. To allow this feature to work, the
40  * analog outputs are not "reset" when the driver attaches.
41  */
42
43 #include <linux/module.h>
44 #include <linux/delay.h>
45
46 #include "../comedi_pci.h"
47
48 /*
49  * PCI BAR2 Register map (dev->iobase)
50  */
51 #define PCI1720_AO_LSB_REG(x)           (0x00 + ((x) * 2))
52 #define PCI1720_AO_MSB_REG(x)           (0x01 + ((x) * 2))
53 #define PCI1720_AO_RANGE_REG            0x08
54 #define PCI1720_AO_RANGE(c, r)          (((r) & 0x3) << ((c) * 2))
55 #define PCI1720_AO_RANGE_MASK(c)        PCI1720_AO_RANGE((c), 0x3)
56 #define PCI1720_SYNC_REG                0x09
57 #define PCI1720_SYNC_CTRL_REG           0x0f
58 #define PCI1720_SYNC_CTRL_SC0           BIT(0)
59 #define PCI1720_BOARDID_REG             0x14
60
61 static const struct comedi_lrange pci1720_ao_range = {
62         4, {
63                 UNI_RANGE(5),
64                 UNI_RANGE(10),
65                 BIP_RANGE(5),
66                 BIP_RANGE(10)
67         }
68 };
69
70 static int pci1720_ao_insn_write(struct comedi_device *dev,
71                                  struct comedi_subdevice *s,
72                                  struct comedi_insn *insn,
73                                  unsigned int *data)
74 {
75         unsigned int chan = CR_CHAN(insn->chanspec);
76         unsigned int range = CR_RANGE(insn->chanspec);
77         unsigned int val;
78         int i;
79
80         /* set the channel range and polarity */
81         val = inb(dev->iobase + PCI1720_AO_RANGE_REG);
82         val &= ~PCI1720_AO_RANGE_MASK(chan);
83         val |= PCI1720_AO_RANGE(chan, range);
84         outb(val, dev->iobase + PCI1720_AO_RANGE_REG);
85
86         val = s->readback[chan];
87         for (i = 0; i < insn->n; i++) {
88                 val = data[i];
89
90                 outb(val & 0xff, dev->iobase + PCI1720_AO_LSB_REG(chan));
91                 outb((val >> 8) & 0xff, dev->iobase + PCI1720_AO_MSB_REG(chan));
92
93                 /* conversion time is 2us (500 kHz throughput) */
94                 usleep_range(2, 100);
95         }
96
97         s->readback[chan] = val;
98
99         return insn->n;
100 }
101
102 static int pci1720_di_insn_bits(struct comedi_device *dev,
103                                 struct comedi_subdevice *s,
104                                 struct comedi_insn *insn,
105                                 unsigned int *data)
106 {
107         data[1] = inb(dev->iobase + PCI1720_BOARDID_REG);
108
109         return insn->n;
110 }
111
112 static int pci1720_auto_attach(struct comedi_device *dev,
113                                unsigned long context)
114 {
115         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
116         struct comedi_subdevice *s;
117         int ret;
118
119         ret = comedi_pci_enable(dev);
120         if (ret)
121                 return ret;
122         dev->iobase = pci_resource_start(pcidev, 2);
123
124         ret = comedi_alloc_subdevices(dev, 2);
125         if (ret)
126                 return ret;
127
128         /* Analog Output subdevice */
129         s = &dev->subdevices[0];
130         s->type         = COMEDI_SUBD_AO;
131         s->subdev_flags = SDF_WRITABLE;
132         s->n_chan       = 4;
133         s->maxdata      = 0x0fff;
134         s->range_table  = &pci1720_ao_range;
135         s->insn_write   = pci1720_ao_insn_write;
136
137         ret = comedi_alloc_subdev_readback(s);
138         if (ret)
139                 return ret;
140
141         /* Digital Input subdevice (BoardID SW1) */
142         s = &dev->subdevices[1];
143         s->type         = COMEDI_SUBD_DI;
144         s->subdev_flags = SDF_READABLE;
145         s->n_chan       = 4;
146         s->maxdata      = 1;
147         s->range_table  = &range_digital;
148         s->insn_bits    = pci1720_di_insn_bits;
149
150         /* disable synchronized output, channels update when written */
151         outb(0, dev->iobase + PCI1720_SYNC_CTRL_REG);
152
153         return 0;
154 }
155
156 static struct comedi_driver adv_pci1720_driver = {
157         .driver_name    = "adv_pci1720",
158         .module         = THIS_MODULE,
159         .auto_attach    = pci1720_auto_attach,
160         .detach         = comedi_pci_detach,
161 };
162
163 static int adv_pci1720_pci_probe(struct pci_dev *dev,
164                                  const struct pci_device_id *id)
165 {
166         return comedi_pci_auto_config(dev, &adv_pci1720_driver,
167                                       id->driver_data);
168 }
169
170 static const struct pci_device_id adv_pci1720_pci_table[] = {
171         { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1720) },
172         { 0 }
173 };
174 MODULE_DEVICE_TABLE(pci, adv_pci1720_pci_table);
175
176 static struct pci_driver adv_pci1720_pci_driver = {
177         .name           = "adv_pci1720",
178         .id_table       = adv_pci1720_pci_table,
179         .probe          = adv_pci1720_pci_probe,
180         .remove         = comedi_pci_auto_unconfig,
181 };
182 module_comedi_pci_driver(adv_pci1720_driver, adv_pci1720_pci_driver);
183
184 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
185 MODULE_DESCRIPTION("Comedi driver for Advantech PCI-1720 Analog Output board");
186 MODULE_LICENSE("GPL");