staging: comedi: das08: rename and rewrite DAS08_INTE
[cascardo/linux.git] / drivers / staging / comedi / drivers / das08.c
1 /*
2  *  comedi/drivers/das08.c
3  *  comedi module for common DAS08 support (used by ISA/PCI/PCMCIA drivers)
4  *
5  *  COMEDI - Linux Control and Measurement Device Interface
6  *  Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7  *  Copyright (C) 2001,2002,2003 Frank Mori Hess <fmhess@users.sourceforge.net>
8  *  Copyright (C) 2004 Salvador E. Tropea <set@users.sf.net> <set@ieee.org>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  */
20
21 #include <linux/module.h>
22
23 #include "../comedidev.h"
24
25 #include "8255.h"
26 #include "comedi_8254.h"
27 #include "das08.h"
28
29 /*
30     cio-das08.pdf
31
32   "isa-das08"
33
34   0     a/d bits 0-3            start 8 bit
35   1     a/d bits 4-11           start 12 bit
36   2     eoc, ip1-3, irq, mux    op1-4, inte, mux
37   3     unused                  unused
38   4567  8254
39   89ab  8255
40
41   requires hard-wiring for async ai
42
43 */
44
45 /*
46  * Data format of DAS08_AI_LSB_REG and DAS08_AI_MSB_REG depends on
47  * 'ai_encoding' member of board structure:
48  *
49  * das08_encode12     : DATA[11..4] = MSB[7..0], DATA[3..0] = LSB[7..4].
50  * das08_pcm_encode12 : DATA[11..8] = MSB[3..0], DATA[7..9] = LSB[7..0].
51  * das08_encode16     : SIGN = MSB[7], MAGNITUDE[14..8] = MSB[6..0],
52  *                      MAGNITUDE[7..0] = LSB[7..0].
53  *                      SIGN==0 for negative input, SIGN==1 for positive input.
54  *                      Note: when read a second time after conversion
55  *                            complete, MSB[7] is an "over-range" bit.
56  */
57 #define DAS08_AI_LSB_REG        0x00    /* (R) AI least significant bits */
58 #define DAS08_AI_MSB_REG        0x01    /* (R) AI most significant bits */
59 #define DAS08_AI_TRIG_REG       0x01    /* (W) AI software trigger */
60 #define DAS08_STATUS_REG        0x02    /* (R) status */
61 #define DAS08_STATUS_AI_BUSY    BIT(7)  /* AI conversion in progress */
62 /*
63  * The IRQ status bit is set to 1 by a rising edge on the external interrupt
64  * input (which may be jumpered to the pacer output).  It is cleared by
65  * setting the INTE control bit to 0.  Not present on "JR" boards.
66  */
67 #define DAS08_STATUS_IRQ        BIT(3)  /* latched interrupt input */
68 /* digital inputs (not "JR" boards) */
69 #define DAS08_STATUS_DI(x)      (((x) & 0x70) >> 4)
70 #define DAS08_CONTROL_REG       0x02    /* (W) control */
71 /*
72  * Note: The AI multiplexor channel can also be read from status register using
73  * the same mask.
74  */
75 #define DAS08_CONTROL_MUX_MASK  0x7     /* multiplexor channel mask */
76 #define DAS08_CONTROL_MUX(x)    ((x) & DAS08_CONTROL_MUX_MASK) /* mux channel */
77 #define DAS08_CONTROL_INTE      BIT(3)  /* interrupt enable (not "JR" boards) */
78 #define   DAS08_DO_MASK         0xf0
79 #define   DAS08_OP(x)           (((x) << 4) & DAS08_DO_MASK)
80
81 /*
82     cio-das08jr.pdf
83
84   "das08/jr-ao"
85
86   0     a/d bits 0-3            unused
87   1     a/d bits 4-11           start 12 bit
88   2     eoc, mux                mux
89   3     di                      do
90   4     unused                  ao0_lsb
91   5     unused                  ao0_msb
92   6     unused                  ao1_lsb
93   7     unused                  ao1_msb
94
95 */
96
97 #define DAS08JR_DIO             3
98 #define DAS08JR_AO_LSB(x)       ((x) ? 6 : 4)
99 #define DAS08JR_AO_MSB(x)       ((x) ? 7 : 5)
100
101 /*
102     cio-das08_aox.pdf
103
104   "das08-aoh"
105   "das08-aol"
106   "das08-aom"
107
108   0     a/d bits 0-3            start 8 bit
109   1     a/d bits 4-11           start 12 bit
110   2     eoc, ip1-3, irq, mux    op1-4, inte, mux
111   3     mux, gain status        gain control
112   4567  8254
113   8     unused                  ao0_lsb
114   9     unused                  ao0_msb
115   a     unused                  ao1_lsb
116   b     unused                  ao1_msb
117   89ab
118   cdef  8255
119 */
120
121 #define DAS08AO_GAIN_CONTROL    3
122 #define DAS08AO_GAIN_STATUS     3
123
124 #define DAS08AO_AO_LSB(x)       ((x) ? 0xa : 8)
125 #define DAS08AO_AO_MSB(x)       ((x) ? 0xb : 9)
126 #define DAS08AO_AO_UPDATE       8
127
128 /* gainlist same as _pgx_ below */
129
130 static const struct comedi_lrange range_das08_pgl = {
131         9, {
132                 BIP_RANGE(10),
133                 BIP_RANGE(5),
134                 BIP_RANGE(2.5),
135                 BIP_RANGE(1.25),
136                 BIP_RANGE(0.625),
137                 UNI_RANGE(10),
138                 UNI_RANGE(5),
139                 UNI_RANGE(2.5),
140                 UNI_RANGE(1.25)
141         }
142 };
143
144 static const struct comedi_lrange range_das08_pgh = {
145         12, {
146                 BIP_RANGE(10),
147                 BIP_RANGE(5),
148                 BIP_RANGE(1),
149                 BIP_RANGE(0.5),
150                 BIP_RANGE(0.1),
151                 BIP_RANGE(0.05),
152                 BIP_RANGE(0.01),
153                 BIP_RANGE(0.005),
154                 UNI_RANGE(10),
155                 UNI_RANGE(1),
156                 UNI_RANGE(0.1),
157                 UNI_RANGE(0.01)
158         }
159 };
160
161 static const struct comedi_lrange range_das08_pgm = {
162         9, {
163                 BIP_RANGE(10),
164                 BIP_RANGE(5),
165                 BIP_RANGE(0.5),
166                 BIP_RANGE(0.05),
167                 BIP_RANGE(0.01),
168                 UNI_RANGE(10),
169                 UNI_RANGE(1),
170                 UNI_RANGE(0.1),
171                 UNI_RANGE(0.01)
172         }
173 };                              /*
174                                    cio-das08jr.pdf
175
176                                    "das08/jr-ao"
177
178                                    0 a/d bits 0-3            unused
179                                    1 a/d bits 4-11           start 12 bit
180                                    2 eoc, mux                mux
181                                    3 di                      do
182                                    4 unused                  ao0_lsb
183                                    5 unused                  ao0_msb
184                                    6 unused                  ao1_lsb
185                                    7 unused                  ao1_msb
186
187                                  */
188
189 static const struct comedi_lrange *const das08_ai_lranges[] = {
190         [das08_pg_none]         = &range_unknown,
191         [das08_bipolar5]        = &range_bipolar5,
192         [das08_pgh]             = &range_das08_pgh,
193         [das08_pgl]             = &range_das08_pgl,
194         [das08_pgm]             = &range_das08_pgm,
195 };
196
197 static const int das08_pgh_gainlist[] = {
198         8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
199 };
200 static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
201 static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
202
203 static const int *const das08_gainlists[] = {
204         [das08_pg_none]         = NULL,
205         [das08_bipolar5]        = NULL,
206         [das08_pgh]             = das08_pgh_gainlist,
207         [das08_pgl]             = das08_pgl_gainlist,
208         [das08_pgm]             = das08_pgm_gainlist,
209 };
210
211 static int das08_ai_eoc(struct comedi_device *dev,
212                         struct comedi_subdevice *s,
213                         struct comedi_insn *insn,
214                         unsigned long context)
215 {
216         unsigned int status;
217
218         status = inb(dev->iobase + DAS08_STATUS_REG);
219         if ((status & DAS08_STATUS_AI_BUSY) == 0)
220                 return 0;
221         return -EBUSY;
222 }
223
224 static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
225                           struct comedi_insn *insn, unsigned int *data)
226 {
227         const struct das08_board_struct *thisboard = dev->board_ptr;
228         struct das08_private_struct *devpriv = dev->private;
229         int n;
230         int chan;
231         int range;
232         int lsb, msb;
233         int ret;
234
235         chan = CR_CHAN(insn->chanspec);
236         range = CR_RANGE(insn->chanspec);
237
238         /* clear crap */
239         inb(dev->iobase + DAS08_AI_LSB_REG);
240         inb(dev->iobase + DAS08_AI_MSB_REG);
241
242         /* set multiplexer */
243         /*  lock to prevent race with digital output */
244         spin_lock(&dev->spinlock);
245         devpriv->do_mux_bits &= ~DAS08_CONTROL_MUX_MASK;
246         devpriv->do_mux_bits |= DAS08_CONTROL_MUX(chan);
247         outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL_REG);
248         spin_unlock(&dev->spinlock);
249
250         if (devpriv->pg_gainlist) {
251                 /* set gain/range */
252                 range = CR_RANGE(insn->chanspec);
253                 outb(devpriv->pg_gainlist[range],
254                      dev->iobase + DAS08AO_GAIN_CONTROL);
255         }
256
257         for (n = 0; n < insn->n; n++) {
258                 /* clear over-range bits for 16-bit boards */
259                 if (thisboard->ai_nbits == 16)
260                         if (inb(dev->iobase + DAS08_AI_MSB_REG) & 0x80)
261                                 dev_info(dev->class_dev, "over-range\n");
262
263                 /* trigger conversion */
264                 outb_p(0, dev->iobase + DAS08_AI_TRIG_REG);
265
266                 ret = comedi_timeout(dev, s, insn, das08_ai_eoc, 0);
267                 if (ret)
268                         return ret;
269
270                 msb = inb(dev->iobase + DAS08_AI_MSB_REG);
271                 lsb = inb(dev->iobase + DAS08_AI_LSB_REG);
272                 if (thisboard->ai_encoding == das08_encode12) {
273                         data[n] = (lsb >> 4) | (msb << 4);
274                 } else if (thisboard->ai_encoding == das08_pcm_encode12) {
275                         data[n] = (msb << 8) + lsb;
276                 } else if (thisboard->ai_encoding == das08_encode16) {
277                         /* FPOS 16-bit boards are sign-magnitude */
278                         if (msb & 0x80)
279                                 data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
280                         else
281                                 data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
282                 } else {
283                         dev_err(dev->class_dev, "bug! unknown ai encoding\n");
284                         return -1;
285                 }
286         }
287
288         return n;
289 }
290
291 static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
292                           struct comedi_insn *insn, unsigned int *data)
293 {
294         data[0] = 0;
295         data[1] = DAS08_STATUS_DI(inb(dev->iobase + DAS08_STATUS_REG));
296
297         return insn->n;
298 }
299
300 static int das08_do_wbits(struct comedi_device *dev,
301                           struct comedi_subdevice *s,
302                           struct comedi_insn *insn,
303                           unsigned int *data)
304 {
305         struct das08_private_struct *devpriv = dev->private;
306
307         if (comedi_dio_update_state(s, data)) {
308                 /* prevent race with setting of analog input mux */
309                 spin_lock(&dev->spinlock);
310                 devpriv->do_mux_bits &= ~DAS08_DO_MASK;
311                 devpriv->do_mux_bits |= DAS08_OP(s->state);
312                 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL_REG);
313                 spin_unlock(&dev->spinlock);
314         }
315
316         data[1] = s->state;
317
318         return insn->n;
319 }
320
321 static int das08jr_di_rbits(struct comedi_device *dev,
322                             struct comedi_subdevice *s,
323                             struct comedi_insn *insn, unsigned int *data)
324 {
325         data[0] = 0;
326         data[1] = inb(dev->iobase + DAS08JR_DIO);
327
328         return insn->n;
329 }
330
331 static int das08jr_do_wbits(struct comedi_device *dev,
332                             struct comedi_subdevice *s,
333                             struct comedi_insn *insn,
334                             unsigned int *data)
335 {
336         if (comedi_dio_update_state(s, data))
337                 outb(s->state, dev->iobase + DAS08JR_DIO);
338
339         data[1] = s->state;
340
341         return insn->n;
342 }
343
344 static void das08_ao_set_data(struct comedi_device *dev,
345                               unsigned int chan, unsigned int data)
346 {
347         const struct das08_board_struct *thisboard = dev->board_ptr;
348         unsigned char lsb;
349         unsigned char msb;
350
351         lsb = data & 0xff;
352         msb = (data >> 8) & 0xff;
353         if (thisboard->is_jr) {
354                 outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
355                 outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
356                 /* load DACs */
357                 inb(dev->iobase + DAS08JR_DIO);
358         } else {
359                 outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
360                 outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
361                 /* load DACs */
362                 inb(dev->iobase + DAS08AO_AO_UPDATE);
363         }
364 }
365
366 static int das08_ao_insn_write(struct comedi_device *dev,
367                                struct comedi_subdevice *s,
368                                struct comedi_insn *insn,
369                                unsigned int *data)
370 {
371         unsigned int chan = CR_CHAN(insn->chanspec);
372         unsigned int val = s->readback[chan];
373         int i;
374
375         for (i = 0; i < insn->n; i++) {
376                 val = data[i];
377                 das08_ao_set_data(dev, chan, val);
378         }
379         s->readback[chan] = val;
380
381         return insn->n;
382 }
383
384 int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
385 {
386         const struct das08_board_struct *thisboard = dev->board_ptr;
387         struct das08_private_struct *devpriv = dev->private;
388         struct comedi_subdevice *s;
389         int ret;
390         int i;
391
392         dev->iobase = iobase;
393
394         dev->board_name = thisboard->name;
395
396         ret = comedi_alloc_subdevices(dev, 6);
397         if (ret)
398                 return ret;
399
400         s = &dev->subdevices[0];
401         /* ai */
402         if (thisboard->ai_nbits) {
403                 s->type = COMEDI_SUBD_AI;
404                 /* XXX some boards actually have differential
405                  * inputs instead of single ended.
406                  * The driver does nothing with arefs though,
407                  * so it's no big deal.
408                  */
409                 s->subdev_flags = SDF_READABLE | SDF_GROUND;
410                 s->n_chan = 8;
411                 s->maxdata = (1 << thisboard->ai_nbits) - 1;
412                 s->range_table = das08_ai_lranges[thisboard->ai_pg];
413                 s->insn_read = das08_ai_rinsn;
414                 devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
415         } else {
416                 s->type = COMEDI_SUBD_UNUSED;
417         }
418
419         s = &dev->subdevices[1];
420         /* ao */
421         if (thisboard->ao_nbits) {
422                 s->type = COMEDI_SUBD_AO;
423                 s->subdev_flags = SDF_WRITABLE;
424                 s->n_chan = 2;
425                 s->maxdata = (1 << thisboard->ao_nbits) - 1;
426                 s->range_table = &range_bipolar5;
427                 s->insn_write = das08_ao_insn_write;
428
429                 ret = comedi_alloc_subdev_readback(s);
430                 if (ret)
431                         return ret;
432
433                 /* initialize all channels to 0V */
434                 for (i = 0; i < s->n_chan; i++) {
435                         s->readback[i] = s->maxdata / 2;
436                         das08_ao_set_data(dev, i, s->readback[i]);
437                 }
438         } else {
439                 s->type = COMEDI_SUBD_UNUSED;
440         }
441
442         s = &dev->subdevices[2];
443         /* di */
444         if (thisboard->di_nchan) {
445                 s->type = COMEDI_SUBD_DI;
446                 s->subdev_flags = SDF_READABLE;
447                 s->n_chan = thisboard->di_nchan;
448                 s->maxdata = 1;
449                 s->range_table = &range_digital;
450                 s->insn_bits =
451                         thisboard->is_jr ? das08jr_di_rbits : das08_di_rbits;
452         } else {
453                 s->type = COMEDI_SUBD_UNUSED;
454         }
455
456         s = &dev->subdevices[3];
457         /* do */
458         if (thisboard->do_nchan) {
459                 s->type = COMEDI_SUBD_DO;
460                 s->subdev_flags = SDF_WRITABLE;
461                 s->n_chan = thisboard->do_nchan;
462                 s->maxdata = 1;
463                 s->range_table = &range_digital;
464                 s->insn_bits =
465                         thisboard->is_jr ? das08jr_do_wbits : das08_do_wbits;
466         } else {
467                 s->type = COMEDI_SUBD_UNUSED;
468         }
469
470         s = &dev->subdevices[4];
471         /* 8255 */
472         if (thisboard->i8255_offset != 0) {
473                 ret = subdev_8255_init(dev, s, NULL, thisboard->i8255_offset);
474                 if (ret)
475                         return ret;
476         } else {
477                 s->type = COMEDI_SUBD_UNUSED;
478         }
479
480         /* Counter subdevice (8254) */
481         s = &dev->subdevices[5];
482         if (thisboard->i8254_offset) {
483                 dev->pacer = comedi_8254_init(dev->iobase +
484                                               thisboard->i8254_offset,
485                                               0, I8254_IO8, 0);
486                 if (!dev->pacer)
487                         return -ENOMEM;
488
489                 comedi_8254_subdevice_init(s, dev->pacer);
490         } else {
491                 s->type = COMEDI_SUBD_UNUSED;
492         }
493
494         return 0;
495 }
496 EXPORT_SYMBOL_GPL(das08_common_attach);
497
498 static int __init das08_init(void)
499 {
500         return 0;
501 }
502 module_init(das08_init);
503
504 static void __exit das08_exit(void)
505 {
506 }
507 module_exit(das08_exit);
508
509 MODULE_AUTHOR("Comedi http://www.comedi.org");
510 MODULE_DESCRIPTION("Comedi low-level driver");
511 MODULE_LICENSE("GPL");