Commit | Line | Data |
---|---|---|
d6a929b7 DS |
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 | |
0acc516a BA |
37 | 0 == unipolar 5V (0V -- +5V) |
38 | 1 == bipolar 5V (-5V -- +5V) | |
d6a929b7 | 39 | [3] - Current offset configuration |
0acc516a BA |
40 | 0 == disabled (0mA -- +32mAV) |
41 | 1 == enabled (+4mA -- +20mAV) | |
d6a929b7 | 42 | [4] - Firmware program configuration |
0acc516a BA |
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) | |
d6a929b7 | 47 | [5] - Analog output 0 range configuration |
0acc516a BA |
48 | 0 == voltage |
49 | 1 == current | |
d6a929b7 DS |
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 | ||
d6a929b7 DS |
64 | #define DT2815_SIZE 2 |
65 | ||
66 | #define DT2815_DATA 0 | |
67 | #define DT2815_STATUS 1 | |
68 | ||
0b011a6f BP |
69 | struct dt2815_private { |
70 | ||
9ced1de6 | 71 | const struct comedi_lrange *range_type_list[8]; |
790c5541 | 72 | unsigned int ao_readback[8]; |
0b011a6f BP |
73 | }; |
74 | ||
da91b269 | 75 | static int dt2815_wait_for_status(struct comedi_device *dev, int status) |
d6a929b7 DS |
76 | { |
77 | int i; | |
78 | ||
79 | for (i = 0; i < 100; i++) { | |
80 | if (inb(dev->iobase + DT2815_STATUS) == status) | |
81 | break; | |
82 | } | |
83 | return status; | |
84 | } | |
85 | ||
0a85b6f0 MT |
86 | static int dt2815_ao_insn_read(struct comedi_device *dev, |
87 | struct comedi_subdevice *s, | |
88 | struct comedi_insn *insn, unsigned int *data) | |
d6a929b7 | 89 | { |
9a1a6cf8 | 90 | struct dt2815_private *devpriv = dev->private; |
d6a929b7 DS |
91 | int i; |
92 | int chan = CR_CHAN(insn->chanspec); | |
93 | ||
0acc516a | 94 | for (i = 0; i < insn->n; i++) |
d6a929b7 | 95 | data[i] = devpriv->ao_readback[chan]; |
d6a929b7 DS |
96 | |
97 | return i; | |
98 | } | |
99 | ||
da91b269 | 100 | static int dt2815_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s, |
0a85b6f0 | 101 | struct comedi_insn *insn, unsigned int *data) |
d6a929b7 | 102 | { |
9a1a6cf8 | 103 | struct dt2815_private *devpriv = dev->private; |
d6a929b7 DS |
104 | int i; |
105 | int chan = CR_CHAN(insn->chanspec); | |
106 | unsigned int status; | |
107 | unsigned int lo, hi; | |
108 | ||
109 | for (i = 0; i < insn->n; i++) { | |
110 | lo = ((data[i] & 0x0f) << 4) | (chan << 1) | 0x01; | |
111 | hi = (data[i] & 0xff0) >> 4; | |
112 | ||
113 | status = dt2815_wait_for_status(dev, 0x00); | |
114 | if (status != 0) { | |
0acc516a BA |
115 | printk(KERN_WARNING "dt2815: failed to write low byte " |
116 | "on %d reason %x\n", chan, status); | |
d6a929b7 DS |
117 | return -EBUSY; |
118 | } | |
119 | ||
120 | outb(lo, dev->iobase + DT2815_DATA); | |
121 | ||
122 | status = dt2815_wait_for_status(dev, 0x10); | |
123 | if (status != 0x10) { | |
0acc516a BA |
124 | printk(KERN_WARNING "dt2815: failed to write high byte " |
125 | "on %d reason %x\n", chan, status); | |
d6a929b7 DS |
126 | return -EBUSY; |
127 | } | |
128 | devpriv->ao_readback[chan] = data[i]; | |
129 | } | |
130 | return i; | |
131 | } | |
132 | ||
133 | /* | |
134 | options[0] Board base address | |
135 | options[1] IRQ (not applicable) | |
136 | options[2] Voltage unipolar/bipolar configuration | |
0acc516a BA |
137 | 0 == unipolar 5V (0V -- +5V) |
138 | 1 == bipolar 5V (-5V -- +5V) | |
d6a929b7 | 139 | options[3] Current offset configuration |
0acc516a BA |
140 | 0 == disabled (0mA -- +32mAV) |
141 | 1 == enabled (+4mA -- +20mAV) | |
d6a929b7 | 142 | options[4] Firmware program configuration |
0acc516a BA |
143 | 0 == program 1 (see manual table 5-4) |
144 | 1 == program 2 (see manual table 5-4) | |
145 | 2 == program 3 (see manual table 5-4) | |
146 | 3 == program 4 (see manual table 5-4) | |
d6a929b7 | 147 | options[5] Analog output 0 range configuration |
0acc516a BA |
148 | 0 == voltage |
149 | 1 == current | |
d6a929b7 DS |
150 | options[6] Analog output 1 range configuration |
151 | ... | |
152 | options[12] Analog output 7 range configuration | |
0acc516a BA |
153 | 0 == voltage |
154 | 1 == current | |
d6a929b7 DS |
155 | */ |
156 | ||
da91b269 | 157 | static int dt2815_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
d6a929b7 | 158 | { |
9a1a6cf8 | 159 | struct dt2815_private *devpriv; |
34c43922 | 160 | struct comedi_subdevice *s; |
d6a929b7 | 161 | int i; |
9ced1de6 | 162 | const struct comedi_lrange *current_range_type, *voltage_range_type; |
8b6c5694 | 163 | int ret; |
d6a929b7 | 164 | |
19b3da3e HS |
165 | ret = comedi_request_region(dev, it->options[0], DT2815_SIZE); |
166 | if (ret) | |
167 | return ret; | |
d6a929b7 | 168 | |
8b6c5694 HS |
169 | ret = comedi_alloc_subdevices(dev, 1); |
170 | if (ret) | |
171 | return ret; | |
172 | ||
c34fa261 HS |
173 | devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL); |
174 | if (!devpriv) | |
175 | return -ENOMEM; | |
176 | dev->private = devpriv; | |
d6a929b7 | 177 | |
76c40c47 | 178 | s = &dev->subdevices[0]; |
d6a929b7 DS |
179 | /* ao subdevice */ |
180 | s->type = COMEDI_SUBD_AO; | |
181 | s->subdev_flags = SDF_WRITABLE; | |
182 | s->maxdata = 0xfff; | |
183 | s->n_chan = 8; | |
184 | s->insn_write = dt2815_ao_insn; | |
185 | s->insn_read = dt2815_ao_insn_read; | |
186 | s->range_table_list = devpriv->range_type_list; | |
187 | ||
188 | current_range_type = (it->options[3]) | |
2c71c4ff | 189 | ? &range_4_20mA : &range_0_32mA; |
d6a929b7 | 190 | voltage_range_type = (it->options[2]) |
0a85b6f0 | 191 | ? &range_bipolar5 : &range_unipolar5; |
d6a929b7 DS |
192 | for (i = 0; i < 8; i++) { |
193 | devpriv->range_type_list[i] = (it->options[5 + i]) | |
0a85b6f0 | 194 | ? current_range_type : voltage_range_type; |
d6a929b7 DS |
195 | } |
196 | ||
197 | /* Init the 2815 */ | |
198 | outb(0x00, dev->iobase + DT2815_STATUS); | |
199 | for (i = 0; i < 100; i++) { | |
200 | /* This is incredibly slow (approx 20 ms) */ | |
201 | unsigned int status; | |
202 | ||
5f74ea14 | 203 | udelay(1000); |
d6a929b7 DS |
204 | status = inb(dev->iobase + DT2815_STATUS); |
205 | if (status == 4) { | |
206 | unsigned int program; | |
207 | program = (it->options[4] & 0x3) << 3 | 0x7; | |
208 | outb(program, dev->iobase + DT2815_DATA); | |
0acc516a BA |
209 | printk(KERN_INFO ", program: 0x%x (@t=%d)\n", |
210 | program, i); | |
d6a929b7 DS |
211 | break; |
212 | } else if (status != 0x00) { | |
0acc516a BA |
213 | printk(KERN_WARNING "dt2815: unexpected status 0x%x " |
214 | "(@t=%d)\n", status, i); | |
215 | if (status & 0x60) | |
d6a929b7 | 216 | outb(0x00, dev->iobase + DT2815_STATUS); |
d6a929b7 DS |
217 | } |
218 | } | |
219 | ||
d6a929b7 DS |
220 | return 0; |
221 | } | |
222 | ||
294f930d | 223 | static struct comedi_driver dt2815_driver = { |
b85eec95 HS |
224 | .driver_name = "dt2815", |
225 | .module = THIS_MODULE, | |
226 | .attach = dt2815_attach, | |
21208519 | 227 | .detach = comedi_legacy_detach, |
b85eec95 | 228 | }; |
294f930d | 229 | module_comedi_driver(dt2815_driver); |
b85eec95 | 230 | |
90f703d3 AT |
231 | MODULE_AUTHOR("Comedi http://www.comedi.org"); |
232 | MODULE_DESCRIPTION("Comedi low-level driver"); | |
233 | MODULE_LICENSE("GPL"); |