Merge branch 'late/fixes' into fixes
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / drivers / dt2814.c
1 /*
2     comedi/drivers/dt2814.c
3     Hardware driver for Data Translation DT2814
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1998 David A. Schleef <ds@schleef.org>
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: dt2814
25 Description: Data Translation DT2814
26 Author: ds
27 Status: complete
28 Devices: [Data Translation] DT2814 (dt2814)
29
30 Configuration options:
31   [0] - I/O port base address
32   [1] - IRQ
33
34 This card has 16 analog inputs multiplexed onto a 12 bit ADC.  There
35 is a minimally useful onboard clock.  The base frequency for the
36 clock is selected by jumpers, and the clock divider can be selected
37 via programmed I/O.  Unfortunately, the clock divider can only be
38 a power of 10, from 1 to 10^7, of which only 3 or 4 are useful.  In
39 addition, the clock does not seem to be very accurate.
40 */
41
42 #include <linux/interrupt.h>
43 #include "../comedidev.h"
44
45 #include <linux/ioport.h>
46 #include <linux/delay.h>
47
48 #include "comedi_fc.h"
49
50 #define DT2814_SIZE 2
51
52 #define DT2814_CSR 0
53 #define DT2814_DATA 1
54
55 /*
56  * flags
57  */
58
59 #define DT2814_FINISH 0x80
60 #define DT2814_ERR 0x40
61 #define DT2814_BUSY 0x20
62 #define DT2814_ENB 0x10
63 #define DT2814_CHANMASK 0x0f
64
65 struct dt2814_private {
66
67         int ntrig;
68         int curadchan;
69 };
70
71 #define DT2814_TIMEOUT 10
72 #define DT2814_MAX_SPEED 100000 /* Arbitrary 10 khz limit */
73
74 static int dt2814_ai_insn_read(struct comedi_device *dev,
75                                struct comedi_subdevice *s,
76                                struct comedi_insn *insn, unsigned int *data)
77 {
78         int n, i, hi, lo;
79         int chan;
80         int status = 0;
81
82         for (n = 0; n < insn->n; n++) {
83                 chan = CR_CHAN(insn->chanspec);
84
85                 outb(chan, dev->iobase + DT2814_CSR);
86                 for (i = 0; i < DT2814_TIMEOUT; i++) {
87                         status = inb(dev->iobase + DT2814_CSR);
88                         printk(KERN_INFO "dt2814: status: %02x\n", status);
89                         udelay(10);
90                         if (status & DT2814_FINISH)
91                                 break;
92                 }
93                 if (i >= DT2814_TIMEOUT) {
94                         printk(KERN_INFO "dt2814: status: %02x\n", status);
95                         return -ETIMEDOUT;
96                 }
97
98                 hi = inb(dev->iobase + DT2814_DATA);
99                 lo = inb(dev->iobase + DT2814_DATA);
100
101                 data[n] = (hi << 4) | (lo >> 4);
102         }
103
104         return n;
105 }
106
107 static int dt2814_ns_to_timer(unsigned int *ns, unsigned int flags)
108 {
109         int i;
110         unsigned int f;
111
112         /* XXX ignores flags */
113
114         f = 10000;              /* ns */
115         for (i = 0; i < 8; i++) {
116                 if ((2 * (*ns)) < (f * 11))
117                         break;
118                 f *= 10;
119         }
120
121         *ns = f;
122
123         return i;
124 }
125
126 static int dt2814_ai_cmdtest(struct comedi_device *dev,
127                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
128 {
129         int err = 0;
130         int tmp;
131
132         /* Step 1 : check if triggers are trivially valid */
133
134         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
135         err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
136         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
137         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
138         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
139
140         if (err)
141                 return 1;
142
143         /* Step 2a : make sure trigger sources are unique */
144
145         err |= cfc_check_trigger_is_unique(cmd->stop_src);
146
147         /* Step 2b : and mutually compatible */
148
149         if (err)
150                 return 2;
151
152         /* Step 3: check if arguments are trivially valid */
153
154         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
155
156         err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 1000000000);
157         err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
158                                          DT2814_MAX_SPEED);
159
160         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
161
162         if (cmd->stop_src == TRIG_COUNT)
163                 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 2);
164         else    /* TRIG_NONE */
165                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
166
167         if (err)
168                 return 3;
169
170         /* step 4: fix up any arguments */
171
172         tmp = cmd->scan_begin_arg;
173         dt2814_ns_to_timer(&cmd->scan_begin_arg, cmd->flags & TRIG_ROUND_MASK);
174         if (tmp != cmd->scan_begin_arg)
175                 err++;
176
177         if (err)
178                 return 4;
179
180         return 0;
181 }
182
183 static int dt2814_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
184 {
185         struct dt2814_private *devpriv = dev->private;
186         struct comedi_cmd *cmd = &s->async->cmd;
187         int chan;
188         int trigvar;
189
190         trigvar =
191             dt2814_ns_to_timer(&cmd->scan_begin_arg,
192                                cmd->flags & TRIG_ROUND_MASK);
193
194         chan = CR_CHAN(cmd->chanlist[0]);
195
196         devpriv->ntrig = cmd->stop_arg;
197         outb(chan | DT2814_ENB | (trigvar << 5), dev->iobase + DT2814_CSR);
198
199         return 0;
200
201 }
202
203 static irqreturn_t dt2814_interrupt(int irq, void *d)
204 {
205         int lo, hi;
206         struct comedi_device *dev = d;
207         struct dt2814_private *devpriv = dev->private;
208         struct comedi_subdevice *s;
209         int data;
210
211         if (!dev->attached) {
212                 comedi_error(dev, "spurious interrupt");
213                 return IRQ_HANDLED;
214         }
215
216         s = &dev->subdevices[0];
217
218         hi = inb(dev->iobase + DT2814_DATA);
219         lo = inb(dev->iobase + DT2814_DATA);
220
221         data = (hi << 4) | (lo >> 4);
222
223         if (!(--devpriv->ntrig)) {
224                 int i;
225
226                 outb(0, dev->iobase + DT2814_CSR);
227                 /* note: turning off timed mode triggers another
228                    sample. */
229
230                 for (i = 0; i < DT2814_TIMEOUT; i++) {
231                         if (inb(dev->iobase + DT2814_CSR) & DT2814_FINISH)
232                                 break;
233                 }
234                 inb(dev->iobase + DT2814_DATA);
235                 inb(dev->iobase + DT2814_DATA);
236
237                 s->async->events |= COMEDI_CB_EOA;
238         }
239         comedi_event(dev, s);
240         return IRQ_HANDLED;
241 }
242
243 static int dt2814_attach(struct comedi_device *dev, struct comedi_devconfig *it)
244 {
245         struct dt2814_private *devpriv;
246         int i, irq;
247         int ret;
248         struct comedi_subdevice *s;
249
250         ret = comedi_request_region(dev, it->options[0], DT2814_SIZE);
251         if (ret)
252                 return ret;
253
254         outb(0, dev->iobase + DT2814_CSR);
255         udelay(100);
256         if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR) {
257                 printk(KERN_ERR "reset error (fatal)\n");
258                 return -EIO;
259         }
260         i = inb(dev->iobase + DT2814_DATA);
261         i = inb(dev->iobase + DT2814_DATA);
262
263         irq = it->options[1];
264 #if 0
265         if (irq < 0) {
266                 save_flags(flags);
267                 sti();
268                 irqs = probe_irq_on();
269
270                 outb(0, dev->iobase + DT2814_CSR);
271
272                 udelay(100);
273
274                 irq = probe_irq_off(irqs);
275                 restore_flags(flags);
276                 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR)
277                         printk(KERN_DEBUG "error probing irq (bad)\n");
278
279
280                 i = inb(dev->iobase + DT2814_DATA);
281                 i = inb(dev->iobase + DT2814_DATA);
282         }
283 #endif
284         dev->irq = 0;
285         if (irq > 0) {
286                 if (request_irq(irq, dt2814_interrupt, 0, "dt2814", dev)) {
287                         printk(KERN_WARNING "(irq %d unavailable)\n", irq);
288                 } else {
289                         printk(KERN_INFO "( irq = %d )\n", irq);
290                         dev->irq = irq;
291                 }
292         } else if (irq == 0) {
293                 printk(KERN_WARNING "(no irq)\n");
294         } else {
295 #if 0
296                 printk(KERN_DEBUG "(probe returned multiple irqs--bad)\n");
297 #else
298                 printk(KERN_WARNING "(irq probe not implemented)\n");
299 #endif
300         }
301
302         ret = comedi_alloc_subdevices(dev, 1);
303         if (ret)
304                 return ret;
305
306         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
307         if (!devpriv)
308                 return -ENOMEM;
309         dev->private = devpriv;
310
311         s = &dev->subdevices[0];
312         dev->read_subdev = s;
313         s->type = COMEDI_SUBD_AI;
314         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
315         s->n_chan = 16;         /* XXX */
316         s->len_chanlist = 1;
317         s->insn_read = dt2814_ai_insn_read;
318         s->do_cmd = dt2814_ai_cmd;
319         s->do_cmdtest = dt2814_ai_cmdtest;
320         s->maxdata = 0xfff;
321         s->range_table = &range_unknown;        /* XXX */
322
323         return 0;
324 }
325
326 static struct comedi_driver dt2814_driver = {
327         .driver_name    = "dt2814",
328         .module         = THIS_MODULE,
329         .attach         = dt2814_attach,
330         .detach         = comedi_legacy_detach,
331 };
332 module_comedi_driver(dt2814_driver);
333
334 MODULE_AUTHOR("Comedi http://www.comedi.org");
335 MODULE_DESCRIPTION("Comedi low-level driver");
336 MODULE_LICENSE("GPL");