staging: comedi: adv_pci1723: remove private data 'da_range'
[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 short ao_data[8];      /* data output buffer */
90 };
91
92 /*
93  * The pci1723 card reset;
94  */
95 static int pci1723_reset(struct comedi_device *dev)
96 {
97         struct pci1723_private *devpriv = dev->private;
98         int i;
99
100         outw(PCI1723_SYNC_CTRL_SYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
101
102         for (i = 0; i < 8; i++) {
103                 /* set all outputs to 0V */
104                 devpriv->ao_data[i] = 0x8000;
105                 outw(devpriv->ao_data[i], dev->iobase + PCI1723_AO_REG(i));
106                 /* set all ranges to +/- 10V */
107                 outw(PCI1723_CTRL_RANGE(0) | PCI1723_CTRL_CHAN(i),
108                      PCI1723_CTRL_REG);
109         }
110
111         outw(0, dev->iobase + PCI1723_RANGE_STROBE_REG);
112         outw(0, dev->iobase + PCI1723_SYNC_STROBE_REG);
113
114         outw(PCI1723_SYNC_CTRL_ASYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
115
116         return 0;
117 }
118
119 static int pci1723_insn_read_ao(struct comedi_device *dev,
120                                 struct comedi_subdevice *s,
121                                 struct comedi_insn *insn, unsigned int *data)
122 {
123         struct pci1723_private *devpriv = dev->private;
124         int n, chan;
125
126         chan = CR_CHAN(insn->chanspec);
127         for (n = 0; n < insn->n; n++)
128                 data[n] = devpriv->ao_data[chan];
129
130         return n;
131 }
132
133 /*
134   analog data output;
135 */
136 static int pci1723_ao_write_winsn(struct comedi_device *dev,
137                                   struct comedi_subdevice *s,
138                                   struct comedi_insn *insn, unsigned int *data)
139 {
140         struct pci1723_private *devpriv = dev->private;
141         unsigned int chan = CR_CHAN(insn->chanspec);
142         int n;
143
144         for (n = 0; n < insn->n; n++) {
145                 devpriv->ao_data[chan] = data[n];
146                 outw(data[n], dev->iobase + PCI1723_AO_REG(chan));
147         }
148
149         return n;
150 }
151
152 /*
153   digital i/o config/query
154 */
155 static int pci1723_dio_insn_config(struct comedi_device *dev,
156                                    struct comedi_subdevice *s,
157                                    struct comedi_insn *insn, unsigned int *data)
158 {
159         unsigned int chan = CR_CHAN(insn->chanspec);
160         unsigned int mask;
161         unsigned short mode;
162         int ret;
163
164         if (chan < 8)
165                 mask = 0x00ff;
166         else
167                 mask = 0xff00;
168
169         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
170         if (ret)
171                 return ret;
172
173         /* update hardware DIO mode */
174         mode = 0x0000;                  /* assume output */
175         if (!(s->io_bits & 0x00ff))
176                 mode |= 0x0001;         /* low byte input */
177         if (!(s->io_bits & 0xff00))
178                 mode |= 0x0002;         /* high byte input */
179         outw(mode, dev->iobase + PCI1723_DIO_CTRL_REG);
180
181         return insn->n;
182 }
183
184 static int pci1723_dio_insn_bits(struct comedi_device *dev,
185                                  struct comedi_subdevice *s,
186                                  struct comedi_insn *insn,
187                                  unsigned int *data)
188 {
189         if (comedi_dio_update_state(s, data))
190                 outw(s->state, dev->iobase + PCI1723_DIO_DATA_REG);
191
192         data[1] = inw(dev->iobase + PCI1723_DIO_DATA_REG);
193
194         return insn->n;
195 }
196
197 static int pci1723_auto_attach(struct comedi_device *dev,
198                                          unsigned long context_unused)
199 {
200         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
201         struct pci1723_private *devpriv;
202         struct comedi_subdevice *s;
203         int ret;
204
205         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
206         if (!devpriv)
207                 return -ENOMEM;
208
209         ret = comedi_pci_enable(dev);
210         if (ret)
211                 return ret;
212         dev->iobase = pci_resource_start(pcidev, 2);
213
214         ret = comedi_alloc_subdevices(dev, 2);
215         if (ret)
216                 return ret;
217
218         s = &dev->subdevices[0];
219         dev->write_subdev = s;
220         s->type         = COMEDI_SUBD_AO;
221         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
222         s->n_chan       = 8;
223         s->maxdata      = 0xffff;
224         s->len_chanlist = 8;
225         s->range_table  = &range_bipolar10;
226         s->insn_write   = pci1723_ao_write_winsn;
227         s->insn_read    = pci1723_insn_read_ao;
228
229         s = &dev->subdevices[1];
230         s->type         = COMEDI_SUBD_DIO;
231         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
232         s->n_chan       = 16;
233         s->maxdata      = 1;
234         s->len_chanlist = 16;
235         s->range_table  = &range_digital;
236         s->insn_config  = pci1723_dio_insn_config;
237         s->insn_bits    = pci1723_dio_insn_bits;
238
239         /* read DIO config */
240         switch (inw(dev->iobase + PCI1723_DIO_CTRL_REG) & 0x03) {
241         case 0x00:      /* low byte output, high byte output */
242                 s->io_bits = 0xFFFF;
243                 break;
244         case 0x01:      /* low byte input, high byte output */
245                 s->io_bits = 0xFF00;
246                 break;
247         case 0x02:      /* low byte output, high byte input */
248                 s->io_bits = 0x00FF;
249                 break;
250         case 0x03:      /* low byte input, high byte input */
251                 s->io_bits = 0x0000;
252                 break;
253         }
254         /* read DIO port state */
255         s->state = inw(dev->iobase + PCI1723_DIO_DATA_REG);
256
257         pci1723_reset(dev);
258
259         return 0;
260 }
261
262 static void pci1723_detach(struct comedi_device *dev)
263 {
264         if (dev->iobase)
265                 pci1723_reset(dev);
266         comedi_pci_detach(dev);
267 }
268
269 static struct comedi_driver adv_pci1723_driver = {
270         .driver_name    = "adv_pci1723",
271         .module         = THIS_MODULE,
272         .auto_attach    = pci1723_auto_attach,
273         .detach         = pci1723_detach,
274 };
275
276 static int adv_pci1723_pci_probe(struct pci_dev *dev,
277                                  const struct pci_device_id *id)
278 {
279         return comedi_pci_auto_config(dev, &adv_pci1723_driver,
280                                       id->driver_data);
281 }
282
283 static const struct pci_device_id adv_pci1723_pci_table[] = {
284         { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1723) },
285         { 0 }
286 };
287 MODULE_DEVICE_TABLE(pci, adv_pci1723_pci_table);
288
289 static struct pci_driver adv_pci1723_pci_driver = {
290         .name           = "adv_pci1723",
291         .id_table       = adv_pci1723_pci_table,
292         .probe          = adv_pci1723_pci_probe,
293         .remove         = comedi_pci_auto_unconfig,
294 };
295 module_comedi_pci_driver(adv_pci1723_driver, adv_pci1723_pci_driver);
296
297 MODULE_AUTHOR("Comedi http://www.comedi.org");
298 MODULE_DESCRIPTION("Comedi low-level driver");
299 MODULE_LICENSE("GPL");