]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/dt2815.c
Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[karo-tx-linux.git] / drivers / staging / comedi / drivers / dt2815.c
1 /*
2    comedi/drivers/dt2815.c
3    Hardware driver for Data Translation DT2815
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 1999 Anders Blomdell <anders.blomdell@control.lth.se>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22  */
23 /*
24 Driver: dt2815
25 Description: Data Translation DT2815
26 Author: ds
27 Status: mostly complete, untested
28 Devices: [Data Translation] DT2815 (dt2815)
29
30 I'm not sure anyone has ever tested this board.  If you have information
31 contrary, please update.
32
33 Configuration options:
34   [0] - I/O port base base address
35   [1] - IRQ (unused)
36   [2] - Voltage unipolar/bipolar configuration
37         0 == unipolar 5V  (0V -- +5V)
38         1 == bipolar 5V  (-5V -- +5V)
39   [3] - Current offset configuration
40         0 == disabled  (0mA -- +32mAV)
41         1 == enabled  (+4mA -- +20mAV)
42   [4] - Firmware program configuration
43         0 == program 1 (see manual table 5-4)
44         1 == program 2 (see manual table 5-4)
45         2 == program 3 (see manual table 5-4)
46         3 == program 4 (see manual table 5-4)
47   [5] - Analog output 0 range configuration
48         0 == voltage
49         1 == current
50   [6] - Analog output 1 range configuration (same options)
51   [7] - Analog output 2 range configuration (same options)
52   [8] - Analog output 3 range configuration (same options)
53   [9] - Analog output 4 range configuration (same options)
54   [10] - Analog output 5 range configuration (same options)
55   [11] - Analog output 6 range configuration (same options)
56   [12] - Analog output 7 range configuration (same options)
57 */
58
59 #include "../comedidev.h"
60
61 #include <linux/ioport.h>
62 #include <linux/delay.h>
63
64 static const struct comedi_lrange
65         range_dt2815_ao_32_current = {1, {RANGE_mA(0, 32)} };
66
67 static const struct comedi_lrange
68         range_dt2815_ao_20_current = {1, {RANGE_mA(4, 20)} };
69
70 #define DT2815_SIZE 2
71
72 #define DT2815_DATA 0
73 #define DT2815_STATUS 1
74
75 struct dt2815_private {
76
77         const struct comedi_lrange *range_type_list[8];
78         unsigned int ao_readback[8];
79 };
80
81 static int dt2815_wait_for_status(struct comedi_device *dev, int status)
82 {
83         int i;
84
85         for (i = 0; i < 100; i++) {
86                 if (inb(dev->iobase + DT2815_STATUS) == status)
87                         break;
88         }
89         return status;
90 }
91
92 static int dt2815_ao_insn_read(struct comedi_device *dev,
93                                struct comedi_subdevice *s,
94                                struct comedi_insn *insn, unsigned int *data)
95 {
96         struct dt2815_private *devpriv = dev->private;
97         int i;
98         int chan = CR_CHAN(insn->chanspec);
99
100         for (i = 0; i < insn->n; i++)
101                 data[i] = devpriv->ao_readback[chan];
102
103         return i;
104 }
105
106 static int dt2815_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
107                           struct comedi_insn *insn, unsigned int *data)
108 {
109         struct dt2815_private *devpriv = dev->private;
110         int i;
111         int chan = CR_CHAN(insn->chanspec);
112         unsigned int status;
113         unsigned int lo, hi;
114
115         for (i = 0; i < insn->n; i++) {
116                 lo = ((data[i] & 0x0f) << 4) | (chan << 1) | 0x01;
117                 hi = (data[i] & 0xff0) >> 4;
118
119                 status = dt2815_wait_for_status(dev, 0x00);
120                 if (status != 0) {
121                         printk(KERN_WARNING "dt2815: failed to write low byte "
122                                "on %d reason %x\n", chan, status);
123                         return -EBUSY;
124                 }
125
126                 outb(lo, dev->iobase + DT2815_DATA);
127
128                 status = dt2815_wait_for_status(dev, 0x10);
129                 if (status != 0x10) {
130                         printk(KERN_WARNING "dt2815: failed to write high byte "
131                                "on %d reason %x\n", chan, status);
132                         return -EBUSY;
133                 }
134                 devpriv->ao_readback[chan] = data[i];
135         }
136         return i;
137 }
138
139 /*
140   options[0]   Board base address
141   options[1]   IRQ (not applicable)
142   options[2]   Voltage unipolar/bipolar configuration
143                 0 == unipolar 5V  (0V -- +5V)
144                 1 == bipolar 5V  (-5V -- +5V)
145   options[3]   Current offset configuration
146                 0 == disabled  (0mA -- +32mAV)
147                 1 == enabled  (+4mA -- +20mAV)
148   options[4]   Firmware program configuration
149                 0 == program 1 (see manual table 5-4)
150                 1 == program 2 (see manual table 5-4)
151                 2 == program 3 (see manual table 5-4)
152                 3 == program 4 (see manual table 5-4)
153   options[5]   Analog output 0 range configuration
154                 0 == voltage
155                 1 == current
156   options[6]   Analog output 1 range configuration
157   ...
158   options[12]   Analog output 7 range configuration
159                 0 == voltage
160                 1 == current
161  */
162
163 static int dt2815_attach(struct comedi_device *dev, struct comedi_devconfig *it)
164 {
165         struct dt2815_private *devpriv;
166         struct comedi_subdevice *s;
167         int i;
168         const struct comedi_lrange *current_range_type, *voltage_range_type;
169         unsigned long iobase;
170         int ret;
171
172         iobase = it->options[0];
173         printk(KERN_INFO "comedi%d: dt2815: 0x%04lx ", dev->minor, iobase);
174         if (!request_region(iobase, DT2815_SIZE, "dt2815")) {
175                 printk(KERN_WARNING "I/O port conflict\n");
176                 return -EIO;
177         }
178
179         dev->iobase = iobase;
180         dev->board_name = "dt2815";
181
182         ret = comedi_alloc_subdevices(dev, 1);
183         if (ret)
184                 return ret;
185
186         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
187         if (!devpriv)
188                 return -ENOMEM;
189         dev->private = devpriv;
190
191         s = &dev->subdevices[0];
192         /* ao subdevice */
193         s->type = COMEDI_SUBD_AO;
194         s->subdev_flags = SDF_WRITABLE;
195         s->maxdata = 0xfff;
196         s->n_chan = 8;
197         s->insn_write = dt2815_ao_insn;
198         s->insn_read = dt2815_ao_insn_read;
199         s->range_table_list = devpriv->range_type_list;
200
201         current_range_type = (it->options[3])
202             ? &range_dt2815_ao_20_current : &range_dt2815_ao_32_current;
203         voltage_range_type = (it->options[2])
204             ? &range_bipolar5 : &range_unipolar5;
205         for (i = 0; i < 8; i++) {
206                 devpriv->range_type_list[i] = (it->options[5 + i])
207                     ? current_range_type : voltage_range_type;
208         }
209
210         /* Init the 2815 */
211         outb(0x00, dev->iobase + DT2815_STATUS);
212         for (i = 0; i < 100; i++) {
213                 /* This is incredibly slow (approx 20 ms) */
214                 unsigned int status;
215
216                 udelay(1000);
217                 status = inb(dev->iobase + DT2815_STATUS);
218                 if (status == 4) {
219                         unsigned int program;
220                         program = (it->options[4] & 0x3) << 3 | 0x7;
221                         outb(program, dev->iobase + DT2815_DATA);
222                         printk(KERN_INFO ", program: 0x%x (@t=%d)\n",
223                                program, i);
224                         break;
225                 } else if (status != 0x00) {
226                         printk(KERN_WARNING "dt2815: unexpected status 0x%x "
227                                "(@t=%d)\n", status, i);
228                         if (status & 0x60)
229                                 outb(0x00, dev->iobase + DT2815_STATUS);
230                 }
231         }
232
233         return 0;
234 }
235
236 static void dt2815_detach(struct comedi_device *dev)
237 {
238         if (dev->iobase)
239                 release_region(dev->iobase, DT2815_SIZE);
240 }
241
242 static struct comedi_driver dt2815_driver = {
243         .driver_name    = "dt2815",
244         .module         = THIS_MODULE,
245         .attach         = dt2815_attach,
246         .detach         = dt2815_detach,
247 };
248 module_comedi_driver(dt2815_driver);
249
250 MODULE_AUTHOR("Comedi http://www.comedi.org");
251 MODULE_DESCRIPTION("Comedi low-level driver");
252 MODULE_LICENSE("GPL");