]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/dt2815.c
Merge branch 'next' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[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 /*
19 Driver: dt2815
20 Description: Data Translation DT2815
21 Author: ds
22 Status: mostly complete, untested
23 Devices: [Data Translation] DT2815 (dt2815)
24
25 I'm not sure anyone has ever tested this board.  If you have information
26 contrary, please update.
27
28 Configuration options:
29   [0] - I/O port base base address
30   [1] - IRQ (unused)
31   [2] - Voltage unipolar/bipolar configuration
32         0 == unipolar 5V  (0V -- +5V)
33         1 == bipolar 5V  (-5V -- +5V)
34   [3] - Current offset configuration
35         0 == disabled  (0mA -- +32mAV)
36         1 == enabled  (+4mA -- +20mAV)
37   [4] - Firmware program configuration
38         0 == program 1 (see manual table 5-4)
39         1 == program 2 (see manual table 5-4)
40         2 == program 3 (see manual table 5-4)
41         3 == program 4 (see manual table 5-4)
42   [5] - Analog output 0 range configuration
43         0 == voltage
44         1 == current
45   [6] - Analog output 1 range configuration (same options)
46   [7] - Analog output 2 range configuration (same options)
47   [8] - Analog output 3 range configuration (same options)
48   [9] - Analog output 4 range configuration (same options)
49   [10] - Analog output 5 range configuration (same options)
50   [11] - Analog output 6 range configuration (same options)
51   [12] - Analog output 7 range configuration (same options)
52 */
53
54 #include <linux/module.h>
55 #include "../comedidev.h"
56
57 #include <linux/delay.h>
58
59 #define DT2815_SIZE 2
60
61 #define DT2815_DATA 0
62 #define DT2815_STATUS 1
63
64 struct dt2815_private {
65
66         const struct comedi_lrange *range_type_list[8];
67         unsigned int ao_readback[8];
68 };
69
70 static int dt2815_wait_for_status(struct comedi_device *dev, int status)
71 {
72         int i;
73
74         for (i = 0; i < 100; i++) {
75                 if (inb(dev->iobase + DT2815_STATUS) == status)
76                         break;
77         }
78         return status;
79 }
80
81 static int dt2815_ao_insn_read(struct comedi_device *dev,
82                                struct comedi_subdevice *s,
83                                struct comedi_insn *insn, unsigned int *data)
84 {
85         struct dt2815_private *devpriv = dev->private;
86         int i;
87         int chan = CR_CHAN(insn->chanspec);
88
89         for (i = 0; i < insn->n; i++)
90                 data[i] = devpriv->ao_readback[chan];
91
92         return i;
93 }
94
95 static int dt2815_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
96                           struct comedi_insn *insn, unsigned int *data)
97 {
98         struct dt2815_private *devpriv = dev->private;
99         int i;
100         int chan = CR_CHAN(insn->chanspec);
101         unsigned int status;
102         unsigned int lo, hi;
103
104         for (i = 0; i < insn->n; i++) {
105                 lo = ((data[i] & 0x0f) << 4) | (chan << 1) | 0x01;
106                 hi = (data[i] & 0xff0) >> 4;
107
108                 status = dt2815_wait_for_status(dev, 0x00);
109                 if (status != 0) {
110                         printk(KERN_WARNING "dt2815: failed to write low byte "
111                                "on %d reason %x\n", chan, status);
112                         return -EBUSY;
113                 }
114
115                 outb(lo, dev->iobase + DT2815_DATA);
116
117                 status = dt2815_wait_for_status(dev, 0x10);
118                 if (status != 0x10) {
119                         printk(KERN_WARNING "dt2815: failed to write high byte "
120                                "on %d reason %x\n", chan, status);
121                         return -EBUSY;
122                 }
123                 devpriv->ao_readback[chan] = data[i];
124         }
125         return i;
126 }
127
128 /*
129   options[0]   Board base address
130   options[1]   IRQ (not applicable)
131   options[2]   Voltage unipolar/bipolar configuration
132                 0 == unipolar 5V  (0V -- +5V)
133                 1 == bipolar 5V  (-5V -- +5V)
134   options[3]   Current offset configuration
135                 0 == disabled  (0mA -- +32mAV)
136                 1 == enabled  (+4mA -- +20mAV)
137   options[4]   Firmware program configuration
138                 0 == program 1 (see manual table 5-4)
139                 1 == program 2 (see manual table 5-4)
140                 2 == program 3 (see manual table 5-4)
141                 3 == program 4 (see manual table 5-4)
142   options[5]   Analog output 0 range configuration
143                 0 == voltage
144                 1 == current
145   options[6]   Analog output 1 range configuration
146   ...
147   options[12]   Analog output 7 range configuration
148                 0 == voltage
149                 1 == current
150  */
151
152 static int dt2815_attach(struct comedi_device *dev, struct comedi_devconfig *it)
153 {
154         struct dt2815_private *devpriv;
155         struct comedi_subdevice *s;
156         int i;
157         const struct comedi_lrange *current_range_type, *voltage_range_type;
158         int ret;
159
160         ret = comedi_request_region(dev, it->options[0], DT2815_SIZE);
161         if (ret)
162                 return ret;
163
164         ret = comedi_alloc_subdevices(dev, 1);
165         if (ret)
166                 return ret;
167
168         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
169         if (!devpriv)
170                 return -ENOMEM;
171
172         s = &dev->subdevices[0];
173         /* ao subdevice */
174         s->type = COMEDI_SUBD_AO;
175         s->subdev_flags = SDF_WRITABLE;
176         s->maxdata = 0xfff;
177         s->n_chan = 8;
178         s->insn_write = dt2815_ao_insn;
179         s->insn_read = dt2815_ao_insn_read;
180         s->range_table_list = devpriv->range_type_list;
181
182         current_range_type = (it->options[3])
183             ? &range_4_20mA : &range_0_32mA;
184         voltage_range_type = (it->options[2])
185             ? &range_bipolar5 : &range_unipolar5;
186         for (i = 0; i < 8; i++) {
187                 devpriv->range_type_list[i] = (it->options[5 + i])
188                     ? current_range_type : voltage_range_type;
189         }
190
191         /* Init the 2815 */
192         outb(0x00, dev->iobase + DT2815_STATUS);
193         for (i = 0; i < 100; i++) {
194                 /* This is incredibly slow (approx 20 ms) */
195                 unsigned int status;
196
197                 udelay(1000);
198                 status = inb(dev->iobase + DT2815_STATUS);
199                 if (status == 4) {
200                         unsigned int program;
201                         program = (it->options[4] & 0x3) << 3 | 0x7;
202                         outb(program, dev->iobase + DT2815_DATA);
203                         printk(KERN_INFO ", program: 0x%x (@t=%d)\n",
204                                program, i);
205                         break;
206                 } else if (status != 0x00) {
207                         printk(KERN_WARNING "dt2815: unexpected status 0x%x "
208                                "(@t=%d)\n", status, i);
209                         if (status & 0x60)
210                                 outb(0x00, dev->iobase + DT2815_STATUS);
211                 }
212         }
213
214         return 0;
215 }
216
217 static struct comedi_driver dt2815_driver = {
218         .driver_name    = "dt2815",
219         .module         = THIS_MODULE,
220         .attach         = dt2815_attach,
221         .detach         = comedi_legacy_detach,
222 };
223 module_comedi_driver(dt2815_driver);
224
225 MODULE_AUTHOR("Comedi http://www.comedi.org");
226 MODULE_DESCRIPTION("Comedi low-level driver");
227 MODULE_LICENSE("GPL");