Staging: comedi: Remove comedi_device typedef
[pandora-kernel.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 comedi_lrange range_dt2815_ao_32_current = { 1, {
65                         RANGE_mA(0, 32)
66         }
67 };
68 static const comedi_lrange range_dt2815_ao_20_current = { 1, {
69                         RANGE_mA(4, 20)
70         }
71 };
72
73 #define DT2815_SIZE 2
74
75 #define DT2815_DATA 0
76 #define DT2815_STATUS 1
77
78 static int dt2815_attach(struct comedi_device * dev, comedi_devconfig * it);
79 static int dt2815_detach(struct comedi_device * dev);
80 static comedi_driver driver_dt2815 = {
81       driver_name:"dt2815",
82       module:THIS_MODULE,
83       attach:dt2815_attach,
84       detach:dt2815_detach,
85 };
86
87 COMEDI_INITCLEANUP(driver_dt2815);
88
89 static void dt2815_free_resources(struct comedi_device * dev);
90
91 typedef struct {
92         const comedi_lrange *range_type_list[8];
93         unsigned int ao_readback[8];
94 } dt2815_private;
95
96 #define devpriv ((dt2815_private *)dev->private)
97
98 static int dt2815_wait_for_status(struct comedi_device * dev, int status)
99 {
100         int i;
101
102         for (i = 0; i < 100; i++) {
103                 if (inb(dev->iobase + DT2815_STATUS) == status)
104                         break;
105         }
106         return status;
107 }
108
109 static int dt2815_ao_insn_read(struct comedi_device * dev, comedi_subdevice * s,
110         comedi_insn * insn, unsigned int * data)
111 {
112         int i;
113         int chan = CR_CHAN(insn->chanspec);
114
115         for (i = 0; i < insn->n; i++) {
116                 data[i] = devpriv->ao_readback[chan];
117         }
118
119         return i;
120 }
121
122 static int dt2815_ao_insn(struct comedi_device * dev, comedi_subdevice * s,
123         comedi_insn * insn, unsigned int * data)
124 {
125         int i;
126         int chan = CR_CHAN(insn->chanspec);
127         unsigned int status;
128         unsigned int lo, hi;
129
130         for (i = 0; i < insn->n; i++) {
131                 lo = ((data[i] & 0x0f) << 4) | (chan << 1) | 0x01;
132                 hi = (data[i] & 0xff0) >> 4;
133
134                 status = dt2815_wait_for_status(dev, 0x00);
135                 if (status != 0) {
136                         rt_printk
137                                 ("dt2815: failed to write low byte on %d reason %x\n",
138                                 chan, status);
139                         return -EBUSY;
140                 }
141
142                 outb(lo, dev->iobase + DT2815_DATA);
143
144                 status = dt2815_wait_for_status(dev, 0x10);
145                 if (status != 0x10) {
146                         rt_printk
147                                 ("dt2815: failed to write high byte on %d reason %x\n",
148                                 chan, status);
149                         return -EBUSY;
150                 }
151                 devpriv->ao_readback[chan] = data[i];
152         }
153         return i;
154 }
155
156 /*
157   options[0]   Board base address
158   options[1]   IRQ (not applicable)
159   options[2]   Voltage unipolar/bipolar configuration
160                  0 == unipolar 5V  (0V -- +5V)
161                  1 == bipolar 5V  (-5V -- +5V)
162   options[3]   Current offset configuration
163                  0 == disabled  (0mA -- +32mAV)
164                  1 == enabled  (+4mA -- +20mAV)
165   options[4]   Firmware program configuration
166                  0 == program 1 (see manual table 5-4)
167                  1 == program 2 (see manual table 5-4)
168                  2 == program 3 (see manual table 5-4)
169                  3 == program 4 (see manual table 5-4)
170   options[5]   Analog output 0 range configuration
171                  0 == voltage
172                  1 == current
173   options[6]   Analog output 1 range configuration
174   ...
175   options[12]   Analog output 7 range configuration
176                  0 == voltage
177                  1 == current
178  */
179
180 static int dt2815_attach(struct comedi_device * dev, comedi_devconfig * it)
181 {
182         comedi_subdevice *s;
183         int i;
184         const comedi_lrange *current_range_type, *voltage_range_type;
185         unsigned long iobase;
186
187         iobase = it->options[0];
188         printk("comedi%d: dt2815: 0x%04lx ", dev->minor, iobase);
189         if (!request_region(iobase, DT2815_SIZE, "dt2815")) {
190                 printk("I/O port conflict\n");
191                 return -EIO;
192         }
193
194         dev->iobase = iobase;
195         dev->board_name = "dt2815";
196
197         if (alloc_subdevices(dev, 1) < 0)
198                 return -ENOMEM;
199         if (alloc_private(dev, sizeof(dt2815_private)) < 0)
200                 return -ENOMEM;
201
202         s = dev->subdevices;
203         /* ao subdevice */
204         s->type = COMEDI_SUBD_AO;
205         s->subdev_flags = SDF_WRITABLE;
206         s->maxdata = 0xfff;
207         s->n_chan = 8;
208         s->insn_write = dt2815_ao_insn;
209         s->insn_read = dt2815_ao_insn_read;
210         s->range_table_list = devpriv->range_type_list;
211
212         current_range_type = (it->options[3])
213                 ? &range_dt2815_ao_20_current : &range_dt2815_ao_32_current;
214         voltage_range_type = (it->options[2])
215                 ? &range_bipolar5 : &range_unipolar5;
216         for (i = 0; i < 8; i++) {
217                 devpriv->range_type_list[i] = (it->options[5 + i])
218                         ? current_range_type : voltage_range_type;
219         }
220
221         /* Init the 2815 */
222         outb(0x00, dev->iobase + DT2815_STATUS);
223         for (i = 0; i < 100; i++) {
224                 /* This is incredibly slow (approx 20 ms) */
225                 unsigned int status;
226
227                 comedi_udelay(1000);
228                 status = inb(dev->iobase + DT2815_STATUS);
229                 if (status == 4) {
230                         unsigned int program;
231                         program = (it->options[4] & 0x3) << 3 | 0x7;
232                         outb(program, dev->iobase + DT2815_DATA);
233                         printk(", program: 0x%x (@t=%d)\n", program, i);
234                         break;
235                 } else if (status != 0x00) {
236                         printk("dt2815: unexpected status 0x%x (@t=%d)\n",
237                                 status, i);
238                         if (status & 0x60) {
239                                 outb(0x00, dev->iobase + DT2815_STATUS);
240                         }
241                 }
242         }
243
244         printk("\n");
245
246         return 0;
247 }
248
249 static void dt2815_free_resources(struct comedi_device * dev)
250 {
251         if (dev->iobase)
252                 release_region(dev->iobase, DT2815_SIZE);
253 }
254
255 static int dt2815_detach(struct comedi_device * dev)
256 {
257         printk("comedi%d: dt2815: remove\n", dev->minor);
258
259         dt2815_free_resources(dev);
260
261         return 0;
262 }