staging: comedi: das16m1: introduce das16m1_ai_set_queue()
[cascardo/linux.git] / drivers / staging / comedi / drivers / das16m1.c
1 /*
2  * Comedi driver for CIO-DAS16/M1
3  * Author: Frank Mori Hess, based on code from the das16 driver.
4  * Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
5  *
6  * COMEDI - Linux Control and Measurement Device Interface
7  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 /*
21  * Driver: das16m1
22  * Description: CIO-DAS16/M1
23  * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
24  * Devices: [Measurement Computing] CIO-DAS16/M1 (das16m1)
25  * Status: works
26  *
27  * This driver supports a single board - the CIO-DAS16/M1. As far as I know,
28  * there are no other boards that have the same register layout. Even the
29  * CIO-DAS16/M1/16 is significantly different.
30  *
31  * I was _barely_ able to reach the full 1 MHz capability of this board, using
32  * a hard real-time interrupt (set the TRIG_RT flag in your struct comedi_cmd
33  * and use rtlinux or RTAI). The board can't do dma, so the bottleneck is
34  * pulling the data across the ISA bus. I timed the interrupt handler, and it
35  * took my computer ~470 microseconds to pull 512 samples from the board. So
36  * at 1 Mhz sampling rate, expect your CPU to be spending almost all of its
37  * time in the interrupt handler.
38  *
39  * This board has some unusual restrictions for its channel/gain list.  If the
40  * list has 2 or more channels in it, then two conditions must be satisfied:
41  * (1) - even/odd channels must appear at even/odd indices in the list
42  * (2) - the list must have an even number of entries.
43  *
44  * Configuration options:
45  *   [0] - base io address
46  *   [1] - irq (optional, but you probably want it)
47  *
48  * irq can be omitted, although the cmd interface will not work without it.
49  */
50
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/interrupt.h>
54 #include "../comedidev.h"
55
56 #include "8255.h"
57 #include "comedi_8254.h"
58
59 #define DAS16M1_SIZE2 8
60
61 #define FIFO_SIZE 1024          /*  1024 sample fifo */
62
63 /*
64  * Register map (dev->iobase)
65  */
66 #define DAS16M1_AI_REG                  0x00    /* 16-bit register */
67 #define DAS16M1_AI_TO_CHAN(x)           (((x) >> 0) & 0xf)
68 #define DAS16M1_AI_TO_SAMPLE(x)         (((x) >> 4) & 0xfff)
69 #define DAS16M1_CS_REG                  0x02
70 #define DAS16M1_CS_EXT_TRIG             BIT(0)
71 #define DAS16M1_CS_OVRUN                BIT(5)
72 #define DAS16M1_CS_IRQDATA              BIT(7)
73 #define DAS16M1_DI_REG                  0x03
74 #define DAS16M1_DO_REG                  0x03
75 #define DAS16M1_CLR_INTR_REG            0x04
76 #define DAS16M1_INTR_CTRL_REG           0x05
77 #define DAS16M1_INTR_CTRL_PACER(x)      (((x) & 0x3) << 0)
78 #define DAS16M1_INTR_CTRL_PACER_EXT     DAS16M1_INTR_CTRL_PACER(2)
79 #define DAS16M1_INTR_CTRL_PACER_INT     DAS16M1_INTR_CTRL_PACER(3)
80 #define DAS16M1_INTR_CTRL_PACER_MASK    DAS16M1_INTR_CTRL_PACER(3)
81 #define DAS16M1_INTR_CTRL_IRQ(x)        (((x) & 0x7) << 4)
82 #define DAS16M1_INTR_CTRL_INTE          BIT(7)
83 #define DAS16M1_Q_ADDR_REG              0x06
84 #define DAS16M1_Q_REG                   0x07
85 #define DAS16M1_Q_CHAN(x)              (((x) & 0x7) << 0)
86 #define DAS16M1_Q_RANGE(x)             (((x) & 0xf) << 4)
87 #define DAS16M1_8254_FIRST             0x8
88 #define DAS16M1_8254_SECOND            0xc
89 #define DAS16M1_82C55                  0x400
90 #define DAS16M1_8254_THIRD             0x404
91
92 static const struct comedi_lrange range_das16m1 = {
93         9, {
94                 BIP_RANGE(5),
95                 BIP_RANGE(2.5),
96                 BIP_RANGE(1.25),
97                 BIP_RANGE(0.625),
98                 UNI_RANGE(10),
99                 UNI_RANGE(5),
100                 UNI_RANGE(2.5),
101                 UNI_RANGE(1.25),
102                 BIP_RANGE(10)
103         }
104 };
105
106 struct das16m1_private_struct {
107         struct comedi_8254 *counter;
108         unsigned int intr_ctrl;
109         unsigned int adc_count;
110         u16 initial_hw_count;
111         unsigned short ai_buffer[FIFO_SIZE];
112         unsigned long extra_iobase;
113 };
114
115 static void das16m1_ai_set_queue(struct comedi_device *dev,
116                                  unsigned int *chanspec, unsigned int len)
117 {
118         unsigned int i;
119
120         for (i = 0; i < len; i++) {
121                 unsigned int chan = CR_CHAN(chanspec[i]);
122                 unsigned int range = CR_RANGE(chanspec[i]);
123
124                 outb(i, dev->iobase + DAS16M1_Q_ADDR_REG);
125                 outb(DAS16M1_Q_CHAN(chan) | DAS16M1_Q_RANGE(range),
126                      dev->iobase + DAS16M1_Q_REG);
127         }
128 }
129
130 static void munge_sample_array(unsigned short *array, unsigned int num_elements)
131 {
132         unsigned int i;
133
134         for (i = 0; i < num_elements; i++)
135                 array[i] = DAS16M1_AI_TO_SAMPLE(array[i]);
136 }
137
138 static int das16m1_ai_check_chanlist(struct comedi_device *dev,
139                                      struct comedi_subdevice *s,
140                                      struct comedi_cmd *cmd)
141 {
142         int i;
143
144         if (cmd->chanlist_len == 1)
145                 return 0;
146
147         if ((cmd->chanlist_len % 2) != 0) {
148                 dev_dbg(dev->class_dev,
149                         "chanlist must be of even length or length 1\n");
150                 return -EINVAL;
151         }
152
153         for (i = 0; i < cmd->chanlist_len; i++) {
154                 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
155
156                 if ((i % 2) != (chan % 2)) {
157                         dev_dbg(dev->class_dev,
158                                 "even/odd channels must go have even/odd chanlist indices\n");
159                         return -EINVAL;
160                 }
161         }
162
163         return 0;
164 }
165
166 static int das16m1_cmd_test(struct comedi_device *dev,
167                             struct comedi_subdevice *s, struct comedi_cmd *cmd)
168 {
169         int err = 0;
170
171         /* Step 1 : check if triggers are trivially valid */
172
173         err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
174         err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
175         err |= comedi_check_trigger_src(&cmd->convert_src,
176                                         TRIG_TIMER | TRIG_EXT);
177         err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
178         err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
179
180         if (err)
181                 return 1;
182
183         /* Step 2a : make sure trigger sources are unique */
184
185         err |= comedi_check_trigger_is_unique(cmd->start_src);
186         err |= comedi_check_trigger_is_unique(cmd->convert_src);
187         err |= comedi_check_trigger_is_unique(cmd->stop_src);
188
189         /* Step 2b : and mutually compatible */
190
191         if (err)
192                 return 2;
193
194         /* Step 3: check if arguments are trivially valid */
195
196         err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
197
198         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
199                 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
200
201         if (cmd->convert_src == TRIG_TIMER)
202                 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 1000);
203
204         err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
205                                            cmd->chanlist_len);
206
207         if (cmd->stop_src == TRIG_COUNT)
208                 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
209         else    /* TRIG_NONE */
210                 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
211
212         if (err)
213                 return 3;
214
215         /* step 4: fix up arguments */
216
217         if (cmd->convert_src == TRIG_TIMER) {
218                 unsigned int arg = cmd->convert_arg;
219
220                 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
221                 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
222         }
223
224         if (err)
225                 return 4;
226
227         /* Step 5: check channel list if it exists */
228         if (cmd->chanlist && cmd->chanlist_len > 0)
229                 err |= das16m1_ai_check_chanlist(dev, s, cmd);
230
231         if (err)
232                 return 5;
233
234         return 0;
235 }
236
237 static int das16m1_cmd_exec(struct comedi_device *dev,
238                             struct comedi_subdevice *s)
239 {
240         struct das16m1_private_struct *devpriv = dev->private;
241         struct comedi_async *async = s->async;
242         struct comedi_cmd *cmd = &async->cmd;
243         unsigned int byte;
244
245         /*  set software count */
246         devpriv->adc_count = 0;
247
248         /*
249          * Initialize lower half of hardware counter, used to determine how
250          * many samples are in fifo.  Value doesn't actually load into counter
251          * until counter's next clock (the next a/d conversion).
252          */
253         comedi_8254_set_mode(devpriv->counter, 1, I8254_MODE2 | I8254_BINARY);
254         comedi_8254_write(devpriv->counter, 1, 0);
255
256         /*
257          * Remember current reading of counter so we know when counter has
258          * actually been loaded.
259          */
260         devpriv->initial_hw_count = comedi_8254_read(devpriv->counter, 1);
261
262         das16m1_ai_set_queue(dev, cmd->chanlist, cmd->chanlist_len);
263
264         /* enable interrupts and set internal pacer counter mode and counts */
265         devpriv->intr_ctrl &= ~DAS16M1_INTR_CTRL_PACER_MASK;
266         if (cmd->convert_src == TRIG_TIMER) {
267                 comedi_8254_update_divisors(dev->pacer);
268                 comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
269                 devpriv->intr_ctrl |= DAS16M1_INTR_CTRL_PACER_INT;
270         } else {        /* TRIG_EXT */
271                 devpriv->intr_ctrl |= DAS16M1_INTR_CTRL_PACER_EXT;
272         }
273
274         /*  set control & status register */
275         byte = 0;
276         /*
277          * If we are using external start trigger (also board dislikes having
278          * both start and conversion triggers external simultaneously).
279          */
280         if (cmd->start_src == TRIG_EXT && cmd->convert_src != TRIG_EXT)
281                 byte |= DAS16M1_CS_EXT_TRIG;
282
283         outb(byte, dev->iobase + DAS16M1_CS_REG);
284
285         /* clear interrupt */
286         outb(0, dev->iobase + DAS16M1_CLR_INTR_REG);
287
288         devpriv->intr_ctrl |= DAS16M1_INTR_CTRL_INTE;
289         outb(devpriv->intr_ctrl, dev->iobase + DAS16M1_INTR_CTRL_REG);
290
291         return 0;
292 }
293
294 static int das16m1_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
295 {
296         struct das16m1_private_struct *devpriv = dev->private;
297
298         /* disable interrupts and pacer */
299         devpriv->intr_ctrl &= ~(DAS16M1_INTR_CTRL_INTE |
300                                 DAS16M1_INTR_CTRL_PACER_MASK);
301         outb(devpriv->intr_ctrl, dev->iobase + DAS16M1_INTR_CTRL_REG);
302
303         return 0;
304 }
305
306 static int das16m1_ai_eoc(struct comedi_device *dev,
307                           struct comedi_subdevice *s,
308                           struct comedi_insn *insn,
309                           unsigned long context)
310 {
311         unsigned int status;
312
313         status = inb(dev->iobase + DAS16M1_CS_REG);
314         if (status & DAS16M1_CS_IRQDATA)
315                 return 0;
316         return -EBUSY;
317 }
318
319 static int das16m1_ai_rinsn(struct comedi_device *dev,
320                             struct comedi_subdevice *s,
321                             struct comedi_insn *insn, unsigned int *data)
322 {
323         int ret;
324         int n;
325
326         das16m1_ai_set_queue(dev, &insn->chanspec, 1);
327
328         for (n = 0; n < insn->n; n++) {
329                 unsigned short val;
330
331                 /* clear interrupt */
332                 outb(0, dev->iobase + DAS16M1_CLR_INTR_REG);
333                 /* trigger conversion */
334                 outb(0, dev->iobase + DAS16M1_AI_REG);
335
336                 ret = comedi_timeout(dev, s, insn, das16m1_ai_eoc, 0);
337                 if (ret)
338                         return ret;
339
340                 val = inw(dev->iobase + DAS16M1_AI_REG);
341                 data[n] = DAS16M1_AI_TO_SAMPLE(val);
342         }
343
344         return n;
345 }
346
347 static int das16m1_di_rbits(struct comedi_device *dev,
348                             struct comedi_subdevice *s,
349                             struct comedi_insn *insn, unsigned int *data)
350 {
351         unsigned int bits;
352
353         bits = inb(dev->iobase + DAS16M1_DI_REG) & 0xf;
354         data[1] = bits;
355         data[0] = 0;
356
357         return insn->n;
358 }
359
360 static int das16m1_do_wbits(struct comedi_device *dev,
361                             struct comedi_subdevice *s,
362                             struct comedi_insn *insn,
363                             unsigned int *data)
364 {
365         if (comedi_dio_update_state(s, data))
366                 outb(s->state, dev->iobase + DAS16M1_DO_REG);
367
368         data[1] = s->state;
369
370         return insn->n;
371 }
372
373 static void das16m1_handler(struct comedi_device *dev, unsigned int status)
374 {
375         struct das16m1_private_struct *devpriv = dev->private;
376         struct comedi_subdevice *s;
377         struct comedi_async *async;
378         struct comedi_cmd *cmd;
379         u16 num_samples;
380         u16 hw_counter;
381
382         s = dev->read_subdev;
383         async = s->async;
384         cmd = &async->cmd;
385
386         /* figure out how many samples are in fifo */
387         hw_counter = comedi_8254_read(devpriv->counter, 1);
388         /*
389          * Make sure hardware counter reading is not bogus due to initial
390          * value not having been loaded yet.
391          */
392         if (devpriv->adc_count == 0 &&
393             hw_counter == devpriv->initial_hw_count) {
394                 num_samples = 0;
395         } else {
396                 /*
397                  * The calculation of num_samples looks odd, but it uses the
398                  * following facts. 16 bit hardware counter is initialized with
399                  * value of zero (which really means 0x1000).  The counter
400                  * decrements by one on each conversion (when the counter
401                  * decrements from zero it goes to 0xffff).  num_samples is a
402                  * 16 bit variable, so it will roll over in a similar fashion
403                  * to the hardware counter.  Work it out, and this is what you
404                  * get.
405                  */
406                 num_samples = -hw_counter - devpriv->adc_count;
407         }
408         /*  check if we only need some of the points */
409         if (cmd->stop_src == TRIG_COUNT) {
410                 if (num_samples > cmd->stop_arg * cmd->chanlist_len)
411                         num_samples = cmd->stop_arg * cmd->chanlist_len;
412         }
413         /*  make sure we dont try to get too many points if fifo has overrun */
414         if (num_samples > FIFO_SIZE)
415                 num_samples = FIFO_SIZE;
416         insw(dev->iobase, devpriv->ai_buffer, num_samples);
417         munge_sample_array(devpriv->ai_buffer, num_samples);
418         comedi_buf_write_samples(s, devpriv->ai_buffer, num_samples);
419         devpriv->adc_count += num_samples;
420
421         if (cmd->stop_src == TRIG_COUNT) {
422                 if (devpriv->adc_count >= cmd->stop_arg * cmd->chanlist_len) {
423                         /* end of acquisition */
424                         async->events |= COMEDI_CB_EOA;
425                 }
426         }
427
428         /*
429          * This probably won't catch overruns since the card doesn't generate
430          * overrun interrupts, but we might as well try.
431          */
432         if (status & DAS16M1_CS_OVRUN) {
433                 async->events |= COMEDI_CB_ERROR;
434                 dev_err(dev->class_dev, "fifo overflow\n");
435         }
436
437         comedi_handle_events(dev, s);
438 }
439
440 static int das16m1_poll(struct comedi_device *dev, struct comedi_subdevice *s)
441 {
442         unsigned long flags;
443         unsigned int status;
444
445         /*  prevent race with interrupt handler */
446         spin_lock_irqsave(&dev->spinlock, flags);
447         status = inb(dev->iobase + DAS16M1_CS_REG);
448         das16m1_handler(dev, status);
449         spin_unlock_irqrestore(&dev->spinlock, flags);
450
451         return comedi_buf_n_bytes_ready(s);
452 }
453
454 static irqreturn_t das16m1_interrupt(int irq, void *d)
455 {
456         int status;
457         struct comedi_device *dev = d;
458
459         if (!dev->attached) {
460                 dev_err(dev->class_dev, "premature interrupt\n");
461                 return IRQ_HANDLED;
462         }
463         /*  prevent race with comedi_poll() */
464         spin_lock(&dev->spinlock);
465
466         status = inb(dev->iobase + DAS16M1_CS_REG);
467
468         if ((status & (DAS16M1_CS_IRQDATA | DAS16M1_CS_OVRUN)) == 0) {
469                 dev_err(dev->class_dev, "spurious interrupt\n");
470                 spin_unlock(&dev->spinlock);
471                 return IRQ_NONE;
472         }
473
474         das16m1_handler(dev, status);
475
476         /* clear interrupt */
477         outb(0, dev->iobase + DAS16M1_CLR_INTR_REG);
478
479         spin_unlock(&dev->spinlock);
480         return IRQ_HANDLED;
481 }
482
483 static int das16m1_irq_bits(unsigned int irq)
484 {
485         switch (irq) {
486         case 10:
487                 return 0x0;
488         case 11:
489                 return 0x1;
490         case 12:
491                 return 0x2;
492         case 15:
493                 return 0x3;
494         case 2:
495                 return 0x4;
496         case 3:
497                 return 0x5;
498         case 5:
499                 return 0x6;
500         case 7:
501                 return 0x7;
502         default:
503                 return 0x0;
504         }
505 }
506
507 /*
508  * Options list:
509  *   0  I/O base
510  *   1  IRQ
511  */
512 static int das16m1_attach(struct comedi_device *dev,
513                           struct comedi_devconfig *it)
514 {
515         struct das16m1_private_struct *devpriv;
516         struct comedi_subdevice *s;
517         int ret;
518
519         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
520         if (!devpriv)
521                 return -ENOMEM;
522
523         ret = comedi_request_region(dev, it->options[0], 0x10);
524         if (ret)
525                 return ret;
526         /* Request an additional region for the 8255 */
527         ret = __comedi_request_region(dev, dev->iobase + DAS16M1_82C55,
528                                       DAS16M1_SIZE2);
529         if (ret)
530                 return ret;
531         devpriv->extra_iobase = dev->iobase + DAS16M1_82C55;
532
533         /* only irqs 2, 3, 4, 5, 6, 7, 10, 11, 12, 14, and 15 are valid */
534         if ((1 << it->options[1]) & 0xdcfc) {
535                 ret = request_irq(it->options[1], das16m1_interrupt, 0,
536                                   dev->board_name, dev);
537                 if (ret == 0)
538                         dev->irq = it->options[1];
539         }
540
541         dev->pacer = comedi_8254_init(dev->iobase + DAS16M1_8254_SECOND,
542                                       I8254_OSC_BASE_10MHZ, I8254_IO8, 0);
543         if (!dev->pacer)
544                 return -ENOMEM;
545
546         devpriv->counter = comedi_8254_init(dev->iobase + DAS16M1_8254_FIRST,
547                                             0, I8254_IO8, 0);
548         if (!devpriv->counter)
549                 return -ENOMEM;
550
551         ret = comedi_alloc_subdevices(dev, 4);
552         if (ret)
553                 return ret;
554
555         s = &dev->subdevices[0];
556         /* ai */
557         s->type = COMEDI_SUBD_AI;
558         s->subdev_flags = SDF_READABLE | SDF_DIFF;
559         s->n_chan = 8;
560         s->maxdata = (1 << 12) - 1;
561         s->range_table = &range_das16m1;
562         s->insn_read = das16m1_ai_rinsn;
563         if (dev->irq) {
564                 dev->read_subdev = s;
565                 s->subdev_flags |= SDF_CMD_READ;
566                 s->len_chanlist = 256;
567                 s->do_cmdtest = das16m1_cmd_test;
568                 s->do_cmd = das16m1_cmd_exec;
569                 s->cancel = das16m1_cancel;
570                 s->poll = das16m1_poll;
571         }
572
573         s = &dev->subdevices[1];
574         /* di */
575         s->type = COMEDI_SUBD_DI;
576         s->subdev_flags = SDF_READABLE;
577         s->n_chan = 4;
578         s->maxdata = 1;
579         s->range_table = &range_digital;
580         s->insn_bits = das16m1_di_rbits;
581
582         s = &dev->subdevices[2];
583         /* do */
584         s->type = COMEDI_SUBD_DO;
585         s->subdev_flags = SDF_WRITABLE;
586         s->n_chan = 4;
587         s->maxdata = 1;
588         s->range_table = &range_digital;
589         s->insn_bits = das16m1_do_wbits;
590
591         s = &dev->subdevices[3];
592         /* 8255 */
593         ret = subdev_8255_init(dev, s, NULL, DAS16M1_82C55);
594         if (ret)
595                 return ret;
596
597         /*  initialize digital output lines */
598         outb(0, dev->iobase + DAS16M1_DO_REG);
599
600         /* set the interrupt level */
601         devpriv->intr_ctrl = DAS16M1_INTR_CTRL_IRQ(das16m1_irq_bits(dev->irq));
602         outb(devpriv->intr_ctrl, dev->iobase + DAS16M1_INTR_CTRL_REG);
603
604         return 0;
605 }
606
607 static void das16m1_detach(struct comedi_device *dev)
608 {
609         struct das16m1_private_struct *devpriv = dev->private;
610
611         if (devpriv) {
612                 if (devpriv->extra_iobase)
613                         release_region(devpriv->extra_iobase, DAS16M1_SIZE2);
614                 kfree(devpriv->counter);
615         }
616         comedi_legacy_detach(dev);
617 }
618
619 static struct comedi_driver das16m1_driver = {
620         .driver_name    = "das16m1",
621         .module         = THIS_MODULE,
622         .attach         = das16m1_attach,
623         .detach         = das16m1_detach,
624 };
625 module_comedi_driver(das16m1_driver);
626
627 MODULE_AUTHOR("Comedi http://www.comedi.org");
628 MODULE_DESCRIPTION("Comedi low-level driver");
629 MODULE_LICENSE("GPL");