STAGING: Fix pcl818.c coding style issue: code indent should use tabs where possible
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / drivers / pcl818.c
1 /*
2  * comedi/drivers/pcl818.c
3  *
4  * Driver: pcl818
5  * Description: Advantech PCL-818 cards, PCL-718
6  * Author: Michal Dobes <dobes@tesnet.cz>
7  * Devices: [Advantech] PCL-818L (pcl818l), PCL-818H (pcl818h),
8  *   PCL-818HD (pcl818hd), PCL-818HG (pcl818hg), PCL-818 (pcl818),
9  *   PCL-718 (pcl718)
10  * Status: works
11  *
12  * All cards have 16 SE/8 DIFF ADCs, one or two DACs, 16 DI and 16 DO.
13  * Differences are only at maximal sample speed, range list and FIFO
14  * support.
15  * The driver support AI mode 0, 1, 3 other subdevices (AO, DI, DO) support
16  * only mode 0. If DMA/FIFO/INT are disabled then AI support only mode 0.
17  * PCL-818HD and PCL-818HG support 1kword FIFO. Driver support this FIFO
18  * but this code is untested.
19  * A word or two about DMA. Driver support DMA operations at two ways:
20  * 1) DMA uses two buffers and after one is filled then is generated
21  *    INT and DMA restart with second buffer. With this mode I'm unable run
22  *    more that 80Ksamples/secs without data dropouts on K6/233.
23  * 2) DMA uses one buffer and run in autoinit mode and the data are
24  *    from DMA buffer moved on the fly with 2kHz interrupts from RTC.
25  *    This mode is used if the interrupt 8 is available for allocation.
26  *    If not, then first DMA mode is used. With this I can run at
27  *    full speed one card (100ksamples/secs) or two cards with
28  *    60ksamples/secs each (more is problem on account of ISA limitations).
29  *    To use this mode you must have compiled  kernel with disabled
30  *    "Enhanced Real Time Clock Support".
31  *    Maybe you can have problems if you use xntpd or similar.
32  *    If you've data dropouts with DMA mode 2 then:
33  *     a) disable IDE DMA
34  *     b) switch text mode console to fb.
35  *
36  *  Options for PCL-818L:
37  *  [0] - IO Base
38  *  [1] - IRQ        (0=disable, 2, 3, 4, 5, 6, 7)
39  *  [2] - DMA        (0=disable, 1, 3)
40  *  [3] - 0, 10=10MHz clock for 8254
41  *            1= 1MHz clock for 8254
42  *  [4] - 0,  5=A/D input  -5V.. +5V
43  *        1, 10=A/D input -10V..+10V
44  *  [5] - 0,  5=D/A output 0-5V  (internal reference -5V)
45  *        1, 10=D/A output 0-10V (internal reference -10V)
46  *        2    =D/A output unknown (external reference)
47  *
48  *  Options for PCL-818, PCL-818H:
49  *  [0] - IO Base
50  *  [1] - IRQ        (0=disable, 2, 3, 4, 5, 6, 7)
51  *  [2] - DMA        (0=disable, 1, 3)
52  *  [3] - 0, 10=10MHz clock for 8254
53  *            1= 1MHz clock for 8254
54  *  [4] - 0,  5=D/A output 0-5V  (internal reference -5V)
55  *        1, 10=D/A output 0-10V (internal reference -10V)
56  *        2    =D/A output unknown (external reference)
57  *
58  *  Options for PCL-818HD, PCL-818HG:
59  *  [0] - IO Base
60  *  [1] - IRQ        (0=disable, 2, 3, 4, 5, 6, 7)
61  *  [2] - DMA/FIFO  (-1=use FIFO, 0=disable both FIFO and DMA,
62  *                    1=use DMA ch 1, 3=use DMA ch 3)
63  *  [3] - 0, 10=10MHz clock for 8254
64  *            1= 1MHz clock for 8254
65  *  [4] - 0,  5=D/A output 0-5V  (internal reference -5V)
66  *        1, 10=D/A output 0-10V (internal reference -10V)
67  *        2    =D/A output unknown (external reference)
68  *
69  *  Options for PCL-718:
70  *  [0] - IO Base
71  *  [1] - IRQ        (0=disable, 2, 3, 4, 5, 6, 7)
72  *  [2] - DMA        (0=disable, 1, 3)
73  *  [3] - 0, 10=10MHz clock for 8254
74  *            1= 1MHz clock for 8254
75  *  [4] -     0=A/D Range is +/-10V
76  *            1=             +/-5V
77  *            2=             +/-2.5V
78  *            3=             +/-1V
79  *            4=             +/-0.5V
80  *            5=             user defined bipolar
81  *            6=             0-10V
82  *            7=             0-5V
83  *            8=             0-2V
84  *            9=             0-1V
85  *           10=             user defined unipolar
86  *  [5] - 0,  5=D/A outputs 0-5V  (internal reference -5V)
87  *        1, 10=D/A outputs 0-10V (internal reference -10V)
88  *            2=D/A outputs unknown (external reference)
89  *  [6] - 0, 60=max  60kHz A/D sampling
90  *        1,100=max 100kHz A/D sampling (PCL-718 with Option 001 installed)
91  *
92  */
93
94 #include <linux/module.h>
95 #include <linux/gfp.h>
96 #include <linux/delay.h>
97 #include <linux/io.h>
98 #include <linux/interrupt.h>
99
100 #include "../comedidev.h"
101
102 #include "comedi_isadma.h"
103 #include "comedi_fc.h"
104 #include "8253.h"
105
106 /* boards constants */
107
108 #define boardPCL818L 0
109 #define boardPCL818H 1
110 #define boardPCL818HD 2
111 #define boardPCL818HG 3
112 #define boardPCL818 4
113 #define boardPCL718 5
114
115 /*
116  * Register I/O map
117  */
118 #define PCL818_AI_LSB_REG                       0x00
119 #define PCL818_AI_MSB_REG                       0x01
120 #define PCL818_RANGE_REG                        0x01
121 #define PCL818_MUX_REG                          0x02
122 #define PCL818_MUX_SCAN(_first, _last)          (((_last) << 4) | (_first))
123 #define PCL818_DO_DI_LSB_REG                    0x03
124 #define PCL818_AO_LSB_REG(x)                    (0x04 + ((x) * 2))
125 #define PCL818_AO_MSB_REG(x)                    (0x05 + ((x) * 2))
126 #define PCL818_STATUS_REG                       0x08
127 #define PCL818_STATUS_NEXT_CHAN_MASK            (0xf << 0)
128 #define PCL818_STATUS_INT                       (1 << 4)
129 #define PCL818_STATUS_MUX                       (1 << 5)
130 #define PCL818_STATUS_UNI                       (1 << 6)
131 #define PCL818_STATUS_EOC                       (1 << 7)
132 #define PCL818_CTRL_REG                         0x09
133 #define PCL818_CTRL_DISABLE_TRIG                (0 << 0)
134 #define PCL818_CTRL_SOFT_TRIG                   (1 << 0)
135 #define PCL818_CTRL_EXT_TRIG                    (2 << 0)
136 #define PCL818_CTRL_PACER_TRIG                  (3 << 0)
137 #define PCL818_CTRL_DMAE                        (1 << 2)
138 #define PCL818_CTRL_IRQ(x)                      ((x) << 4)
139 #define PCL818_CTRL_INTE                        (1 << 7)
140 #define PCL818_CNTENABLE_REG                    0x0a
141 #define PCL818_CNTENABLE_PACER_ENA              (0 << 0)
142 #define PCL818_CNTENABLE_PACER_TRIG0            (1 << 0)
143 #define PCL818_CNTENABLE_CNT0_EXT_CLK           (0 << 1)
144 #define PCL818_CNTENABLE_CNT0_INT_CLK           (1 << 1)
145 #define PCL818_DO_DI_MSB_REG                    0x0b
146 #define PCL818_TIMER_BASE                       0x0c
147
148 /* W: fifo enable/disable */
149 #define PCL818_FI_ENABLE 6
150 /* W: fifo interrupt clear */
151 #define PCL818_FI_INTCLR 20
152 /* W: fifo interrupt clear */
153 #define PCL818_FI_FLUSH 25
154 /* R: fifo status */
155 #define PCL818_FI_STATUS 25
156 /* R: one record from FIFO */
157 #define PCL818_FI_DATALO 23
158 #define PCL818_FI_DATAHI 24
159
160 #define MAGIC_DMA_WORD 0x5a5a
161
162 static const struct comedi_lrange range_pcl818h_ai = {
163         9, {
164                 BIP_RANGE(5),
165                 BIP_RANGE(2.5),
166                 BIP_RANGE(1.25),
167                 BIP_RANGE(0.625),
168                 UNI_RANGE(10),
169                 UNI_RANGE(5),
170                 UNI_RANGE(2.5),
171                 UNI_RANGE(1.25),
172                 BIP_RANGE(10)
173         }
174 };
175
176 static const struct comedi_lrange range_pcl818hg_ai = {
177         10, {
178                 BIP_RANGE(5),
179                 BIP_RANGE(0.5),
180                 BIP_RANGE(0.05),
181                 BIP_RANGE(0.005),
182                 UNI_RANGE(10),
183                 UNI_RANGE(1),
184                 UNI_RANGE(0.1),
185                 UNI_RANGE(0.01),
186                 BIP_RANGE(10),
187                 BIP_RANGE(1),
188                 BIP_RANGE(0.1),
189                 BIP_RANGE(0.01)
190         }
191 };
192
193 static const struct comedi_lrange range_pcl818l_l_ai = {
194         4, {
195                 BIP_RANGE(5),
196                 BIP_RANGE(2.5),
197                 BIP_RANGE(1.25),
198                 BIP_RANGE(0.625)
199         }
200 };
201
202 static const struct comedi_lrange range_pcl818l_h_ai = {
203         4, {
204                 BIP_RANGE(10),
205                 BIP_RANGE(5),
206                 BIP_RANGE(2.5),
207                 BIP_RANGE(1.25)
208         }
209 };
210
211 static const struct comedi_lrange range718_bipolar1 = {
212         1, {
213                 BIP_RANGE(1)
214         }
215 };
216
217 static const struct comedi_lrange range718_bipolar0_5 = {
218         1, {
219                 BIP_RANGE(0.5)
220         }
221 };
222
223 static const struct comedi_lrange range718_unipolar2 = {
224         1, {
225                 UNI_RANGE(2)
226         }
227 };
228
229 static const struct comedi_lrange range718_unipolar1 = {
230         1, {
231                 BIP_RANGE(1)
232         }
233 };
234
235 struct pcl818_board {
236         const char *name;
237         unsigned int ns_min;
238         int n_aochan;
239         const struct comedi_lrange *ai_range_type;
240         unsigned int has_dma:1;
241         unsigned int has_fifo:1;
242         unsigned int is_818:1;
243 };
244
245 static const struct pcl818_board boardtypes[] = {
246         {
247                 .name           = "pcl818l",
248                 .ns_min         = 25000,
249                 .n_aochan       = 1,
250                 .ai_range_type  = &range_pcl818l_l_ai,
251                 .has_dma        = 1,
252                 .is_818         = 1,
253         }, {
254                 .name           = "pcl818h",
255                 .ns_min         = 10000,
256                 .n_aochan       = 1,
257                 .ai_range_type  = &range_pcl818h_ai,
258                 .has_dma        = 1,
259                 .is_818         = 1,
260         }, {
261                 .name           = "pcl818hd",
262                 .ns_min         = 10000,
263                 .n_aochan       = 1,
264                 .ai_range_type  = &range_pcl818h_ai,
265                 .has_dma        = 1,
266                 .has_fifo       = 1,
267                 .is_818         = 1,
268         }, {
269                 .name           = "pcl818hg",
270                 .ns_min         = 10000,
271                 .n_aochan       = 1,
272                 .ai_range_type  = &range_pcl818hg_ai,
273                 .has_dma        = 1,
274                 .has_fifo       = 1,
275                 .is_818         = 1,
276         }, {
277                 .name           = "pcl818",
278                 .ns_min         = 10000,
279                 .n_aochan       = 2,
280                 .ai_range_type  = &range_pcl818h_ai,
281                 .has_dma        = 1,
282                 .is_818         = 1,
283         }, {
284                 .name           = "pcl718",
285                 .ns_min         = 16000,
286                 .n_aochan       = 2,
287                 .ai_range_type  = &range_unipolar5,
288                 .has_dma        = 1,
289         }, {
290                 .name           = "pcm3718",
291                 .ns_min         = 10000,
292                 .ai_range_type  = &range_pcl818h_ai,
293                 .has_dma        = 1,
294                 .is_818         = 1,
295         },
296 };
297
298 struct pcl818_private {
299         struct comedi_isadma *dma;
300         unsigned int ns_min;    /*  manimal allowed delay between samples (in us) for actual card */
301         int i8253_osc_base;     /*  1/frequency of on board oscilator in ns */
302         unsigned int act_chanlist[16];  /*  MUX setting for actual AI operations */
303         unsigned int act_chanlist_len;  /*  how long is actual MUX list */
304         unsigned int act_chanlist_pos;  /*  actual position in MUX list */
305         unsigned int divisor1;
306         unsigned int divisor2;
307         unsigned int usefifo:1;
308         unsigned int ai_cmd_running:1;
309         unsigned int ai_cmd_canceled:1;
310 };
311
312 static void pcl818_start_pacer(struct comedi_device *dev, bool load_counters)
313 {
314         struct pcl818_private *devpriv = dev->private;
315         unsigned long timer_base = dev->iobase + PCL818_TIMER_BASE;
316
317         i8254_set_mode(timer_base, 0, 2, I8254_MODE2 | I8254_BINARY);
318         i8254_set_mode(timer_base, 0, 1, I8254_MODE2 | I8254_BINARY);
319         udelay(1);
320
321         if (load_counters) {
322                 i8254_write(timer_base, 0, 2, devpriv->divisor2);
323                 i8254_write(timer_base, 0, 1, devpriv->divisor1);
324         }
325 }
326
327 static void pcl818_ai_setup_dma(struct comedi_device *dev,
328                                 struct comedi_subdevice *s,
329                                 unsigned int unread_samples)
330 {
331         struct pcl818_private *devpriv = dev->private;
332         struct comedi_isadma *dma = devpriv->dma;
333         struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
334         unsigned int max_samples = comedi_bytes_to_samples(s, desc->maxsize);
335         unsigned int nsamples;
336
337         comedi_isadma_disable(dma->chan);
338
339         /*
340          * Determine dma size based on the buffer maxsize plus the number of
341          * unread samples and the number of samples remaining in the command.
342          */
343         nsamples = comedi_nsamples_left(s, max_samples + unread_samples);
344         if (nsamples > unread_samples) {
345                 nsamples -= unread_samples;
346                 desc->size = comedi_samples_to_bytes(s, nsamples);
347                 comedi_isadma_program(desc);
348         }
349 }
350
351 static void pcl818_ai_set_chan_range(struct comedi_device *dev,
352                                      unsigned int chan,
353                                      unsigned int range)
354 {
355         outb(chan, dev->iobase + PCL818_MUX_REG);
356         outb(range, dev->iobase + PCL818_RANGE_REG);
357 }
358
359 static void pcl818_ai_set_chan_scan(struct comedi_device *dev,
360                                     unsigned int first_chan,
361                                     unsigned int last_chan)
362 {
363         outb(PCL818_MUX_SCAN(first_chan, last_chan),
364              dev->iobase + PCL818_MUX_REG);
365 }
366
367 static void pcl818_ai_setup_chanlist(struct comedi_device *dev,
368                                      unsigned int *chanlist,
369                                      unsigned int seglen)
370 {
371         struct pcl818_private *devpriv = dev->private;
372         unsigned int first_chan = CR_CHAN(chanlist[0]);
373         unsigned int last_chan;
374         unsigned int range;
375         int i;
376
377         devpriv->act_chanlist_len = seglen;
378         devpriv->act_chanlist_pos = 0;
379
380         /* store range list to card */
381         for (i = 0; i < seglen; i++) {
382                 last_chan = CR_CHAN(chanlist[i]);
383                 range = CR_RANGE(chanlist[i]);
384
385                 devpriv->act_chanlist[i] = last_chan;
386
387                 pcl818_ai_set_chan_range(dev, last_chan, range);
388         }
389
390         udelay(1);
391
392         pcl818_ai_set_chan_scan(dev, first_chan, last_chan);
393 }
394
395 static void pcl818_ai_clear_eoc(struct comedi_device *dev)
396 {
397         /* writing any value clears the interrupt request */
398         outb(0, dev->iobase + PCL818_STATUS_REG);
399 }
400
401 static void pcl818_ai_soft_trig(struct comedi_device *dev)
402 {
403         /* writing any value triggers a software conversion */
404         outb(0, dev->iobase + PCL818_AI_LSB_REG);
405 }
406
407 static unsigned int pcl818_ai_get_fifo_sample(struct comedi_device *dev,
408                                               struct comedi_subdevice *s,
409                                               unsigned int *chan)
410 {
411         unsigned int val;
412
413         val = inb(dev->iobase + PCL818_FI_DATALO);
414         val |= (inb(dev->iobase + PCL818_FI_DATAHI) << 8);
415
416         if (chan)
417                 *chan = val & 0xf;
418
419         return (val >> 4) & s->maxdata;
420 }
421
422 static unsigned int pcl818_ai_get_sample(struct comedi_device *dev,
423                                          struct comedi_subdevice *s,
424                                          unsigned int *chan)
425 {
426         unsigned int val;
427
428         val = inb(dev->iobase + PCL818_AI_MSB_REG) << 8;
429         val |= inb(dev->iobase + PCL818_AI_LSB_REG);
430
431         if (chan)
432                 *chan = val & 0xf;
433
434         return (val >> 4) & s->maxdata;
435 }
436
437 static int pcl818_ai_eoc(struct comedi_device *dev,
438                          struct comedi_subdevice *s,
439                          struct comedi_insn *insn,
440                          unsigned long context)
441 {
442         unsigned int status;
443
444         status = inb(dev->iobase + PCL818_STATUS_REG);
445         if (status & PCL818_STATUS_INT)
446                 return 0;
447         return -EBUSY;
448 }
449
450 static bool pcl818_ai_write_sample(struct comedi_device *dev,
451                                    struct comedi_subdevice *s,
452                                    unsigned int chan, unsigned int val)
453 {
454         struct pcl818_private *devpriv = dev->private;
455         struct comedi_cmd *cmd = &s->async->cmd;
456         unsigned int expected_chan;
457
458         expected_chan = devpriv->act_chanlist[devpriv->act_chanlist_pos];
459         if (chan != expected_chan) {
460                 dev_dbg(dev->class_dev,
461                         "A/D mode1/3 %s - channel dropout %d!=%d !\n",
462                         (devpriv->dma) ? "DMA" :
463                         (devpriv->usefifo) ? "FIFO" : "IRQ",
464                         chan, expected_chan);
465                 s->async->events |= COMEDI_CB_ERROR;
466                 return false;
467         }
468
469         comedi_buf_write_samples(s, &val, 1);
470
471         devpriv->act_chanlist_pos++;
472         if (devpriv->act_chanlist_pos >= devpriv->act_chanlist_len)
473                 devpriv->act_chanlist_pos = 0;
474
475         if (cmd->stop_src == TRIG_COUNT &&
476             s->async->scans_done >= cmd->stop_arg) {
477                 s->async->events |= COMEDI_CB_EOA;
478                 return false;
479         }
480
481         return true;
482 }
483
484 static void pcl818_handle_eoc(struct comedi_device *dev,
485                               struct comedi_subdevice *s)
486 {
487         unsigned int chan;
488         unsigned int val;
489
490         if (pcl818_ai_eoc(dev, s, NULL, 0)) {
491                 dev_err(dev->class_dev, "A/D mode1/3 IRQ without DRDY!\n");
492                 s->async->events |= COMEDI_CB_ERROR;
493                 return;
494         }
495
496         val = pcl818_ai_get_sample(dev, s, &chan);
497         pcl818_ai_write_sample(dev, s, chan, val);
498 }
499
500 static void pcl818_handle_dma(struct comedi_device *dev,
501                               struct comedi_subdevice *s)
502 {
503         struct pcl818_private *devpriv = dev->private;
504         struct comedi_isadma *dma = devpriv->dma;
505         struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
506         unsigned short *ptr = desc->virt_addr;
507         unsigned int nsamples = comedi_bytes_to_samples(s, desc->size);
508         unsigned int chan;
509         unsigned int val;
510         int i;
511
512         /* restart dma with the next buffer */
513         dma->cur_dma = 1 - dma->cur_dma;
514         pcl818_ai_setup_dma(dev, s, nsamples);
515
516         for (i = 0; i < nsamples; i++) {
517                 val = ptr[i];
518                 chan = val & 0xf;
519                 val = (val >> 4) & s->maxdata;
520                 if (!pcl818_ai_write_sample(dev, s, chan, val))
521                         break;
522         }
523 }
524
525 static void pcl818_handle_fifo(struct comedi_device *dev,
526                                struct comedi_subdevice *s)
527 {
528         unsigned int status;
529         unsigned int chan;
530         unsigned int val;
531         int i, len;
532
533         status = inb(dev->iobase + PCL818_FI_STATUS);
534
535         if (status & 4) {
536                 dev_err(dev->class_dev, "A/D mode1/3 FIFO overflow!\n");
537                 s->async->events |= COMEDI_CB_ERROR;
538                 return;
539         }
540
541         if (status & 1) {
542                 dev_err(dev->class_dev,
543                         "A/D mode1/3 FIFO interrupt without data!\n");
544                 s->async->events |= COMEDI_CB_ERROR;
545                 return;
546         }
547
548         if (status & 2)
549                 len = 512;
550         else
551                 len = 0;
552
553         for (i = 0; i < len; i++) {
554                 val = pcl818_ai_get_fifo_sample(dev, s, &chan);
555                 if (!pcl818_ai_write_sample(dev, s, chan, val))
556                         break;
557         }
558 }
559
560 static irqreturn_t pcl818_interrupt(int irq, void *d)
561 {
562         struct comedi_device *dev = d;
563         struct pcl818_private *devpriv = dev->private;
564         struct comedi_subdevice *s = dev->read_subdev;
565         struct comedi_cmd *cmd = &s->async->cmd;
566
567         if (!dev->attached || !devpriv->ai_cmd_running) {
568                 pcl818_ai_clear_eoc(dev);
569                 return IRQ_HANDLED;
570         }
571
572         if (devpriv->ai_cmd_canceled) {
573                 /*
574                  * The cleanup from ai_cancel() has been delayed
575                  * until now because the card doesn't seem to like
576                  * being reprogrammed while a DMA transfer is in
577                  * progress.
578                  */
579                 s->async->scans_done = cmd->stop_arg;
580                 s->cancel(dev, s);
581                 return IRQ_HANDLED;
582         }
583
584         if (devpriv->dma)
585                 pcl818_handle_dma(dev, s);
586         else if (devpriv->usefifo)
587                 pcl818_handle_fifo(dev, s);
588         else
589                 pcl818_handle_eoc(dev, s);
590
591         pcl818_ai_clear_eoc(dev);
592
593         comedi_handle_events(dev, s);
594         return IRQ_HANDLED;
595 }
596
597 static int check_channel_list(struct comedi_device *dev,
598                               struct comedi_subdevice *s,
599                               unsigned int *chanlist, unsigned int n_chan)
600 {
601         unsigned int chansegment[16];
602         unsigned int i, nowmustbechan, seglen, segpos;
603
604         /* correct channel and range number check itself comedi/range.c */
605         if (n_chan < 1) {
606                 dev_err(dev->class_dev, "range/channel list is empty!\n");
607                 return 0;
608         }
609
610         if (n_chan > 1) {
611                 /*  first channel is every time ok */
612                 chansegment[0] = chanlist[0];
613                 /*  build part of chanlist */
614                 for (i = 1, seglen = 1; i < n_chan; i++, seglen++) {
615                         /* we detect loop, this must by finish */
616
617                         if (chanlist[0] == chanlist[i])
618                                 break;
619                         nowmustbechan =
620                             (CR_CHAN(chansegment[i - 1]) + 1) % s->n_chan;
621                         if (nowmustbechan != CR_CHAN(chanlist[i])) {    /*  channel list isn't continuous :-( */
622                                 dev_dbg(dev->class_dev,
623                                         "channel list must be continuous! chanlist[%i]=%d but must be %d or %d!\n",
624                                         i, CR_CHAN(chanlist[i]), nowmustbechan,
625                                         CR_CHAN(chanlist[0]));
626                                 return 0;
627                         }
628                         /*  well, this is next correct channel in list */
629                         chansegment[i] = chanlist[i];
630                 }
631
632                 /*  check whole chanlist */
633                 for (i = 0, segpos = 0; i < n_chan; i++) {
634                         if (chanlist[i] != chansegment[i % seglen]) {
635                                 dev_dbg(dev->class_dev,
636                                         "bad channel or range number! chanlist[%i]=%d,%d,%d and not %d,%d,%d!\n",
637                                         i, CR_CHAN(chansegment[i]),
638                                         CR_RANGE(chansegment[i]),
639                                         CR_AREF(chansegment[i]),
640                                         CR_CHAN(chanlist[i % seglen]),
641                                         CR_RANGE(chanlist[i % seglen]),
642                                         CR_AREF(chansegment[i % seglen]));
643                                 return 0;       /*  chan/gain list is strange */
644                         }
645                 }
646         } else {
647                 seglen = 1;
648         }
649         return seglen;
650 }
651
652 static int check_single_ended(unsigned int port)
653 {
654         if (inb(port + PCL818_STATUS_REG) & PCL818_STATUS_MUX)
655                 return 1;
656         return 0;
657 }
658
659 static int ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
660                       struct comedi_cmd *cmd)
661 {
662         const struct pcl818_board *board = dev->board_ptr;
663         struct pcl818_private *devpriv = dev->private;
664         int err = 0;
665         unsigned int arg;
666
667         /* Step 1 : check if triggers are trivially valid */
668
669         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
670         err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
671         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
672         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
673         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
674
675         if (err)
676                 return 1;
677
678         /* Step 2a : make sure trigger sources are unique */
679
680         err |= cfc_check_trigger_is_unique(cmd->convert_src);
681         err |= cfc_check_trigger_is_unique(cmd->stop_src);
682
683         /* Step 2b : and mutually compatible */
684
685         if (err)
686                 return 2;
687
688         /* Step 3: check if arguments are trivially valid */
689
690         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
691         err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
692
693         if (cmd->convert_src == TRIG_TIMER)
694                 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
695                                                  board->ns_min);
696         else    /* TRIG_EXT */
697                 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
698
699         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
700
701         if (cmd->stop_src == TRIG_COUNT)
702                 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
703         else    /* TRIG_NONE */
704                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
705
706         if (err)
707                 return 3;
708
709         /* step 4: fix up any arguments */
710
711         if (cmd->convert_src == TRIG_TIMER) {
712                 arg = cmd->convert_arg;
713                 i8253_cascade_ns_to_timer(devpriv->i8253_osc_base,
714                                           &devpriv->divisor1,
715                                           &devpriv->divisor2,
716                                           &arg, cmd->flags);
717                 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, arg);
718         }
719
720         if (err)
721                 return 4;
722
723         /* step 5: complain about special chanlist considerations */
724
725         if (cmd->chanlist) {
726                 if (!check_channel_list(dev, s, cmd->chanlist,
727                                         cmd->chanlist_len))
728                         return 5;       /*  incorrect channels list */
729         }
730
731         return 0;
732 }
733
734 static int pcl818_ai_cmd(struct comedi_device *dev,
735                          struct comedi_subdevice *s)
736 {
737         struct pcl818_private *devpriv = dev->private;
738         struct comedi_isadma *dma = devpriv->dma;
739         struct comedi_cmd *cmd = &s->async->cmd;
740         unsigned int ctrl = 0;
741         unsigned int seglen;
742
743         if (devpriv->ai_cmd_running)
744                 return -EBUSY;
745
746         pcl818_start_pacer(dev, false);
747
748         seglen = check_channel_list(dev, s, cmd->chanlist, cmd->chanlist_len);
749         if (seglen < 1)
750                 return -EINVAL;
751         pcl818_ai_setup_chanlist(dev, cmd->chanlist, seglen);
752
753         devpriv->ai_cmd_running = 1;
754         devpriv->ai_cmd_canceled = 0;
755         devpriv->act_chanlist_pos = 0;
756
757         if (cmd->convert_src == TRIG_TIMER)
758                 ctrl |= PCL818_CTRL_PACER_TRIG;
759         else
760                 ctrl |= PCL818_CTRL_EXT_TRIG;
761
762         outb(PCL818_CNTENABLE_PACER_ENA, dev->iobase + PCL818_CNTENABLE_REG);
763
764         if (dma) {
765                 /* setup and enable dma for the first buffer */
766                 dma->cur_dma = 0;
767                 pcl818_ai_setup_dma(dev, s, 0);
768
769                 ctrl |= PCL818_CTRL_INTE | PCL818_CTRL_IRQ(dev->irq) |
770                         PCL818_CTRL_DMAE;
771         } else if (devpriv->usefifo) {
772                 /* enable FIFO */
773                 outb(1, dev->iobase + PCL818_FI_ENABLE);
774         } else {
775                 ctrl |= PCL818_CTRL_INTE | PCL818_CTRL_IRQ(dev->irq);
776         }
777         outb(ctrl, dev->iobase + PCL818_CTRL_REG);
778
779         if (cmd->convert_src == TRIG_TIMER)
780                 pcl818_start_pacer(dev, true);
781
782         return 0;
783 }
784
785 static int pcl818_ai_cancel(struct comedi_device *dev,
786                             struct comedi_subdevice *s)
787 {
788         struct pcl818_private *devpriv = dev->private;
789         struct comedi_isadma *dma = devpriv->dma;
790         struct comedi_cmd *cmd = &s->async->cmd;
791
792         if (!devpriv->ai_cmd_running)
793                 return 0;
794
795         if (dma) {
796                 if (cmd->stop_src == TRIG_NONE ||
797                     (cmd->stop_src == TRIG_COUNT &&
798                      s->async->scans_done < cmd->stop_arg)) {
799                         if (!devpriv->ai_cmd_canceled) {
800                                 /*
801                                 * Wait for running dma transfer to end,
802                                 * do cleanup in interrupt.
803                                 */
804                                 devpriv->ai_cmd_canceled = 1;
805                                 return 0;
806                         }
807                 }
808                 comedi_isadma_disable(dma->chan);
809         }
810
811         outb(PCL818_CTRL_DISABLE_TRIG, dev->iobase + PCL818_CTRL_REG);
812         pcl818_start_pacer(dev, false);
813         pcl818_ai_clear_eoc(dev);
814
815         if (devpriv->usefifo) { /*  FIFO shutdown */
816                 outb(0, dev->iobase + PCL818_FI_INTCLR);
817                 outb(0, dev->iobase + PCL818_FI_FLUSH);
818                 outb(0, dev->iobase + PCL818_FI_ENABLE);
819         }
820         devpriv->ai_cmd_running = 0;
821         devpriv->ai_cmd_canceled = 0;
822
823         return 0;
824 }
825
826 static int pcl818_ai_insn_read(struct comedi_device *dev,
827                                struct comedi_subdevice *s,
828                                struct comedi_insn *insn,
829                                unsigned int *data)
830 {
831         unsigned int chan = CR_CHAN(insn->chanspec);
832         unsigned int range = CR_RANGE(insn->chanspec);
833         int ret = 0;
834         int i;
835
836         outb(PCL818_CTRL_SOFT_TRIG, dev->iobase + PCL818_CTRL_REG);
837
838         pcl818_ai_set_chan_range(dev, chan, range);
839         pcl818_ai_set_chan_scan(dev, chan, chan);
840
841         for (i = 0; i < insn->n; i++) {
842                 pcl818_ai_clear_eoc(dev);
843                 pcl818_ai_soft_trig(dev);
844
845                 ret = comedi_timeout(dev, s, insn, pcl818_ai_eoc, 0);
846                 if (ret)
847                         break;
848
849                 data[i] = pcl818_ai_get_sample(dev, s, NULL);
850         }
851         pcl818_ai_clear_eoc(dev);
852
853         return ret ? ret : insn->n;
854 }
855
856 static int pcl818_ao_insn_write(struct comedi_device *dev,
857                                 struct comedi_subdevice *s,
858                                 struct comedi_insn *insn,
859                                 unsigned int *data)
860 {
861         unsigned int chan = CR_CHAN(insn->chanspec);
862         unsigned int val = s->readback[chan];
863         int i;
864
865         for (i = 0; i < insn->n; i++) {
866                 val = data[i];
867                 outb((val & 0x000f) << 4,
868                      dev->iobase + PCL818_AO_LSB_REG(chan));
869                 outb((val & 0x0ff0) >> 4,
870                      dev->iobase + PCL818_AO_MSB_REG(chan));
871         }
872         s->readback[chan] = val;
873
874         return insn->n;
875 }
876
877 static int pcl818_di_insn_bits(struct comedi_device *dev,
878                                struct comedi_subdevice *s,
879                                struct comedi_insn *insn,
880                                unsigned int *data)
881 {
882         data[1] = inb(dev->iobase + PCL818_DO_DI_LSB_REG) |
883                   (inb(dev->iobase + PCL818_DO_DI_MSB_REG) << 8);
884
885         return insn->n;
886 }
887
888 static int pcl818_do_insn_bits(struct comedi_device *dev,
889                                struct comedi_subdevice *s,
890                                struct comedi_insn *insn,
891                                unsigned int *data)
892 {
893         if (comedi_dio_update_state(s, data)) {
894                 outb(s->state & 0xff, dev->iobase + PCL818_DO_DI_LSB_REG);
895                 outb((s->state >> 8), dev->iobase + PCL818_DO_DI_MSB_REG);
896         }
897
898         data[1] = s->state;
899
900         return insn->n;
901 }
902
903 static void pcl818_reset(struct comedi_device *dev)
904 {
905         const struct pcl818_board *board = dev->board_ptr;
906         unsigned long timer_base = dev->iobase + PCL818_TIMER_BASE;
907         unsigned int chan;
908
909         /* flush and disable the FIFO */
910         if (board->has_fifo) {
911                 outb(0, dev->iobase + PCL818_FI_INTCLR);
912                 outb(0, dev->iobase + PCL818_FI_FLUSH);
913                 outb(0, dev->iobase + PCL818_FI_ENABLE);
914         }
915
916         /* disable analog input trigger */
917         outb(PCL818_CTRL_DISABLE_TRIG, dev->iobase + PCL818_CTRL_REG);
918         pcl818_ai_clear_eoc(dev);
919
920         pcl818_ai_set_chan_range(dev, 0, 0);
921
922         /* stop pacer */
923         outb(PCL818_CNTENABLE_PACER_ENA, dev->iobase + PCL818_CNTENABLE_REG);
924         i8254_set_mode(timer_base, 0, 2, I8254_MODE0 | I8254_BINARY);
925         i8254_set_mode(timer_base, 0, 1, I8254_MODE0 | I8254_BINARY);
926         i8254_set_mode(timer_base, 0, 0, I8254_MODE0 | I8254_BINARY);
927
928         /* set analog output channels to 0V */
929         for (chan = 0; chan < board->n_aochan; chan++) {
930                 outb(0, dev->iobase + PCL818_AO_LSB_REG(chan));
931                 outb(0, dev->iobase + PCL818_AO_MSB_REG(chan));
932         }
933
934         /* set all digital outputs low */
935         outb(0, dev->iobase + PCL818_DO_DI_MSB_REG);
936         outb(0, dev->iobase + PCL818_DO_DI_LSB_REG);
937 }
938
939 static void pcl818_set_ai_range_table(struct comedi_device *dev,
940                                       struct comedi_subdevice *s,
941                                       struct comedi_devconfig *it)
942 {
943         const struct pcl818_board *board = dev->board_ptr;
944
945         /* default to the range table from the boardinfo */
946         s->range_table = board->ai_range_type;
947
948         /* now check the user config option based on the boardtype */
949         if (board->is_818) {
950                 if (it->options[4] == 1 || it->options[4] == 10) {
951                         /* secondary range list jumper selectable */
952                         s->range_table = &range_pcl818l_h_ai;
953                 }
954         } else {
955                 switch (it->options[4]) {
956                 case 0:
957                         s->range_table = &range_bipolar10;
958                         break;
959                 case 1:
960                         s->range_table = &range_bipolar5;
961                         break;
962                 case 2:
963                         s->range_table = &range_bipolar2_5;
964                         break;
965                 case 3:
966                         s->range_table = &range718_bipolar1;
967                         break;
968                 case 4:
969                         s->range_table = &range718_bipolar0_5;
970                         break;
971                 case 6:
972                         s->range_table = &range_unipolar10;
973                         break;
974                 case 7:
975                         s->range_table = &range_unipolar5;
976                         break;
977                 case 8:
978                         s->range_table = &range718_unipolar2;
979                         break;
980                 case 9:
981                         s->range_table = &range718_unipolar1;
982                         break;
983                 default:
984                         s->range_table = &range_unknown;
985                         break;
986                 }
987         }
988 }
989
990 static void pcl818_alloc_dma(struct comedi_device *dev, unsigned int dma_chan)
991 {
992         struct pcl818_private *devpriv = dev->private;
993
994         /* only DMA channels 3 and 1 are valid */
995         if (!(dma_chan == 3 || dma_chan == 1))
996                 return;
997
998         /* DMA uses two 16K buffers */
999         devpriv->dma = comedi_isadma_alloc(dev, 2, dma_chan, dma_chan,
1000                                            PAGE_SIZE * 4, COMEDI_ISADMA_READ);
1001 }
1002
1003 static void pcl818_free_dma(struct comedi_device *dev)
1004 {
1005         struct pcl818_private *devpriv = dev->private;
1006
1007         if (devpriv)
1008                 comedi_isadma_free(devpriv->dma);
1009 }
1010
1011 static int pcl818_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1012 {
1013         const struct pcl818_board *board = dev->board_ptr;
1014         struct pcl818_private *devpriv;
1015         struct comedi_subdevice *s;
1016         int ret;
1017
1018         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1019         if (!devpriv)
1020                 return -ENOMEM;
1021
1022         ret = comedi_request_region(dev, it->options[0],
1023                                     board->has_fifo ? 0x20 : 0x10);
1024         if (ret)
1025                 return ret;
1026
1027         /* we can use IRQ 2-7 for async command support */
1028         if (it->options[1] >= 2 && it->options[1] <= 7) {
1029                 ret = request_irq(it->options[1], pcl818_interrupt, 0,
1030                                   dev->board_name, dev);
1031                 if (ret == 0)
1032                         dev->irq = it->options[1];
1033         }
1034
1035         /* should we use the FIFO? */
1036         if (dev->irq && board->has_fifo && it->options[2] == -1)
1037                 devpriv->usefifo = 1;
1038
1039         /* we need an IRQ to do DMA on channel 3 or 1 */
1040         if (dev->irq && board->has_dma)
1041                 pcl818_alloc_dma(dev, it->options[2]);
1042
1043         ret = comedi_alloc_subdevices(dev, 4);
1044         if (ret)
1045                 return ret;
1046
1047         s = &dev->subdevices[0];
1048         s->type         = COMEDI_SUBD_AI;
1049         s->subdev_flags = SDF_READABLE;
1050         if (check_single_ended(dev->iobase)) {
1051                 s->n_chan       = 16;
1052                 s->subdev_flags |= SDF_COMMON | SDF_GROUND;
1053         } else {
1054                 s->n_chan       = 8;
1055                 s->subdev_flags |= SDF_DIFF;
1056         }
1057         s->maxdata      = 0x0fff;
1058
1059         pcl818_set_ai_range_table(dev, s, it);
1060
1061         s->insn_read    = pcl818_ai_insn_read;
1062         if (dev->irq) {
1063                 dev->read_subdev = s;
1064                 s->subdev_flags |= SDF_CMD_READ;
1065                 s->len_chanlist = s->n_chan;
1066                 s->do_cmdtest   = ai_cmdtest;
1067                 s->do_cmd       = pcl818_ai_cmd;
1068                 s->cancel       = pcl818_ai_cancel;
1069         }
1070
1071         /* Analog Output subdevice */
1072         s = &dev->subdevices[1];
1073         if (board->n_aochan) {
1074                 s->type         = COMEDI_SUBD_AO;
1075                 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
1076                 s->n_chan       = board->n_aochan;
1077                 s->maxdata      = 0x0fff;
1078                 s->range_table  = &range_unipolar5;
1079                 if (board->is_818) {
1080                         if ((it->options[4] == 1) || (it->options[4] == 10))
1081                                 s->range_table = &range_unipolar10;
1082                         if (it->options[4] == 2)
1083                                 s->range_table = &range_unknown;
1084                 } else {
1085                         if ((it->options[5] == 1) || (it->options[5] == 10))
1086                                 s->range_table = &range_unipolar10;
1087                         if (it->options[5] == 2)
1088                                 s->range_table = &range_unknown;
1089                 }
1090                 s->insn_write   = pcl818_ao_insn_write;
1091
1092                 ret = comedi_alloc_subdev_readback(s);
1093                 if (ret)
1094                         return ret;
1095         } else {
1096                 s->type         = COMEDI_SUBD_UNUSED;
1097         }
1098
1099         /* Digital Input subdevice */
1100         s = &dev->subdevices[2];
1101         s->type         = COMEDI_SUBD_DI;
1102         s->subdev_flags = SDF_READABLE;
1103         s->n_chan       = 16;
1104         s->maxdata      = 1;
1105         s->range_table  = &range_digital;
1106         s->insn_bits    = pcl818_di_insn_bits;
1107
1108         /* Digital Output subdevice */
1109         s = &dev->subdevices[3];
1110         s->type         = COMEDI_SUBD_DO;
1111         s->subdev_flags = SDF_WRITABLE;
1112         s->n_chan       = 16;
1113         s->maxdata      = 1;
1114         s->range_table  = &range_digital;
1115         s->insn_bits    = pcl818_do_insn_bits;
1116
1117         /* select 1/10MHz oscilator */
1118         if ((it->options[3] == 0) || (it->options[3] == 10))
1119                 devpriv->i8253_osc_base = I8254_OSC_BASE_10MHZ;
1120         else
1121                 devpriv->i8253_osc_base = I8254_OSC_BASE_1MHZ;
1122
1123         /* max sampling speed */
1124         devpriv->ns_min = board->ns_min;
1125
1126         if (!board->is_818) {
1127                 if ((it->options[6] == 1) || (it->options[6] == 100))
1128                         devpriv->ns_min = 10000;        /* extended PCL718 to 100kHz DAC */
1129         }
1130
1131         pcl818_reset(dev);
1132
1133         return 0;
1134 }
1135
1136 static void pcl818_detach(struct comedi_device *dev)
1137 {
1138         struct pcl818_private *devpriv = dev->private;
1139
1140         if (devpriv) {
1141                 pcl818_ai_cancel(dev, dev->read_subdev);
1142                 pcl818_reset(dev);
1143         }
1144         pcl818_free_dma(dev);
1145         comedi_legacy_detach(dev);
1146 }
1147
1148 static struct comedi_driver pcl818_driver = {
1149         .driver_name    = "pcl818",
1150         .module         = THIS_MODULE,
1151         .attach         = pcl818_attach,
1152         .detach         = pcl818_detach,
1153         .board_name     = &boardtypes[0].name,
1154         .num_names      = ARRAY_SIZE(boardtypes),
1155         .offset         = sizeof(struct pcl818_board),
1156 };
1157 module_comedi_driver(pcl818_driver);
1158
1159 MODULE_AUTHOR("Comedi http://www.comedi.org");
1160 MODULE_DESCRIPTION("Comedi low-level driver");
1161 MODULE_LICENSE("GPL");