2 * comedi/drivers/das08.c
3 * comedi driver for common DAS08 support (used by ISA/PCI/PCMCIA drivers)
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>
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.
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.
23 * Description: DAS-08 compatible boards
24 * Devices: various, see das08_isa, das08_cs, and das08_pci drivers
25 * Author: Warren Jasper, ds, Frank Hess
26 * Updated: Fri, 31 Aug 2012 19:19:06 +0100
29 * This driver is used by the das08_isa, das08_cs, and das08_pci
30 * drivers to provide the common support for the DAS-08 hardware.
32 * The driver doesn't support asynchronous commands, since the
33 * cheap das08 hardware doesn't really support them.
36 #include <linux/module.h>
38 #include "../comedidev.h"
49 0 a/d bits 0-3 start 8 bit
50 1 a/d bits 4-11 start 12 bit
51 2 eoc, ip1-3, irq, mux op1-4, inte, mux
56 requires hard-wiring for async ai
62 #define DAS08_TRIG_12BIT 1
63 #define DAS08_STATUS 2
64 #define DAS08_EOC (1<<7)
65 #define DAS08_IRQ (1<<3)
66 #define DAS08_IP(x) (((x)>>4)&0x7)
67 #define DAS08_CONTROL 2
68 #define DAS08_MUX_MASK 0x7
69 #define DAS08_MUX(x) ((x) & DAS08_MUX_MASK)
70 #define DAS08_INTE (1<<3)
71 #define DAS08_DO_MASK 0xf0
72 #define DAS08_OP(x) (((x) << 4) & DAS08_DO_MASK)
80 1 a/d bits 4-11 start 12 bit
91 #define DAS08JR_AO_LSB(x) ((x) ? 6 : 4)
92 #define DAS08JR_AO_MSB(x) ((x) ? 7 : 5)
101 0 a/d bits 0-3 start 8 bit
102 1 a/d bits 4-11 start 12 bit
103 2 eoc, ip1-3, irq, mux op1-4, inte, mux
104 3 mux, gain status gain control
114 #define DAS08AO_GAIN_CONTROL 3
115 #define DAS08AO_GAIN_STATUS 3
117 #define DAS08AO_AO_LSB(x) ((x) ? 0xa : 8)
118 #define DAS08AO_AO_MSB(x) ((x) ? 0xb : 9)
119 #define DAS08AO_AO_UPDATE 8
121 /* gainlist same as _pgx_ below */
123 static const struct comedi_lrange range_das08_pgl = {
137 static const struct comedi_lrange range_das08_pgh = {
154 static const struct comedi_lrange range_das08_pgm = {
171 0 a/d bits 0-3 unused
172 1 a/d bits 4-11 start 12 bit
182 static const struct comedi_lrange *const das08_ai_lranges[] = {
190 static const int das08_pgh_gainlist[] = {
191 8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
193 static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
194 static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
196 static const int *const das08_gainlists[] = {
204 static int das08_ai_eoc(struct comedi_device *dev,
205 struct comedi_subdevice *s,
206 struct comedi_insn *insn,
207 unsigned long context)
211 status = inb(dev->iobase + DAS08_STATUS);
212 if ((status & DAS08_EOC) == 0)
217 static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
218 struct comedi_insn *insn, unsigned int *data)
220 const struct das08_board_struct *thisboard = comedi_board(dev);
221 struct das08_private_struct *devpriv = dev->private;
228 chan = CR_CHAN(insn->chanspec);
229 range = CR_RANGE(insn->chanspec);
232 inb(dev->iobase + DAS08_LSB);
233 inb(dev->iobase + DAS08_MSB);
235 /* set multiplexer */
236 /* lock to prevent race with digital output */
237 spin_lock(&dev->spinlock);
238 devpriv->do_mux_bits &= ~DAS08_MUX_MASK;
239 devpriv->do_mux_bits |= DAS08_MUX(chan);
240 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
241 spin_unlock(&dev->spinlock);
243 if (s->range_table->length > 1) {
245 range = CR_RANGE(insn->chanspec);
246 outb(devpriv->pg_gainlist[range],
247 dev->iobase + DAS08AO_GAIN_CONTROL);
250 for (n = 0; n < insn->n; n++) {
251 /* clear over-range bits for 16-bit boards */
252 if (thisboard->ai_nbits == 16)
253 if (inb(dev->iobase + DAS08_MSB) & 0x80)
254 dev_info(dev->class_dev, "over-range\n");
256 /* trigger conversion */
257 outb_p(0, dev->iobase + DAS08_TRIG_12BIT);
259 ret = comedi_timeout(dev, s, insn, das08_ai_eoc, 0);
263 msb = inb(dev->iobase + DAS08_MSB);
264 lsb = inb(dev->iobase + DAS08_LSB);
265 if (thisboard->ai_encoding == das08_encode12) {
266 data[n] = (lsb >> 4) | (msb << 4);
267 } else if (thisboard->ai_encoding == das08_pcm_encode12) {
268 data[n] = (msb << 8) + lsb;
269 } else if (thisboard->ai_encoding == das08_encode16) {
270 /* FPOS 16-bit boards are sign-magnitude */
272 data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
274 data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
276 comedi_error(dev, "bug! unknown ai encoding");
284 static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
285 struct comedi_insn *insn, unsigned int *data)
288 data[1] = DAS08_IP(inb(dev->iobase + DAS08_STATUS));
293 static int das08_do_wbits(struct comedi_device *dev,
294 struct comedi_subdevice *s,
295 struct comedi_insn *insn,
298 struct das08_private_struct *devpriv = dev->private;
300 if (comedi_dio_update_state(s, data)) {
301 /* prevent race with setting of analog input mux */
302 spin_lock(&dev->spinlock);
303 devpriv->do_mux_bits &= ~DAS08_DO_MASK;
304 devpriv->do_mux_bits |= DAS08_OP(s->state);
305 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
306 spin_unlock(&dev->spinlock);
314 static int das08jr_di_rbits(struct comedi_device *dev,
315 struct comedi_subdevice *s,
316 struct comedi_insn *insn, unsigned int *data)
319 data[1] = inb(dev->iobase + DAS08JR_DIO);
324 static int das08jr_do_wbits(struct comedi_device *dev,
325 struct comedi_subdevice *s,
326 struct comedi_insn *insn,
329 if (comedi_dio_update_state(s, data))
330 outb(s->state, dev->iobase + DAS08JR_DIO);
337 static void das08_ao_set_data(struct comedi_device *dev,
338 unsigned int chan, unsigned int data)
340 const struct das08_board_struct *thisboard = comedi_board(dev);
341 struct das08_private_struct *devpriv = dev->private;
346 msb = (data >> 8) & 0xff;
347 if (thisboard->is_jr) {
348 outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
349 outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
351 inb(dev->iobase + DAS08JR_DIO);
353 outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
354 outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
356 inb(dev->iobase + DAS08AO_AO_UPDATE);
358 devpriv->ao_readback[chan] = data;
361 static void das08_ao_initialize(struct comedi_device *dev,
362 struct comedi_subdevice *s)
367 data = s->maxdata / 2; /* should be about 0 volts */
368 for (n = 0; n < s->n_chan; n++)
369 das08_ao_set_data(dev, n, data);
372 static int das08_ao_winsn(struct comedi_device *dev,
373 struct comedi_subdevice *s,
374 struct comedi_insn *insn, unsigned int *data)
379 chan = CR_CHAN(insn->chanspec);
381 for (n = 0; n < insn->n; n++)
382 das08_ao_set_data(dev, chan, *data);
387 static int das08_ao_rinsn(struct comedi_device *dev,
388 struct comedi_subdevice *s,
389 struct comedi_insn *insn, unsigned int *data)
391 struct das08_private_struct *devpriv = dev->private;
395 chan = CR_CHAN(insn->chanspec);
397 for (n = 0; n < insn->n; n++)
398 data[n] = devpriv->ao_readback[chan];
403 static void i8254_initialize(struct comedi_device *dev)
405 const struct das08_board_struct *thisboard = comedi_board(dev);
406 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
407 unsigned int mode = I8254_MODE0 | I8254_BINARY;
410 for (i = 0; i < 3; ++i)
411 i8254_set_mode(i8254_iobase, 0, i, mode);
414 static int das08_counter_read(struct comedi_device *dev,
415 struct comedi_subdevice *s,
416 struct comedi_insn *insn, unsigned int *data)
418 const struct das08_board_struct *thisboard = comedi_board(dev);
419 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
420 int chan = insn->chanspec;
422 data[0] = i8254_read(i8254_iobase, 0, chan);
426 static int das08_counter_write(struct comedi_device *dev,
427 struct comedi_subdevice *s,
428 struct comedi_insn *insn, unsigned int *data)
430 const struct das08_board_struct *thisboard = comedi_board(dev);
431 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
432 int chan = insn->chanspec;
434 i8254_write(i8254_iobase, 0, chan, data[0]);
438 static int das08_counter_config(struct comedi_device *dev,
439 struct comedi_subdevice *s,
440 struct comedi_insn *insn, unsigned int *data)
442 const struct das08_board_struct *thisboard = comedi_board(dev);
443 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
444 int chan = insn->chanspec;
447 case INSN_CONFIG_SET_COUNTER_MODE:
448 i8254_set_mode(i8254_iobase, 0, chan, data[1]);
450 case INSN_CONFIG_8254_READ_STATUS:
451 data[1] = i8254_status(i8254_iobase, 0, chan);
460 int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
462 const struct das08_board_struct *thisboard = comedi_board(dev);
463 struct das08_private_struct *devpriv = dev->private;
464 struct comedi_subdevice *s;
467 dev->iobase = iobase;
469 dev->board_name = thisboard->name;
471 ret = comedi_alloc_subdevices(dev, 6);
475 s = &dev->subdevices[0];
477 if (thisboard->ai_nbits) {
478 s->type = COMEDI_SUBD_AI;
479 /* XXX some boards actually have differential
480 * inputs instead of single ended.
481 * The driver does nothing with arefs though,
482 * so it's no big deal.
484 s->subdev_flags = SDF_READABLE | SDF_GROUND;
486 s->maxdata = (1 << thisboard->ai_nbits) - 1;
487 s->range_table = das08_ai_lranges[thisboard->ai_pg];
488 s->insn_read = das08_ai_rinsn;
489 devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
491 s->type = COMEDI_SUBD_UNUSED;
494 s = &dev->subdevices[1];
496 if (thisboard->ao_nbits) {
497 s->type = COMEDI_SUBD_AO;
498 s->subdev_flags = SDF_WRITABLE;
500 s->maxdata = (1 << thisboard->ao_nbits) - 1;
501 s->range_table = &range_bipolar5;
502 s->insn_write = das08_ao_winsn;
503 s->insn_read = das08_ao_rinsn;
504 das08_ao_initialize(dev, s);
506 s->type = COMEDI_SUBD_UNUSED;
509 s = &dev->subdevices[2];
511 if (thisboard->di_nchan) {
512 s->type = COMEDI_SUBD_DI;
513 s->subdev_flags = SDF_READABLE;
514 s->n_chan = thisboard->di_nchan;
516 s->range_table = &range_digital;
518 thisboard->is_jr ? das08jr_di_rbits : das08_di_rbits;
520 s->type = COMEDI_SUBD_UNUSED;
523 s = &dev->subdevices[3];
525 if (thisboard->do_nchan) {
526 s->type = COMEDI_SUBD_DO;
527 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
528 s->n_chan = thisboard->do_nchan;
530 s->range_table = &range_digital;
532 thisboard->is_jr ? das08jr_do_wbits : das08_do_wbits;
534 s->type = COMEDI_SUBD_UNUSED;
537 s = &dev->subdevices[4];
539 if (thisboard->i8255_offset != 0) {
540 ret = subdev_8255_init(dev, s, NULL,
541 dev->iobase + thisboard->i8255_offset);
545 s->type = COMEDI_SUBD_UNUSED;
548 s = &dev->subdevices[5];
550 if (thisboard->i8254_offset != 0) {
551 s->type = COMEDI_SUBD_COUNTER;
552 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
555 s->insn_read = das08_counter_read;
556 s->insn_write = das08_counter_write;
557 s->insn_config = das08_counter_config;
558 i8254_initialize(dev);
560 s->type = COMEDI_SUBD_UNUSED;
565 EXPORT_SYMBOL_GPL(das08_common_attach);
567 static int __init das08_init(void)
571 module_init(das08_init);
573 static void __exit das08_exit(void)
576 module_exit(das08_exit);
578 MODULE_AUTHOR("Comedi http://www.comedi.org");
579 MODULE_DESCRIPTION("Comedi low-level driver");
580 MODULE_LICENSE("GPL");