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