51349893fb95a05ffcb303211ec18bd9ce629e91
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / drivers / adv_pci1723.c
1 /*
2    comedi/drivers/pci1723.c
3
4    COMEDI - Linux Control and Measurement Device Interface
5    Copyright (C) 2000 David A. Schleef <ds@schleef.org>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 */
17 /*
18 Driver: adv_pci1723
19 Description: Advantech PCI-1723
20 Author: yonggang <rsmgnu@gmail.com>, Ian Abbott <abbotti@mev.co.uk>
21 Devices: [Advantech] PCI-1723 (adv_pci1723)
22 Updated: Mon, 14 Apr 2008 15:12:56 +0100
23 Status: works
24
25 Configuration Options:
26   [0] - PCI bus of device (optional)
27   [1] - PCI slot of device (optional)
28
29   If bus/slot is not specified, the first supported
30   PCI device found will be used.
31
32 Subdevice 0 is 8-channel AO, 16-bit, range +/- 10 V.
33
34 Subdevice 1 is 16-channel DIO.  The channels are configurable as input or
35 output in 2 groups (0 to 7, 8 to 15).  Configuring any channel implicitly
36 configures all channels in the same group.
37
38 TODO:
39
40 1. Add the two milliamp ranges to the AO subdevice (0 to 20 mA, 4 to 20 mA).
41 2. Read the initial ranges and values of the AO subdevice at start-up instead
42    of reinitializing them.
43 3. Implement calibration.
44 */
45
46 #include <linux/module.h>
47 #include <linux/pci.h>
48
49 #include "../comedidev.h"
50
51 /*
52  * PCI Bar 2 I/O Register map (dev->iobase)
53  */
54 #define PCI1723_AO_REG(x)               (0x00 + ((x) * 2))
55 #define PCI1723_BOARD_ID_REG            0x10
56 #define PCI1723_BOARD_ID_MASK           (0xf << 0)
57 #define PCI1723_SYNC_CTRL_REG           0x12
58 #define PCI1723_SYNC_CTRL_ASYNC         (0 << 0)
59 #define PCI1723_SYNC_CTRL_SYNC          (1 << 0)
60 #define PCI1723_CTRL_REG                0x14
61 #define PCI1723_CTRL_BUSY               (1 << 15)
62 #define PCI1723_CTRL_INIT               (1 << 14)
63 #define PCI1723_CTRL_SELF               (1 << 8)
64 #define PCI1723_CTRL_IDX(x)             (((x) & 0x3) << 6)
65 #define PCI1723_CTRL_RANGE(x)           (((x) & 0x3) << 4)
66 #define PCI1723_CTRL_GAIN               (0 << 3)
67 #define PCI1723_CTRL_OFFSET             (1 << 3)
68 #define PCI1723_CTRL_CHAN(x)            (((x) & 0x7) << 0)
69 #define PCI1723_CALIB_CTRL_REG          0x16
70 #define PCI1723_CALIB_CTRL_CS           (1 << 2)
71 #define PCI1723_CALIB_CTRL_DAT          (1 << 1)
72 #define PCI1723_CALIB_CTRL_CLK          (1 << 0)
73 #define PCI1723_CALIB_STROBE_REG        0x18
74 #define PCI1723_DIO_CTRL_REG            0x1a
75 #define PCI1723_DIO_CTRL_HDIO           (1 << 1)
76 #define PCI1723_DIO_CTRL_LDIO           (1 << 0)
77 #define PCI1723_DIO_DATA_REG            0x1c
78 #define PCI1723_CALIB_DATA_REG          0x1e
79 #define PCI1723_SYNC_STROBE_REG         0x20
80 #define PCI1723_RESET_AO_STROBE_REG     0x22
81 #define PCI1723_RESET_CALIB_STROBE_REG  0x24
82 #define PCI1723_RANGE_STROBE_REG        0x26
83 #define PCI1723_VREF_REG                0x28
84 #define PCI1723_VREF_NEG10V             (0 << 0)
85 #define PCI1723_VREF_0V                 (1 << 0)
86 #define PCI1723_VREF_POS10V             (3 << 0)
87
88 struct pci1723_private {
89         unsigned char da_range[8];      /* D/A output range for each channel */
90         unsigned short ao_data[8];      /* data output buffer */
91 };
92
93 /*
94  * The pci1723 card reset;
95  */
96 static int pci1723_reset(struct comedi_device *dev)
97 {
98         struct pci1723_private *devpriv = dev->private;
99         int i;
100
101         outw(PCI1723_SYNC_CTRL_SYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
102
103         for (i = 0; i < 8; i++) {
104                 /* set all outputs to 0V */
105                 devpriv->ao_data[i] = 0x8000;
106                 outw(devpriv->ao_data[i], dev->iobase + PCI1723_AO_REG(i));
107                 /* set all ranges to +/- 10V */
108                 devpriv->da_range[i] = 0;
109                 outw(((devpriv->da_range[i] << 4) | i),
110                      PCI1723_CTRL_REG);
111         }
112
113         outw(0, dev->iobase + PCI1723_RANGE_STROBE_REG);
114         outw(0, dev->iobase + PCI1723_SYNC_STROBE_REG);
115
116         outw(PCI1723_SYNC_CTRL_ASYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
117
118         return 0;
119 }
120
121 static int pci1723_insn_read_ao(struct comedi_device *dev,
122                                 struct comedi_subdevice *s,
123                                 struct comedi_insn *insn, unsigned int *data)
124 {
125         struct pci1723_private *devpriv = dev->private;
126         int n, chan;
127
128         chan = CR_CHAN(insn->chanspec);
129         for (n = 0; n < insn->n; n++)
130                 data[n] = devpriv->ao_data[chan];
131
132         return n;
133 }
134
135 /*
136   analog data output;
137 */
138 static int pci1723_ao_write_winsn(struct comedi_device *dev,
139                                   struct comedi_subdevice *s,
140                                   struct comedi_insn *insn, unsigned int *data)
141 {
142         struct pci1723_private *devpriv = dev->private;
143         unsigned int chan = CR_CHAN(insn->chanspec);
144         int n;
145
146         for (n = 0; n < insn->n; n++) {
147                 devpriv->ao_data[chan] = data[n];
148                 outw(data[n], dev->iobase + PCI1723_AO_REG(chan));
149         }
150
151         return n;
152 }
153
154 /*
155   digital i/o config/query
156 */
157 static int pci1723_dio_insn_config(struct comedi_device *dev,
158                                    struct comedi_subdevice *s,
159                                    struct comedi_insn *insn, unsigned int *data)
160 {
161         unsigned int chan = CR_CHAN(insn->chanspec);
162         unsigned int mask;
163         unsigned short mode;
164         int ret;
165
166         if (chan < 8)
167                 mask = 0x00ff;
168         else
169                 mask = 0xff00;
170
171         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
172         if (ret)
173                 return ret;
174
175         /* update hardware DIO mode */
176         mode = 0x0000;                  /* assume output */
177         if (!(s->io_bits & 0x00ff))
178                 mode |= 0x0001;         /* low byte input */
179         if (!(s->io_bits & 0xff00))
180                 mode |= 0x0002;         /* high byte input */
181         outw(mode, dev->iobase + PCI1723_DIO_CTRL_REG);
182
183         return insn->n;
184 }
185
186 static int pci1723_dio_insn_bits(struct comedi_device *dev,
187                                  struct comedi_subdevice *s,
188                                  struct comedi_insn *insn,
189                                  unsigned int *data)
190 {
191         if (comedi_dio_update_state(s, data))
192                 outw(s->state, dev->iobase + PCI1723_DIO_DATA_REG);
193
194         data[1] = inw(dev->iobase + PCI1723_DIO_DATA_REG);
195
196         return insn->n;
197 }
198
199 static int pci1723_auto_attach(struct comedi_device *dev,
200                                          unsigned long context_unused)
201 {
202         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
203         struct pci1723_private *devpriv;
204         struct comedi_subdevice *s;
205         int ret;
206
207         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
208         if (!devpriv)
209                 return -ENOMEM;
210
211         ret = comedi_pci_enable(dev);
212         if (ret)
213                 return ret;
214         dev->iobase = pci_resource_start(pcidev, 2);
215
216         ret = comedi_alloc_subdevices(dev, 2);
217         if (ret)
218                 return ret;
219
220         s = &dev->subdevices[0];
221         dev->write_subdev = s;
222         s->type         = COMEDI_SUBD_AO;
223         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
224         s->n_chan       = 8;
225         s->maxdata      = 0xffff;
226         s->len_chanlist = 8;
227         s->range_table  = &range_bipolar10;
228         s->insn_write   = pci1723_ao_write_winsn;
229         s->insn_read    = pci1723_insn_read_ao;
230
231         s = &dev->subdevices[1];
232         s->type         = COMEDI_SUBD_DIO;
233         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
234         s->n_chan       = 16;
235         s->maxdata      = 1;
236         s->len_chanlist = 16;
237         s->range_table  = &range_digital;
238         s->insn_config  = pci1723_dio_insn_config;
239         s->insn_bits    = pci1723_dio_insn_bits;
240
241         /* read DIO config */
242         switch (inw(dev->iobase + PCI1723_DIO_CTRL_REG) & 0x03) {
243         case 0x00:      /* low byte output, high byte output */
244                 s->io_bits = 0xFFFF;
245                 break;
246         case 0x01:      /* low byte input, high byte output */
247                 s->io_bits = 0xFF00;
248                 break;
249         case 0x02:      /* low byte output, high byte input */
250                 s->io_bits = 0x00FF;
251                 break;
252         case 0x03:      /* low byte input, high byte input */
253                 s->io_bits = 0x0000;
254                 break;
255         }
256         /* read DIO port state */
257         s->state = inw(dev->iobase + PCI1723_DIO_DATA_REG);
258
259         pci1723_reset(dev);
260
261         return 0;
262 }
263
264 static void pci1723_detach(struct comedi_device *dev)
265 {
266         if (dev->iobase)
267                 pci1723_reset(dev);
268         comedi_pci_detach(dev);
269 }
270
271 static struct comedi_driver adv_pci1723_driver = {
272         .driver_name    = "adv_pci1723",
273         .module         = THIS_MODULE,
274         .auto_attach    = pci1723_auto_attach,
275         .detach         = pci1723_detach,
276 };
277
278 static int adv_pci1723_pci_probe(struct pci_dev *dev,
279                                  const struct pci_device_id *id)
280 {
281         return comedi_pci_auto_config(dev, &adv_pci1723_driver,
282                                       id->driver_data);
283 }
284
285 static const struct pci_device_id adv_pci1723_pci_table[] = {
286         { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1723) },
287         { 0 }
288 };
289 MODULE_DEVICE_TABLE(pci, adv_pci1723_pci_table);
290
291 static struct pci_driver adv_pci1723_pci_driver = {
292         .name           = "adv_pci1723",
293         .id_table       = adv_pci1723_pci_table,
294         .probe          = adv_pci1723_pci_probe,
295         .remove         = comedi_pci_auto_unconfig,
296 };
297 module_comedi_pci_driver(adv_pci1723_driver, adv_pci1723_pci_driver);
298
299 MODULE_AUTHOR("Comedi http://www.comedi.org");
300 MODULE_DESCRIPTION("Comedi low-level driver");
301 MODULE_LICENSE("GPL");