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