Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / comedi / drivers / pcl711.c
1 /*
2  * pcl711.c
3  * Comedi driver for PC-LabCard PCL-711 and AdSys ACL-8112 and compatibles
4  * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
5  *                    Janne Jalkanen <jalkanen@cs.hut.fi>
6  *                    Eric Bunn <ebu@cs.hut.fi>
7  *
8  * COMEDI - Linux Control and Measurement Device Interface
9  * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  */
21
22 /*
23  * Driver: pcl711
24  * Description: Advantech PCL-711 and 711b, ADLink ACL-8112
25  * Devices: [Advantech] PCL-711 (pcl711), PCL-711B (pcl711b),
26  *   [ADLink] ACL-8112HG (acl8112hg), ACL-8112DG (acl8112dg)
27  * Author: David A. Schleef <ds@schleef.org>
28  *         Janne Jalkanen <jalkanen@cs.hut.fi>
29  *         Eric Bunn <ebu@cs.hut.fi>
30  * Updated:
31  * Status: mostly complete
32  *
33  * Configuration Options:
34  *   [0] - I/O port base
35  *   [1] - IRQ, optional
36  */
37
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/interrupt.h>
41
42 #include "../comedidev.h"
43
44 #include "comedi_8254.h"
45
46 /*
47  * I/O port register map
48  */
49 #define PCL711_TIMER_BASE       0x00
50 #define PCL711_AI_LSB_REG       0x04
51 #define PCL711_AI_MSB_REG       0x05
52 #define PCL711_AI_MSB_DRDY      (1 << 4)
53 #define PCL711_AO_LSB_REG(x)    (0x04 + ((x) * 2))
54 #define PCL711_AO_MSB_REG(x)    (0x05 + ((x) * 2))
55 #define PCL711_DI_LSB_REG       0x06
56 #define PCL711_DI_MSB_REG       0x07
57 #define PCL711_INT_STAT_REG     0x08
58 #define PCL711_INT_STAT_CLR     (0 << 0)  /* any value will work */
59 #define PCL711_AI_GAIN_REG      0x09
60 #define PCL711_AI_GAIN(x)       (((x) & 0xf) << 0)
61 #define PCL711_MUX_REG          0x0a
62 #define PCL711_MUX_CHAN(x)      (((x) & 0xf) << 0)
63 #define PCL711_MUX_CS0          (1 << 4)
64 #define PCL711_MUX_CS1          (1 << 5)
65 #define PCL711_MUX_DIFF         (PCL711_MUX_CS0 | PCL711_MUX_CS1)
66 #define PCL711_MODE_REG         0x0b
67 #define PCL711_MODE_DEFAULT     (0 << 0)
68 #define PCL711_MODE_SOFTTRIG    (1 << 0)
69 #define PCL711_MODE_EXT         (2 << 0)
70 #define PCL711_MODE_EXT_IRQ     (3 << 0)
71 #define PCL711_MODE_PACER       (4 << 0)
72 #define PCL711_MODE_PACER_IRQ   (6 << 0)
73 #define PCL711_MODE_IRQ(x)      (((x) & 0x7) << 4)
74 #define PCL711_SOFTTRIG_REG     0x0c
75 #define PCL711_SOFTTRIG         (0 << 0)  /* any value will work */
76 #define PCL711_DO_LSB_REG       0x0d
77 #define PCL711_DO_MSB_REG       0x0e
78
79 static const struct comedi_lrange range_pcl711b_ai = {
80         5, {
81                 BIP_RANGE(5),
82                 BIP_RANGE(2.5),
83                 BIP_RANGE(1.25),
84                 BIP_RANGE(0.625),
85                 BIP_RANGE(0.3125)
86         }
87 };
88
89 static const struct comedi_lrange range_acl8112hg_ai = {
90         12, {
91                 BIP_RANGE(5),
92                 BIP_RANGE(0.5),
93                 BIP_RANGE(0.05),
94                 BIP_RANGE(0.005),
95                 UNI_RANGE(10),
96                 UNI_RANGE(1),
97                 UNI_RANGE(0.1),
98                 UNI_RANGE(0.01),
99                 BIP_RANGE(10),
100                 BIP_RANGE(1),
101                 BIP_RANGE(0.1),
102                 BIP_RANGE(0.01)
103         }
104 };
105
106 static const struct comedi_lrange range_acl8112dg_ai = {
107         9, {
108                 BIP_RANGE(5),
109                 BIP_RANGE(2.5),
110                 BIP_RANGE(1.25),
111                 BIP_RANGE(0.625),
112                 UNI_RANGE(10),
113                 UNI_RANGE(5),
114                 UNI_RANGE(2.5),
115                 UNI_RANGE(1.25),
116                 BIP_RANGE(10)
117         }
118 };
119
120 struct pcl711_board {
121         const char *name;
122         int n_aichan;
123         int n_aochan;
124         int maxirq;
125         const struct comedi_lrange *ai_range_type;
126 };
127
128 static const struct pcl711_board boardtypes[] = {
129         {
130                 .name           = "pcl711",
131                 .n_aichan       = 8,
132                 .n_aochan       = 1,
133                 .ai_range_type  = &range_bipolar5,
134         }, {
135                 .name           = "pcl711b",
136                 .n_aichan       = 8,
137                 .n_aochan       = 1,
138                 .maxirq         = 7,
139                 .ai_range_type  = &range_pcl711b_ai,
140         }, {
141                 .name           = "acl8112hg",
142                 .n_aichan       = 16,
143                 .n_aochan       = 2,
144                 .maxirq         = 15,
145                 .ai_range_type  = &range_acl8112hg_ai,
146         }, {
147                 .name           = "acl8112dg",
148                 .n_aichan       = 16,
149                 .n_aochan       = 2,
150                 .maxirq         = 15,
151                 .ai_range_type  = &range_acl8112dg_ai,
152         },
153 };
154
155 static void pcl711_ai_set_mode(struct comedi_device *dev, unsigned int mode)
156 {
157         /*
158          * The pcl711b board uses bits in the mode register to select the
159          * interrupt. The other boards supported by this driver all use
160          * jumpers on the board.
161          *
162          * Enables the interrupt when needed on the pcl711b board. These
163          * bits do nothing on the other boards.
164          */
165         if (mode == PCL711_MODE_EXT_IRQ || mode == PCL711_MODE_PACER_IRQ)
166                 mode |= PCL711_MODE_IRQ(dev->irq);
167
168         outb(mode, dev->iobase + PCL711_MODE_REG);
169 }
170
171 static unsigned int pcl711_ai_get_sample(struct comedi_device *dev,
172                                          struct comedi_subdevice *s)
173 {
174         unsigned int val;
175
176         val = inb(dev->iobase + PCL711_AI_MSB_REG) << 8;
177         val |= inb(dev->iobase + PCL711_AI_LSB_REG);
178
179         return val & s->maxdata;
180 }
181
182 static int pcl711_ai_cancel(struct comedi_device *dev,
183                             struct comedi_subdevice *s)
184 {
185         outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
186         pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
187         return 0;
188 }
189
190 static irqreturn_t pcl711_interrupt(int irq, void *d)
191 {
192         struct comedi_device *dev = d;
193         struct comedi_subdevice *s = dev->read_subdev;
194         struct comedi_cmd *cmd = &s->async->cmd;
195         unsigned int data;
196
197         if (!dev->attached) {
198                 dev_err(dev->class_dev, "spurious interrupt\n");
199                 return IRQ_HANDLED;
200         }
201
202         data = pcl711_ai_get_sample(dev, s);
203
204         outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
205
206         comedi_buf_write_samples(s, &data, 1);
207
208         if (cmd->stop_src == TRIG_COUNT &&
209             s->async->scans_done >= cmd->stop_arg)
210                 s->async->events |= COMEDI_CB_EOA;
211
212         comedi_handle_events(dev, s);
213
214         return IRQ_HANDLED;
215 }
216
217 static void pcl711_set_changain(struct comedi_device *dev,
218                                 struct comedi_subdevice *s,
219                                 unsigned int chanspec)
220 {
221         unsigned int chan = CR_CHAN(chanspec);
222         unsigned int range = CR_RANGE(chanspec);
223         unsigned int aref = CR_AREF(chanspec);
224         unsigned int mux = 0;
225
226         outb(PCL711_AI_GAIN(range), dev->iobase + PCL711_AI_GAIN_REG);
227
228         if (s->n_chan > 8) {
229                 /* Select the correct MPC508A chip */
230                 if (aref == AREF_DIFF) {
231                         chan &= 0x7;
232                         mux |= PCL711_MUX_DIFF;
233                 } else {
234                         if (chan < 8)
235                                 mux |= PCL711_MUX_CS0;
236                         else
237                                 mux |= PCL711_MUX_CS1;
238                 }
239         }
240         outb(mux | PCL711_MUX_CHAN(chan), dev->iobase + PCL711_MUX_REG);
241 }
242
243 static int pcl711_ai_eoc(struct comedi_device *dev,
244                          struct comedi_subdevice *s,
245                          struct comedi_insn *insn,
246                          unsigned long context)
247 {
248         unsigned int status;
249
250         status = inb(dev->iobase + PCL711_AI_MSB_REG);
251         if ((status & PCL711_AI_MSB_DRDY) == 0)
252                 return 0;
253         return -EBUSY;
254 }
255
256 static int pcl711_ai_insn_read(struct comedi_device *dev,
257                                struct comedi_subdevice *s,
258                                struct comedi_insn *insn,
259                                unsigned int *data)
260 {
261         int ret;
262         int i;
263
264         pcl711_set_changain(dev, s, insn->chanspec);
265
266         pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
267
268         for (i = 0; i < insn->n; i++) {
269                 outb(PCL711_SOFTTRIG, dev->iobase + PCL711_SOFTTRIG_REG);
270
271                 ret = comedi_timeout(dev, s, insn, pcl711_ai_eoc, 0);
272                 if (ret)
273                         return ret;
274
275                 data[i] = pcl711_ai_get_sample(dev, s);
276         }
277
278         return insn->n;
279 }
280
281 static int pcl711_ai_cmdtest(struct comedi_device *dev,
282                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
283 {
284         int err = 0;
285
286         /* Step 1 : check if triggers are trivially valid */
287
288         err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
289         err |= comedi_check_trigger_src(&cmd->scan_begin_src,
290                                         TRIG_TIMER | TRIG_EXT);
291         err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
292         err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
293         err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
294
295         if (err)
296                 return 1;
297
298         /* Step 2a : make sure trigger sources are unique */
299
300         err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
301         err |= comedi_check_trigger_is_unique(cmd->stop_src);
302
303         /* Step 2b : and mutually compatible */
304
305         if (err)
306                 return 2;
307
308         /* Step 3: check if arguments are trivially valid */
309
310         err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
311
312         if (cmd->scan_begin_src == TRIG_EXT) {
313                 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
314         } else {
315 #define MAX_SPEED 1000
316                 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
317                                                     MAX_SPEED);
318         }
319
320         err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
321         err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
322                                            cmd->chanlist_len);
323
324         if (cmd->stop_src == TRIG_COUNT)
325                 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
326         else    /* TRIG_NONE */
327                 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
328
329         if (err)
330                 return 3;
331
332         /* step 4 */
333
334         if (cmd->scan_begin_src == TRIG_TIMER) {
335                 unsigned int arg = cmd->scan_begin_arg;
336
337                 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
338                 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
339         }
340
341         if (err)
342                 return 4;
343
344         return 0;
345 }
346
347 static int pcl711_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
348 {
349         struct comedi_cmd *cmd = &s->async->cmd;
350
351         pcl711_set_changain(dev, s, cmd->chanlist[0]);
352
353         if (cmd->scan_begin_src == TRIG_TIMER) {
354                 comedi_8254_update_divisors(dev->pacer);
355                 comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
356                 outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
357                 pcl711_ai_set_mode(dev, PCL711_MODE_PACER_IRQ);
358         } else {
359                 pcl711_ai_set_mode(dev, PCL711_MODE_EXT_IRQ);
360         }
361
362         return 0;
363 }
364
365 static void pcl711_ao_write(struct comedi_device *dev,
366                             unsigned int chan, unsigned int val)
367 {
368         outb(val & 0xff, dev->iobase + PCL711_AO_LSB_REG(chan));
369         outb((val >> 8) & 0xff, dev->iobase + PCL711_AO_MSB_REG(chan));
370 }
371
372 static int pcl711_ao_insn_write(struct comedi_device *dev,
373                                 struct comedi_subdevice *s,
374                                 struct comedi_insn *insn,
375                                 unsigned int *data)
376 {
377         unsigned int chan = CR_CHAN(insn->chanspec);
378         unsigned int val = s->readback[chan];
379         int i;
380
381         for (i = 0; i < insn->n; i++) {
382                 val = data[i];
383                 pcl711_ao_write(dev, chan, val);
384         }
385         s->readback[chan] = val;
386
387         return insn->n;
388 }
389
390 static int pcl711_di_insn_bits(struct comedi_device *dev,
391                                struct comedi_subdevice *s,
392                                struct comedi_insn *insn,
393                                unsigned int *data)
394 {
395         unsigned int val;
396
397         val = inb(dev->iobase + PCL711_DI_LSB_REG);
398         val |= (inb(dev->iobase + PCL711_DI_MSB_REG) << 8);
399
400         data[1] = val;
401
402         return insn->n;
403 }
404
405 static int pcl711_do_insn_bits(struct comedi_device *dev,
406                                struct comedi_subdevice *s,
407                                struct comedi_insn *insn,
408                                unsigned int *data)
409 {
410         unsigned int mask;
411
412         mask = comedi_dio_update_state(s, data);
413         if (mask) {
414                 if (mask & 0x00ff)
415                         outb(s->state & 0xff, dev->iobase + PCL711_DO_LSB_REG);
416                 if (mask & 0xff00)
417                         outb((s->state >> 8), dev->iobase + PCL711_DO_MSB_REG);
418         }
419
420         data[1] = s->state;
421
422         return insn->n;
423 }
424
425 static int pcl711_attach(struct comedi_device *dev, struct comedi_devconfig *it)
426 {
427         const struct pcl711_board *board = dev->board_ptr;
428         struct comedi_subdevice *s;
429         int ret;
430
431         ret = comedi_request_region(dev, it->options[0], 0x10);
432         if (ret)
433                 return ret;
434
435         if (it->options[1] && it->options[1] <= board->maxirq) {
436                 ret = request_irq(it->options[1], pcl711_interrupt, 0,
437                                   dev->board_name, dev);
438                 if (ret == 0)
439                         dev->irq = it->options[1];
440         }
441
442         dev->pacer = comedi_8254_init(dev->iobase + PCL711_TIMER_BASE,
443                                       I8254_OSC_BASE_2MHZ, I8254_IO8, 0);
444         if (!dev->pacer)
445                 return -ENOMEM;
446
447         ret = comedi_alloc_subdevices(dev, 4);
448         if (ret)
449                 return ret;
450
451         /* Analog Input subdevice */
452         s = &dev->subdevices[0];
453         s->type         = COMEDI_SUBD_AI;
454         s->subdev_flags = SDF_READABLE | SDF_GROUND;
455         if (board->n_aichan > 8)
456                 s->subdev_flags |= SDF_DIFF;
457         s->n_chan       = board->n_aichan;
458         s->maxdata      = 0xfff;
459         s->range_table  = board->ai_range_type;
460         s->insn_read    = pcl711_ai_insn_read;
461         if (dev->irq) {
462                 dev->read_subdev = s;
463                 s->subdev_flags |= SDF_CMD_READ;
464                 s->len_chanlist = 1;
465                 s->do_cmdtest   = pcl711_ai_cmdtest;
466                 s->do_cmd       = pcl711_ai_cmd;
467                 s->cancel       = pcl711_ai_cancel;
468         }
469
470         /* Analog Output subdevice */
471         s = &dev->subdevices[1];
472         s->type         = COMEDI_SUBD_AO;
473         s->subdev_flags = SDF_WRITABLE;
474         s->n_chan       = board->n_aochan;
475         s->maxdata      = 0xfff;
476         s->range_table  = &range_bipolar5;
477         s->insn_write   = pcl711_ao_insn_write;
478
479         ret = comedi_alloc_subdev_readback(s);
480         if (ret)
481                 return ret;
482
483         /* Digital Input subdevice */
484         s = &dev->subdevices[2];
485         s->type         = COMEDI_SUBD_DI;
486         s->subdev_flags = SDF_READABLE;
487         s->n_chan       = 16;
488         s->maxdata      = 1;
489         s->range_table  = &range_digital;
490         s->insn_bits    = pcl711_di_insn_bits;
491
492         /* Digital Output subdevice */
493         s = &dev->subdevices[3];
494         s->type         = COMEDI_SUBD_DO;
495         s->subdev_flags = SDF_WRITABLE;
496         s->n_chan       = 16;
497         s->maxdata      = 1;
498         s->range_table  = &range_digital;
499         s->insn_bits    = pcl711_do_insn_bits;
500
501         /* clear DAC */
502         pcl711_ao_write(dev, 0, 0x0);
503         pcl711_ao_write(dev, 1, 0x0);
504
505         return 0;
506 }
507
508 static struct comedi_driver pcl711_driver = {
509         .driver_name    = "pcl711",
510         .module         = THIS_MODULE,
511         .attach         = pcl711_attach,
512         .detach         = comedi_legacy_detach,
513         .board_name     = &boardtypes[0].name,
514         .num_names      = ARRAY_SIZE(boardtypes),
515         .offset         = sizeof(struct pcl711_board),
516 };
517 module_comedi_driver(pcl711_driver);
518
519 MODULE_AUTHOR("Comedi http://www.comedi.org");
520 MODULE_DESCRIPTION("Comedi driver for PCL-711 compatible boards");
521 MODULE_LICENSE("GPL");